roller_handling.class.php 9.9 KB

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