Add handheld order processing indicator and caching functionality
- Introduced `is_handheld` field in order creation to identify orders from handheld devices. - Added logic to set, cache, and expire pending handheld orders automatically at midnight. - Enhanced `orders_o` class with `setPendingHandheldIndicator` and `isPendingHandheld` methods. - Updated order processing flow to include `pending_handheld` indicator in responses.
This commit is contained in:
@@ -34,6 +34,7 @@ class orders_o extends db
|
||||
public object_property $booking_id;
|
||||
public object_property $wash_id; // The XL Vask Wash ID, if any
|
||||
public object_property $lane; // The lane used for the order, if any
|
||||
public object_property $using_hand_held; // Whether the order is being processed using a handheld device
|
||||
|
||||
/**
|
||||
* Temporary keys
|
||||
@@ -87,6 +88,7 @@ class orders_o extends db
|
||||
$this->booking_id = new object_property($this->table, $this->id, 'booking_id', 'int', false);
|
||||
$this->wash_id = new object_property($this->table, $this->id, 'wash_id', 'string', false);
|
||||
$this->lane = new object_property($this->table, $this->id, 'lane', 'string', false);
|
||||
$this->using_hand_held = new object_property($this->table, $this->id, 'using_hand_held', 'bool', false);
|
||||
}
|
||||
|
||||
public function getCustomerByOrderId(?string $order_id): users_o
|
||||
@@ -553,6 +555,7 @@ class orders_o extends db
|
||||
'wash_id' => $this->wash_id->value(),
|
||||
'lane' => $this->lane->value(),
|
||||
'closed_at' => (int)$this->invoice_collection_id->value() ? (new collected_order_invoices_o())->select((int)$this->invoice_collection_id->value())->closed_at->value() : null,
|
||||
'pending_handheld' => $this->isPendingHandheld(),
|
||||
];
|
||||
// Cache the object
|
||||
if ($cacheResult) {
|
||||
@@ -1252,4 +1255,40 @@ class orders_o extends db
|
||||
$user->getUserByCustomerNumber((int)$this->customer_id->value());
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or unset the pending handheld cache indicator for the order
|
||||
* This indicator is used to mark orders that are pending handheld processing
|
||||
* The indicator is stored in the cache and expires at midnight
|
||||
* @param bool $isPending Whether to set (true) or unset (false) the pending handheld indicator
|
||||
* @throws Exception If the order is not selected
|
||||
*/
|
||||
public function setPendingHandheldIndicator(bool $isPending = true): void
|
||||
{
|
||||
self::requireSelected();
|
||||
// Set or unset the pending handheld cache indicator for the order
|
||||
$cache_key = 'pending_handheld_cache_indicator';
|
||||
if ($isPending) {
|
||||
// Get the amount of seconds until midnight
|
||||
$secondsUntilMidnight = (int)(strtotime('tomorrow') - time());
|
||||
$this->cache($cache_key, 1);
|
||||
$this->setCachedExpiration($cache_key, $secondsUntilMidnight);
|
||||
$this->using_hand_held->set(true); // Mark the order as using handheld
|
||||
} else {
|
||||
$this->deleteCached($cache_key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether the order is marked as pending handheld processing
|
||||
* @return bool True if the order is marked as pending handheld, false otherwise
|
||||
* @throws Exception If the order is not selected
|
||||
*/
|
||||
public function isPendingHandheld(): bool
|
||||
{
|
||||
self::requireSelected();
|
||||
// Check if the pending handheld cache indicator is set for the order
|
||||
$cache_key = 'pending_handheld_cache_indicator';
|
||||
return $this->getCached($cache_key) === 1;
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@ class ordersRoute
|
||||
// Add the customer name to the order
|
||||
$order['customer_name'] = (new users_o())->getCustomerName($order['customer_id']);
|
||||
$order['user_id'] = (int)$tmp_customer->id;
|
||||
$order['pending_handheld'] = $order_obj->isPendingHandheld();
|
||||
/** @var array $order */
|
||||
return $order;
|
||||
},
|
||||
@@ -131,6 +132,18 @@ class ordersRoute
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
// Check if the required fields are set
|
||||
$data = $this->getData($data, $response);
|
||||
// This is used to determine if the order is created from a handheld device,
|
||||
// If it is, we will add an indicator "pending" to the order, in the cache.
|
||||
// This will automatically be removed at midnight, or when the order is completed.
|
||||
if (isset($data['is_handheld']) && !is_bool($data['is_handheld'])) {
|
||||
$response->error('is_handheld must be a boolean', 400);
|
||||
}
|
||||
if (isset($data['is_handheld'])) {
|
||||
$isHandHeld = (bool)$data['is_handheld'];
|
||||
unset($data['is_handheld']);
|
||||
} else {
|
||||
$isHandHeld = false;
|
||||
}
|
||||
// Validate the department
|
||||
if (!(new departments_o())->getDepartmentById((int)$data['department_id'])) {
|
||||
$response->error('Department not found', 400);
|
||||
@@ -165,12 +178,16 @@ class ordersRoute
|
||||
'reg_3' => (string)$reg_3,
|
||||
...(!empty($data['lane']) ? ['lane' => (int)$data['lane']] : []), // Optional lane
|
||||
...(!empty($data['wash_id']) ? ['wash_id' => (string)$data['wash_id']] : []), // Optional wash ID
|
||||
...(!empty($data['booking_id']) ? ['booking_id' => (int)$data['booking_id']] : []), // Optional booking ID
|
||||
...(!empty($data['booking_id']) ? ['booking_id' => (int)$data['booking_id']] : []), // Optional booking ID
|
||||
'created_at' => (string)($data['created_at'] ?? date('Y-m-d H:i:s')), // Default to current time if not set
|
||||
];
|
||||
// Create the order
|
||||
//$order = (new orders_o())->add((int)$data['customer_id'], $user->id, $data['reference'], $data['notes'], (int)$data['department_id'], (string)$reg_1, (string)$reg_2, (string)$reg_3);
|
||||
$order = (new orders_o())->addArray($new_data);
|
||||
// If the order is created from a handheld device, we will add an indicator "pending" to the order, in the cache.
|
||||
if ($isHandHeld) {
|
||||
$order->setPendingHandheldIndicator();
|
||||
}
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $data['department_id'], 1, $user->id, 'ADD_ORDER', 'Successfully added an order (ID: ' . $data['department_id'] . ')');
|
||||
// Return a success message, containing the orders array
|
||||
|
||||
Reference in New Issue
Block a user