roller_handling_helper.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. public function CreateScooterRentHistoryRecord($inventory_id, $invoice_number, $timestamp = null, $ticket_id, $status = 1)
  37. {
  38. if (!is_null($timestamp)) {
  39. $ticket_id = $this->getTicketId($invoice_number, $timestamp);
  40. }
  41. ApiBbusLog::ScooterRentLog('RentHistoryRecord: ___START___');
  42. global $user;
  43. $rollerRentHistoryObj = new RollerRentHistory($this->db);
  44. $rollerRentHistoryObj->inventory_id = $inventory_id;
  45. $rollerRentHistoryObj->ticket_id = $ticket_id;
  46. $rollerRentHistoryObj->invoice_number = $invoice_number;
  47. $rollerRentHistoryObj->status = $status;
  48. return $rollerRentHistoryObj->create($user);
  49. }
  50. private function checkError($error)
  51. {
  52. if ($error > 0) {
  53. $this->db->rollback();
  54. ApiBbusLog::ScooterRentLog('ConnectFactureAndScooter Rollback...');
  55. throw new RestException(509);
  56. }
  57. }
  58. private function updateScooterBbticket($selectedTicket)
  59. {
  60. $ticketChecker = new TicketChecker();
  61. $validated_at = date('Y-m-d H:i:s');
  62. $duration = $this->getDurationByProductId($selectedTicket->ticket_id);
  63. $expire_at = $this->getExpireAt($selectedTicket, $duration, $validated_at);
  64. $usage = $this->setUsage($selectedTicket);
  65. $sql = "UPDATE " . $this->db->prefix() . "bbus_bbticket SET validated_at = '" . $validated_at . "', expire_at = '" . $expire_at . "'";
  66. $sql .= ", usage = '" . $usage . "'";
  67. $sql .= " WHERE rowid = " . $selectedTicket->rowid;
  68. return $this->db->query($sql);
  69. }
  70. private function getDurationByProductId($ticket_id)
  71. {
  72. $sql = "SELECT p.duration as duration FROM " . $this->db->prefix() . "product AS p WHERE rowid = " . $ticket_id;
  73. $result = $this->db->query($sql);
  74. while ($row = $this->db->fetch_object($result)) {
  75. return $row->duration;
  76. }
  77. }
  78. private function getExpireAt($obj, $duration, $validated_at)
  79. {
  80. return date('Y-m-d H:i:s', strtotime($validated_at . ' +' . substr($duration, 0, -1) . ' ' . $this->getIntervalTimeByDuration()));
  81. }
  82. private function setUsage($selectedTicket)
  83. {
  84. $usage = (int)$selectedTicket->usage + 1;
  85. return $usage;
  86. }
  87. private function getIntervalTimeByDuration()
  88. {
  89. $duration = $this->duration[-1];
  90. switch ($duration) {
  91. case 'h':
  92. return 'hours';
  93. case 'd':
  94. return 'days';
  95. case 'w':
  96. return 'weeks';
  97. case 'm':
  98. return 'months';
  99. case 'y':
  100. return 'years';
  101. default:
  102. return 'hours';
  103. }
  104. }
  105. public function checkRentHistory($rowid, $invoice_number)
  106. {
  107. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$rowid} AND invoice_number = '{$invoice_number}' AND status = 1";
  108. return $this->db->query($sql);
  109. }
  110. public function getInvoiceNumberByInvetoryId($inventory_id)
  111. {
  112. $invoice_number = '';
  113. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$inventory_id} AND status = 1 ORDER BY date_creation DESC LIMIT 1";
  114. $result = $this->db->query($sql);
  115. if ($this->db->num_rows($result) > 0) {
  116. while ($row = $this->db->fetch_object($result)) {
  117. $invoice_number = $row->invoice_number;
  118. }
  119. }
  120. return $invoice_number;
  121. }
  122. public function getRollerrenthistoryDataByInvetoryId($inventory_id)
  123. {
  124. $rollerrenthistoryData = '';
  125. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$inventory_id} AND status = 1 ORDER BY date_creation DESC LIMIT 1";
  126. $result = $this->db->query($sql);
  127. if ($this->db->num_rows($result) > 0) {
  128. while ($row = $this->db->fetch_object($result)) {
  129. $rollerrenthistoryData = $row;
  130. }
  131. }
  132. return $rollerrenthistoryData;
  133. }
  134. public function getTicketId($invoice_number, $timestamp)
  135. {
  136. $ticketData = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
  137. return $ticketData->rowid;
  138. }
  139. public function getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp)
  140. {
  141. $bbTicketData = [];
  142. $sql = "SELECT bbt.* FROM llx_bbus_bbticketinvoiceprinting as bbtip
  143. INNER JOIN llx_product_extrafields as pre ON pre.fk_object = bbtip.product_id
  144. INNER JOIN llx_bbus_bbticket as bbt ON bbt.rowid = bbtip.ticket_id
  145. WHERE bbtip.invoice_number = '{$invoice_number}'
  146. AND bbtip.printing_date_timestamp = '{$timestamp}'
  147. AND pre.basic_service IN ('2','3')
  148. ORDER BY bbtip.rowid DESC limit 1";
  149. $result = $this->db->query($sql);
  150. if ($this->db->num_rows($result) > 0) {
  151. while ($row = $this->db->fetch_object($result)) {
  152. $bbTicketData = $row;
  153. }
  154. }
  155. return $bbTicketData;
  156. }
  157. public function getTicketIdByInvoiceNumberFromRollerRentHistory($invoice_number, $scooter_code)
  158. {
  159. $scooterData = $this->getScooterRowidByCode($scooter_code);
  160. $sql = "SELECT ticket_id FROM public.llx_rollerstorage_rollerrenthistory
  161. WHERE invoice_number = '{$invoice_number}' AND inventory_id = {$scooterData}
  162. ORDER BY rowid DESC LIMIT 1";
  163. $result = $this->db->query($sql);
  164. if ($this->db->num_rows($result) > 0) {
  165. while ($row = $this->db->fetch_object($result)) {
  166. return $row->ticket_id;
  167. }
  168. }
  169. return null;
  170. }
  171. public function checkExpireDate($ticket_id)
  172. {
  173. $now = date("Y-m-d H:i:s", dol_now());
  174. if ($ticket_id !== '') {
  175. $sql = "SELECT expire_at FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
  176. $result = $this->db->query($sql);
  177. if($this->db->num_rows($result) > 0){
  178. while($row = $this->db->fetch_object($result)){
  179. if (strtotime($row->expire_at) < strtotime($now)) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. }
  186. } else {
  187. throw new RestException(508);
  188. }
  189. return false;
  190. }
  191. public function getTicketIdByInvoiceNumber($invoice_number, $timestamp)
  192. {
  193. $bbticketObj = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
  194. return $bbticketObj->ticket_id;
  195. }
  196. public function isRented($QRCodeData, $inventory_id)
  197. {
  198. $rollerrenthistory = false;
  199. $sql = "SELECT status FROM public.llx_rollerstorage_rollerrenthistory
  200. WHERE invoice_number = '{$QRCodeData[0]}' AND inventory_id = {$inventory_id} AND status = 1
  201. ORDER BY rowid DESC LIMIT 1";
  202. $result = $this->db->query($sql);
  203. if ($this->db->num_rows($result) > 0) {
  204. while ($row = $this->db->fetch_object($result)) {
  205. $rollerrenthistory = $row->status == 1;
  206. }
  207. }
  208. return $rollerrenthistory;
  209. }
  210. }