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)); \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))); } $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')) { 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)); $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); curl_setopt($this->connection, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->connection, CURLOPT_SSL_VERIFYPEER, 0); } 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 getCustomers() { $response = $this->getResponse("Clienti", "Get", array()); return $response; } protected function getStockByStockFilter($params) { $response = $this->getResponse("Stoc", "Get", $params); return $response; } 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'])) { $list[] = array( 'id' => str_replace(" ", "_", $pdl['name']) . '__' . str_replace(" ", "_", $gestiune['name']), 'name' => $pdl['name'] . ' - ' . $gestiune['name'] ); } } } } } } return $list; } public function printException($message) { return 'Err Facturis: ' . $message . ' '; } }