lunatours.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. use Luracast\Restler\RestException;
  3. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/api_bbus_log.class.php';
  4. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/apiinvoicehelper.class.php';
  5. require_once DOL_DOCUMENT_ROOT . '/custom/booking/class/preorder.class.php';
  6. require_once DOL_DOCUMENT_ROOT . '/custom/booking/class/api_booking.class.php';
  7. require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';
  8. require_once DOL_DOCUMENT_ROOT . '/custom/booking/class/bookinghistory.class.php';
  9. class LunaTours
  10. {
  11. public $db;
  12. public $user;
  13. public $apiInvoiceHelper;
  14. public $BookingApi;
  15. public $PreOrder;
  16. const EMAIL_TEMPLATE = 'invoiceemail';
  17. const GLOBAL_CONF_SEND_TO_EMAIL = 'BBUS_INVOICE_PRINTING_ALERT_EMAIL';
  18. const GLOBAL_CONF_EMAIL_FROM = 'GLOBAL_CONF_EMAIL_FROM';
  19. const INVOICE_SUBJECT = 'INVOICE_SUBJECT';
  20. const INVOICE_DESCRIPTION = 'INVOICE_DESCRIPTION';
  21. public function __construct($db)
  22. {
  23. global $db, $user;
  24. $this->db = $db;
  25. $this->user = $user;
  26. $this->apiInvoiceHelper = new ApiInvoiceHelper;
  27. $this->BookingApi = new BookingApi();
  28. $this->PreOrder = new PreOrder($db);
  29. }
  30. public function localCheckAvailablePlacesForCheckPermission($product_id)
  31. {
  32. ApiBbusLog::LunaToursLog("localCheckAvailablePlacesForCheckPermission: {$product_id}");
  33. $date_from = $this->getCutOffTimeDate($product_id);
  34. $sql = "SELECT
  35. ac.id
  36. FROM llx_actioncomm as ac
  37. LEFT JOIN llx_actioncomm_extrafields as ace ON ace.fk_object = ac.id
  38. INNER JOIN llx_eventwizard_eventdetails as ed ON ac.fk_element = ed.rowid
  39. INNER JOIN llx_eventwizard_eventproduct as ep ON ep.fk_eventdetails = ac.fk_element
  40. WHERE ac.code = 'AC_EVENT'
  41. AND ep.fk_product = {$product_id}
  42. AND ac.datep > '{$date_from}'
  43. ORDER BY ac.datep ASC";
  44. $result = $this->db->query($sql);
  45. return $this->db->num_rows($result) > 0 ? 1 : 0;
  46. }
  47. private function getCutOffTimeDate($product_id)
  48. {
  49. $date = date("Y-m-d H:i:s");
  50. $sql = "SELECT cut_off_time_app FROM llx_product_extrafields WHERE fk_object = {$product_id}";
  51. if ($result = $this->db->query($sql)) {
  52. if ($this->db->num_rows($result) > 0) {
  53. while ($row = $this->db->fetch_object($result)) {
  54. $cutOffTime = $row->cut_off_time_app;
  55. }
  56. $dateTimestamp = strtotime($date);
  57. $date = date("Y-m-d H:i:s", $dateTimestamp + $cutOffTime * 60);
  58. }
  59. }
  60. return $date;
  61. }
  62. public function localavailableplaces($date_from, $date_to, $product_id)
  63. {
  64. ApiBbusLog::LunaToursLog("localAvailablePlaces");
  65. $array = [];
  66. $date = new DateTime($date_to);
  67. $date->modify('+1 day');
  68. $date_to = $date->format('Y-m-d');
  69. $date_from = $this->getCutOffTimeDate($product_id);
  70. $sql = "SELECT
  71. ac.id,
  72. ac.datep,
  73. ace.max_num,
  74. ace.participants,
  75. ace.buffer
  76. FROM llx_actioncomm as ac
  77. LEFT JOIN llx_actioncomm_extrafields as ace ON ace.fk_object = ac.id
  78. INNER JOIN llx_eventwizard_eventdetails as ed ON ac.fk_element = ed.rowid
  79. INNER JOIN llx_eventwizard_eventproduct as ep ON ep.fk_eventdetails = ac.fk_element
  80. WHERE ac.code = 'AC_EVENT'
  81. AND ep.fk_product = {$product_id}
  82. AND ac.datep > '{$date_from}'
  83. AND ac.datep2 < '{$date_to} 00:00:00'
  84. ORDER BY ac.datep ASC";
  85. $result = $this->db->query($sql);
  86. while ($row = $this->db->fetch_object($result)) {
  87. $max_num = $row->max_num;
  88. $buffer = $row->buffer;
  89. $participants = $row->participants;
  90. $date = strtotime($row->datep);
  91. $element['event_id'] = $row->id;
  92. $element['participants'] = is_null($participants) ? 0 : (int) $participants;
  93. $element['max_num'] = $max_num;
  94. $element['buffer'] = $buffer;
  95. //if ($max_num > $participants) {
  96. // if ($max_num + $buffer > $participants + $participant_number) {
  97. $array[date("Y-m-d", $date)][date("H:i", $date)] = $element;
  98. // }
  99. //}
  100. }
  101. return $array;
  102. }
  103. function createLog($fk_event, $reservations, $product_id)
  104. {
  105. /**
  106. * LOG SECTION
  107. */
  108. ApiBbusLog::LunaToursLog("=== NEW INVOICE ===");
  109. ApiBbusLog::LunaToursLog("User: {$this->user->firstname} {$this->user->lastname} (ID: {$this->user->id})");
  110. ApiBbusLog::LunaToursLog(json_encode([
  111. 'fk_event' => $fk_event,
  112. 'reservations' => $reservations,
  113. 'product_id' => $product_id
  114. ]));
  115. }
  116. function localcreatedpreorderobj($fk_event, $reservations, $product_id)
  117. {
  118. global $user;
  119. ApiBbusLog::LunaToursLog("localcreatedpreorderobj");
  120. $createdPreOrderOBJ = [];
  121. for ($i = 1; $i <= $reservations; $i++) {
  122. dol_include_once('/comm/action/class/actioncomm.class.php');
  123. dol_include_once('/custom/booking/class/preorder.class.php');
  124. $this->apiInvoiceHelper->increaseParticipant($fk_event);
  125. $lines = $this->apiInvoiceHelper->getProductLines($product_id);
  126. foreach ($lines as $line) {
  127. $preOrderObj = $this->PreOrder;
  128. $preOrderObj->ref = $this->BookingApi->generateRandomString();
  129. $preOrderObj->fk_event = $fk_event;
  130. $preOrderObj->fk_product = $product_id;
  131. $result = $preOrderObj->create($user);
  132. if ($result > 0) {
  133. $createdPreOrderOBJ[] = [
  134. 'id' => $preOrderObj->id,
  135. 'ref' => $preOrderObj->ref,
  136. ];
  137. } else {
  138. foreach ($preOrderObj->errors as $error) {
  139. ApiBbusLog::LunaToursLog("Error: " . $error);
  140. }
  141. ApiBbusLog::LunaToursLog("Unsaved event: " . $fk_event);
  142. throw new RestException(401, 'Unsaved event: ' . $fk_event);
  143. }
  144. }
  145. }
  146. ApiBbusLog::LunaToursLog(json_encode($createdPreOrderOBJ));
  147. return $createdPreOrderOBJ;
  148. }
  149. public function localValidateAndSaveInvoice($invoice, $preorder, $payment, $cardPaymentLog, $company_invoice = [])
  150. {
  151. ApiBbusLog::LunaToursLog("localValidateAndSaveInvoice");
  152. global $user, $db, $conf;
  153. $server_host_curl = false;
  154. $createdInvoiceData = [];
  155. $apiInvoiceHelper = new ApiInvoiceHelper;
  156. $bbusApi = new BBus();
  157. foreach ($preorder as $rowid) {
  158. $params = compact('rowid');
  159. ApiBbusLog::LunaToursLog("localValidateAndSaveInvoice: local");
  160. $preorderArray = $this->BookingApi->localpreorderarray($rowid);
  161. $lines = $apiInvoiceHelper->getProductLinesWithoutDiscount($preorderArray->fk_product);
  162. $createdInvoiceArray = $bbusApi->invoice($invoice, $lines, $payment, $cardPaymentLog = '', $sendId = '', $server_host_curl, $company_invoice);
  163. $createdInvoiceData[] = $createdInvoiceArray;
  164. }
  165. ApiBbusLog::LunaToursLog("Visszaküld");
  166. $asd = json_encode($createdInvoiceData);
  167. ApiBbusLog::LunaToursLog("{$asd}");
  168. return $createdInvoiceData;
  169. }
  170. public function saveEventData($fk_event, $fk_facture = null, $fk_eventproduct = null, $ref = null, $note = '')
  171. {
  172. ApiBbusLog::appLog("saveEventData");
  173. dol_include_once('/custom/booking/class/bookinghistory.class.php');
  174. dol_include_once('/comm/action/class/actioncomm.class.php');
  175. $actionCommObj = new ActionComm($this->db);
  176. $sql = "SELECT fk_element, datep, datep2 FROM " . MAIN_DB_PREFIX . $actionCommObj->table_element . " WHERE id = {$fk_event}";
  177. ApiBbusLog::appLog("{$sql}");
  178. $result = $this->db->query($sql);
  179. if ($this->db->num_rows($result) > 0) {
  180. $row = $this->db->fetch_object($result);
  181. $BookingHistory = new BookingHistory($this->db);
  182. $BookingHistory->fk_event = $fk_event;
  183. $BookingHistory->fk_facture = $fk_facture;
  184. $BookingHistory->fk_event_detail = $row->fk_element;
  185. $BookingHistory->fk_eventproduct = $fk_eventproduct;
  186. $BookingHistory->date_start = strtotime($row->datep);
  187. $BookingHistory->date_end = strtotime($row->datep2);
  188. $BookingHistory->invoice_number = $ref;
  189. $BookingHistory->note = $note;
  190. $result = $BookingHistory->create($this->user);
  191. if ($result > 0) {
  192. ApiBbusLog::appLog("saveEventData: OK {$BookingHistory->id}");
  193. return $BookingHistory->id;
  194. } else {
  195. foreach ($BookingHistory->errors as $error) {
  196. ApiBbusLog::appLog("saveEventData: {$error}");
  197. }
  198. ApiBbusLog::appLog("saveEventData: ERROR");
  199. throw new RestException(401, 'Unsaved');
  200. }
  201. } else {
  202. throw new RestException(401, 'No Event record');
  203. }
  204. }
  205. public function updateBbticket($invoice, $bookingHistory)
  206. {
  207. $this->BookingApi->updateBbticket($invoice, $bookingHistory);
  208. }
  209. public function deletePreorder($preorder)
  210. {
  211. $this->BookingApi->deletePreorder($preorder);
  212. }
  213. public function emailSender($note, $createdInvoiceArray)
  214. {
  215. foreach ($createdInvoiceArray as $invoice) {
  216. $note_array = [];
  217. ApiBbusLog::LunaToursLog("Start E-mail sending");
  218. $note_array = json_decode($note, true);
  219. $emailAddress = $note_array['email'];
  220. $const = new GlobalConst;
  221. $emailFrom = $const->load(self::GLOBAL_CONF_EMAIL_FROM);
  222. $recipients = $emailAddress;
  223. //$recipients = 'szollosi.laszlo@urbanms.hu';
  224. if (!empty($recipients) && !empty($emailFrom)) {
  225. $emailTemplateHandler = new EmailTemplateHandler;
  226. $template = $emailTemplateHandler->load(self::EMAIL_TEMPLATE);
  227. $templateObject = new stdClass();
  228. foreach ($template as $key => $value) {
  229. $templateObject->$key = $value;
  230. }
  231. if ($templateObject) {
  232. $varsInvoice = [
  233. '__SUBJECT__' => $const->load(self::INVOICE_SUBJECT),
  234. '__DESCRIPTION__' => $const->load(self::INVOICE_DESCRIPTION),
  235. ];
  236. $template = $emailTemplateHandler->prepareTemplate($templateObject, $varsInvoice);
  237. $attachment = [DOL_DOCUMENT_ROOT . '/documents/facture/' . $invoice['invoice']['ref'] . '/' . $invoice['invoice']['ref'] . '.pdf'];
  238. $mimetype = ['application/pdf'];
  239. $mimefilename = [$invoice['invoice']['ref'] . '.pdf'];
  240. $mail = new CMailFile(
  241. $template->topic,
  242. $recipients,
  243. $emailFrom,
  244. $template->content,
  245. $attachment,
  246. $mimetype,
  247. $mimefilename,
  248. //null,
  249. //null,
  250. //null,
  251. '',
  252. '',
  253. 0,
  254. 1
  255. );
  256. // Send Email
  257. $mail->sendfile();
  258. ApiBbusLog::LunaToursLog('E-mail address: ' . $emailAddress);
  259. if ($mail->error) {
  260. ApiBbusLog::LunaToursLog($mail->error);
  261. // do something...
  262. dol_syslog('CRON ALERT EMAIL ERROR: ' . $mail->error, LOG_ERR);
  263. } else {
  264. ApiBbusLog::LunaToursLog("E-mail sent");
  265. }
  266. }
  267. }
  268. }
  269. }
  270. public function createPermissionArray($array){
  271. foreach ($array as $key => $item) {
  272. if ($this->localCheckAvailablePlacesForCheckPermission($item->rowid)) {
  273. $permissionArray[$item->rowid] = $item;
  274. }
  275. }
  276. return $permissionArray;
  277. }
  278. }