123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace Core\FacturisRepository;
- class Repository
- {
- const URL = "https://api.facturis-online.ro/api/";
- const CONNECTTIMEOUT = "5";
- const TIMEOUT = "300";
- public $apiKey;
- public $username;
- public $password;
- public $fiscalCode;
- public function __construct($apiKey, $username, $password, $fiscalCode)
- {
- $this->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 . ' ';
- }
- }
|