roller_handling.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/rollerrenthistory.class.php';
  3. require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/roller_handling_helper.class.php';
  4. use Luracast\Restler\RestException;
  5. trait RollerHandling
  6. {
  7. use RollerHandligHelper;
  8. /**
  9. * Get status of a roller by barcode
  10. *
  11. * Return an array with product information.
  12. *
  13. * @param string $code Roller QR code
  14. *
  15. * @return array|mixed Data without useless information
  16. *
  17. * @url POST RENT_checkScooterStatus
  18. */
  19. // Kellenek még a foglalási adatok ha vanank egy tömbben. HA nincs ,akkor üres tömb.
  20. public function RENT_checkScooterStatus(string $code)
  21. {
  22. ApiBbusLog::ScooterRentLog("RENT_checkScooterStatus: ___START___");
  23. $scooterData = $this->getAllScooterDataByCode($code);
  24. ApiBbusLog::ScooterRentLog("RENT_checkScooterStatus: ___END___");
  25. return $scooterData;
  26. }
  27. /**
  28. * Get status of a roller by barcode and Invoice number
  29. *
  30. * Return an array with product information.
  31. *
  32. * @param string $code Roller QR code
  33. * @param string $invoice_number Invoice number
  34. *
  35. * @return array|mixed Data without useless information
  36. *
  37. * @url POST RENT_checkScooterStatusAfterTicketValidation
  38. */
  39. // Kellenek még a foglalási adatok ha vanank egy tömbben. HA nincs ,akkor üres tömb.
  40. public function RENT_checkScooterStatusAfterTicketValidation(string $code, $invoice_number)
  41. {
  42. ApiBbusLog::ScooterRentLog("RENT_checkScooterStatusAfterTicketValidation: ___START___");
  43. $scooterDataArray = [];
  44. $scooterData = $this->getAllScooterDataByCode($code);
  45. $scooterDataArray['scooter'] = $scooterData;
  46. $scooterDataArray['isRented'] = $this->db->num_rows($this->checkRentHistory($scooterData->rowid, $invoice_number)) > 0;
  47. ApiBbusLog::ScooterRentLog("RENT_checkScooterStatusAfterTicketValidation: ___END___");
  48. return $scooterDataArray;
  49. }
  50. /**
  51. * Get available statuses of a roller
  52. *
  53. * Return an array with product information.
  54. *
  55. * @return array|mixed Data without useless information
  56. *
  57. * @url GET RENT_GetAvailableStatuses
  58. */
  59. public function RENT_GetAvailableStatuses()
  60. {
  61. ApiBbusLog::ScooterRentLog("RENT_GetAvailableStatuses: ___START___");
  62. $statuses = [];
  63. $sql = "SELECT status_id FROM llx_rollerstorage_statuses WHERE status_id != 112";
  64. $result = $this->db->query($sql);
  65. if ($this->db->num_rows($result) > 0) {
  66. while ($row = $this->db->fetch_object($result)) {
  67. $statuses[] = $row->status_id;
  68. }
  69. ApiBbusLog::ScooterRentLog("RENT_GetAvailableStatuses: ___END___");
  70. return $statuses;
  71. }
  72. ApiBbusLog::ScooterRentLog("RENT_GetAvailableStatuses: ___END___");
  73. return $statuses;
  74. }
  75. /**
  76. * Get all statuses of a rollers
  77. *
  78. * Return an array with product information.
  79. *
  80. * @return array|mixed Data without useless information
  81. *
  82. * @url GET RENT_GetAllStatuses
  83. */
  84. public function RENT_GetAllStatuses()
  85. {
  86. ApiBbusLog::ScooterRentLog("RENT_GetAllStatuses: ___START___");
  87. $statuses = [];
  88. $sql = "SELECT status_id FROM llx_rollerstorage_statuses";
  89. $result = $this->db->query($sql);
  90. if ($this->db->num_rows($result) > 0) {
  91. while ($row = $this->db->fetch_object($result)) {
  92. $statuses[] = $row->status_id;
  93. }
  94. ApiBbusLog::ScooterRentLog("RENT_GetAllStatuses: ___END___");
  95. return $statuses;
  96. }
  97. ApiBbusLog::ScooterRentLog("RENT_GetAllStatuses: ___END___");
  98. return $statuses;
  99. }
  100. /**
  101. * Set status of a scooter
  102. *
  103. * @param string $statuscode
  104. * @param string $rowid
  105. *
  106. * @return array|mixed Data without useless information
  107. *
  108. * @url POST RENT_setScooterStatus
  109. */
  110. public function RENT_setScooterStatus($statuscode, $rowid)
  111. {
  112. $statuses = $this->RENT_GetAllStatuses();
  113. if (!in_array($statuscode, $statuses)) {
  114. throw new RestException(404, 'Status Not found.');
  115. }
  116. ApiBbusLog::ScooterRentLog("RENT_setScooterStatus: ___START___");
  117. $sql = "UPDATE llx_inventory SET status = {$statuscode} WHERE rowid = {$rowid}";
  118. $result = $this->db->query($sql);
  119. $inventoryObj = new Inventory($this->db);
  120. $resultupdated = $inventoryObj->fetch($rowid);
  121. ApiBbusLog::ScooterRentLog("Scooter status changed to {$inventoryObj->status}!");
  122. ApiBbusLog::ScooterRentLog("RENT_setScooterStatus: ___END___");
  123. return $inventoryObj->status;
  124. }
  125. /**
  126. * Connect scooter and facture
  127. *
  128. * @param string $inventory_id
  129. * @param string $ticketQRCode
  130. *
  131. * @return array|mixed Data without useless information
  132. *
  133. * @url POST RENT_ConnectFactureAndScooter
  134. */
  135. public function RENT_ConnectFactureAndScooter($inventory_id, $ticketQRCode, $isReplaced = false)
  136. {
  137. ApiBbusLog::ScooterRentLog("RENT_ConnectFactureAndScooter: ___START___");
  138. $QRCodeData = explode('_', $ticketQRCode);
  139. if ($this->isRented($QRCodeData, $inventory_id)) {
  140. ApiBbusLog::ScooterRentLog("RENT_ConnectFactureAndScooter: Already rented -> return: FALSE");
  141. return false;
  142. }
  143. $error = 0;
  144. $this->db->begin();
  145. $ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($inventory_id, $QRCodeData[0], $QRCodeData[1], null);
  146. (int)$ScooterRentHistoryId > 0 ?: $error++;
  147. ApiBbusLog::ScooterRentLog("RentHistoryRecord: {$ScooterRentHistoryId}");
  148. $this->RENT_setScooterStatus("112", $inventory_id) == '112' ?: $error++;
  149. $ticket_id = $this->getTicketId($QRCodeData[0], $QRCodeData[1]);
  150. if (!$isReplaced) {
  151. $sql = "SELECT * FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
  152. $result = $this->db->query($sql);
  153. while ($row = $this->db->fetch_object($result)) {
  154. $this->updateScooterBbticket($row) > 0 ?: $error++;
  155. }
  156. }
  157. $this->checkError($error);
  158. $this->db->commit();
  159. ApiBbusLog::ScooterRentLog("RENT_ConnectFactureAndScooter: ___END___");
  160. return "OK";
  161. }
  162. /**
  163. * Scooter isExpired check
  164. *
  165. * @param string $code Scooter title code
  166. *
  167. * @return array|mixed Data without useless information
  168. *
  169. * @url POST RENT_isExpired
  170. */
  171. public function RENT_isExpired($code)
  172. {
  173. ApiBbusLog::ScooterRentLog("RENT_isExpired: ___START___");
  174. $scooterData = $this->getScooterRowidByCode($code);
  175. $rollerrenthistoryData = $this->getRollerrenthistoryDataByInvetoryId($scooterData);
  176. return $this->checkExpireDate($rollerrenthistoryData->ticket_id);
  177. }
  178. /**
  179. * Scooter Take back
  180. *
  181. * @param string $code
  182. * @param int $roller_status
  183. *
  184. * @return array|mixed Data without useless information
  185. *
  186. * @url POST RENT_scooterTakeBack
  187. */
  188. public function RENT_scooterTakeBack($code, $roller_status)
  189. {
  190. ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: ___START___");
  191. # Ezt hívjuk meg akkor is, ha csere van (státusz: 110 (In stock) / 114 (Faulty)) és akkor is ha rendes visszavételezés történik, de ebben az esetben 111-es státusszal.
  192. $error = 0;
  193. $scooterData = $this->getScooterRowidByCode($code);
  194. $rollerrenthistoryData = $this->getRollerrenthistoryDataByInvetoryId($scooterData);
  195. $this->db->begin();
  196. # Create RollerRentHistory Record
  197. $ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($scooterData, $rollerrenthistoryData->invoice_number, null, $rollerrenthistoryData->ticket_id, 0);
  198. (int)$ScooterRentHistoryId > 0 ?: $error++;
  199. # Update Rollerstatus in llx_inventory
  200. $this->RENT_setScooterStatus($roller_status, $scooterData) == $roller_status ?: $error++;
  201. $this->checkError($error);
  202. $this->db->commit();
  203. ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: {$code} -> Rent record created: {$ScooterRentHistoryId}");
  204. ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: ___END___");
  205. return 'OK';
  206. }
  207. /**
  208. * Scooter replacement
  209. *
  210. * @param string $code_1 //Scooter QRcode
  211. * @param string $scooter_1_status //1st Scooter status
  212. * @param string $code_2 //Scooter QRcode
  213. *
  214. * @return array|mixed Data without useless information
  215. *
  216. * @url POST RENT_scooterReplacement
  217. */
  218. public function RENT_scooterReplacement($code_1, $scooter_1_status, $code_2)
  219. {
  220. $error = 0;
  221. ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___START___");
  222. # Take the scooter back
  223. $this->RENT_scooterTakeBack($code_1, $scooter_1_status);
  224. $scooter_2_data = $this->RENT_checkScooterStatus($code_2);
  225. if ($scooter_2_data->status == '110') {
  226. $isReplaced = true;
  227. $scooter_1_data = $this->RENT_checkScooterStatus($code_1);
  228. $invoice_number = $this->getInvoiceNumberByInvetoryId($scooter_1_data->rowid);
  229. $ticket_id = $this->getTicketIdByInvoiceNumberFromRollerRentHistory($invoice_number, $code_1);
  230. $this->db->begin();
  231. $ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($scooter_2_data->rowid, $invoice_number, null, $ticket_id, 1);
  232. (int)$ScooterRentHistoryId > 0 ?: $error++;
  233. ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: {$code_2} -> Rent record created: {$ScooterRentHistoryId}");
  234. $this->RENT_setScooterStatus("112", $scooter_2_data->rowid) == '112' ?: $error++;
  235. if (!$isReplaced) {
  236. $sql = "SELECT * FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
  237. $result = $this->db->query($sql);
  238. while ($row = $this->db->fetch_object($result)) {
  239. $this->updateScooterBbticket($row) > 0 ?: $error++;
  240. }
  241. }
  242. $this->checkError($error);
  243. $this->db->commit();
  244. ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___END___");
  245. return "OK";
  246. //$this->RENT_ConnectFactureAndScooter($scooter_2_data->rowid, $invoice_number, $ticket_id, true);
  247. } else {
  248. return $scooter_2_data->status;
  249. }
  250. ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___END___");
  251. return 'OK';
  252. }
  253. }