refactor: 重構 VendorProduct API 與新增進貨單重複檢查前端邏輯
1. 將 VendorProductController 中的 Eloquent 關聯操作改為透過 ProcurementService 使用 DB 操作,解除跨模組 Model 直接依賴。 2. ProcurementService 加入 vendor product 的資料存取方法。 3. 進貨單建立前端 (GoodsReceipt/Create.tsx) 新增重複進貨檢查與警告對話框邏輯。
This commit is contained in:
@@ -79,4 +79,29 @@ interface ProcurementServiceInterface
|
||||
* @return Collection
|
||||
*/
|
||||
public function getVendorsByIds(array $ids): Collection;
|
||||
|
||||
/**
|
||||
* 新增供貨商品關聯
|
||||
*/
|
||||
public function attachProductToVendor(int $vendorId, int $productId, ?float $lastPrice): void;
|
||||
|
||||
/**
|
||||
* 更新供貨商品價格
|
||||
*/
|
||||
public function updateVendorProductPrice(int $vendorId, int $productId, ?float $lastPrice): void;
|
||||
|
||||
/**
|
||||
* 檢查廠商是否已有該供貨商品
|
||||
*/
|
||||
public function checkVendorHasProduct(int $vendorId, int $productId): bool;
|
||||
|
||||
/**
|
||||
* 取得供貨商品的價格
|
||||
*/
|
||||
public function getVendorProductPrice(int $vendorId, int $productId): ?float;
|
||||
|
||||
/**
|
||||
* 移除供貨商品關聯
|
||||
*/
|
||||
public function detachProductFromVendor(int $vendorId, int $productId): void;
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -99,4 +99,52 @@ class ProcurementService implements ProcurementServiceInterface
|
||||
{
|
||||
return \App\Modules\Procurement\Models\Vendor::whereIn('id', $ids)->get(['id', 'name', 'code']);
|
||||
}
|
||||
|
||||
public function attachProductToVendor(int $vendorId, int $productId, ?float $lastPrice): void
|
||||
{
|
||||
\Illuminate\Support\Facades\DB::table('product_vendor')->insert([
|
||||
'vendor_id' => $vendorId,
|
||||
'product_id' => $productId,
|
||||
'last_price' => $lastPrice,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateVendorProductPrice(int $vendorId, int $productId, ?float $lastPrice): void
|
||||
{
|
||||
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->where('product_id', $productId)
|
||||
->update([
|
||||
'last_price' => $lastPrice,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function checkVendorHasProduct(int $vendorId, int $productId): bool
|
||||
{
|
||||
return \Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->where('product_id', $productId)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function getVendorProductPrice(int $vendorId, int $productId): ?float
|
||||
{
|
||||
$pivot = \Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->where('product_id', $productId)
|
||||
->first();
|
||||
|
||||
return $pivot ? (float) $pivot->last_price : null;
|
||||
}
|
||||
|
||||
public function detachProductFromVendor(int $vendorId, int $productId): void
|
||||
{
|
||||
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->where('product_id', $productId)
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user