Add xlvask_potential_order_matches_o object and logic to manage potential order matches in XLVask tasks.

This commit is contained in:
Jepp9350
2025-06-16 09:37:05 +02:00
parent c2fb7cf873
commit d72c534b2f
2 changed files with 143 additions and 0 deletions
@@ -5,6 +5,7 @@ namespace helpers;
use Exception;
use objects\orders_o;
use objects\users_o;
use objects\xlvask_potential_order_matches_o;
class xlvask_tasks
{
@@ -255,6 +256,19 @@ class xlvask_tasks
//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) {
$xlvask_potential_order_matches_o = new xlvask_potential_order_matches_o();
// Check if the potential order match is already in the database (Prevent duplicates)
if (!$xlvask_potential_order_matches_o->doesWashPotentialOrderMatchExist(
$log->WashId,
)) {
// There's no match in the database, so we will add it.
$xlvask_potential_order_matches_o->add(
$log->WashId,
$log->getPotentialOrder()->id,
$customer->customerId,
$customer->externId, // Customer number
);
}
//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.
@@ -0,0 +1,129 @@
<?php
namespace objects;
use classes\db;
use classes\object_property;
use classes\slack;
use Exception;
use traits\db_object_t;
class xlvask_potential_order_matches_o extends db
{
use db_object_t;
public object_property $wash_id; // The wash id (In XL Vask - UUID)
public object_property $order; // The order id that this potential order match is for (In XL Vask - UUID)
public object_property $customerId; // The customer id (In XL Vask - UUID)
public object_property $customer_number; // The customer number
public object_property $ignore_duplicate; // Whether to ignore duplicate matches (0 = No, 1 = Yes)
public object_property $department; // The department id
public object_property $created_at; // The created at timestamp
public object_property $updated_at; // The updated at timestamp
public object_property $deleted_at; // The deleted at timestamp
public function structure(): void
{
$this->setTable('xlvask_potential_order_matches');
}
/**
* Convert the object to an array
* @throws Exception If the object is not selected
* @throws Exception If the object is not found
*/
public function asArray(): array
{
self::requireSelected();
return [
'id' => (int)$this->id,
'wash_id' => (string)$this->wash_id->value(),
'order' => (int)$this->order->value(),
'customer_id' => (int)$this->customerId->value(),
'customer_number' => $this->customer_number->value(),
'ignore_duplicate' => (int)$this->ignore_duplicate->value(),
'department' => (int)$this->department->value(),
'created_at' => (int)$this->created_at->value(),
'updated_at' => (int)$this->updated_at->value(),
];
}
/**
* Add a new object to the database
* @param string $washId The wash id (In XL Vask - UUID)
* @param int $order The order id that this potential order match is for (In XL Vask - UUID)
* @param string $customerId The customer id (In XL Vask - UUID)
* @param int $customerNumber The customer number
* @return void
* @throws Exception If the object was not created successfully
*/
public function add(
string $washId,
int $order,
string $customerId,
int $customerNumber,
): void
{
$tmp_id = self::add_object([
'wash_id' => $washId,
'order' => (int)$order,
'customer_id' => $customerId,
'customer_number' => (int)$customerNumber,
'department' => (int)(new orders_o())->select((int)$order)->department_id->value(),
]);
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
self::notifyDepartment();
}
public function getObjectProperties(): void
{
$this->wash_id = new object_property($this->table, $this->id, 'wash_id', 'string', true);
$this->order = new object_property($this->table, $this->id, 'order', 'int', true);
$this->customerId = new object_property($this->table, $this->id, 'customer_id', 'string', true);
$this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true);
$this->ignore_duplicate = new object_property($this->table, $this->id, 'ignore_duplicate', 'int', true);
$this->department = new object_property($this->table, $this->id, 'department', 'int', true);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'int', true);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'int', true);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'int', false);
}
public function objectChanged(): void
{
//TODO: Add cache invalidation
}
/**
* Notify the department about the new potential order match
* @throws Exception If the object is not selected
* @throws Exception If the department is not found
* @throws Exception If the department notification fails
*/
public function notifyDepartment(): void
{
$this->requireSelected();
$slack = new slack();
$slack->send_message('
A new potential order match has been created:
- Wash ID: ' . $this->wash_id->value() . '
- Order ID: ' . $this->order->value() . '
- Customer ID: ' . $this->customerId->value() . '
- Customer Number: ' . $this->customer_number->value() . '
- Department: ' . $this->department->value(),
'xlvask_potential_order_matches');
}
public function doesWashPotentialOrderMatchExist(string $WashId): bool
{
// Check if a potential order match exists for the given wash ID
return count($this->getFieldsWhere(
[
'wash_id' => $WashId,
],
[
'id'
])) > 0;
}
}