ntaklogger.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/custom/ntak/class/ntaklog.class.php';
  3. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  4. class NtakLogger
  5. {
  6. public const LOG_FILE_PREFIX = '_ntak';
  7. private static ?NtakLogger $instance = null;
  8. private User $user;
  9. public DoliDb $db;
  10. /**
  11. *
  12. */
  13. public function __construct()
  14. {
  15. global $db;
  16. $this->db = $db;
  17. $this->user = new User($this->db);
  18. $this->user->fetch(1);
  19. }
  20. /**
  21. *
  22. */
  23. public static function getInstance(): NtakLogger
  24. {
  25. if (self::$instance === null) {
  26. self::$instance = new NtakLogger;
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. *
  32. */
  33. public function logIntoFile(string $msg, int $level): void
  34. {
  35. dol_syslog($msg, $level, 0, self::LOG_FILE_PREFIX);
  36. }
  37. /**
  38. *
  39. */
  40. public function logIntoDb(int $invoiceId, int $status, string $errorMsg = null, string $data, string $ntakId = null): void
  41. {
  42. $log = new NtakLog($this->db);
  43. $log->ref = bin2hex(random_bytes(20));
  44. $log->status = $status;
  45. $log->error_msg = $errorMsg;
  46. $log->sent_data = $data;
  47. $log->fk_facture = $invoiceId;
  48. $log->ntak_id = $ntakId;
  49. if ($log->create($this->user) < 1) {
  50. $this->logIntoFile('Cannot save NTAK log data into DB', LOG_ALERT);
  51. }
  52. }
  53. /**
  54. *
  55. */
  56. public function massDbLogByInvoiceRef(array $refs, int $status, string $errorMsg = null, string $data, string $ntakId = null): void
  57. {
  58. foreach ($refs as $ref) {
  59. $invoice = new Facture($this->db);
  60. if ($invoice->fetch(0, $ref) > 0) {
  61. $this->logIntoDb($invoice->id, $status, $errorMsg, $data, $ntakId);
  62. }
  63. }
  64. }
  65. /**
  66. *
  67. */
  68. public function getOrigSentData(string $ntakId): array
  69. {
  70. $data = [];
  71. $sql = "
  72. SELECT sent_data FROM public.llx_ntak_ntaklog
  73. WHERE ntak_id ILIKE '{$ntakId}'
  74. ORDER BY rowid ASC
  75. LIMIT 1
  76. ";
  77. $rows = $this->db->query($sql);
  78. if ($rows) {
  79. while ($row = $this->db->fetch_row($rows)) {
  80. $data = json_decode(reset($row), true);
  81. if (json_last_error() === JSON_ERROR_NONE) {
  82. unset($data['feldolgozasAzonositok']);
  83. } else {
  84. $data = [];
  85. }
  86. }
  87. }
  88. return $data;
  89. }
  90. }