Files
star-erp/app/Modules/Procurement/Controllers/VendorProductController.php
sky121113 106de4e945
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 53s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
feat: 修正庫存與撥補單邏輯並整合文件
1. 修復倉庫統計數據加總與樣式。
2. 修正可用庫存計算邏輯(排除不可銷售倉庫)。
3. 撥補單商品列表加入批號與效期顯示。
4. 修正撥補單儲存邏輯以支援精確批號轉移。
5. 整合 FEATURES.md 至 README.md。
2026-01-26 14:59:24 +08:00

131 lines
4.3 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 Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class VendorProductController extends Controller
{
public function __construct(
protected InventoryServiceInterface $inventoryService
) {}
/**
* 新增供貨商品 (Attach)
*/
public function store(Request $request, Vendor $vendor)
{
$validated = $request->validate([
'product_id' => 'required|exists:products,id',
'last_price' => 'nullable|numeric|min:0',
]);
// 檢查是否已存在
if ($vendor->products()->where('product_id', $validated['product_id'])->exists()) {
return redirect()->back()->with('error', '該商品已在供貨清單中');
}
$vendor->products()->attach($validated['product_id'], [
'last_price' => $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 = $vendor->products()->where('product_id', $productId)->first()?->pivot?->last_price;
$vendor->products()->updateExistingPivot($productId, [
'last_price' => $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 = $vendor->products()->where('product_id', $productId)->first()?->pivot?->last_price;
$vendor->products()->detach($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', '供貨商品已移除');
}
}