apiKey = $apiKey; $this->username = $username; $this->password = $password; $this->fiscalCode = $fiscalCode; } private function getResponse($method, $action, $params = array()) { if( empty($this->apiKey) || empty($this->username) || empty($this->password) || empty($this->fiscalCode)) { throw new \Exception('error_auth_data'); } $this->connect(); $fields = array(); $fields['APIkey'] = $this->apiKey; $fields['u'] = $this->username; $fields['p'] = $this->password; $fields['c'] = $this->fiscalCode; $fields['met'] = $method; $fields['act'] = $action; $postFields = array_merge($fields, $params); curl_setopt($this->connection, CURLOPT_POSTFIELDS, "json=" . json_encode($postFields, JSON_PARTIAL_OUTPUT_ON_ERROR)); curl_setopt($this->connection, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->connection, CURLOPT_SSL_VERIFYPEER, 0); \Core\Log\FileLog::write('Facturis Request: ' . json_encode($params), \Core\Log\FileLog::DATA); $response = curl_exec( $this->connection ); $curl_error = curl_error($this->connection); $this->close(); if($curl_error) { throw new \Exception($this->printException(json_encode($curl_error))); } if ($action == 'Facturi' && $method == 'FacturiHtml') { header('Content-type: application/pdf'); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); echo $response; exit; } $output = json_decode($response, true); if(isset($output['error'])) { throw new \Exception($this->printException(json_encode($output['result']))); } if(isset($output['success']) && $output['success'] == '2000') { return $output['result']; } if( ($method == 'Stoc' && $action == 'Get') || ($method == 'Comenzi' && $action == 'Ins') || ($method == 'Proforme' && $action == 'Ins') || ($method == 'Facturi' && $action == 'Ins')) { return $output; } throw new \Exception($this->printException('err CURL')); } private function getResponseWithoutException($method, $action, $params = array()) { $this->connect(); $fields = array(); $fields['APIkey'] = $this->apiKey; $fields['u'] = $this->username; $fields['p'] = $this->password; $fields['c'] = $this->fiscalCode; $fields['met'] = $method; $fields['act'] = $action; $postFields = array_merge($fields, $params); curl_setopt($this->connection, CURLOPT_POSTFIELDS, "json=" . json_encode($postFields, JSON_PARTIAL_OUTPUT_ON_ERROR)); curl_setopt($this->connection, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->connection, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec( $this->connection ); $curl_error = curl_error($this->connection); $this->close(); if($curl_error) { return array(); } $output = json_decode($response, true); if(isset($output['error'])) { return array(); } if(isset($output['success']) && $output['success'] == '2000') { return $output['result']; } if(($method == 'Stoc' && $action == 'Get') || ($method == 'Comenzi' && $action == 'Ins')) { return $output; } return array(); } private function connect() { $this->connection = curl_init(); curl_setopt($this->connection, CURLOPT_URL, self::URL); curl_setopt($this->connection, CURLOPT_POST, true); curl_setopt($this->connection, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT); curl_setopt($this->connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->connection, CURLOPT_HEADER, false); curl_setopt($this->connection, CURLOPT_TIMEOUT, self::TIMEOUT); } private function close() { curl_close($this->connection); } public function getPdl() { $response = $this->getResponseWithoutException("Pdl", "GetSelectForUser"); return $response; } public function test() { $response = $this->getResponse("Pdl", "GetSelectForUser"); } protected function getGestiuni($params) { $response = $this->getResponseWithoutException("Gestiuni", "GetSelect", $params); return $response; } protected function insertOrder($params) { return $this->getResponse("Comenzi", "Ins", $params); } protected function getProducts() { $response = $this->getResponse("Produse", "Get", array()); return $response; } protected function insertProduct($product) { $response = $this->getResponse("Produse", "Ins", $product); return $response; } protected function insertProforma($params) { $response = $this->getResponse("Proforme", "Ins", $params); return $response; } protected function insertAvize($params) { $response = $this->getResponse("Avize", "Ins", $params); return $response; } protected function insertExpfactura($params) { $response = $this->getResponse("Facturi", "Ins", $params); return $response; } protected function updateProforma($params) { $response = $this->getResponse("Proforme", "Upd", $params); return $response; } protected function updateAvize($params) { $response = $this->getResponse("Avize", "Upd", $params); return $response; } protected function updateExpfactura($params) { $response = $this->getResponse("Facturi", "Upd", $params); return $response; } protected function checkProforma($params) { $response = $this->getResponse("Proforme", "GetProformaIdForShop", $params); return $response; } protected function checkAvize($params) { $response = $this->getResponse("Avize", "GetAvizeIdForShop", $params); return $response; } protected function checkExpfactura($params) { $response = $this->getResponse("Facturi", "GetFacturiIdForShop", $params); return $response; } protected function insertInvoice($params) { $response = $this->getResponse("Facturi", "Ins", $params); return $response; } protected function getCustomers() { $response = $this->getResponse("Clienti", "Get", array()); return $response; } protected function getStockByStockFilter($params) { $response = $this->getResponse("Stoc", "Get", $params); return $response; } protected function viewProforma($params) { $this->getResponse("FacturiHtml", "Facturi", $params); } protected function viewAvize($params) { $this->getResponse("FacturiHtml", "Avize", $params); } protected function viewExpfactura($params) { $this->getResponse("FacturiHtml", "Expfactura", $params); } protected function viewInvoice($params) { $this->getResponse("FacturiHtml", "Facturi", $params); } public function getPdlGestiuni() { $list = array(); $pedl = $this->getPdl(); if(!empty($pedl)) { foreach($pedl as $pdl) { if(isset($pdl['id'])) { $gestiuni = $this->getGestiuni(array("pdl_curent" => 0, "pdl_key" => $pdl['id'])); if(!empty($gestiuni)) { foreach($gestiuni as $gestiune) { if (isset($pdl['name']) && isset($gestiune['name'])) { $id = str_replace(" ", "_", $pdl['name']); $id .= '__' . str_replace(" ", "_", $gestiune['name']); $list[] = array( 'id' => $id, 'name' => $pdl['name'] . ' - ' . $gestiune['name'] ); } } } } } } return $list; } public function getPdls() { $list = array(); $pedl = $this->getPdl(); if (!empty($pedl)) { foreach ($pedl as $pdl) { if (isset($pdl['id'])) { $list[] = array( 'id' => $pdl['id'], 'name' => $pdl['name'] ); } } } return $list; } public function printException($message) { return 'Err Facturis: ' . $message . ' '; } }