diff --git a/db/README.md b/db/README.md index dba14e9..70f534e 100644 --- a/db/README.md +++ b/db/README.md @@ -17,6 +17,7 @@ 13. `db/migrations/0017_manufacturer_lot_traceability.sql` 14. `db/migrations/0018_shopify_delta_sync_state.sql` 15. `db/migrations/0019_allow_multiple_open_stock_lots.sql` +16. `db/migrations/0020_include_stock_adjustments_in_balance.sql` ## Beispielausfuehrung diff --git a/db/migrations/0020_include_stock_adjustments_in_balance.sql b/db/migrations/0020_include_stock_adjustments_in_balance.sql new file mode 100644 index 0000000..5b745f2 --- /dev/null +++ b/db/migrations/0020_include_stock_adjustments_in_balance.sql @@ -0,0 +1,22 @@ +BEGIN; + +CREATE OR REPLACE VIEW v_stock_lot_balance AS +SELECT + sl.id AS stock_lot_id, + sl.product_id, + COALESCE(SUM(CASE WHEN sm.move_type = 'in' THEN sm.qty ELSE 0 END), 0) AS qty_in, + COALESCE(SUM(CASE WHEN sm.move_type = 'out' THEN sm.qty ELSE 0 END), 0) AS qty_out, + COALESCE(SUM( + CASE + WHEN sm.move_type = 'in' THEN sm.qty + WHEN sm.move_type = 'out' THEN -sm.qty + WHEN sm.move_type = 'adjustment' AND sm.to_location_id IS NOT NULL AND sm.from_location_id IS NULL THEN sm.qty + WHEN sm.move_type = 'adjustment' AND sm.from_location_id IS NOT NULL AND sm.to_location_id IS NULL THEN -sm.qty + ELSE 0 + END + ), 0) AS qty_net +FROM stock_lot sl +LEFT JOIN stock_move sm ON sm.lot_id = sl.id +GROUP BY sl.id, sl.product_id; + +COMMIT; diff --git a/modules/erp/lager/service.php b/modules/erp/lager/service.php index 220e598..1e6038a 100644 --- a/modules/erp/lager/service.php +++ b/modules/erp/lager/service.php @@ -343,6 +343,8 @@ function update_stock_lot_traceability( "SELECT COALESCE(SUM(CASE WHEN move_type = 'in' THEN qty WHEN move_type = 'out' THEN -qty + WHEN move_type = 'adjustment' AND to_location_id IS NOT NULL AND from_location_id IS NULL THEN qty + WHEN move_type = 'adjustment' AND from_location_id IS NOT NULL AND to_location_id IS NULL THEN -qty ELSE 0 END), 0) FROM stock_move @@ -351,8 +353,8 @@ function update_stock_lot_traceability( $balanceStmt->execute([':lot_id' => $lotId]); $quantityDelta = $targetQty - (float) $balanceStmt->fetchColumn(); $receiptQty = $quantityDelta > 0 ? $quantityDelta : 0.0; - $outQty = $quantityDelta < 0 ? abs($quantityDelta) : 0.0; - $adjustmentQty = 0.0; + $outQty = 0.0; + $adjustmentQty = $quantityDelta < 0 ? $quantityDelta : 0.0; } if (($adjustmentQty !== 0.0 || $outQty > 0) && (string) $before['status'] !== 'current' && $targetQty === null) {