Bläddra i källkod

roller status update for is expired

szollosil 5 månader sedan
förälder
incheckning
c2b939d83c

+ 1 - 0
custom/bbus/class/roller_handling.class.php

@@ -28,6 +28,7 @@ trait RollerHandling
 	{
 		ApiBbusLog::ScooterRentLog("RENT_checkScooterStatus: ___START___");
 		$scooterData = $this->getAllScooterDataByCode($code);
+		$scooterData->isExpired = $this->RENT_isExpired($code);
 		ApiBbusLog::ScooterRentLog("RENT_checkScooterStatus: ___END___");
 		return $scooterData;
 	}

+ 280 - 0
custom/bbus/class/roller_handling.class.php.bak

@@ -0,0 +1,280 @@
+<?php
+
+require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/rollerrenthistory.class.php';
+require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/roller_handling_helper.class.php';
+require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/rollerhistory.class.php';
+
+
+use Luracast\Restler\RestException;
+
+trait RollerHandling
+{
+	use RollerHandligHelper;
+
+	/**
+	 * Get status of a roller by barcode
+	 *
+	 * Return an array with product information.
+	 *
+	 * @param  string $code	Roller QR code
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST RENT_checkScooterStatus
+	 */
+
+	// Kellenek még a foglalási adatok ha vanank egy tömbben. HA nincs ,akkor üres tömb.
+	public function RENT_checkScooterStatus(string $code)
+	{
+		ApiBbusLog::ScooterRentLog("RENT_checkScooterStatus: ___START___");
+		$scooterData = $this->getAllScooterDataByCode($code);
+		ApiBbusLog::ScooterRentLog("RENT_checkScooterStatus: ___END___");
+		return $scooterData;
+	}
+
+
+	/**
+	 * Get status of a roller by barcode and Invoice number
+	 *
+	 * Return an array with product information.
+	 *
+	 * @param  string $code	Roller QR code
+	 * @param  string $invoice_number	Invoice number
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST RENT_checkScooterStatusAfterTicketValidation
+	 */
+
+	// Kellenek még a foglalási adatok ha vanank egy tömbben. HA nincs ,akkor üres tömb.
+	public function RENT_checkScooterStatusAfterTicketValidation(string $code, $invoice_number)
+	{
+		ApiBbusLog::ScooterRentLog("RENT_checkScooterStatusAfterTicketValidation: ___START___");
+		$scooterDataArray = [];
+		$scooterData = $this->getAllScooterDataByCode($code);
+		$scooterDataArray['scooter'] = $scooterData;
+		$scooterDataArray['isRented'] = $this->db->num_rows($this->checkRentHistory($scooterData->rowid, $invoice_number)) > 0;
+		ApiBbusLog::ScooterRentLog("RENT_checkScooterStatusAfterTicketValidation: ___END___");
+		return $scooterDataArray;
+	}
+
+
+	/**
+	 * Get available statuses of a roller
+	 *
+	 * Return an array with product information.
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url GET RENT_GetAvailableStatuses
+	 */
+	public function RENT_GetAvailableStatuses()
+	{
+		ApiBbusLog::ScooterRentLog("RENT_GetAvailableStatuses: ___START___");
+		$statuses = [];
+		$sql = "SELECT status_id FROM llx_rollerstorage_statuses WHERE status_id != 112";
+		$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_GetAvailableStatuses: ___END___");
+			return $statuses;
+		}
+		ApiBbusLog::ScooterRentLog("RENT_GetAvailableStatuses: ___END___");
+		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
+	 *
+	 * @param  string $statuscode
+	 * @param  string $rowid
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST RENT_setScooterStatus
+	 */
+	public function RENT_setScooterStatus($statuscode, $rowid)
+	{
+		$statuses = $this->RENT_GetAllStatuses();
+		if (!in_array($statuscode, $statuses)) {
+			throw new RestException(404, 'Status Not found.');
+		}
+		$inventoryObjFROM = new Inventory($this->db);
+		$resultFROM = $inventoryObjFROM->fetch($rowid);
+		$fromStatus = $inventoryObjFROM->status;
+		ApiBbusLog::ScooterRentLog("RENT_setScooterStatus: ___START___");
+		$sql = "UPDATE llx_inventory SET status = {$statuscode} WHERE rowid = {$rowid}";
+		$result = $this->db->query($sql);
+		$inventoryObj = new Inventory($this->db);
+		$resultupdated = $inventoryObj->fetch($rowid);
+		$this->createHistoryRecord($inventoryObj, $fromStatus);
+		ApiBbusLog::ScooterRentLog("Scooter status changed to {$inventoryObj->status}!");
+		ApiBbusLog::ScooterRentLog("RENT_setScooterStatus: ___END___");
+		return $inventoryObj->status;
+	}
+
+	/**
+	 * Connect scooter and facture
+	 *
+	 * @param  string $inventory_id
+	 * @param  string $ticketQRCode
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST RENT_ConnectFactureAndScooter
+	 */
+	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, $QRCodeData[0], $QRCodeData[1], null);
+		(int)$ScooterRentHistoryId > 0 ?: $error++;
+		ApiBbusLog::ScooterRentLog("RentHistoryRecord: {$ScooterRentHistoryId}");
+		$this->RENT_setScooterStatus("112", $inventory_id) == '112' ?: $error++;
+		$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)) {
+				$this->updateScooterBbticket($row) > 0 ?: $error++;
+			}
+		}
+		$this->checkError($error);
+		$this->db->commit();
+		ApiBbusLog::ScooterRentLog("RENT_ConnectFactureAndScooter: ___END___");
+		return "OK";
+	}
+
+	/**
+	 * Scooter isExpired check
+	 *
+	 * @param  string $code Scooter title code
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST RENT_isExpired
+	 */
+	public function RENT_isExpired($code)
+	{
+		ApiBbusLog::ScooterRentLog("RENT_isExpired: ___START___");
+		$scooterData = $this->getScooterRowidByCode($code);
+		$rollerrenthistoryData = $this->getRollerrenthistoryDataByInvetoryId($scooterData);
+		return $this->checkExpireDate($rollerrenthistoryData->ticket_id);
+	}
+
+	/**
+	 * Scooter Take back
+	 *
+	 * @param  string $code
+	 * @param  int $roller_status
+	 *
+	 * @return array|mixed Data without useless information
+	 *
+	 * @url POST RENT_scooterTakeBack
+	 */
+	public function RENT_scooterTakeBack($code, $roller_status)
+	{
+		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);
+		$rollerrenthistoryData = $this->getRollerrenthistoryDataByInvetoryId($scooterData);
+		$this->db->begin();
+		# Create RollerRentHistory Record
+		$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_scooterTakeBack: {$code} -> Rent record created: {$ScooterRentHistoryId}");
+		ApiBbusLog::ScooterRentLog("RENT_scooterTakeBack: ___END___");
+		return 'OK';
+	}
+
+	/**
+	 * Scooter replacement
+	 *
+	 * @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
+	 *
+	 * @url POST RENT_scooterReplacement
+	 */
+	public function RENT_scooterReplacement($code_1, $scooter_1_status, $code_2)
+	{
+		$error = 0;
+		ApiBbusLog::ScooterRentLog("RENT_scooterReplacement: ___START___");
+		# Take the scooter back
+		$scooter_1_data = $this->RENT_checkScooterStatus($code_1);
+		$scooter_2_data = $this->RENT_checkScooterStatus($code_2);
+		if ($scooter_1_data->status == $scooter_1_status) {
+			$scooter_1_status = '110';
+		}
+		$this->RENT_scooterTakeBack($code_1, $scooter_1_status);
+		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->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___");
+		return 'OK';
+	}
+}

