Link OTC sales to market days
This commit is contained in:
1 parent
759e337976
commit
47c09a3e4c
6 files changed
+167
-9
No files matched your search
@@ -146,10 +146,12 @@ function create_direct_sales_order(PDO $pdo, array $fields): array
|
||||
$stmt = $pdo->prepare(
|
||||
'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
|
||||
amount_net, amount_shipping, amount_tax, amount_discount, total_amount, currency,
|
||||
market_occurrence_id, market_occurrence_day_id, 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()
|
||||
:amount_net, 0, 0, 0, :total_amount, \'CHF\',
|
||||
:market_occurrence_id, :market_occurrence_day_id, NOW(), NOW(), NOW()
|
||||
)
|
||||
RETURNING id, external_ref'
|
||||
);
|
||||
@@ -161,6 +163,8 @@ function create_direct_sales_order(PDO $pdo, array $fields): array
|
||||
':payment_method_id' => $fields[':payment_method_id'] ?? null,
|
||||
':amount_net' => $fields[':amount_net'] ?? null,
|
||||
':total_amount' => $fields[':total_amount'] ?? null,
|
||||
':market_occurrence_id' => $fields[':market_occurrence_id'] ?? null,
|
||||
':market_occurrence_day_id' => $fields[':market_occurrence_day_id'] ?? null,
|
||||
]);
|
||||
|
||||
$row = $stmt->fetch();
|
||||
|
||||
@@ -41,6 +41,20 @@ $products = $data['products'];
|
||||
$orderDate = trim((string) $data['orderDate']);
|
||||
$payments = $data['payments'];
|
||||
$billing = is_array($data['billing']) ? $data['billing'] : [];
|
||||
$marketOccurrenceId = $data['marketOccurrenceId'] ?? null;
|
||||
$marketOccurrenceDayId = $data['marketOccurrenceDayId'] ?? null;
|
||||
|
||||
if (($marketOccurrenceId === null) !== ($marketOccurrenceDayId === null)) {
|
||||
json_response(422, ['ok' => false, 'error' => 'Veranstaltung und Durchführungstag müssen gemeinsam angegeben werden.']);
|
||||
}
|
||||
|
||||
if ($marketOccurrenceId !== null && (!ctype_digit((string) $marketOccurrenceId) || (int) $marketOccurrenceId <= 0)) {
|
||||
json_response(422, ['ok' => false, 'error' => 'Ungültige Veranstaltung.']);
|
||||
}
|
||||
|
||||
if ($marketOccurrenceDayId !== null && (!ctype_digit((string) $marketOccurrenceDayId) || (int) $marketOccurrenceDayId <= 0)) {
|
||||
json_response(422, ['ok' => false, 'error' => 'Ungültiger Durchführungstag.']);
|
||||
}
|
||||
|
||||
if (!is_array($products) || count($products) === 0) {
|
||||
json_response(422, ['ok' => false, 'error' => 'No products specified']);
|
||||
@@ -115,6 +129,27 @@ try {
|
||||
$pdo = connect_database($env);
|
||||
$pdo->beginTransaction();
|
||||
|
||||
if ($marketOccurrenceId !== null) {
|
||||
$marketLinkStmt = $pdo->prepare(
|
||||
'SELECT 1
|
||||
FROM market_occurrence o
|
||||
INNER JOIN market_definition m ON m.id = o.market_definition_id
|
||||
INNER JOIN market_occurrence_day d ON d.occurrence_id = o.id
|
||||
WHERE o.id = :occurrence_id
|
||||
AND d.id = :day_id
|
||||
AND m.archived_at IS NULL
|
||||
AND o.archived_at IS NULL
|
||||
LIMIT 1'
|
||||
);
|
||||
$marketLinkStmt->execute([
|
||||
':occurrence_id' => (int) $marketOccurrenceId,
|
||||
':day_id' => (int) $marketOccurrenceDayId,
|
||||
]);
|
||||
if ($marketLinkStmt->fetchColumn() === false) {
|
||||
throw new InvalidArgumentException('Die Veranstaltung oder der Durchführungstag ist nicht aktiv oder nicht miteinander verknüpft.');
|
||||
}
|
||||
}
|
||||
|
||||
$locations = get_default_location_ids($pdo);
|
||||
|
||||
$paymentMethodIds = [];
|
||||
@@ -135,6 +170,8 @@ try {
|
||||
':amount_net' => $totalPrice,
|
||||
':total_amount' => $totalPrice,
|
||||
':order_date' => $orderDateTime->format('Y-m-d H:i:s'),
|
||||
':market_occurrence_id' => $marketOccurrenceId === null ? null : (int) $marketOccurrenceId,
|
||||
':market_occurrence_day_id' => $marketOccurrenceDayId === null ? null : (int) $marketOccurrenceDayId,
|
||||
]);
|
||||
|
||||
$orderId = (int) $order['id'];
|
||||
@@ -221,7 +258,16 @@ try {
|
||||
'excelTrigger' => $excelTrigger,
|
||||
'message' => 'OTC order created successfully',
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
} catch (InvalidArgumentException $e) {
|
||||
if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
|
||||
json_response(422, [
|
||||
'ok' => false,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
if (isset($pdo) && $pdo instanceof PDO && $pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user