Offene Chargen mit Sicherheitsabfrage loeschen
This commit is contained in:
1 parent
44faf75e28
commit
668ebbc64a
3 files changed
+67
-3
No files matched your search
@@ -65,6 +65,15 @@ try {
|
||||
json_response(200, ['ok' => true, 'lot' => $updatedLot, 'balance' => get_stock_lot_balance($pdo, (int) ($data['lotId'] ?? 0))]);
|
||||
}
|
||||
|
||||
if ($action === 'delete_open_lot') {
|
||||
delete_open_stock_lot(
|
||||
$pdo,
|
||||
(int) ($data['lotId'] ?? 0),
|
||||
(string) ($currentUser['username'] ?? $currentUser['email'] ?? 'unknown')
|
||||
);
|
||||
json_response(200, ['ok' => true]);
|
||||
}
|
||||
|
||||
if ($action === 'prepare_lot') {
|
||||
$productId = (int) ($data['productId'] ?? 0);
|
||||
$lotNumber = trim((string) ($data['lotNumber'] ?? ''));
|
||||
|
||||
@@ -809,6 +809,58 @@ function get_or_create_open_lot(PDO $pdo, int $productId): int
|
||||
return (int) $newLotId;
|
||||
}
|
||||
|
||||
function delete_open_stock_lot(PDO $pdo, int $lotId, string $changedBy): void
|
||||
{
|
||||
if ($lotId <= 0 || trim($changedBy) === '') {
|
||||
throw new InvalidArgumentException('Offene Charge und Bediener sind erforderlich');
|
||||
}
|
||||
|
||||
$startedTransaction = !$pdo->inTransaction();
|
||||
if ($startedTransaction) {
|
||||
$pdo->beginTransaction();
|
||||
}
|
||||
|
||||
try {
|
||||
$lotStmt = $pdo->prepare(
|
||||
"SELECT id, product_id, lot_number, manufacturer_party_id, manufacturer_lot_number,
|
||||
mfg_date, expiry_date, purchase_date, status
|
||||
FROM stock_lot
|
||||
WHERE id = :id
|
||||
FOR UPDATE"
|
||||
);
|
||||
$lotStmt->execute([':id' => $lotId]);
|
||||
$lot = $lotStmt->fetch();
|
||||
if (!is_array($lot)) {
|
||||
throw new InvalidArgumentException('Charge nicht gefunden');
|
||||
}
|
||||
if ((string) $lot['status'] !== 'open') {
|
||||
throw new InvalidArgumentException('Nur offene Chargen koennen geloescht werden');
|
||||
}
|
||||
|
||||
system_write_audit_log($pdo, 'stock_lot', (string) $lotId, 'deleted', $changedBy, $lot, null, [
|
||||
'module' => 'erp.lager',
|
||||
]);
|
||||
|
||||
$moveStmt = $pdo->prepare('DELETE FROM stock_move WHERE lot_id = :lot_id');
|
||||
$moveStmt->execute([':lot_id' => $lotId]);
|
||||
|
||||
$deleteStmt = $pdo->prepare('DELETE FROM stock_lot WHERE id = :id AND status = \'open\'');
|
||||
$deleteStmt->execute([':id' => $lotId]);
|
||||
if ($deleteStmt->rowCount() !== 1) {
|
||||
throw new RuntimeException('Offene Charge konnte nicht geloescht werden');
|
||||
}
|
||||
|
||||
if ($startedTransaction) {
|
||||
$pdo->commit();
|
||||
}
|
||||
} catch (Throwable $error) {
|
||||
if ($startedTransaction && $pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
||||
function replace_virtual_lot_allocation(
|
||||
PDO $pdo,
|
||||
int $allocationId,
|
||||
|
||||
Reference in new issue
Block a user