Переглянути джерело

rollerhandling and apiproductlist fixes

szollosil 1 рік тому
батько
коміт
6aa79e4836

+ 1 - 1
custom/bbus/class/apiproductlisthelper.class.php

@@ -169,7 +169,7 @@ class ApiProductListHelper
 		global $conf;
 		$bookingApi = new BookingApi($this->db);
 		$eventData = $this->geteventData($product_id);
-		if($eventData->is_event){
+		if($eventData->is_event == '1'){
 			
 			if($eventData->server_host == $conf->global->LOCAL_SERVER_HOST){
 				$result = $bookingApi->localCheckAvailablePlaces($product_id);

+ 76 - 22
custom/bbus/class/roller_handling.class.php

@@ -83,6 +83,31 @@ trait RollerHandling
 		return $statuses;
 	}
 
+	/**
+	 * Get all statuses of a rollers
+	 *
+	 * Return an array with product information.
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url GET RENT_GetAllStatuses
+	 */
+	public function RENT_GetAllStatuses()
+	{
+		ApiBbusLog::ScooterRentLog("RENT_GetAllStatuses: ___START___");
+		$statuses = [];
+		$sql = "SELECT status_id FROM llx_rollerstorage_statuses";
+		$result = $this->db->query($sql);
+		if ($this->db->num_rows($result) > 0) {
+			while ($row = $this->db->fetch_object($result)) {
+				$statuses[] = $row->status_id;
+			}
+			ApiBbusLog::ScooterRentLog("RENT_GetAllStatuses: ___END___");
+			return $statuses;
+		}
+		ApiBbusLog::ScooterRentLog("RENT_GetAllStatuses: ___END___");
+		return $statuses;
+	}
 
 	/**
 	 * Set status of a scooter
@@ -96,6 +121,10 @@ trait RollerHandling
 	 */
 	public function RENT_setScooterStatus($statuscode, $rowid)
 	{
+		$statuses = $this->RENT_GetAllStatuses();
+		if (!in_array($statuscode, $statuses)) {
+			throw new RestException(404, 'Status Not found.');
+		}
 		ApiBbusLog::ScooterRentLog("RENT_setScooterStatus: ___START___");
 		$sql = "UPDATE llx_inventory SET status = {$statuscode} WHERE rowid = {$rowid}";
 		$result = $this->db->query($sql);
@@ -107,26 +136,31 @@ trait RollerHandling
 	}
 
 	/**
-	 * Connect scooter and event
+	 * Connect scooter and facture
 	 *
 	 * @param  string $inventory_id
-	 * @param  string $invoice_number
-	 * @param  int $ticket_id
+	 * @param  string $ticketQRCode
 	 *
 	 * @return array|mixed Data without useless information
 	 *
 	 * @url POST RENT_ConnectFactureAndScooter
 	 */
-	public function RENT_ConnectFactureAndScooter($inventory_id, $invoice_number, $ticket_id, $isReplaced = false)
+	public function RENT_ConnectFactureAndScooter($inventory_id, $ticketQRCode, $isReplaced = false)
 	{
 		ApiBbusLog::ScooterRentLog("RENT_ConnectFactureAndScooter: ___START___");
+		$QRCodeData = explode('_', $ticketQRCode);
+		if ($this->isRented($QRCodeData, $inventory_id)) {
+			ApiBbusLog::ScooterRentLog("RENT_ConnectFactureAndScooter:  Already rented -> return: FALSE");
+			return false;
+		}
 		$error = 0;
 		$this->db->begin();
-		$ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($inventory_id, $invoice_number);
+		$ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($inventory_id, $QRCodeData[0], $QRCodeData[1], null);
 		(int)$ScooterRentHistoryId > 0 ?: $error++;
 		ApiBbusLog::ScooterRentLog("RentHistoryRecord: {$ScooterRentHistoryId}");
 		$this->RENT_setScooterStatus("112", $inventory_id) == '112' ?: $error++;
-		if(!$isReplaced){
+		$ticket_id = $this->getTicketId($QRCodeData[0], $QRCodeData[1]);
+		if (!$isReplaced) {
 			$sql = "SELECT * FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
 			$result = $this->db->query($sql);
 			while ($row = $this->db->fetch_object($result)) {
@@ -140,7 +174,7 @@ trait RollerHandling
 	}
 
 	/**
-	 * Scooter Take back
+	 * Scooter isExpired check
 	 *
 	 * @param  string $code Scooter title code
 	 *
@@ -152,8 +186,8 @@ trait RollerHandling
 	{
 		ApiBbusLog::ScooterRentLog("RENT_isExpired: ___START___");
 		$scooterData = $this->getScooterRowidByCode($code);
-		$invoice_number = $this->getInvoiceNumberByInvetoryId($scooterData);
-		return $this->checkExpireDate($invoice_number);
+		$rollerrenthistoryData = $this->getRollerrenthistoryDataByInvetoryId($scooterData);
+		return $this->checkExpireDate($rollerrenthistoryData->ticket_id);
 	}
 
 	/**
@@ -168,30 +202,30 @@ trait RollerHandling
 	 */
 	public function RENT_scooterTakeBack($code, $roller_status)
 	{
-		ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___START___");
-
+		ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: ___START___");
 		# 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.
 		$error = 0;
 		$scooterData = $this->getScooterRowidByCode($code);
-		$invoice_number = $this->getInvoiceNumberByInvetoryId($scooterData);
+		$rollerrenthistoryData = $this->getRollerrenthistoryDataByInvetoryId($scooterData);
 		$this->db->begin();
 		# Create RollerRentHistory Record
-		$ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($scooterData, $invoice_number, 0);
+		$ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($scooterData, $rollerrenthistoryData->invoice_number, null, $rollerrenthistoryData->ticket_id, 0);
 		(int)$ScooterRentHistoryId > 0 ?: $error++;
 		# Update Rollerstatus in llx_inventory
 		$this->RENT_setScooterStatus($roller_status, $scooterData)  == $roller_status ?: $error++;
 		$this->checkError($error);
 		$this->db->commit();
-		ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___END___");
+		ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: {$code} -> Rent record created: {$ScooterRentHistoryId}");
+		ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: ___END___");
 		return 'OK';
 	}
 
 	/**
-	 * Scooter Take back
+	 * Scooter replacement
 	 *
-	 * @param  string $code_1
-	 * @param  string $scooter_1_status
-	 * @param  string $code_2
+	 * @param  string $code_1				//Scooter QRcode
+	 * @param  string $scooter_1_status		//1st Scooter status
+	 * @param  string $code_2				//Scooter QRcode
 	 *
 	 * @return array|mixed Data without useless information
 	 *
@@ -199,15 +233,35 @@ trait RollerHandling
 	 */
 	public function RENT_scooterReplacement($code_1, $scooter_1_status, $code_2)
 	{
+		$error = 0;
 		ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___START___");
+		# Take the scooter back
 		$this->RENT_scooterTakeBack($code_1, $scooter_1_status);
 		$scooter_2_data = $this->RENT_checkScooterStatus($code_2);
-		if($scooter_2_data->status == '110'){
+		if ($scooter_2_data->status == '110') {
+			$isReplaced = true;
 			$scooter_1_data = $this->RENT_checkScooterStatus($code_1);
 			$invoice_number = $this->getInvoiceNumberByInvetoryId($scooter_1_data->rowid);
-			$ticket_id = $this->getTicketIdByInvoiceNumber($invoice_number);
-			$this->RENT_ConnectFactureAndScooter($scooter_2_data->rowid, $invoice_number, $ticket_id, true);
-		}else{
+			$ticket_id = $this->getTicketIdByInvoiceNumberFromRollerRentHistory($invoice_number, $code_1);
+			$this->db->begin();
+			$ScooterRentHistoryId = $this->CreateScooterRentHistoryRecord($scooter_2_data->rowid, $invoice_number, null, $ticket_id, 1);
+			(int)$ScooterRentHistoryId > 0 ?: $error++;
+			ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: {$code_2} -> Rent record created: {$ScooterRentHistoryId}");
+			$this->RENT_setScooterStatus("112", $scooter_2_data->rowid) == '112' ?: $error++;
+			if (!$isReplaced) {
+				$sql = "SELECT * FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
+				$result = $this->db->query($sql);
+				while ($row = $this->db->fetch_object($result)) {
+					$this->updateScooterBbticket($row) > 0 ?: $error++;
+				}
+			}
+			$this->checkError($error);
+			$this->db->commit();
+			ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___END___");
+			return "OK";
+			//$this->RENT_ConnectFactureAndScooter($scooter_2_data->rowid, $invoice_number, $ticket_id, true);
+
+		} else {
 			return $scooter_2_data->status;
 		}
 		ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___END___");

+ 76 - 10
custom/bbus/class/roller_handling_helper.class.php

@@ -37,12 +37,16 @@ trait RollerHandligHelper
         return [];
     }
 
-    private function CreateScooterRentHistoryRecord($inventory_id, $invoice_number, $status = 1)
+    public function CreateScooterRentHistoryRecord($inventory_id, $invoice_number, $timestamp = null, $ticket_id, $status = 1)
     {
+        if (!is_null($timestamp)) {
+            $ticket_id = $this->getTicketId($invoice_number, $timestamp);
+        }
         ApiBbusLog::ScooterRentLog('RentHistoryRecord: ___START___');
         global $user;
         $rollerRentHistoryObj = new RollerRentHistory($this->db);
         $rollerRentHistoryObj->inventory_id = $inventory_id;
+        $rollerRentHistoryObj->ticket_id = $ticket_id;
         $rollerRentHistoryObj->invoice_number = $invoice_number;
         $rollerRentHistoryObj->status = $status;
         return $rollerRentHistoryObj->create($user);
@@ -128,10 +132,35 @@ trait RollerHandligHelper
         return $invoice_number;
     }
 
-    public function getBbticketDataByInvoiceNumber($invoice_number)
+    public function getRollerrenthistoryDataByInvetoryId($inventory_id)
+    {
+        $rollerrenthistoryData = '';
+        $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$inventory_id} AND status = 1 ORDER BY date_creation DESC LIMIT 1";
+        $result = $this->db->query($sql);
+        if ($this->db->num_rows($result) > 0) {
+            while ($row = $this->db->fetch_object($result)) {
+                $rollerrenthistoryData = $row;
+            }
+        }
+        return $rollerrenthistoryData;
+    }
+
+    public function getTicketId($invoice_number, $timestamp)
+    {
+        $ticketData = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
+        return $ticketData->rowid;
+    }
+
+    public function getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp)
     {
         $bbTicketData = [];
-        $sql = "SELECT * FROM llx_bbus_bbticket WHERE invoice_number = '{$invoice_number}'";
+        $sql = "SELECT bbt.* FROM llx_bbus_bbticketinvoiceprinting as bbtip 
+            INNER JOIN llx_product_extrafields as pre ON pre.fk_object = bbtip.product_id
+            INNER JOIN llx_bbus_bbticket as bbt ON bbt.rowid = bbtip.ticket_id
+            WHERE bbtip.invoice_number = '{$invoice_number}'
+            AND bbtip.printing_date_timestamp = '{$timestamp}'
+            AND pre.basic_service IN ('2','3')
+            ORDER BY bbtip.rowid DESC limit 1";
         $result = $this->db->query($sql);
         if ($this->db->num_rows($result) > 0) {
             while ($row = $this->db->fetch_object($result)) {
@@ -141,13 +170,35 @@ trait RollerHandligHelper
         return $bbTicketData;
     }
 
-    public function checkExpireDate($invoice_number)
+    public function getTicketIdByInvoiceNumberFromRollerRentHistory($invoice_number, $scooter_code)
+    {
+        $scooterData = $this->getScooterRowidByCode($scooter_code);
+        $sql = "SELECT ticket_id FROM public.llx_rollerstorage_rollerrenthistory 
+            WHERE invoice_number = '{$invoice_number}' AND inventory_id = {$scooterData}
+            ORDER BY rowid DESC LIMIT 1";
+        $result = $this->db->query($sql);
+        if ($this->db->num_rows($result) > 0) {
+            while ($row = $this->db->fetch_object($result)) {
+                return $row->ticket_id;
+            }
+        }
+        return null;
+    }
+
+    public function checkExpireDate($ticket_id)
     {
         $now = date("Y-m-d H:i:s", dol_now());
-        if ($invoice_number !== '') {
-            $ticketData = $this->getBbticketDataByInvoiceNumber($invoice_number);
-            if (strtotime($ticketData->expire_at) < strtotime($now)) {
-                return true;
+        if ($ticket_id !== '') {
+            $sql = "SELECT expire_at FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
+            $result = $this->db->query($sql);
+            if($this->db->num_rows($result) > 0){
+                while($row = $this->db->fetch_object($result)){
+                    if (strtotime($row->expire_at) < strtotime($now)) {
+                        return true;
+                    } else {
+                        return false;
+                    }
+                }
             }
         } else {
             throw new RestException(508);
@@ -155,9 +206,24 @@ trait RollerHandligHelper
         return false;
     }
 
-    public function getTicketIdByInvoiceNumber($invoice_number)
+    public function getTicketIdByInvoiceNumber($invoice_number, $timestamp)
     {
-        $bbticketObj = $this->getBbticketDataByInvoiceNumber($invoice_number);
+        $bbticketObj = $this->getBbticketDataByInvoiceNumber_and_Timestamp($invoice_number, $timestamp);
         return $bbticketObj->ticket_id;
     }
+
+    public function isRented($QRCodeData, $inventory_id)
+    {
+        $rollerrenthistory = false;
+        $sql = "SELECT status FROM public.llx_rollerstorage_rollerrenthistory 
+            WHERE invoice_number = '{$QRCodeData[0]}' AND inventory_id = {$inventory_id} AND status = 1
+            ORDER BY rowid DESC LIMIT 1";
+        $result = $this->db->query($sql);
+        if ($this->db->num_rows($result) > 0) {
+            while ($row = $this->db->fetch_object($result)) {
+                $rollerrenthistory = $row->status == 1;
+            }
+        }
+        return $rollerrenthistory;
+    }
 }