roller_handling_helper.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. use Luracast\Restler\RestException;
  3. trait RollerHandligHelper
  4. {
  5. public function getAllScooterDataByCode($code)
  6. {
  7. $deviceTypes = ['5' => 'Seev Citycoco', '6' => 'WIZTEM'];
  8. $sql = "SELECT
  9. i.rowid, i.entity, i.ref, i.fk_warehouse, i.status, i.title
  10. , ent.ref, ent.lieu
  11. , ie.device_type
  12. FROM llx_inventory as i
  13. INNER JOIN llx_entrepot AS ent ON ent.rowid = i.fk_warehouse
  14. INNER JOIN llx_inventory_extrafields as ie ON ie.fk_object = i.rowid
  15. WHERE i.title ILIKE '%{$code}%'";
  16. $result = $this->db->query($sql);
  17. if ($this->db->num_rows($result) > 0) {
  18. while ($row = $this->db->fetch_object($result)) {
  19. $row->device_type = $deviceTypes[$row->device_type];
  20. return $row;
  21. }
  22. }
  23. return [];
  24. }
  25. public function getScooterRowidByCode($code)
  26. {
  27. $sql = "SELECT * FROM llx_inventory as i WHERE i.title ILIKE '%{$code}%'";
  28. $result = $this->db->query($sql);
  29. if ($this->db->num_rows($result) > 0) {
  30. while ($row = $this->db->fetch_object($result)) {
  31. return $row->rowid;
  32. }
  33. }
  34. return [];
  35. }
  36. private function CreateScooterRentHistoryRecord($inventory_id, $invoice_number, $status = 1)
  37. {
  38. ApiBbusLog::ScooterRentLog('RentHistoryRecord: ___START___');
  39. global $user;
  40. $rollerRentHistoryObj = new RollerRentHistory($this->db);
  41. $rollerRentHistoryObj->inventory_id = $inventory_id;
  42. $rollerRentHistoryObj->invoice_number = $invoice_number;
  43. $rollerRentHistoryObj->status = $status;
  44. return $rollerRentHistoryObj->create($user);
  45. }
  46. private function checkError($error)
  47. {
  48. if ($error > 0) {
  49. $this->db->rollback();
  50. ApiBbusLog::ScooterRentLog('ConnectFactureAndScooter Rollback...');
  51. throw new RestException(509);
  52. }
  53. }
  54. private function updateScooterBbticket($selectedTicket)
  55. {
  56. $ticketChecker = new TicketChecker();
  57. $validated_at = date('Y-m-d H:i:s');
  58. $duration = $this->getDurationByProductId($selectedTicket->ticket_id);
  59. $expire_at = $this->getExpireAt($selectedTicket, $duration, $validated_at);
  60. $usage = $this->setUsage($selectedTicket);
  61. $sql = "UPDATE " . $this->db->prefix() . "bbus_bbticket SET validated_at = '" . $validated_at . "', expire_at = '" . $expire_at . "'";
  62. $sql .= ", usage = '" . $usage . "'";
  63. $sql .= " WHERE rowid = " . $selectedTicket->rowid;
  64. return $this->db->query($sql);
  65. }
  66. private function getDurationByProductId($ticket_id)
  67. {
  68. $sql = "SELECT p.duration as duration FROM " . $this->db->prefix() . "product AS p WHERE rowid = " . $ticket_id;
  69. $result = $this->db->query($sql);
  70. while ($row = $this->db->fetch_object($result)) {
  71. return $row->duration;
  72. }
  73. }
  74. private function getExpireAt($obj, $duration, $validated_at)
  75. {
  76. return date('Y-m-d H:i:s', strtotime($validated_at . ' +' . substr($duration, 0, -1) . ' ' . $this->getIntervalTimeByDuration()));
  77. }
  78. private function setUsage($selectedTicket)
  79. {
  80. $usage = (int)$selectedTicket->usage + 1;
  81. return $usage;
  82. }
  83. private function getIntervalTimeByDuration()
  84. {
  85. $duration = $this->duration[-1];
  86. switch ($duration) {
  87. case 'h':
  88. return 'hours';
  89. case 'd':
  90. return 'days';
  91. case 'w':
  92. return 'weeks';
  93. case 'm':
  94. return 'months';
  95. case 'y':
  96. return 'years';
  97. default:
  98. return 'hours';
  99. }
  100. }
  101. public function checkRentHistory($rowid, $invoice_number)
  102. {
  103. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$rowid} AND invoice_number = '{$invoice_number}' AND status = 1";
  104. return $this->db->query($sql);
  105. }
  106. public function getInvoiceNumberByInvetoryId($inventory_id)
  107. {
  108. $invoice_number = '';
  109. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$inventory_id} AND status = 1 ORDER BY date_creation DESC LIMIT 1";
  110. $result = $this->db->query($sql);
  111. if ($this->db->num_rows($result) > 0) {
  112. while ($row = $this->db->fetch_object($result)) {
  113. $invoice_number = $row->invoice_number;
  114. }
  115. }
  116. return $invoice_number;
  117. }
  118. public function getBbticketDataByInvoiceNumber($invoice_number)
  119. {
  120. $bbTicketData = [];
  121. $sql = "SELECT * FROM llx_bbus_bbticket WHERE invoice_number = '{$invoice_number}'";
  122. $result = $this->db->query($sql);
  123. if ($this->db->num_rows($result) > 0) {
  124. while ($row = $this->db->fetch_object($result)) {
  125. $bbTicketData = $row;
  126. }
  127. }
  128. return $bbTicketData;
  129. }
  130. public function checkExpireDate($invoice_number)
  131. {
  132. $now = date("Y-m-d H:i:s", dol_now());
  133. if ($invoice_number !== '') {
  134. $ticketData = $this->getBbticketDataByInvoiceNumber($invoice_number);
  135. if (strtotime($ticketData->expire_at) < strtotime($now)) {
  136. return true;
  137. }
  138. } else {
  139. throw new RestException(508);
  140. }
  141. return false;
  142. }
  143. public function getTicketIdByInvoiceNumber($invoice_number)
  144. {
  145. $bbticketObj = $this->getBbticketDataByInvoiceNumber($invoice_number);
  146. return $bbticketObj->ticket_id;
  147. }
  148. }