roller_handling_helper.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. INNER JOIN llx_bbus_basicservices as bbs ON bbs.rowid = CAST(pre.basic_service AS integer)
  146. WHERE bbtip.invoice_number = '{$invoice_number}'
  147. AND bbtip.printing_date_timestamp = '{$timestamp}'
  148. AND bbs.server_host = 'excelia'
  149. ORDER BY bbtip.rowid DESC limit 1";
  150. $result = $this->db->query($sql);
  151. if ($this->db->num_rows($result) > 0) {
  152. while ($row = $this->db->fetch_object($result)) {
  153. $bbTicketData = $row;
  154. }
  155. }
  156. return $bbTicketData;
  157. }
  158. public function getTicketIdByInvoiceNumberFromRollerRentHistory($invoice_number, $scooter_code)
  159. {
  160. $scooterData = $this->getScooterRowidByCode($scooter_code);
  161. $sql = "SELECT ticket_id FROM public.llx_rollerstorage_rollerrenthistory
  162. WHERE invoice_number = '{$invoice_number}' AND inventory_id = {$scooterData}
  163. ORDER BY rowid DESC LIMIT 1";
  164. $result = $this->db->query($sql);
  165. if ($this->db->num_rows($result) > 0) {
  166. while ($row = $this->db->fetch_object($result)) {
  167. return $row->ticket_id;
  168. }
  169. }
  170. return null;
  171. }
  172. public function checkExpireDate($ticket_id)
  173. {
  174. $now = date("Y-m-d H:i:s", dol_now());
  175. if ($ticket_id !== '') {
  176. $sql = "SELECT expire_at FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
  177. $result = $this->db->query($sql);
  178. if ($this->db->num_rows($result) > 0) {
  179. while ($row = $this->db->fetch_object($result)) {
  180. if (strtotime($row->expire_at) < strtotime($now)) {
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. }
  186. }
  187. } else {
  188. throw new RestException(508);
  189. }
  190. return false;
  191. }
  192. public function getTicketIdByInvoiceNumber($invoice_number, $timestamp)
  193. {
  194. $bbticketObj = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
  195. return $bbticketObj->ticket_id;
  196. }
  197. public function isRented($QRCodeData, $inventory_id)
  198. {
  199. $rollerrenthistory = false;
  200. $sql = "SELECT status FROM public.llx_rollerstorage_rollerrenthistory
  201. WHERE invoice_number = '{$QRCodeData[0]}' AND inventory_id = {$inventory_id} AND status = 1
  202. ORDER BY rowid DESC LIMIT 1";
  203. $result = $this->db->query($sql);
  204. if ($this->db->num_rows($result) > 0) {
  205. while ($row = $this->db->fetch_object($result)) {
  206. $rollerrenthistory = $row->status == 1;
  207. }
  208. }
  209. return $rollerrenthistory;
  210. }
  211. public function createHistoryRecord($obj, $fromStatus){
  212. global $user;
  213. //print_r($obj);exit;
  214. $statuses2 = new Statuses($this->db);
  215. $status_id_from_statuses = $statuses2->getRowIdFromStatusId($this->db, $fromStatus);
  216. $status_id_to_statuses = $statuses2->getRowIdFromStatusId($this->db, $obj->status);
  217. $rollerhistory = new RollerHistory($this->db);
  218. $rollerhistory->fk_inventory = $obj->id;
  219. $rollerhistory->warehouse_from = $obj->fk_warehouse;
  220. $rollerhistory->warehouse_to = $obj->fk_warehouse;
  221. $rollerhistory->status_from = $status_id_from_statuses;
  222. $rollerhistory->status_to = $status_id_to_statuses;
  223. $rollerhistory->status = 0;
  224. if ($rollerhistory->create($user) == -1) {
  225. dol_syslog("Nem sikerult a rollerstorage_history mentese.");
  226. }
  227. }
  228. }