ntaksender.class.php_ORIG 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntakinvoice.class.php';
  3. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntakconnector/ntak_client.class.php';
  4. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntaklogger.class.php';
  5. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  6. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntaksendresult.class.php';
  7. require_once DOL_DOCUMENT_ROOT.'/custom/multicompany/class/dao_multicompany.class.php';
  8. class NtakSender
  9. {
  10. public $db;
  11. private $unsentInvoices;
  12. private $validatedTickets;
  13. private $notSuccesses;
  14. private NtakLogger $ntakLogger;
  15. private $testInvoices = [
  16. //'RR2311-000283',
  17. //'RR2311-000282',
  18. //'RR2311-000283',
  19. // ...
  20. //'BBH2401-000025',
  21. ];
  22. public function __construct()
  23. {
  24. global $db;
  25. $this->db = $db;
  26. $this->ntakLogger = NtakLogger::getInstance();
  27. }
  28. /**
  29. *
  30. */
  31. public function checkNtakStatus(): bool
  32. {
  33. $daoMulticompany = new DaoMulticompany($this->db);
  34. $daoMulticompany->getEntities();
  35. if (empty($daoMulticompany->entities)) {
  36. $this->ntakLogger->logIntoFile('Missing entities', LOG_ERR);
  37. } else {
  38. foreach ($daoMulticompany->entities as $entity) {
  39. $this->loadNtakStatuses($entity->id);
  40. }
  41. }
  42. return true;
  43. }
  44. /**
  45. *
  46. */
  47. public function loadNtakStatuses(int $entity): void
  48. {
  49. $sql = "
  50. SELECT f.rowid, f.ref, e.ntak_id, e.ntak_status
  51. FROM llx_facture AS f
  52. LEFT JOIN llx_facture_extrafields AS e ON e.fk_object=f.rowid
  53. WHERE e.ntak_status ILIKE 'BEFOGADVA'
  54. AND f.entity={$entity}
  55. ORDER BY f.datec DESC
  56. ";
  57. $rows = $this->db->query($sql);
  58. if ($rows) {
  59. while ($row = $this->db->fetch_object($rows)) {
  60. $ntakResult = $this->checkSentInvoice($row->ntak_id, $row->rowid);
  61. if (!empty($ntakResult['uzenetValaszok'][0]['statusz'])) {
  62. $invoice = new Facture($this->db);
  63. if ($invoice->fetch($row->rowid) > 0) {
  64. $invoice->array_options['options_ntak_status'] = $ntakResult['uzenetValaszok'][0]['statusz'];
  65. $invoice->updateExtraField('ntak_status');
  66. }
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. *
  73. */
  74. public function send(): bool
  75. {
  76. $this->ntakLogger->logIntoFile('Start sending', LOG_INFO);
  77. $daoMulticompany = new DaoMulticompany($this->db);
  78. $daoMulticompany->getEntities();
  79. if (empty($daoMulticompany->entities)) {
  80. $this->ntakLogger->logIntoFile('Missing entities', LOG_ERR);
  81. } else {
  82. foreach ($daoMulticompany->entities as $entity) {
  83. for ($i = 0; $i < 10; $i++) {
  84. $this->massSending($entity->id);
  85. }
  86. }
  87. }
  88. $this->ntakLogger->logIntoFile('Finish sending', LOG_INFO);
  89. return true;
  90. }
  91. /**
  92. *
  93. */
  94. private function massSending(int $entityId): void
  95. {
  96. $this->setUnsentInvoices($entityId);
  97. if (!empty($this->unsentInvoices)) {
  98. $ntakClient = NtakClient::getInstance();
  99. $massData = [
  100. 'programAdatok' => [],
  101. 'tranzakcioAdatok' => []
  102. ];
  103. $invoices = [];
  104. foreach ($this->unsentInvoices as $unsentInvoiceData) {
  105. $invoice = new NtakInvoice($this->db);
  106. if ($invoice->fetch($unsentInvoiceData->rowid) > 0) {
  107. $ntakSendResult = $ntakClient->send($invoice, false);
  108. if (empty($massData['tranzakcioAdatok'])) {
  109. $massData = $ntakSendResult->generatedData;
  110. } else {
  111. if (!empty($ntakSendResult)) {
  112. if (!empty($ntakSendResult->generatedData['programAdatok'])) {
  113. // filter
  114. foreach ($ntakSendResult->generatedData['programAdatok'] as $programData) {
  115. $ref = $programData['tssProgramAzonosito'];
  116. $exists = array_filter($massData['programAdatok'], function ($item) use ($ref) {
  117. return ($item['tssProgramAzonosito'] === $ref);
  118. });
  119. if (empty($exists)) {
  120. $massData['programAdatok'][] = $programData;
  121. }
  122. }
  123. //$massData['programAdatok'] = array_merge($massData['programAdatok'], $ntakSendResult->generatedData['programAdatok']);
  124. }
  125. if (!empty($ntakSendResult->generatedData['tranzakcioAdatok'])) {
  126. $massData['tranzakcioAdatok'] = array_merge($massData['tranzakcioAdatok'], $ntakSendResult->generatedData['tranzakcioAdatok']);
  127. }
  128. }
  129. }
  130. $invoices[] = $invoice;
  131. }
  132. }
  133. if (!empty($massData['tranzakcioAdatok'])) {
  134. $ntakSendResult = $ntakClient->sendToServer($massData);
  135. if (!empty($ntakSendResult->ntakId)) {
  136. foreach ($invoices as $inv) {
  137. $inv->array_options['options_ntak_id'] = $ntakSendResult->ntakId;
  138. $inv->updateExtraField('ntak_id');
  139. $inv->array_options['options_ntak_status'] = $ntakSendResult->ntakStatus;
  140. $inv->updateExtraField('ntak_status');
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. *
  148. */
  149. public function checkSentInvoice(string $ntakId, int $invoiceId): array
  150. {
  151. return (NtakClient::getInstance())->checkWithLog($ntakId, $invoiceId);
  152. }
  153. /**
  154. *
  155. */
  156. public function sendByInvoice(Facture $facture): ?NtakSendResult
  157. {
  158. $ntakSendResult = null;
  159. $this->ntakLogger->logIntoFile('Send invoice by ID ('.$facture->id.')', LOG_INFO);
  160. $ntakClient = NtakClient::getInstance();
  161. $massData = [
  162. 'programAdatok' => [],
  163. 'tranzakcioAdatok' => []
  164. ];
  165. $invoices = [];
  166. $invoice = new NtakInvoice($this->db);
  167. if ($invoice->fetch($facture->id) > 0) {
  168. $ntakSendResult = $ntakClient->send($invoice, false);
  169. if (empty($massData['tranzakcioAdatok'])) {
  170. $massData = $ntakSendResult->generatedData;
  171. } else {
  172. if (!empty($ntakSendResult)) {
  173. if (!empty($ntakSendResult->generatedData['programAdatok'])) {
  174. // filter
  175. foreach ($ntakSendResult->generatedData['programAdatok'] as $programData) {
  176. $ref = $programData['tssProgramAzonosito'];
  177. $exists = array_filter($massData['programAdatok'], function ($item) use ($ref) {
  178. return ($item['tssProgramAzonosito'] === $ref);
  179. });
  180. if (empty($exists)) {
  181. $massData['programAdatok'][] = $programData;
  182. }
  183. }
  184. //$massData['programAdatok'] = array_merge($massData['programAdatok'], $ntakSendResult->generatedData['programAdatok']);
  185. }
  186. if (!empty($ntakSendResult->generatedData['tranzakcioAdatok'])) {
  187. $massData['tranzakcioAdatok'] = array_merge($massData['tranzakcioAdatok'], $ntakSendResult->generatedData['tranzakcioAdatok']);
  188. }
  189. }
  190. }
  191. $invoices[] = $invoice;
  192. }
  193. if (!empty($massData['tranzakcioAdatok'])) {
  194. $ntakSendResult = $ntakClient->sendToServer($massData);
  195. if (!empty($ntakSendResult->ntakId)) {
  196. foreach ($invoices as $inv) {
  197. $inv->array_options['options_ntak_id'] = $ntakSendResult->ntakId;
  198. $inv->updateExtraField('ntak_id');
  199. $inv->array_options['options_ntak_status'] = $ntakSendResult->ntakStatus;
  200. $inv->updateExtraField('ntak_status');
  201. }
  202. }
  203. }
  204. /*
  205. $invoice = new NtakInvoice($this->db);
  206. if ($invoice->fetch($facture->id) > 0) {
  207. $invoice->loadPayments();
  208. // pass invoice to sender
  209. $ntakSendResult = (NtakClient::getInstance())->send($invoice);
  210. }
  211. $this->ntakLogger->logIntoFile('Send invoice finish', LOG_INFO);
  212. */
  213. return $ntakSendResult;
  214. }
  215. /**
  216. *
  217. */
  218. private function setUnsentInvoices(int $entityId): void
  219. {
  220. $this->ntakLogger->logIntoFile('Get unsent invoices from DB', LOG_INFO);
  221. $this->unsentInvoices = [];
  222. $testCondition = '';
  223. if (!empty($this->testInvoices)) {
  224. $testCondition = " AND f.ref IN ('".implode("','", $this->testInvoices)."') ";
  225. }
  226. /*
  227. $sql = "
  228. SELECT f.rowid, f.ref
  229. FROM ".$this->db->prefix()."facture AS f
  230. WHERE f.date_valid IS NOT NULL
  231. AND NOT EXISTS (
  232. SELECT 1
  233. FROM ".$this->db->prefix()."ntak_ntaklog AS l
  234. WHERE l.status = 1
  235. AND l.fk_facture = f.rowid
  236. )
  237. {$testCondition}
  238. ORDER BY f.datec ASC
  239. ";
  240. */
  241. /*
  242. $sql = "
  243. SELECT f.rowid, f.ref
  244. FROM ".$this->db->prefix()."facture AS f
  245. LEFT JOIN ".$this->db->prefix()."facture_extrafields AS e ON e.fk_object=f.rowid
  246. WHERE f.date_valid IS NOT NULL
  247. AND (e.ntak_status NOT ILIKE 'BEFOGADVA' OR e.ntak_status IS NULL)
  248. AND f.entity={$entityId}
  249. {$testCondition}
  250. ORDER BY f.datec DESC LIMIT 10
  251. ";
  252. */
  253. $sql = "
  254. SELECT t.rowid, t.ref, t.prefs
  255. FROM (
  256. SELECT f.rowid, f.ref, STRING_AGG(p.ref, ', ') AS prefs
  257. FROM ".$this->db->prefix()."facture AS f
  258. LEFT JOIN ".$this->db->prefix()."facture_extrafields AS e ON e.fk_object=f.rowid
  259. LEFT JOIN ".$this->db->prefix()."facturedet AS fd ON fd.fk_facture=f.rowid
  260. LEFT JOIN ".$this->db->prefix()."product AS p ON p.rowid=fd.fk_product
  261. WHERE f.date_valid IS NOT NULL
  262. AND (e.ntak_status NOT ILIKE 'BEFOGADVA' OR e.ntak_status IS NULL)
  263. AND f.entity={$entityId}
  264. AND f.multicurrency_total_ttc>0
  265. {$testCondition}
  266. GROUP BY f.rowid, f.ref
  267. ORDER BY f.datec DESC
  268. ) AS t
  269. WHERE (t.prefs ILIKE '%,%' OR t.prefs NOT ILIKE '%card%')
  270. ";
  271. $rows = $this->db->query($sql);
  272. if ($rows) {
  273. while ($row = $this->db->fetch_object($rows)) {
  274. $this->unsentInvoices[] = $row;
  275. }
  276. }
  277. $numOfInvoices = count($this->unsentInvoices);
  278. $logMsg = 'There is no unsent invoice';
  279. if ($numOfInvoices > 0) {
  280. $logMsg = 'There are '.$numOfInvoices.' unsent invoices';
  281. }
  282. $this->ntakLogger->logIntoFile($logMsg, LOG_INFO);
  283. }
  284. /**
  285. *
  286. */
  287. public function processValidatedTickets(): void
  288. {
  289. $this->ntakLogger->logIntoFile('Send ticket to validate', LOG_INFO);
  290. $ntakClient = NtakClient::getInstance();
  291. array_walk(
  292. $this->validatedTickets,
  293. function ($ticket) use ($ntakClient) {
  294. $ntakClient->validate(
  295. $ticket->ticket_log_id,
  296. $ticket->fact_ref,
  297. $ticket->ntak_id,
  298. $ticket->prod_ref,
  299. $ticket->prod_date,
  300. $ticket->log_date,
  301. $ticket->fact_id
  302. );
  303. }
  304. );
  305. $this->ntakLogger->logIntoFile('Ticket validated', LOG_INFO);
  306. }
  307. }