Fix direct order insert
This commit is contained in:
@@ -130,23 +130,38 @@ function create_direct_sales_order(PDO $pdo, array $fields): array
|
||||
throw new RuntimeException('Missing order date');
|
||||
}
|
||||
|
||||
$sequenceStmt = $pdo->query("SELECT nextval(pg_get_serial_sequence('sales_order', 'id')) AS id");
|
||||
$sequenceRow = $sequenceStmt !== false ? $sequenceStmt->fetch() : false;
|
||||
if (!is_array($sequenceRow) || !isset($sequenceRow['id'])) {
|
||||
throw new RuntimeException('Could not reserve order id');
|
||||
}
|
||||
|
||||
$orderId = (int) $sequenceRow['id'];
|
||||
$externalRef = sprintf(
|
||||
'DIR-%s-%05d',
|
||||
(new DateTimeImmutable($orderDate))->format('Ymd'),
|
||||
$orderId
|
||||
);
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
"WITH next_id AS (
|
||||
SELECT nextval(pg_get_serial_sequence('sales_order', 'id')) AS id
|
||||
)
|
||||
INSERT INTO sales_order (
|
||||
'INSERT INTO sales_order (
|
||||
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 (
|
||||
:id, :external_ref, :party_id, \'direct\', :order_date, \'imported\', \'paid\', :payment_method_id,
|
||||
:amount_net, 0, 0, 0, :total_amount, \'CHF\', NOW(), NOW(), NOW()
|
||||
)
|
||||
SELECT
|
||||
next_id.id,
|
||||
'DIR-' || TO_CHAR(CAST(:order_date AS timestamp), 'YYYYMMDD') || '-' || LPAD(next_id.id::text, 5, '0'),
|
||||
:party_id, 'direct', :order_date, 'imported', 'paid', :payment_method_id,
|
||||
:amount_net, 0, 0, 0, :total_amount, 'CHF', NOW(), NOW(), NOW()
|
||||
FROM next_id
|
||||
RETURNING id, external_ref"
|
||||
RETURNING id, external_ref'
|
||||
);
|
||||
$stmt->execute($fields);
|
||||
$stmt->execute([
|
||||
':id' => $orderId,
|
||||
':external_ref' => $externalRef,
|
||||
':party_id' => $fields[':party_id'] ?? null,
|
||||
':order_date' => $orderDate,
|
||||
':payment_method_id' => $fields[':payment_method_id'] ?? null,
|
||||
':amount_net' => $fields[':amount_net'] ?? null,
|
||||
':total_amount' => $fields[':total_amount'] ?? null,
|
||||
]);
|
||||
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) {
|
||||
|
||||
Reference in New Issue
Block a user