lunatours.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. public function __construct($db)
  17. {
  18. global $db, $user;
  19. $this->db = $db;
  20. $this->user = $user;
  21. $this->apiInvoiceHelper = new ApiInvoiceHelper;
  22. $this->BookingApi = new BookingApi();
  23. $this->PreOrder = new PreOrder($db);
  24. }
  25. public function localCheckAvailablePlacesForCheckPermission($product_id)
  26. {
  27. ApiBbusLog::LunaToursLog("localCheckAvailablePlacesForCheckPermission: {$product_id}");
  28. $date_from = $this->getCutOffTimeDate($product_id);
  29. $sql = "SELECT
  30. ac.id
  31. FROM llx_actioncomm as ac
  32. LEFT JOIN llx_actioncomm_extrafields as ace ON ace.fk_object = ac.id
  33. INNER JOIN llx_eventwizard_eventdetails as ed ON ac.fk_element = ed.rowid
  34. INNER JOIN llx_eventwizard_eventproduct as ep ON ep.fk_eventdetails = ac.fk_element
  35. WHERE ac.code = 'AC_EVENT'
  36. AND ep.fk_product = {$product_id}
  37. AND ac.datep > '{$date_from}'
  38. ORDER BY ac.datep ASC";
  39. $result = $this->db->query($sql);
  40. return $this->db->num_rows($result) > 0 ? 1 : 0;
  41. }
  42. private function getCutOffTimeDate($product_id)
  43. {
  44. $date = date("Y-m-d H:i:s");
  45. $sql = "SELECT cut_off_time_app FROM llx_product_extrafields WHERE fk_object = {$product_id}";
  46. if ($result = $this->db->query($sql)) {
  47. if ($this->db->num_rows($result) > 0) {
  48. while ($row = $this->db->fetch_object($result)) {
  49. $cutOffTime = $row->cut_off_time_app;
  50. }
  51. $dateTimestamp = strtotime($date);
  52. $date = date("Y-m-d H:i:s", $dateTimestamp + $cutOffTime * 60);
  53. }
  54. }
  55. return $date;
  56. }
  57. public function localavailableplaces($date_from, $date_to, $product_id)
  58. {
  59. ApiBbusLog::LunaToursLog("localAvailablePlaces");
  60. $array = [];
  61. $date = new DateTime($date_to);
  62. $date->modify('+1 day');
  63. $date_to = $date->format('Y-m-d');
  64. $date_from = $this->getCutOffTimeDate($product_id);
  65. $sql = "SELECT
  66. ac.id,
  67. ac.datep,
  68. ace.max_num,
  69. ace.participants,
  70. ace.buffer
  71. FROM llx_actioncomm as ac
  72. LEFT JOIN llx_actioncomm_extrafields as ace ON ace.fk_object = ac.id
  73. INNER JOIN llx_eventwizard_eventdetails as ed ON ac.fk_element = ed.rowid
  74. INNER JOIN llx_eventwizard_eventproduct as ep ON ep.fk_eventdetails = ac.fk_element
  75. WHERE ac.code = 'AC_EVENT'
  76. AND ep.fk_product = {$product_id}
  77. AND ac.datep > '{$date_from}'
  78. AND ac.datep2 < '{$date_to} 00:00:00'
  79. ORDER BY ac.datep ASC";
  80. $result = $this->db->query($sql);
  81. while ($row = $this->db->fetch_object($result)) {
  82. $max_num = $row->max_num;
  83. $buffer = $row->buffer;
  84. $participants = $row->participants;
  85. $date = strtotime($row->datep);
  86. $element['event_id'] = $row->id;
  87. $element['participants'] = is_null($participants) ? 0 : (int) $participants;
  88. $element['max_num'] = $max_num;
  89. $element['buffer'] = $buffer;
  90. //if ($max_num > $participants) {
  91. // if ($max_num + $buffer > $participants + $participant_number) {
  92. $array[date("Y-m-d", $date)][date("H:i", $date)] = $element;
  93. // }
  94. //}
  95. }
  96. return $array;
  97. }
  98. function createLog($fk_event, $reservations, $product_id)
  99. {
  100. /**
  101. * LOG SECTION
  102. */
  103. ApiBbusLog::LunaToursLog("=== NEW INVOICE ===");
  104. ApiBbusLog::LunaToursLog("User: {$this->user->firstname} {$this->user->lastname} (ID: {$this->user->id})");
  105. ApiBbusLog::LunaToursLog(json_encode([
  106. 'fk_event' => $fk_event,
  107. 'reservations' => $reservations,
  108. 'product_id' => $product_id
  109. ]));
  110. }
  111. function localcreatedpreorderobj($fk_event, $reservations, $product_id)
  112. {
  113. global $user;
  114. ApiBbusLog::LunaToursLog("localcreatedpreorderobj");
  115. $createdPreOrderOBJ = [];
  116. for ($i = 1; $i <= $reservations; $i++) {
  117. dol_include_once('/comm/action/class/actioncomm.class.php');
  118. dol_include_once('/custom/booking/class/preorder.class.php');
  119. $this->apiInvoiceHelper->increaseParticipant($fk_event);
  120. $lines = $this->apiInvoiceHelper->getProductLines($product_id);
  121. foreach ($lines as $line) {
  122. $preOrderObj = $this->PreOrder;
  123. $preOrderObj->ref = $this->BookingApi->generateRandomString();
  124. $preOrderObj->fk_event = $fk_event;
  125. $preOrderObj->fk_product = $product_id;
  126. $result = $preOrderObj->create($user);
  127. if ($result > 0) {
  128. $createdPreOrderOBJ[] = [
  129. 'id' => $preOrderObj->id,
  130. 'ref' => $preOrderObj->ref,
  131. ];
  132. } else {
  133. foreach ($preOrderObj->errors as $error) {
  134. ApiBbusLog::LunaToursLog("Error: " . $error);
  135. }
  136. ApiBbusLog::LunaToursLog("Unsaved event: " . $fk_event);
  137. throw new RestException(401, 'Unsaved event: ' . $fk_event);
  138. }
  139. }
  140. }
  141. ApiBbusLog::LunaToursLog(json_encode($createdPreOrderOBJ));
  142. return $createdPreOrderOBJ;
  143. }
  144. public function localValidateAndSaveInvoice($invoice, $preorder, $payment, $cardPaymentLog, $company_invoice = [])
  145. {
  146. ApiBbusLog::LunaToursLog("localValidateAndSaveInvoice");
  147. global $user, $db, $conf;
  148. $server_host_curl = false;
  149. $createdInvoiceData = [];
  150. $apiInvoiceHelper = new ApiInvoiceHelper;
  151. $bbusApi = new BBus();
  152. foreach ($preorder as $rowid) {
  153. $params = compact('rowid');
  154. ApiBbusLog::LunaToursLog("localValidateAndSaveInvoice: local");
  155. $preorderArray = $this->BookingApi->localpreorderarray($rowid);
  156. $lines = $apiInvoiceHelper->getProductLinesWithoutDiscount($preorderArray->fk_product);
  157. $createdInvoiceArray = $bbusApi->invoice($invoice, $lines, $payment, $cardPaymentLog = '', $sendId = '', $server_host_curl, $company_invoice);
  158. $createdInvoiceData[] = $createdInvoiceArray;
  159. }
  160. ApiBbusLog::LunaToursLog("Visszaküld");
  161. $asd = json_encode($createdInvoiceData);
  162. ApiBbusLog::LunaToursLog("{$asd}");
  163. return $createdInvoiceData;
  164. }
  165. public function saveEventData($fk_event, $fk_facture = null, $fk_eventproduct = null, $ref = null, $note = '')
  166. {
  167. ApiBbusLog::appLog("saveEventData");
  168. dol_include_once('/custom/booking/class/bookinghistory.class.php');
  169. dol_include_once('/comm/action/class/actioncomm.class.php');
  170. $actionCommObj = new ActionComm($this->db);
  171. $sql = "SELECT fk_element, datep, datep2 FROM " . MAIN_DB_PREFIX . $actionCommObj->table_element . " WHERE id = {$fk_event}";
  172. ApiBbusLog::appLog("{$sql}");
  173. $result = $this->db->query($sql);
  174. if ($this->db->num_rows($result) > 0) {
  175. $row = $this->db->fetch_object($result);
  176. $BookingHistory = new BookingHistory($this->db);
  177. $BookingHistory->fk_event = $fk_event;
  178. $BookingHistory->fk_facture = $fk_facture;
  179. $BookingHistory->fk_event_detail = $row->fk_element;
  180. $BookingHistory->fk_eventproduct = $fk_eventproduct;
  181. $BookingHistory->date_start = strtotime($row->datep);
  182. $BookingHistory->date_end = strtotime($row->datep2);
  183. $BookingHistory->invoice_number = $ref;
  184. $BookingHistory->note = $note;
  185. $result = $BookingHistory->create($this->user);
  186. if ($result > 0) {
  187. ApiBbusLog::appLog("saveEventData: OK {$BookingHistory->id}");
  188. return $BookingHistory->id;
  189. } else {
  190. foreach ($BookingHistory->errors as $error) {
  191. ApiBbusLog::appLog("saveEventData: {$error}");
  192. }
  193. ApiBbusLog::appLog("saveEventData: ERROR");
  194. throw new RestException(401, 'Unsaved');
  195. }
  196. } else {
  197. throw new RestException(401, 'No Event record');
  198. }
  199. }
  200. public function updateBbticket($invoice, $bookingHistory){
  201. $this->BookingApi->updateBbticket($invoice, $bookingHistory);
  202. }
  203. public function deletePreorder($preorder){
  204. $this->BookingApi->deletePreorder($preorder);
  205. }
  206. }