1. 將 VendorProductController 中的 Eloquent 關聯操作改為透過 ProcurementService 使用 DB 操作,解除跨模組 Model 直接依賴。 2. ProcurementService 加入 vendor product 的資料存取方法。 3. 進貨單建立前端 (GoodsReceipt/Create.tsx) 新增重複進貨檢查與警告對話框邏輯。
137 lines
4.6 KiB
PHP
137 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Procurement\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Modules\Procurement\Models\Vendor;
|
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
|
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class VendorProductController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected InventoryServiceInterface $inventoryService,
|
|
protected ProcurementServiceInterface $procurementService
|
|
) {}
|
|
|
|
/**
|
|
* 新增供貨商品 (Attach)
|
|
*/
|
|
public function store(Request $request, Vendor $vendor)
|
|
{
|
|
$validated = $request->validate([
|
|
'product_id' => 'required|exists:products,id',
|
|
'last_price' => 'nullable|numeric|min:0',
|
|
]);
|
|
|
|
// 檢查是否已存在
|
|
if ($this->procurementService->checkVendorHasProduct($vendor->id, $validated['product_id'])) {
|
|
return redirect()->back()->with('error', '該商品已在供貨清單中');
|
|
}
|
|
|
|
$this->procurementService->attachProductToVendor(
|
|
$vendor->id,
|
|
$validated['product_id'],
|
|
$validated['last_price'] ?? null
|
|
);
|
|
|
|
// 記錄操作
|
|
$product = $this->inventoryService->getProduct($validated['product_id']);
|
|
activity()
|
|
->performedOn($vendor)
|
|
->withProperties([
|
|
'attributes' => [
|
|
'product_name' => $product->name,
|
|
'last_price' => $validated['last_price'] ?? null,
|
|
],
|
|
'sub_subject' => '供貨商品',
|
|
'snapshot' => [
|
|
'name' => "{$vendor->name}-{$product->name}", // 顯示例如:台積電-紅糖
|
|
'vendor_name' => $vendor->name,
|
|
'product_name' => $product->name,
|
|
]
|
|
])
|
|
->event('created')
|
|
->log('新增供貨商品');
|
|
|
|
return redirect()->back()->with('success', '供貨商品已新增');
|
|
}
|
|
|
|
/**
|
|
* 更新供貨商品資訊 (Update Pivot)
|
|
*/
|
|
public function update(Request $request, Vendor $vendor, $productId)
|
|
{
|
|
$validated = $request->validate([
|
|
'last_price' => 'nullable|numeric|min:0',
|
|
]);
|
|
|
|
// 獲取舊價格
|
|
$old_price = $this->procurementService->getVendorProductPrice($vendor->id, $productId);
|
|
|
|
$this->procurementService->updateVendorProductPrice(
|
|
$vendor->id,
|
|
$productId,
|
|
$validated['last_price'] ?? null
|
|
);
|
|
|
|
// 記錄操作
|
|
$product = $this->inventoryService->getProduct($productId);
|
|
activity()
|
|
->performedOn($vendor)
|
|
->withProperties([
|
|
'old' => [
|
|
'last_price' => $old_price,
|
|
],
|
|
'attributes' => [
|
|
'last_price' => $validated['last_price'] ?? null,
|
|
],
|
|
'sub_subject' => '供貨商品',
|
|
'snapshot' => [
|
|
'name' => "{$vendor->name}-{$product->name}",
|
|
'vendor_name' => $vendor->name,
|
|
'product_name' => $product->name,
|
|
]
|
|
])
|
|
->event('updated')
|
|
->log('更新供貨商品價格');
|
|
|
|
return redirect()->back()->with('success', '供貨資訊已更新');
|
|
}
|
|
|
|
/**
|
|
* 移除供貨商品 (Detach)
|
|
*/
|
|
public function destroy(Vendor $vendor, $productId)
|
|
{
|
|
// 記錄操作 (需在 detach 前獲取資訊)
|
|
$product = $this->inventoryService->getProduct($productId);
|
|
$old_price = $this->procurementService->getVendorProductPrice($vendor->id, $productId);
|
|
|
|
$this->procurementService->detachProductFromVendor($vendor->id, $productId);
|
|
|
|
if ($product) {
|
|
activity()
|
|
->performedOn($vendor)
|
|
->withProperties([
|
|
'old' => [
|
|
'product_name' => $product->name,
|
|
'last_price' => $old_price,
|
|
],
|
|
'sub_subject' => '供貨商品',
|
|
'snapshot' => [
|
|
'name' => "{$vendor->name}-{$product->name}",
|
|
'vendor_name' => $vendor->name,
|
|
'product_name' => $product->name,
|
|
]
|
|
])
|
|
->event('deleted')
|
|
->log('移除供貨商品');
|
|
}
|
|
|
|
return redirect()->back()->with('success', '供貨商品已移除');
|
|
}
|
|
}
|