123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace Core\FacturisRepository;
- class Repository
- {
- const URL = "https://api.facturis-online.ro/api/";
- const CONNECTTIMEOUT = "5";
- const TIMEOUT = "500";
- 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, 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 . ' ';
- }
- }
|