From fb812e147e9ffacfb4d2436d9e1df4d72e7486c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Gla=CC=88ser?= Date: Mon, 15 Jun 2026 14:05:29 +0200 Subject: [PATCH] OTC auf Produkt-ID umstellen --- modules/erp/direktverkauf/api/otc-order.php | 32 ++++++++++++++------- modules/erp/lager/service.php | 10 +------ modules/shared/auth/ui/home.php | 2 +- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/modules/erp/direktverkauf/api/otc-order.php b/modules/erp/direktverkauf/api/otc-order.php index ea17cdc..02d308c 100644 --- a/modules/erp/direktverkauf/api/otc-order.php +++ b/modules/erp/direktverkauf/api/otc-order.php @@ -53,19 +53,19 @@ if ($totalPrice === null || $totalPrice <= 0) { $resolvedProducts = []; $totalQty = 0.0; foreach ($products as $product) { - if (!is_array($product) || !isset($product['qty'], $product['title'])) { - json_response(422, ['ok' => false, 'error' => 'Product missing title or qty']); + if (!is_array($product) || !isset($product['qty'], $product['productId'])) { + json_response(422, ['ok' => false, 'error' => 'Product missing productId or qty']); } $qty = parse_number($product['qty']); - $title = trim((string) $product['title']); - if ($qty === null || $qty <= 0 || $title === '') { - json_response(422, ['ok' => false, 'error' => 'Invalid product quantity or title']); + $productId = (int) $product['productId']; + if ($qty === null || $qty <= 0 || $productId <= 0) { + json_response(422, ['ok' => false, 'error' => 'Invalid product quantity or productId']); } $resolvedProducts[] = [ 'qty' => (float) $qty, - 'title' => $title, + 'productId' => $productId, ]; $totalQty += (float) $qty; } @@ -105,12 +105,24 @@ try { foreach ($resolvedProducts as $product) { $lineNo++; $qty = (float) $product['qty']; - $title = $product['title']; + $productId = (int) $product['productId']; - $resolvedProduct = resolve_otc_product($pdo, $title); - $sellableItemId = resolve_sellable_item_id($pdo, '', $title); + $productStmt = $pdo->prepare( + "SELECT id, sku, name + FROM product + WHERE id = :id + AND status = 'active' + LIMIT 1" + ); + $productStmt->execute([':id' => $productId]); + $resolvedProduct = $productStmt->fetch(); + if (!is_array($resolvedProduct)) { + throw new RuntimeException("Kein aktives ERP-Produkt fuer Produkt-ID {$productId} gefunden"); + } + + $sellableItemId = find_sellable_item_for_product($pdo, $productId); if ($sellableItemId === null) { - throw new RuntimeException("Kein Artikel-Mapping fuer '{$title}' gefunden"); + throw new RuntimeException("Kein Artikel-Mapping fuer Produkt-ID {$productId} gefunden"); } if ($lineNo === count($resolvedProducts)) { diff --git a/modules/erp/lager/service.php b/modules/erp/lager/service.php index a106259..7a944da 100644 --- a/modules/erp/lager/service.php +++ b/modules/erp/lager/service.php @@ -35,15 +35,7 @@ function get_otc_order_form_products(PDO $pdo): array WHERE p.status = 'active' GROUP BY p.id, p.name HAVING COALESCE(SUM(v.qty_net), 0) > 0 - ORDER BY - CASE - WHEN p.name ILIKE '%shiitake%' THEN 1 - WHEN p.name ILIKE '%reishi%' THEN 2 - WHEN p.name ILIKE '%lion''s mane%' THEN 3 - WHEN p.name ILIKE '%chaga%' THEN 4 - ELSE 99 - END, - p.id ASC" + ORDER BY p.id ASC" ); $products = []; diff --git a/modules/shared/auth/ui/home.php b/modules/shared/auth/ui/home.php index ea37a3b..26fe5d0 100644 --- a/modules/shared/auth/ui/home.php +++ b/modules/shared/auth/ui/home.php @@ -438,7 +438,7 @@ function render_auth_home_page(array $user, array $otcProducts = []): void echo " if (!updateFormState()) { return; }"; echo " const products = productInputs"; echo " .filter((input) => getInputValue(input) > 0)"; - echo " .map((input) => ({ title: input.dataset.title, qty: getInputValue(input) }));"; + echo " .map((input) => ({ productId: parseInt(input.dataset.productId || '0', 10), qty: getInputValue(input) }));"; echo " const orderData = {"; echo " products: products,"; echo " totalPrice: parseFloat(totalPriceInput.value || '0') || 0,";