ntaksender.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. global $conf;
  77. $this->ntakLogger->logIntoFile('Start sending', LOG_INFO);
  78. $this->ntakLogger->logIntoFile('Entity:' . $conf->entity, LOG_INFO);
  79. return true;
  80. exit;
  81. $daoMulticompany = new DaoMulticompany($this->db);
  82. $daoMulticompany->getEntities();
  83. if (empty($daoMulticompany->entities)) {
  84. $this->ntakLogger->logIntoFile('Missing entities', LOG_ERR);
  85. } else {
  86. foreach ($daoMulticompany->entities as $entity) {
  87. for ($i = 0; $i < 10; $i++) {
  88. $this->massSending($entity->id);
  89. }
  90. }
  91. }
  92. $this->ntakLogger->logIntoFile('Finish sending', LOG_INFO);
  93. return true;
  94. }
  95. public function send2(): bool
  96. {
  97. global $conf;
  98. $this->ntakLogger->logIntoFile('Start sending', LOG_INFO);
  99. $this->ntakLogger->logIntoFile('Entity:' . $conf->entity, LOG_INFO);
  100. //exit;
  101. if (empty($conf->entity)) {
  102. $this->ntakLogger->logIntoFile('Missing entities', LOG_ERR);
  103. } else {
  104. //for ($i = 0; $i < 10; $i++) {
  105. $this->massSending($conf->entity);
  106. //}
  107. }
  108. $this->ntakLogger->logIntoFile('Finish sending', LOG_INFO);
  109. return true;
  110. }
  111. /**
  112. *
  113. */
  114. private function massSending(int $entityId): void
  115. {
  116. $this->setUnsentInvoices($entityId);
  117. if (!empty($this->unsentInvoices)) {
  118. $ntakClient = NtakClient::getInstance();
  119. $massData = [
  120. 'programAdatok' => [],
  121. 'tranzakcioAdatok' => []
  122. ];
  123. $invoices = [];
  124. foreach ($this->unsentInvoices as $unsentInvoiceData) {
  125. $invoice = new NtakInvoice($this->db);
  126. if ($invoice->fetch($unsentInvoiceData->rowid) > 0) {
  127. $ntakSendResult = $ntakClient->send($invoice, false);
  128. if (empty($massData['tranzakcioAdatok'])) {
  129. $massData = $ntakSendResult->generatedData;
  130. } else {
  131. if (!empty($ntakSendResult)) {
  132. if (!empty($ntakSendResult->generatedData['programAdatok'])) {
  133. // filter
  134. foreach ($ntakSendResult->generatedData['programAdatok'] as $programData) {
  135. $ref = $programData['tssProgramAzonosito'];
  136. $exists = array_filter($massData['programAdatok'], function ($item) use ($ref) {
  137. return ($item['tssProgramAzonosito'] === $ref);
  138. });
  139. if (empty($exists)) {
  140. $massData['programAdatok'][] = $programData;
  141. }
  142. }
  143. //$massData['programAdatok'] = array_merge($massData['programAdatok'], $ntakSendResult->generatedData['programAdatok']);
  144. }
  145. if (!empty($ntakSendResult->generatedData['tranzakcioAdatok'])) {
  146. $massData['tranzakcioAdatok'] = array_merge($massData['tranzakcioAdatok'], $ntakSendResult->generatedData['tranzakcioAdatok']);
  147. }
  148. }
  149. }
  150. $invoices[] = $invoice;
  151. }
  152. }
  153. if (!empty($massData['tranzakcioAdatok'])) {
  154. $ntakSendResult = $ntakClient->sendToServer($massData);
  155. if (!empty($ntakSendResult->ntakId)) {
  156. foreach ($invoices as $inv) {
  157. $inv->array_options['options_ntak_id'] = $ntakSendResult->ntakId;
  158. $inv->updateExtraField('ntak_id');
  159. $inv->array_options['options_ntak_status'] = $ntakSendResult->ntakStatus;
  160. $inv->updateExtraField('ntak_status');
  161. }
  162. }
  163. }
  164. }
  165. }
  166. /**
  167. *
  168. */
  169. public function checkSentInvoice(string $ntakId, int $invoiceId): array
  170. {
  171. return (NtakClient::getInstance())->checkWithLog($ntakId, $invoiceId);
  172. }
  173. /**
  174. *
  175. */
  176. public function sendByInvoice(Facture $facture): ?NtakSendResult
  177. {
  178. $ntakSendResult = null;
  179. $this->ntakLogger->logIntoFile('Send invoice by ID ('.$facture->id.')', LOG_INFO);
  180. $invoice = new NtakInvoice($this->db);
  181. if ($invoice->fetch($facture->id) > 0) {
  182. $invoice->loadPayments();
  183. // pass invoice to sender
  184. $ntakSendResult = (NtakClient::getInstance())->send($invoice);
  185. }
  186. $this->ntakLogger->logIntoFile('Send invoice finish', LOG_INFO);
  187. return $ntakSendResult;
  188. }
  189. /**
  190. *
  191. */
  192. private function setUnsentInvoices(int $entityId): void
  193. {
  194. $this->ntakLogger->logIntoFile('Get unsent invoices from DB', LOG_INFO);
  195. $this->unsentInvoices = [];
  196. $testCondition = '';
  197. if (!empty($this->testInvoices)) {
  198. $testCondition = " AND f.ref IN ('".implode("','", $this->testInvoices)."') ";
  199. }
  200. /*
  201. $sql = "
  202. SELECT f.rowid, f.ref
  203. FROM ".$this->db->prefix()."facture AS f
  204. WHERE f.date_valid IS NOT NULL
  205. AND NOT EXISTS (
  206. SELECT 1
  207. FROM ".$this->db->prefix()."ntak_ntaklog AS l
  208. WHERE l.status = 1
  209. AND l.fk_facture = f.rowid
  210. )
  211. {$testCondition}
  212. ORDER BY f.datec ASC
  213. ";
  214. */
  215. /*
  216. $sql = "
  217. SELECT f.rowid, f.ref
  218. FROM ".$this->db->prefix()."facture AS f
  219. LEFT JOIN ".$this->db->prefix()."facture_extrafields AS e ON e.fk_object=f.rowid
  220. WHERE f.date_valid IS NOT NULL
  221. AND (e.ntak_status NOT ILIKE 'BEFOGADVA' OR e.ntak_status IS NULL)
  222. AND f.entity={$entityId}
  223. {$testCondition}
  224. ORDER BY f.datec DESC LIMIT 10
  225. ";
  226. */
  227. //AND (e.ntak_status NOT ILIKE 'BEFOGADVA' OR e.ntak_status IS NULL)
  228. $sql = "
  229. SELECT t.rowid, t.ref, t.prefs
  230. FROM (
  231. SELECT f.rowid, f.ref, STRING_AGG(p.ref, ', ') AS prefs
  232. FROM ".$this->db->prefix()."facture AS f
  233. LEFT JOIN ".$this->db->prefix()."facture_extrafields AS e ON e.fk_object=f.rowid
  234. LEFT JOIN ".$this->db->prefix()."facturedet AS fd ON fd.fk_facture=f.rowid
  235. LEFT JOIN ".$this->db->prefix()."product AS p ON p.rowid=fd.fk_product
  236. WHERE f.date_valid IS NOT NULL
  237. AND e.ntak_status IS NULL
  238. AND f.entity={$entityId}
  239. AND f.multicurrency_total_ttc>0
  240. {$testCondition}
  241. GROUP BY f.rowid, f.ref
  242. ORDER BY f.datec DESC
  243. ) AS t
  244. WHERE (t.prefs ILIKE '%,%' OR t.prefs NOT ILIKE '%card%') LIMIT 1
  245. ";
  246. //print $sql;exit;
  247. $rows = $this->db->query($sql);
  248. if ($rows) {
  249. while ($row = $this->db->fetch_object($rows)) {
  250. $this->unsentInvoices[] = $row;
  251. }
  252. }
  253. $numOfInvoices = count($this->unsentInvoices);
  254. $logMsg = 'There is no unsent invoice';
  255. if ($numOfInvoices > 0) {
  256. $logMsg = 'There are '.$numOfInvoices.' unsent invoices';
  257. }
  258. $this->ntakLogger->logIntoFile($logMsg, LOG_INFO);
  259. }
  260. /**
  261. *
  262. */
  263. public function processValidatedTickets(): void
  264. {
  265. $this->ntakLogger->logIntoFile('Send ticket to validate', LOG_INFO);
  266. $ntakClient = NtakClient::getInstance();
  267. array_walk(
  268. $this->validatedTickets,
  269. function ($ticket) use ($ntakClient) {
  270. $ntakClient->validate(
  271. $ticket->ticket_log_id,
  272. $ticket->fact_ref,
  273. $ticket->ntak_id,
  274. $ticket->prod_ref,
  275. $ticket->prod_date,
  276. $ticket->log_date,
  277. $ticket->fact_id
  278. );
  279. }
  280. );
  281. $this->ntakLogger->logIntoFile('Ticket validated', LOG_INFO);
  282. }
  283. }