refactor: 重構 VendorProduct API 與新增進貨單重複檢查前端邏輯
1. 將 VendorProductController 中的 Eloquent 關聯操作改為透過 ProcurementService 使用 DB 操作,解除跨模組 Model 直接依賴。 2. ProcurementService 加入 vendor product 的資料存取方法。 3. 進貨單建立前端 (GoodsReceipt/Create.tsx) 新增重複進貨檢查與警告對話框邏輯。
This commit is contained in:
@@ -5,13 +5,15 @@ 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 InventoryServiceInterface $inventoryService,
|
||||
protected ProcurementServiceInterface $procurementService
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -25,13 +27,15 @@ class VendorProductController extends Controller
|
||||
]);
|
||||
|
||||
// 檢查是否已存在
|
||||
if ($vendor->products()->where('product_id', $validated['product_id'])->exists()) {
|
||||
if ($this->procurementService->checkVendorHasProduct($vendor->id, $validated['product_id'])) {
|
||||
return redirect()->back()->with('error', '該商品已在供貨清單中');
|
||||
}
|
||||
|
||||
$vendor->products()->attach($validated['product_id'], [
|
||||
'last_price' => $validated['last_price'] ?? null
|
||||
]);
|
||||
$this->procurementService->attachProductToVendor(
|
||||
$vendor->id,
|
||||
$validated['product_id'],
|
||||
$validated['last_price'] ?? null
|
||||
);
|
||||
|
||||
// 記錄操作
|
||||
$product = $this->inventoryService->getProduct($validated['product_id']);
|
||||
@@ -65,11 +69,13 @@ class VendorProductController extends Controller
|
||||
]);
|
||||
|
||||
// 獲取舊價格
|
||||
$old_price = $vendor->products()->where('product_id', $productId)->first()?->pivot?->last_price;
|
||||
$old_price = $this->procurementService->getVendorProductPrice($vendor->id, $productId);
|
||||
|
||||
$vendor->products()->updateExistingPivot($productId, [
|
||||
'last_price' => $validated['last_price'] ?? null
|
||||
]);
|
||||
$this->procurementService->updateVendorProductPrice(
|
||||
$vendor->id,
|
||||
$productId,
|
||||
$validated['last_price'] ?? null
|
||||
);
|
||||
|
||||
// 記錄操作
|
||||
$product = $this->inventoryService->getProduct($productId);
|
||||
@@ -102,9 +108,9 @@ class VendorProductController extends Controller
|
||||
{
|
||||
// 記錄操作 (需在 detach 前獲取資訊)
|
||||
$product = $this->inventoryService->getProduct($productId);
|
||||
$old_price = $vendor->products()->where('product_id', $productId)->first()?->pivot?->last_price;
|
||||
$old_price = $this->procurementService->getVendorProductPrice($vendor->id, $productId);
|
||||
|
||||
$vendor->products()->detach($productId);
|
||||
$this->procurementService->detachProductFromVendor($vendor->id, $productId);
|
||||
|
||||
if ($product) {
|
||||
activity()
|
||||
|
||||
Reference in New Issue
Block a user