roller_handling_helper.class.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. use Luracast\Restler\RestException;
  3. trait RollerHandligHelper
  4. {
  5. public function getAllScooterDataByCode($code)
  6. {
  7. $deviceTypes = ['6' => 'Seev Citycoco', '7' => 'WIZTEM', '8' => 'MONSTERROLLER', '9' => 'NEOS'];
  8. $rentableStatuses = [110, 115];
  9. $sql = "SELECT
  10. i.rowid, i.entity, i.ref, i.fk_warehouse, i.status, i.title
  11. , ent.ref, ent.lieu
  12. , ie.device_type
  13. FROM llx_inventory as i
  14. INNER JOIN llx_entrepot AS ent ON ent.rowid = i.fk_warehouse
  15. INNER JOIN llx_inventory_extrafields as ie ON ie.fk_object = i.rowid
  16. WHERE i.title ILIKE '%{$code}%'";
  17. $result = $this->db->query($sql);
  18. if ($this->db->num_rows($result) > 0) {
  19. while ($row = $this->db->fetch_object($result)) {
  20. $row->device_type = $deviceTypes[$row->device_type];
  21. $row->isRentable = in_array($row->status, $rentableStatuses);
  22. return $row;
  23. }
  24. }
  25. return [];
  26. }
  27. public function getScooterRowidByCode($code)
  28. {
  29. $sql = "SELECT * FROM llx_inventory as i WHERE i.title ILIKE '%{$code}%'";
  30. $result = $this->db->query($sql);
  31. if ($this->db->num_rows($result) > 0) {
  32. while ($row = $this->db->fetch_object($result)) {
  33. return $row->rowid;
  34. }
  35. }
  36. return [];
  37. }
  38. public function CreateScooterRentHistoryRecord($inventory_id, $invoice_number, $timestamp = null, $ticket_id, $status = 1)
  39. {
  40. if (!is_null($timestamp)) {
  41. $ticket_id = $this->getTicketId($invoice_number, $timestamp);
  42. }
  43. ApiBbusLog::ScooterRentLog('RentHistoryRecord: ___START___');
  44. global $user;
  45. $rollerRentHistoryObj = new RollerRentHistory($this->db);
  46. $rollerRentHistoryObj->inventory_id = $inventory_id;
  47. $rollerRentHistoryObj->ticket_id = $ticket_id;
  48. $rollerRentHistoryObj->invoice_number = $invoice_number;
  49. $rollerRentHistoryObj->status = $status;
  50. return $rollerRentHistoryObj->create($user);
  51. }
  52. private function checkError($error)
  53. {
  54. if ($error > 0) {
  55. $this->db->rollback();
  56. ApiBbusLog::ScooterRentLog('ConnectFactureAndScooter Rollback...');
  57. throw new RestException(509);
  58. }
  59. }
  60. private function updateScooterBbticket($selectedTicket)
  61. {
  62. $ticketChecker = new TicketChecker();
  63. $validated_at = date('Y-m-d H:i:s');
  64. $duration = $this->getDurationByProductId($selectedTicket->ticket_id);
  65. $expire_at = $this->getExpireAt($selectedTicket, $duration, $validated_at);
  66. $usage = $this->setUsage($selectedTicket);
  67. $sql = "UPDATE " . $this->db->prefix() . "bbus_bbticket SET validated_at = '" . $validated_at . "', expire_at = '" . $expire_at . "'";
  68. $sql .= ", usage = '" . $usage . "'";
  69. $sql .= " WHERE rowid = " . $selectedTicket->rowid;
  70. return $this->db->query($sql);
  71. }
  72. private function getDurationByProductId($ticket_id)
  73. {
  74. $sql = "SELECT p.duration as duration FROM " . $this->db->prefix() . "product AS p WHERE rowid = " . $ticket_id;
  75. $result = $this->db->query($sql);
  76. while ($row = $this->db->fetch_object($result)) {
  77. return $row->duration;
  78. }
  79. }
  80. private function getExpireAt($obj, $duration, $validated_at)
  81. {
  82. return date('Y-m-d H:i:s', strtotime($validated_at . ' +' . substr($duration, 0, -1) . ' ' . $this->getIntervalTimeByDuration()));
  83. }
  84. private function setUsage($selectedTicket)
  85. {
  86. $usage = (int)$selectedTicket->usage + 1;
  87. return $usage;
  88. }
  89. private function getIntervalTimeByDuration()
  90. {
  91. $duration = $this->duration[-1];
  92. switch ($duration) {
  93. case 'h':
  94. return 'hours';
  95. case 'd':
  96. return 'days';
  97. case 'w':
  98. return 'weeks';
  99. case 'm':
  100. return 'months';
  101. case 'y':
  102. return 'years';
  103. default:
  104. return 'hours';
  105. }
  106. }
  107. public function checkRentHistory($rowid, $invoice_number)
  108. {
  109. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$rowid} AND invoice_number = '{$invoice_number}' AND status = 1";
  110. return $this->db->query($sql);
  111. }
  112. public function getInvoiceNumberByInvetoryId($inventory_id)
  113. {
  114. $invoice_number = '';
  115. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$inventory_id} AND status = 1 ORDER BY date_creation DESC LIMIT 1";
  116. $result = $this->db->query($sql);
  117. if ($this->db->num_rows($result) > 0) {
  118. while ($row = $this->db->fetch_object($result)) {
  119. $invoice_number = $row->invoice_number;
  120. }
  121. }
  122. return $invoice_number;
  123. }
  124. public function getRollerrenthistoryDataByInvetoryId($inventory_id)
  125. {
  126. $rollerrenthistoryData = new stdClass();
  127. $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$inventory_id} AND status = 1 ORDER BY date_creation DESC LIMIT 1";
  128. $result = $this->db->query($sql);
  129. if ($this->db->num_rows($result) > 0) {
  130. while ($row = $this->db->fetch_object($result)) {
  131. $rollerrenthistoryData = $row;
  132. }
  133. } else {
  134. $rollerrenthistoryData->ticket_id = '';
  135. }
  136. return $rollerrenthistoryData;
  137. }
  138. public function getTicketId($invoice_number, $timestamp)
  139. {
  140. $ticketData = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
  141. return $ticketData->rowid;
  142. }
  143. public function getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp)
  144. {
  145. global $conf;
  146. $bbTicketData = [];
  147. $sql = "SELECT bbt.* FROM llx_bbus_bbticketinvoiceprinting as bbtip
  148. INNER JOIN llx_product_extrafields as pre ON pre.fk_object = bbtip.product_id
  149. INNER JOIN llx_bbus_bbticket as bbt ON bbt.rowid = bbtip.ticket_id
  150. INNER JOIN llx_bbus_basicservices as bbs ON bbs.rowid = CAST(pre.basic_service AS integer)
  151. WHERE bbtip.invoice_number = '{$invoice_number}'
  152. AND bbtip.printing_date_timestamp = '{$timestamp}'
  153. AND bbs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'
  154. ORDER BY bbtip.rowid DESC limit 1";
  155. $result = $this->db->query($sql);
  156. if ($this->db->num_rows($result) > 0) {
  157. while ($row = $this->db->fetch_object($result)) {
  158. $bbTicketData = $row;
  159. }
  160. }
  161. return $bbTicketData;
  162. }
  163. public function getTicketIdByInvoiceNumberFromRollerRentHistory($invoice_number, $scooter_code)
  164. {
  165. $scooterData = $this->getScooterRowidByCode($scooter_code);
  166. $sql = "SELECT ticket_id FROM public.llx_rollerstorage_rollerrenthistory
  167. WHERE invoice_number = '{$invoice_number}' AND inventory_id = {$scooterData}
  168. ORDER BY rowid DESC LIMIT 1";
  169. $result = $this->db->query($sql);
  170. if ($this->db->num_rows($result) > 0) {
  171. while ($row = $this->db->fetch_object($result)) {
  172. return $row->ticket_id;
  173. }
  174. }
  175. return null;
  176. }
  177. public function checkExpireDate($ticket_id)
  178. {
  179. $array = [];
  180. $now = date("Y-m-d H:i:s", dol_now());
  181. if ($ticket_id !== '') {
  182. $sql = "SELECT expire_at FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
  183. $result = $this->db->query($sql);
  184. if ($this->db->num_rows($result) > 0) {
  185. while ($row = $this->db->fetch_object($result)) {
  186. if (strtotime($row->expire_at) < strtotime($now)) {
  187. $array['isExpired'] = true;
  188. $array['date'] = $row->expire_at;
  189. return $array;
  190. } else {
  191. $array['isExpired'] = false;
  192. return $array;
  193. }
  194. }
  195. }
  196. } else {
  197. $array['isExpired'] = false;
  198. }
  199. return $array;
  200. }
  201. public function getTicketIdByInvoiceNumber($invoice_number, $timestamp)
  202. {
  203. $bbticketObj = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
  204. return $bbticketObj->ticket_id;
  205. }
  206. public function isRented($QRCodeData, $inventory_id)
  207. {
  208. $rollerrenthistory = false;
  209. $sql = "SELECT status FROM public.llx_rollerstorage_rollerrenthistory
  210. WHERE invoice_number = '{$QRCodeData[0]}' AND inventory_id = {$inventory_id} AND status = 1
  211. ORDER BY rowid DESC LIMIT 1";
  212. $result = $this->db->query($sql);
  213. if ($this->db->num_rows($result) > 0) {
  214. while ($row = $this->db->fetch_object($result)) {
  215. $rollerrenthistory = $row->status == 1;
  216. }
  217. }
  218. return $rollerrenthistory;
  219. }
  220. public function createHistoryRecord($obj, $fromStatus){
  221. global $user;
  222. //print_r($obj);exit;
  223. $statuses2 = new Statuses($this->db);
  224. $status_id_from_statuses = $statuses2->getRowIdFromStatusId($this->db, $fromStatus);
  225. $status_id_to_statuses = $statuses2->getRowIdFromStatusId($this->db, $obj->status);
  226. $rollerhistory = new RollerHistory($this->db);
  227. $rollerhistory->fk_inventory = $obj->id;
  228. $rollerhistory->warehouse_from = $obj->fk_warehouse;
  229. $rollerhistory->warehouse_to = $obj->fk_warehouse;
  230. $rollerhistory->status_from = $status_id_from_statuses;
  231. $rollerhistory->status_to = $status_id_to_statuses;
  232. $rollerhistory->status = 0;
  233. if ($rollerhistory->create($user) == -1) {
  234. dol_syslog("Nem sikerult a rollerstorage_history mentese.");
  235. }
  236. }
  237. }