refactor: 重構 VendorProduct API 與新增進貨單重複檢查前端邏輯
All checks were successful
ERP-Deploy-Production / deploy-production (push) Successful in 1m9s
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m10s

1. 將 VendorProductController 中的 Eloquent 關聯操作改為透過 ProcurementService 使用 DB 操作,解除跨模組 Model 直接依賴。
2. ProcurementService 加入 vendor product 的資料存取方法。
3. 進貨單建立前端 (GoodsReceipt/Create.tsx) 新增重複進貨檢查與警告對話框邏輯。
This commit is contained in:
2026-02-25 11:11:28 +08:00
parent e406ecd63d
commit ad91b08dbc
11 changed files with 689 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ use App\Modules\Inventory\Services\InventoryService;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use Illuminate\Http\Request;
use App\Modules\Procurement\Models\Vendor;
use App\Modules\Inventory\Services\DuplicateCheckService;
use Inertia\Inertia;
use App\Modules\Inventory\Models\GoodsReceipt;
use Illuminate\Support\Facades\DB;
@@ -17,15 +18,18 @@ class GoodsReceiptController extends Controller
protected $goodsReceiptService;
protected $inventoryService;
protected $procurementService;
protected $duplicateCheckService;
public function __construct(
GoodsReceiptService $goodsReceiptService,
InventoryService $inventoryService,
ProcurementServiceInterface $procurementService
ProcurementServiceInterface $procurementService,
DuplicateCheckService $duplicateCheckService
) {
$this->goodsReceiptService = $goodsReceiptService;
$this->inventoryService = $inventoryService;
$this->procurementService = $procurementService;
$this->duplicateCheckService = $duplicateCheckService;
}
public function index(Request $request)
@@ -159,10 +163,6 @@ class GoodsReceiptController extends Controller
'warehouse_id' => 'required|exists:warehouses,id',
'type' => 'required|in:standard,miscellaneous,other',
'purchase_order_id' => 'nullable|required_if:type,standard|exists:purchase_orders,id',
// Vendor ID is required if standard, but optional/nullable for misc/other?
// Stick to existing logic: if standard, we infer vendor from PO usually, or frontend sends it.
// For now let's make vendor_id optional for misc/other or user must select one?
// "雜項入庫" might not have a vendor. Let's make it nullable.
'vendor_id' => 'nullable|integer',
'received_date' => 'required|date',
'remarks' => 'nullable|string',
@@ -173,6 +173,7 @@ class GoodsReceiptController extends Controller
'items.*.unit_price' => 'required|numeric|min:0',
'items.*.batch_number' => 'nullable|string',
'items.*.expiry_date' => 'nullable|date',
'force' => 'nullable|boolean',
]);
try {
@@ -183,6 +184,15 @@ class GoodsReceiptController extends Controller
}
}
/**
* 預檢重複進貨 API
*/
public function checkDuplicate(Request $request)
{
$result = $this->duplicateCheckService->checkDuplicateReceipt($request->all());
return response()->json($result);
}
public function submit(GoodsReceipt $goodsReceipt)
{
if (!auth()->user()->can('goods_receipts.edit')) {

View File

@@ -179,6 +179,9 @@ Route::middleware('auth')->group(function () {
Route::get('/goods-receipts', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'index'])->name('goods-receipts.index');
Route::get('/goods-receipts/create', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'create'])->middleware('permission:goods_receipts.create')->name('goods-receipts.create');
Route::get('/goods-receipts/{goods_receipt}', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'show'])->name('goods-receipts.show');
Route::post('/goods-receipts/check-duplicate', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'checkDuplicate'])
->middleware('permission:goods_receipts.create')
->name('goods-receipts.check-duplicate');
Route::post('/goods-receipts', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'store'])->middleware('permission:goods_receipts.create')->name('goods-receipts.store');
// 點收提交路由

View File

