feat: 實作銷售單匯入管理、貨道扣庫優化及 UI 細節調整
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m8s

This commit is contained in:
2026-02-09 14:36:47 +08:00
parent 590580e20a
commit b6fe9ad9f3
22 changed files with 1274 additions and 33 deletions

View File

@@ -59,13 +59,18 @@ class InventoryService implements InventoryServiceInterface
return $stock >= $quantity;
}
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null, bool $force = false): void
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null, bool $force = false, ?string $slot = null): void
{
DB::transaction(function () use ($productId, $warehouseId, $quantity, $reason, $force) {
$inventories = Inventory::where('product_id', $productId)
DB::transaction(function () use ($productId, $warehouseId, $quantity, $reason, $force, $slot) {
$query = Inventory::where('product_id', $productId)
->where('warehouse_id', $warehouseId)
->where('quantity', '>', 0)
->orderBy('arrival_date', 'asc')
->where('quantity', '>', 0);
if ($slot) {
$query->where('location', $slot);
}
$inventories = $query->orderBy('arrival_date', 'asc')
->get();
$remainingToDecrease = $quantity;
@@ -80,19 +85,25 @@ class InventoryService implements InventoryServiceInterface
if ($remainingToDecrease > 0) {
if ($force) {
// Find any existing inventory record in this warehouse to subtract from, or create one
$inventory = Inventory::where('product_id', $productId)
->where('warehouse_id', $warehouseId)
->first();
// Find any existing inventory record in this warehouse/slot to subtract from, or create one
$query = Inventory::where('product_id', $productId)
->where('warehouse_id', $warehouseId);
if ($slot) {
$query->where('location', $slot);
}
$inventory = $query->first();
if (!$inventory) {
$inventory = Inventory::create([
'warehouse_id' => $warehouseId,
'product_id' => $productId,
'location' => $slot,
'quantity' => 0,
'unit_cost' => 0,
'total_value' => 0,
'batch_number' => 'POS-AUTO-' . time(),
'batch_number' => 'POS-AUTO-' . ($slot ? $slot . '-' : '') . time(),
'arrival_date' => now(),
'origin_country' => 'TW',
'quality_status' => 'normal',