Add logic to handle duplicate potential order matches, include shouldIgnoreDuplicate method, and introduce route to ignore duplicates with permission checks.

This commit is contained in:
Jepp9350
2025-06-16 11:31:13 +02:00
parent 3987cf5de9
commit 8edc0f9312
3 changed files with 72 additions and 4 deletions
@@ -6,6 +6,7 @@ use classes\xlvask;
use Exception;
use objects\departments_o;
use objects\orders_o;
use objects\xlvask_potential_order_matches_o;
class xlvask_usage_log
{
@@ -551,8 +552,11 @@ class xlvask_usage_log
$date['end'],
);
if ($order) {
// If an order is found, return it
return $order;
// Make sure the order duplicate is not ignored
if (!(new xlvask_potential_order_matches_o())->shouldIgnoreDuplicate($this->WashId)) {
// If an order is found, return it
return $order;
}
}
// If no order is found, return null
return null;
@@ -66,7 +66,7 @@ class xlvask_potential_order_matches_o extends db
int $department,
): void
{
echo 'Wash ID: ' . $washId . ', Order: ' . $order . ', Customer ID: ' . $customer_id . ', Customer Number: ' . $customerNumber . ', Department: ' . $department . "\n";
//echo 'Wash ID: ' . $washId . ', Order: ' . $order . ', Customer ID: ' . $customer_id . ', Customer Number: ' . $customerNumber . ', Department: ' . $department . "\n";
$tmp_id = self::add_object(
[
'wash_id' => $washId,
@@ -77,7 +77,7 @@ class xlvask_potential_order_matches_o extends db
'department' => $department,
]
);
echo 'Temporary ID: ' . $tmp_id . "\n";
//echo 'Temporary ID: ' . $tmp_id . "\n";
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
@@ -122,6 +122,24 @@ class xlvask_potential_order_matches_o extends db
'xlvask_potential_order_matches');
}
public function shouldIgnoreDuplicate(string $WashId): bool
{
if (!$this->doesWashPotentialOrderMatchExist($WashId)) {
return false;
}
// Check if the potential order match for the given wash ID has ignore_duplicate set to 1
$result = $this->getFieldsWhere(
[
'wash_id' => $WashId,
'ignore_duplicate' => 1,
],
[
'id'
]
);
return count($result) > 0;
}
public function doesWashPotentialOrderMatchExist(string $WashId): bool
{
// Check if a potential order match exists for the given wash ID
@@ -4,6 +4,7 @@ namespace routes;
use classes\authentication;
use objects\logs_o;
use objects\xlvask_potential_order_matches_o;
use traits\route_t;
class potentialOrderMatchesRoute
@@ -80,5 +81,50 @@ class potentialOrderMatchesRoute
'list_all_potential_order_matches' => 'List all potential order matches for all users (superuser only)',
]
);
$this->post('/orders/sync/potential-matches/ignore-duplicate', function () {
// Require the user to be logged in
global $response;
self::requirePermission('ignore_duplicate_potential_order_matches');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('potential_order_matches', 'global', 1, $user->id, 'IGNORE_DUPLICATE_POTENTIAL_ORDER_MATCHES', 'User ignored duplicate potential order matches');
// Get the potential order match data from the request
self::requireParameters(['id']);
self::requireType((int)self::getParameter('id'), self::type_int());
self::requireMinValue((int)self::getParameter('id'), 1); // Ensure the ID is a positive integer
$potentialOrderMatch = new xlvask_potential_order_matches_o();
$potentialOrderMatch->select((int)self::getParameter('id'));
if (!$potentialOrderMatch->exists()) {
// Log the incident
(new logs_o())->add('potential_order_matches', 'global', 1, $user->id, 'IGNORE_DUPLICATE_POTENTIAL_ORDER_MATCHES', 'Potential order match not found');
// Return an error response
$response->error('Potential order match not found', 404);
}
// Check if the user has permission to ignore duplicate potential order matches in the department of the potential order match
self::requireDepartmentAccess((int)$potentialOrderMatch->department->value());
// Set the ignore_duplicate field to 1
$potentialOrderMatch->ignore_duplicate->set(1);
// Save the potential order match
$potentialOrderMatch->objectChanged();
// Return a success response
$response->success(
$potentialOrderMatch->asArray(),
200,
);
} else {
// Log the incident
(new logs_o())->add('potential_order_matches', 'global', 1, 0, 'IGNORE_DUPLICATE_POTENTIAL_ORDER_MATCHES', 'No user found, or invalid session');
// Return an error response
$response->error('Invalid session', 400);
}
},
[
'ignore_duplicate_potential_order_matches' => 'Ignore duplicate potential order matches for a specific wash and order',
]
);
}
}