@@ -0,0 +1,192 @@
<?php
namespace App\Modules\Inventory\Services;
use App\Modules\Inventory\Models\GoodsReceipt;
use App\Modules\Inventory\Models\GoodsReceiptItem;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class DuplicateCheckService
{
/**
* 檢查疑似重複進貨
*
* @param array $data
* @return array
*/
public function checkDuplicateReceipt(array $data): array
{
$warnings = [];
$vendorId = isset($data['vendor_id']) && $data['vendor_id'] !== '' ? (int)$data['vendor_id'] : null;
$poId = $data['purchase_order_id'] ?? null;
$items = $data['items'] ?? [];
$daysRange = 7; // 近期範圍天數
// 1. 同 PO 重複檢查 (僅限標準採購)
if ($data['type'] === 'standard' && $poId) {
$existingReceipts = GoodsReceipt::where('purchase_order_id', $poId)
->where('status', '!=', 'cancelled')
->withCount('items')
->get();
if ($existingReceipts->isNotEmpty()) {
$warnings[] = [
'level' => 'high',
'type' => 'same_po',
'title' => '同一採購單已有進貨紀錄',
'message' => "此採購單已被引用於 {$existingReceipts->count()} 筆進貨單中,請確認是否為分批交貨。",
'related_receipts' => $existingReceipts->map(fn($r) => [
'id' => $r->id,
'code' => $r->code,
'received_date' => $r->received_date->format('Y-m-d'),
'status' => match($r->status) {
'completed' => '已完成',
'draft' => '草稿',
'pending_audit' => '待審核',
'rejected' => '已退回',
default => $r->status
},
'item_count' => $r->items_count,
]),
];
}
}
// 2. 近期同品項檢查 (針對每一個 item)
$duplicatedItems = [];
$recentDate = Carbon::now()->subDays($daysRange);
foreach ($items as $item) {
$productId = $item['product_id'];
$qty = (float)$item['quantity_received'];
// 尋找近期同供應商、同品項的進貨紀錄
$recentHits = GoodsReceiptItem::whereHas('goodsReceipt', function($query) use ($vendorId, $recentDate) {
$query->where('vendor_id', $vendorId)
->where('received_date', '>=', $recentDate)
->where('status', '!=', 'cancelled');
})
->where('product_id', $productId)
->with(['goodsReceipt:id,code,received_date'])
->get();
foreach ($recentHits as $hit) {
$hitQty = (float)$hit->quantity_received;
// 如果數量完全相同,視為高風險
$isExactMatch = abs($hitQty - $qty) < 0.001;
$duplicatedItems[] = [
'product_id' => $productId,
'product_name' => $item['product_name'] ?? '未知商品',
'last_receipt_code' => $hit->goodsReceipt->code,
'last_receipt_date' => $hit->goodsReceipt->received_date->format('Y-m-d'),
'last_quantity' => $hitQty,
'current_quantity' => $qty,
'is_high_risk' => $isExactMatch
];
}
}
if (!empty($duplicatedItems)) {
// 按商品分組顯示
$warnings[] = [
'level' => collect($duplicatedItems)->contains('is_high_risk', true) ? 'high' : 'medium',
'type' => 'recent_duplicate_product',
'title' => '近期同供應商商品進貨紀錄',
'message' => "偵測到以下商品在近 {$daysRange} 天內有過進貨紀錄,請確認是否重複收貨。",
'duplicated_items' => $duplicatedItems,
];
}
// 3. 長期未調整單價檢查 (90 天內同供應商、同品項單價未變動)
$stalePriceItems = $this->checkStalePrice($items, $vendorId);
if (!empty($stalePriceItems)) {
$warnings[] = [
'level' => 'medium',
'type' => 'stale_price',
'title' => '長期未調整進貨單價',
'message' => '以下商品的進貨單價在過去 90 天內未曾變動,建議確認是否需要重新議價。',
'stale_items' => $stalePriceItems,
];
}
return [
'has_warnings' => !empty($warnings),
'warnings' => $warnings,
];
}
/**
* 檢查長期未調整單價的品項
*
* 邏輯:針對每個品項,查詢同供應商在過去 90 天內的所有進貨單價。
* 若所有紀錄的單價皆相同,且至少有 1 筆以上紀錄,則視為「未調整」。
*
* @param array $items 本次進貨的品項列表
* @param mixed $vendorId 供應商 ID
* @return array
*/
private function checkStalePrice(array $items, $vendorId): array
{
// dump("Entering checkStalePrice", count($items), $vendorId);
$staleDays = 90;
$sinceDate = Carbon::now()->subDays($staleDays);
$staleItems = [];
foreach ($items as $item) {
$productId = (int) $item['product_id'];
$currentPrice = (float) ($item['unit_price'] ?? 0);
// 查詢過去 90 天內的所有進貨單品項
$query = GoodsReceiptItem::whereHas('goodsReceipt', function ($query) use ($vendorId, $sinceDate) {
if ($vendorId) {
$query->where('vendor_id', $vendorId);
}
$query->where('received_date', '>=', $sinceDate->format('Y-m-d'))
->where('status', '!=', 'cancelled');
})
->where('product_id', $productId)
->with(['goodsReceipt:id,code,received_date'])
->orderBy('id', 'desc');
$historicalPrices = $query->get();
// dump("Prod $productId count: " . $historicalPrices->count());
// 至少需要 1 筆歷史紀錄
if ($historicalPrices->count() < 1) {
continue;
}
// 檢查所有歷史單價是否完全相同 (排除 Decimal 物件比對問題)
$prices = $historicalPrices->map(fn($h) => round((float)$h->unit_price, 2));
$distinctPricesCount = $prices->unique()->count();
if ($distinctPricesCount > 1) {
continue; // 歷史上有變動過
}
$historicalPrice = $prices->first();
// 本次單價也跟歷史相同 (容許小數差距)
if (round($currentPrice, 2) === round($historicalPrice, 2)) {
$oldestReceipt = $historicalPrices->last();
$newestReceipt = $historicalPrices->first();
$staleItems[] = [
'product_id' => $productId,
'product_name' => $item['product_name'] ?? '未知商品',
'unit_price' => $currentPrice,
'record_count' => $historicalPrices->count(),
'earliest_date' => $oldestReceipt->goodsReceipt->received_date->format('Y-m-d'),
'latest_date' => $newestReceipt->goodsReceipt->received_date->format('Y-m-d'),
'latest_code' => $newestReceipt->goodsReceipt->code,
];
}
}
return $staleItems;
}
}

View File

@@ -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;
}

View File

@@ -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()

View File

@@ -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();
}
}