feat(inventory): 開放倉庫編號編輯、優化調撥單條碼搜尋與庫存匯入範本雙分頁說明
This commit is contained in:
@@ -12,6 +12,10 @@ use App\Modules\Inventory\Models\Product;
|
||||
use App\Modules\Inventory\Models\Inventory;
|
||||
use App\Modules\Inventory\Models\InventoryTransaction;
|
||||
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
|
||||
use App\Modules\Inventory\Imports\InventoryImport;
|
||||
use App\Modules\Inventory\Exports\InventoryTemplateExport;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
use App\Modules\Core\Contracts\CoreServiceInterface;
|
||||
|
||||
@@ -603,4 +607,35 @@ class InventoryController extends Controller
|
||||
|
||||
return redirect()->back()->with('error', '未提供查詢參數');
|
||||
}
|
||||
|
||||
/**
|
||||
* 匯入入庫
|
||||
*/
|
||||
public function import(Request $request, Warehouse $warehouse)
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|mimes:xlsx,xls,csv',
|
||||
'inboundDate' => 'required|date',
|
||||
'notes' => 'nullable|string',
|
||||
]);
|
||||
|
||||
try {
|
||||
Excel::import(
|
||||
new InventoryImport($warehouse, $request->inboundDate, $request->notes),
|
||||
$request->file('file')
|
||||
);
|
||||
|
||||
return back()->with('success', '庫存資料匯入成功');
|
||||
} catch (\Exception $e) {
|
||||
return back()->withErrors(['file' => '匯入過程中發生錯誤: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下載匯入範本 (.xlsx)
|
||||
*/
|
||||
public function template()
|
||||
{
|
||||
return Excel::download(new InventoryTemplateExport, '庫存匯入範本.xlsx');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +212,8 @@ class TransferOrderController extends Controller
|
||||
return [
|
||||
'product_id' => (string) $inv->product_id,
|
||||
'product_name' => $inv->product->name,
|
||||
'product_code' => $inv->product->code, // Added code
|
||||
'product_code' => $inv->product->code,
|
||||
'product_barcode' => $inv->product->barcode,
|
||||
'batch_number' => $inv->batch_number,
|
||||
'quantity' => (float) $inv->quantity,
|
||||
'unit_cost' => (float) $inv->unit_cost,
|
||||
|
||||
@@ -123,6 +123,7 @@ class WarehouseController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code' => 'required|string|max:20|unique:warehouses,code',
|
||||
'name' => 'required|string|max:50',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
@@ -131,14 +132,6 @@ class WarehouseController extends Controller
|
||||
'driver_name' => 'nullable|string|max:50',
|
||||
]);
|
||||
|
||||
// 自動產生代碼
|
||||
$prefix = 'WH';
|
||||
$lastWarehouse = Warehouse::latest('id')->first();
|
||||
$nextId = $lastWarehouse ? $lastWarehouse->id + 1 : 1;
|
||||
$code = $prefix . str_pad($nextId, 3, '0', STR_PAD_LEFT);
|
||||
|
||||
$validated['code'] = $code;
|
||||
|
||||
Warehouse::create($validated);
|
||||
|
||||
return redirect()->back()->with('success', '倉庫已建立');
|
||||
@@ -147,6 +140,7 @@ class WarehouseController extends Controller
|
||||
public function update(Request $request, Warehouse $warehouse)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code' => 'required|string|max:20|unique:warehouses,code,' . $warehouse->id,
|
||||
'name' => 'required|string|max:50',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
|
||||
79
app/Modules/Inventory/Exports/InventoryTemplateExport.php
Normal file
79
app/Modules/Inventory/Exports/InventoryTemplateExport.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
use Maatwebsite\Excel\Concerns\FromArray;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
|
||||
class InventoryTemplateExport implements WithMultipleSheets
|
||||
{
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
new InventoryDataSheet(),
|
||||
new InventoryInstructionSheet(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class InventoryDataSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize
|
||||
{
|
||||
public function array(): array
|
||||
{
|
||||
// 資料分頁保持完全空白
|
||||
return [];
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'商品條碼',
|
||||
'商品代號',
|
||||
'商品名稱',
|
||||
'數量',
|
||||
'入庫單價',
|
||||
'批號',
|
||||
'產地',
|
||||
'效期',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return '資料填寫';
|
||||
}
|
||||
}
|
||||
|
||||
class InventoryInstructionSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize
|
||||
{
|
||||
public function array(): array
|
||||
{
|
||||
return [
|
||||
['商品條碼', '擇一輸入', '系統會「優先」依據條碼匹配商品。若有填寫,條碼必須存在於系統中'],
|
||||
['商品代號', '擇一輸入', '若條碼未填寫,系統會依據代號匹配商品'],
|
||||
['商品名稱', '選填', '僅供對照參考,匯入時系統會自動忽略此欄位內容'],
|
||||
['數量', '必填', '入庫的商品數量,須為大於 0 的數字'],
|
||||
['入庫單價', '選填', '未填寫時將預設使用商品的「採購成本價」'],
|
||||
['批號', '選填', '如需批次控管請填寫,若留空系統會自動標記為 "NO-BATCH"'],
|
||||
['產地', '選填', '商品的生產地資訊 (如:TW)'],
|
||||
['效期', '選填', '格式請務必使用 YYYY-MM-DD (例如: 2026-12-31)'],
|
||||
['', '', ''],
|
||||
['匹配規則說明', '', '1. 系統會優先比對「商品條碼」。'],
|
||||
['', '', '2. 若條碼欄位為空,則嘗試比對「商品代號」。'],
|
||||
['', '', '3. 若上述兩者皆無法匹配到既有商品,該列資料將匯入失敗。'],
|
||||
];
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return ['欄位名稱', '必要性', '填寫說明'];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return '填寫規則';
|
||||
}
|
||||
}
|
||||
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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,8 @@ Route::middleware('auth')->group(function () {
|
||||
Route::middleware('permission:inventory.adjust')->group(function () {
|
||||
Route::get('/warehouses/{warehouse}/inventory/create', [InventoryController::class, 'create'])->name('warehouses.inventory.create');
|
||||
Route::post('/warehouses/{warehouse}/inventory', [InventoryController::class, 'store'])->name('warehouses.inventory.store');
|
||||
Route::get('/warehouses/inventory/template', [InventoryController::class, 'template'])->name('warehouses.inventory.template');
|
||||
Route::post('/warehouses/{warehouse}/inventory/import', [InventoryController::class, 'import'])->name('warehouses.inventory.import');
|
||||
Route::get('/warehouses/{warehouse}/inventory/{inventoryId}/edit', [InventoryController::class, 'edit'])->name('warehouses.inventory.edit');
|
||||
Route::put('/warehouses/{warehouse}/inventory/{inventoryId}', [InventoryController::class, 'update'])->name('warehouses.inventory.update');
|
||||
Route::delete('/warehouses/{warehouse}/inventory/{inventoryId}', [InventoryController::class, 'destroy'])->name('warehouses.inventory.destroy');
|
||||
|
||||
Reference in New Issue
Block a user