Fix virtual shortage lot activation and warning

This commit is contained in:
gitea_admin committed 2026-09-08 16:55:37 +02:00
1 parent 17efaee53f
commit 8846a57b43
3 files changed
+129 -11

No files matched your search

+98 -1
View File
@@ -523,7 +523,7 @@ function sync_open_lot_activation_selection(PDO $pdo, int $productId): void
"SELECT sl.id
FROM stock_lot sl
INNER JOIN v_stock_lot_balance v ON v.stock_lot_id = sl.id
WHERE sl.product_id = :product_id
WHERE product_id = :product_id
AND sl.status = 'open'
AND NULLIF(TRIM(sl.lot_number), '') IS NOT NULL
AND sl.manufacturer_party_id IS NOT NULL
@@ -590,6 +590,103 @@ function sync_open_lot_activation_selection(PDO $pdo, int $productId): void
}
}
function promote_open_lot_when_virtual_shortage_is_current(PDO $pdo, int $productId, int $lotId): bool
{
$startedTransaction = !$pdo->inTransaction();
if ($startedTransaction) {
$pdo->beginTransaction();
}
try {
$currentStmt = $pdo->prepare(
"SELECT id
FROM stock_lot
WHERE product_id = :product_id
AND status = 'virtual_shortage'
ORDER BY id
LIMIT 1
FOR UPDATE"
);
$currentStmt->execute([':product_id' => $productId]);
if ($currentStmt->fetchColumn() === false || !is_qualified_open_lot($pdo, $lotId)) {
if ($startedTransaction) {
$pdo->commit();
}
return false;
}
$realCurrentStmt = $pdo->prepare(
"SELECT id
FROM stock_lot
WHERE product_id = :product_id
AND status = 'current'
LIMIT 1
FOR UPDATE"
);
$realCurrentStmt->execute([':product_id' => $productId]);
$realCurrent = $realCurrentStmt->fetch();
if (is_array($realCurrent)) {
$realCurrentBalanceStmt = $pdo->prepare(
"SELECT COALESCE(qty_net, 0)
FROM v_stock_lot_balance
WHERE stock_lot_id = :lot_id"
);
$realCurrentBalanceStmt->execute([':lot_id' => (int) $realCurrent['id']]);
$realCurrentQty = (float) ($realCurrentBalanceStmt->fetchColumn() ?: 0);
} else {
$realCurrentQty = 0.0;
}
if ($realCurrentQty > 0.0000001) {
if ($startedTransaction) {
$pdo->commit();
}
return false;
}
$lotStmt = $pdo->prepare(
"SELECT id
FROM stock_lot
WHERE id = :lot_id
AND product_id = :product_id
AND status = 'open'
FOR UPDATE"
);
$lotStmt->execute([':lot_id' => $lotId, ':product_id' => $productId]);
if ($lotStmt->fetchColumn() === false) {
if ($startedTransaction) {
$pdo->commit();
}
return false;
}
if (is_array($realCurrent)) {
$closeStmt = $pdo->prepare(
"UPDATE stock_lot
SET status = 'closed', updated_at = NOW()
WHERE id = :lot_id"
);
$closeStmt->execute([':lot_id' => (int) $realCurrent['id']]);
}
$promoteStmt = $pdo->prepare(
"UPDATE stock_lot
SET status = 'current', next_active = FALSE, updated_at = NOW()
WHERE id = :lot_id"
);
$promoteStmt->execute([':lot_id' => $lotId]);
if ($startedTransaction) {
$pdo->commit();
}
return true;
} catch (Throwable $error) {
if ($startedTransaction && $pdo->inTransaction()) {
$pdo->rollBack();
}
throw $error;
}
}
function select_open_lot_for_activation(PDO $pdo, int $productId, int $lotId): void
{
if ($productId <= 0 || $lotId <= 0) {