Repository.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Core\FacturisRepository;
  3. class Repository
  4. {
  5. const URL = "https://api.facturis-online.ro/api/";
  6. const CONNECTTIMEOUT = "5";
  7. const TIMEOUT = "300";
  8. public $apiKey;
  9. public $username;
  10. public $password;
  11. public $fiscalCode;
  12. public function __construct($apiKey, $username, $password, $fiscalCode)
  13. {
  14. $this->apiKey = $apiKey;
  15. $this->username = $username;
  16. $this->password = $password;
  17. $this->fiscalCode = $fiscalCode;
  18. }
  19. private function getResponse($method, $action, $params = array())
  20. {
  21. if(
  22. empty($this->apiKey) ||
  23. empty($this->username) ||
  24. empty($this->password) ||
  25. empty($this->fiscalCode))
  26. {
  27. throw new \Exception('error_auth_data');
  28. }
  29. $this->connect();
  30. $fields = array();
  31. $fields['APIkey'] = $this->apiKey;
  32. $fields['u'] = $this->username;
  33. $fields['p'] = $this->password;
  34. $fields['c'] = $this->fiscalCode;
  35. $fields['met'] = $method;
  36. $fields['act'] = $action;
  37. $postFields = array_merge($fields, $params);
  38. curl_setopt($this->connection,CURLOPT_POSTFIELDS, "json=" . json_encode($postFields));
  39. \Core\Log\FileLog::write('Facturis Request: ' . json_encode($params), \Core\Log\FileLog::DATA);
  40. $response = curl_exec( $this->connection );
  41. $curl_error = curl_error($this->connection);
  42. $this->close();
  43. if($curl_error)
  44. {
  45. throw new \Exception($this->printException(json_encode($curl_error)));
  46. }
  47. $output = json_decode($response, true);
  48. if(isset($output['error']))
  49. {
  50. throw new \Exception($this->printException(json_encode($output['result'])));
  51. }
  52. if(isset($output['success']) && $output['success'] == '2000')
  53. {
  54. return $output['result'];
  55. }
  56. if(
  57. ($method == 'Stoc' && $action == 'Get') ||
  58. ($method == 'Comenzi' && $action == 'Ins') ||
  59. ($method == 'Proforme' && $action == 'Ins'))
  60. {
  61. return $output;
  62. }
  63. throw new \Exception($this->printException('err CURL'));
  64. }
  65. private function getResponseWithoutException($method, $action, $params = array())
  66. {
  67. $this->connect();
  68. $fields = array();
  69. $fields['APIkey'] = $this->apiKey;
  70. $fields['u'] = $this->username;
  71. $fields['p'] = $this->password;
  72. $fields['c'] = $this->fiscalCode;
  73. $fields['met'] = $method;
  74. $fields['act'] = $action;
  75. $postFields = array_merge($fields, $params);
  76. curl_setopt($this->connection,CURLOPT_POSTFIELDS, "json=" . json_encode($postFields));
  77. $response = curl_exec( $this->connection );
  78. $curl_error = curl_error($this->connection);
  79. $this->close();
  80. if($curl_error)
  81. {
  82. return array();
  83. }
  84. $output = json_decode($response, true);
  85. if(isset($output['error']))
  86. {
  87. return array();
  88. }
  89. if(isset($output['success']) && $output['success'] == '2000')
  90. {
  91. return $output['result'];
  92. }
  93. if(($method == 'Stoc' && $action == 'Get') || ($method == 'Comenzi' && $action == 'Ins'))
  94. {
  95. return $output;
  96. }
  97. return array();
  98. }
  99. private function connect()
  100. {
  101. $this->connection = curl_init();
  102. curl_setopt($this->connection, CURLOPT_URL, self::URL);
  103. curl_setopt($this->connection, CURLOPT_POST, true);
  104. curl_setopt($this->connection, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT);
  105. curl_setopt($this->connection, CURLOPT_RETURNTRANSFER, true);
  106. curl_setopt($this->connection, CURLOPT_HEADER, false);
  107. curl_setopt($this->connection, CURLOPT_TIMEOUT, self::TIMEOUT);
  108. curl_setopt($this->connection, CURLOPT_SSL_VERIFYHOST, 0);
  109. curl_setopt($this->connection, CURLOPT_SSL_VERIFYPEER, 0);
  110. }
  111. private function close()
  112. {
  113. curl_close($this->connection);
  114. }
  115. public function getPdl()
  116. {
  117. $response = $this->getResponseWithoutException("Pdl", "GetSelectForUser");
  118. return $response;
  119. }
  120. public function test()
  121. {
  122. $response = $this->getResponse("Pdl", "GetSelectForUser");
  123. }
  124. protected function getGestiuni($params)
  125. {
  126. $response = $this->getResponseWithoutException("Gestiuni", "GetSelect", $params);
  127. return $response;
  128. }
  129. protected function insertOrder($params)
  130. {
  131. return $this->getResponse("Comenzi", "Ins", $params);
  132. }
  133. protected function getProducts()
  134. {
  135. $response = $this->getResponse("Produse", "Get", array());
  136. return $response;
  137. }
  138. protected function insertProduct($product)
  139. {
  140. $response = $this->getResponse("Produse", "Ins", $product);
  141. return $response;
  142. }
  143. protected function insertProforma($params)
  144. {
  145. $response = $this->getResponse("Proforme", "Ins", $params);
  146. return $response;
  147. }
  148. protected function getCustomers()
  149. {
  150. $response = $this->getResponse("Clienti", "Get", array());
  151. return $response;
  152. }
  153. protected function getStockByStockFilter($params)
  154. {
  155. $response = $this->getResponse("Stoc", "Get", $params);
  156. return $response;
  157. }
  158. public function getPdlGestiuni()
  159. {
  160. $list = array();
  161. $pedl = $this->getPdl();
  162. if(!empty($pedl))
  163. {
  164. foreach($pedl as $pdl)
  165. {
  166. if(isset($pdl['id']))
  167. {
  168. $gestiuni = $this->getGestiuni(array("pdl_curent" => 0, "pdl_key" => $pdl['id']));
  169. if(!empty($gestiuni))
  170. {
  171. foreach($gestiuni as $gestiune)
  172. {
  173. if(isset($pdl['name']) && isset($gestiune['name']))
  174. {
  175. $list[] = array(
  176. 'id' => str_replace(" ", "_", $pdl['name']) . '__' . str_replace(" ", "_", $gestiune['name']),
  177. 'name' => $pdl['name'] . ' - ' . $gestiune['name']
  178. );
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. return $list;
  186. }
  187. public function printException($message)
  188. {
  189. return 'Err Facturis: ' . $message . ' ';
  190. }
  191. }