roller_handling.class.php 10.0 KB

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