feat(inventory): 開放倉庫編號編輯、優化調撥單條碼搜尋與庫存匯入範本雙分頁說明
This commit is contained in:
122
app/Modules/Inventory/Imports/InventoryImport.php
Normal file
122
app/Modules/Inventory/Imports/InventoryImport.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Imports;
|
||||
|
||||
use App\Modules\Inventory\Models\Product;
|
||||
use App\Modules\Inventory\Models\Inventory;
|
||||
use App\Modules\Inventory\Models\Warehouse;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMapping
|
||||
{
|
||||
private $warehouse;
|
||||
private $inboundDate;
|
||||
private $notes;
|
||||
|
||||
public function __construct(Warehouse $warehouse, string $inboundDate, ?string $notes = null)
|
||||
{
|
||||
HeadingRowFormatter::default('none');
|
||||
$this->warehouse = $warehouse;
|
||||
$this->inboundDate = $inboundDate;
|
||||
$this->notes = $notes;
|
||||
}
|
||||
|
||||
public function map($row): array
|
||||
{
|
||||
// 處理條碼或代號為字串
|
||||
if (isset($row['商品條碼'])) {
|
||||
$row['商品條碼'] = (string) $row['商品條碼'];
|
||||
}
|
||||
if (isset($row['商品代號'])) {
|
||||
$row['商品代號'] = (string) $row['商品代號'];
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function model(array $row)
|
||||
{
|
||||
// 查找商品
|
||||
$product = null;
|
||||
if (!empty($row['商品條碼'])) {
|
||||
$product = Product::where('barcode', $row['商品條碼'])->first();
|
||||
}
|
||||
if (!$product && !empty($row['商品代號'])) {
|
||||
$product = Product::where('code', $row['商品代號'])->first();
|
||||
}
|
||||
|
||||
if (!$product) {
|
||||
return null; // 透過 Validation 攔截
|
||||
}
|
||||
|
||||
$quantity = (float) $row['數量'];
|
||||
$unitCost = isset($row['入庫單價']) ? (float) $row['入庫單價'] : ($product->cost_price ?? 0);
|
||||
|
||||
// 批號邏輯:若 Excel 留空則使用 NO-BATCH
|
||||
$batchNumber = !empty($row['批號']) ? $row['批號'] : 'NO-BATCH';
|
||||
$originCountry = $row['產地'] ?? 'TW';
|
||||
$expiryDate = !empty($row['效期']) ? $row['效期'] : null;
|
||||
|
||||
return DB::transaction(function () use ($product, $quantity, $unitCost, $batchNumber, $originCountry, $expiryDate) {
|
||||
// 使用與 InventoryController 相同的 firstOrNew 邏輯
|
||||
$inventory = $this->warehouse->inventories()->withTrashed()->firstOrNew(
|
||||
[
|
||||
'product_id' => $product->id,
|
||||
'batch_number' => $batchNumber
|
||||
],
|
||||
[
|
||||
'quantity' => 0,
|
||||
'unit_cost' => $unitCost,
|
||||
'total_value' => 0,
|
||||
'arrival_date' => $this->inboundDate,
|
||||
'expiry_date' => $expiryDate,
|
||||
'origin_country' => $originCountry,
|
||||
]
|
||||
);
|
||||
|
||||
if ($inventory->trashed()) {
|
||||
$inventory->restore();
|
||||
}
|
||||
|
||||
// 更新數量
|
||||
$oldQty = $inventory->quantity;
|
||||
$inventory->quantity += $quantity;
|
||||
|
||||
// 更新單價與總價值
|
||||
$inventory->unit_cost = $unitCost;
|
||||
$inventory->total_value = $inventory->quantity * $unitCost;
|
||||
$inventory->save();
|
||||
|
||||
// 記錄交易歷史
|
||||
$inventory->transactions()->create([
|
||||
'warehouse_id' => $this->warehouse->id,
|
||||
'product_id' => $product->id,
|
||||
'batch_number' => $inventory->batch_number,
|
||||
'quantity' => $quantity,
|
||||
'unit_cost' => $unitCost,
|
||||
'transaction_type' => '手動入庫',
|
||||
'reason' => 'Excel 匯入入庫',
|
||||
'notes' => $this->notes,
|
||||
'expiry_date' => $inventory->expiry_date,
|
||||
]);
|
||||
|
||||
return $inventory;
|
||||
});
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'商品條碼' => ['nullable', 'string'],
|
||||
'商品代號' => ['nullable', 'string'],
|
||||
'數量' => ['required', 'numeric', 'min:0.01'],
|
||||
'入庫單價' => ['nullable', 'numeric', 'min:0'],
|
||||
'效期' => ['nullable', 'date'],
|
||||
'產地' => ['nullable', 'string', 'max:2'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user