Add support for closed_at field in order invoices
Introduce the closed_at field to track closure dates for order invoices. Updated validation, processing logic, and database integration to ensure correct handling of the new field. Adjusted related routes to pass and store the closed_at value where applicable.
This commit is contained in:
@@ -485,7 +485,7 @@ class collected_order_invoices_o extends db
|
||||
* @return self The created object
|
||||
* @throws Exception If the object was not created successfully
|
||||
*/
|
||||
public function add(int $customer_number, ?string $name = null, ?string $notes = null, ?int $processor = null): self
|
||||
public function add(int $customer_number, ?string $name = null, ?string $notes = null, ?int $processor = null, ?string $closed_at = null): self
|
||||
{
|
||||
global /** @var db $db */
|
||||
$db;
|
||||
@@ -500,13 +500,21 @@ class collected_order_invoices_o extends db
|
||||
if (!empty($processor)) {
|
||||
$processor = $db->escape_string($processor);
|
||||
}
|
||||
if (!empty($closed_at)) {
|
||||
$closed_at = $db->escape_string($closed_at);
|
||||
// Check if the closed_at date is in the correct format (YYYY-MM-DD HH:MM:SS)
|
||||
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $closed_at)) {
|
||||
throw new Exception('Closed at date is not in the correct format');
|
||||
}
|
||||
}
|
||||
// Require the customer number to be of a valid customer
|
||||
self::requireValidCustomer($customer_number);
|
||||
// Add the object
|
||||
$tmp_id = self::add_object([
|
||||
'customer_number' => (int)$customer_number,
|
||||
'name' => $name,
|
||||
'notes' => $notes
|
||||
'notes' => $notes,
|
||||
...(!empty($closed_at) ? ['closed_at' => (string)$closed_at] : []),
|
||||
]);
|
||||
self::select((int)$tmp_id);
|
||||
self::requireSelected();
|
||||
|
||||
@@ -104,7 +104,7 @@ class orderInvoicesRoute
|
||||
(new logs_o())->add('orderInvoices', 'global', 1, $user->id, 'ADD_COLLECTED_INVOICE', 'User added a collected order invoice');
|
||||
// Require the customer number, and validate its type and length
|
||||
self::requireParameters(['customer_number']);
|
||||
self::requireType((int)self::getParameter('customer_number'), 'int');
|
||||
self::requireType((int)self::getParameter('customer_number'), self::type_int());
|
||||
self::requireMinLength('customer_number', 1);
|
||||
self::requireMaxLength('customer_number', 10);
|
||||
// Require the customer number to be above 0
|
||||
@@ -116,30 +116,47 @@ class orderInvoicesRoute
|
||||
$name = null;
|
||||
$notes = null;
|
||||
$processor = null;
|
||||
$closed_at = null;
|
||||
// Check if the name is set
|
||||
if (self::isParametersSet(['name'])) {
|
||||
self::requireType((string)self::getParameter('name'), 'string');
|
||||
self::requireType((string)self::getParameter('name'), self::type_string());
|
||||
self::requireMinLength('name', 1);
|
||||
self::requireMaxLength('name', 255);
|
||||
$name = (string)self::getParameter('name');
|
||||
}
|
||||
// Check if the notes are set
|
||||
if (self::isParametersSet(['notes'])) {
|
||||
self::requireType((string)self::getParameter('notes'), 'string');
|
||||
self::requireType((string)self::getParameter('notes'), self::type_string());
|
||||
$notes = (string)self::getParameter('notes');
|
||||
}
|
||||
// Check if the processor is set
|
||||
if (self::isParametersSet(['processor'])) {
|
||||
self::requireType((int)self::getParameter('processor'), 'int');
|
||||
self::requireType((int)self::getParameter('processor'), self::type_int());
|
||||
$processor = (int)self::getParameter('processor');
|
||||
}
|
||||
// Check if the closed_at is set
|
||||
if (self::isParametersSet(['closed_at'])) {
|
||||
// Check if the closed date is set, and validate its type
|
||||
if (!!self::getParameter('closed_at')) {
|
||||
self::requireType((string)self::getParameter('closed_at'), self::type_string());
|
||||
$closed_at = (string)self::getParameter('closed_at');
|
||||
// Check if the closed date is valid
|
||||
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $closed_at)) {
|
||||
$response->error('Invalid closed date format', 400);
|
||||
}
|
||||
// Set the time to 00:00:01z
|
||||
$closed_at = date('Y-m-d H:i:s', strtotime($closed_at . ' 00:00:01'));
|
||||
}
|
||||
}
|
||||
|
||||
// Add the collected order invoice
|
||||
$collected_order_invoices = new collected_order_invoices_o();
|
||||
$collected_order_invoices->add(
|
||||
$customer->id,
|
||||
$name,
|
||||
$notes,
|
||||
$processor
|
||||
$processor,
|
||||
$closed_at ?: null,
|
||||
);
|
||||
$response->success($collected_order_invoices->asArray());
|
||||
} else {
|
||||
|
||||
@@ -65,8 +65,11 @@ class ordersRoute
|
||||
'processor' => (int)(new collected_order_invoices_o())->select($order['invoice_collection_id'])->processor->value() ?? null,
|
||||
];
|
||||
}
|
||||
// Get the customer
|
||||
$tmp_customer = (new users_o())->getUserByCustomerNumber((int)$order['customer_id']);
|
||||
// Add the customer name to the order
|
||||
$order['customer_name'] = (new users_o())->getCustomerName($order['customer_id']);
|
||||
$order['user_id'] = (int)$tmp_customer->id;
|
||||
/** @var array $order */
|
||||
return $order;
|
||||
},
|
||||
@@ -179,6 +182,10 @@ class ordersRoute
|
||||
if (isset($data['reg_1'])) {
|
||||
$order->reg_1->set($data['reg_1']);
|
||||
}
|
||||
// Check if the invoice collection is set
|
||||
if (isset($data['invoice_collection_id'])) {
|
||||
$order->invoice_collection_id->set((int)$data['invoice_collection_id']);
|
||||
}
|
||||
// If the department ID is set, validate it
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'EDIT_ORDER', 'Successfully updated an order (ID: ' . $data['id'] . ')');
|
||||
|
||||
Reference in New Issue
Block a user