Add OTC order date picker
This commit is contained in:
@@ -127,10 +127,10 @@ function create_direct_sales_order(PDO $pdo, array $fields): array
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
"INSERT INTO sales_order (
|
||||
external_ref, party_id, order_source, order_status, payment_status, payment_method_id,
|
||||
external_ref, party_id, order_source, order_date, order_status, payment_status, payment_method_id,
|
||||
amount_net, amount_shipping, amount_tax, amount_discount, total_amount, currency, imported_at, created_at, updated_at
|
||||
) VALUES (
|
||||
'', :party_id, 'direct', 'imported', 'paid', :payment_method_id,
|
||||
'', :party_id, 'direct', :order_date, 'imported', 'paid', :payment_method_id,
|
||||
:amount_net, 0, 0, 0, :total_amount, 'CHF', NOW(), NOW(), NOW()
|
||||
)
|
||||
RETURNING id, external_ref"
|
||||
|
||||
@@ -30,7 +30,7 @@ if (!is_array($data)) {
|
||||
json_response(400, ['ok' => false, 'error' => 'JSON object expected']);
|
||||
}
|
||||
|
||||
$required = ['products', 'totalPrice', 'paymentMethod', 'billing'];
|
||||
$required = ['products', 'totalPrice', 'orderDate', 'paymentMethod', 'billing'];
|
||||
foreach ($required as $field) {
|
||||
if (!array_key_exists($field, $data)) {
|
||||
json_response(422, ['ok' => false, 'error' => "Missing field: {$field}"]);
|
||||
@@ -39,6 +39,7 @@ foreach ($required as $field) {
|
||||
|
||||
$products = $data['products'];
|
||||
$totalPrice = parse_number($data['totalPrice']);
|
||||
$orderDate = trim((string) $data['orderDate']);
|
||||
$paymentMethodCode = trim((string) $data['paymentMethod']);
|
||||
$billing = is_array($data['billing']) ? $data['billing'] : [];
|
||||
|
||||
@@ -50,6 +51,12 @@ if ($totalPrice === null || $totalPrice <= 0) {
|
||||
json_response(422, ['ok' => false, 'error' => 'Invalid total price']);
|
||||
}
|
||||
|
||||
$orderDateTime = DateTimeImmutable::createFromFormat('!Y-m-d', $orderDate);
|
||||
$orderDateErrors = DateTimeImmutable::getLastErrors();
|
||||
if ($orderDateTime === false || $orderDateErrors === false || $orderDateErrors['warning_count'] > 0 || $orderDateErrors['error_count'] > 0) {
|
||||
json_response(422, ['ok' => false, 'error' => 'Invalid order date']);
|
||||
}
|
||||
|
||||
$resolvedProducts = [];
|
||||
$totalQty = 0.0;
|
||||
foreach ($products as $product) {
|
||||
@@ -94,6 +101,7 @@ try {
|
||||
':payment_method_id' => $paymentMethodId,
|
||||
':amount_net' => $totalPrice,
|
||||
':total_amount' => $totalPrice,
|
||||
':order_date' => $orderDateTime->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
$orderId = (int) $order['id'];
|
||||
|
||||
Reference in New Issue
Block a user