Correct independent stock lot activation flow

This commit is contained in:
gitea_admin committed 2026-09-08 14:38:54 +02:00
1 parent 9eb9cd6e0c
commit 3f15756596
5 files changed
+324 -48

No files matched your search

+30 -11
View File
@@ -74,6 +74,7 @@ try {
(int) ($data['lotId'] ?? 0),
(string) ($currentUser['username'] ?? $currentUser['email'] ?? 'unknown')
);
sync_open_lot_activation_selection($pdo, $productId);
json_response(200, ['ok' => true, 'productId' => $productId, 'availableQty' => get_lager_product_available_qty($pdo, $productId)]);
}
@@ -81,8 +82,22 @@ try {
$productId = (int) ($data['productId'] ?? 0);
$lotNumber = trim((string) ($data['lotNumber'] ?? ''));
$lotId = (int) ($data['lotId'] ?? 0);
if ($lotNumber === '' && $lotId <= 0) {
throw new InvalidArgumentException('Naurua-Chargennummer fehlt');
$manufacturerPartyId = $data['manufacturerPartyId'] ?? null;
$manufacturerPartyId = $manufacturerPartyId === null || $manufacturerPartyId === '' ? null : (int) $manufacturerPartyId;
$manufacturerLotNumber = trim((string) ($data['manufacturerLotNumber'] ?? ''));
$mfgDate = trim((string) ($data['mfgDate'] ?? ''));
$expiryDate = trim((string) ($data['expiryDate'] ?? ''));
$purchaseDate = trim((string) ($data['purchaseDate'] ?? ''));
$receiptQty = isset($data['receiptQty']) && $data['receiptQty'] !== '' ? (float) $data['receiptQty'] : 0.0;
$targetQty = isset($data['targetQty']) && $data['targetQty'] !== '' ? (float) $data['targetQty'] : null;
if ($lotNumber === '' || $manufacturerPartyId === null || $manufacturerLotNumber === '' || $mfgDate === '' || $expiryDate === '' || $purchaseDate === '') {
throw new InvalidArgumentException('Alle Chargenfelder sind Pflichtfelder');
}
if ($lotId <= 0 && $receiptQty <= 0) {
throw new InvalidArgumentException('Eingangsmenge muss groesser als 0 sein');
}
if ($lotId > 0 && $targetQty !== null && $targetQty <= 0) {
throw new InvalidArgumentException('Menge muss groesser als 0 sein');
}
if ($lotId > 0) {
$lotStmt = $pdo->prepare(
@@ -97,20 +112,16 @@ try {
throw new InvalidArgumentException('Offene Charge nicht gefunden');
}
} else {
$lotId = get_or_create_open_lot($pdo, $productId);
$lotId = create_open_lot($pdo, $productId);
}
$manufacturerPartyId = $data['manufacturerPartyId'] ?? null;
$manufacturerPartyId = $manufacturerPartyId === null || $manufacturerPartyId === '' ? null : (int) $manufacturerPartyId;
$receiptQty = isset($data['receiptQty']) && $data['receiptQty'] !== '' ? (float) $data['receiptQty'] : 0.0;
$targetQty = isset($data['targetQty']) && $data['targetQty'] !== '' ? (float) $data['targetQty'] : null;
$preparedLot = update_stock_lot_traceability(
$pdo,
$lotId,
$manufacturerPartyId,
isset($data['manufacturerLotNumber']) ? (string) $data['manufacturerLotNumber'] : null,
isset($data['mfgDate']) ? (string) $data['mfgDate'] : null,
isset($data['expiryDate']) ? (string) $data['expiryDate'] : null,
isset($data['purchaseDate']) ? (string) $data['purchaseDate'] : null,
$manufacturerLotNumber,
$mfgDate,
$expiryDate,
$purchaseDate,
(string) ($currentUser['username'] ?? $currentUser['email'] ?? 'unknown'),
$receiptQty,
0.0,
@@ -119,11 +130,19 @@ try {
$targetQty,
$lotId > 0 && $lotNumber === ''
);
sync_open_lot_activation_selection($pdo, $productId);
refresh_lager_stock_lot_forecast_for_product($pdo, $productId);
$forecast = get_lager_product_forecast($pdo, $productId);
json_response(200, ['ok' => true, 'productId' => $productId, 'lot' => $preparedLot, 'balance' => get_stock_lot_balance($pdo, $lotId), 'availableQty' => get_lager_product_available_qty($pdo, $productId), 'forecast' => $forecast]);
}
if ($action === 'select_open_lot') {
$productId = (int) ($data['productId'] ?? 0);
$lotId = (int) ($data['lotId'] ?? 0);
select_open_lot_for_activation($pdo, $productId, $lotId);
json_response(200, ['ok' => true, 'productId' => $productId, 'selectedLotId' => $lotId]);
}
json_response(422, ['ok' => false, 'error' => 'Unsupported action']);
} catch (JsonException) {
json_response(400, ['ok' => false, 'error' => 'Invalid JSON payload']);