From f8c0c5a78c4de29fffefcf79ba2c4588d34100dc Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 11 Jun 2025 09:38:21 +0200 Subject: [PATCH] Extend order selection logic with registration number and date range in `orders_o`, update Slack messaging for modules, and implement potential order detection in XLVask tasks. --- services/nginx/app/classes/slack.php | 6 ++- .../modules/xlvask/helpers/xlvask_tasks.php | 9 ++++- .../xlvask/helpers/xlvask_usage_log.php | 40 +++++++++++++++++++ services/nginx/app/objects/orders_o.php | 39 ++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/services/nginx/app/classes/slack.php b/services/nginx/app/classes/slack.php index 4f9f99fe..8ff14c3f 100644 --- a/services/nginx/app/classes/slack.php +++ b/services/nginx/app/classes/slack.php @@ -122,9 +122,13 @@ class slack implements notification_i . "Status: $status"; } - public function send_message(string $string): void + public function send_message(string $string, string $module = null): void { global $SLACK_DEFAULT_WEBHOOK; + // Format the message if a module is provided + if ($module !== null) { + $string = "*$module*\n" . $string; + } // Send the message to the slack webhook self::add_log(self::send_webhook_message($string, $SLACK_DEFAULT_WEBHOOK)); } diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php b/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php index 42aceeef..8a18c098 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php @@ -250,8 +250,15 @@ class xlvask_tasks } echo '#' . PHP_EOL; if (!$log->isLinkedToOrder()) { + echo '# Checking for orders that might be addressing this wash.' . PHP_EOL; + // Check if an order that matches this wash exists. + if ($log->getPotentialOrder() !== null) { + echo '# This wash might be associated with an order: ' . $log->getPotentialOrder()->id . PHP_EOL; + } else { + // If no order is found, we will generate a new order. + echo '# No potential order found for this wash.' . PHP_EOL; + } echo '# This wash is not linked to an order, generating a new order.' . PHP_EOL; - } echo '# ------------------------' . PHP_EOL; } diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php b/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php index abe12297..72ad5a3c 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php @@ -2,6 +2,7 @@ namespace helpers; +use classes\xlvask; use Exception; use objects\departments_o; use objects\orders_o; @@ -514,4 +515,43 @@ class xlvask_usage_log // If the hall is not set or does not contain a lane number, return null return null; } + + public function getCustomer(): xlvask_customer + { + $xlvask = new xlvask(); + $matches = $xlvask->getCustomers( + $this->CustomerId, + ); + if (isset($matches[0]) && $matches[0] instanceof xlvask_customer) { + return $matches[0]; + } else { + throw new Exception("No customer found with ID: " . $this->CustomerId); + } + } + + /** + * @throws Exception + */ + public function getPotentialOrder(): ?orders_o + { + // To prevent duplicate orders, we will check if an order that looks like it belongs to this wash already exists. + // There will be false positives, but we will try to minimize them. + // Usually, vehicles aren't washed more than once a day, so we will check for orders with the same license plate and within a 24-hour period. + $date = [ + 'start' => date('Y-m-d H:i:s', strtotime($this->StartTime) - 86400), // 24 hours before the start time + 'end' => date('Y-m-d H:i:s', strtotime($this->FinishTime) + 86400), // 24 hours after the finish time + ]; + echo 'Looking for potential order for vehicle ' . $this->RegistrationNumber . ' between ' . $date['start'] . ' and ' . $date['end'] . PHP_EOL; + $order = (new orders_o())->selectByRegistrationNumberAndDateRange( + $this->RegistrationNumber, + $date['start'], + $date['end'], + ); + if ($order) { + // If an order is found, return it + return $order; + } + // If no order is found, return null + return null; + } } \ No newline at end of file diff --git a/services/nginx/app/objects/orders_o.php b/services/nginx/app/objects/orders_o.php index a133cca0..6d8e5624 100644 --- a/services/nginx/app/objects/orders_o.php +++ b/services/nginx/app/objects/orders_o.php @@ -7,6 +7,7 @@ use classes\motorapi; use classes\object_property; use classes\response; use Exception; +use helpers\xlvask_usage_log; use traits\db_object_t; class orders_o extends db @@ -674,4 +675,42 @@ class orders_o extends db $this->select((int)$result[0]['id']); return $this; } + + /** + * Select an order by its registration number and date range (This is used to find potential duplicate orders) + * @param int|string|null $RegistrationNumber The registration number to select the order by + * @param string $dateFrom The start date of the date range (inclusive) "Y-m-d H:i:s" format + * @param string $dateTo The end date of the date range (inclusive) "Y-m-d H:i:s" format + * @return orders_o|null The selected order object or null if no order is found + * @throws Exception If the select fails + * @see xlvask_usage_log::getPotentialOrder() + */ + public function selectByRegistrationNumberAndDateRange(int|string|null $RegistrationNumber, string $dateFrom, string $dateTo): orders_o|null + { + global $db; + // Validate the date range + if (strtotime($dateFrom) === false || strtotime($dateTo) === false) { + throw new Exception('Invalid date range provided'); + } + if (strtotime($dateFrom) > strtotime($dateTo)) { + throw new Exception('The start date cannot be after the end date'); + } + // Ensure the registration number is a string + $RegistrationNumber = (string)$RegistrationNumber; + if (empty($RegistrationNumber)) { + throw new Exception('Registration number cannot be empty'); + } + // Prepare the SQL query to find the order by registration number and date range + $RegistrationNumber = $db->escape_string($RegistrationNumber); + $dateFrom = $db->escape_string($dateFrom); + $dateTo = $db->escape_string($dateTo); + // Select the order by registration number and date range + $sql = "SELECT id FROM $this->table WHERE (reg_1 = '$RegistrationNumber' OR reg_2 = '$RegistrationNumber' OR reg_3 = '$RegistrationNumber') AND created_at BETWEEN '$dateFrom' AND '$dateTo' AND deleted_at IS NULL"; + $result = $db->query($sql); + if ($result->num_rows > 0) { + $row = $result->fetch_assoc(); + return $this->select((int)$row['id']); + } + return null; // No order found with the given registration number and date range + } } \ No newline at end of file