+ 8 - 3
custom/bbus/class/roller_handling_helper.class.php

@@ -191,6 +191,7 @@ trait RollerHandligHelper
 
     public function checkExpireDate($ticket_id)
     {
+        $array = [];
         $now = date("Y-m-d H:i:s", dol_now());
         if ($ticket_id !== '') {
             $sql = "SELECT expire_at FROM llx_bbus_bbticket WHERE rowid = {$ticket_id}";
@@ -198,16 +199,20 @@ trait RollerHandligHelper
             if ($this->db->num_rows($result) > 0) {
                 while ($row = $this->db->fetch_object($result)) {
                     if (strtotime($row->expire_at) < strtotime($now)) {
-                        return true;
+                        $array['isExpired'] = true;
+                        $array['date'] = $row->expire_at;
+                        return $array;
                     } else {
-                        return false;
+                        $array['isExpired'] = false;
+                        return $array;
                     }
                 }
             }
         } else {
             throw new RestException(508);
         }
-        return false;
+        $array['isExpired'] = false;
+        return $array;
     }
 
     public function getTicketIdByInvoiceNumber($invoice_number, $timestamp)

+ 253 - 0
custom/bbus/class/roller_handling_helper.class.php.bak

@@ -0,0 +1,253 @@
+<?php
+
+use Luracast\Restler\RestException;
+
+trait RollerHandligHelper
+{
+    public function getAllScooterDataByCode($code)
+    {
+        $deviceTypes = ['5' => 'Seev Citycoco', '6' => 'WIZTEM'];
+        $rentableStatuses = [110, 115];
+        $sql = "SELECT 
+            i.rowid, i.entity, i.ref, i.fk_warehouse, i.status, i.title
+            , ent.ref, ent.lieu
+            , ie.device_type
+            FROM llx_inventory as i 
+            INNER JOIN llx_entrepot AS ent ON ent.rowid = i.fk_warehouse
+            INNER JOIN llx_inventory_extrafields as ie ON ie.fk_object = i.rowid
+            WHERE i.title ILIKE '%{$code}%'";
+        $result = $this->db->query($sql);
+        if ($this->db->num_rows($result) > 0) {
+            while ($row = $this->db->fetch_object($result)) {
+                $row->device_type = $deviceTypes[$row->device_type];
+                $row->isRentable = in_array($row->status, $rentableStatuses);
+                return $row;
+            }
+        }
+        return [];
+    }
+
+    public function getScooterRowidByCode($code)
+    {
+        $sql = "SELECT * FROM llx_inventory as i WHERE i.title ILIKE '%{$code}%'";
+        $result = $this->db->query($sql);
+        if ($this->db->num_rows($result) > 0) {
+            while ($row = $this->db->fetch_object($result)) {
+                return $row->rowid;
+            }
+        }
+        return [];
+    }
+
+    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);
+    }
+
+    private function checkError($error)
+    {
+        if ($error > 0) {
+            $this->db->rollback();
+            ApiBbusLog::ScooterRentLog('ConnectFactureAndScooter Rollback...');
+            throw new RestException(509);
+        }
+    }
+
+    private function updateScooterBbticket($selectedTicket)
+    {
+        $ticketChecker = new TicketChecker();
+        $validated_at = date('Y-m-d H:i:s');
+        $duration = $this->getDurationByProductId($selectedTicket->ticket_id);
+        $expire_at = $this->getExpireAt($selectedTicket, $duration, $validated_at);
+        $usage = $this->setUsage($selectedTicket);
+        $sql = "UPDATE " . $this->db->prefix() . "bbus_bbticket SET validated_at = '" . $validated_at . "', expire_at = '" . $expire_at . "'";
+        $sql .= ", usage = '" . $usage . "'";
+        $sql .= " WHERE rowid = " . $selectedTicket->rowid;
+        return $this->db->query($sql);
+    }
+
+    private function getDurationByProductId($ticket_id)
+    {
+        $sql = "SELECT p.duration as duration  FROM " . $this->db->prefix() . "product AS p WHERE rowid = " . $ticket_id;
+        $result = $this->db->query($sql);
+        while ($row = $this->db->fetch_object($result)) {
+            return $row->duration;
+        }
+    }
+
+    private function getExpireAt($obj, $duration, $validated_at)
+    {
+        return date('Y-m-d H:i:s', strtotime($validated_at . ' +' . substr($duration, 0, -1) . ' ' . $this->getIntervalTimeByDuration()));
+    }
+
+    private function setUsage($selectedTicket)
+    {
+        $usage = (int)$selectedTicket->usage + 1;
+        return $usage;
+    }
+
+    private function getIntervalTimeByDuration()
+    {
+        $duration = $this->duration[-1];
+        switch ($duration) {
+            case 'h':
+                return 'hours';
+            case 'd':
+                return 'days';
+            case 'w':
+                return 'weeks';
+            case 'm':
+                return 'months';
+            case 'y':
+                return 'years';
+            default:
+                return 'hours';
+        }
+    }
+
+    public function checkRentHistory($rowid, $invoice_number)
+    {
+        $sql = "SELECT * FROM llx_rollerstorage_rollerrenthistory WHERE inventory_id = {$rowid} AND invoice_number = '{$invoice_number}' AND status = 1";
+        return $this->db->query($sql);
+    }
+
+    public function getInvoiceNumberByInvetoryId($inventory_id)
+    {
+        $invoice_number = '';
+        $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)) {
+                $invoice_number = $row->invoice_number;
+            }
+        }
+        return $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)
+    {
+        global $conf;
+        $bbTicketData = [];
+        $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
+            INNER JOIN llx_bbus_basicservices as bbs ON bbs.rowid = CAST(pre.basic_service AS integer)
+            WHERE bbtip.invoice_number = '{$invoice_number}'
+            AND bbtip.printing_date_timestamp = '{$timestamp}'
+			AND bbs.server_host = '{$conf->global->LOCAL_SERVER_HOST}'
+            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)) {
+                $bbTicketData = $row;
+            }
+        }
+        return $bbTicketData;
+    }
+
+    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 ($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);
+        }
+        return false;
+    }
+
+    public function getTicketIdByInvoiceNumber($invoice_number, $timestamp)
+    {
+        $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;
+    }
+
+    public function createHistoryRecord($obj, $fromStatus){
+		global $user;
+		//print_r($obj);exit;
+
+		$statuses2 = new Statuses($this->db);
+        $status_id_from_statuses = $statuses2->getRowIdFromStatusId($this->db, $fromStatus);
+    	$status_id_to_statuses = $statuses2->getRowIdFromStatusId($this->db, $obj->status);
+		
+		$rollerhistory = new RollerHistory($this->db);
+		$rollerhistory->fk_inventory = $obj->id;
+		$rollerhistory->warehouse_from = $obj->fk_warehouse;
+		$rollerhistory->warehouse_to = $obj->fk_warehouse;
+		$rollerhistory->status_from = $status_id_from_statuses;
+		$rollerhistory->status_to = $status_id_to_statuses;
+		$rollerhistory->status = 0;
+		if ($rollerhistory->create($user) == -1) {
+			dol_syslog("Nem sikerult a rollerstorage_history mentese.");
+		}
+	}
+}