Repository.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 = "500";
  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, JSON_PARTIAL_OUTPUT_ON_ERROR));
  39. curl_setopt($this->connection, CURLOPT_SSL_VERIFYHOST, 0);
  40. curl_setopt($this->connection, CURLOPT_SSL_VERIFYPEER, 0);
  41. \Core\Log\FileLog::write('Facturis Request: ' . json_encode($params), \Core\Log\FileLog::DATA);
  42. $response = curl_exec( $this->connection );
  43. $curl_error = curl_error($this->connection);
  44. $this->close();
  45. if($curl_error)
  46. {
  47. throw new \Exception($this->printException(json_encode($curl_error)));
  48. }
  49. if ($action == 'Facturi' && $method == 'FacturiHtml') {
  50. header('Content-type: application/pdf');
  51. header('Content-Transfer-Encoding: binary');
  52. header('Accept-Ranges: bytes');
  53. echo $response;
  54. exit;
  55. }
  56. $output = json_decode($response, true);
  57. if(isset($output['error']))
  58. {
  59. throw new \Exception($this->printException(json_encode($output['result'])));
  60. }
  61. if(isset($output['success']) && $output['success'] == '2000')
  62. {
  63. return $output['result'];
  64. }
  65. if(
  66. ($method == 'Stoc' && $action == 'Get') ||
  67. ($method == 'Comenzi' && $action == 'Ins') ||
  68. ($method == 'Proforme' && $action == 'Ins') ||
  69. ($method == 'Facturi' && $action == 'Ins')) {
  70. return $output;
  71. }
  72. throw new \Exception($this->printException('err CURL'));
  73. }
  74. private function getResponseWithoutException($method, $action, $params = array())
  75. {
  76. $this->connect();
  77. $fields = array();
  78. $fields['APIkey'] = $this->apiKey;
  79. $fields['u'] = $this->username;
  80. $fields['p'] = $this->password;
  81. $fields['c'] = $this->fiscalCode;
  82. $fields['met'] = $method;
  83. $fields['act'] = $action;
  84. $postFields = array_merge($fields, $params);
  85. curl_setopt($this->connection, CURLOPT_POSTFIELDS, "json=" . json_encode($postFields, JSON_PARTIAL_OUTPUT_ON_ERROR));
  86. curl_setopt($this->connection, CURLOPT_SSL_VERIFYHOST, 0);
  87. curl_setopt($this->connection, CURLOPT_SSL_VERIFYPEER, 0);
  88. $response = curl_exec( $this->connection );
  89. $curl_error = curl_error($this->connection);
  90. $this->close();
  91. if($curl_error)
  92. {
  93. return array();
  94. }
  95. $output = json_decode($response, true);
  96. if(isset($output['error']))
  97. {
  98. return array();
  99. }
  100. if(isset($output['success']) && $output['success'] == '2000')
  101. {
  102. return $output['result'];
  103. }
  104. if(($method == 'Stoc' && $action == 'Get') || ($method == 'Comenzi' && $action == 'Ins'))
  105. {
  106. return $output;
  107. }
  108. return array();
  109. }
  110. private function connect()
  111. {
  112. $this->connection = curl_init();
  113. curl_setopt($this->connection, CURLOPT_URL, self::URL);
  114. curl_setopt($this->connection, CURLOPT_POST, true);
  115. curl_setopt($this->connection, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT);
  116. curl_setopt($this->connection, CURLOPT_RETURNTRANSFER, true);
  117. curl_setopt($this->connection, CURLOPT_HEADER, false);
  118. curl_setopt($this->connection, CURLOPT_TIMEOUT, self::TIMEOUT);
  119. }
  120. private function close()
  121. {
  122. curl_close($this->connection);
  123. }
  124. public function getPdl()
  125. {
  126. $response = $this->getResponseWithoutException("Pdl", "GetSelectForUser");
  127. return $response;
  128. }
  129. public function test()
  130. {
  131. $response = $this->getResponse("Pdl", "GetSelectForUser");
  132. }
  133. protected function getGestiuni($params)
  134. {
  135. $response = $this->getResponseWithoutException("Gestiuni", "GetSelect", $params);
  136. return $response;
  137. }
  138. protected function insertOrder($params)
  139. {
  140. return $this->getResponse("Comenzi", "Ins", $params);
  141. }
  142. protected function getProducts()
  143. {
  144. $response = $this->getResponse("Produse", "Get", array());
  145. return $response;
  146. }
  147. protected function insertProduct($product)
  148. {
  149. $response = $this->getResponse("Produse", "Ins", $product);
  150. return $response;
  151. }
  152. protected function insertProforma($params)
  153. {
  154. $response = $this->getResponse("Proforme", "Ins", $params);
  155. return $response;
  156. }
  157. protected function insertAvize($params)
  158. {
  159. $response = $this->getResponse("Avize", "Ins", $params);
  160. return $response;
  161. }
  162. protected function insertExpfactura($params)
  163. {
  164. $response = $this->getResponse("Facturi", "Ins", $params);
  165. return $response;
  166. }
  167. protected function updateProforma($params)
  168. {
  169. $response = $this->getResponse("Proforme", "Upd", $params);
  170. return $response;
  171. }
  172. protected function updateAvize($params)
  173. {
  174. $response = $this->getResponse("Avize", "Upd", $params);
  175. return $response;
  176. }
  177. protected function updateExpfactura($params)
  178. {
  179. $response = $this->getResponse("Facturi", "Upd", $params);
  180. return $response;
  181. }
  182. protected function checkProforma($params)
  183. {
  184. $response = $this->getResponse("Proforme", "GetProformaIdForShop", $params);
  185. return $response;
  186. }
  187. protected function checkAvize($params)
  188. {
  189. $response = $this->getResponse("Avize", "GetAvizeIdForShop", $params);
  190. return $response;
  191. }
  192. protected function checkExpfactura($params)
  193. {
  194. $response = $this->getResponse("Facturi", "GetFacturiIdForShop", $params);
  195. return $response;
  196. }
  197. protected function insertInvoice($params)
  198. {
  199. $response = $this->getResponse("Facturi", "Ins", $params);
  200. return $response;
  201. }
  202. protected function getCustomers()
  203. {
  204. $response = $this->getResponse("Clienti", "Get", array());
  205. return $response;
  206. }
  207. protected function getStockByStockFilter($params)
  208. {
  209. $response = $this->getResponse("Stoc", "Get", $params);
  210. return $response;
  211. }
  212. protected function viewProforma($params)
  213. {
  214. $this->getResponse("FacturiHtml", "Facturi", $params);
  215. }
  216. protected function viewAvize($params)
  217. {
  218. $this->getResponse("FacturiHtml", "Avize", $params);
  219. }
  220. protected function viewExpfactura($params)
  221. {
  222. $this->getResponse("FacturiHtml", "Expfactura", $params);
  223. }
  224. protected function viewInvoice($params)
  225. {
  226. $this->getResponse("FacturiHtml", "Facturi", $params);
  227. }
  228. public function getPdlGestiuni()
  229. {
  230. $list = array();
  231. $pedl = $this->getPdl();
  232. if(!empty($pedl))
  233. {
  234. foreach($pedl as $pdl)
  235. {
  236. if(isset($pdl['id']))
  237. {
  238. $gestiuni = $this->getGestiuni(array("pdl_curent" => 0, "pdl_key" => $pdl['id']));
  239. if(!empty($gestiuni))
  240. {
  241. foreach($gestiuni as $gestiune)
  242. {
  243. if (isset($pdl['name']) && isset($gestiune['name'])) {
  244. $id = str_replace(" ", "_", $pdl['name']);
  245. $id .= '__' . str_replace(" ", "_", $gestiune['name']);
  246. $list[] = array(
  247. 'id' => $id,
  248. 'name' => $pdl['name'] . ' - ' . $gestiune['name']
  249. );
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. return $list;
  257. }
  258. public function getPdls()
  259. {
  260. $list = array();
  261. $pedl = $this->getPdl();
  262. if (!empty($pedl)) {
  263. foreach ($pedl as $pdl) {
  264. if (isset($pdl['id'])) {
  265. $list[] = array(
  266. 'id' => $pdl['id'],
  267. 'name' => $pdl['name']
  268. );
  269. }
  270. }
  271. }
  272. return $list;
  273. }
  274. public function printException($message)
  275. {
  276. return 'Err Facturis: ' . $message . ' ';
  277. }
  278. }