feat: 修正庫存與撥補單邏輯並整合文件
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 53s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

1. 修復倉庫統計數據加總與樣式。
2. 修正可用庫存計算邏輯(排除不可銷售倉庫)。
3. 撥補單商品列表加入批號與效期顯示。
4. 修正撥補單儲存邏輯以支援精確批號轉移。
5. 整合 FEATURES.md 至 README.md。
This commit is contained in:
2026-01-26 14:59:24 +08:00
parent b0848a6bb8
commit 106de4e945
81 changed files with 4118 additions and 1023 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Modules\Core\Contracts;
use Illuminate\Support\Collection;
interface CoreServiceInterface
{
/**
* Get multiple users by their IDs.
*
* @param array $ids
* @return Collection
*/
public function getUsersByIds(array $ids): Collection;
/**
* Get a specific user by ID.
*
* @param int $id
* @return object|null
*/
public function getUser(int $id): ?object;
/**
* Get all users.
*
* @return Collection
*/
public function getAllUsers(): Collection;
}

View File

@@ -96,12 +96,12 @@ class ActivityLogController extends Controller
];
});
// Prepare subject types for frontend filter
// 準備用於前端篩選的主題類型
$subjectTypes = collect($this->getSubjectMap())->map(function ($label, $value) {
return ['label' => $label, 'value' => $value];
})->values();
// Get users for causer filter
// 取得用於操作者篩選的使用者
$users = \App\Modules\Core\Models\User::select('id', 'name')->orderBy('name')->get()
->map(function ($user) {
return ['label' => $user->name, 'value' => (string) $user->id];

View File

@@ -13,7 +13,7 @@ use Illuminate\Validation\Rule;
class RoleController extends Controller
{
/**
* Display a listing of the resource.
* 顯示資源列表。
*/
public function index(Request $request)
{
@@ -23,7 +23,7 @@ class RoleController extends Controller
$query = Role::withCount('users', 'permissions')
->with('users:id,name,username');
// Handle sorting
// 處理排序
if (in_array($sortBy, ['users_count', 'permissions_count', 'created_at', 'id'])) {
$query->orderBy($sortBy, $sortOrder);
} else {
@@ -39,7 +39,7 @@ class RoleController extends Controller
}
/**
* Show the form for creating a new resource.
* 顯示建立新資源的表單。
*/
public function create()
{
@@ -51,7 +51,7 @@ class RoleController extends Controller
}
/**
* Store a newly created resource in storage.
* 將新建立的資源儲存到儲存體中。
*/
public function store(Request $request)
{
@@ -75,7 +75,7 @@ class RoleController extends Controller
}
/**
* Show the form for editing the specified resource.
* 顯示編輯指定資源的表單。
*/
public function edit(string $id)
{
@@ -97,7 +97,7 @@ class RoleController extends Controller
}
/**
* Update the specified resource in storage.
* 更新儲存體中的指定資源。
*/
public function update(Request $request, string $id)
{
@@ -127,7 +127,7 @@ class RoleController extends Controller
}
/**
* Remove the specified resource from storage.
* 從儲存體中移除指定資源。
*/
public function destroy(string $id)
{

View File

@@ -14,7 +14,7 @@ use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
/**
* Display a listing of the resource.
* 顯示資源列表。
*/
public function index(Request $request)
{
@@ -26,7 +26,7 @@ class UserController extends Controller
$query = User::with(['roles:id,name,display_name']);
// Handle Search
// 處理搜尋
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
@@ -35,14 +35,14 @@ class UserController extends Controller
});
}
// Handle Role Filter
// 處理角色篩選
if ($roleId && $roleId !== 'all') {
$query->whereHas('roles', function ($q) use ($roleId) {
$q->where('id', $roleId);
});
}
// Handle sorting
// 處理排序
if (in_array($sortBy, ['name', 'created_at'])) {
$query->orderBy($sortBy, $sortOrder);
} else {
@@ -60,7 +60,7 @@ class UserController extends Controller
}
/**
* Show the form for creating a new resource.
* 顯示建立新資源的表單。
*/
public function create()
{
@@ -72,7 +72,7 @@ class UserController extends Controller
}
/**
* Store a newly created resource in storage.
* 將新建立的資源儲存到儲存體中。
*/
public function store(Request $request)
{
@@ -98,7 +98,7 @@ class UserController extends Controller
if (!empty($validated['roles'])) {
$user->syncRoles($validated['roles']);
// Update the 'created' log to include roles
// 更新 'created' 紀錄以包含角色資訊
$activity = \Spatie\Activitylog\Models\Activity::where('subject_type', get_class($user))
->where('subject_id', $user->id)
->where('event', 'created')
@@ -118,7 +118,7 @@ class UserController extends Controller
}
/**
* Show the form for editing the specified resource.
* 顯示編輯指定資源的表單。
*/
public function edit(string $id)
{
@@ -133,7 +133,7 @@ class UserController extends Controller
}
/**
* Update the specified resource in storage.
* 更新儲存體中的指定資源。
*/
public function update(Request $request, string $id)
{
@@ -150,7 +150,7 @@ class UserController extends Controller
'password.confirmed' => '密碼確認不符',
]);
// 1. Prepare data and detect changes
// 1. 準備資料並偵測變更
$userData = [
'name' => $validated['name'],
'email' => $validated['email'],
@@ -163,7 +163,7 @@ class UserController extends Controller
$user->fill($userData);
// Capture dirty attributes for manual logging
// 捕捉變更屬性以進行手動記錄
$dirty = $user->getDirty();
$oldAttributes = [];
$newAttributes = [];
@@ -173,10 +173,10 @@ class UserController extends Controller
$newAttributes[$key] = $value;
}
// Save without triggering events (prevents duplicate log)
// 儲存但不觸發事件(防止重複記錄)
$user->saveQuietly();
// 2. Handle Roles
// 2. 處理角色
$roleChanges = null;
if (isset($validated['roles'])) {
$oldRoles = $user->roles()->pluck('display_name')->join(', ');
@@ -191,7 +191,7 @@ class UserController extends Controller
}
}
// 3. Manually Log activity (Single Consolidated Log)
// 3. 手動記錄活動(單一整合記錄)
if (!empty($newAttributes) || $roleChanges) {
$properties = [
'attributes' => $newAttributes,
@@ -209,7 +209,7 @@ class UserController extends Controller
->event('updated')
->withProperties($properties)
->tap(function (\Spatie\Activitylog\Contracts\Activity $activity) use ($user) {
// Manually add snapshot since we aren't using the model's LogOptions due to saveQuietly
// 手動加入快照,因為使用 saveQuietly 所以不使用模型的 LogOptions
$activity->properties = $activity->properties->merge([
'snapshot' => [
'name' => $user->name,
@@ -224,7 +224,7 @@ class UserController extends Controller
}
/**
* Remove the specified resource from storage.
* 從儲存體中移除指定資源。
*/
public function destroy(string $id)
{

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Modules\Core;
use Illuminate\Support\ServiceProvider;
use App\Modules\Core\Contracts\CoreServiceInterface;
use App\Modules\Core\Services\CoreService;
class CoreServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(CoreServiceInterface::class, CoreService::class);
}
public function boot(): void
{
//
}
}

View File

@@ -16,10 +16,20 @@ class User extends Authenticatable
use HasFactory, Notifiable, HasRoles, LogsActivity;
/**
* The attributes that are mass assignable.
* 可批量賦值的屬性。
*
* @var list<string>
*/
/**
* 建立模型的新工廠實例。
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\UserFactory::new();
}
protected $fillable = [
'name',
'email',
@@ -28,7 +38,7 @@ class User extends Authenticatable
];
/**
* The attributes that should be hidden for serialization.
* 序列化時應隱藏的屬性。
*
* @var list<string>
*/
@@ -38,7 +48,7 @@ class User extends Authenticatable
];
/**
* Get the attributes that should be cast.
* 取得應進行轉換的屬性。
*
* @return array<string, string>
*/

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Modules\Core\Services;
use App\Modules\Core\Contracts\CoreServiceInterface;
use App\Modules\Core\Models\User;
use Illuminate\Support\Collection;
class CoreService implements CoreServiceInterface
{
/**
* Get multiple users by their IDs.
*
* @param array $ids
* @return Collection
*/
public function getUsersByIds(array $ids): Collection
{
return User::whereIn('id', $ids)->get();
}
/**
* Get a specific user by ID.
*
* @param int $id
* @return object|null
*/
public function getUser(int $id): ?object
{
return User::find($id);
}
/**
* Get all users.
*
* @return Collection
*/
public function getAllUsers(): Collection
{
return User::all();
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Modules\Finance\Contracts;
use Illuminate\Support\Collection;
interface FinanceServiceInterface
{
/**
* Get accounting report data.
*
* @param string $start
* @param string $end
* @return array
*/
public function getAccountingReportData(string $start, string $end): array;
/**
* Get all utility fees with filters.
*
* @param array $filters
* @return mixed
*/
public function getUtilityFees(array $filters);
/**
* Get unique categories of utility fees.
*
* @return Collection
*/
public function getUniqueCategories(): Collection;
}

View File

@@ -3,9 +3,7 @@
namespace App\Modules\Finance\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Finance\Models\UtilityFee;
use App\Modules\Procurement\Models\PurchaseOrder;
use App\Modules\Finance\Contracts\FinanceServiceInterface;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Carbon;
@@ -13,49 +11,20 @@ use Illuminate\Pagination\LengthAwarePaginator;
class AccountingReportController extends Controller
{
protected $financeService;
public function __construct(FinanceServiceInterface $financeService)
{
$this->financeService = $financeService;
}
public function index(Request $request)
{
$dateStart = $request->input('date_start', Carbon::now()->toDateString());
$dateEnd = $request->input('date_end', Carbon::now()->toDateString());
// 1. Get Purchase Orders (Completed or Received that are ready for accounting)
$purchaseOrders = PurchaseOrder::with(['vendor'])
->whereIn('status', ['received', 'completed'])
->whereBetween('created_at', [$dateStart . ' 00:00:00', $dateEnd . ' 23:59:59'])
->get()
->map(function ($po) {
return [
'id' => 'PO-' . $po->id,
'date' => Carbon::parse($po->created_at)->timezone(config('app.timezone'))->toDateString(),
'source' => '採購單',
'category' => '進貨支出',
'item' => $po->vendor->name ?? '未知廠商',
'reference' => $po->code,
'invoice_number' => $po->invoice_number,
'amount' => $po->grand_total,
];
});
// 2. Get Utility Fees
$utilityFees = UtilityFee::whereBetween('transaction_date', [$dateStart, $dateEnd])
->get()
->map(function ($fee) {
return [
'id' => 'UF-' . $fee->id,
'date' => $fee->transaction_date->format('Y-m-d'),
'source' => '公共事業費',
'category' => $fee->category,
'item' => $fee->description ?: $fee->category,
'reference' => '-',
'invoice_number' => $fee->invoice_number,
'amount' => $fee->amount,
];
});
// Combine and Sort
$allRecords = $purchaseOrders->concat($utilityFees)
->sortByDesc('date')
->values();
$reportData = $this->financeService->getAccountingReportData($dateStart, $dateEnd);
$allRecords = $reportData['records'];
// 3. Manual Pagination
$perPage = $request->input('per_page', 10);
@@ -70,16 +39,9 @@ class AccountingReportController extends Controller
['path' => $request->url(), 'query' => $request->query()]
);
$summary = [
'total_amount' => $allRecords->sum('amount'),
'purchase_total' => $purchaseOrders->sum('amount'),
'utility_total' => $utilityFees->sum('amount'),
'record_count' => $allRecords->count(),
];
return Inertia::render('Accounting/Report', [
'records' => $paginatedRecords,
'summary' => $summary,
'summary' => $reportData['summary'],
'filters' => [
'date_start' => $dateStart,
'date_end' => $dateEnd,
@@ -94,60 +56,25 @@ class AccountingReportController extends Controller
$dateEnd = $request->input('date_end', Carbon::now()->toDateString());
$selectedIdsParam = $request->input('selected_ids');
$purchaseOrdersQuery = PurchaseOrder::with(['vendor'])
->whereIn('status', ['received', 'completed']);
$utilityFeesQuery = UtilityFee::query();
$reportData = $this->financeService->getAccountingReportData($dateStart, $dateEnd);
$allRecords = $reportData['records'];
if ($selectedIdsParam) {
$ids = explode(',', $selectedIdsParam);
$poIds = [];
$ufIds = [];
foreach ($ids as $id) {
if (str_starts_with($id, 'PO-')) {
$poIds[] = substr($id, 3);
} elseif (str_starts_with($id, 'UF-')) {
$ufIds[] = substr($id, 3);
}
}
$purchaseOrders = $purchaseOrdersQuery->whereIn('id', $poIds)->get();
$utilityFees = $utilityFeesQuery->whereIn('id', $ufIds)->get();
} else {
$purchaseOrders = $purchaseOrdersQuery
->whereBetween('created_at', [$dateStart . ' 00:00:00', $dateEnd . ' 23:59:59'])
->get();
$utilityFees = $utilityFeesQuery
->whereBetween('transaction_date', [$dateStart, $dateEnd])
->get();
$allRecords = $allRecords->whereIn('id', $ids);
}
$allRecords = collect();
foreach ($purchaseOrders as $po) {
$allRecords->push([
Carbon::parse($po->created_at)->toDateString(),
'採購單',
'進貨支出',
$po->vendor->name ?? '',
$po->code,
$po->invoice_number,
(float)$po->grand_total,
]);
}
foreach ($utilityFees as $fee) {
$allRecords->push([
Carbon::parse($fee->transaction_date)->toDateString(),
'公共事業費',
$fee->category,
$fee->description,
'-',
$fee->invoice_number,
(float)$fee->amount,
]);
}
$allRecords = $allRecords->sortByDesc(0);
$exportData = $allRecords->map(function ($record) {
return [
$record['date'],
$record['source'],
$record['category'],
$record['item'],
$record['reference'],
$record['invoice_number'],
$record['amount'],
];
});
$filename = "accounting_report_{$dateStart}_{$dateEnd}.csv";
$headers = [
@@ -155,14 +82,14 @@ class AccountingReportController extends Controller
'Content-Disposition' => "attachment; filename=\"{$filename}\"",
];
$callback = function () use ($allRecords) {
$callback = function () use ($exportData) {
$file = fopen('php://output', 'w');
// BOM for Excel compatibility with UTF-8
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
fputcsv($file, ['日期', '來源', '類別', '項目', '參考單號', '發票號碼', '金額']);
foreach ($allRecords as $row) {
foreach ($exportData as $row) {
fputcsv($file, $row);
}
fclose($file);

View File

@@ -4,57 +4,30 @@ namespace App\Modules\Finance\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Finance\Models\UtilityFee;
use App\Modules\Finance\Contracts\FinanceServiceInterface;
use Illuminate\Http\Request;
use Inertia\Inertia;
class UtilityFeeController extends Controller
{
protected $financeService;
public function __construct(FinanceServiceInterface $financeService)
{
$this->financeService = $financeService;
}
public function index(Request $request)
{
$query = UtilityFee::query();
// Search
if ($request->has('search')) {
$search = $request->input('search');
$query->where(function($q) use ($search) {
$q->where('category', 'like', "%{$search}%")
->orWhere('invoice_number', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%");
});
}
// Filtering
if ($request->filled('category') && $request->input('category') !== 'all') {
$query->where('category', $request->input('category'));
}
if ($request->filled('date_start')) {
$query->where('transaction_date', '>=', $request->input('date_start'));
}
if ($request->filled('date_end')) {
$query->where('transaction_date', '<=', $request->input('date_end'));
}
// Sorting
$sortField = $request->input('sort_field');
$sortDirection = $request->input('sort_direction');
if ($sortField && $sortDirection) {
$query->orderBy($sortField, $sortDirection);
} else {
$query->orderBy('created_at', 'desc');
}
$fees = $query->paginate($request->input('per_page', 10))->withQueryString();
$availableCategories = UtilityFee::distinct()->pluck('category');
$filters = $request->only(['search', 'category', 'date_start', 'date_end', 'sort_field', 'sort_direction', 'per_page']);
$fees = $this->financeService->getUtilityFees($filters)->withQueryString();
$availableCategories = $this->financeService->getUniqueCategories();
return Inertia::render('UtilityFee/Index', [
'fees' => $fees,
'availableCategories' => $availableCategories,
'filters' => $request->only(['search', 'category', 'date_start', 'date_end', 'sort_field', 'sort_direction', 'per_page']),
'filters' => $filters,
]);
}
@@ -70,19 +43,10 @@ class UtilityFeeController extends Controller
$fee = UtilityFee::create($validated);
// Log activity
activity()
->performedOn($fee)
->causedBy(auth()->user())
->event('created')
->withProperties([
'attributes' => $fee->getAttributes(),
'snapshot' => [
'category' => $fee->category,
'amount' => $fee->amount,
'transaction_date' => $fee->transaction_date->format('Y-m-d'),
]
])
->log('created');
return redirect()->back();
@@ -98,52 +62,12 @@ class UtilityFeeController extends Controller
'description' => 'nullable|string',
]);
// Capture old attributes before update
$oldAttributes = $utility_fee->getAttributes();
$utility_fee->update($validated);
// Capture new attributes
$newAttributes = $utility_fee->getAttributes();
// Manual logOnlyDirty: Filter attributes to only include changes
$changedAttributes = [];
$changedOldAttributes = [];
foreach ($newAttributes as $key => $value) {
// Skip timestamps if they are the only change (optional, but good practice)
if (in_array($key, ['updated_at'])) continue;
$oldValue = $oldAttributes[$key] ?? null;
// Simple comparison (casting to string to handle date objects vs strings if necessary,
// but Eloquent attributes are usually consistent if casted.
// Using loose comparison != handles most cases correctly)
if ($value != $oldValue) {
$changedAttributes[$key] = $value;
$changedOldAttributes[$key] = $oldValue;
}
}
// Only log if there are changes (excluding just updated_at)
if (empty($changedAttributes)) {
return redirect()->back();
}
// Log activity with before/after comparison
activity()
->performedOn($utility_fee)
->causedBy(auth()->user())
->event('updated')
->withProperties([
'attributes' => $changedAttributes,
'old' => $changedOldAttributes,
'snapshot' => [
'category' => $utility_fee->category,
'amount' => $utility_fee->amount,
'transaction_date' => $utility_fee->transaction_date->format('Y-m-d'),
]
])
->log('updated');
return redirect()->back();
@@ -151,24 +75,10 @@ class UtilityFeeController extends Controller
public function destroy(UtilityFee $utility_fee)
{
// Capture data snapshot before deletion
$snapshot = [
'category' => $utility_fee->category,
'amount' => $utility_fee->amount,
'transaction_date' => $utility_fee->transaction_date->format('Y-m-d'),
'invoice_number' => $utility_fee->invoice_number,
'description' => $utility_fee->description,
];
// Log activity before deletion
activity()
->performedOn($utility_fee)
->causedBy(auth()->user())
->event('deleted')
->withProperties([
'attributes' => $utility_fee->getAttributes(),
'snapshot' => $snapshot
])
->log('deleted');
$utility_fee->delete();

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Modules\Finance;
use Illuminate\Support\ServiceProvider;
use App\Modules\Finance\Contracts\FinanceServiceInterface;
use App\Modules\Finance\Services\FinanceService;
class FinanceServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(FinanceServiceInterface::class, FinanceService::class);
}
public function boot(): void
{
//
}
}

View File

@@ -11,26 +11,25 @@ class UtilityFee extends Model
use HasFactory;
protected $fillable = [
'type', // 'electricity', 'water', 'gas', etc.
'billing_period_start',
'billing_period_end',
'due_date',
'transaction_date',
'category',
'amount',
'usage_amount', // kWh, m3, etc.
'unit', // 度, 立方米
'status', // 'pending', 'paid', 'overdue'
'paid_at',
'payment_method',
'notes',
'receipt_image_path',
'invoice_number',
'description',
];
protected $casts = [
'billing_period_start' => 'date',
'billing_period_end' => 'date',
'due_date' => 'date',
'paid_at' => 'datetime',
'transaction_date' => 'date',
'amount' => 'decimal:2',
'usage_amount' => 'decimal:2',
];
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$activity->properties = $activity->properties->put('snapshot', [
'transaction_date' => $this->transaction_date->format('Y-m-d'),
'category' => $this->category,
'amount' => $this->amount,
'invoice_number' => $this->invoice_number,
]);
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace App\Modules\Finance\Services;
use App\Modules\Finance\Contracts\FinanceServiceInterface;
use App\Modules\Finance\Models\UtilityFee;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\Carbon;
class FinanceService implements FinanceServiceInterface
{
protected $procurementService;
public function __construct(ProcurementServiceInterface $procurementService)
{
$this->procurementService = $procurementService;
}
public function getAccountingReportData(string $start, string $end): array
{
// 1. 獲取採購單資料
$purchaseOrders = $this->procurementService->getPurchaseOrdersByDate($start, $end)
->map(function ($po) {
return [
'id' => 'PO-' . $po->id,
'date' => Carbon::parse($po->created_at)->timezone(config('app.timezone'))->toDateString(),
'source' => '採購單',
'category' => '進貨支出',
'item' => $po->vendor->name ?? '未知廠商',
'reference' => $po->code,
'invoice_number' => $po->invoice_number,
'amount' => (float)$po->grand_total,
];
});
// 2. 獲取公共事業費 (注意:目前資料表欄位為 transaction_date)
$utilityFees = UtilityFee::whereBetween('transaction_date', [$start, $end])
->get()
->map(function ($fee) {
return [
'id' => 'UF-' . $fee->id,
'date' => $fee->transaction_date->format('Y-m-d'),
'source' => '公共事業費',
'category' => $fee->category,
'item' => $fee->description ?: $fee->category,
'reference' => '-',
'invoice_number' => $fee->invoice_number,
'amount' => (float)$fee->amount,
];
});
$allRecords = $purchaseOrders->concat($utilityFees)
->sortByDesc('date')
->values();
return [
'records' => $allRecords,
'summary' => [
'total_amount' => $allRecords->sum('amount'),
'purchase_total' => $purchaseOrders->sum('amount'),
'utility_total' => $utilityFees->sum('amount'),
'record_count' => $allRecords->count(),
]
];
}
public function getUtilityFees(array $filters)
{
$query = UtilityFee::query();
if (!empty($filters['search'])) {
$search = $filters['search'];
$query->where(function($q) use ($search) {
$q->where('category', 'like', "%{$search}%")
->orWhere('invoice_number', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%");
});
}
if (!empty($filters['category']) && $filters['category'] !== 'all') {
$query->where('category', $filters['category']);
}
if (!empty($filters['date_start'])) {
$query->where('transaction_date', '>=', $filters['date_start']);
}
if (!empty($filters['date_end'])) {
$query->where('transaction_date', '<=', $filters['date_end']);
}
$sortField = $filters['sort_field'] ?? 'created_at';
$sortDirection = $filters['sort_direction'] ?? 'desc';
$query->orderBy($sortField, $sortDirection);
return $query->paginate($filters['per_page'] ?? 10);
}
public function getUniqueCategories(): Collection
{
return UtilityFee::distinct()->pluck('category');
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Modules\Inventory\Contracts;
interface InventoryServiceInterface
{
/**
* Check if a product has sufficient stock in a specific warehouse.
*
* @param int $productId
* @param int $warehouseId
* @param float $quantity
* @return bool
*/
public function checkStock(int $productId, int $warehouseId, float $quantity): bool;
/**
* Decrease stock for a product (e.g., when an order is placed).
*
* @param int $productId
* @param int $warehouseId
* @param float $quantity
* @param string|null $reason
* @return void
*/
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null): void;
/**
* Get all active warehouses.
*
* @return \Illuminate\Support\Collection
*/
public function getAllWarehouses();
/**
* Get multiple products by their IDs.
*
* @param array $ids
* @return \Illuminate\Support\Collection
*/
public function getProductsByIds(array $ids);
/**
* Get a specific product by ID.
*
* @param int $id
* @return object|null
*/
public function getProduct(int $id);
/**
* Get a specific warehouse by ID.
*
* @param int $id
* @return object|null
*/
public function getWarehouse(int $id);
/**
* Get all available inventories in a specific warehouse.
*
* @param int $warehouseId
* @return \Illuminate\Support\Collection
*/
public function getInventoriesByWarehouse(int $warehouseId);
/**
* Get all products.
*
* @return \Illuminate\Support\Collection
*/
public function getAllProducts();
/**
* Get all units.
*
* @return \Illuminate\Support\Collection
*/
public function getUnits();
/**
* Create a new inventory record (e.g., for finished goods).
*
* @param array $data
* @return object
*/
public function createInventoryRecord(array $data);
/**
* Decrease quantity of a specific inventory record.
*
* @param int $inventoryId
* @param float $quantity
* @param string|null $reason
* @param string|null $referenceType
* @param int|string|null $referenceId
* @return void
*/
public function decreaseInventoryQuantity(int $inventoryId, float $quantity, ?string $reason = null, ?string $referenceType = null, $referenceId = null);
}

View File

@@ -5,11 +5,16 @@ namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
class InventoryController extends Controller
{
public function index(\Illuminate\Http\Request $request, \App\Modules\Inventory\Models\Warehouse $warehouse)
public function index(Request $request, Warehouse $warehouse)
{
$warehouse->load([
'inventories.product.category',
@@ -17,7 +22,7 @@ class InventoryController extends Controller
'inventories.lastIncomingTransaction',
'inventories.lastOutgoingTransaction'
]);
$allProducts = \App\Modules\Inventory\Models\Product::with('category')->get();
$allProducts = Product::with('category')->get();
// 1. 準備 availableProducts
$availableProducts = $allProducts->map(function ($product) {
@@ -98,7 +103,7 @@ class InventoryController extends Controller
];
});
return \Inertia\Inertia::render('Warehouse/Inventory', [
return Inertia::render('Warehouse/Inventory', [
'warehouse' => $warehouse,
'inventories' => $inventories,
'safetyStockSettings' => $safetyStockSettings,
@@ -106,10 +111,10 @@ class InventoryController extends Controller
]);
}
public function create(\App\Modules\Inventory\Models\Warehouse $warehouse)
public function create(Warehouse $warehouse)
{
// 取得所有商品供前端選單使用
$products = \App\Modules\Inventory\Models\Product::with(['baseUnit', 'largeUnit'])
$products = Product::with(['baseUnit', 'largeUnit'])
->select('id', 'name', 'code', 'base_unit_id', 'large_unit_id', 'conversion_rate')
->get()
->map(function ($product) {
@@ -123,13 +128,13 @@ class InventoryController extends Controller
];
});
return \Inertia\Inertia::render('Warehouse/AddInventory', [
return Inertia::render('Warehouse/AddInventory', [
'warehouse' => $warehouse,
'products' => $products,
]);
}
public function store(\Illuminate\Http\Request $request, \App\Modules\Inventory\Models\Warehouse $warehouse)
public function store(Request $request, Warehouse $warehouse)
{
$validated = $request->validate([
'inboundDate' => 'required|date',
@@ -144,22 +149,22 @@ class InventoryController extends Controller
'items.*.expiryDate' => 'nullable|date',
]);
return \Illuminate\Support\Facades\DB::transaction(function () use ($validated, $warehouse) {
return DB::transaction(function () use ($validated, $warehouse) {
foreach ($validated['items'] as $item) {
$inventory = null;
if ($item['batchMode'] === 'existing') {
// 模式 A選擇現有批號 (包含已刪除的也要能找回來累加)
$inventory = \App\Modules\Inventory\Models\Inventory::withTrashed()->findOrFail($item['inventoryId']);
$inventory = Inventory::withTrashed()->findOrFail($item['inventoryId']);
if ($inventory->trashed()) {
$inventory->restore();
}
} else {
// 模式 B建立新批號
$originCountry = $item['originCountry'] ?? 'TW';
$product = \App\Modules\Inventory\Models\Product::find($item['productId']);
$product = Product::find($item['productId']);
$batchNumber = \App\Modules\Inventory\Models\Inventory::generateBatchNumber(
$batchNumber = Inventory::generateBatchNumber(
$product->code ?? 'UNK',
$originCountry,
$validated['inboundDate']
@@ -210,12 +215,12 @@ class InventoryController extends Controller
/**
* API: 取得商品在特定倉庫的所有批號,並回傳當前日期/產地下的一個流水號
*/
public function getBatches(\App\Modules\Inventory\Models\Warehouse $warehouse, $productId, \Illuminate\Http\Request $request)
public function getBatches(Warehouse $warehouse, $productId, Request $request)
{
$originCountry = $request->query('originCountry', 'TW');
$arrivalDate = $request->query('arrivalDate', now()->format('Y-m-d'));
$batches = \App\Modules\Inventory\Models\Inventory::where('warehouse_id', $warehouse->id)
$batches = Inventory::where('warehouse_id', $warehouse->id)
->where('product_id', $productId)
->get()
->map(function ($inventory) {
@@ -229,10 +234,10 @@ class InventoryController extends Controller
});
// 計算下一個流水號
$product = \App\Modules\Inventory\Models\Product::find($productId);
$product = Product::find($productId);
$nextSequence = '01';
if ($product) {
$batchNumber = \App\Modules\Inventory\Models\Inventory::generateBatchNumber(
$batchNumber = Inventory::generateBatchNumber(
$product->code ?? 'UNK',
$originCountry,
$arrivalDate
@@ -246,7 +251,7 @@ class InventoryController extends Controller
]);
}
public function edit(\Illuminate\Http\Request $request, \App\Modules\Inventory\Models\Warehouse $warehouse, $inventoryId)
public function edit(Request $request, Warehouse $warehouse, $inventoryId)
{
// 取得庫存紀錄,包含商品資訊與異動紀錄 (含經手人)
// 這裡如果是 Mock 的 ID (mock-inv-1),需要特殊處理
@@ -254,7 +259,7 @@ class InventoryController extends Controller
return redirect()->back()->with('error', '無法編輯範例資料');
}
$inventory = \App\Modules\Inventory\Models\Inventory::with(['product', 'transactions' => function($query) {
$inventory = Inventory::with(['product', 'transactions' => function($query) {
$query->orderBy('actual_time', 'desc')->orderBy('id', 'desc');
}, 'transactions.user'])->findOrFail($inventoryId);
@@ -284,20 +289,20 @@ class InventoryController extends Controller
];
});
return \Inertia\Inertia::render('Warehouse/EditInventory', [
return Inertia::render('Warehouse/EditInventory', [
'warehouse' => $warehouse,
'inventory' => $inventoryData,
'transactions' => $transactions,
]);
}
public function update(\Illuminate\Http\Request $request, \App\Modules\Inventory\Models\Warehouse $warehouse, $inventoryId)
public function update(Request $request, Warehouse $warehouse, $inventoryId)
{
// 若是 product ID (舊邏輯),先轉為 inventory
// 但新路由我們傳的是 inventory ID
// 為了相容,我們先判斷 $inventoryId 是 inventory ID
$inventory = \App\Modules\Inventory\Models\Inventory::find($inventoryId);
$inventory = Inventory::find($inventoryId);
// 如果找不到 (可能是舊路由傳 product ID)
if (!$inventory) {
@@ -322,7 +327,7 @@ class InventoryController extends Controller
'lastOutboundDate' => 'nullable|date',
]);
return \Illuminate\Support\Facades\DB::transaction(function () use ($validated, $inventory) {
return DB::transaction(function () use ($validated, $inventory) {
$currentQty = (float) $inventory->quantity;
$newQty = (float) $validated['quantity'];
@@ -395,9 +400,9 @@ class InventoryController extends Controller
});
}
public function destroy(\App\Modules\Inventory\Models\Warehouse $warehouse, $inventoryId)
public function destroy(Warehouse $warehouse, $inventoryId)
{
$inventory = \App\Modules\Inventory\Models\Inventory::findOrFail($inventoryId);
$inventory = Inventory::findOrFail($inventoryId);
// 庫存 > 0 不允許刪除 (哪怕是軟刪除)
if ($inventory->quantity > 0) {
@@ -430,7 +435,7 @@ class InventoryController extends Controller
if ($productId) {
// 商品層級查詢
$inventories = \App\Modules\Inventory\Models\Inventory::where('warehouse_id', $warehouse->id)
$inventories = Inventory::where('warehouse_id', $warehouse->id)
->where('product_id', $productId)
->with(['product', 'transactions' => function($query) {
$query->orderBy('actual_time', 'desc')->orderBy('id', 'desc');
@@ -491,7 +496,7 @@ class InventoryController extends Controller
];
})->values();
return \Inertia\Inertia::render('Warehouse/InventoryHistory', [
return Inertia::render('Warehouse/InventoryHistory', [
'warehouse' => $warehouse,
'inventory' => [
'id' => 'product-' . $productId,
@@ -505,7 +510,7 @@ class InventoryController extends Controller
if ($inventoryId) {
// 單一批號查詢
$inventory = \App\Modules\Inventory\Models\Inventory::with(['product', 'transactions' => function($query) {
$inventory = Inventory::with(['product', 'transactions' => function($query) {
$query->orderBy('actual_time', 'desc')->orderBy('id', 'desc');
}, 'transactions.user'])->findOrFail($inventoryId);
@@ -521,7 +526,7 @@ class InventoryController extends Controller
];
});
return \Inertia\Inertia::render('Warehouse/InventoryHistory', [
return Inertia::render('Warehouse/InventoryHistory', [
'warehouse' => $warehouse,
'inventory' => [
'id' => (string) $inventory->id,

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Unit;
use App\Modules\Inventory\Models\Category;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
@@ -13,7 +14,7 @@ use Inertia\Response;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
* 顯示資源列表。
*/
public function index(Request $request): Response
{
@@ -40,7 +41,7 @@ class ProductController extends Controller
$sortField = $request->input('sort_field', 'id');
$sortDirection = $request->input('sort_direction', 'desc');
// Define allowed sort fields to prevent SQL injection
// 定義允許的排序欄位以防止 SQL 注入
$allowedSorts = ['id', 'code', 'name', 'category_id', 'base_unit_id', 'conversion_rate'];
if (!in_array($sortField, $allowedSorts)) {
$sortField = 'id';
@@ -49,11 +50,11 @@ class ProductController extends Controller
$sortDirection = 'desc';
}
// Handle relation sorting (category name) separately if needed, or simple join
// 如果需要,分別處理關聯排序(分類名稱),或簡單的 join
if ($sortField === 'category_id') {
// Join categories for sorting by name? Or just by ID?
// Simple approach: sort by ID for now, or join if user wants name sort.
// Let's assume standard field sorting first.
// 加入分類以便按名稱排序?還是僅按 ID
// 簡單方法:目前按 ID 排序,如果使用者想要按名稱排序則 join。
// 先假設標準欄位排序。
$query->orderBy('category_id', $sortDirection);
} else {
$query->orderBy($sortField, $sortDirection);
@@ -61,18 +62,49 @@ class ProductController extends Controller
$products = $query->paginate($perPage)->withQueryString();
$categories = \App\Modules\Inventory\Models\Category::where('is_active', true)->get();
$products->getCollection()->transform(function ($product) {
return (object) [
'id' => (string) $product->id,
'code' => $product->code,
'name' => $product->name,
'categoryId' => $product->category_id,
'category' => $product->category ? (object) [
'id' => $product->category->id,
'name' => $product->category->name,
] : null,
'brand' => $product->brand,
'specification' => $product->specification,
'baseUnitId' => $product->base_unit_id,
'baseUnit' => $product->baseUnit ? (object) [
'id' => $product->baseUnit->id,
'name' => $product->baseUnit->name,
] : null,
'largeUnitId' => $product->large_unit_id,
'largeUnit' => $product->largeUnit ? (object) [
'id' => $product->largeUnit->id,
'name' => $product->largeUnit->name,
] : null,
'purchaseUnitId' => $product->purchase_unit_id,
'purchaseUnit' => $product->purchaseUnit ? (object) [
'id' => $product->purchaseUnit->id,
'name' => $product->purchaseUnit->name,
] : null,
'conversionRate' => (float) $product->conversion_rate,
];
});
$categories = Category::where('is_active', true)->get();
return Inertia::render('Product/Index', [
'products' => $products,
'categories' => $categories,
'units' => Unit::all(),
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
'filters' => $request->only(['search', 'category_id', 'per_page', 'sort_field', 'sort_direction']),
]);
}
/**
* Store a newly created resource in storage.
* 將新建立的資源儲存到儲存體中。
*/
public function store(Request $request)
{
@@ -107,7 +139,7 @@ class ProductController extends Controller
}
/**
* Update the specified resource in storage.
* 更新儲存體中的指定資源。
*/
public function update(Request $request, Product $product)
{
@@ -141,7 +173,7 @@ class ProductController extends Controller
}
/**
* Remove the specified resource from storage.
* 從儲存體中移除指定資源。
*/
public function destroy(Product $product)
{

View File

@@ -29,25 +29,30 @@ class TransferOrderController extends Controller
]);
return DB::transaction(function () use ($validated) {
// 1. 檢查來源倉庫庫存
// 1. 檢查來源倉庫庫存 (精確匹配產品與批號)
$sourceInventory = Inventory::where('warehouse_id', $validated['sourceWarehouseId'])
->where('product_id', $validated['productId'])
->where('batch_number', $validated['batchNumber'])
->first();
if (!$sourceInventory || $sourceInventory->quantity < $validated['quantity']) {
throw ValidationException::withMessages([
'quantity' => ['來源倉庫庫存不足'],
'quantity' => ['來源倉庫指定批號庫存不足'],
]);
}
// 2. 獲取或建立目標倉庫庫存
// 2. 獲取或建立目標倉庫庫存 (精確匹配產品與批號,並繼承效期與品質狀態)
$targetInventory = Inventory::firstOrCreate(
[
'warehouse_id' => $validated['targetWarehouseId'],
'product_id' => $validated['productId'],
'batch_number' => $validated['batchNumber'],
],
[
'quantity' => 0,
'expiry_date' => $sourceInventory->expiry_date,
'quality_status' => $sourceInventory->quality_status,
'origin_country' => $sourceInventory->origin_country,
]
);
@@ -109,11 +114,12 @@ class TransferOrderController extends Controller
->get()
->map(function ($inv) {
return [
'productId' => (string) $inv->product_id,
'productName' => $inv->product->name,
'batchNumber' => 'BATCH-' . $inv->id, // 模擬批號
'availableQty' => (float) $inv->quantity,
'unit' => $inv->product->baseUnit?->name ?? '個',
'product_id' => (string) $inv->product_id,
'product_name' => $inv->product->name,
'batch_number' => $inv->batch_number,
'quantity' => (float) $inv->quantity,
'unit_name' => $inv->product->baseUnit?->name ?? '個',
'expiry_date' => $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
];
});

View File

@@ -11,7 +11,7 @@ use Illuminate\Http\Request;
class UnitController extends Controller
{
/**
* Store a newly created resource in storage.
* 將新建立的資源儲存到儲存體中。
*/
public function store(Request $request)
{
@@ -31,7 +31,7 @@ class UnitController extends Controller
}
/**
* Update the specified resource in storage.
* 更新儲存體中的指定資源。
*/
public function update(Request $request, Unit $unit)
{
@@ -51,11 +51,11 @@ class UnitController extends Controller
}
/**
* Remove the specified resource from storage.
* 從儲存體中移除指定資源。
*/
public function destroy(Unit $unit)
{
// Check if unit is used in any product
// 檢查單位是否已被任何商品使用
$isUsed = Product::where('base_unit_id', $unit->id)
->orWhere('large_unit_id', $unit->id)
->orWhere('purchase_unit_id', $unit->id)

View File

@@ -24,13 +24,45 @@ class WarehouseController extends Controller
});
}
$warehouses = $query->withSum('inventories as total_quantity', 'quantity')
$warehouses = $query->withSum('inventories as book_stock', 'quantity') // 帳面庫存 = 所有庫存總和
->withSum(['inventories as available_stock' => function ($query) {
// 可用庫存 = 庫存 > 0 且 品質正常 且 (未過期 或 無效期)
$query->where('quantity', '>', 0)
->where('quality_status', 'normal')
->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
});
}], 'quantity')
->orderBy('created_at', 'desc')
->paginate(10)
->withQueryString();
// 修正各倉庫列表中的可用庫存計算:若倉庫不可銷售,則可用庫存為 0
$warehouses->getCollection()->transform(function ($w) {
if (!$w->is_sellable) {
$w->available_stock = 0;
}
return $w;
});
// 計算全域總計 (不分頁)
$totals = [
'available_stock' => \App\Modules\Inventory\Models\Inventory::where('quantity', '>', 0)
->where('quality_status', 'normal')
->whereHas('warehouse', function ($q) {
$q->where('is_sellable', true);
})
->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
})->sum('quantity'),
'book_stock' => \App\Modules\Inventory\Models\Inventory::sum('quantity'),
];
return Inertia::render('Warehouse/Index', [
'warehouses' => $warehouses,
'totals' => $totals,
'filters' => $request->only(['search']),
]);
}
@@ -41,9 +73,10 @@ class WarehouseController extends Controller
'name' => 'required|string|max:50',
'address' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_sellable' => 'nullable|boolean',
]);
// Auto-generate code
// 自動產生代碼
$prefix = 'WH';
$lastWarehouse = Warehouse::latest('id')->first();
$nextId = $lastWarehouse ? $lastWarehouse->id + 1 : 1;
@@ -62,6 +95,7 @@ class WarehouseController extends Controller
'name' => 'required|string|max:50',
'address' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_sellable' => 'nullable|boolean',
]);
$warehouse->update($validated);

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Modules\Inventory;
use Illuminate\Support\ServiceProvider;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Services\InventoryService;
class InventoryServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(InventoryServiceInterface::class, InventoryService::class);
}
public function boot(): void
{
//
}
}

View File

@@ -4,7 +4,7 @@ namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Procurement\Models\PurchaseOrder; // Cross-module dependency
class Inventory extends Model
{
@@ -35,8 +35,8 @@ class Inventory extends Model
];
/**
* Transient property to store the reason for the activity log (e.g., "Replenishment #123").
* This is not stored in the database column but used for logging context.
* 用於活動記錄的暫時屬性(例如 "補貨 #123")。
* 此屬性不存儲在資料庫欄位中,但用於記錄上下文。
* @var string|null
*/
public $activityLogReason;
@@ -55,12 +55,12 @@ class Inventory extends Model
$attributes = $properties['attributes'] ?? [];
$snapshot = $properties['snapshot'] ?? [];
// Always snapshot names for context, even if IDs didn't change
// $this refers to the Inventory model instance
// 始終對名稱進行快照以便於上下文顯示,即使 ID 未更改
// $this 指的是 Inventory 模型實例
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : ($snapshot['warehouse_name'] ?? null);
$snapshot['product_name'] = $this->product ? $this->product->name : ($snapshot['product_name'] ?? null);
// Capture the reason if set
// 如果已設定原因,則進行捕捉
if ($this->activityLogReason) {
$attributes['_reason'] = $this->activityLogReason;
}
@@ -105,13 +105,7 @@ class Inventory extends Model
});
}
/**
* 來源採購單
*/
public function sourcePurchaseOrder(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(PurchaseOrder::class, 'source_purchase_order_id');
}
/**
* 產生批號

View File

@@ -4,7 +4,7 @@ namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Core\Models\User; // Cross-module Core dependency
use App\Modules\Core\Models\User; // 跨模組核心依賴
class InventoryTransaction extends Model
{

View File

@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use App\Modules\Procurement\Models\Vendor; // Cross-module dependency (Procurement)
class Product extends Model
{
@@ -32,7 +32,7 @@ class Product extends Model
];
/**
* Get the category that owns the product.
* 取得該商品所屬的分類。
*/
public function category(): BelongsTo
{
@@ -54,10 +54,7 @@ class Product extends Model
return $this->belongsTo(Unit::class, 'purchase_unit_id');
}
public function vendors(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Vendor::class)->withPivot('last_price')->withTimestamps();
}
public function inventories(): \Illuminate\Database\Eloquent\Relations\HasMany
{
@@ -83,13 +80,13 @@ class Product extends Model
$attributes = $properties['attributes'] ?? [];
$snapshot = $properties['snapshot'] ?? [];
// Handle Category Name Snapshot
// 處理分類名稱快照
if (isset($attributes['category_id'])) {
$category = Category::find($attributes['category_id']);
$snapshot['category_name'] = $category ? $category->name : null;
}
// Handle Unit Name Snapshots
// 處理單位名稱快照
$unitFields = ['base_unit_id', 'large_unit_id', 'purchase_unit_id'];
foreach ($unitFields as $field) {
if (isset($attributes[$field])) {
@@ -99,7 +96,7 @@ class Product extends Model
}
}
// Always snapshot self name for context (so logs always show "Cola")
// 始終對自身名稱進行快照以便於上下文顯示(這樣日誌總是顯示 "可樂"
$snapshot['name'] = $this->name;
$properties['attributes'] = $attributes;

View File

@@ -4,7 +4,7 @@ namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Procurement\Models\PurchaseOrder; // Cross-module dependency (Procurement)
class Warehouse extends Model
{
@@ -17,6 +17,11 @@ class Warehouse extends Model
'name',
'address',
'description',
'is_sellable',
];
protected $casts = [
'is_sellable' => 'boolean',
];
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
@@ -43,10 +48,7 @@ class Warehouse extends Model
return $this->hasMany(Inventory::class);
}
public function purchaseOrders(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(PurchaseOrder::class);
}
public function products(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{

View File

@@ -0,0 +1,168 @@
<?php
namespace App\Modules\Inventory\Services;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\Product;
use Illuminate\Support\Facades\DB;
class InventoryService implements InventoryServiceInterface
{
public function getAllWarehouses()
{
return Warehouse::all();
}
public function getAllProducts()
{
return Product::with(['baseUnit'])->get();
}
public function getUnits()
{
return \App\Modules\Inventory\Models\Unit::all();
}
public function getInventoriesByIds(array $ids, array $with = [])
{
return Inventory::whereIn('id', $ids)->with($with)->get();
}
public function getProduct(int $id)
{
return Product::find($id);
}
public function getProductsByIds(array $ids)
{
return Product::whereIn('id', $ids)->get();
}
public function getWarehouse(int $id)
{
return Warehouse::find($id);
}
public function checkStock(int $productId, int $warehouseId, float $quantity): bool
{
$stock = Inventory::where('product_id', $productId)
->where('warehouse_id', $warehouseId)
->sum('quantity');
return $stock >= $quantity;
}
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null): void
{
DB::transaction(function () use ($productId, $warehouseId, $quantity, $reason) {
$inventories = Inventory::where('product_id', $productId)
->where('warehouse_id', $warehouseId)
->where('quantity', '>', 0)
->orderBy('arrival_date', 'asc')
->get();
$remainingToDecrease = $quantity;
foreach ($inventories as $inventory) {
if ($remainingToDecrease <= 0) break;
$decreaseAmount = min($inventory->quantity, $remainingToDecrease);
$this->decreaseInventoryQuantity($inventory->id, $decreaseAmount, $reason);
$remainingToDecrease -= $decreaseAmount;
}
if ($remainingToDecrease > 0) {
// 這裡可以選擇報錯或允許負庫存,目前為了嚴謹拋出異常
throw new \Exception("庫存不足,無法扣除所有請求的數量。");
}
});
}
public function getInventoriesByWarehouse(int $warehouseId)
{
return Inventory::with(['product.baseUnit', 'product.largeUnit'])
->where('warehouse_id', $warehouseId)
->where('quantity', '>', 0)
->orderBy('arrival_date', 'asc')
->get();
}
public function createInventoryRecord(array $data)
{
return DB::transaction(function () use ($data) {
// 嘗試查找是否已有相同批號的庫存
$inventory = Inventory::where('warehouse_id', $data['warehouse_id'])
->where('product_id', $data['product_id'])
->where('batch_number', $data['batch_number'] ?? null)
->first();
$balanceBefore = 0;
if ($inventory) {
// 若存在,則更新數量與相關資訊 (鎖定行以避免併發問題)
$inventory = Inventory::lockForUpdate()->find($inventory->id);
$balanceBefore = $inventory->quantity;
$inventory->quantity += $data['quantity'];
// 更新其他可能變更的欄位 (如最後入庫日)
$inventory->arrival_date = $data['arrival_date'] ?? $inventory->arrival_date;
$inventory->save();
} else {
// 若不存在,則建立新紀錄
$inventory = Inventory::create([
'warehouse_id' => $data['warehouse_id'],
'product_id' => $data['product_id'],
'quantity' => $data['quantity'],
'batch_number' => $data['batch_number'] ?? null,
'box_number' => $data['box_number'] ?? null,
'origin_country' => $data['origin_country'] ?? 'TW',
'arrival_date' => $data['arrival_date'] ?? now(),
'expiry_date' => $data['expiry_date'] ?? null,
'quality_status' => $data['quality_status'] ?? 'normal',
'source_purchase_order_id' => $data['source_purchase_order_id'] ?? null,
]);
}
\App\Modules\Inventory\Models\InventoryTransaction::create([
'inventory_id' => $inventory->id,
'type' => '入庫',
'quantity' => $data['quantity'],
'balance_before' => $balanceBefore,
'balance_after' => $inventory->quantity,
'reason' => $data['reason'] ?? '手動入庫',
'reference_type' => $data['reference_type'] ?? null,
'reference_id' => $data['reference_id'] ?? null,
'user_id' => auth()->id(),
'actual_time' => now(),
]);
return $inventory;
});
}
public function decreaseInventoryQuantity(int $inventoryId, float $quantity, ?string $reason = null, ?string $referenceType = null, $referenceId = null): void
{
DB::transaction(function () use ($inventoryId, $quantity, $reason, $referenceType, $referenceId) {
$inventory = Inventory::lockForUpdate()->findOrFail($inventoryId);
$balanceBefore = $inventory->quantity;
$inventory->decrement('quantity', $quantity);
$inventory->refresh();
\App\Modules\Inventory\Models\InventoryTransaction::create([
'inventory_id' => $inventory->id,
'type' => '出庫',
'quantity' => -$quantity,
'balance_before' => $balanceBefore,
'balance_after' => $inventory->quantity,
'reason' => $reason ?? '庫存扣減',
'reference_type' => $referenceType,
'reference_id' => $referenceId,
'user_id' => auth()->id(),
'actual_time' => now(),
]);
});
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Modules\Procurement\Contracts;
use Illuminate\Support\Collection;
interface ProcurementServiceInterface
{
/**
* Get purchase orders within a date range.
*
* @param string $start
* @param string $end
* @param array $statuses
* @return Collection
*/
public function getPurchaseOrdersByDate(string $start, string $end, array $statuses = ['received', 'completed']): Collection;
/**
* Get purchase orders by multiple IDs.
*
* @param array $ids
* @param array $with
* @return Collection
*/
public function getPurchaseOrdersByIds(array $ids, array $with = []): Collection;
}

View File

@@ -6,18 +6,30 @@ use App\Http\Controllers\Controller;
use App\Modules\Procurement\Models\PurchaseOrder;
use App\Modules\Procurement\Models\Vendor;
use App\Modules\Inventory\Models\Warehouse;
// use App\Modules\Inventory\Models\Warehouse; // REFACTORED: 移除直接依賴
use App\Modules\Inventory\Contracts\InventoryServiceInterface; // NEW: 使用契約
use App\Modules\Core\Contracts\CoreServiceInterface; // NEW: 使用核心服務契約
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\DB;
class PurchaseOrderController extends Controller
{
protected $inventoryService;
protected $coreService;
public function __construct(InventoryServiceInterface $inventoryService, CoreServiceInterface $coreService)
{
$this->inventoryService = $inventoryService;
$this->coreService = $coreService;
}
public function index(Request $request)
{
$query = PurchaseOrder::with(['vendor', 'warehouse', 'user']);
// 1. 從關聯中移除 'warehouse''user'
$query = PurchaseOrder::with(['vendor']);
// Search
// 搜尋
if ($request->search) {
$query->where(function($q) use ($request) {
$q->where('code', 'like', "%{$request->search}%")
@@ -27,7 +39,7 @@ class PurchaseOrderController extends Controller
});
}
// Filters
// 篩選
if ($request->status && $request->status !== 'all') {
$query->where('status', $request->status);
}
@@ -36,7 +48,7 @@ class PurchaseOrderController extends Controller
$query->where('warehouse_id', $request->warehouse_id);
}
// Date Range
// 日期範圍
if ($request->date_start) {
$query->whereDate('created_at', '>=', $request->date_start);
}
@@ -45,7 +57,7 @@ class PurchaseOrderController extends Controller
$query->whereDate('created_at', '<=', $request->date_end);
}
// Sorting
// 排序
$sortField = $request->sort_field ?? 'id';
$sortDirection = $request->sort_direction ?? 'desc';
$allowedSortFields = ['id', 'code', 'status', 'total_amount', 'created_at', 'expected_delivery_date'];
@@ -57,36 +69,89 @@ class PurchaseOrderController extends Controller
$perPage = $request->input('per_page', 10);
$orders = $query->paginate($perPage)->withQueryString();
// 2. 手動注入倉庫與使用者資料
$warehouses = $this->inventoryService->getAllWarehouses();
$userIds = $orders->getCollection()->pluck('user_id')->unique()->toArray();
$users = $this->coreService->getUsersByIds($userIds)->keyBy('id');
$orders->getCollection()->transform(function ($order) use ($warehouses, $users) {
// 水和倉庫
$warehouse = $warehouses->firstWhere('id', $order->warehouse_id);
$order->setRelation('warehouse', $warehouse);
// 水和使用者
$user = $users->get($order->user_id);
$order->setRelation('user', $user);
// 轉換為前端期望的格式 (camelCase)
return (object) [
'id' => (string) $order->id,
'poNumber' => $order->code,
'supplierId' => (string) $order->vendor_id,
'supplierName' => $order->vendor?->name ?? 'Unknown',
'expectedDate' => $order->expected_delivery_date?->toISOString(),
'status' => $order->status,
'totalAmount' => (float) $order->total_amount,
'taxAmount' => (float) $order->tax_amount,
'grandTotal' => (float) $order->grand_total,
'createdAt' => $order->created_at->toISOString(),
'createdBy' => $user?->name ?? 'System',
'warehouse_id' => (int) $order->warehouse_id,
'warehouse_name' => $warehouse?->name ?? 'Unknown',
'remark' => $order->remark,
];
});
return Inertia::render('PurchaseOrder/Index', [
'orders' => $orders,
'filters' => $request->only(['search', 'status', 'warehouse_id', 'date_start', 'date_end', 'sort_field', 'sort_direction', 'per_page']),
'warehouses' => Warehouse::all(['id', 'name']),
'warehouses' => $warehouses->map(fn($w)=>(object)['id'=>$w->id, 'name'=>$w->name]),
]);
}
public function create()
{
$vendors = Vendor::with(['products.baseUnit', 'products.largeUnit', 'products.purchaseUnit'])->get()->map(function ($vendor) {
// 1. 獲取廠商(無關聯)
$vendors = Vendor::all();
// 2. 手動注入:獲取 Pivot 資料
$vendorIds = $vendors->pluck('id')->toArray();
$pivots = DB::table('product_vendor')->whereIn('vendor_id', $vendorIds)->get();
$productIds = $pivots->pluck('product_id')->unique()->toArray();
// 3. 從服務獲取商品
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
// 4. 重建前端結構
$vendors = $vendors->map(function ($vendor) use ($pivots, $products) {
$vendorProductPivots = $pivots->where('vendor_id', $vendor->id);
$commonProducts = $vendorProductPivots->map(function($pivot) use ($products) {
$product = $products[$pivot->product_id] ?? null;
if (!$product) return null;
return [
'productId' => (string) $product->id,
'productName' => $product->name,
'base_unit_id' => $product->base_unit_id,
'base_unit_name' => $product->baseUnit?->name,
'large_unit_id' => $product->large_unit_id,
'large_unit_name' => $product->largeUnit?->name,
'purchase_unit_id' => $product->purchase_unit_id,
'conversion_rate' => (float) $product->conversion_rate,
'lastPrice' => (float) $pivot->last_price,
];
})->filter()->values();
return [
'id' => (string) $vendor->id,
'name' => $vendor->name,
'commonProducts' => $vendor->products->map(function ($product) {
return [
'productId' => (string) $product->id,
'productName' => $product->name,
'base_unit_id' => $product->base_unit_id,
'base_unit_name' => $product->baseUnit?->name,
'large_unit_id' => $product->large_unit_id,
'large_unit_name' => $product->largeUnit?->name,
'purchase_unit_id' => $product->purchase_unit_id,
'conversion_rate' => (float) $product->conversion_rate,
'lastPrice' => (float) ($product->pivot->last_price ?? 0),
];
})
'commonProducts' => $commonProducts
];
});
$warehouses = Warehouse::all()->map(function ($w) {
$warehouses = $this->inventoryService->getAllWarehouses()->map(function ($w) {
return [
'id' => (string) $w->id,
'name' => $w->name,
@@ -141,7 +206,7 @@ class PurchaseOrderController extends Controller
$totalAmount += $item['subtotal'];
}
// Tax calculation
// 稅額計算
$taxAmount = isset($validated['tax_amount']) ? $validated['tax_amount'] : round($totalAmount * 0.05, 2);
$grandTotal = $totalAmount + $taxAmount;
@@ -200,120 +265,148 @@ class PurchaseOrderController extends Controller
public function show($id)
{
$order = PurchaseOrder::with(['vendor', 'warehouse', 'user', 'items.product.baseUnit', 'items.product.largeUnit'])->findOrFail($id);
$order = PurchaseOrder::with(['vendor', 'items'])->findOrFail($id);
$order->items->transform(function ($item) use ($order) {
$product = $item->product;
if ($product) {
// 手動附加所有必要的屬性
$item->productId = (string) $product->id;
$item->productName = $product->name;
$item->base_unit_id = $product->base_unit_id;
$item->base_unit_name = $product->baseUnit?->name;
$item->large_unit_id = $product->large_unit_id;
$item->large_unit_name = $product->largeUnit?->name;
$item->purchase_unit_id = $product->purchase_unit_id;
$item->conversion_rate = (float) $product->conversion_rate;
// Fetch last price
$lastPrice = DB::table('product_vendor')
->where('vendor_id', $order->vendor_id)
->where('product_id', $product->id)
->value('last_price');
$item->previousPrice = (float) ($lastPrice ?? 0);
// 手動注入
$order->setRelation('warehouse', $this->inventoryService->getWarehouse($order->warehouse_id));
$order->setRelation('user', $this->coreService->getUser($order->user_id));
// 設定當前選中的單位 ID (from saved item)
$item->unitId = $item->unit_id;
$productIds = $order->items->pluck('product_id')->unique()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
// 決定 selectedUnit (用於 UI 顯示)
if ($item->unitId && $item->large_unit_id && $item->unitId == $item->large_unit_id) {
$item->selectedUnit = 'large';
} else {
$item->selectedUnit = 'base';
}
$item->unitPrice = (float) $item->unit_price;
}
return $item;
$formattedItems = $order->items->map(function ($item) use ($order, $products) {
$product = $products[$item->product_id] ?? null;
return (object) [
'productId' => (string) $item->product_id,
'productName' => $product?->name ?? 'Unknown',
'quantity' => (float) $item->quantity,
'unitId' => $item->unit_id,
'base_unit_id' => $product?->base_unit_id,
'base_unit_name' => $product?->baseUnit?->name,
'large_unit_id' => $product?->large_unit_id,
'large_unit_name' => $product?->largeUnit?->name,
'purchase_unit_id' => $product?->purchase_unit_id,
'conversion_rate' => (float) ($product?->conversion_rate ?? 1),
'selectedUnit' => ($item->unit_id && $product?->large_unit_id && $item->unit_id == $product->large_unit_id) ? 'large' : 'base',
'unitPrice' => (float) $item->unit_price,
'previousPrice' => (float) (DB::table('product_vendor')->where('vendor_id', $order->vendor_id)->where('product_id', $item->product_id)->value('last_price') ?? 0),
'subtotal' => (float) $item->subtotal,
];
});
$formattedOrder = (object) [
'id' => (string) $order->id,
'poNumber' => $order->code,
'supplierId' => (string) $order->vendor_id,
'supplierName' => $order->vendor?->name ?? 'Unknown',
'expectedDate' => $order->expected_delivery_date?->toISOString(),
'status' => $order->status,
'items' => $formattedItems,
'totalAmount' => (float) $order->total_amount,
'taxAmount' => (float) $order->tax_amount,
'grandTotal' => (float) $order->grand_total,
'createdAt' => $order->created_at->toISOString(),
'createdBy' => $order->user?->name ?? 'System',
'warehouse_id' => (int) $order->warehouse_id,
'warehouse_name' => $order->warehouse?->name ?? 'Unknown',
'remark' => $order->remark,
'invoiceNumber' => $order->invoice_number,
'invoiceDate' => $order->invoice_date,
'invoiceAmount' => (float) $order->invoice_amount,
];
return Inertia::render('PurchaseOrder/Show', [
'order' => $order
'order' => $formattedOrder
]);
}
public function edit($id)
{
$order = PurchaseOrder::with(['items.product'])->findOrFail($id);
// 1. 獲取訂單
$order = PurchaseOrder::with(['items'])->findOrFail($id);
// 2. 獲取廠商與商品(與 create 邏輯一致)
$vendors = Vendor::all();
$vendorIds = $vendors->pluck('id')->toArray();
$pivots = DB::table('product_vendor')->whereIn('vendor_id', $vendorIds)->get();
$productIds = $pivots->pluck('product_id')->unique()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
$vendors = $vendors->map(function ($vendor) use ($pivots, $products) {
$vendorProductPivots = $pivots->where('vendor_id', $vendor->id);
$commonProducts = $vendorProductPivots->map(function($pivot) use ($products) {
$product = $products[$pivot->product_id] ?? null;
if (!$product) return null;
return [
'productId' => (string) $product->id,
'productName' => $product->name,
'base_unit_id' => $product->base_unit_id,
'base_unit_name' => $product->baseUnit?->name,
'large_unit_id' => $product->large_unit_id,
'large_unit_name' => $product->largeUnit?->name,
'purchase_unit_id' => $product->purchase_unit_id,
'conversion_rate' => (float) $product->conversion_rate,
'lastPrice' => (float) $pivot->last_price,
];
})->filter()->values();
$vendors = Vendor::with(['products.baseUnit', 'products.largeUnit', 'products.purchaseUnit'])->get()->map(function ($vendor) {
return [
'id' => (string) $vendor->id,
'name' => $vendor->name,
'commonProducts' => $vendor->products->map(function ($product) {
return [
'productId' => (string) $product->id,
'productName' => $product->name,
'base_unit_id' => $product->base_unit_id,
'base_unit_name' => $product->baseUnit?->name,
'large_unit_id' => $product->large_unit_id,
'large_unit_name' => $product->largeUnit?->name,
'purchase_unit_id' => $product->purchase_unit_id,
'conversion_rate' => (float) $product->conversion_rate,
'lastPrice' => (float) ($product->pivot->last_price ?? 0),
];
})
'commonProducts' => $commonProducts
];
});
$warehouses = Warehouse::all()->map(function ($w) {
// 3. 獲取倉庫
$warehouses = $this->inventoryService->getAllWarehouses()->map(function ($w) {
return [
'id' => (string) $w->id,
'name' => $w->name,
];
});
// Transform items for frontend form
// Transform items for frontend form
// 4. 注入訂單項目特定資料
// 2. 注入訂單項目
$itemProductIds = $order->items->pluck('product_id')->toArray();
$itemProducts = $this->inventoryService->getProductsByIds($itemProductIds)->keyBy('id');
$vendorId = $order->vendor_id;
$order->items->transform(function ($item) use ($vendorId) {
$product = $item->product;
if ($product) {
// 手動附加所有必要的屬性
$item->productId = (string) $product->id;
$item->productName = $product->name;
$item->base_unit_id = $product->base_unit_id;
$item->base_unit_name = $product->baseUnit?->name;
$item->large_unit_id = $product->large_unit_id;
$item->large_unit_name = $product->largeUnit?->name;
$item->conversion_rate = (float) $product->conversion_rate;
// Fetch last price
$lastPrice = DB::table('product_vendor')
->where('vendor_id', $vendorId)
->where('product_id', $product->id)
->value('last_price');
$item->previousPrice = (float) ($lastPrice ?? 0);
// 設定當前選中的單位 ID
$item->unitId = $item->unit_id; // 資料庫中的 unit_id
// 決定 selectedUnit (用於 UI 狀態)
if ($item->unitId && $item->large_unit_id && $item->unitId == $item->large_unit_id) {
$item->selectedUnit = 'large';
} else {
$item->selectedUnit = 'base';
}
$item->unitPrice = (float) $item->unit_price;
}
return $item;
$formattedItems = $order->items->map(function ($item) use ($vendorId, $itemProducts) {
$product = $itemProducts[$item->product_id] ?? null;
return (object) [
'productId' => (string) $item->product_id,
'productName' => $product?->name ?? 'Unknown',
'quantity' => (float) $item->quantity,
'unitId' => $item->unit_id,
'base_unit_id' => $product?->base_unit_id,
'base_unit_name' => $product?->baseUnit?->name,
'large_unit_id' => $product?->large_unit_id,
'large_unit_name' => $product?->largeUnit?->name,
'conversion_rate' => (float) ($product?->conversion_rate ?? 1),
'selectedUnit' => ($item->unit_id && $product?->large_unit_id && $item->unit_id == $product->large_unit_id) ? 'large' : 'base',
'unitPrice' => (float) $item->unit_price,
'previousPrice' => (float) (DB::table('product_vendor')->where('vendor_id', $vendorId)->where('product_id', $item->product_id)->value('last_price') ?? 0),
'subtotal' => (float) $item->subtotal,
];
});
$formattedOrder = (object) [
'id' => (string) $order->id,
'poNumber' => $order->code,
'supplierId' => (string) $order->vendor_id,
'warehouse_id' => (int) $order->warehouse_id,
'expectedDate' => $order->expected_delivery_date?->format('Y-m-d'),
'status' => $order->status,
'items' => $formattedItems,
'remark' => $order->remark,
'invoiceNumber' => $order->invoice_number,
'invoiceDate' => $order->invoice_date,
'invoiceAmount' => (float) $order->invoice_amount,
'taxAmount' => (float) $order->tax_amount,
];
return Inertia::render('PurchaseOrder/Create', [
'order' => $order,
'order' => $formattedOrder,
'suppliers' => $vendors,
'warehouses' => $warehouses,
]);
@@ -337,7 +430,7 @@ class PurchaseOrderController extends Controller
'items.*.quantity' => 'required|numeric|min:0.01',
'items.*.subtotal' => 'required|numeric|min:0', // 總金額
'items.*.unitId' => 'nullable|exists:units,id',
// Allow both tax_amount and taxAmount for compatibility
// 允許 tax_amount taxAmount 以保持相容性
'tax_amount' => 'nullable|numeric|min:0',
'taxAmount' => 'nullable|numeric|min:0',
]);
@@ -350,12 +443,12 @@ class PurchaseOrderController extends Controller
$totalAmount += $item['subtotal'];
}
// Tax calculation (handle both keys)
// 稅額計算(處理兩個鍵)
$inputTax = $validated['tax_amount'] ?? $validated['taxAmount'] ?? null;
$taxAmount = !is_null($inputTax) ? $inputTax : round($totalAmount * 0.05, 2);
$grandTotal = $totalAmount + $taxAmount;
// 1. Fill attributes but don't save yet to capture changes
// 1. 填充屬性但暫不儲存以捕捉變更
$order->fill([
'vendor_id' => $validated['vendor_id'],
'warehouse_id' => $validated['warehouse_id'],
@@ -370,7 +463,7 @@ class PurchaseOrderController extends Controller
'invoice_amount' => $validated['invoice_amount'] ?? null,
]);
// Capture attribute changes for manual logging
// 捕捉變更屬性以進行手動記錄
$dirty = $order->getDirty();
$oldAttributes = [];
$newAttributes = [];
@@ -380,10 +473,10 @@ class PurchaseOrderController extends Controller
$newAttributes[$key] = $value;
}
// Save without triggering events (prevents duplicate log)
// 儲存但不觸發事件(防止重複記錄)
$order->saveQuietly();
// 2. Capture old items with product names for diffing
// 2. 捕捉包含商品名稱的舊項目以進行比對
$oldItems = $order->items()->with('product', 'unit')->get()->map(function($item) {
return [
'id' => $item->id,
@@ -396,7 +489,7 @@ class PurchaseOrderController extends Controller
];
})->keyBy('product_id');
// Sync items (Original logic)
// 同步項目(原始邏輯)
$order->items()->delete();
$newItemsData = [];
@@ -414,14 +507,14 @@ class PurchaseOrderController extends Controller
$newItemsData[] = $newItem;
}
// 3. Calculate Item Diffs
// 3. 計算項目差異
$itemDiffs = [
'added' => [],
'removed' => [],
'updated' => [],
];
// Re-fetch new items to ensure we have fresh relations
// 重新獲取新項目以確保擁有最新的關聯
$newItemsFormatted = $order->items()->with('product', 'unit')->get()->map(function($item) {
return [
'product_id' => $item->product_id,
@@ -433,20 +526,20 @@ class PurchaseOrderController extends Controller
];
})->keyBy('product_id');
// Find removed
// 找出已移除的項目
foreach ($oldItems as $productId => $oldItem) {
if (!$newItemsFormatted->has($productId)) {
$itemDiffs['removed'][] = $oldItem;
}
}
// Find added and updated
// 找出新增和更新的項目
foreach ($newItemsFormatted as $productId => $newItem) {
if (!$oldItems->has($productId)) {
$itemDiffs['added'][] = $newItem;
} else {
$oldItem = $oldItems[$productId];
// Compare fields
// 比對欄位
if (
$oldItem['quantity'] != $newItem['quantity'] ||
$oldItem['unit_id'] != $newItem['unit_id'] ||
@@ -469,8 +562,8 @@ class PurchaseOrderController extends Controller
}
}
// 4. Manually Log activity (Single Consolidated Log)
// Log if there are attribute changes OR item changes
// 4. 手動記錄活動(單一整合記錄)
// 如果有屬性變更或項目變更則記錄
if (!empty($newAttributes) || !empty($itemDiffs['added']) || !empty($itemDiffs['removed']) || !empty($itemDiffs['updated'])) {
activity()
->performedOn($order)
@@ -505,19 +598,24 @@ class PurchaseOrderController extends Controller
try {
DB::beginTransaction();
$order = PurchaseOrder::with(['items.product', 'items.unit'])->findOrFail($id);
$order = PurchaseOrder::with(['items'])->findOrFail($id);
// Capture items for logging
$items = $order->items->map(function ($item) {
// 為記錄注入資料
$productIds = $order->items->pluck('product_id')->unique()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
// 捕捉項目以進行記錄
$items = $order->items->map(function ($item) use ($products) {
$product = $products[$item->product_id] ?? null;
return [
'product_name' => $item->product_name,
'product_name' => $product?->name ?? 'Unknown',
'quantity' => floatval($item->quantity),
'unit_name' => $item->unit_name,
'unit_name' => 'N/A',
'subtotal' => floatval($item->subtotal),
];
})->toArray();
// Manually log the deletion with items
// 手動記錄包含項目的刪除操作
activity()
->performedOn($order)
->causedBy(auth()->user())
@@ -538,10 +636,10 @@ class PurchaseOrderController extends Controller
])
->log('deleted');
// Disable automatic logging for this operation
// 對此操作停用自動記錄
$order->disableLogging();
// Delete associated items first
// 先刪除關聯項目
$order->items()->delete();
$order->delete();

View File

@@ -4,15 +4,21 @@ 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 Inertia\Inertia;
use Inertia\Response;
class VendorController extends Controller
{
public function __construct(
protected InventoryServiceInterface $inventoryService
) {}
/**
* Display a listing of the resource.
* 顯示資源列表。
*/
public function index(\Illuminate\Http\Request $request): \Inertia\Response
public function index(Request $request): Response
{
$query = Vendor::query();
@@ -44,28 +50,71 @@ class VendorController extends Controller
->paginate($perPage)
->withQueryString();
return \Inertia\Inertia::render('Vendor/Index', [
$vendors->getCollection()->transform(function ($vendor) {
return (object) [
'id' => (string) $vendor->id,
'code' => $vendor->code,
'name' => $vendor->name,
'shortName' => $vendor->short_name,
'taxId' => $vendor->tax_id,
'owner' => $vendor->owner,
'contactName' => $vendor->contact_name,
'phone' => $vendor->phone,
'tel' => $vendor->tel,
'email' => $vendor->email,
'address' => $vendor->address,
'remark' => $vendor->remark,
];
});
return Inertia::render('Vendor/Index', [
'vendors' => $vendors,
'filters' => $request->only(['search', 'sort_field', 'sort_direction', 'per_page']),
]);
}
/**
* Display the specified resource.
* 顯示指定資源。
*/
public function show(Vendor $vendor): \Inertia\Response
public function show(Vendor $vendor): Response
{
$vendor->load(['products.baseUnit', 'products.largeUnit']);
return \Inertia\Inertia::render('Vendor/Show', [
'vendor' => $vendor,
'products' => \App\Modules\Inventory\Models\Product::with('baseUnit')->get(),
$formattedVendor = (object) [
'id' => (string) $vendor->id,
'code' => $vendor->code,
'name' => $vendor->name,
'shortName' => $vendor->short_name,
'taxId' => $vendor->tax_id,
'owner' => $vendor->owner,
'contactName' => $vendor->contact_name,
'phone' => $vendor->phone,
'tel' => $vendor->tel,
'email' => $vendor->email,
'address' => $vendor->address,
'remark' => $vendor->remark,
'supplyProducts' => $vendor->products->map(fn($p) => (object) [
'id' => (string) $p->pivot->id,
'productId' => (string) $p->id,
'productName' => $p->name,
'unit' => $p->baseUnit?->name ?? 'N/A',
'baseUnit' => $p->baseUnit?->name,
'largeUnit' => $p->largeUnit?->name,
'conversionRate' => (float) $p->conversion_rate,
'lastPrice' => (float) $p->pivot->last_price,
]),
];
return Inertia::render('Vendor/Show', [
'vendor' => $formattedVendor,
'products' => $this->inventoryService->getAllProducts(), // 使用已有的服務獲取所有商品供選取
]);
}
/**
* Store a newly created resource in storage.
* 將新建立的資源儲存到儲存體中。
*/
public function store(\Illuminate\Http\Request $request)
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
@@ -80,7 +129,7 @@ class VendorController extends Controller
'remark' => 'nullable|string',
]);
// Auto-generate code
// 自動產生代碼
$prefix = 'V';
$lastVendor = Vendor::latest('id')->first();
$nextId = $lastVendor ? $lastVendor->id + 1 : 1;
@@ -94,9 +143,9 @@ class VendorController extends Controller
}
/**
* Update the specified resource in storage.
* 更新儲存體中的指定資源。
*/
public function update(\Illuminate\Http\Request $request, Vendor $vendor)
public function update(Request $request, Vendor $vendor)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
@@ -117,7 +166,7 @@ class VendorController extends Controller
}
/**
* Remove the specified resource from storage.
* 從儲存體中移除指定資源。
*/
public function destroy(Vendor $vendor)
{

View File

@@ -4,12 +4,16 @@ 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)
*/
@@ -30,7 +34,7 @@ class VendorProductController extends Controller
]);
// 記錄操作
$product = \App\Modules\Inventory\Models\Product::find($validated['product_id']);
$product = $this->inventoryService->getProduct($validated['product_id']);
activity()
->performedOn($vendor)
->withProperties([
@@ -68,7 +72,7 @@ class VendorProductController extends Controller
]);
// 記錄操作
$product = \App\Modules\Inventory\Models\Product::find($productId);
$product = $this->inventoryService->getProduct($productId);
activity()
->performedOn($vendor)
->withProperties([
@@ -97,7 +101,7 @@ class VendorProductController extends Controller
public function destroy(Vendor $vendor, $productId)
{
// 記錄操作 (需在 detach 前獲取資訊)
$product = \App\Modules\Inventory\Models\Product::find($productId);
$product = $this->inventoryService->getProduct($productId);
$old_price = $vendor->products()->where('product_id', $productId)->first()?->pivot?->last_price;
$vendor->products()->detach($productId);

View File

@@ -4,8 +4,7 @@ namespace App\Modules\Procurement\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Core\Models\User;
class PurchaseOrder extends Model
{
@@ -14,19 +13,19 @@ class PurchaseOrder extends Model
use \Spatie\Activitylog\Traits\LogsActivity;
protected $fillable = [
'po_number',
'code',
'vendor_id',
'warehouse_id',
'user_id',
'order_date',
'expected_delivery_date',
'status',
'total_amount',
'notes',
'tax_amount',
'grand_total',
'remark',
];
protected $casts = [
'order_date' => 'date',
'expected_delivery_date' => 'date',
'total_amount' => 'decimal:2',
];
@@ -43,14 +42,13 @@ class PurchaseOrder extends Model
{
$snapshot = $activity->properties['snapshot'] ?? [];
$snapshot['po_number'] = $this->po_number;
$snapshot['po_number'] = $this->code;
if ($this->vendor) {
$snapshot['vendor_name'] = $this->vendor->name;
}
if ($this->warehouse) {
$snapshot['warehouse_name'] = $this->warehouse->name;
}
// Warehouse relation removed in Strict Mode. Snapshot should be set via manual hydration if needed,
// or during the procurement process where warehouse_id is known.
$activity->properties = $activity->properties->merge([
'snapshot' => $snapshot
@@ -62,15 +60,9 @@ class PurchaseOrder extends Model
return $this->belongsTo(Vendor::class);
}
public function warehouse(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Warehouse::class);
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class);
}
public function items(): \Illuminate\Database\Eloquent\Relations\HasMany
{

View File

@@ -37,8 +37,5 @@ class PurchaseOrderItem extends Model
return $this->belongsTo(PurchaseOrder::class);
}
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Product::class);
}
}

View File

@@ -16,18 +16,18 @@ class Vendor extends Model
protected $fillable = [
'code',
'name',
'contact_person',
'email',
'phone',
'address',
'short_name',
'tax_id',
'payment_terms',
'owner',
'contact_name',
'tel',
'phone',
'email',
'address',
'remark',
];
public function products(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Product::class)->withPivot('last_price')->withTimestamps();
}
public function purchaseOrders(): \Illuminate\Database\Eloquent\Relations\HasMany
{

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Modules\Procurement;
use Illuminate\Support\ServiceProvider;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Procurement\Services\ProcurementService;
class ProcurementServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(ProcurementServiceInterface::class, ProcurementService::class);
}
public function boot(): void
{
//
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Modules\Procurement\Services;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Procurement\Models\PurchaseOrder;
use Illuminate\Support\Collection;
class ProcurementService implements ProcurementServiceInterface
{
public function getPurchaseOrdersByDate(string $start, string $end, array $statuses = ['received', 'completed']): Collection
{
return PurchaseOrder::with(['vendor'])
->whereIn('status', $statuses)
->whereBetween('created_at', [$start . ' 00:00:00', $end . ' 23:59:59'])
->get();
}
public function getPurchaseOrdersByIds(array $ids, array $with = []): Collection
{
return PurchaseOrder::whereIn('id', $ids)->with($with)->get();
}
}

View File

@@ -4,11 +4,10 @@ namespace App\Modules\Production\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Product;
use App\Modules\Production\Models\ProductionOrder;
use App\Modules\Production\Models\ProductionOrderItem;
use App\Modules\Inventory\Models\Unit;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Core\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
@@ -16,20 +15,31 @@ use Inertia\Response;
class ProductionOrderController extends Controller
{
protected $inventoryService;
public function __construct(InventoryServiceInterface $inventoryService)
{
$this->inventoryService = $inventoryService;
}
/**
* 生產工單列表
*/
public function index(Request $request): Response
{
$query = ProductionOrder::with(['product', 'warehouse', 'user']);
// 不再使用 with(),避免跨模組 Eager Loading
$query = ProductionOrder::query();
// 搜尋
// 搜尋 (此處 orWhereHas 暫時保留,因 Laravel query builder 仍可作用於資料表層級,
// 但實務上若模組完全隔離,應考慮搜尋引擎或 ID 預選)
if ($request->filled('search')) {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('code', 'like', "%{$search}%")
->orWhere('output_batch_number', 'like', "%{$search}%")
->orWhereHas('product', fn($pq) => $pq->where('name', 'like', "%{$search}%"));
->orWhere('output_batch_number', 'like', "%{$search}%");
// 若要搜尋產品名稱,現在需先從 Inventory 查出 IDs
$productIds = \App\Modules\Inventory\Models\Product::where('name', 'like', "%{$search}%")->pluck('id');
$q->orWhereIn('product_id', $productIds);
});
}
@@ -38,19 +48,29 @@ class ProductionOrderController extends Controller
$query->where('status', $request->status);
}
// 排
$sortField = $request->input('sort_field', 'created_at');
$sortDirection = $request->input('sort_direction', 'desc');
$allowedSorts = ['id', 'code', 'production_date', 'output_quantity', 'created_at'];
if (!in_array($sortField, $allowedSorts)) {
$sortField = 'created_at';
}
$query->orderBy($sortField, $sortDirection);
// 排除軟刪除
$query->orderBy($request->input('sort_field', 'created_at'), $request->input('sort_direction', 'desc'));
// 分頁
$perPage = $request->input('per_page', 10);
$productionOrders = $query->paginate($perPage)->withQueryString();
// --- 手動資料水和 (Manual Hydration) ---
$productIds = $productionOrders->pluck('product_id')->unique()->filter()->toArray();
$warehouseIds = $productionOrders->pluck('warehouse_id')->unique()->filter()->toArray();
$userIds = $productionOrders->pluck('user_id')->unique()->filter()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
$warehouses = $this->inventoryService->getAllWarehouses()->whereIn('id', $warehouseIds)->keyBy('id');
$users = User::whereIn('id', $userIds)->get()->keyBy('id'); // Core 模組暫由 Model 直接獲取
$productionOrders->getCollection()->transform(function ($order) use ($products, $warehouses, $users) {
$order->product = $products->get($order->product_id);
$order->warehouse = $warehouses->get($order->warehouse_id);
$order->user = $users->get($order->user_id);
return $order;
});
return Inertia::render('Production/Index', [
'productionOrders' => $productionOrders,
'filters' => $request->only(['search', 'status', 'per_page', 'sort_field', 'sort_direction']),
@@ -63,9 +83,9 @@ class ProductionOrderController extends Controller
public function create(): Response
{
return Inertia::render('Production/Create', [
'products' => Product::with(['baseUnit'])->get(),
'warehouses' => Warehouse::all(),
'units' => Unit::all(),
'products' => $this->inventoryService->getAllProducts(),
'warehouses' => $this->inventoryService->getAllWarehouses(),
'units' => $this->inventoryService->getUnits(),
]);
}
@@ -74,56 +94,26 @@ class ProductionOrderController extends Controller
*/
public function store(Request $request)
{
$status = $request->input('status', 'draft'); // 預設為草稿
$status = $request->input('status', 'draft');
// 共用驗證規則
$baseRules = [
'product_id' => 'required|exists:products,id',
'product_id' => 'required',
'output_batch_number' => 'required|string|max:50',
'status' => 'nullable|in:draft,completed',
];
// 完成模式需要完整驗證
$completedRules = [
'warehouse_id' => 'required|exists:warehouses,id',
'warehouse_id' => 'required',
'output_quantity' => 'required|numeric|min:0.01',
'output_box_count' => 'nullable|string|max:10',
'production_date' => 'required|date',
'expiry_date' => 'nullable|date|after_or_equal:production_date',
'remark' => 'nullable|string',
'items' => 'required|array|min:1',
'items.*.inventory_id' => 'required|exists:inventories,id',
'items.*.inventory_id' => 'required',
'items.*.quantity_used' => 'required|numeric|min:0.0001',
'items.*.unit_id' => 'nullable|exists:units,id',
];
// 草稿模式的寬鬆規則
$draftRules = [
'warehouse_id' => 'nullable|exists:warehouses,id',
'output_quantity' => 'nullable|numeric|min:0',
'output_box_count' => 'nullable|string|max:10',
'production_date' => 'nullable|date',
'expiry_date' => 'nullable|date',
'remark' => 'nullable|string',
'items' => 'nullable|array',
'items.*.inventory_id' => 'nullable|exists:inventories,id',
'items.*.quantity_used' => 'nullable|numeric|min:0',
'items.*.unit_id' => 'nullable|exists:units,id',
];
$rules = $status === 'completed' ? array_merge($baseRules, $completedRules) : $baseRules;
$rules = $status === 'completed'
? array_merge($baseRules, $completedRules)
: array_merge($baseRules, $draftRules);
$validated = $request->validate($rules, [
'product_id.required' => '請選擇成品商品',
'output_batch_number.required' => '請輸入成品批號',
'warehouse_id.required' => '請選擇入庫倉庫',
'output_quantity.required' => '請輸入生產數量',
'production_date.required' => '請選擇生產日期',
'items.required' => '請至少新增一項原物料',
'items.min' => '請至少新增一項原物料',
]);
$validated = $request->validate($rules);
DB::transaction(function () use ($validated, $request, $status) {
// 1. 建立生產工單
@@ -133,20 +123,22 @@ class ProductionOrderController extends Controller
'warehouse_id' => $validated['warehouse_id'] ?? null,
'output_quantity' => $validated['output_quantity'] ?? 0,
'output_batch_number' => $validated['output_batch_number'],
'output_box_count' => $validated['output_box_count'] ?? null,
'output_box_count' => $request->output_box_count,
'production_date' => $validated['production_date'] ?? now()->toDateString(),
'expiry_date' => $validated['expiry_date'] ?? null,
'expiry_date' => $request->expiry_date,
'user_id' => auth()->id(),
'status' => $status,
'remark' => $validated['remark'] ?? null,
'remark' => $request->remark,
]);
// 2. 建立明細 (草稿與完成模式皆需儲存)
if (!empty($validated['items'])) {
foreach ($validated['items'] as $item) {
if (empty($item['inventory_id'])) continue;
activity()
->performedOn($productionOrder)
->causedBy(auth()->user())
->log('created');
// 建立明細
// 2. 處理明細
if (!empty($request->items)) {
foreach ($request->items as $item) {
ProductionOrderItem::create([
'production_order_id' => $productionOrder->id,
'inventory_id' => $item['inventory_id'],
@@ -154,52 +146,71 @@ class ProductionOrderController extends Controller
'unit_id' => $item['unit_id'] ?? null,
]);
// 若為完成模式,則扣減原物料庫存
if ($status === 'completed') {
$inventory = Inventory::findOrFail($item['inventory_id']);
$inventory->decrement('quantity', $item['quantity_used']);
$this->inventoryService->decreaseInventoryQuantity(
$item['inventory_id'],
$item['quantity_used'],
"生產單 #{$productionOrder->code} 耗料",
ProductionOrder::class,
$productionOrder->id
);
}
}
}
// 3. 若為完成模式,執行成品入庫
// 3. 成品入庫
if ($status === 'completed') {
$product = Product::findOrFail($validated['product_id']);
Inventory::create([
$this->inventoryService->createInventoryRecord([
'warehouse_id' => $validated['warehouse_id'],
'product_id' => $validated['product_id'],
'quantity' => $validated['output_quantity'],
'batch_number' => $validated['output_batch_number'],
'box_number' => $validated['output_box_count'],
'origin_country' => 'TW', // 生產預設為本地
'box_number' => $request->output_box_count,
'arrival_date' => $validated['production_date'],
'expiry_date' => $validated['expiry_date'] ?? null,
'quality_status' => 'normal',
'expiry_date' => $request->expiry_date,
'reason' => "生產單 #{$productionOrder->code} 成品入庫",
'reference_type' => ProductionOrder::class,
'reference_id' => $productionOrder->id,
]);
activity()
->performedOn($productionOrder)
->causedBy(auth()->user())
->log('completed');
}
});
$message = $status === 'completed'
? '生產單已建立,原物料已扣減,成品已入庫'
: '生產單草稿已儲存';
return redirect()->route('production-orders.index')
->with('success', $message);
->with('success', $status === 'completed' ? '生產單已完成並入庫' : '草稿已儲存');
}
/**
* 檢視生產單詳情(含追溯資訊)
* 檢視生產單詳情
*/
public function show(ProductionOrder $productionOrder): Response
{
$productionOrder->load([
'product.baseUnit',
'warehouse',
'user',
'items.inventory.product',
'items.inventory.sourcePurchaseOrder.vendor',
'items.unit',
]);
// 手動水和主表資料
$productionOrder->product = $this->inventoryService->getProduct($productionOrder->product_id);
if ($productionOrder->product) {
$productionOrder->product->base_unit = $this->inventoryService->getUnits()->where('id', $productionOrder->product->base_unit_id)->first();
}
$productionOrder->warehouse = $this->inventoryService->getWarehouse($productionOrder->warehouse_id);
$productionOrder->user = User::find($productionOrder->user_id);
// 手動水和明細資料
$items = $productionOrder->items;
$inventoryIds = $items->pluck('inventory_id')->unique()->filter()->toArray();
$inventories = $this->inventoryService->getInventoriesByIds(
$inventoryIds,
['product.baseUnit', 'sourcePurchaseOrder.vendor']
)->keyBy('id');
$units = $this->inventoryService->getUnits()->keyBy('id');
foreach ($items as $item) {
$item->inventory = $inventories->get($item->inventory_id);
$item->unit = $units->get($item->unit_id);
}
return Inertia::render('Production/Show', [
'productionOrder' => $productionOrder,
@@ -207,57 +218,67 @@ class ProductionOrderController extends Controller
}
/**
* 取得倉庫內可用庫存(供 BOM 選擇)
* 取得倉庫內可用庫存
*/
public function getWarehouseInventories(Warehouse $warehouse)
public function getWarehouseInventories($warehouseId)
{
$inventories = Inventory::with(['product.baseUnit', 'product.largeUnit'])
->where('warehouse_id', $warehouse->id)
->where('quantity', '>', 0)
->where('quality_status', 'normal')
->orderBy('arrival_date', 'asc') // FIFO舊的排前面
->get()
->map(function ($inv) {
return [
'id' => $inv->id,
'product_id' => $inv->product_id,
'product_name' => $inv->product->name,
'product_code' => $inv->product->code,
'batch_number' => $inv->batch_number,
'box_number' => $inv->box_number,
'quantity' => $inv->quantity,
'arrival_date' => $inv->arrival_date?->format('Y-m-d'),
'expiry_date' => $inv->expiry_date?->format('Y-m-d'),
'unit_name' => $inv->product->baseUnit?->name,
'base_unit_id' => $inv->product->base_unit_id,
'base_unit_name' => $inv->product->baseUnit?->name,
'large_unit_id' => $inv->product->large_unit_id,
'large_unit_name' => $inv->product->largeUnit?->name,
'conversion_rate' => $inv->product->conversion_rate,
];
});
$inventories = $this->inventoryService->getInventoriesByWarehouse($warehouseId);
$data = $inventories->map(function ($inv) {
return [
'id' => $inv->id,
'product_id' => $inv->product_id,
'product_name' => $inv->product->name ?? '未知商品',
'product_code' => $inv->product->code ?? '',
'batch_number' => $inv->batch_number,
'box_number' => $inv->box_number,
'quantity' => $inv->quantity,
'arrival_date' => $inv->arrival_date ? $inv->arrival_date->format('Y-m-d') : null,
'expiry_date' => $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
'unit_name' => $inv->product->baseUnit->name ?? '',
'base_unit_id' => $inv->product->base_unit_id ?? null,
'large_unit_id' => $inv->product->large_unit_id ?? null,
'conversion_rate' => $inv->product->conversion_rate ?? 1,
];
});
return response()->json($inventories);
return response()->json($data);
}
/**
* 編輯生產單(僅限草稿狀態)
* 編輯生產單
*/
public function edit(ProductionOrder $productionOrder): Response
{
// 只有草稿可以編輯
if ($productionOrder->status !== 'draft') {
return redirect()->route('production-orders.show', $productionOrder->id)
->with('error', '只有草稿狀態的生產單可以編輯');
}
$productionOrder->load(['product', 'warehouse', 'items.inventory.product', 'items.unit']);
// 基本水和
$productionOrder->product = $this->inventoryService->getProduct($productionOrder->product_id);
$productionOrder->warehouse = $this->inventoryService->getWarehouse($productionOrder->warehouse_id);
// 手動水和明細資料
$items = $productionOrder->items;
$inventoryIds = $items->pluck('inventory_id')->unique()->filter()->toArray();
$inventories = $this->inventoryService->getInventoriesByIds(
$inventoryIds,
['product.baseUnit']
)->keyBy('id');
$units = $this->inventoryService->getUnits()->keyBy('id');
foreach ($items as $item) {
$item->inventory = $inventories->get($item->inventory_id);
$item->unit = $units->get($item->unit_id);
}
return Inertia::render('Production/Edit', [
'productionOrder' => $productionOrder,
'products' => Product::with(['baseUnit'])->get(),
'warehouses' => Warehouse::all(),
'units' => Unit::all(),
'products' => $this->inventoryService->getAllProducts(),
'warehouses' => $this->inventoryService->getAllWarehouses(),
'units' => $this->inventoryService->getUnits(),
]);
}
@@ -266,85 +287,60 @@ class ProductionOrderController extends Controller
*/
public function update(Request $request, ProductionOrder $productionOrder)
{
// 只有草稿可以編輯
if ($productionOrder->status !== 'draft') {
return redirect()->route('production-orders.show', $productionOrder->id)
->with('error', '只有草稿狀態的生產單可以編輯');
return redirect()->route('production-orders.show', $productionOrder->id)
->with('error', '只有草稿可以修改');
}
$status = $request->input('status', 'draft');
// 共用驗證規則
// 基礎驗證規則
$baseRules = [
'product_id' => 'required|exists:products,id',
'output_batch_number' => 'required|string|max:50',
'status' => 'nullable|in:draft,completed',
'status' => 'required|in:draft,completed',
'remark' => 'nullable|string',
];
// 完成模式需要完整驗證
// 完工時的嚴格驗證規則
$completedRules = [
'warehouse_id' => 'required|exists:warehouses,id',
'output_quantity' => 'required|numeric|min:0.01',
'output_box_count' => 'nullable|string|max:10',
'production_date' => 'required|date',
'expiry_date' => 'nullable|date|after_or_equal:production_date',
'remark' => 'nullable|string',
'expiry_date' => 'nullable|date',
'items' => 'required|array|min:1',
'items.*.inventory_id' => 'required|exists:inventories,id',
'items.*.quantity_used' => 'required|numeric|min:0.0001',
'items.*.unit_id' => 'nullable|exists:units,id',
];
// 草稿模式的寬鬆規則
$draftRules = [
'warehouse_id' => 'nullable|exists:warehouses,id',
'output_quantity' => 'nullable|numeric|min:0',
'output_box_count' => 'nullable|string|max:10',
'production_date' => 'nullable|date',
'expiry_date' => 'nullable|date',
'remark' => 'nullable|string',
'items' => 'nullable|array',
'items.*.inventory_id' => 'nullable|exists:inventories,id',
'items.*.quantity_used' => 'nullable|numeric|min:0',
'items.*.unit_id' => 'nullable|exists:units,id',
];
// 若狀態切換為 completed需合併驗證規則
$rules = $status === 'completed' ? array_merge($baseRules, $completedRules) : $baseRules;
$validated = $request->validate($rules);
$rules = $status === 'completed'
? array_merge($baseRules, $completedRules)
: array_merge($baseRules, $draftRules);
$validated = $request->validate($rules, [
'product_id.required' => '請選擇成品商品',
'output_batch_number.required' => '請輸入成品批號',
'warehouse_id.required' => '請選擇入庫倉庫',
'output_quantity.required' => '請輸入生產數量',
'production_date.required' => '請選擇生產日期',
'items.required' => '請至少新增一項原物料',
'items.min' => '請至少新增一項原物料',
]);
DB::transaction(function () use ($validated, $status, $productionOrder) {
// 更新生產工單基本資料
DB::transaction(function () use ($validated, $request, $status, $productionOrder) {
$productionOrder->update([
'product_id' => $validated['product_id'],
'warehouse_id' => $validated['warehouse_id'] ?? null,
'warehouse_id' => $validated['warehouse_id'] ?? $productionOrder->warehouse_id,
'output_quantity' => $validated['output_quantity'] ?? 0,
'output_batch_number' => $validated['output_batch_number'],
'output_box_count' => $validated['output_box_count'] ?? null,
'output_box_count' => $request->output_box_count,
'production_date' => $validated['production_date'] ?? now()->toDateString(),
'expiry_date' => $validated['expiry_date'] ?? null,
'expiry_date' => $request->expiry_date,
'status' => $status,
'remark' => $validated['remark'] ?? null,
'remark' => $request->remark,
]);
// 刪除舊的明細
activity()
->performedOn($productionOrder)
->causedBy(auth()->user())
->log('updated');
// 重新建立明細
$productionOrder->items()->delete();
// 重新建立明細 (草稿與完成模式皆需儲存)
if (!empty($validated['items'])) {
foreach ($validated['items'] as $item) {
if (empty($item['inventory_id'])) continue;
if (!empty($request->items)) {
foreach ($request->items as $item) {
ProductionOrderItem::create([
'production_order_id' => $productionOrder->id,
'inventory_id' => $item['inventory_id'],
@@ -352,35 +348,63 @@ class ProductionOrderController extends Controller
'unit_id' => $item['unit_id'] ?? null,
]);
// 若為完成模式,則扣減原物料庫存
if ($status === 'completed') {
$inventory = Inventory::findOrFail($item['inventory_id']);
$inventory->decrement('quantity', $item['quantity_used']);
$this->inventoryService->decreaseInventoryQuantity(
$item['inventory_id'],
$item['quantity_used'],
"生產單 #{$productionOrder->code} 耗料",
ProductionOrder::class,
$productionOrder->id
);
}
}
}
// 若為完成模式,執行成品入庫
if ($status === 'completed') {
Inventory::create([
$this->inventoryService->createInventoryRecord([
'warehouse_id' => $validated['warehouse_id'],
'product_id' => $validated['product_id'],
'quantity' => $validated['output_quantity'],
'batch_number' => $validated['output_batch_number'],
'box_number' => $validated['output_box_count'],
'origin_country' => 'TW',
'box_number' => $request->output_box_count,
'arrival_date' => $validated['production_date'],
'expiry_date' => $validated['expiry_date'] ?? null,
'quality_status' => 'normal',
'expiry_date' => $request->expiry_date,
'reason' => "生產單 #{$productionOrder->code} 成品入庫",
'reference_type' => ProductionOrder::class,
'reference_id' => $productionOrder->id,
]);
activity()
->performedOn($productionOrder)
->causedBy(auth()->user())
->log('completed');
}
});
$message = $status === 'completed'
? '生產單已完成,原物料已扣減,成品已入庫'
: '生產單草稿已更新';
return redirect()->route('production-orders.index')
->with('success', $message);
->with('success', '生產單已更新');
}
/**
* 刪除生產單
*/
public function destroy(ProductionOrder $productionOrder)
{
if ($productionOrder->status === 'completed') {
return redirect()->back()->with('error', '已完工的生產單無法刪除');
}
DB::transaction(function () use ($productionOrder) {
// 紀錄刪除動作 (需在刪除前或使用軟刪除)
activity()
->performedOn($productionOrder)
->causedBy(auth()->user())
->log('deleted');
$productionOrder->items()->delete();
$productionOrder->delete();
});
return redirect()->route('production-orders.index')->with('success', '生產單已刪除');
}
}

View File

@@ -0,0 +1,191 @@
<?php
namespace App\Modules\Production\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Production\Models\Recipe;
use App\Modules\Production\Models\RecipeItem;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Inertia\Response;
class RecipeController extends Controller
{
protected $inventoryService;
public function __construct(InventoryServiceInterface $inventoryService)
{
$this->inventoryService = $inventoryService;
}
/**
* 配方列表
*/
public function index(Request $request): Response
{
$query = Recipe::query();
if ($request->filled('search')) {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('code', 'like', "%{$search}%")
->orWhere('name', 'like', "%{$search}%");
$productIds = $this->inventoryService->getProductsByName($search)->pluck('id');
$q->orWhereIn('product_id', $productIds);
});
}
$query->orderBy($request->input('sort_field', 'created_at'), $request->input('sort_direction', 'desc'));
$recipes = $query->paginate($request->input('per_page', 10))->withQueryString();
// Manual Hydration
$productIds = $recipes->pluck('product_id')->unique()->filter()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
$recipes->getCollection()->transform(function ($recipe) use ($products) {
$recipe->product = $products->get($recipe->product_id);
return $recipe;
});
return Inertia::render('Production/Recipe/Index', [
'recipes' => $recipes,
'filters' => $request->only(['search', 'per_page', 'sort_field', 'sort_direction']),
]);
}
/**
* 新增配方表單
*/
public function create(): Response
{
return Inertia::render('Production/Recipe/Create', [
'products' => $this->inventoryService->getAllProducts(),
'units' => $this->inventoryService->getUnits(),
]);
}
/**
* 儲存配方
*/
public function store(Request $request)
{
$validated = $request->validate([
'product_id' => 'required|exists:products,id',
'code' => 'required|string|max:50|unique:recipes,code',
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'yield_quantity' => 'required|numeric|min:0.01',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.0001',
'items.*.unit_id' => 'nullable|exists:units,id',
'items.*.remark' => 'nullable|string',
]);
DB::transaction(function () use ($validated) {
$recipe = Recipe::create([
'product_id' => $validated['product_id'],
'code' => $validated['code'],
'name' => $validated['name'],
'description' => $validated['description'],
'yield_quantity' => $validated['yield_quantity'],
'is_active' => true,
]);
foreach ($validated['items'] as $item) {
RecipeItem::create([
'recipe_id' => $recipe->id,
'product_id' => $item['product_id'],
'quantity' => $item['quantity'],
'unit_id' => $item['unit_id'],
'remark' => $item['remark'],
]);
}
});
return redirect()->route('recipes.index')->with('success', '配方已建立');
}
/**
* 編輯配方表單
*/
public function edit(Recipe $recipe): Response
{
// Hydrate Product
$recipe->product = $this->inventoryService->getProduct($recipe->product_id);
// Load items with details
$items = $recipe->items;
$productIds = $items->pluck('product_id')->unique()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
$units = $this->inventoryService->getUnits()->keyBy('id');
foreach ($items as $item) {
$item->product = $products->get($item->product_id);
$item->unit = $units->get($item->unit_id);
}
return Inertia::render('Production/Recipe/Edit', [
'recipe' => $recipe,
'products' => $this->inventoryService->getAllProducts(),
'units' => $this->inventoryService->getUnits(),
]);
}
/**
* 更新配方
*/
public function update(Request $request, Recipe $recipe)
{
$validated = $request->validate([
'product_id' => 'required|exists:products,id',
'code' => 'required|string|max:50|unique:recipes,code,' . $recipe->id,
'name' => 'required|string|max:255',
'description' => 'nullable|string',
'yield_quantity' => 'required|numeric|min:0.01',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.0001',
'items.*.unit_id' => 'nullable|exists:units,id',
'items.*.remark' => 'nullable|string',
]);
DB::transaction(function () use ($validated, $recipe) {
$recipe->update([
'product_id' => $validated['product_id'],
'code' => $validated['code'],
'name' => $validated['name'],
'description' => $validated['description'],
'yield_quantity' => $validated['yield_quantity'],
]);
// Sync items (Delete all and recreate)
$recipe->items()->delete();
foreach ($validated['items'] as $item) {
RecipeItem::create([
'recipe_id' => $recipe->id,
'product_id' => $item['product_id'],
'quantity' => $item['quantity'],
'unit_id' => $item['unit_id'],
'remark' => $item['remark'],
]);
}
});
return redirect()->route('recipes.index')->with('success', '配方已更新');
}
/**
* 刪除配方
*/
public function destroy(Recipe $recipe)
{
$recipe->delete();
return redirect()->back()->with('success', '配方已刪除');
}
}

View File

@@ -4,14 +4,12 @@ namespace App\Modules\Production\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Core\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class ProductionOrder extends Model
{
/** @use HasFactory<\Database\Factories\ProductionOrderFactory> */
use HasFactory;
use HasFactory, LogsActivity;
protected $fillable = [
'code',
@@ -27,6 +25,38 @@ class ProductionOrder extends Model
'remark',
];
protected $casts = [
'production_date' => 'date',
'expiry_date' => 'date',
'output_quantity' => 'decimal:2',
];
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly([
'code',
'status',
'output_quantity',
'output_batch_number',
'production_date',
'remark'
])
->logOnlyDirty()
->dontSubmitEmptyLogs()
->setDescriptionForEvent(fn(string $eventName) => "生產工單已{$this->getEventDescription($eventName)}");
}
protected function getEventDescription($eventName): string
{
return match ($eventName) {
'created' => '建立',
'updated' => '更新',
'deleted' => '刪除',
default => $eventName,
};
}
public static function generateCode()
{
$prefix = 'PO' . now()->format('Ymd');
@@ -40,27 +70,28 @@ class ProductionOrder extends Model
return $prefix . $sequence;
}
protected $casts = [
'order_date' => 'date',
'start_date' => 'datetime',
'completion_date' => 'datetime',
'quantity' => 'decimal:2',
'produced_quantity' => 'decimal:2',
];
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
/**
* @deprecated 使用 InventoryServiceInterface 獲取產品資訊
*/
public function product()
{
return $this->belongsTo(Product::class);
return null;
}
public function warehouse(): \Illuminate\Database\Eloquent\Relations\BelongsTo
/**
* @deprecated 使用 InventoryServiceInterface 獲取倉庫資訊
*/
public function warehouse()
{
return $this->belongsTo(Warehouse::class);
return null;
}
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
/**
* @deprecated 使用 CoreServiceInterface 獲取使用者資訊
*/
public function user()
{
return $this->belongsTo(User::class);
return null;
}
public function items(): \Illuminate\Database\Eloquent\Relations\HasMany

View File

@@ -22,14 +22,20 @@ class ProductionOrderItem extends Model
'quantity_used' => 'decimal:4',
];
/**
* @deprecated 使用 InventoryServiceInterface 獲取庫存資訊
*/
public function inventory()
{
return $this->belongsTo(\App\Modules\Inventory\Models\Inventory::class);
return null;
}
/**
* @deprecated
*/
public function unit()
{
return $this->belongsTo(\App\Modules\Inventory\Models\Unit::class);
return null;
}
public function productionOrder(): \Illuminate\Database\Eloquent\Relations\BelongsTo
@@ -37,8 +43,11 @@ class ProductionOrderItem extends Model
return $this->belongsTo(ProductionOrder::class);
}
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
/**
* @deprecated 使用 InventoryServiceInterface 獲取產品資訊
*/
public function product()
{
return $this->belongsTo(Product::class);
return null;
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Modules\Production\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Recipe extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'product_id',
'code',
'name',
'description',
'yield_quantity',
'is_active',
];
protected $casts = [
'yield_quantity' => 'decimal:2',
'is_active' => 'boolean',
];
public function items()
{
return $this->hasMany(RecipeItem::class);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Modules\Production\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RecipeItem extends Model
{
use HasFactory;
protected $fillable = [
'recipe_id',
'product_id',
'quantity',
'unit_id',
'remark',
];
protected $casts = [
'quantity' => 'decimal:4',
];
public function recipe()
{
return $this->belongsTo(Recipe::class);
}
}

View File

@@ -2,8 +2,12 @@
use Illuminate\Support\Facades\Route;
use App\Modules\Production\Controllers\ProductionOrderController;
use App\Modules\Production\Controllers\RecipeController;
Route::middleware('auth')->group(function () {
// 配方管理
Route::resource('recipes', RecipeController::class);
// 生產管理
Route::middleware('permission:production_orders.view')->group(function () {
Route::get('/production-orders', [ProductionOrderController::class, 'index'])->name('production-orders.index');

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Modules\Shared\Contracts;
/**
* Base Service Interface
* 所有模組的 Service 都應繼承此介面 (若有通用方法)
*/
interface ServiceInterface
{
// Future common methods
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Modules\Shared;
use Illuminate\Support\ServiceProvider;
class SharedServiceProvider extends ServiceProvider
{
public function register(): void
{
// Register shared services or repositories here
}
public function boot(): void
{
//
}
}

View File

@@ -28,12 +28,20 @@ class ModuleServiceProvider extends ServiceProvider
foreach ($modules as $module) {
// $moduleName = basename($module);
// Load Routes
$routesPath = $module . '/Routes/web.php';
if (File::exists($routesPath)) {
Route::middleware('web')
->group($routesPath);
}
// Load Service Provider
$moduleName = basename($module);
$providerClass = "App\\Modules\\{$moduleName}\\{$moduleName}ServiceProvider";
if (class_exists($providerClass)) {
$this->app->register($providerClass);
}
}
}
}