nsu_controller_helper.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. class NsuControllerHelper{
  3. const EMAIL_TEMPLATE = 'invoiceemail';
  4. const GLOBAL_CONF_SEND_TO_EMAIL = 'BBUS_INVOICE_PRINTING_ALERT_EMAIL';
  5. const GLOBAL_CONF_EMAIL_FROM = 'GLOBAL_CONF_EMAIL_FROM';
  6. const INVOICE_SUBJECT = 'INVOICE_SUBJECT';
  7. const INVOICE_DESCRIPTION = 'INVOICE_DESCRIPTION';
  8. public function getObject($sql){
  9. global $db;
  10. $array = [];
  11. $result = $db->query($sql);
  12. if($db->num_rows($result) > 0){
  13. while($row = $db->fetch_object($result)){
  14. $array[] = $row;
  15. }
  16. }
  17. return $array;
  18. }
  19. public function generateTransactionID($sendId){
  20. $DBARTransactionID = substr(bin2hex(random_bytes(16)), 0, 16);
  21. ApiNSULog::invoiceLog("{$sendId}: DBAR Transaction ID: {$DBARTransactionID}");
  22. return $DBARTransactionID;
  23. }
  24. public function getProductLines($product_id)
  25. {
  26. global $db;
  27. $lines = [];
  28. $productObj = new Product($db);
  29. $result = $productObj->fetch($product_id);
  30. if($db->num_rows($result) > 0){
  31. $lines[] = (array) $productObj;
  32. }
  33. return $lines;
  34. }
  35. public function emailSender($note, $createdInvoiceArray)
  36. {
  37. foreach ($createdInvoiceArray as $invoice) {
  38. $note_array = [];
  39. ApiNSULog::invoiceLog("Start E-mail sending");
  40. $note_array = json_decode($note, true);
  41. $emailAddress = $note_array['email'];
  42. $const = new GlobalConst;
  43. $emailFrom = $const->load(self::GLOBAL_CONF_EMAIL_FROM);
  44. $recipients = $emailAddress;
  45. //$recipients = 'szollosi.laszlo@urbanms.hu';
  46. if (!empty($recipients) && !empty($emailFrom)) {
  47. $emailTemplateHandler = new EmailTemplateHandler;
  48. $template = $emailTemplateHandler->load(self::EMAIL_TEMPLATE);
  49. $templateObject = new stdClass();
  50. foreach ($template as $key => $value) {
  51. $templateObject->$key = $value;
  52. }
  53. if ($templateObject) {
  54. $varsInvoice = [
  55. '__SUBJECT__' => $const->load(self::INVOICE_SUBJECT),
  56. '__DESCRIPTION__' => $const->load(self::INVOICE_DESCRIPTION),
  57. ];
  58. $template = $emailTemplateHandler->prepareTemplate($templateObject, $varsInvoice);
  59. $attachment = [DOL_DOCUMENT_ROOT . '/documents/facture/' . $invoice['invoice']['ref'] . '/' . $invoice['invoice']['ref'] . '.pdf'];
  60. $mimetype = ['application/pdf'];
  61. $mimefilename = [$invoice['invoice']['ref'] . '.pdf'];
  62. $mail = new CMailFile(
  63. $template->topic,
  64. $recipients,
  65. $emailFrom,
  66. $template->content,
  67. $attachment,
  68. $mimetype,
  69. $mimefilename,
  70. //null,
  71. //null,
  72. //null,
  73. '',
  74. '',
  75. 0,
  76. 1
  77. );
  78. // Send Email
  79. $mail->sendfile();
  80. ApiNSULog::invoiceLog('E-mail address: ' . $emailAddress);
  81. if ($mail->error) {
  82. ApiNSULog::invoiceLog($mail->error);
  83. // do something...
  84. dol_syslog('CRON ALERT EMAIL ERROR: ' . $mail->error, LOG_ERR);
  85. } else {
  86. ApiNSULog::invoiceLog("E-mail sent");
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }