Compare commits
65 Commits
5668e17e61
...
demo
| Author | SHA1 | Date | |
|---|---|---|---|
| 076a71f3db | |||
| f96d2870c3 | |||
| 96440f6b50 | |||
| 1cb27576c2 | |||
| 8f6b8d55cc | |||
| 60f5f00a9e | |||
| a2b2a90f51 | |||
| 0b4aeacb55 | |||
| dfd0a047f2 | |||
| e3ceedc579 | |||
| f2e5ef121e | |||
| 7a1fc02dfc | |||
| 5f34855233 | |||
| bee8ecb55b | |||
| 0198b7de90 | |||
| b57a4feeab | |||
| 55a806051d | |||
| 6ca0bafd60 | |||
| adf13410ba | |||
| d52a215916 | |||
| 197df3bec4 | |||
| 2437aa2672 | |||
| a987f4345e | |||
| 89291918fd | |||
| 3f7a625191 | |||
| e11193c2a7 | |||
| 02e5f5d4ea | |||
| 36b90370a8 | |||
| 5290dd2cbe | |||
| 8e0252e8fc | |||
| 2fd5de96b2 | |||
| 951005c616 | |||
| ee0bacafc2 | |||
| 93390aad80 | |||
| 6b6e840f35 | |||
| e4c83ebd6d | |||
| d9edc603c7 | |||
| dda92393d2 | |||
| 47deab9804 | |||
| e921810f70 | |||
| 07b7d9b327 | |||
| 016366407c | |||
| ba50905626 | |||
| f4ed358393 | |||
| 7c395c89b5 | |||
| ed264b031a | |||
| dd2e63c08b | |||
| b498fe93ff | |||
| 6b324b4bd0 | |||
| a898873211 | |||
| f543b98d0f | |||
| 183583c739 | |||
| 58bd995cd8 | |||
| 036f4a4fb6 | |||
| 0a955fb993 | |||
| 7dac2d1f77 | |||
| 649af40919 | |||
| 5f8b2a1c2d | |||
| 4bbbde685d | |||
| 5e32526471 | |||
| f960aaaeb2 | |||
| 63e4f88a14 | |||
| e3df090afd | |||
| 878b90e2ad | |||
| 299cf37054 |
@@ -1,115 +0,0 @@
|
|||||||
---
|
|
||||||
trigger: always_on
|
|
||||||
---
|
|
||||||
|
|
||||||
---
|
|
||||||
name: 操作紀錄實作規範
|
|
||||||
description: 規範系統內 Activity Log 的實作標準,包含自動名稱解析、複雜單據合併記錄、與前端顯示優化。
|
|
||||||
---
|
|
||||||
|
|
||||||
# 操作紀錄實作規範 (Activity Logging Skill)
|
|
||||||
|
|
||||||
本文件定義了 Star ERP 系統中操作紀錄的最高實作標準,旨在確保每筆日誌都具有「高度可讀性」與「單一性」。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 1. 後端實作核心 (Backend)
|
|
||||||
|
|
||||||
### 1.1 全域 ID 轉名稱邏輯 (Global ID Resolution)
|
|
||||||
為了讓管理者能直覺看懂日誌,所有的 ID(如 `warehouse_id`, `created_by`)在記錄時都應自動解析為名稱。此邏輯應統一在 Model 的 `tapActivity` 中實作。
|
|
||||||
|
|
||||||
#### 關鍵實作參考:
|
|
||||||
```php
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
||||||
{
|
|
||||||
// 🚩 核心:轉換為陣列以避免 Indirect modification error
|
|
||||||
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
|
||||||
? $activity->properties->toArray()
|
|
||||||
: $activity->properties;
|
|
||||||
|
|
||||||
// 1. Snapshot 快照:用於主描述的上下文(例如:單號、名稱)
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
|
||||||
$snapshot['doc_no'] = $this->doc_no;
|
|
||||||
$snapshot['warehouse_name'] = $this->warehouse?->name;
|
|
||||||
$properties['snapshot'] = $snapshot;
|
|
||||||
|
|
||||||
// 2. 名稱解析:自動將 attributes 與 old 中的 ID 換成人名/物名
|
|
||||||
$resolver = function (&$data) {
|
|
||||||
if (empty($data) || !is_array($data)) return;
|
|
||||||
|
|
||||||
// 使用者 ID 轉換
|
|
||||||
foreach (['created_by', 'updated_by', 'completed_by'] as $f) {
|
|
||||||
if (isset($data[$f]) && is_numeric($data[$f])) {
|
|
||||||
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 倉庫 ID 轉換
|
|
||||||
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
|
||||||
$data['warehouse_id'] = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id'])?->name;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
|
||||||
if (isset($properties['old'])) $resolver($properties['old']);
|
|
||||||
|
|
||||||
$activity->properties = $properties;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 1.2 複雜操作的日誌合併 (Log Consolidation)
|
|
||||||
當一個操作同時涉及「多個品項異動」與「單據狀態變更」時,**嚴禁**產生多筆重複日誌。
|
|
||||||
|
|
||||||
* **策略**:在 Service 層手動發送主日誌,並使用 `saveQuietly()` 更新單據屬性以抑止 Trait 的自動日誌。
|
|
||||||
* **格式**:主日誌應包含 `items_diff` (品項差異) 與 `attributes/old` (單據狀態變更)。
|
|
||||||
|
|
||||||
```php
|
|
||||||
// Service 中的實作方式
|
|
||||||
DB::transaction(function () use ($doc, $items) {
|
|
||||||
// 1. 更新品項 (記錄變動細節)
|
|
||||||
$updatedItems = $this->getUpdatedItems($doc, $items);
|
|
||||||
|
|
||||||
// 2. 靜默更新單據狀態 (避免 Trait 產生冗餘日誌)
|
|
||||||
$doc->status = 'completed';
|
|
||||||
$doc->saveQuietly();
|
|
||||||
|
|
||||||
// 3. 手動觸發單一合併日誌
|
|
||||||
activity()
|
|
||||||
->performedOn($doc)
|
|
||||||
->withProperties([
|
|
||||||
'items_diff' => ['updated' => $updatedItems],
|
|
||||||
'attributes' => ['status' => 'completed'],
|
|
||||||
'old' => ['status' => 'counting']
|
|
||||||
])
|
|
||||||
->log('updated');
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 2. 前端介面規範 (Frontend)
|
|
||||||
|
|
||||||
### 2.1 標籤命名規範 (Field Labels)
|
|
||||||
前端顯示應完全移除「ID」字眼,提供最友善的閱讀體驗。
|
|
||||||
|
|
||||||
**檔案位置**: `resources/js/Components/ActivityLog/ActivityDetailDialog.tsx`
|
|
||||||
```typescript
|
|
||||||
const fieldLabels: Record<string, string> = {
|
|
||||||
warehouse_id: '倉庫', // ❌ 禁用「倉庫 ID」
|
|
||||||
created_by: '建立者', // ❌ 禁用「建立者 ID」
|
|
||||||
completed_by: '完成者',
|
|
||||||
status: '狀態',
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2.2 特殊結構顯示
|
|
||||||
* **品項異動**:前端應能渲染 `items_diff` 結構,以「品項名稱 + 數值變動」的方式呈現表格(已在 `ActivityDetailDialog` 實作)。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 3. 開發檢核清單 (Checklist)
|
|
||||||
|
|
||||||
- [ ] **Model**: `tapActivity` 是否已處理 Collection 快照?
|
|
||||||
- [ ] **Model**: 是否已實作全域 ID 至名稱的自動解析?
|
|
||||||
- [ ] **Service**: 是否使用 `saveQuietly()` 避免產生重複的「單據已更新」日誌?
|
|
||||||
- [ ] **UI**: `fieldLabels` 是否已移除所有「ID」字樣?
|
|
||||||
- [ ] **UI**: 若有品項異動,是否已正確格式化傳入 `items_diff`?
|
|
||||||
@@ -62,6 +62,10 @@ trigger: always_on
|
|||||||
* 生成 React 組件時,必須符合專案現有的 Tailwind CSS 配置。
|
* 生成 React 組件時,必須符合專案現有的 Tailwind CSS 配置。
|
||||||
* 必須考慮 ERP 邏輯(例如:權限判斷、操作日誌、資料完整性)。
|
* 必須考慮 ERP 邏輯(例如:權限判斷、操作日誌、資料完整性)。
|
||||||
* 新增功能時,請先判斷應歸屬於哪個 Module,並建立在 `app/Modules/` 對應目錄下。
|
* 新增功能時,請先判斷應歸屬於哪個 Module,並建立在 `app/Modules/` 對應目錄下。
|
||||||
|
* **核心要求:UI 規範與彈性設計 (重要)**:
|
||||||
|
* 在開發「新功能」或「新頁面」前,產出的 `implementation_plan.md` 中**必須包含「UI 規範核對清單」**,明確列出將使用哪些已定義於 `ui-consistency/SKILL.md` 的元件(例如:`AlertDialog`、特定圖示名稱等)。
|
||||||
|
* **已規範部分**:絕對遵循《客戶端後台 UI 統一規範》進行實作。
|
||||||
|
* **未規範部分**:若遇到規範外的新 UI 區塊,請保有設計彈性,運用 Tailwind 打造符合 ERP 調性的初版設計,依據使用者的實際感受進行後續調整與收錄。
|
||||||
|
|
||||||
## 8. 多租戶開發規範 (Multi-tenancy Standards)
|
## 8. 多租戶開發規範 (Multi-tenancy Standards)
|
||||||
本專案採用多租戶隔離架構,開發時必須遵守以下資料同步規則:
|
本專案採用多租戶隔離架構,開發時必須遵守以下資料同步規則:
|
||||||
@@ -80,8 +84,17 @@ trigger: always_on
|
|||||||
* **執行 Composer**: `./vendor/bin/sail composer install`
|
* **執行 Composer**: `./vendor/bin/sail composer install`
|
||||||
* **執行 Node/NPM**: `./vendor/bin/sail npm run dev`
|
* **執行 Node/NPM**: `./vendor/bin/sail npm run dev`
|
||||||
|
|
||||||
## 10. 日期處理 (Date Handling)
|
## 10. 部署與查修環境 (CI/CD & Troubleshooting)
|
||||||
|
* **自動化部署**:本專案使用 CI/CD 自動化部署,開發者只需 push 程式碼至對應分支即可。
|
||||||
|
* **Demo 環境 (對應 `demo` 分支)**:若需查修測試站問題(例如查看 Error Log 或資料庫),請連線 `ssh gitea_work`。
|
||||||
|
* **Production 環境 (對應 `main` 分支)**:若需查修正式站問題,請連線 `ssh erp`。
|
||||||
|
|
||||||
- 前端顯示日期時預設使用 `resources/js/lib/date.ts` 提供的 `formatDate` 工具。
|
## 11. 瀏覽器測試規範 (Browser Testing)
|
||||||
- 避免直接顯示原始 ISO 字串(如 `...Z` 結尾的格式)。
|
當需要進行瀏覽器自動化測試或手動驗證時,請遵守以下連線資訊:
|
||||||
- **智慧格式切換**:`formatDate` 會自動判斷原始資料,若時間部分為 `00:00:00` 則僅顯示 `YYYY-MM-DD`,否則顯示 `YYYY-MM-DD HH:mm:ss`。
|
|
||||||
|
* **本地測試網址**:`http://localhost:8081/`
|
||||||
|
* **預設管理員帳號**:`admin`
|
||||||
|
* **預設管理員密碼**:`password`
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> 在執行 browser subagent 或進行 E2E 測試時,請務必確認為 `8081` Port,以避免連線至錯誤的服務環境。
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
---
|
|
||||||
trigger: always_on
|
|
||||||
---
|
|
||||||
|
|
||||||
---
|
|
||||||
name: 權限管理與實作規範
|
|
||||||
description: 為新功能實作權限控制的完整流程規範,包含後端 Seeder 設定、Middleware 路由保護與前端權限判斷。
|
|
||||||
---
|
|
||||||
|
|
||||||
# 權限管理與實作規範
|
|
||||||
|
|
||||||
本文件說明如何在新增功能時,一併實作完整的權限控制機制。專案採用 `spatie/laravel-permission` 套件進行權限管理。
|
|
||||||
|
|
||||||
## 1. 定義權限 (Backend)
|
|
||||||
|
|
||||||
所有權限皆定義於 `database/seeders/PermissionSeeder.php`。
|
|
||||||
|
|
||||||
### 步驟:
|
|
||||||
|
|
||||||
1. 開啟 `database/seeders/PermissionSeeder.php`。
|
|
||||||
2. 在 `$permissions` 陣列中新增功能對應的權限字串。
|
|
||||||
* **命名慣例**:`{resource}.{action}` (例如:`system.view_logs`, `products.create`)
|
|
||||||
* 常用動作:`view`, `create`, `edit`, `delete`, `publish`, `export`
|
|
||||||
3. 在下方「角色分配」區段,將新權限分配給適合的角色。
|
|
||||||
* `super-admin`:通常擁有所有權限(程式碼中 `Permission::all()` 自動涵蓋,無需手動新增)。
|
|
||||||
* `admin`:通常擁有大部分權限。
|
|
||||||
* 其他角色 (`warehouse-manager`, `purchaser`, `viewer`):依業務邏輯分配。
|
|
||||||
|
|
||||||
### 範例:
|
|
||||||
|
|
||||||
```php
|
|
||||||
// 1. 新增權限字串
|
|
||||||
$permissions = [
|
|
||||||
// ... 現有權限
|
|
||||||
'system.view_logs', // 新增:檢視系統日誌
|
|
||||||
];
|
|
||||||
|
|
||||||
// ...
|
|
||||||
|
|
||||||
// 2. 分配給角色
|
|
||||||
$admin->givePermissionTo([
|
|
||||||
// ... 現有權限
|
|
||||||
'system.view_logs',
|
|
||||||
]);
|
|
||||||
```
|
|
||||||
|
|
||||||
## 2. 套用資料庫變更
|
|
||||||
|
|
||||||
修改 Seeder 後,必須重新執行 Seeder 以將權限寫入資料庫。
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 對於所有租戶執行 Seeder (開發環境)
|
|
||||||
php artisan tenants:seed --class=PermissionSeeder
|
|
||||||
```
|
|
||||||
|
|
||||||
## 3. 路由保護 (Backend Middleware)
|
|
||||||
|
|
||||||
在 `routes/web.php` 中,使用 `permission:{name}` middleware 保護路由。
|
|
||||||
|
|
||||||
### 範例:
|
|
||||||
|
|
||||||
```php
|
|
||||||
// 單一權限保護
|
|
||||||
Route::get('/logs', [LogController::class, 'index'])
|
|
||||||
->middleware('permission:system.view_logs')
|
|
||||||
->name('logs.index');
|
|
||||||
|
|
||||||
// 路由群組保護
|
|
||||||
Route::middleware('permission:products.view')->group(function () {
|
|
||||||
// ...
|
|
||||||
});
|
|
||||||
|
|
||||||
// 多重權限 (OR 邏輯:有其一即可)
|
|
||||||
Route::middleware('permission:products.create|products.edit')->group(function () {
|
|
||||||
// ...
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## 4. 前端權限判斷 (React Component)
|
|
||||||
|
|
||||||
使用自訂 Hook `usePermission` 來控制 UI 元素的顯示(例如:隱藏沒有權限的按鈕)。
|
|
||||||
|
|
||||||
### 引入 Hook:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { usePermission } from "@/hooks/usePermission";
|
|
||||||
```
|
|
||||||
|
|
||||||
### 使用方式:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
export default function ProductIndex() {
|
|
||||||
const { can } = usePermission();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h1>商品列表</h1>
|
|
||||||
|
|
||||||
{/* 只有擁有 create 權限才顯示按鈕 */}
|
|
||||||
{can('products.create') && (
|
|
||||||
<Button>新增商品</Button>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* 組合判斷 */}
|
|
||||||
{can('products.edit') && <EditButton />}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 權限 Hook 介面說明:
|
|
||||||
|
|
||||||
- `can(permission: string)`: 檢查當前使用者是否擁有指定權限。
|
|
||||||
- `canAny(permissions: string[])`: 檢查當前使用者是否擁有陣列中**任一**權限。
|
|
||||||
- `hasRole(role: string)`: 檢查當前使用者是否擁有指定角色。
|
|
||||||
|
|
||||||
## 5. 配置權限群組名稱 (Backend UI Config)
|
|
||||||
|
|
||||||
為了讓新權限在「角色與權限」管理介面中顯示正確的中文分組標題,需修改 Controller 設定。
|
|
||||||
|
|
||||||
### 步驟:
|
|
||||||
|
|
||||||
1. 開啟 `app/Http/Controllers/Admin/RoleController.php`。
|
|
||||||
2. 找到 `getGroupedPermissions` 方法。
|
|
||||||
3. 在 `$groupDefinitions` 陣列中,新增 `{resource}` 對應的中文名稱。
|
|
||||||
|
|
||||||
### 範例:
|
|
||||||
|
|
||||||
```php
|
|
||||||
$groupDefinitions = [
|
|
||||||
'products' => '商品資料管理',
|
|
||||||
// ...
|
|
||||||
'utility_fees' => '公共事業費管理', // 新增此行
|
|
||||||
];
|
|
||||||
```
|
|
||||||
|
|
||||||
## 檢核清單
|
|
||||||
|
|
||||||
- [ ] `PermissionSeeder.php` 已新增權限字串。
|
|
||||||
- [ ] `PermissionSeeder.php` 已將新權限分配給對應角色。
|
|
||||||
- [ ] 已執行 `php artisan tenants:seed --class=PermissionSeeder` 更新資料庫。
|
|
||||||
- [ ] `RoleController.php` 已新增權限群組的中文名稱映射。
|
|
||||||
- [ ] 後端路由 (`routes/web.php`) 已加上 middleware 保護。
|
|
||||||
- [ ] 前端頁面/按鈕已使用 `usePermission` 進行顯示控制。
|
|
||||||
57
.agents/rules/skill-trigger.md
Normal file
57
.agents/rules/skill-trigger.md
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
---
|
||||||
|
trigger: always_on
|
||||||
|
---
|
||||||
|
|
||||||
|
# 技能觸發規範 (Skill Trigger Rules)
|
||||||
|
|
||||||
|
本文件確保 AI 助手在對話中能**主動辨識**需要參照技能 (Skill) 的時機。
|
||||||
|
Skills 位於 `.agents/skills/`,採漸進式揭露以節省 Token。
|
||||||
|
**若對話內容命中以下任一觸發條件,必須先使用 `view_file` 讀取對應的 `SKILL.md` 後再進行作業。**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 觸發對照表
|
||||||
|
|
||||||
|
| 觸發詞 / 情境 | 對應 Skill | 路徑 |
|
||||||
|
|---|---|---|
|
||||||
|
| 操作紀錄、Activity Log、日誌、`tapActivity`、`LogsActivity`、`saveQuietly`、`activity()`、`items_diff` | **操作紀錄實作規範** | `.agents/skills/activity-logging/SKILL.md` |
|
||||||
|
| 權限、permission、角色、role、`usePermission`、`<Can>`、`PermissionSeeder`、middleware protection | **權限管理與實作規範** | `.agents/skills/permission-management/SKILL.md` |
|
||||||
|
| 跨模組、Service Interface、`Contracts`、模組間通訊、`ServiceProvider` 綁定、禁止跨模組引用 | **跨模組調用與通訊規範** | `.agents/skills/cross-module-communication/SKILL.md` |
|
||||||
|
| 按鈕樣式、表格規範、圖標、分頁、Badge、Toast、表單、UI 統一、頁面佈局、`button-filled-*`、`button-outlined-*`、`lucide-react`、色彩系統 | **客戶端後台 UI 統一規範** | `.agents/skills/ui-consistency/SKILL.md` |
|
||||||
|
| Git 分支、commit、push、合併、部署、`feature/`、`hotfix/`、`develop`、`main` | **Git 分支管理與開發規範** | `.agents/skills/git-workflows/SKILL.md` |
|
||||||
|
| E2E、端到端測試、Playwright、`spec.ts`、功能驗證、自動化測試、回歸測試 | **E2E 端到端測試規範** | `.agents/skills/e2e-testing/SKILL.md` |
|
||||||
|
| 查詢、撈資料、Query、Controller、下拉選單、Eloquent、N+1、`->get()`、select、交易、Transaction、Bulk、分頁、索引 | **資料庫與 ORM 最佳實踐規範** | `/home/mama/.gemini/antigravity/global_skills/database-best-practices/SKILL.md` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 強制觸發場景
|
||||||
|
|
||||||
|
以下場景**無論對話中是否出現觸發詞**,都必須主動載入對應 Skill:
|
||||||
|
|
||||||
|
### 🔴 新增功能或頁面時
|
||||||
|
必須同時讀取:
|
||||||
|
1. **permission-management** — 設定權限
|
||||||
|
2. **ui-consistency** — 遵循 UI 規範
|
||||||
|
3. **activity-logging** — 若涉及 Model CRUD,需加上操作紀錄
|
||||||
|
4. **e2e-testing** — 確認是否需要新增對應的 E2E 測試
|
||||||
|
|
||||||
|
### 🔴 新增或修改 Model 時
|
||||||
|
必須讀取:
|
||||||
|
1. **activity-logging** — `tapActivity` 實作
|
||||||
|
2. **cross-module-communication** — 確認是否涉及跨模組引用
|
||||||
|
|
||||||
|
### 🔴 Git 操作時
|
||||||
|
必須讀取:
|
||||||
|
1. **git-workflows** — 分支命名與 commit 格式
|
||||||
|
|
||||||
|
### 🔴 新增或修改 API 與 Controller 撈取資料庫邏輯時
|
||||||
|
必須讀取:
|
||||||
|
1. **database-best-practices** — 確認查詢優化、交易安全、批量寫入與索引規範
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 注意事項
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> 即使你「記得」Skill 的大致內容,仍必須重新讀取 `SKILL.md`。
|
||||||
|
> 因為 Skill 文件可能已經更新,且記憶中的內容可能不完整。
|
||||||
@@ -1,512 +0,0 @@
|
|||||||
---
|
|
||||||
trigger: always_on
|
|
||||||
---
|
|
||||||
|
|
||||||
---
|
|
||||||
name: 客戶端後台 UI 統一規範
|
|
||||||
description: 確保 Star ERP 客戶端(租戶端)後台所有頁面的 UI 元件保持統一的樣式與行為
|
|
||||||
---
|
|
||||||
|
|
||||||
## 概述
|
|
||||||
|
|
||||||
本技能提供 Star ERP 系統**客戶端(租戶端)後台**的 UI 統一性規範,確保所有頁面使用一致的元件、樣式類別、圖標和佈局模式。
|
|
||||||
|
|
||||||
> **適用範圍**:本規範適用於租戶端後台(使用 `AuthenticatedLayout` 的頁面),**不適用於**中央管理後台(`LandlordLayout`)。
|
|
||||||
|
|
||||||
## 核心原則
|
|
||||||
|
|
||||||
1. **使用統一的 UI 組件庫**:優先使用 `@/Components/ui/` 中的 47 個元件
|
|
||||||
2. **遵循既定的樣式類別**:使用 `app.css` 中定義的自定義按鈕類別
|
|
||||||
3. **統一的圖標系統**:全面使用 `lucide-react` 圖標
|
|
||||||
4. **一致的佈局模式**:表格、分頁、操作按鈕等保持相同結構
|
|
||||||
5. **權限控制**:所有操作按鈕必須使用 `<Can>` 元件包裹
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 1. 專案結構
|
|
||||||
|
|
||||||
### 1.1 關鍵目錄
|
|
||||||
|
|
||||||
```
|
|
||||||
resources/
|
|
||||||
├── css/
|
|
||||||
│ └── app.css # 全域樣式與設計 Token
|
|
||||||
├── js/
|
|
||||||
│ ├── Components/
|
|
||||||
│ │ ├── ui/ # 47 個基礎 UI 元件 (shadcn/ui)
|
|
||||||
│ │ ├── shared/ # 共用業務元件 (Pagination, BreadcrumbNav 等)
|
|
||||||
│ │ └── Permission/ # 權限控制元件 (Can, HasRole, CanAll)
|
|
||||||
│ ├── Layouts/
|
|
||||||
│ │ ├── AuthenticatedLayout.tsx # 客戶端後台佈局 ⬅️ 本規範適用
|
|
||||||
│ │ └── LandlordLayout.tsx # 中央管理後台佈局
|
|
||||||
│ └── Pages/ # 頁面元件
|
|
||||||
```
|
|
||||||
|
|
||||||
### 1.2 可用 UI 元件清單
|
|
||||||
|
|
||||||
```
|
|
||||||
accordion, alert, alert-dialog, avatar, badge, breadcrumb, button,
|
|
||||||
calendar, card, carousel, chart, checkbox, collapsible, command,
|
|
||||||
context-menu, dialog, drawer, dropdown-menu, form, hover-card,
|
|
||||||
input, input-otp, label, menubar, navigation-menu, pagination,
|
|
||||||
popover, progress, radio-group, resizable, scroll-area,
|
|
||||||
searchable-select, select, separator, sheet, sidebar, skeleton,
|
|
||||||
slider, sonner, switch, table, tabs, textarea, toggle, toggle-group,
|
|
||||||
tooltip
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 2. 色彩系統
|
|
||||||
|
|
||||||
### 2.1 主題色 (Primary) - **動態租戶品牌色**
|
|
||||||
|
|
||||||
> **注意**:主題色會根據租戶設定(Branding)動態改變,**嚴禁**在程式碼中 Hardcode 色碼(如 `#01ab83`)。
|
|
||||||
> 請務必使用 Tailwind Utility Class 或 CSS 變數。
|
|
||||||
|
|
||||||
| Tailwind Class | CSS Variable | 說明 |
|
|
||||||
|----------------|--------------|------|
|
|
||||||
| `*-primary-main` | `--primary-main` | **主色**:與租戶設定一致(預設綠色),用於主要按鈕、連結、強調文字 |
|
|
||||||
| `*-primary-dark` | `--primary-dark` | **深色**:系統自動計算,用於 Hover 狀態 |
|
|
||||||
| `*-primary-light` | `--primary-light` | **淺色**:系統自動計算,用於次要強調 |
|
|
||||||
| `*-primary-lightest` | `--primary-lightest` | **最淺色**:系統自動計算,用於背景底色、Active 狀態 |
|
|
||||||
|
|
||||||
**運作機制**:
|
|
||||||
`AuthenticatedLayout` 會根據後端回傳的 `branding` 資料,自動注入 CSS 變數覆寫預設值。
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// ✅ 正確:使用 Tailwind Class
|
|
||||||
<div className="text-primary-main">...</div>
|
|
||||||
|
|
||||||
// ✅ 正確:使用 CSS 變數 (自定義樣式時)
|
|
||||||
<div style={{ borderColor: 'var(--primary-main)' }}>...</div>
|
|
||||||
|
|
||||||
// ❌ 錯誤:寫死色碼 (會導致租戶無法換色)
|
|
||||||
<div className="text-[#01ab83]">...</div>
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2.2 灰階 (Grey Scale)
|
|
||||||
|
|
||||||
```css
|
|
||||||
--grey-0: #1a1a1a; /* 深黑 - 標題文字 */
|
|
||||||
--grey-1: #4a4a4a; /* 深灰 - 主要內文 */
|
|
||||||
--grey-2: #6b6b6b; /* 中灰 - 次要內文、Placeholder */
|
|
||||||
--grey-3: #9e9e9e; /* 淺灰 - 禁用文字、輔助說明 */
|
|
||||||
--grey-4: #e0e0e0; /* 極淺灰 - 邊框、分隔線 */
|
|
||||||
--grey-5: #fff; /* 白色 - 背景、按鈕文字 */
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2.3 狀態色 (State Colors)
|
|
||||||
|
|
||||||
```css
|
|
||||||
--other-success: #01ab83; /* 成功 - 同主題色 */
|
|
||||||
--other-error: #dc2626; /* 錯誤 - 刪除、警示 */
|
|
||||||
--other-warning: #f59e0b; /* 警告 - 提醒、注意 */
|
|
||||||
--other-info: #3b82f6; /* 資訊 - 說明、提示 */
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 3. 按鈕規範
|
|
||||||
|
|
||||||
### 3.1 按鈕樣式類別
|
|
||||||
|
|
||||||
專案在 `resources/css/app.css` 中定義了統一的按鈕樣式,**必須**使用這些類別:
|
|
||||||
|
|
||||||
#### Filled 按鈕(實心按鈕)— 用於主要操作
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// ✅ 主要操作按鈕(綠色主題色)- 新增、儲存、確認
|
|
||||||
<Button className="button-filled-primary">
|
|
||||||
<Plus className="h-4 w-4 mr-2" />
|
|
||||||
新增項目
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
// ✅ 成功操作
|
|
||||||
<Button className="button-filled-success">確認</Button>
|
|
||||||
|
|
||||||
// ✅ 資訊操作(用於系統提示、說明等非業務主流程)
|
|
||||||
<Button className="button-filled-info">系統資訊</Button>
|
|
||||||
|
|
||||||
// ✅ 警告操作
|
|
||||||
<Button className="button-filled-warning">警告</Button>
|
|
||||||
|
|
||||||
// ✅ 錯誤/刪除操作(AlertDialog 內確認按鈕)
|
|
||||||
<Button className="button-filled-error">刪除</Button>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Outlined 按鈕(邊框按鈕)— 用於次要操作
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// ✅ 編輯按鈕(表格操作列)
|
|
||||||
<Button variant="outline" size="sm" className="button-outlined-primary">
|
|
||||||
<Pencil className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
// ✅ 刪除按鈕(表格操作列)
|
|
||||||
<Button variant="outline" size="sm" className="button-outlined-error">
|
|
||||||
<Trash2 className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Text 按鈕(文字按鈕)
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<Button className="button-text-primary">查看更多</Button>
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3.2 按鈕大小
|
|
||||||
|
|
||||||
| Size | 高度 | 使用情境 |
|
|
||||||
|------|------|----------|
|
|
||||||
| `size="sm"` | h-8 | 表格操作列、緊湊佈局 |
|
|
||||||
| `size="default"` | h-9 | 一般操作、表單提交 |
|
|
||||||
| `size="lg"` | h-10 | 主要 CTA、頁面主操作 |
|
|
||||||
| `size="icon"` | 9×9 | 純圖標按鈕 |
|
|
||||||
|
|
||||||
### 3.3 常見操作按鈕模式
|
|
||||||
|
|
||||||
#### 頁面頂部新增按鈕
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<Can permission="resource.create">
|
|
||||||
<Link href={route('resource.create')}>
|
|
||||||
<Button className="button-filled-primary">
|
|
||||||
<Plus className="h-4 w-4 mr-2" />
|
|
||||||
新增XXX
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Can>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 表格操作列檢視按鈕
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<Can permission="resource.view">
|
|
||||||
<Link href={route('resource.show', item.id)}>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="button-outlined-primary"
|
|
||||||
title="檢視"
|
|
||||||
>
|
|
||||||
<Eye className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Can>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 表格操作列編輯按鈕
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<Can permission="resource.edit">
|
|
||||||
<Link href={route('resource.edit', item.id)}>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="button-outlined-primary"
|
|
||||||
title="編輯"
|
|
||||||
>
|
|
||||||
<Pencil className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Can>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 表格操作列刪除按鈕(帶確認對話框)
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<Can permission="resource.delete">
|
|
||||||
<AlertDialog>
|
|
||||||
<AlertDialogTrigger asChild>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
className="button-outlined-error"
|
|
||||||
title="刪除"
|
|
||||||
>
|
|
||||||
<Trash2 className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
</AlertDialogTrigger>
|
|
||||||
<AlertDialogContent>
|
|
||||||
<AlertDialogHeader>
|
|
||||||
<AlertDialogTitle>確認刪除</AlertDialogTitle>
|
|
||||||
<AlertDialogDescription>
|
|
||||||
確定要刪除「{item.name}」嗎?此操作無法復原。
|
|
||||||
</AlertDialogDescription>
|
|
||||||
</AlertDialogHeader>
|
|
||||||
<AlertDialogFooter>
|
|
||||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
|
||||||
<AlertDialogAction
|
|
||||||
onClick={() => handleDelete(item.id)}
|
|
||||||
className="bg-red-600 hover:bg-red-700"
|
|
||||||
>
|
|
||||||
刪除
|
|
||||||
</AlertDialogAction>
|
|
||||||
</AlertDialogFooter>
|
|
||||||
</AlertDialogContent>
|
|
||||||
</AlertDialog>
|
|
||||||
</Can>
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3.4 返回按鈕規範
|
|
||||||
|
|
||||||
詳情頁面(如:查看庫存、進貨單詳情)的返回按鈕應統一放置於 **頁面標題上方**,並採用「**圖標 + 文字**」的 Outlined 樣式。
|
|
||||||
|
|
||||||
**樣式規格**:
|
|
||||||
- **位置**:標題區域上方 (`mb-6`),獨立於標題列
|
|
||||||
- **樣式**:`variant="outline"` + `className="gap-2 button-outlined-primary"`
|
|
||||||
- **圖標**:`<ArrowLeft className="h-4 w-4" />`
|
|
||||||
- **文字**:清楚說明返回目的地,例如「返回倉庫管理」、「返回列表」
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<div className="mb-6">
|
|
||||||
<Link href={route('resource.index')}>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
className="gap-2 button-outlined-primary"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4" />
|
|
||||||
返回列表
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</div>
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 3.5 頁面佈局規範(新增/編輯頁面)
|
|
||||||
|
|
||||||
### 標準結構
|
|
||||||
|
|
||||||
新增/編輯頁面(如:商品新增、採購單建立)應遵循以下標準結構:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<AuthenticatedLayout breadcrumbs={...}>
|
|
||||||
<Head title="..." />
|
|
||||||
|
|
||||||
<div className="container mx-auto p-6 max-w-7xl">
|
|
||||||
{/* Header */}
|
|
||||||
<div className="mb-6">
|
|
||||||
{/* 返回按鈕 */}
|
|
||||||
<Link href={route('resource.index')}>
|
|
||||||
<Button
|
|
||||||
variant="outline"
|
|
||||||
className="gap-2 button-outlined-primary mb-4"
|
|
||||||
>
|
|
||||||
<ArrowLeft className="h-4 w-4" />
|
|
||||||
返回列表
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
|
|
||||||
{/* 頁面標題區塊 */}
|
|
||||||
<div className="mb-4">
|
|
||||||
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
|
||||||
<Icon className="h-6 w-6 text-primary-main" />
|
|
||||||
頁面標題
|
|
||||||
</h1>
|
|
||||||
<p className="text-gray-500 mt-1">
|
|
||||||
頁面說明文字
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* 表單或內容區塊 */}
|
|
||||||
<FormComponent ... />
|
|
||||||
</div>
|
|
||||||
</AuthenticatedLayout>
|
|
||||||
```
|
|
||||||
|
|
||||||
### 關鍵規範
|
|
||||||
|
|
||||||
1. **外層容器**:使用 `className="container mx-auto p-6 max-w-7xl"` 確保寬度與間距一致
|
|
||||||
2. **Header 包裹**:使用 `<div className="mb-6">` 包裹返回按鈕與標題區塊
|
|
||||||
3. **返回按鈕**:加上 `mb-4` 與標題區塊分隔
|
|
||||||
4. **標題區塊**:使用 `<div className="mb-4">` 包裹 h1 和 p 標籤
|
|
||||||
5. **標題樣式**:`text-2xl font-bold text-grey-0 flex items-center gap-2`
|
|
||||||
6. **說明文字**:`text-gray-500 mt-1`
|
|
||||||
|
|
||||||
### 範例頁面
|
|
||||||
|
|
||||||
- ✅ `/resources/js/Pages/PurchaseOrder/Create.tsx`(建立採購單)
|
|
||||||
- ✅ `/resources/js/Pages/Product/Create.tsx`(新增商品)
|
|
||||||
- ✅ `/resources/js/Pages/Product/Edit.tsx`(編輯商品)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 4. 圖標規範
|
|
||||||
|
|
||||||
### 4.1 統一使用 lucide-react
|
|
||||||
|
|
||||||
**統一使用 `lucide-react`**,禁止使用其他圖標庫(如 FontAwesome、Material Icons、react-icons 等)。
|
|
||||||
|
|
||||||
### 4.2 圖標尺寸標準
|
|
||||||
|
|
||||||
| 尺寸 | 類別 | 使用情境 |
|
|
||||||
|------|------|----------|
|
|
||||||
| 小型 | `h-3 w-3` | Badge 內、小文字旁 |
|
|
||||||
| 標準 | `h-4 w-4` | 按鈕內、表格操作 |
|
|
||||||
| 標題 | `h-5 w-5` | 側邊欄選單 |
|
|
||||||
| 大型 | `h-6 w-6` | 頁面標題 |
|
|
||||||
|
|
||||||
### 4.3 常用操作圖標映射
|
|
||||||
|
|
||||||
| 操作 | 圖標組件 | 使用情境 |
|
|
||||||
|------|----------|----------|
|
|
||||||
| 新增 | `<Plus />` | 新增按鈕 |
|
|
||||||
| 編輯 | `<Pencil />` | 編輯按鈕 |
|
|
||||||
| 刪除 | `<Trash2 />` | 刪除按鈕 |
|
|
||||||
| 查看 | `<Eye />` | 查看詳情 |
|
|
||||||
| 搜尋 | `<Search />` | 搜尋欄位 |
|
|
||||||
| 篩選 | `<Filter />` | 篩選功能 |
|
|
||||||
| 下載 | `<Download />` | 下載/匯出 |
|
|
||||||
| 上傳 | `<Upload />` | 上傳/匯入 |
|
|
||||||
| 設定 | `<Settings />` | 設定功能 |
|
|
||||||
| 複製 | `<Copy />` | 複製內容 |
|
|
||||||
| 郵件 | `<Mail />` | Email 顯示 |
|
|
||||||
| 使用者 | `<Users />`, `<User />` | 使用者管理 |
|
|
||||||
| 權限 | `<Shield />` | 角色/權限 |
|
|
||||||
| 排序 | `<ArrowUpDown />`, `<ArrowUp />`, `<ArrowDown />` | 表格排序 |
|
|
||||||
| 儀表板 | `<LayoutDashboard />` | 首頁/總覽 |
|
|
||||||
| 商品 | `<Package />` | 商品管理 |
|
|
||||||
| 倉庫 | `<Warehouse />` | 倉庫管理 |
|
|
||||||
| 廠商 | `<Truck />`, `<Contact2 />` | 廠商管理 |
|
|
||||||
| 採購 | `<ShoppingCart />` | 採購管理 |
|
|
||||||
|
|
||||||
### 4.4 圖標使用範例
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { Plus, Pencil, Trash2, Users } from 'lucide-react';
|
|
||||||
|
|
||||||
// 頁面標題
|
|
||||||
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
|
||||||
<Users className="h-6 w-6 text-[#01ab83]" />
|
|
||||||
使用者管理
|
|
||||||
</h1>
|
|
||||||
|
|
||||||
// 按鈕內圖標(圖標在左,帶文字)
|
|
||||||
<Button className="button-filled-primary">
|
|
||||||
<Plus className="h-4 w-4 mr-2" />
|
|
||||||
新增使用者
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
// 純圖標按鈕(表格操作列)
|
|
||||||
<Button variant="outline" size="sm" className="button-outlined-primary">
|
|
||||||
<Pencil className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 5. 表格規範
|
|
||||||
|
|
||||||
### 5.1 表格容器
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
|
|
||||||
<Table>
|
|
||||||
{/* 表格內容 */}
|
|
||||||
</Table>
|
|
||||||
</div>
|
|
||||||
```
|
|
||||||
|
|
||||||
### 5.2 表格標題列
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<TableHeader className="bg-gray-50">
|
|
||||||
<TableRow>
|
|
||||||
<TableHead className="w-[50px] text-center">#</TableHead>
|
|
||||||
<TableHead>名稱</TableHead>
|
|
||||||
<TableHead className="text-center">操作</TableHead>
|
|
||||||
</TableRow>
|
|
||||||
</TableHeader>
|
|
||||||
```
|
|
||||||
|
|
||||||
**關鍵要點**:
|
|
||||||
- 使用 `bg-gray-50` 背景色
|
|
||||||
- 序號欄位固定寬度 `w-[50px]` 並置中
|
|
||||||
- 操作欄位置中顯示
|
|
||||||
|
|
||||||
### 5.3 表格主體
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
<TableBody>
|
|
||||||
{items.length === 0 ? (
|
|
||||||
<TableRow>
|
|
||||||
<TableCell colSpan={5} className="text-center py-8 text-gray-500">
|
|
||||||
無符合條件的資料
|
|
||||||
</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
) : (
|
|
||||||
items.map((item, index) => (
|
|
||||||
<TableRow key={item.id}>
|
|
||||||
<TableCell className="text-gray-500 font-medium text-center">
|
|
||||||
{startIndex + index}
|
|
||||||
</TableCell>
|
|
||||||
{/* 其他欄位 */}
|
|
||||||
<TableCell className="text-center">
|
|
||||||
<div className="flex items-center justify-center gap-2">
|
|
||||||
{/* 操作按鈕 */}
|
|
||||||
</div>
|
|
||||||
</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
</TableBody>
|
|
||||||
```
|
|
||||||
|
|
||||||
**關鍵要點**:
|
|
||||||
- 空狀態訊息使用置中、灰色文字
|
|
||||||
- 序號欄使用 `text-gray-500 font-medium text-center`
|
|
||||||
- 操作欄使用 `flex items-center justify-center gap-2` 排列按鈕
|
|
||||||
|
|
||||||
### 5.4 欄位排序規範
|
|
||||||
|
|
||||||
當表格需要支援排序時,請遵循以下模式:
|
|
||||||
|
|
||||||
1. **圖標邏輯**:
|
|
||||||
* 未排序:`ArrowUpDown` (class: `text-muted-foreground`)
|
|
||||||
* 升冪 (asc):`ArrowUp` (class: `text-primary`)
|
|
||||||
* 降冪 (desc):`ArrowDown` (class: `text-primary`)
|
|
||||||
2. **結構**:在 `TableHead` 內使用 `button` 元素。
|
|
||||||
3. **後端配合**:後端 Controller **必須** 處理 `sort_by` 與 `sort_order` 參數。
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// 1. 定義 Helper Component (在元件內部)
|
|
||||||
const SortIcon = ({ field }: { field: string }) => {
|
|
||||||
if (filters.sort_by !== field) {
|
|
||||||
return <ArrowUpDown className="h-4 w-4 text-muted-foreground ml-1" />;
|
|
||||||
}
|
|
||||||
if (filters.sort_order === "asc") {
|
|
||||||
return <ArrowUp className="h-4 w-4 text-primary ml-1" />;
|
|
||||||
}
|
|
||||||
return <ArrowDown className="h-4 w-4 text-primary ml-1" />;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 2. 表格標題應用
|
|
||||||
<TableHead>
|
|
||||||
<button
|
|
||||||
onClick={() => handleSort('created_at')}
|
|
||||||
className="flex items-center hover:text-gray-900"
|
|
||||||
>
|
|
||||||
建立時間 <SortIcon field="created_at" />
|
|
||||||
</button>
|
|
||||||
</TableHead>
|
|
||||||
|
|
||||||
// 3. 排序處理函式 (三態切換:未排序 -> 升冪 -> 降冪 -> 未排序)
|
|
||||||
const handleSort = (field: string) => {
|
|
||||||
let newSortBy: string | undefined = field;
|
|
||||||
let newSortOrder: 'asc' | 'desc' | undefined = 'asc';
|
|
||||||
|
|
||||||
if (filters.sort_by === field) {
|
|
||||||
if (filters.sort_order === 'asc') {
|
|
||||||
newSortOrder = 'desc';
|
|
||||||
} else {
|
|
||||||
// desc -> reset (回到預設排序)
|
|
||||||
newSortBy = undefined;
|
|
||||||
newSortOrder = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
router.get(
|
|
||||||
route(route().curr
|
|
||||||
285
.agents/skills/activity-logging/SKILL.md
Normal file
285
.agents/skills/activity-logging/SKILL.md
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
---
|
||||||
|
name: 操作紀錄實作規範 (Activity Logging Skill)
|
||||||
|
description: 規範系統內 Activity Log 的實作標準,包含自動名稱解析、複雜單據合併記錄、與前端顯示優化。
|
||||||
|
---
|
||||||
|
|
||||||
|
# 操作紀錄實作規範 (Activity Logging Skill)
|
||||||
|
|
||||||
|
本技能定義了 Star ERP 系統中操作紀錄的最高實作標準,旨在確保每筆日誌都具有「高度可讀性」與「單一性」。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 啟用 Activity Log (Model 基本設定)
|
||||||
|
|
||||||
|
在 Model 中引用 `LogsActivity` trait 並實作 `getActivitylogOptions` 方法。
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Spatie\Activitylog\Traits\LogsActivity;
|
||||||
|
use Spatie\Activitylog\LogOptions;
|
||||||
|
|
||||||
|
class Product extends Model
|
||||||
|
{
|
||||||
|
use LogsActivity;
|
||||||
|
|
||||||
|
public function getActivitylogOptions(): LogOptions
|
||||||
|
{
|
||||||
|
return LogOptions::defaults()
|
||||||
|
->logAll()
|
||||||
|
->logOnlyDirty() // ✅ 關鍵:只記錄有變動的欄位
|
||||||
|
->dontSubmitEmptyLogs(); // 若無變動則不記錄
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. `tapActivity` 實作規範 (Backend 核心)
|
||||||
|
|
||||||
|
### 2.1 型別宣告:統一使用 `Contracts\Activity`
|
||||||
|
|
||||||
|
```php
|
||||||
|
// ✅ 正確:使用介面
|
||||||
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
|
|
||||||
|
// ❌ 禁止:使用具體類別
|
||||||
|
public function tapActivity(\Spatie\Activitylog\Models\Activity $activity, string $eventName)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 必須 `toArray()` 避免 Indirect modification error
|
||||||
|
|
||||||
|
```php
|
||||||
|
// 🚩 核心:轉換為陣列以避免 Indirect modification error
|
||||||
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
|
// ... 操作 $properties ...
|
||||||
|
|
||||||
|
$activity->properties = $properties; // 最後整體回寫
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.3 Snapshot 快照策略
|
||||||
|
|
||||||
|
為確保資料被刪除後仍能辨識操作對象,**必須**在 `properties.snapshot` 中儲存關鍵識別資訊。
|
||||||
|
|
||||||
|
```php
|
||||||
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
|
$snapshot['doc_no'] = $this->doc_no; // 單號
|
||||||
|
$snapshot['name'] = $this->name; // 名稱
|
||||||
|
$snapshot['warehouse_name'] = $this->warehouse?->name; // 關聯名稱
|
||||||
|
$properties['snapshot'] = $snapshot;
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.4 全域 ID 轉名稱邏輯 (ID Resolution)
|
||||||
|
|
||||||
|
所有的 ID(如 `warehouse_id`, `created_by`)在記錄時應自動解析為名稱。
|
||||||
|
|
||||||
|
#### 模組內 Model:可直接查詢
|
||||||
|
|
||||||
|
```php
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
// 同模組內的 Model 可以直接查詢
|
||||||
|
foreach (['created_by', 'updated_by', 'completed_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 跨模組 Model:必須透過 Service Interface
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> 依據跨模組通訊規範,若需解析其他模組的 ID(例如在 `Procurement` 模組中解析 `warehouse_id`),
|
||||||
|
> **禁止**直接 `Warehouse::find()`,必須透過 Service Interface。
|
||||||
|
|
||||||
|
```php
|
||||||
|
// ✅ 正確:透過 Service Interface 取得跨模組資料
|
||||||
|
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
||||||
|
$warehouse = app(\App\Modules\Inventory\Contracts\InventoryServiceInterface::class)
|
||||||
|
->getWarehouse($data['warehouse_id']);
|
||||||
|
$data['warehouse_id'] = $warehouse?->name ?? $data['warehouse_id'];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> `Core` 模組的 `User`, `Role`, `Tenant` 屬於全域例外,其他模組可直接查詢。
|
||||||
|
> 詳見 [跨模組通訊規範](file:///home/mama/projects/star-erp/.agents/skills/cross-module-communication/SKILL.md)。
|
||||||
|
|
||||||
|
### 2.5 完整 `tapActivity` 範例(參考 PurchaseOrder)
|
||||||
|
|
||||||
|
```php
|
||||||
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
|
{
|
||||||
|
// 🚩 轉換為陣列
|
||||||
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
|
// 1. Snapshot 快照
|
||||||
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
|
$snapshot['po_number'] = $this->code;
|
||||||
|
$snapshot['vendor_name'] = $this->vendor?->name;
|
||||||
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
// 2. ID 轉名稱
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
// 全域例外:User 可直接查
|
||||||
|
foreach (['user_id', 'created_by', 'updated_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name ?? $data[$f];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 同模組:可直接查
|
||||||
|
if (isset($data['vendor_id']) && is_numeric($data['vendor_id'])) {
|
||||||
|
$data['vendor_id'] = Vendor::find($data['vendor_id'])?->name ?? $data['vendor_id'];
|
||||||
|
}
|
||||||
|
// 跨模組:必須透過 Service Interface
|
||||||
|
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
||||||
|
$warehouse = app(\App\Modules\Inventory\Contracts\InventoryServiceInterface::class)
|
||||||
|
->getWarehouse($data['warehouse_id']);
|
||||||
|
$data['warehouse_id'] = $warehouse?->name ?? $data['warehouse_id'];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
|
// 3. 合併 activityProperties (手動傳入的 items_diff 等)
|
||||||
|
if (!empty($this->activityProperties)) {
|
||||||
|
$properties = array_merge($properties, $this->activityProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activity->properties = $properties;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 複雜操作的日誌合併 (Log Consolidation)
|
||||||
|
|
||||||
|
當一個操作同時涉及「多個品項異動」與「單據狀態變更」時,**嚴禁**產生多筆重複日誌。
|
||||||
|
|
||||||
|
### 3.1 手動記錄必須自行過濾差異
|
||||||
|
|
||||||
|
```php
|
||||||
|
// ✅ 正確:自行比對差異,只存變動值
|
||||||
|
$changedAttributes = [];
|
||||||
|
$changedOldAttributes = [];
|
||||||
|
|
||||||
|
foreach ($newAttributes as $key => $value) {
|
||||||
|
if ($value != ($oldAttributes[$key] ?? null)) {
|
||||||
|
$changedAttributes[$key] = $value;
|
||||||
|
$changedOldAttributes[$key] = $oldAttributes[$key] ?? null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($changedAttributes)) {
|
||||||
|
activity()
|
||||||
|
->withProperties(['attributes' => $changedAttributes, 'old' => $changedOldAttributes])
|
||||||
|
->log('updated');
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 `saveQuietly()` + 手動日誌 合併策略
|
||||||
|
|
||||||
|
```php
|
||||||
|
DB::transaction(function () use ($doc, $items) {
|
||||||
|
// 1. 更新品項 (記錄變動細節)
|
||||||
|
$updatedItems = $this->getUpdatedItems($doc, $items);
|
||||||
|
|
||||||
|
// 2. 靜默更新單據狀態 (避免 Trait 產生冗餘日誌)
|
||||||
|
$doc->status = 'completed';
|
||||||
|
$doc->saveQuietly();
|
||||||
|
|
||||||
|
// 3. 手動觸發單一合併日誌
|
||||||
|
activity()
|
||||||
|
->performedOn($doc)
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => ['updated' => $updatedItems],
|
||||||
|
'attributes' => ['status' => 'completed'],
|
||||||
|
'old' => ['status' => 'counting']
|
||||||
|
])
|
||||||
|
->log('updated');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> 使用 `saveQuietly()` 會繞過 Model Events(如自動單號產生)。
|
||||||
|
> 若 Model 有 `creating`/`updating` 事件產生單號,需在 Service 中手動處理。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 後端 Controller 映射 (Subject Map)
|
||||||
|
|
||||||
|
新增 Model 時,必須同步在 `ActivityLogController::getSubjectMap()` 加入中文映射。
|
||||||
|
|
||||||
|
**位置**: `app/Modules/Core/Controllers/ActivityLogController.php`
|
||||||
|
|
||||||
|
```php
|
||||||
|
private function getSubjectMap()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'App\Modules\Inventory\Models\Product' => '商品',
|
||||||
|
'App\Modules\Finance\Models\UtilityFee' => '公共事業費',
|
||||||
|
// ... 新增此行
|
||||||
|
];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 前端介面規範 (Frontend)
|
||||||
|
|
||||||
|
### 5.1 標籤命名規範 (Field Labels)
|
||||||
|
|
||||||
|
前端顯示應完全移除「ID」字眼,提供最友善的閱讀體驗。
|
||||||
|
|
||||||
|
**位置**: `resources/js/Components/ActivityLog/ActivityDetailDialog.tsx`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const fieldLabels: Record<string, string> = {
|
||||||
|
warehouse_id: '倉庫', // ❌ 禁用「倉庫 ID」
|
||||||
|
created_by: '建立者', // ❌ 禁用「建立者 ID」
|
||||||
|
completed_by: '完成者',
|
||||||
|
status: '狀態',
|
||||||
|
// 新增 Model 的欄位翻譯 ...
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 `nameParams` 必須在兩處同步更新
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> `nameParams` 在 `LogTable.tsx` 和 `ActivityDetailDialog.tsx` 中各有一份,
|
||||||
|
> 新增時**必須兩處同步更新**,否則會導致列表與詳情頁顯示不一致。
|
||||||
|
|
||||||
|
| 檔案 | 用途 |
|
||||||
|
|---|---|
|
||||||
|
| `resources/js/Components/ActivityLog/LogTable.tsx` | 列表頁的描述文字 |
|
||||||
|
| `resources/js/Components/ActivityLog/ActivityDetailDialog.tsx` | 對話框標題 |
|
||||||
|
|
||||||
|
### 5.3 特殊結構顯示
|
||||||
|
|
||||||
|
* **品項異動**:前端已能渲染 `items_diff` 結構,以「品項名稱 + 數值變動」方式呈現表格。
|
||||||
|
* **顯示過濾邏輯**(已內建於 `ActivityDetailDialog`):
|
||||||
|
- **Created**: 顯示初始化欄位
|
||||||
|
- **Updated**: 僅顯示有變動的欄位 (`isChanged` 判斷)
|
||||||
|
- **Deleted**: 顯示刪除前的完整資料
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 開發檢核清單 (Checklist)
|
||||||
|
|
||||||
|
- [ ] **Model**: 是否已設定 `logOnlyDirty` + `dontSubmitEmptyLogs`?
|
||||||
|
- [ ] **Model**: `tapActivity` 型別是否使用 `Contracts\Activity`?
|
||||||
|
- [ ] **Model**: `tapActivity` 是否已使用 `toArray()` 處理 Collection?
|
||||||
|
- [ ] **Model**: 是否已實作 Snapshot(關鍵識別資訊)?
|
||||||
|
- [ ] **Model**: ID 轉名稱是否遵守跨模組規範(Core 例外,其餘需透過 Interface)?
|
||||||
|
- [ ] **Service**: 是否使用 `saveQuietly()` 搭配手動 `activity()` 避免重複日誌?
|
||||||
|
- [ ] **Controller**: `ActivityLogController::getSubjectMap()` 是否已新增 Model 中文映射?
|
||||||
|
- [ ] **UI**: `fieldLabels` 是否已新增欄位中文翻譯?
|
||||||
|
- [ ] **UI**: `nameParams` 是否已在 `LogTable` 和 `ActivityDetailDialog` 兩處同步?
|
||||||
@@ -1,7 +1,3 @@
|
|||||||
---
|
|
||||||
trigger: always_on
|
|
||||||
---
|
|
||||||
|
|
||||||
---
|
---
|
||||||
name: 跨模組調用與通訊規範 (Cross-Module Communication)
|
name: 跨模組調用與通訊規範 (Cross-Module Communication)
|
||||||
description: 規範 Laravel Modular Monolith 架構下,不同業務模組中如何彼此調用資料與邏輯,包含禁止項目、Interface 實作、與 Service 綁定規則。
|
description: 規範 Laravel Modular Monolith 架構下,不同業務模組中如何彼此調用資料與邏輯,包含禁止項目、Interface 實作、與 Service 綁定規則。
|
||||||
@@ -14,12 +10,8 @@ description: 規範 Laravel Modular Monolith 架構下,不同業務模組中
|
|||||||
## 🚫 絕對禁止的行為 (Strict Prohibitions)
|
## 🚫 絕對禁止的行為 (Strict Prohibitions)
|
||||||
|
|
||||||
* **禁止跨模組 Eloquent 關聯(例外除外)**
|
* **禁止跨模組 Eloquent 關聯(例外除外)**
|
||||||
* **錯誤**:在 `app/Modules/Sales/Models/Order.php` 中撰寫 `public function product() { return $this->belongsTo(\App\Modules\Inventory\Models\Product::class); }`。
|
|
||||||
* **原因**:這會造成資料庫查詢的強耦合。如果 `Inventory` 模組修改了 Schema,`Sales` 模組會無預警崩壞。
|
|
||||||
* **禁止跨模組直接引入 (use) Model**
|
* **禁止跨模組直接引入 (use) Model**
|
||||||
* **錯誤**:在 `app/Modules/Procurement/Controllers/PurchaseOrderController.php` 頂端寫 `use App\Modules\Inventory\Models\Warehouse;`。
|
|
||||||
* **禁止跨模組直接實例化 (new) Service**
|
* **禁止跨模組直接實例化 (new) Service**
|
||||||
* **錯誤**:`$inventoryService = new \App\Modules\Inventory\Services\InventoryService();`
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -28,9 +20,9 @@ description: 規範 Laravel Modular Monolith 架構下,不同業務模組中
|
|||||||
雖然我們嚴格禁止跨模組直接相依,但為了開發效率與框架機制的完整性,**`Core` 模組下的特定基礎設施模型 (Infrastructure Models) 被視為全域例外**。
|
雖然我們嚴格禁止跨模組直接相依,但為了開發效率與框架機制的完整性,**`Core` 模組下的特定基礎設施模型 (Infrastructure Models) 被視為全域例外**。
|
||||||
|
|
||||||
其他業務模組 **可以** 透過 Eloquent (`belongsTo` / `hasMany`) 直接關聯以下 Model:
|
其他業務模組 **可以** 透過 Eloquent (`belongsTo` / `hasMany`) 直接關聯以下 Model:
|
||||||
1. **`App\Modules\Core\Models\User`**:因為幾乎所有表都有 `created_by` / `updated_by`,直接關聯可保留 `with('creator')` 等便利性。
|
1. **`App\Modules\Core\Models\User`**
|
||||||
2. **`App\Modules\Core\Models\Role`**:權限判定已深度整合至系統底層。
|
2. **`App\Modules\Core\Models\Role`**
|
||||||
3. **`App\Modules\Core\Models\Tenant`**:多租戶架構 (Tenancy) 的核心基石,底層查詢會頻繁依賴。
|
3. **`App\Modules\Core\Models\Tenant`**
|
||||||
|
|
||||||
> **⚠️ 注意**:這項例外是單向的。`Core` 模組內的業務邏輯(如 `DashboardController`)**絕對不能**反過來直接 `use` 外部業務模組的 Model,仍必須透過外部模組的 Service Interface 來索取資料。
|
> **⚠️ 注意**:這項例外是單向的。`Core` 模組內的業務邏輯(如 `DashboardController`)**絕對不能**反過來直接 `use` 外部業務模組的 Model,仍必須透過外部模組的 Service Interface 來索取資料。
|
||||||
|
|
||||||
@@ -45,18 +37,12 @@ description: 規範 Laravel Modular Monolith 架構下,不同業務模組中
|
|||||||
如果 `Inventory` 模組需要提供功能給外部使用,請在 `app/Modules/Inventory/Contracts/` 建立 Interface 檔案。
|
如果 `Inventory` 模組需要提供功能給外部使用,請在 `app/Modules/Inventory/Contracts/` 建立 Interface 檔案。
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// app/Modules/Inventory/Contracts/InventoryServiceInterface.php
|
|
||||||
namespace App\Modules\Inventory\Contracts;
|
namespace App\Modules\Inventory\Contracts;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
interface InventoryServiceInterface
|
interface InventoryServiceInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* 取得可用的倉庫清單
|
|
||||||
*
|
|
||||||
* @return Collection 包含每個倉庫的 id, name, code 等基本資料
|
|
||||||
*/
|
|
||||||
public function getActiveWarehouses(): Collection;
|
public function getActiveWarehouses(): Collection;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -66,7 +52,6 @@ interface InventoryServiceInterface
|
|||||||
由 `Inventory` 模組自己的 Service 來實作上述介面。
|
由 `Inventory` 模組自己的 Service 來實作上述介面。
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// app/Modules/Inventory/Services/InventoryService.php
|
|
||||||
namespace App\Modules\Inventory\Services;
|
namespace App\Modules\Inventory\Services;
|
||||||
|
|
||||||
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||||||
@@ -77,8 +62,6 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
{
|
{
|
||||||
public function getActiveWarehouses(): Collection
|
public function getActiveWarehouses(): Collection
|
||||||
{
|
{
|
||||||
// 建議只取出需要的欄位,或者轉換為 DTO / 陣列
|
|
||||||
// 避免將完整的 Eloquent Model 實例拋出模組外
|
|
||||||
return Warehouse::where('is_active', true)
|
return Warehouse::where('is_active', true)
|
||||||
->select(['id', 'name', 'code'])
|
->select(['id', 'name', 'code'])
|
||||||
->get();
|
->get();
|
||||||
@@ -89,7 +72,6 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
然後進入 `app/Modules/Inventory/InventoryServiceProvider.php` 完成綁定:
|
然後進入 `app/Modules/Inventory/InventoryServiceProvider.php` 完成綁定:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// app/Modules/Inventory/InventoryServiceProvider.php
|
|
||||||
namespace App\Modules\Inventory;
|
namespace App\Modules\Inventory;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
@@ -100,7 +82,6 @@ class InventoryServiceProvider extends ServiceProvider
|
|||||||
{
|
{
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
// 綁定介面與實體
|
|
||||||
$this->app->bind(InventoryServiceInterface::class, InventoryService::class);
|
$this->app->bind(InventoryServiceInterface::class, InventoryService::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,10 +89,9 @@ class InventoryServiceProvider extends ServiceProvider
|
|||||||
|
|
||||||
### Step 3: 調用方透過依賴注入 (DI) 使用服務
|
### Step 3: 調用方透過依賴注入 (DI) 使用服務
|
||||||
|
|
||||||
當 `Procurement` 模組需要取得倉庫資料時,禁止直接 new 服務或呼叫倉庫 Model。必須透過**建構子注入**或**方法注入**取得 `InventoryServiceInterface`。
|
當 `Procurement` 模組需要取得倉庫資料時,必須透過**建構子注入**或**方法注入**取得 `InventoryServiceInterface`。
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// app/Modules/Procurement/Controllers/PurchaseOrderController.php
|
|
||||||
namespace App\Modules\Procurement\Controllers;
|
namespace App\Modules\Procurement\Controllers;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
@@ -120,14 +100,12 @@ use Inertia\Inertia;
|
|||||||
|
|
||||||
class PurchaseOrderController extends Controller
|
class PurchaseOrderController extends Controller
|
||||||
{
|
{
|
||||||
// 透過建構子注入介面
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected InventoryServiceInterface $inventoryService
|
protected InventoryServiceInterface $inventoryService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
// 僅能呼叫介面有定義的方法
|
|
||||||
$warehouses = $this->inventoryService->getActiveWarehouses();
|
$warehouses = $this->inventoryService->getActiveWarehouses();
|
||||||
|
|
||||||
return Inertia::render('Procurement/PurchaseOrder/Create', [
|
return Inertia::render('Procurement/PurchaseOrder/Create', [
|
||||||
@@ -141,14 +119,11 @@ class PurchaseOrderController extends Controller
|
|||||||
|
|
||||||
## ⚠️ 跨模組資料回傳的注意事項 (Data Hydration)
|
## ⚠️ 跨模組資料回傳的注意事項 (Data Hydration)
|
||||||
|
|
||||||
* **回傳純粹資料**:為了防止其他模組意外觸發 Lazy Loading (`$item->product->name`),請盡量在 Service 中就用 `with()` 載入好關聯,或者直接轉為原生的 Array、`stdClass`、或具體的 DTO。
|
* **回傳純粹資料**:建議在 Service 中用 `with()` 載入好關聯,或者直接轉為原生的 Array 或有具體結構的 DTO,避免依賴 Lazy Loading。
|
||||||
* **手動組合 (Manual Hydration)**:若某個頁面需要合併兩個模組的資料,這也是被允許的,但必須在 Controller 層級呼叫兩個不同的 Service Interface 後,手動合併。
|
* **手動組合 (Manual Hydration)**:若某個頁面需要合併兩個模組的資料,必須在 Controller 層級呼叫兩個不同的 Service Interface 後,手動合併。
|
||||||
|
|
||||||
### 範例:手動合併資料
|
### 範例:手動合併資料
|
||||||
```php
|
```php
|
||||||
// 錯誤示範(禁止在 OrderService 中去查使用者的關聯)
|
|
||||||
$orders = Order::with('user')->get(); // 如果 user 表在 Core 模組,這是不允許的
|
|
||||||
|
|
||||||
// 正確示範:在各自模組取資料,並手動組裝
|
// 正確示範:在各自模組取資料,並手動組裝
|
||||||
$orders = $this->orderService->getOrders();
|
$orders = $this->orderService->getOrders();
|
||||||
$userIds = $orders->pluck('user_id')->unique()->toArray();
|
$userIds = $orders->pluck('user_id')->unique()->toArray();
|
||||||
266
.agents/skills/e2e-testing/SKILL.md
Normal file
266
.agents/skills/e2e-testing/SKILL.md
Normal file
@@ -0,0 +1,266 @@
|
|||||||
|
---
|
||||||
|
name: E2E 端到端測試規範 (E2E Testing with Playwright)
|
||||||
|
description: 規範 Playwright 端到端測試的撰寫慣例、目錄結構、共用工具與執行方式,確保所有 E2E 測試保持一致性與可維護性。
|
||||||
|
---
|
||||||
|
|
||||||
|
# E2E 端到端測試規範 (E2E Testing with Playwright)
|
||||||
|
|
||||||
|
本技能定義了 Star ERP 系統中端到端 (E2E) 測試的實作標準,使用 Playwright 模擬真實使用者操作瀏覽器,驗證 UI 顯示與功能流程的正確性。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 專案結構
|
||||||
|
|
||||||
|
### 1.1 目錄配置
|
||||||
|
|
||||||
|
```
|
||||||
|
star-erp/
|
||||||
|
├── playwright.config.ts # Playwright 設定檔
|
||||||
|
├── e2e/ # E2E 測試根目錄
|
||||||
|
│ ├── helpers/ # 共用工具函式
|
||||||
|
│ │ └── auth.ts # 登入 helper
|
||||||
|
│ ├── screenshots/ # 測試截圖存放
|
||||||
|
│ ├── auth.spec.ts # 認證相關測試(登入、登出)
|
||||||
|
│ ├── inventory.spec.ts # 庫存模組測試
|
||||||
|
│ ├── products.spec.ts # 商品模組測試
|
||||||
|
│ └── {module}.spec.ts # 依模組命名
|
||||||
|
├── playwright-report/ # HTML 測試報告(自動產生,已 gitignore)
|
||||||
|
└── test-results/ # 失敗截圖與錄影(自動產生,已 gitignore)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 1.2 命名規範
|
||||||
|
|
||||||
|
| 項目 | 規範 | 範例 |
|
||||||
|
|---|---|---|
|
||||||
|
| 測試檔案 | 小寫,依模組命名 `.spec.ts` | `inventory.spec.ts` |
|
||||||
|
| 測試群組 | `test.describe('中文功能名稱')` | `test.describe('庫存查詢')` |
|
||||||
|
| 測試案例 | 中文描述「**應**」開頭 | `test('應顯示庫存清單')` |
|
||||||
|
| 截圖檔案 | `{module}-{scenario}.png` | `inventory-search-result.png` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 設定檔 (playwright.config.ts)
|
||||||
|
|
||||||
|
### 2.1 核心設定
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { defineConfig, devices } from '@playwright/test';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
testDir: './e2e',
|
||||||
|
fullyParallel: true,
|
||||||
|
forbidOnly: !!process.env.CI,
|
||||||
|
retries: process.env.CI ? 2 : 0,
|
||||||
|
workers: process.env.CI ? 1 : undefined,
|
||||||
|
reporter: 'html',
|
||||||
|
use: {
|
||||||
|
baseURL: 'http://localhost:8081', // Sail 開發伺服器
|
||||||
|
screenshot: 'only-on-failure', // 失敗時自動截圖
|
||||||
|
video: 'retain-on-failure', // 失敗時保留錄影
|
||||||
|
trace: 'on-first-retry', // 重試時收集 trace
|
||||||
|
},
|
||||||
|
projects: [
|
||||||
|
{
|
||||||
|
name: 'chromium',
|
||||||
|
use: { ...devices['Desktop Chrome'] },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 重要注意事項
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> `baseURL` 必須指向本機 Sail 開發伺服器(預設 `http://localhost:8081`)。
|
||||||
|
> 確保測試前已執行 `./vendor/bin/sail up -d` 與 `./vendor/bin/sail npm run dev`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 共用工具 (Helpers)
|
||||||
|
|
||||||
|
### 3.1 登入 Helper
|
||||||
|
|
||||||
|
位置:`e2e/helpers/auth.ts`
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Page } from '@playwright/test';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 共用登入函式
|
||||||
|
* 使用測試帳號登入 ERP 系統
|
||||||
|
*/
|
||||||
|
export async function login(page: Page, username = 'mama', password = 'mama9453') {
|
||||||
|
await page.goto('/');
|
||||||
|
await page.fill('#username', username);
|
||||||
|
await page.fill('#password', password);
|
||||||
|
await page.getByRole('button', { name: '登入系統' }).click();
|
||||||
|
// 等待儀表板載入完成
|
||||||
|
await page.waitForSelector('text=系統概況', { timeout: 10000 });
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3.2 使用方式
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { login } from './helpers/auth';
|
||||||
|
|
||||||
|
test('應顯示庫存清單', async ({ page }) => {
|
||||||
|
await login(page);
|
||||||
|
await page.goto('/inventory/stock-query');
|
||||||
|
// ...斷言
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 測試撰寫規範
|
||||||
|
|
||||||
|
### 4.1 測試結構模板
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { login } from './helpers/auth';
|
||||||
|
|
||||||
|
test.describe('模組功能名稱', () => {
|
||||||
|
|
||||||
|
// 若整個 describe 都需要登入,使用 beforeEach
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await login(page);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('應正確顯示頁面標題與關鍵元素', async ({ page }) => {
|
||||||
|
await page.goto('/target-page');
|
||||||
|
|
||||||
|
// 驗證頁面標題
|
||||||
|
await expect(page.getByText('頁面標題')).toBeVisible();
|
||||||
|
|
||||||
|
// 驗證表格存在
|
||||||
|
await expect(page.locator('table')).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('應能執行 CRUD 操作', async ({ page }) => {
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4.2 斷言 (Assertions) 慣例
|
||||||
|
|
||||||
|
| 場景 | 優先使用 | 避免使用 |
|
||||||
|
|---|---|---|
|
||||||
|
| 驗證頁面載入 | `page.getByText('關鍵文字')` | `page.waitForURL()` ※ |
|
||||||
|
| 驗證元素存在 | `expect(locator).toBeVisible()` | `.count() > 0` |
|
||||||
|
| 驗證表格資料 | `page.locator('table tbody tr')` | 硬編碼行數 |
|
||||||
|
| 等待操作完成 | `expect().toBeVisible({ timeout })` | `page.waitForTimeout()` |
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> ※ Star ERP 使用 Inertia.js,頁面導航不一定改變 URL(例如儀表板路由為 `/`)。
|
||||||
|
> 因此**優先使用頁面內容驗證**,而非依賴 URL 變化。
|
||||||
|
|
||||||
|
### 4.3 選擇器優先順序
|
||||||
|
|
||||||
|
依照 Playwright 官方建議,選擇器優先順序為:
|
||||||
|
|
||||||
|
1. **Role** — `page.getByRole('button', { name: '登入系統' })`
|
||||||
|
2. **Text** — `page.getByText('系統概況')`
|
||||||
|
3. **Label** — `page.getByLabel('帳號')`
|
||||||
|
4. **Placeholder** — `page.getByPlaceholder('請輸入...')`
|
||||||
|
5. **Test ID** — `page.getByTestId('submit-btn')`(需在元件加 `data-testid`)
|
||||||
|
6. **CSS** — `page.locator('#username')`(最後手段)
|
||||||
|
|
||||||
|
### 4.4 禁止事項
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ❌ 禁止:硬等待(不可預期的等待時間)
|
||||||
|
await page.waitForTimeout(5000);
|
||||||
|
|
||||||
|
// ✅ 正確:等待特定條件
|
||||||
|
await expect(page.getByText('操作成功')).toBeVisible({ timeout: 5000 });
|
||||||
|
|
||||||
|
// ❌ 禁止:在測試中寫死測試資料的 ID
|
||||||
|
await page.goto('/products/42/edit');
|
||||||
|
|
||||||
|
// ✅ 正確:從頁面互動導航
|
||||||
|
await page.locator('table tbody tr').first().getByRole('button', { name: '編輯' }).click();
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 截圖與視覺回歸
|
||||||
|
|
||||||
|
### 5.1 手動截圖(文件用途)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 成功截圖存於 e2e/screenshots/
|
||||||
|
await page.screenshot({
|
||||||
|
path: 'e2e/screenshots/inventory-list.png',
|
||||||
|
fullPage: true,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5.2 視覺回歸測試(偵測 UI 變化)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
test('庫存頁面 UI 應保持一致', async ({ page }) => {
|
||||||
|
await login(page);
|
||||||
|
await page.goto('/inventory/stock-query');
|
||||||
|
// 比對截圖,pixel 級差異會報錯
|
||||||
|
await expect(page).toHaveScreenshot('stock-query.png', {
|
||||||
|
maxDiffPixelRatio: 0.01, // 容許 1% 差異(動態資料)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> 首次執行 `toHaveScreenshot()` 會自動建立基準截圖。
|
||||||
|
> 後續執行會與基準比對,更新基準用:`npx playwright test --update-snapshots`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 執行指令速查
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 執行所有 E2E 測試
|
||||||
|
npx playwright test
|
||||||
|
|
||||||
|
# 執行特定模組測試
|
||||||
|
npx playwright test e2e/login.spec.ts
|
||||||
|
|
||||||
|
# UI 互動模式(可視化瀏覽器操作)
|
||||||
|
npx playwright test --ui
|
||||||
|
|
||||||
|
# 帶頭模式(顯示瀏覽器畫面)
|
||||||
|
npx playwright test --headed
|
||||||
|
|
||||||
|
# 產生 HTML 報告並開啟
|
||||||
|
npx playwright test --reporter=html
|
||||||
|
npx playwright show-report
|
||||||
|
|
||||||
|
# 更新視覺回歸基準截圖
|
||||||
|
npx playwright test --update-snapshots
|
||||||
|
|
||||||
|
# 只執行特定測試案例(用 -g 篩選名稱)
|
||||||
|
npx playwright test -g "登入"
|
||||||
|
|
||||||
|
# Debug 模式(逐步執行)
|
||||||
|
npx playwright test --debug
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 開發檢核清單 (Checklist)
|
||||||
|
|
||||||
|
### 新增頁面或功能時:
|
||||||
|
|
||||||
|
- [ ] 是否已為新頁面建立對應的 `.spec.ts` 測試檔?
|
||||||
|
- [ ] 測試是否覆蓋主要的 Happy Path(正常操作流程)?
|
||||||
|
- [ ] 測試是否覆蓋關鍵的 Error Path(錯誤處理)?
|
||||||
|
- [ ] 共用的登入步驟是否使用 `helpers/auth.ts`?
|
||||||
|
- [ ] 斷言是否優先使用頁面內容而非 URL?
|
||||||
|
- [ ] 選擇器是否遵循優先順序(Role > Text > Label > CSS)?
|
||||||
|
- [ ] 測試是否可獨立執行(不依賴其他測試的狀態)?
|
||||||
|
|
||||||
|
### 提交程式碼前:
|
||||||
|
|
||||||
|
- [ ] 全部 E2E 測試是否通過?(`npx playwright test`)
|
||||||
|
- [ ] 是否有遺留的 `test.only` 或 `test.skip`?
|
||||||
51
.agents/skills/git-workflows/SKILL.md
Normal file
51
.agents/skills/git-workflows/SKILL.md
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
---
|
||||||
|
name: Git 分支管理與開發規範 (Git Workflow)
|
||||||
|
description: 規範開發過程中的 Git 分支架構、合併限制、環境部署流程以及提交訊息格式。
|
||||||
|
---
|
||||||
|
|
||||||
|
# Git 分支管理與開發規範 (Git Workflow)
|
||||||
|
|
||||||
|
為了確保系統穩定性與發布紀律,所有開發者與 AI 助手必須嚴格遵守以下環境發布流程與時段限制。
|
||||||
|
|
||||||
|
## 1. 分支架構與環境定義
|
||||||
|
|
||||||
|
| 分支 | 環境 | 用途描述 | 合併來源 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| **`dev`** | 本機開發 | 日常開發與功能實作。 | `feature/*` |
|
||||||
|
| **`demo`** | 測試/預佈署 | 鏡像生產環境。用於正式上線前的最終驗證。 | `dev` |
|
||||||
|
| **`main`** | 生產環境 | 正式版本分支。僅存放透過 `demo` 驗證後的代碼。 | `demo` |
|
||||||
|
|
||||||
|
## 2. 發布時段與約束 (Release Window)
|
||||||
|
|
||||||
|
### Main 分支發布限制 (Mandatory)
|
||||||
|
1. **強制規範**:若執行推送/合併指令時未明確包含目標分支,**嚴禁** 自行預設或推論為 `main`。我必須先詢問使用者:「請問要推送到哪一個目標分支?(dev / demo / main)」。
|
||||||
|
2. **標準發布時間**:週一至週四,**12:00 (中午) 之前**。
|
||||||
|
3. **非標準時段提醒**:若於上述時段以外(週五、週末、國定假日或下班時間)欲合併至 `main`:
|
||||||
|
- AI 助手**必須攔截並主動提示風險**(例如:週末災難風險)。
|
||||||
|
- 必須取得使用者明確書面同意(如:「我確定現在要上線」)方可執行。
|
||||||
|
4. **合併鏈路**:一般功能/修正必須先上 `demo` 測試。`main` 的程式原則上應從 `demo` 分支合併而來。
|
||||||
|
|
||||||
|
## 3. 開發與修復流程 (SOP)
|
||||||
|
|
||||||
|
### 標準開發流程
|
||||||
|
1. `feature/*` -> `dev` (隨時合併,主要測試點)。
|
||||||
|
2. `dev` -> `demo` (隨時合併,進行類生產環境測試)。
|
||||||
|
3. `demo` -> `main` (僅限允許時段進行,正式上線)。
|
||||||
|
|
||||||
|
### 緊急修復流程 (Hotfix)
|
||||||
|
1. 直接從 `main` 建立 `hotfix/*` 分支進行修復。
|
||||||
|
2. 修復完成並通過測試後合併回 `main`。
|
||||||
|
3. **重要同步**:修復後的程式碼必須立即合併回 `demo` 與 `dev`,確保各環境修復同步。
|
||||||
|
|
||||||
|
## 4. 提交訊息規範 (Commit Messages)
|
||||||
|
|
||||||
|
提交訊息必須包含以下前綴:
|
||||||
|
- `[FIX]`:修復 Bug。
|
||||||
|
- `[FEAT]`:新增功能。
|
||||||
|
- `[DOCS]`:文件更新。
|
||||||
|
- `[STYLE]`:UI/格式調整。
|
||||||
|
- `[REFACTOR]`:重構。
|
||||||
|
|
||||||
|
---
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> 身為 AI 助手 (Antigravity),我會監控合併對象與當前時間。若您的命令涉及合併至 `main` 且不在允許時段內,我會優先進行安全提醒。
|
||||||
206
.agents/skills/permission-management/SKILL.md
Normal file
206
.agents/skills/permission-management/SKILL.md
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
---
|
||||||
|
name: 權限管理與實作規範
|
||||||
|
description: 為新功能實作權限控制的完整流程規範,包含後端 Seeder 設定、Middleware 路由保護與前端權限判斷。
|
||||||
|
---
|
||||||
|
|
||||||
|
# 權限管理與實作規範
|
||||||
|
|
||||||
|
本文件說明如何在新增功能時,一併實作完整的權限控制機制。專案採用 `spatie/laravel-permission` 套件進行權限管理。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 定義權限 (Backend Seeder)
|
||||||
|
|
||||||
|
所有權限皆定義於 `database/seeders/PermissionSeeder.php`。
|
||||||
|
|
||||||
|
### 步驟:
|
||||||
|
|
||||||
|
1. 開啟 `database/seeders/PermissionSeeder.php`。
|
||||||
|
2. 在 `$permissions` 關聯陣列中新增功能對應的權限。
|
||||||
|
* **命名慣例**:`{resource}.{action}`(例如:`system.view_logs`, `products.create`)
|
||||||
|
* **格式**:`'權限字串' => '中文動作名稱'`
|
||||||
|
* 常用動作:`view`, `create`, `edit`, `delete`, `approve`, `cancel`, `export`
|
||||||
|
3. 在下方「角色分配」區段,將新權限分配給適合的角色。
|
||||||
|
|
||||||
|
### 範例:
|
||||||
|
|
||||||
|
```php
|
||||||
|
// 1. 新增權限(注意:是 key => value 格式)
|
||||||
|
$permissions = [
|
||||||
|
// ... 現有權限
|
||||||
|
'utility_fees.view' => '檢視',
|
||||||
|
'utility_fees.create' => '建立',
|
||||||
|
'utility_fees.edit' => '編輯',
|
||||||
|
'utility_fees.delete' => '刪除',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 2. 分配給角色
|
||||||
|
$admin->givePermissionTo([
|
||||||
|
// ... 現有權限
|
||||||
|
'utility_fees.view', 'utility_fees.create', 'utility_fees.edit', 'utility_fees.delete',
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 現有角色定義:
|
||||||
|
|
||||||
|
| 角色 | 說明 | 權限範圍 |
|
||||||
|
|---|---|---|
|
||||||
|
| `super-admin` | 系統管理員 | 自動擁有所有權限(`Permission::all()`) |
|
||||||
|
| `admin` | 一般管理員 | 大部分權限(除角色管理外) |
|
||||||
|
| `warehouse-manager` | 倉庫管理員 | 庫存、盤點、調撥、進貨、門市叫貨 |
|
||||||
|
| `purchaser` | 採購人員 | 商品檢視、採購單、退貨、供應商、進貨 |
|
||||||
|
| `viewer` | 檢視人員 | 僅限各模組的 `.view` 權限 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 套用資料庫變更 (Multi-tenancy)
|
||||||
|
|
||||||
|
修改 Seeder 後,必須在**中央與所有租戶**同步執行。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 對所有租戶執行 Seeder
|
||||||
|
./vendor/bin/sail php artisan tenants:seed --class=PermissionSeeder
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> 僅執行 `db:seed` 只會更新中央資料庫。務必使用 `tenants:seed` 確保所有租戶同步。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 路由保護 (Backend Middleware)
|
||||||
|
|
||||||
|
路由保護定義在各模組自己的 `app/Modules/{ModuleName}/Routes/web.php` 中。
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> 路由檔在各模組內(如 `app/Modules/Finance/Routes/web.php`),**不是**全域的 `routes/web.php`。
|
||||||
|
|
||||||
|
### 範例:
|
||||||
|
|
||||||
|
```php
|
||||||
|
// 單一權限保護
|
||||||
|
Route::middleware('permission:utility_fees.view')->group(function () {
|
||||||
|
Route::get('/utility-fees', [UtilityFeeController::class, 'index'])->name('utility-fees.index');
|
||||||
|
Route::get('/utility-fees/{utilityFee}', [UtilityFeeController::class, 'show'])->name('utility-fees.show');
|
||||||
|
});
|
||||||
|
|
||||||
|
// 巢狀權限群組
|
||||||
|
Route::middleware('permission:utility_fees.create')->group(function () {
|
||||||
|
Route::get('/utility-fees/create', [UtilityFeeController::class, 'create'])->name('utility-fees.create');
|
||||||
|
Route::post('/utility-fees', [UtilityFeeController::class, 'store'])->name('utility-fees.store');
|
||||||
|
});
|
||||||
|
|
||||||
|
// 單行 middleware
|
||||||
|
Route::delete('/utility-fees/{utilityFee}', [UtilityFeeController::class, 'destroy'])
|
||||||
|
->middleware('permission:utility_fees.delete')
|
||||||
|
->name('utility-fees.destroy');
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 配置權限群組名稱 (Backend UI Config)
|
||||||
|
|
||||||
|
為了讓新權限在「角色與權限」管理介面中正確分組並顯示中文標題,需修改 Controller。
|
||||||
|
|
||||||
|
**位置**: `app/Modules/Core/Controllers/RoleController.php` → `getGroupedPermissions()`
|
||||||
|
|
||||||
|
```php
|
||||||
|
$groupDefinitions = [
|
||||||
|
'products' => '商品資料管理',
|
||||||
|
'warehouses' => '倉庫管理',
|
||||||
|
'inventory' => '庫存資料管理',
|
||||||
|
// ...
|
||||||
|
'utility_fees' => '公共事業費管理', // ✅ 新增此行
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> 未加入 `$groupDefinitions` 的權限群組仍會顯示,但標題會以原始 key(英文)呈現。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 前端權限判斷 (React)
|
||||||
|
|
||||||
|
### 5.1 方式一:`usePermission` Hook(在邏輯中判斷)
|
||||||
|
|
||||||
|
**位置**: `resources/js/hooks/usePermission.ts`
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { usePermission } from "@/hooks/usePermission";
|
||||||
|
|
||||||
|
export default function ProductIndex() {
|
||||||
|
const { can, canAny, isSuperAdmin } = usePermission();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{can('products.create') && <Button>新增商品</Button>}
|
||||||
|
{canAny(['products.edit', 'products.delete']) && <ManageDropdown />}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Hook 完整介面:
|
||||||
|
|
||||||
|
| 方法 | 說明 |
|
||||||
|
|---|---|
|
||||||
|
| `can(permission)` | 檢查是否擁有**指定**權限 |
|
||||||
|
| `canAny(permissions[])` | 檢查是否擁有**任一**權限 |
|
||||||
|
| `canAll(permissions[])` | 檢查是否擁有**所有**權限 |
|
||||||
|
| `hasRole(role)` | 檢查是否擁有**指定**角色 |
|
||||||
|
| `hasAnyRole(roles[])` | 檢查是否擁有**任一**角色 |
|
||||||
|
| `hasAllRoles(roles[])` | 檢查是否擁有**所有**角色 |
|
||||||
|
| `isSuperAdmin()` | 是否為超級管理員 |
|
||||||
|
|
||||||
|
> 所有方法對 `super-admin` 角色自動回傳 `true`。
|
||||||
|
|
||||||
|
### 5.2 方式二:`<Can>` / `<HasRole>` / `<CanAll>` 元件(在 JSX 中包裹)
|
||||||
|
|
||||||
|
**位置**: `resources/js/Components/Permission/Can.tsx`
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { Can, HasRole, CanAll } from '@/Components/Permission/Can';
|
||||||
|
|
||||||
|
// 單一權限
|
||||||
|
<Can permission="products.create">
|
||||||
|
<Button>新增商品</Button>
|
||||||
|
</Can>
|
||||||
|
|
||||||
|
// 任一權限(OR 邏輯)
|
||||||
|
<Can permission={['products.edit', 'products.delete']}>
|
||||||
|
<ManageDropdown />
|
||||||
|
</Can>
|
||||||
|
|
||||||
|
// 所有權限都必須有(AND 邏輯)
|
||||||
|
<CanAll permissions={['products.edit', 'products.delete']}>
|
||||||
|
<Button>完整管理</Button>
|
||||||
|
</CanAll>
|
||||||
|
|
||||||
|
// 角色判斷
|
||||||
|
<HasRole role="admin">
|
||||||
|
<Link href="/admin">管理後台</Link>
|
||||||
|
</HasRole>
|
||||||
|
|
||||||
|
// Fallback 支援
|
||||||
|
<Can permission="products.delete" fallback={<span className="text-gray-400">無權限</span>}>
|
||||||
|
<Button variant="destructive">刪除</Button>
|
||||||
|
</Can>
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> UI 規範要求:所有可操作按鈕(新增、編輯、刪除)**必須**包裹 `<Can>` 元件或使用 `can()` 判斷。
|
||||||
|
> 詳見 [UI 統一規範](file:///home/mama/projects/star-erp/.agents/skills/ui-consistency/SKILL.md)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 開發檢核清單 (Checklist)
|
||||||
|
|
||||||
|
### 後端
|
||||||
|
- [ ] `PermissionSeeder.php` 已新增權限字串(`'key' => '中文動作名稱'` 格式)。
|
||||||
|
- [ ] `PermissionSeeder.php` 已將新權限分配給 `admin` 及其他適用角色。
|
||||||
|
- [ ] 已執行 `./vendor/bin/sail php artisan tenants:seed --class=PermissionSeeder` 同步所有租戶。
|
||||||
|
- [ ] `RoleController.php` 的 `$groupDefinitions` 已新增權限群組中文名稱。
|
||||||
|
- [ ] 模組路由 (`app/Modules/{ModuleName}/Routes/web.php`) 已加上 `middleware('permission:...')` 保護。
|
||||||
|
|
||||||
|
### 前端
|
||||||
|
- [ ] 頁面按鈕已使用 `usePermission` Hook 或 `<Can>` 元件進行權限控制。
|
||||||
|
- [ ] 所有可操作按鈕都包裹於權限判斷中(符合 UI 統一規範)。
|
||||||
@@ -80,7 +80,7 @@ tooltip
|
|||||||
<div style={{ borderColor: 'var(--primary-main)' }}>...</div>
|
<div style={{ borderColor: 'var(--primary-main)' }}>...</div>
|
||||||
|
|
||||||
// ❌ 錯誤:寫死色碼 (會導致租戶無法換色)
|
// ❌ 錯誤:寫死色碼 (會導致租戶無法換色)
|
||||||
<div className="text-[#01ab83]">...</div>
|
<div className="text-primary-main">...</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2.2 灰階 (Grey Scale)
|
### 2.2 灰階 (Grey Scale)
|
||||||
@@ -319,7 +319,7 @@ import { Plus, Pencil, Trash2, Users } from 'lucide-react';
|
|||||||
|
|
||||||
// 頁面標題
|
// 頁面標題
|
||||||
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
||||||
<Users className="h-6 w-6 text-[#01ab83]" />
|
<Users className="h-6 w-6 text-primary-main" />
|
||||||
使用者管理
|
使用者管理
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
@@ -584,7 +584,7 @@ export default function ResourceIndex() {
|
|||||||
<div className="flex items-center justify-between mb-6">
|
<div className="flex items-center justify-between mb-6">
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
||||||
<IconComponent className="h-6 w-6 text-[#01ab83]" />
|
<IconComponent className="h-6 w-6 text-primary-main" />
|
||||||
頁面標題
|
頁面標題
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-gray-500 mt-1">
|
<p className="text-gray-500 mt-1">
|
||||||
@@ -781,7 +781,75 @@ import { SearchableSelect } from "@/Components/ui/searchable-select";
|
|||||||
- **Select / SearchableSelect**: 必須確保 Trigger 按鈕高度為 `h-9`
|
- **Select / SearchableSelect**: 必須確保 Trigger 按鈕高度為 `h-9`
|
||||||
- **禁止使用**: 除非有特殊設計需求,否則避免使用 `h-10` (40px) 或其他非標準高度。
|
- **禁止使用**: 除非有特殊設計需求,否則避免使用 `h-10` (40px) 或其他非標準高度。
|
||||||
|
|
||||||
## 11.6 日期輸入框樣式 (Date Input Style)
|
## 11.6 數字輸入框規範 (Numeric Inputs)
|
||||||
|
|
||||||
|
當需求為輸入**整數**數量(例如:實際產出數量、標準產出量)時,**嚴禁自行開發組合 `Plus` (+) 與 `Minus` (-) 按鈕的複合元件**。
|
||||||
|
|
||||||
|
**必須使用原生 HTML5 數字輸入與屬性**:
|
||||||
|
1. 使用 `<Input type="number" />` 確保預設渲染瀏覽器原生的上下調整小箭頭 (Spinner)。
|
||||||
|
2. 針對整數需求,固定加上 `step="1"` 屬性。
|
||||||
|
3. 視需求加上 `min` 與 `max` 控制上下限。
|
||||||
|
|
||||||
|
這樣既能保持與現有「新增配方」等模組的「標準產出量」欄位行為高度一致,亦能維持畫面的極簡風格。
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
// ✅ 正確:依賴原生行為
|
||||||
|
<Input
|
||||||
|
type="number"
|
||||||
|
step="1"
|
||||||
|
min="0"
|
||||||
|
max={outputQuantity}
|
||||||
|
value={actualOutputQuantity}
|
||||||
|
onChange={(e) => setActualOutputQuantity(e.target.value)}
|
||||||
|
className="h-9 w-24 text-center"
|
||||||
|
/>
|
||||||
|
|
||||||
|
// ❌ 錯誤:過度設計、浪費空間與破壞一致性
|
||||||
|
<div className="flex">
|
||||||
|
<Button><Minus /></Button>
|
||||||
|
<Input type="number" />
|
||||||
|
<Button><Plus /></Button>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
## 11.7 日期顯示規範 (Date Display)
|
||||||
|
|
||||||
|
前端顯示日期時**禁止直接顯示原始 ISO 字串**(如 `2024-03-06T08:30:00.000000Z`),必須使用 `resources/js/lib/date.ts` 提供的工具函式。
|
||||||
|
|
||||||
|
### 可用函式
|
||||||
|
|
||||||
|
| 函式 | 說明 | 輸出範例 |
|
||||||
|
|---|---|---|
|
||||||
|
| `formatDate(dateStr)` | **智慧格式**:自動判斷是否包含時間 | `2024-03-06` 或 `2024-03-06 08:30:00` |
|
||||||
|
| `formatDate(dateStr, 'yyyy-MM-dd')` | 指定格式輸出 | `2024-03-06` |
|
||||||
|
| `formatDateOnly(dateStr)` | 強制僅顯示日期 | `2024-03-06` |
|
||||||
|
|
||||||
|
### 智慧格式切換邏輯
|
||||||
|
|
||||||
|
`formatDate` 會自動判斷原始資料:
|
||||||
|
- 若時間部分為 `00:00:00`(通常代表後端僅提供日期)→ 僅顯示 `YYYY-MM-DD`
|
||||||
|
- 若時間部分有值 → 顯示 `YYYY-MM-DD HH:mm:ss`
|
||||||
|
- 若輸入為 `null` / `undefined` / 無效字串 → 顯示 `"-"`
|
||||||
|
|
||||||
|
### 使用範例
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
import { formatDate, formatDateOnly } from "@/lib/date";
|
||||||
|
|
||||||
|
// ✅ 正確:使用 formatDate 自動判斷
|
||||||
|
<span>{formatDate(item.created_at)}</span> // → "2024-03-06 08:30:00"
|
||||||
|
<span>{formatDate(item.transaction_date)}</span> // → "2024-03-06"(因為時間為 00:00:00)
|
||||||
|
|
||||||
|
// ✅ 正確:強制只顯示日期
|
||||||
|
<span>{formatDateOnly(item.due_date)}</span> // → "2024-03-06"
|
||||||
|
|
||||||
|
// ❌ 禁止:直接顯示原始 ISO 字串
|
||||||
|
<span>{item.created_at}</span> // → "2024-03-06T08:30:00.000000Z" 😱
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11.7 日期輸入框樣式 (Date Input Style)
|
||||||
|
|
||||||
日期輸入框應採用「**左側裝飾圖示 + 右側原生操作**」的配置,以保持視覺一致性並保留瀏覽器原生便利性。
|
日期輸入框應採用「**左側裝飾圖示 + 右側原生操作**」的配置,以保持視覺一致性並保留瀏覽器原生便利性。
|
||||||
|
|
||||||
@@ -805,7 +873,7 @@ import { Input } from "@/Components/ui/input";
|
|||||||
</div>
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
## 11.7 搜尋選單樣式 (SearchableSelect Style)
|
## 11.8 搜尋選單樣式 (SearchableSelect Style)
|
||||||
|
|
||||||
`SearchableSelect` 元件在表單或篩選列中使用時,高度必須設定為 `h-9` 以與輸入框對齊。
|
`SearchableSelect` 元件在表單或篩選列中使用時,高度必須設定為 `h-9` 以與輸入框對齊。
|
||||||
|
|
||||||
@@ -816,7 +884,7 @@ import { Input } from "@/Components/ui/input";
|
|||||||
/>
|
/>
|
||||||
```
|
```
|
||||||
|
|
||||||
## 11.8 篩選列規範 (Filter Bar Norms)
|
## 11.9 篩選列規範 (Filter Bar Norms)
|
||||||
|
|
||||||
列表頁面的篩選區域(Filter Bar)應遵循以下規範以節省空間並保持層級清晰:
|
列表頁面的篩選區域(Filter Bar)應遵循以下規範以節省空間並保持層級清晰:
|
||||||
|
|
||||||
35
.agents/workflows/now-push.md
Normal file
35
.agents/workflows/now-push.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
---
|
||||||
|
description: 將目前的變更提交並推送至指定的遠端分支 (遵守專案規範)
|
||||||
|
---
|
||||||
|
|
||||||
|
# 快速推送工作流 (now-push)
|
||||||
|
|
||||||
|
本工作流旨在規範化 Git 提交與推送流程,確保符合專案的開發規範 (繁體中文、規範前綴) 與發布紀律 (Release Window)。
|
||||||
|
|
||||||
|
## 執行步驟
|
||||||
|
|
||||||
|
1. **讀取規範 (Mandatory)**
|
||||||
|
在執行任何 Git 操作前,**必須** 先讀取 Git 分支管理與開發規範:
|
||||||
|
`view_file` -> [Git SKILL.md](file:///home/mama/projects/star-erp/.agents/skills/git-workflows/SKILL.md)
|
||||||
|
|
||||||
|
2. **檢查與準備**
|
||||||
|
- 執行 `git status` 檢查目前工作目錄。
|
||||||
|
- 根據 **SKILL.md** 的規範撰寫繁體中文提交訊息。
|
||||||
|
|
||||||
|
3. **目標分支安全檢查**
|
||||||
|
- 嚴格遵守 **SKILL.md** 中的「分支架構」、「發布時段」與「強制分支明確指定」規則。
|
||||||
|
- 若未指明目標分支,主動詢問使用者,不可私自預設為 `main`。
|
||||||
|
- **【最嚴格限制】**:`main` 分支的程式碼**只能**, **必須**從 `demo` 分支合併而來。絕對禁止將 `dev` (或 `feature/*`) 直接合併進 `main`。
|
||||||
|
|
||||||
|
4. **執行推送 (Push) 與嚴格合併鏈路**
|
||||||
|
- **若目標為 `dev`**:直接 `git push origin [目前分支]:dev` 或 commit 後 merge 到 dev。
|
||||||
|
- **若目標為 `demo`**:必須先確保變更已在 `dev` 且無衝突,然後 `git checkout demo && git merge dev && git push origin demo`。
|
||||||
|
- **若目標為 `main`**:
|
||||||
|
必須確保變更已經依照順序通過前置環境,嚴格執行以下流程(缺一不可):
|
||||||
|
1. `git checkout dev && git merge [目前分支] && git push origin dev`
|
||||||
|
2. `git checkout demo && git merge dev && git push origin demo`
|
||||||
|
3. `git checkout main && git merge demo && git push origin main`
|
||||||
|
*(就算遭遇衝突,也必須在對應的分支上解衝突,絕對不可略過 `demo` 直接 `dev -> main`)*
|
||||||
|
|
||||||
|
5. **後續同步 (針對 Hotfix)**
|
||||||
|
- 依照 **SKILL.md** 的「緊急修復流程(Hotfix)」:若有從 main 開出來的 hotfix 分支直接併回 main 的例外情況(需使用者明確指示),**必須**同步將 main 分支 merge 回 `demo` 與 `dev` 分支,維持全環境版本一致。
|
||||||
@@ -1,158 +0,0 @@
|
|||||||
---
|
|
||||||
name: 操作紀錄實作規範
|
|
||||||
description: 規範系統內 Activity Log 的實作標準,包含後端資料過濾、快照策略、與前端顯示邏輯。
|
|
||||||
---
|
|
||||||
|
|
||||||
# 操作紀錄實作規範
|
|
||||||
|
|
||||||
本文件說明如何在開發新功能時,依據系統規範實作 `spatie/laravel-activitylog` 操作紀錄,確保資料儲存效率與前端顯示一致性。
|
|
||||||
|
|
||||||
## 1. 後端實作標準 (Backend)
|
|
||||||
|
|
||||||
所有 Model 之操作紀錄應遵循「僅儲存變動資料」與「保留關鍵快照」兩大原則。
|
|
||||||
|
|
||||||
### 1.1 啟用 Activity Log
|
|
||||||
|
|
||||||
在 Model 中引用 `LogsActivity` trait 並實作 `getActivitylogOptions` 方法。
|
|
||||||
|
|
||||||
```php
|
|
||||||
use Spatie\Activitylog\Traits\LogsActivity;
|
|
||||||
use Spatie\Activitylog\LogOptions;
|
|
||||||
|
|
||||||
class Product extends Model
|
|
||||||
{
|
|
||||||
use LogsActivity;
|
|
||||||
|
|
||||||
public function getActivitylogOptions(): LogOptions
|
|
||||||
{
|
|
||||||
return LogOptions::defaults()
|
|
||||||
->logAll()
|
|
||||||
->logOnlyDirty() // ✅ 關鍵:只記錄有變動的欄位
|
|
||||||
->dontSubmitEmptyLogs(); // 若無變動則不記錄
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 1.2 手動記錄 (Manual Logging)
|
|
||||||
|
|
||||||
若需在 Controller 手動記錄(例如需客製化邏輯),**必須**自行實作變動過濾,不可直接儲存所有屬性。
|
|
||||||
|
|
||||||
**錯誤範例 (Do NOT do this):**
|
|
||||||
```php
|
|
||||||
// ❌ 錯誤:這會導致每次更新都記錄所有欄位,即使它們沒變
|
|
||||||
activity()
|
|
||||||
->withProperties(['attributes' => $newAttributes, 'old' => $oldAttributes])
|
|
||||||
->log('updated');
|
|
||||||
```
|
|
||||||
|
|
||||||
**正確範例 (Do this):**
|
|
||||||
```php
|
|
||||||
// ✅ 正確:自行比對差異,只存變動值
|
|
||||||
$changedAttributes = [];
|
|
||||||
$changedOldAttributes = [];
|
|
||||||
|
|
||||||
foreach ($newAttributes as $key => $value) {
|
|
||||||
if ($value != ($oldAttributes[$key] ?? null)) {
|
|
||||||
$changedAttributes[$key] = $value;
|
|
||||||
$changedOldAttributes[$key] = $oldAttributes[$key] ?? null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($changedAttributes)) {
|
|
||||||
activity()
|
|
||||||
->withProperties(['attributes' => $changedAttributes, 'old' => $changedOldAttributes])
|
|
||||||
->log('updated');
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 1.3 快照策略 (Snapshot Strategy)
|
|
||||||
|
|
||||||
為確保資料被刪除後仍能辨識操作對象,**必須**在 `properties.snapshot` 中儲存關鍵識別資訊(如名稱、代號、類別名稱)。
|
|
||||||
|
|
||||||
**主要方式:使用 `tapActivity` (推薦)**
|
|
||||||
|
|
||||||
```php
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
|
||||||
{
|
|
||||||
$properties = $activity->properties;
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
|
||||||
|
|
||||||
// 保存關鍵關聯名稱 (避免關聯資料刪除後 ID 失效)
|
|
||||||
$snapshot['category_name'] = $this->category ? $this->category->name : null;
|
|
||||||
$snapshot['po_number'] = $this->code; // 儲存單號
|
|
||||||
|
|
||||||
// 保存自身名稱 (Context)
|
|
||||||
$snapshot['name'] = $this->name;
|
|
||||||
|
|
||||||
$properties['snapshot'] = $snapshot;
|
|
||||||
$activity->properties = $properties;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 2. 顯示名稱映射 (UI Mapping)
|
|
||||||
|
|
||||||
### 2.1 對象名稱映射 (Mapping)
|
|
||||||
|
|
||||||
需在 `ActivityLogController.php` 中設定 Model 與中文名稱的對應,讓前端列表能顯示中文對象(如「公共事業費」而非 `UtilityFee`)。
|
|
||||||
|
|
||||||
**位置**: `app/Http/Controllers/Admin/ActivityLogController.php`
|
|
||||||
|
|
||||||
```php
|
|
||||||
protected function getSubjectMap()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'App\Modules\Inventory\Models\Product' => '商品',
|
|
||||||
'App\Modules\Finance\Models\UtilityFee' => '公共事業費', // ✅ 新增映射
|
|
||||||
];
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2.2 欄位名稱中文化 (Field Translation)
|
|
||||||
|
|
||||||
需在前端 `ActivityDetailDialog` 中設定欄位名稱的中文翻譯。
|
|
||||||
|
|
||||||
**位置**: `resources/js/Components/ActivityLog/ActivityDetailDialog.tsx`
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const fieldLabels: Record<string, string> = {
|
|
||||||
// ... 既有欄位
|
|
||||||
'transaction_date': '費用日期',
|
|
||||||
'category': '費用類別',
|
|
||||||
'amount': '金額',
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
## 3. 前端顯示邏輯 (Frontend)
|
|
||||||
|
|
||||||
### 3.1 列表描述生成 (Description Generation)
|
|
||||||
|
|
||||||
前端 `LogTable.tsx` 會依據 `properties.snapshot` 中的欄位自動組建描述(例如:「Admin 新增 電話費 公共事業費」)。
|
|
||||||
|
|
||||||
若您的 Model 使用了特殊的識別欄位(例如 `category`),**必須**將其加入 `nameParams` 陣列中。
|
|
||||||
|
|
||||||
**位置**: `resources/js/Components/ActivityLog/LogTable.tsx`
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const nameParams = [
|
|
||||||
'po_number', 'name', 'code',
|
|
||||||
'category_name',
|
|
||||||
'category' // ✅ 確保加入此欄位,前端才能抓到 $snapshot['category']
|
|
||||||
];
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3.2 詳情過濾邏輯
|
|
||||||
|
|
||||||
前端 `ActivityDetailDialog` 已內建智慧過濾邏輯:
|
|
||||||
- **Created**: 顯示初始化欄位。
|
|
||||||
- **Updated**: **僅顯示有變動的欄位** (由 `isChanged` 判斷)。
|
|
||||||
- **Deleted**: 顯示刪除前的完整資料。
|
|
||||||
|
|
||||||
開發者僅需確保傳入的 `attributes` 與 `old` 資料結構正確,過濾邏輯會自動運作。
|
|
||||||
|
|
||||||
## 檢核清單
|
|
||||||
|
|
||||||
- [ ] **Backend**: Model 是否已設定 `logOnlyDirty` 或手動實作過濾?
|
|
||||||
- [ ] **Backend**: 是否已透過 `tapActivity` 或手動方式記錄 Snapshot(關鍵名稱)?
|
|
||||||
- [ ] **Backend**: 是否已在 `ActivityLogController` 加入 Model 中文名稱映射?
|
|
||||||
- [ ] **Frontend**: 是否已在 `ActivityDetailDialog` 加入欄位中文翻譯?
|
|
||||||
- [ ] **Frontend**: 若使用特殊識別欄位,是否已加入 `LogTable` 的 `nameParams`?
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
---
|
|
||||||
name: 權限管理與實作規範
|
|
||||||
description: 為新功能實作權限控制的完整流程規範,包含後端 Seeder 設定、Middleware 路由保護與前端權限判斷。
|
|
||||||
---
|
|
||||||
|
|
||||||
# 權限管理與實作規範
|
|
||||||
|
|
||||||
本文件說明如何在新增功能時,一併實作完整的權限控制機制。專案採用 `spatie/laravel-permission` 套件進行權限管理。
|
|
||||||
|
|
||||||
## 1. 定義權限 (Backend)
|
|
||||||
|
|
||||||
所有權限皆定義於 `database/seeders/PermissionSeeder.php`。
|
|
||||||
|
|
||||||
### 步驟:
|
|
||||||
|
|
||||||
1. 開啟 `database/seeders/PermissionSeeder.php`。
|
|
||||||
2. 在 `$permissions` 陣列中新增功能對應的權限字串。
|
|
||||||
* **命名慣例**:`{resource}.{action}` (例如:`system.view_logs`, `products.create`)
|
|
||||||
* 常用動作:`view`, `create`, `edit`, `delete`, `publish`, `export`
|
|
||||||
3. 在下方「角色分配」區段,將新權限分配給適合的角色。
|
|
||||||
* `super-admin`:通常擁有所有權限(程式碼中 `Permission::all()` 自動涵蓋,無需手動新增)。
|
|
||||||
* `admin`:通常擁有大部分權限。
|
|
||||||
* 其他角色 (`warehouse-manager`, `purchaser`, `viewer`):依業務邏輯分配。
|
|
||||||
|
|
||||||
### 範例:
|
|
||||||
|
|
||||||
```php
|
|
||||||
// 1. 新增權限字串
|
|
||||||
$permissions = [
|
|
||||||
// ... 現有權限
|
|
||||||
'system.view_logs', // 新增:檢視系統日誌
|
|
||||||
];
|
|
||||||
|
|
||||||
// ...
|
|
||||||
|
|
||||||
// 2. 分配給角色
|
|
||||||
$admin->givePermissionTo([
|
|
||||||
// ... 現有權限
|
|
||||||
'system.view_logs',
|
|
||||||
]);
|
|
||||||
```
|
|
||||||
|
|
||||||
## 2. 套用資料庫變更
|
|
||||||
|
|
||||||
修改 Seeder 後,必須重新執行 Seeder 以將權限寫入資料庫。
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 對於所有租戶執行 Seeder (開發環境)
|
|
||||||
php artisan tenants:seed --class=PermissionSeeder
|
|
||||||
```
|
|
||||||
|
|
||||||
## 3. 路由保護 (Backend Middleware)
|
|
||||||
|
|
||||||
在 `routes/web.php` 中,使用 `permission:{name}` middleware 保護路由。
|
|
||||||
|
|
||||||
### 範例:
|
|
||||||
|
|
||||||
```php
|
|
||||||
// 單一權限保護
|
|
||||||
Route::get('/logs', [LogController::class, 'index'])
|
|
||||||
->middleware('permission:system.view_logs')
|
|
||||||
->name('logs.index');
|
|
||||||
|
|
||||||
// 路由群組保護
|
|
||||||
Route::middleware('permission:products.view')->group(function () {
|
|
||||||
// ...
|
|
||||||
});
|
|
||||||
|
|
||||||
// 多重權限 (OR 邏輯:有其一即可)
|
|
||||||
Route::middleware('permission:products.create|products.edit')->group(function () {
|
|
||||||
// ...
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## 4. 前端權限判斷 (React Component)
|
|
||||||
|
|
||||||
使用自訂 Hook `usePermission` 來控制 UI 元素的顯示(例如:隱藏沒有權限的按鈕)。
|
|
||||||
|
|
||||||
### 引入 Hook:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
import { usePermission } from "@/hooks/usePermission";
|
|
||||||
```
|
|
||||||
|
|
||||||
### 使用方式:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
export default function ProductIndex() {
|
|
||||||
const { can } = usePermission();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h1>商品列表</h1>
|
|
||||||
|
|
||||||
{/* 只有擁有 create 權限才顯示按鈕 */}
|
|
||||||
{can('products.create') && (
|
|
||||||
<Button>新增商品</Button>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* 組合判斷 */}
|
|
||||||
{can('products.edit') && <EditButton />}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 權限 Hook 介面說明:
|
|
||||||
|
|
||||||
- `can(permission: string)`: 檢查當前使用者是否擁有指定權限。
|
|
||||||
- `canAny(permissions: string[])`: 檢查當前使用者是否擁有陣列中**任一**權限。
|
|
||||||
- `hasRole(role: string)`: 檢查當前使用者是否擁有指定角色。
|
|
||||||
|
|
||||||
## 5. 配置權限群組名稱 (Backend UI Config)
|
|
||||||
|
|
||||||
為了讓新權限在「角色與權限」管理介面中顯示正確的中文分組標題,需修改 Controller 設定。
|
|
||||||
|
|
||||||
### 步驟:
|
|
||||||
|
|
||||||
1. 開啟 `app/Http/Controllers/Admin/RoleController.php`。
|
|
||||||
2. 找到 `getGroupedPermissions` 方法。
|
|
||||||
3. 在 `$groupDefinitions` 陣列中,新增 `{resource}` 對應的中文名稱。
|
|
||||||
|
|
||||||
### 範例:
|
|
||||||
|
|
||||||
```php
|
|
||||||
$groupDefinitions = [
|
|
||||||
'products' => '商品資料管理',
|
|
||||||
// ...
|
|
||||||
'utility_fees' => '公共事業費管理', // 新增此行
|
|
||||||
];
|
|
||||||
```
|
|
||||||
|
|
||||||
## 檢核清單
|
|
||||||
|
|
||||||
- [ ] `PermissionSeeder.php` 已新增權限字串。
|
|
||||||
- [ ] `PermissionSeeder.php` 已將新權限分配給對應角色。
|
|
||||||
- [ ] 已執行 `php artisan tenants:seed --class=PermissionSeeder` 更新資料庫。
|
|
||||||
- [ ] `RoleController.php` 已新增權限群組的中文名稱映射。
|
|
||||||
- [ ] 後端路由 (`routes/web.php`) 已加上 middleware 保護。
|
|
||||||
- [ ] 前端頁面/按鈕已使用 `usePermission` 進行顯示控制。
|
|
||||||
@@ -92,6 +92,7 @@ jobs:
|
|||||||
php artisan tenants:migrate --force &&
|
php artisan tenants:migrate --force &&
|
||||||
php artisan db:seed --force &&
|
php artisan db:seed --force &&
|
||||||
php artisan tenants:run db:seed --option=\"class=PermissionSeeder\" --option=\"force=true\" &&
|
php artisan tenants:run db:seed --option=\"class=PermissionSeeder\" --option=\"force=true\" &&
|
||||||
|
php artisan tenants:run db:seed --option=\"class=SystemSettingSeeder\" --option=\"force=true\" &&
|
||||||
php artisan permission:cache-reset &&
|
php artisan permission:cache-reset &&
|
||||||
php artisan optimize:clear &&
|
php artisan optimize:clear &&
|
||||||
php artisan optimize &&
|
php artisan optimize &&
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ jobs:
|
|||||||
php artisan tenants:migrate --force &&
|
php artisan tenants:migrate --force &&
|
||||||
php artisan db:seed --force &&
|
php artisan db:seed --force &&
|
||||||
php artisan tenants:run db:seed --option=\"class=PermissionSeeder\" --option=\"force=true\" &&
|
php artisan tenants:run db:seed --option=\"class=PermissionSeeder\" --option=\"force=true\" &&
|
||||||
|
php artisan tenants:run db:seed --option=\"class=SystemSettingSeeder\" --option=\"force=true\" &&
|
||||||
php artisan permission:cache-reset &&
|
php artisan permission:cache-reset &&
|
||||||
php artisan optimize:clear &&
|
php artisan optimize:clear &&
|
||||||
php artisan optimize &&
|
php artisan optimize &&
|
||||||
|
|||||||
14
.gitignore
vendored
14
.gitignore
vendored
@@ -18,6 +18,7 @@
|
|||||||
/public/storage
|
/public/storage
|
||||||
/storage/*.key
|
/storage/*.key
|
||||||
/storage/pail
|
/storage/pail
|
||||||
|
/storage/tenant*
|
||||||
/vendor
|
/vendor
|
||||||
Homestead.json
|
Homestead.json
|
||||||
Homestead.yaml
|
Homestead.yaml
|
||||||
@@ -29,3 +30,16 @@ Thumbs.db
|
|||||||
/docs/presentation
|
/docs/presentation
|
||||||
docs/Monthly_Report_2026_01.pptx
|
docs/Monthly_Report_2026_01.pptx
|
||||||
docs/f6_1770350984272.xlsx
|
docs/f6_1770350984272.xlsx
|
||||||
|
公共事業費-描述.md
|
||||||
|
.gitignore
|
||||||
|
BOM表自動計算成本.md
|
||||||
|
公共事業費-類別維護.md
|
||||||
|
|
||||||
|
# Playwright
|
||||||
|
node_modules/
|
||||||
|
/test-results/
|
||||||
|
/playwright-report/
|
||||||
|
/blob-report/
|
||||||
|
/playwright/.cache/
|
||||||
|
/playwright/.auth/
|
||||||
|
e2e/screenshots/
|
||||||
|
|||||||
@@ -180,4 +180,3 @@ docker compose down
|
|||||||
- **多租戶**:
|
- **多租戶**:
|
||||||
- 中央邏輯 (Landlord) 與租戶邏輯 (Tenant) 分離。
|
- 中央邏輯 (Landlord) 與租戶邏輯 (Tenant) 分離。
|
||||||
- 租戶路由定義於 `routes/tenant.php` (但在本專案架構中,大部分路由在 `web.php` 並透過 Middleware 判斷環境)。
|
- 租戶路由定義於 `routes/tenant.php` (但在本專案架構中,大部分路由在 `web.php` 並透過 Middleware 判斷環境)。
|
||||||
|
|
||||||
|
|||||||
112
app/Console/Commands/NotifyUtilityFeeStatus.php
Normal file
112
app/Console/Commands/NotifyUtilityFeeStatus.php
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
class NotifyUtilityFeeStatus extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'finance:notify-utility-fees';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = '檢查公共事業費狀態並寄送 Email 通知管理員';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->info("正在掃描公共事業費狀態...");
|
||||||
|
|
||||||
|
// 1. 更新逾期狀態 (pending -> overdue)
|
||||||
|
\App\Modules\Finance\Models\UtilityFee::where('payment_status', \App\Modules\Finance\Models\UtilityFee::STATUS_PENDING)
|
||||||
|
->whereNotNull('due_date')
|
||||||
|
->where('due_date', '<', now()->startOfDay())
|
||||||
|
->update(['payment_status' => \App\Modules\Finance\Models\UtilityFee::STATUS_OVERDUE]);
|
||||||
|
|
||||||
|
// 2. 獲取可能需要處理的單據 (pending 或 overdue)
|
||||||
|
$feesToCheck = \App\Modules\Finance\Models\UtilityFee::whereIn('payment_status', [
|
||||||
|
\App\Modules\Finance\Models\UtilityFee::STATUS_PENDING,
|
||||||
|
\App\Modules\Finance\Models\UtilityFee::STATUS_OVERDUE
|
||||||
|
])
|
||||||
|
->whereNotNull('due_date')
|
||||||
|
->orderBy('due_date', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($feesToCheck->isEmpty()) {
|
||||||
|
$this->info("目前沒有未繳納的公共事業費。");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 根據業務規則過濾出今天「真正」需要發信的單據
|
||||||
|
$today = now()->startOfDay();
|
||||||
|
$unpaidFees = $feesToCheck->filter(function ($fee) use ($today) {
|
||||||
|
$dueDate = \Illuminate\Support\Carbon::parse($fee->due_date)->startOfDay();
|
||||||
|
$diffInDays = $today->diffInDays($dueDate, false);
|
||||||
|
|
||||||
|
// 如果已經逾期 (overdue),每天都要發信
|
||||||
|
if ($fee->payment_status === \App\Modules\Finance\Models\UtilityFee::STATUS_OVERDUE) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是待繳納 (pending),僅在特定天數發信
|
||||||
|
// 規則:到期前 7 天、3 天、當天 (0 天)
|
||||||
|
return in_array($diffInDays, [7, 3, 0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($unpaidFees->isEmpty()) {
|
||||||
|
$this->info("今日無符合發信條件的公共事業費提醒。");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 讀取系統設定
|
||||||
|
$senderEmail = \App\Modules\Core\Models\SystemSetting::getVal('notification.utility_fee_sender_email');
|
||||||
|
$senderPassword = \App\Modules\Core\Models\SystemSetting::getVal('notification.utility_fee_sender_password');
|
||||||
|
$recipientEmailsStr = \App\Modules\Core\Models\SystemSetting::getVal('notification.utility_fee_recipient_emails');
|
||||||
|
|
||||||
|
if (empty($senderEmail) || empty($senderPassword) || empty($recipientEmailsStr)) {
|
||||||
|
$this->warn("系統設定中缺乏完整的 Email 通知參數,跳過寄送通知。請至「系統設定」->「通知設定」完善資料。");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 動態覆寫應用程式名稱與 SMTP Config
|
||||||
|
$tenantName = tenant('name') ?? config('app.name');
|
||||||
|
config([
|
||||||
|
'app.name' => $tenantName,
|
||||||
|
'mail.mailers.smtp.username' => $senderEmail,
|
||||||
|
'mail.mailers.smtp.password' => $senderPassword,
|
||||||
|
'mail.from.address' => $senderEmail,
|
||||||
|
'mail.from.name' => $tenantName . ' (系統通知)'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 清理原先可能的 Mailer 實例,確保使用新的 Config
|
||||||
|
\Illuminate\Support\Facades\Mail::purge();
|
||||||
|
|
||||||
|
// 5. 解析收件者並寄送 Email
|
||||||
|
$recipients = array_map('trim', explode(',', $recipientEmailsStr));
|
||||||
|
$validRecipients = array_filter($recipients, fn($e) => filter_var($e, FILTER_VALIDATE_EMAIL));
|
||||||
|
|
||||||
|
if (empty($validRecipients)) {
|
||||||
|
$this->warn("無效的收件者 Email 格式,跳過寄送通知。");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
\Illuminate\Support\Facades\Mail::to($validRecipients)->send(new \App\Mail\PaymentReminderMail($unpaidFees));
|
||||||
|
$this->info("通知郵件已成功寄送至: " . implode(', ', $validRecipients));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->error("Email 寄送失敗: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
59
app/Mail/PaymentReminderMail.php
Normal file
59
app/Mail/PaymentReminderMail.php
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Mail;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Mail\Mailables\Content;
|
||||||
|
use Illuminate\Mail\Mailables\Envelope;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class PaymentReminderMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
public $fees;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*/
|
||||||
|
public function __construct($fees)
|
||||||
|
{
|
||||||
|
$this->fees = $fees;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message envelope.
|
||||||
|
*/
|
||||||
|
public function envelope(): Envelope
|
||||||
|
{
|
||||||
|
$tenantName = tenant('name') ?? '系統';
|
||||||
|
return new Envelope(
|
||||||
|
subject: "【{$tenantName}】公共事業費繳費/逾期通知",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message content definition.
|
||||||
|
*/
|
||||||
|
public function content(): Content
|
||||||
|
{
|
||||||
|
return new Content(
|
||||||
|
markdown: 'emails.payment-reminder',
|
||||||
|
with: [
|
||||||
|
'fees' => $this->fees,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the attachments for the message.
|
||||||
|
*
|
||||||
|
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||||
|
*/
|
||||||
|
public function attachments(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
54
app/Mail/TestNotificationMail.php
Normal file
54
app/Mail/TestNotificationMail.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Mail;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Mail\Mailables\Content;
|
||||||
|
use Illuminate\Mail\Mailables\Envelope;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class TestNotificationMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message envelope.
|
||||||
|
*/
|
||||||
|
public function envelope(): Envelope
|
||||||
|
{
|
||||||
|
$tenantName = tenant('name') ?? '系統';
|
||||||
|
return new Envelope(
|
||||||
|
subject: "【{$tenantName}】電子郵件通知測試",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message content definition.
|
||||||
|
*/
|
||||||
|
public function content(): Content
|
||||||
|
{
|
||||||
|
return new Content(
|
||||||
|
markdown: 'emails.test-notification',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the attachments for the message.
|
||||||
|
*
|
||||||
|
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||||
|
*/
|
||||||
|
public function attachments(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ class ActivityLogController extends Controller
|
|||||||
'App\Modules\Procurement\Models\PurchaseOrder' => '採購單',
|
'App\Modules\Procurement\Models\PurchaseOrder' => '採購單',
|
||||||
'App\Modules\Inventory\Models\Warehouse' => '倉庫',
|
'App\Modules\Inventory\Models\Warehouse' => '倉庫',
|
||||||
'App\Modules\Inventory\Models\Inventory' => '庫存',
|
'App\Modules\Inventory\Models\Inventory' => '庫存',
|
||||||
|
'App\Modules\Inventory\Models\InventoryTransaction' => '庫存異動紀錄',
|
||||||
'App\Modules\Finance\Models\UtilityFee' => '公共事業費',
|
'App\Modules\Finance\Models\UtilityFee' => '公共事業費',
|
||||||
'App\Modules\Inventory\Models\GoodsReceipt' => '進貨單',
|
'App\Modules\Inventory\Models\GoodsReceipt' => '進貨單',
|
||||||
'App\Modules\Production\Models\ProductionOrder' => '生產工單',
|
'App\Modules\Production\Models\ProductionOrder' => '生產工單',
|
||||||
@@ -31,12 +32,17 @@ class ActivityLogController extends Controller
|
|||||||
'App\Modules\Inventory\Models\InventoryCountDoc' => '庫存盤點單',
|
'App\Modules\Inventory\Models\InventoryCountDoc' => '庫存盤點單',
|
||||||
'App\Modules\Inventory\Models\InventoryAdjustDoc' => '庫存盤調單',
|
'App\Modules\Inventory\Models\InventoryAdjustDoc' => '庫存盤調單',
|
||||||
'App\Modules\Inventory\Models\InventoryTransferOrder' => '庫存調撥單',
|
'App\Modules\Inventory\Models\InventoryTransferOrder' => '庫存調撥單',
|
||||||
|
'App\Modules\Inventory\Models\StoreRequisition' => '門市叫貨單',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$sortBy = $request->input('sort_by', 'created_at');
|
$sortBy = $request->input('sort_by', 'created_at');
|
||||||
$sortOrder = $request->input('sort_order', 'desc');
|
$sortOrder = $request->input('sort_order', 'desc');
|
||||||
|
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ class RoleController extends Controller
|
|||||||
*/
|
*/
|
||||||
private function getGroupedPermissions()
|
private function getGroupedPermissions()
|
||||||
{
|
{
|
||||||
$allPermissions = Permission::orderBy('name')->get();
|
$allPermissions = Permission::select('id', 'name', 'guard_name')->orderBy('name')->get();
|
||||||
$grouped = [];
|
$grouped = [];
|
||||||
|
|
||||||
foreach ($allPermissions as $permission) {
|
foreach ($allPermissions as $permission) {
|
||||||
@@ -185,6 +185,7 @@ class RoleController extends Controller
|
|||||||
'inventory_adjust' => '庫存盤調管理',
|
'inventory_adjust' => '庫存盤調管理',
|
||||||
'inventory_transfer' => '庫存調撥管理',
|
'inventory_transfer' => '庫存調撥管理',
|
||||||
'inventory_report' => '庫存報表',
|
'inventory_report' => '庫存報表',
|
||||||
|
'inventory_traceability' => '批號溯源',
|
||||||
'vendors' => '廠商資料管理',
|
'vendors' => '廠商資料管理',
|
||||||
'purchase_orders' => '採購單管理',
|
'purchase_orders' => '採購單管理',
|
||||||
'purchase_returns' => '採購退回管理',
|
'purchase_returns' => '採購退回管理',
|
||||||
@@ -198,6 +199,7 @@ class RoleController extends Controller
|
|||||||
'sales_imports' => '銷售單匯入管理',
|
'sales_imports' => '銷售單匯入管理',
|
||||||
'sales_orders' => '銷售訂單管理',
|
'sales_orders' => '銷售訂單管理',
|
||||||
'store_requisitions' => '門市叫貨申請',
|
'store_requisitions' => '門市叫貨申請',
|
||||||
|
'procurement_analysis' => '採購統計分析',
|
||||||
'users' => '使用者管理',
|
'users' => '使用者管理',
|
||||||
'roles' => '角色與權限',
|
'roles' => '角色與權限',
|
||||||
'system' => '系統管理',
|
'system' => '系統管理',
|
||||||
|
|||||||
97
app/Modules/Core/Controllers/SystemSettingController.php
Normal file
97
app/Modules/Core/Controllers/SystemSettingController.php
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Core\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Modules\Core\Models\SystemSetting;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class SystemSettingController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 顯示系統設定頁面
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$settings = SystemSetting::all()->groupBy('group');
|
||||||
|
|
||||||
|
return Inertia::render('Admin/Setting/Index', [
|
||||||
|
'settings' => $settings,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新系統設定
|
||||||
|
*/
|
||||||
|
public function update(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'settings' => 'required|array',
|
||||||
|
'settings.*.key' => 'required|string|exists:system_settings,key',
|
||||||
|
'settings.*.value' => 'nullable',
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($validated['settings'] as $item) {
|
||||||
|
SystemSetting::where('key', $item['key'])->update([
|
||||||
|
'value' => $item['value']
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除記憶體快取,確保後續讀取拿到最新值
|
||||||
|
SystemSetting::clearCache();
|
||||||
|
|
||||||
|
return redirect()->back()->with('success', '系統設定已更新');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 測試發送通知信
|
||||||
|
*/
|
||||||
|
public function testNotification(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'settings' => 'required|array',
|
||||||
|
'settings.*.key' => 'required|string',
|
||||||
|
'settings.*.value' => 'nullable|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$settings = collect($validated['settings'])->pluck('value', 'key');
|
||||||
|
|
||||||
|
$senderEmail = $settings['notification.utility_fee_sender_email'] ?? null;
|
||||||
|
$senderPassword = $settings['notification.utility_fee_sender_password'] ?? null;
|
||||||
|
$recipientEmailsStr = $settings['notification.utility_fee_recipient_emails'] ?? null;
|
||||||
|
|
||||||
|
if (empty($senderEmail) || empty($senderPassword) || empty($recipientEmailsStr)) {
|
||||||
|
return back()->with('error', '請先填寫完整發信帳號、密碼及收件者信箱。');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 動態覆寫應用程式名稱與 SMTP Config
|
||||||
|
$tenantName = tenant('name') ?? config('app.name');
|
||||||
|
config([
|
||||||
|
'app.name' => $tenantName,
|
||||||
|
'mail.mailers.smtp.username' => $senderEmail,
|
||||||
|
'mail.mailers.smtp.password' => $senderPassword,
|
||||||
|
'mail.from.address' => $senderEmail,
|
||||||
|
'mail.from.name' => $tenantName . ' (系統通知)'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 清理原先可能的 Mailer 實例,確保使用新的 Config
|
||||||
|
\Illuminate\Support\Facades\Mail::purge();
|
||||||
|
|
||||||
|
// 解析收件者
|
||||||
|
$recipients = array_map('trim', explode(',', $recipientEmailsStr));
|
||||||
|
$validRecipients = array_filter($recipients, fn($e) => filter_var($e, FILTER_VALIDATE_EMAIL));
|
||||||
|
|
||||||
|
if (empty($validRecipients)) {
|
||||||
|
return back()->with('error', '無效的收件者 Email 格式。');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
\Illuminate\Support\Facades\Mail::to($validRecipients)->send(new \App\Mail\TestNotificationMail());
|
||||||
|
return back()->with('success', '測試信件已成功發送,請檢查收件匣。');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return back()->with('error', '測試發信失敗: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -18,7 +18,12 @@ class UserController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$sortBy = $request->input('sort_by', 'id');
|
$sortBy = $request->input('sort_by', 'id');
|
||||||
$sortOrder = $request->input('sort_order', 'asc');
|
$sortOrder = $request->input('sort_order', 'asc');
|
||||||
$search = $request->input('search');
|
$search = $request->input('search');
|
||||||
|
|||||||
61
app/Modules/Core/Models/SystemSetting.php
Normal file
61
app/Modules/Core/Models/SystemSetting.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Core\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class SystemSetting extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'group',
|
||||||
|
'key',
|
||||||
|
'value',
|
||||||
|
'type',
|
||||||
|
'description',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同請求內的記憶體快取,避免重複查詢 DB
|
||||||
|
* PHP 請求結束後自動釋放,無需額外處理失效
|
||||||
|
*/
|
||||||
|
protected static array $cache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取得特定設定值(含記憶體快取)
|
||||||
|
*/
|
||||||
|
public static function getVal(string $key, $default = null)
|
||||||
|
{
|
||||||
|
if (array_key_exists($key, static::$cache)) {
|
||||||
|
return static::$cache[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
$setting = self::where('key', $key)->first();
|
||||||
|
|
||||||
|
if (!$setting) {
|
||||||
|
static::$cache[$key] = $default;
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $setting->value;
|
||||||
|
|
||||||
|
// 根據 type 進行類別轉換
|
||||||
|
$resolved = match ($setting->type) {
|
||||||
|
'integer', 'number' => (int) $value,
|
||||||
|
'boolean', 'bool' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
|
||||||
|
'json', 'array' => json_decode($value, true),
|
||||||
|
default => $value,
|
||||||
|
};
|
||||||
|
|
||||||
|
static::$cache[$key] = $resolved;
|
||||||
|
|
||||||
|
return $resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除記憶體快取(儲存設定後應呼叫)
|
||||||
|
*/
|
||||||
|
public static function clearCache(): void
|
||||||
|
{
|
||||||
|
static::$cache = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ use App\Modules\Core\Controllers\ProfileController;
|
|||||||
use App\Modules\Core\Controllers\RoleController;
|
use App\Modules\Core\Controllers\RoleController;
|
||||||
use App\Modules\Core\Controllers\UserController;
|
use App\Modules\Core\Controllers\UserController;
|
||||||
use App\Modules\Core\Controllers\ActivityLogController;
|
use App\Modules\Core\Controllers\ActivityLogController;
|
||||||
|
use App\Modules\Core\Controllers\SystemSettingController;
|
||||||
|
|
||||||
// 登入/登出路由
|
// 登入/登出路由
|
||||||
Route::get('/login', [LoginController::class, 'show'])->name('login');
|
Route::get('/login', [LoginController::class, 'show'])->name('login');
|
||||||
@@ -56,5 +57,11 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::get('/activity-logs', [ActivityLogController::class, 'index'])->name('activity-logs.index');
|
Route::get('/activity-logs', [ActivityLogController::class, 'index'])->name('activity-logs.index');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::middleware('permission:system.settings.view')->group(function () {
|
||||||
|
Route::get('/settings', [SystemSettingController::class, 'index'])->name('settings.index');
|
||||||
|
Route::post('/settings', [SystemSettingController::class, 'update'])->name('settings.update');
|
||||||
|
Route::post('/settings/test-notification', [SystemSettingController::class, 'testNotification'])->name('settings.test-notification');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class CoreService implements CoreServiceInterface
|
|||||||
*/
|
*/
|
||||||
public function getUsersByIds(array $ids): Collection
|
public function getUsersByIds(array $ids): Collection
|
||||||
{
|
{
|
||||||
return User::whereIn('id', $ids)->get();
|
return User::select('id', 'name')->whereIn('id', $ids)->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,7 +37,7 @@ class CoreService implements CoreServiceInterface
|
|||||||
*/
|
*/
|
||||||
public function getAllUsers(): Collection
|
public function getAllUsers(): Collection
|
||||||
{
|
{
|
||||||
return User::all();
|
return User::select('id', 'name')->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ensureSystemUserExists()
|
public function ensureSystemUserExists()
|
||||||
|
|||||||
@@ -63,7 +63,11 @@ class AccountPayableController extends Controller
|
|||||||
$query->where('due_date', '<=', $request->date_end);
|
$query->where('due_date', '<=', $request->date_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$payables = $query->latest()->paginate($perPage)->withQueryString();
|
$payables = $query->latest()->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
// Manual Hydration for Vendors
|
// Manual Hydration for Vendors
|
||||||
|
|||||||
@@ -27,7 +27,11 @@ class AccountingReportController extends Controller
|
|||||||
$allRecords = $reportData['records'];
|
$allRecords = $reportData['records'];
|
||||||
|
|
||||||
// 3. Manual Pagination
|
// 3. Manual Pagination
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$page = $request->input('page', 1);
|
$page = $request->input('page', 1);
|
||||||
$offset = ($page - 1) * $perPage;
|
$offset = ($page - 1) * $perPage;
|
||||||
|
|
||||||
@@ -65,14 +69,25 @@ class AccountingReportController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$exportData = $allRecords->map(function ($record) {
|
$exportData = $allRecords->map(function ($record) {
|
||||||
|
$taxAmount = (float)($record['tax_amount'] ?? 0);
|
||||||
|
$totalAmount = (float)($record['amount'] ?? 0);
|
||||||
|
$untaxedAmount = $totalAmount - $taxAmount;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
$record['date'],
|
$record['date'],
|
||||||
$record['source'],
|
$record['source'],
|
||||||
$record['category'],
|
$record['category'],
|
||||||
$record['item'],
|
$record['item'],
|
||||||
$record['reference'],
|
$record['reference'],
|
||||||
$record['invoice_number'],
|
$record['invoice_date'] ?? '-',
|
||||||
$record['amount'],
|
$record['invoice_number'] ?? '-',
|
||||||
|
$untaxedAmount,
|
||||||
|
$taxAmount,
|
||||||
|
$totalAmount,
|
||||||
|
$record['payment_method'] ?? '-',
|
||||||
|
$record['payment_note'] ?? '-',
|
||||||
|
$record['remarks'] ?? '-',
|
||||||
|
$record['status'] ?? '-',
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -87,7 +102,11 @@ class AccountingReportController extends Controller
|
|||||||
// BOM for Excel compatibility with UTF-8
|
// BOM for Excel compatibility with UTF-8
|
||||||
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
|
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
|
||||||
|
|
||||||
fputcsv($file, ['日期', '來源', '類別', '項目', '參考單號', '發票號碼', '金額']);
|
fputcsv($file, [
|
||||||
|
'日期', '來源', '類別', '項目', '參考單號',
|
||||||
|
'發票日期', '發票號碼', '未稅金額', '稅額', '總金額',
|
||||||
|
'付款方式', '付款備註', '內部備註', '狀態'
|
||||||
|
]);
|
||||||
|
|
||||||
foreach ($exportData as $row) {
|
foreach ($exportData as $row) {
|
||||||
fputcsv($file, $row);
|
fputcsv($file, $row);
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ namespace App\Modules\Finance\Controllers;
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Modules\Finance\Models\UtilityFee;
|
use App\Modules\Finance\Models\UtilityFee;
|
||||||
|
use App\Modules\Finance\Models\UtilityFeeAttachment;
|
||||||
use App\Modules\Finance\Contracts\FinanceServiceInterface;
|
use App\Modules\Finance\Contracts\FinanceServiceInterface;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
|
|
||||||
class UtilityFeeController extends Controller
|
class UtilityFeeController extends Controller
|
||||||
@@ -34,13 +36,16 @@ class UtilityFeeController extends Controller
|
|||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'transaction_date' => 'required|date',
|
'transaction_date' => 'nullable|date',
|
||||||
|
'due_date' => 'required|date',
|
||||||
'category' => 'required|string|max:255',
|
'category' => 'required|string|max:255',
|
||||||
'amount' => 'required|numeric|min:0',
|
'amount' => 'required|numeric|min:0',
|
||||||
'invoice_number' => 'nullable|string|max:255',
|
'invoice_number' => 'nullable|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$validated['payment_status'] = $this->determineStatus($validated);
|
||||||
|
|
||||||
$fee = UtilityFee::create($validated);
|
$fee = UtilityFee::create($validated);
|
||||||
|
|
||||||
activity()
|
activity()
|
||||||
@@ -55,13 +60,16 @@ class UtilityFeeController extends Controller
|
|||||||
public function update(Request $request, UtilityFee $utility_fee)
|
public function update(Request $request, UtilityFee $utility_fee)
|
||||||
{
|
{
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'transaction_date' => 'required|date',
|
'transaction_date' => 'nullable|date',
|
||||||
|
'due_date' => 'required|date',
|
||||||
'category' => 'required|string|max:255',
|
'category' => 'required|string|max:255',
|
||||||
'amount' => 'required|numeric|min:0',
|
'amount' => 'required|numeric|min:0',
|
||||||
'invoice_number' => 'nullable|string|max:255',
|
'invoice_number' => 'nullable|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$validated['payment_status'] = $this->determineStatus($validated);
|
||||||
|
|
||||||
$utility_fee->update($validated);
|
$utility_fee->update($validated);
|
||||||
|
|
||||||
activity()
|
activity()
|
||||||
@@ -73,6 +81,22 @@ class UtilityFeeController extends Controller
|
|||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判定繳費狀態
|
||||||
|
*/
|
||||||
|
private function determineStatus(array $data): string
|
||||||
|
{
|
||||||
|
if (!empty($data['transaction_date'])) {
|
||||||
|
return UtilityFee::STATUS_PAID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($data['due_date']) && now()->startOfDay()->gt(\Illuminate\Support\Carbon::parse($data['due_date']))) {
|
||||||
|
return UtilityFee::STATUS_OVERDUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return UtilityFee::STATUS_PENDING;
|
||||||
|
}
|
||||||
|
|
||||||
public function destroy(UtilityFee $utility_fee)
|
public function destroy(UtilityFee $utility_fee)
|
||||||
{
|
{
|
||||||
activity()
|
activity()
|
||||||
@@ -81,8 +105,82 @@ class UtilityFeeController extends Controller
|
|||||||
->event('deleted')
|
->event('deleted')
|
||||||
->log('deleted');
|
->log('deleted');
|
||||||
|
|
||||||
|
// 刪除實體檔案 (如果 cascade 沒處理或是想要手動清理)
|
||||||
|
foreach ($utility_fee->attachments as $attachment) {
|
||||||
|
Storage::disk('public')->delete($attachment->file_path);
|
||||||
|
}
|
||||||
|
|
||||||
$utility_fee->delete();
|
$utility_fee->delete();
|
||||||
|
|
||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 獲取附件列表
|
||||||
|
*/
|
||||||
|
public function attachments(UtilityFee $utility_fee)
|
||||||
|
{
|
||||||
|
return response()->json([
|
||||||
|
'attachments' => $utility_fee->attachments()->orderBy('created_at', 'desc')->get()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上傳附件
|
||||||
|
*/
|
||||||
|
public function uploadAttachment(Request $request, UtilityFee $utility_fee)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'file' => 'required|file|mimes:jpeg,jpg,png,webp,pdf|max:2048', // 2MB
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 檢查數量限制 (最多 3 張)
|
||||||
|
if ($utility_fee->attachments()->count() >= 3) {
|
||||||
|
return response()->json(['message' => '附件數量已達上限 (最多 3 個)'], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
$file = $request->file('file');
|
||||||
|
$path = $file->store("utility-fee-attachments/{$utility_fee->id}", 'public');
|
||||||
|
|
||||||
|
$attachment = $utility_fee->attachments()->create([
|
||||||
|
'file_path' => $path,
|
||||||
|
'original_name' => $file->getClientOriginalName(),
|
||||||
|
'mime_type' => $file->getMimeType(),
|
||||||
|
'size' => $file->getSize(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
activity()
|
||||||
|
->performedOn($utility_fee)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('attachment_uploaded')
|
||||||
|
->log("uploaded attachment: {$attachment->original_name}");
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => '上傳成功',
|
||||||
|
'attachment' => $attachment
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刪除附件
|
||||||
|
*/
|
||||||
|
public function deleteAttachment(UtilityFee $utility_fee, UtilityFeeAttachment $attachment)
|
||||||
|
{
|
||||||
|
// 確保附件屬於該費用
|
||||||
|
if ($attachment->utility_fee_id !== $utility_fee->id) {
|
||||||
|
abort(403);
|
||||||
|
}
|
||||||
|
|
||||||
|
Storage::disk('public')->delete($attachment->file_path);
|
||||||
|
|
||||||
|
$attachment->delete();
|
||||||
|
|
||||||
|
activity()
|
||||||
|
->performedOn($utility_fee)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('attachment_deleted')
|
||||||
|
->log("deleted attachment: {$attachment->original_name}");
|
||||||
|
|
||||||
|
return response()->json(['message' => '刪除成功']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,29 +7,47 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
class UtilityFee extends Model
|
class UtilityFee extends Model
|
||||||
{
|
{
|
||||||
/** @use HasFactory<\Database\Factories\UtilityFeeFactory> */
|
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 此公共事業費的附件
|
||||||
|
*/
|
||||||
|
public function attachments()
|
||||||
|
{
|
||||||
|
return $this->hasMany(UtilityFeeAttachment::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 狀態常數
|
||||||
|
const STATUS_PENDING = 'pending';
|
||||||
|
const STATUS_PAID = 'paid';
|
||||||
|
const STATUS_OVERDUE = 'overdue';
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'transaction_date',
|
'transaction_date',
|
||||||
|
'due_date',
|
||||||
'category',
|
'category',
|
||||||
'amount',
|
'amount',
|
||||||
|
'payment_status',
|
||||||
'invoice_number',
|
'invoice_number',
|
||||||
'description',
|
'description',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'transaction_date' => 'date',
|
'transaction_date' => 'date:Y-m-d',
|
||||||
|
'due_date' => 'date:Y-m-d',
|
||||||
'amount' => 'decimal:2',
|
'amount' => 'decimal:2',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$activity->properties = $activity->properties->put('snapshot', [
|
$snapshot = [
|
||||||
'transaction_date' => $this->transaction_date->format('Y-m-d'),
|
'transaction_date' => $this->transaction_date?->format('Y-m-d'),
|
||||||
|
'due_date' => $this->due_date?->format('Y-m-d'),
|
||||||
'category' => $this->category,
|
'category' => $this->category,
|
||||||
'amount' => $this->amount,
|
'amount' => $this->amount,
|
||||||
|
'payment_status' => $this->payment_status,
|
||||||
'invoice_number' => $this->invoice_number,
|
'invoice_number' => $this->invoice_number,
|
||||||
]);
|
];
|
||||||
|
$activity->properties = $activity->properties->put('snapshot', $snapshot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
36
app/Modules/Finance/Models/UtilityFeeAttachment.php
Normal file
36
app/Modules/Finance/Models/UtilityFeeAttachment.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Finance\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class UtilityFeeAttachment extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'utility_fee_id',
|
||||||
|
'file_path',
|
||||||
|
'original_name',
|
||||||
|
'mime_type',
|
||||||
|
'size',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $appends = ['url'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 附件所属的公共事業費
|
||||||
|
*/
|
||||||
|
public function utilityFee(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(UtilityFee::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 獲取附件的全路徑 URL
|
||||||
|
*/
|
||||||
|
public function getUrlAttribute()
|
||||||
|
{
|
||||||
|
return tenant_asset($this->file_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,11 @@ Route::middleware('auth')->group(function () {
|
|||||||
});
|
});
|
||||||
Route::middleware('permission:utility_fees.edit')->group(function () {
|
Route::middleware('permission:utility_fees.edit')->group(function () {
|
||||||
Route::put('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'update'])->name('utility-fees.update');
|
Route::put('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'update'])->name('utility-fees.update');
|
||||||
|
|
||||||
|
// 附件管理 (Ajax)
|
||||||
|
Route::get('/utility-fees/{utility_fee}/attachments', [UtilityFeeController::class, 'attachments'])->name('utility-fees.attachments');
|
||||||
|
Route::post('/utility-fees/{utility_fee}/attachments', [UtilityFeeController::class, 'uploadAttachment'])->name('utility-fees.upload-attachment');
|
||||||
|
Route::delete('/utility-fees/{utility_fee}/attachments/{attachment}', [UtilityFeeController::class, 'deleteAttachment'])->name('utility-fees.delete-attachment');
|
||||||
});
|
});
|
||||||
Route::middleware('permission:utility_fees.delete')->group(function () {
|
Route::middleware('permission:utility_fees.delete')->group(function () {
|
||||||
Route::delete('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'destroy'])->name('utility-fees.destroy');
|
Route::delete('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'destroy'])->name('utility-fees.destroy');
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ class AccountPayableService
|
|||||||
'total_amount' => collect($receiptData['items'] ?? [])->sum('total_amount'),
|
'total_amount' => collect($receiptData['items'] ?? [])->sum('total_amount'),
|
||||||
'tax_amount' => 0, // 假設後續會實作稅額計算,目前預設為 0
|
'tax_amount' => 0, // 假設後續會實作稅額計算,目前預設為 0
|
||||||
'status' => AccountPayable::STATUS_PENDING,
|
'status' => AccountPayable::STATUS_PENDING,
|
||||||
// 設定應付日期,預設為進貨後 30 天 (可依據供應商設定調整)
|
// 設定應付日期,預設為進貨後天數 (由系統設定決定,預設 30 天)
|
||||||
'due_date' => now()->addDays(30)->toDateString(),
|
'due_date' => now()->addDays(\App\Modules\Core\Models\SystemSetting::getVal('finance.ap_payment_days', 30))->toDateString(),
|
||||||
'created_by' => $userId,
|
'created_by' => $userId,
|
||||||
'remarks' => "由進貨單 {$receiptData['code']} 自動生成",
|
'remarks' => "由進貨單 {$receiptData['code']} 自動生成",
|
||||||
]);
|
]);
|
||||||
@@ -70,6 +70,7 @@ class AccountPayableService
|
|||||||
|
|
||||||
$latest = AccountPayable::where('document_number', 'like', $lastPrefix)
|
$latest = AccountPayable::where('document_number', 'like', $lastPrefix)
|
||||||
->orderBy('document_number', 'desc')
|
->orderBy('document_number', 'desc')
|
||||||
|
->lockForUpdate()
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$latest) {
|
if (!$latest) {
|
||||||
|
|||||||
@@ -19,23 +19,48 @@ class FinanceService implements FinanceServiceInterface
|
|||||||
|
|
||||||
public function getAccountingReportData(string $start, string $end): array
|
public function getAccountingReportData(string $start, string $end): array
|
||||||
{
|
{
|
||||||
// 1. 獲取採購單資料
|
// 1. 獲取應付帳款資料 (已付款)
|
||||||
$purchaseOrders = $this->procurementService->getPurchaseOrdersByDate($start, $end)
|
$accountPayables = \App\Modules\Finance\Models\AccountPayable::where('status', \App\Modules\Finance\Models\AccountPayable::STATUS_PAID)
|
||||||
->map(function ($po) {
|
->whereNotNull('paid_at')
|
||||||
return [
|
->whereBetween('paid_at', [$start, $end])
|
||||||
'id' => 'PO-' . $po->id,
|
->get();
|
||||||
'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)
|
// 取得供應商資料 (Manual Hydration)
|
||||||
$utilityFees = UtilityFee::whereBetween('transaction_date', [$start, $end])
|
$vendorIds = $accountPayables->pluck('vendor_id')->unique()->filter()->toArray();
|
||||||
|
$vendorsMap = $this->procurementService->getVendorsByIds($vendorIds)->keyBy('id');
|
||||||
|
|
||||||
|
// 付款方式對映
|
||||||
|
$paymentMethodMap = [
|
||||||
|
'cash' => '現金',
|
||||||
|
'bank_transfer' => '銀行轉帳',
|
||||||
|
'check' => '支票',
|
||||||
|
'credit_card' => '信用卡',
|
||||||
|
];
|
||||||
|
|
||||||
|
$payableRecords = $accountPayables->map(function ($ap) use ($vendorsMap, $paymentMethodMap) {
|
||||||
|
$vendorName = isset($vendorsMap[$ap->vendor_id]) ? $vendorsMap[$ap->vendor_id]->name : '未知廠商';
|
||||||
|
$mappedPaymentMethod = $paymentMethodMap[$ap->payment_method] ?? $ap->payment_method;
|
||||||
|
return [
|
||||||
|
'id' => 'AP-' . $ap->id,
|
||||||
|
'date' => Carbon::parse($ap->paid_at)->timezone(config('app.timezone'))->toDateString(),
|
||||||
|
'source' => '應付帳款',
|
||||||
|
'category' => '進貨支出',
|
||||||
|
'item' => $vendorName,
|
||||||
|
'reference' => $ap->document_number,
|
||||||
|
'invoice_date' => $ap->invoice_date ? $ap->invoice_date->format('Y-m-d') : null,
|
||||||
|
'invoice_number' => $ap->invoice_number,
|
||||||
|
'amount' => (float)$ap->total_amount,
|
||||||
|
'tax_amount' => (float)$ap->tax_amount,
|
||||||
|
'status' => $ap->status,
|
||||||
|
'payment_method' => $mappedPaymentMethod,
|
||||||
|
'payment_note' => $ap->payment_note,
|
||||||
|
'remarks' => $ap->remarks,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. 獲取公共事業費 (已繳費)
|
||||||
|
$utilityFees = UtilityFee::where('payment_status', UtilityFee::STATUS_PAID)
|
||||||
|
->whereBetween('transaction_date', [$start, $end])
|
||||||
->get()
|
->get()
|
||||||
->map(function ($fee) {
|
->map(function ($fee) {
|
||||||
return [
|
return [
|
||||||
@@ -45,12 +70,18 @@ class FinanceService implements FinanceServiceInterface
|
|||||||
'category' => $fee->category,
|
'category' => $fee->category,
|
||||||
'item' => $fee->description ?: $fee->category,
|
'item' => $fee->description ?: $fee->category,
|
||||||
'reference' => '-',
|
'reference' => '-',
|
||||||
|
'invoice_date' => null,
|
||||||
'invoice_number' => $fee->invoice_number,
|
'invoice_number' => $fee->invoice_number,
|
||||||
'amount' => (float)$fee->amount,
|
'amount' => (float)$fee->amount,
|
||||||
|
'tax_amount' => 0.0,
|
||||||
|
'status' => $fee->payment_status,
|
||||||
|
'payment_method' => null,
|
||||||
|
'payment_note' => null,
|
||||||
|
'remarks' => $fee->description,
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
$allRecords = $purchaseOrders->concat($utilityFees)
|
$allRecords = $payableRecords->concat($utilityFees)
|
||||||
->sortByDesc('date')
|
->sortByDesc('date')
|
||||||
->values();
|
->values();
|
||||||
|
|
||||||
@@ -58,7 +89,7 @@ class FinanceService implements FinanceServiceInterface
|
|||||||
'records' => $allRecords,
|
'records' => $allRecords,
|
||||||
'summary' => [
|
'summary' => [
|
||||||
'total_amount' => $allRecords->sum('amount'),
|
'total_amount' => $allRecords->sum('amount'),
|
||||||
'purchase_total' => $purchaseOrders->sum('amount'),
|
'payable_total' => $payableRecords->sum('amount'),
|
||||||
'utility_total' => $utilityFees->sum('amount'),
|
'utility_total' => $utilityFees->sum('amount'),
|
||||||
'record_count' => $allRecords->count(),
|
'record_count' => $allRecords->count(),
|
||||||
]
|
]
|
||||||
@@ -67,7 +98,7 @@ class FinanceService implements FinanceServiceInterface
|
|||||||
|
|
||||||
public function getUtilityFees(array $filters)
|
public function getUtilityFees(array $filters)
|
||||||
{
|
{
|
||||||
$query = UtilityFee::query();
|
$query = UtilityFee::withCount('attachments');
|
||||||
|
|
||||||
if (!empty($filters['search'])) {
|
if (!empty($filters['search'])) {
|
||||||
$search = $filters['search'];
|
$search = $filters['search'];
|
||||||
@@ -94,7 +125,13 @@ class FinanceService implements FinanceServiceInterface
|
|||||||
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
||||||
$query->orderBy($sortField, $sortDirection);
|
$query->orderBy($sortField, $sortDirection);
|
||||||
|
|
||||||
return $query->paginate($filters['per_page'] ?? 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = (int) ($filters['per_page'] ?? $defaultPerPage);
|
||||||
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->paginate($perPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUniqueCategories(): Collection
|
public function getUniqueCategories(): Collection
|
||||||
|
|||||||
@@ -58,59 +58,74 @@ class SyncOrderAction
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 預檢 (Pre-flight check) N+1 優化 ---
|
// --- 預檢 (Pre-flight check) 僅使用 product_id ---
|
||||||
$items = $data['items'];
|
$items = $data['items'];
|
||||||
$posProductIds = array_column($items, 'pos_product_id');
|
$targetErpIds = array_column($items, 'product_id');
|
||||||
|
|
||||||
// 一次性查出所有相關的 Product
|
// 一次性查出所有相關的 Product
|
||||||
$products = $this->productService->findByExternalPosIds($posProductIds)->keyBy('external_pos_id');
|
$productsById = $this->productService->findByIds($targetErpIds)->keyBy('id');
|
||||||
|
|
||||||
|
$resolvedProducts = [];
|
||||||
$missingIds = [];
|
$missingIds = [];
|
||||||
foreach ($posProductIds as $id) {
|
|
||||||
if (!$products->has($id)) {
|
foreach ($items as $index => $item) {
|
||||||
$missingIds[] = $id;
|
$productId = $item['product_id'];
|
||||||
|
$product = $productsById->get($productId);
|
||||||
|
|
||||||
|
if ($product) {
|
||||||
|
$resolvedProducts[$index] = $product;
|
||||||
|
} else {
|
||||||
|
$missingIds[] = $productId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($missingIds)) {
|
if (!empty($missingIds)) {
|
||||||
// 回報所有缺漏的 ID
|
|
||||||
throw ValidationException::withMessages([
|
throw ValidationException::withMessages([
|
||||||
'items' => ["The following products are not found: " . implode(', ', $missingIds) . ". Please sync products first."]
|
'items' => ["The following product IDs are not found: " . implode(', ', array_unique($missingIds)) . ". Please ensure these products exist in the system."]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 執行寫入交易 ---
|
// --- 執行寫入交易 ---
|
||||||
$result = DB::transaction(function () use ($data, $items, $products) {
|
$result = DB::transaction(function () use ($data, $items, $resolvedProducts) {
|
||||||
// 1. 建立訂單
|
// 1. 查找倉庫(提前至建立訂單前,以便判定來源)
|
||||||
|
$warehouseCode = $data['warehouse_code'];
|
||||||
|
$warehouses = $this->inventoryService->getWarehousesByCodes([$warehouseCode]);
|
||||||
|
|
||||||
|
if ($warehouses->isEmpty()) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'warehouse_code' => ["Warehouse with code {$warehouseCode} not found."]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
$warehouse = $warehouses->first();
|
||||||
|
$warehouseId = $warehouse->id;
|
||||||
|
|
||||||
|
// 2. 自動判定來源:若是販賣機倉庫則標記為 vending,其餘為 pos
|
||||||
|
$source = ($warehouse->type === \App\Enums\WarehouseType::VENDING) ? 'vending' : 'pos';
|
||||||
|
|
||||||
|
// 3. 建立訂單
|
||||||
$order = SalesOrder::create([
|
$order = SalesOrder::create([
|
||||||
'external_order_id' => $data['external_order_id'],
|
'external_order_id' => $data['external_order_id'],
|
||||||
|
'name' => $data['name'],
|
||||||
'status' => 'completed',
|
'status' => 'completed',
|
||||||
'payment_method' => $data['payment_method'] ?? 'cash',
|
'payment_method' => $data['payment_method'] ?? 'cash',
|
||||||
'total_amount' => 0,
|
'total_amount' => $data['total_amount'],
|
||||||
|
'total_qty' => $data['total_qty'],
|
||||||
'sold_at' => $data['sold_at'] ?? now(),
|
'sold_at' => $data['sold_at'] ?? now(),
|
||||||
'raw_payload' => $data,
|
'raw_payload' => $data,
|
||||||
'source' => $data['source'] ?? 'pos',
|
'source' => $source,
|
||||||
'source_label' => $data['source_label'] ?? null,
|
'source_label' => $data['source_label'] ?? null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 2. 查找或建立倉庫
|
|
||||||
$warehouseId = $data['warehouse_id'] ?? null;
|
|
||||||
|
|
||||||
if (empty($warehouseId)) {
|
|
||||||
$warehouseName = $data['warehouse'] ?? '銷售倉庫';
|
|
||||||
$warehouse = $this->inventoryService->findOrCreateWarehouseByName($warehouseName);
|
|
||||||
$warehouseId = $warehouse->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
$totalAmount = 0;
|
$totalAmount = 0;
|
||||||
|
|
||||||
// 3. 處理訂單明細
|
// 3. 處理訂單明細
|
||||||
$orderItemsData = [];
|
$orderItemsData = [];
|
||||||
foreach ($items as $itemData) {
|
foreach ($items as $index => $itemData) {
|
||||||
$product = $products->get($itemData['pos_product_id']);
|
$product = $resolvedProducts[$index];
|
||||||
|
|
||||||
$qty = $itemData['qty'];
|
$qty = $itemData['qty'];
|
||||||
$price = $itemData['price'];
|
$price = $itemData['price'];
|
||||||
|
$batchNumber = $itemData['batch_number'] ?? null;
|
||||||
$lineTotal = $qty * $price;
|
$lineTotal = $qty * $price;
|
||||||
$totalAmount += $lineTotal;
|
$totalAmount += $lineTotal;
|
||||||
|
|
||||||
@@ -131,7 +146,11 @@ class SyncOrderAction
|
|||||||
$warehouseId,
|
$warehouseId,
|
||||||
$qty,
|
$qty,
|
||||||
"POS Order: " . $order->external_order_id,
|
"POS Order: " . $order->external_order_id,
|
||||||
true
|
true,
|
||||||
|
null, // Slot (location)
|
||||||
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
|
$order->id,
|
||||||
|
$batchNumber
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -90,14 +90,16 @@ class SyncVendingOrderAction
|
|||||||
'source_label' => $data['machine_id'] ?? null,
|
'source_label' => $data['machine_id'] ?? null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 2. 查找或建立倉庫
|
// 2. 查找倉庫
|
||||||
$warehouseId = $data['warehouse_id'] ?? null;
|
$warehouseCode = $data['warehouse_code'];
|
||||||
|
$warehouses = $this->inventoryService->getWarehousesByCodes([$warehouseCode]);
|
||||||
if (empty($warehouseId)) {
|
|
||||||
$warehouseName = $data['warehouse'] ?? '販賣機倉庫';
|
if ($warehouses->isEmpty()) {
|
||||||
$warehouse = $this->inventoryService->findOrCreateWarehouseByName($warehouseName);
|
throw ValidationException::withMessages([
|
||||||
$warehouseId = $warehouse->id;
|
'warehouse_code' => ["Warehouse with code {$warehouseCode} not found."]
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
$warehouseId = $warehouses->first()->id;
|
||||||
|
|
||||||
$totalAmount = 0;
|
$totalAmount = 0;
|
||||||
|
|
||||||
@@ -128,7 +130,10 @@ class SyncVendingOrderAction
|
|||||||
$warehouseId,
|
$warehouseId,
|
||||||
$qty,
|
$qty,
|
||||||
"Vending Order: " . $order->external_order_id,
|
"Vending Order: " . $order->external_order_id,
|
||||||
true
|
true,
|
||||||
|
null,
|
||||||
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
|
$order->id
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Integration\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
|
class InventorySyncController extends Controller
|
||||||
|
{
|
||||||
|
protected $inventoryService;
|
||||||
|
|
||||||
|
public function __construct(InventoryServiceInterface $inventoryService)
|
||||||
|
{
|
||||||
|
$this->inventoryService = $inventoryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提供外部 POS 查詢指定倉庫的商品庫存餘額
|
||||||
|
*
|
||||||
|
* @param string $warehouseCode
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
public function show(\Illuminate\Http\Request $request, string $warehouseCode): JsonResponse
|
||||||
|
{
|
||||||
|
// 透過 Service 調用跨模組庫存查詢功能,傳入篩選條件
|
||||||
|
$inventoryData = $this->inventoryService->getPosInventoryByWarehouseCode(
|
||||||
|
$warehouseCode,
|
||||||
|
$request->only(['product_id', 'barcode', 'code', 'external_pos_id'])
|
||||||
|
);
|
||||||
|
|
||||||
|
// 若回傳 null,表示尋無此倉庫代碼
|
||||||
|
if (is_null($inventoryData)) {
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => "Warehouse with code '{$warehouseCode}' not found.",
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 JSON 格式回傳組合好的商品庫存列表
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'warehouse_code' => $warehouseCode,
|
||||||
|
'data' => $inventoryData->map(function ($item) {
|
||||||
|
return [
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'external_pos_id' => $item->external_pos_id,
|
||||||
|
'product_code' => $item->product_code,
|
||||||
|
'product_name' => $item->product_name,
|
||||||
|
'barcode' => $item->barcode,
|
||||||
|
'category_name' => $item->category_name ?? '未分類',
|
||||||
|
'unit_name' => $item->unit_name ?? '個',
|
||||||
|
'price' => (float) $item->price,
|
||||||
|
'brand' => $item->brand,
|
||||||
|
'specification' => $item->specification,
|
||||||
|
'batch_number' => $item->batch_number,
|
||||||
|
'expiry_date' => $item->expiry_date,
|
||||||
|
'quantity' => (float) $item->total_quantity,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
], 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,8 +23,8 @@ class ProductSyncController extends Controller
|
|||||||
'name' => 'required|string|max:255',
|
'name' => 'required|string|max:255',
|
||||||
'price' => 'nullable|numeric|min:0|max:99999999.99',
|
'price' => 'nullable|numeric|min:0|max:99999999.99',
|
||||||
'barcode' => 'nullable|string|max:100',
|
'barcode' => 'nullable|string|max:100',
|
||||||
'category' => 'nullable|string|max:100',
|
'category' => 'required|string|max:100',
|
||||||
'unit' => 'nullable|string|max:100',
|
'unit' => 'required|string|max:100',
|
||||||
'brand' => 'nullable|string|max:100',
|
'brand' => 'nullable|string|max:100',
|
||||||
'specification' => 'nullable|string|max:255',
|
'specification' => 'nullable|string|max:255',
|
||||||
'cost_price' => 'nullable|numeric|min:0|max:99999999.99',
|
'cost_price' => 'nullable|numeric|min:0|max:99999999.99',
|
||||||
@@ -41,6 +41,8 @@ class ProductSyncController extends Controller
|
|||||||
'data' => [
|
'data' => [
|
||||||
'id' => $product->id,
|
'id' => $product->id,
|
||||||
'external_pos_id' => $product->external_pos_id,
|
'external_pos_id' => $product->external_pos_id,
|
||||||
|
'code' => $product->code,
|
||||||
|
'barcode' => $product->barcode,
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@@ -50,4 +52,63 @@ class ProductSyncController extends Controller
|
|||||||
], 500);
|
], 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜尋商品(供外部 API 使用)。
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'product_id' => 'nullable|integer',
|
||||||
|
'external_pos_id' => 'nullable|string|max:255',
|
||||||
|
'barcode' => 'nullable|string|max:100',
|
||||||
|
'code' => 'nullable|string|max:100',
|
||||||
|
'category' => 'nullable|string|max:100',
|
||||||
|
'updated_after' => 'nullable|date',
|
||||||
|
'per_page' => 'nullable|integer|min:1|max:100',
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$perPage = $request->input('per_page', 50);
|
||||||
|
$products = $this->productService->searchProducts($request->all(), $perPage);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'success',
|
||||||
|
'data' => $products->getCollection()->map(function ($product) {
|
||||||
|
return [
|
||||||
|
'id' => $product->id,
|
||||||
|
'code' => $product->code,
|
||||||
|
'barcode' => $product->barcode,
|
||||||
|
'name' => $product->name,
|
||||||
|
'external_pos_id' => $product->external_pos_id,
|
||||||
|
'category_name' => $product->category?->name ?? '未分類',
|
||||||
|
'brand' => $product->brand,
|
||||||
|
'specification' => $product->specification,
|
||||||
|
'unit_name' => $product->baseUnit?->name ?? '個',
|
||||||
|
'price' => (float) $product->price,
|
||||||
|
'cost_price' => (float) $product->cost_price,
|
||||||
|
'member_price' => (float) $product->member_price,
|
||||||
|
'wholesale_price' => (float) $product->wholesale_price,
|
||||||
|
'is_active' => (bool) $product->is_active,
|
||||||
|
'updated_at' => $product->updated_at->format('Y-m-d H:i:s'),
|
||||||
|
];
|
||||||
|
}),
|
||||||
|
'meta' => [
|
||||||
|
'current_page' => $products->currentPage(),
|
||||||
|
'last_page' => $products->lastPage(),
|
||||||
|
'per_page' => $products->perPage(),
|
||||||
|
'total' => $products->total(),
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error('Product Search Failed', ['error' => $e->getMessage()]);
|
||||||
|
return response()->json([
|
||||||
|
'status' => 'error',
|
||||||
|
'message' => 'Search failed: ' . $e->getMessage(),
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ class SalesOrderController extends Controller
|
|||||||
|
|
||||||
// 搜尋篩選 (外部訂單號)
|
// 搜尋篩選 (外部訂單號)
|
||||||
if ($request->filled('search')) {
|
if ($request->filled('search')) {
|
||||||
$query->where('external_order_id', 'like', '%' . $request->search . '%');
|
$query->where(function ($q) use ($request) {
|
||||||
|
$q->where('external_order_id', 'like', '%' . $request->search . '%')
|
||||||
|
->orWhere('name', 'like', '%' . $request->search . '%');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 來源篩選
|
// 來源篩選
|
||||||
@@ -26,15 +29,26 @@ class SalesOrderController extends Controller
|
|||||||
$query->where('source', $request->source);
|
$query->where('source', $request->source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 付款方式篩選
|
||||||
|
if ($request->filled('payment_method')) {
|
||||||
|
$query->where('payment_method', $request->payment_method);
|
||||||
|
}
|
||||||
|
|
||||||
// 排序
|
// 排序
|
||||||
$query->orderBy('sold_at', 'desc');
|
$query->orderBy('sold_at', 'desc');
|
||||||
|
|
||||||
$orders = $query->paginate($request->input('per_page', 10))
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = (int) $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
$orders = $query->paginate($perPage)
|
||||||
->withQueryString();
|
->withQueryString();
|
||||||
|
|
||||||
return Inertia::render('Integration/SalesOrders/Index', [
|
return Inertia::render('Integration/SalesOrders/Index', [
|
||||||
'orders' => $orders,
|
'orders' => $orders,
|
||||||
'filters' => $request->only(['search', 'per_page', 'source']),
|
'filters' => $request->only(['search', 'per_page', 'source', 'status', 'payment_method']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ class SalesOrder extends Model
|
|||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'external_order_id',
|
'external_order_id',
|
||||||
|
'name',
|
||||||
'status',
|
'status',
|
||||||
'payment_method',
|
'payment_method',
|
||||||
'total_amount',
|
'total_amount',
|
||||||
|
'total_qty',
|
||||||
'sold_at',
|
'sold_at',
|
||||||
'raw_payload',
|
'raw_payload',
|
||||||
'source',
|
'source',
|
||||||
@@ -24,6 +26,7 @@ class SalesOrder extends Model
|
|||||||
'sold_at' => 'datetime',
|
'sold_at' => 'datetime',
|
||||||
'raw_payload' => 'array',
|
'raw_payload' => 'array',
|
||||||
'total_amount' => 'decimal:4',
|
'total_amount' => 'decimal:4',
|
||||||
|
'total_qty' => 'decimal:4',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function items(): HasMany
|
public function items(): HasMany
|
||||||
|
|||||||
@@ -23,12 +23,15 @@ class SyncOrderRequest extends FormRequest
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'external_order_id' => 'required|string',
|
'external_order_id' => 'required|string',
|
||||||
'warehouse' => 'nullable|string',
|
'name' => 'required|string|max:255',
|
||||||
'warehouse_id' => 'nullable|integer',
|
'warehouse_code' => 'required|string',
|
||||||
'payment_method' => 'nullable|string|in:cash,credit_card,line_pay,ecpay,transfer,other',
|
'payment_method' => 'nullable|string|in:cash,credit_card,line_pay,ecpay,transfer,other',
|
||||||
|
'total_amount' => 'required|numeric|min:0',
|
||||||
|
'total_qty' => 'required|numeric|min:0',
|
||||||
'sold_at' => 'nullable|date',
|
'sold_at' => 'nullable|date',
|
||||||
'items' => 'required|array|min:1',
|
'items' => 'required|array|min:1',
|
||||||
'items.*.pos_product_id' => 'required|string',
|
'items.*.product_id' => 'required|integer',
|
||||||
|
'items.*.batch_number' => 'nullable|string',
|
||||||
'items.*.qty' => 'required|numeric|min:0.0001',
|
'items.*.qty' => 'required|numeric|min:0.0001',
|
||||||
'items.*.price' => 'required|numeric|min:0',
|
'items.*.price' => 'required|numeric|min:0',
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ class SyncVendingOrderRequest extends FormRequest
|
|||||||
return [
|
return [
|
||||||
'external_order_id' => 'required|string',
|
'external_order_id' => 'required|string',
|
||||||
'machine_id' => 'nullable|string',
|
'machine_id' => 'nullable|string',
|
||||||
'warehouse' => 'nullable|string',
|
'warehouse_code' => 'required|string',
|
||||||
'warehouse_id' => 'nullable|integer',
|
|
||||||
'payment_method' => 'nullable|string|in:cash,electronic,line_pay,other',
|
'payment_method' => 'nullable|string|in:cash,electronic,line_pay,other',
|
||||||
'sold_at' => 'nullable|date',
|
'sold_at' => 'nullable|date',
|
||||||
'items' => 'required|array|min:1',
|
'items' => 'required|array|min:1',
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ use Illuminate\Support\Facades\Route;
|
|||||||
use App\Modules\Integration\Controllers\ProductSyncController;
|
use App\Modules\Integration\Controllers\ProductSyncController;
|
||||||
use App\Modules\Integration\Controllers\OrderSyncController;
|
use App\Modules\Integration\Controllers\OrderSyncController;
|
||||||
use App\Modules\Integration\Controllers\VendingOrderSyncController;
|
use App\Modules\Integration\Controllers\VendingOrderSyncController;
|
||||||
|
use App\Modules\Integration\Controllers\InventorySyncController;
|
||||||
|
|
||||||
Route::prefix('api/v1/integration')
|
Route::prefix('api/v1/integration')
|
||||||
->middleware(['api', 'throttle:integration', 'integration.tenant', 'auth:sanctum'])
|
->middleware(['api', 'throttle:integration', 'integration.tenant', 'auth:sanctum'])
|
||||||
->group(function () {
|
->group(function () {
|
||||||
|
Route::get('products', [ProductSyncController::class, 'index']);
|
||||||
Route::post('products/upsert', [ProductSyncController::class, 'upsert']);
|
Route::post('products/upsert', [ProductSyncController::class, 'upsert']);
|
||||||
Route::post('orders', [OrderSyncController::class, 'store']);
|
Route::post('orders', [OrderSyncController::class, 'store']);
|
||||||
Route::post('vending/orders', [VendingOrderSyncController::class, 'store']);
|
Route::post('vending/orders', [VendingOrderSyncController::class, 'store']);
|
||||||
|
Route::get('inventory/{warehouse_code}', [InventorySyncController::class, 'show']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -21,9 +21,12 @@ interface InventoryServiceInterface
|
|||||||
* @param string|null $reason
|
* @param string|null $reason
|
||||||
* @param bool $force
|
* @param bool $force
|
||||||
* @param string|null $slot
|
* @param string|null $slot
|
||||||
|
* @param string|null $referenceType
|
||||||
|
* @param int|string|null $referenceId
|
||||||
|
* @param string|null $batchNumber
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null, bool $force = false, ?string $slot = null): void;
|
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null, bool $force = false, ?string $slot = null, ?string $referenceType = null, $referenceId = null, ?string $batchNumber = null): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all active warehouses.
|
* Get all active warehouses.
|
||||||
@@ -131,7 +134,7 @@ interface InventoryServiceInterface
|
|||||||
* @param int $perPage 每頁筆數
|
* @param int $perPage 每頁筆數
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getStockQueryData(array $filters = [], int $perPage = 10): array;
|
public function getStockQueryData(array $filters = [], ?int $perPage = null): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get statistics for the dashboard.
|
* Get statistics for the dashboard.
|
||||||
@@ -157,4 +160,32 @@ interface InventoryServiceInterface
|
|||||||
* Get items expiring soon for dashboard.
|
* Get items expiring soon for dashboard.
|
||||||
*/
|
*/
|
||||||
public function getExpiringSoon(int $limit = 5): \Illuminate\Support\Collection;
|
public function getExpiringSoon(int $limit = 5): \Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get inventory summary (group by product) for a specific warehouse code
|
||||||
|
*
|
||||||
|
* @param string $code
|
||||||
|
* @param array $filters
|
||||||
|
* @return \Illuminate\Support\Collection|null
|
||||||
|
*/
|
||||||
|
public function getPosInventoryByWarehouseCode(string $code, array $filters = []);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 處理批量入庫邏輯 (含批號產生與現有批號累加)。
|
||||||
|
*
|
||||||
|
* @param \App\Modules\Inventory\Models\Warehouse $warehouse
|
||||||
|
* @param array $items 入庫品項清單
|
||||||
|
* @param array $meta 資料包含 inboundDate, reason, notes
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function processIncomingInventory(\App\Modules\Inventory\Models\Warehouse $warehouse, array $items, array $meta): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 處理單一庫存項目的調整。
|
||||||
|
*
|
||||||
|
* @param \App\Modules\Inventory\Models\Inventory $inventory
|
||||||
|
* @param array $data 包含 quantity, operation, type, reason, unit_cost 等
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function adjustInventory(\App\Modules\Inventory\Models\Inventory $inventory, array $data): void;
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,14 @@ interface ProductServiceInterface
|
|||||||
*/
|
*/
|
||||||
public function findByExternalPosIds(array $externalPosIds);
|
public function findByExternalPosIds(array $externalPosIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 透過多個 ERP 內部 ID 查找產品。
|
||||||
|
*
|
||||||
|
* @param array $ids
|
||||||
|
* @return \Illuminate\Database\Eloquent\Collection
|
||||||
|
*/
|
||||||
|
public function findByIds(array $ids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 透過多個 ERP 商品代碼查找產品(供販賣機 API 使用)。
|
* 透過多個 ERP 商品代碼查找產品(供販賣機 API 使用)。
|
||||||
*
|
*
|
||||||
@@ -38,4 +46,53 @@ interface ProductServiceInterface
|
|||||||
* @return \Illuminate\Database\Eloquent\Collection
|
* @return \Illuminate\Database\Eloquent\Collection
|
||||||
*/
|
*/
|
||||||
public function findByCodes(array $codes);
|
public function findByCodes(array $codes);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立新商品。
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return \App\Modules\Inventory\Models\Product
|
||||||
|
*/
|
||||||
|
public function createProduct(array $data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新現有商品。
|
||||||
|
*
|
||||||
|
* @param \App\Modules\Inventory\Models\Product $product
|
||||||
|
* @param array $data
|
||||||
|
* @return \App\Modules\Inventory\Models\Product
|
||||||
|
*/
|
||||||
|
public function updateProduct(\App\Modules\Inventory\Models\Product $product, array $data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成隨機 8 碼代號 (大寫英文+數字)。
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function generateRandomCode();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成隨機 13 碼條碼 (純數字)。
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function generateRandomBarcode();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根據條碼或代號查找商品。
|
||||||
|
*
|
||||||
|
* @param string|null $barcode
|
||||||
|
* @param string|null $code
|
||||||
|
* @return \App\Modules\Inventory\Models\Product|null
|
||||||
|
*/
|
||||||
|
public function findByBarcodeOrCode(?string $barcode, ?string $code);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜尋商品(供外部 API 使用)。
|
||||||
|
*
|
||||||
|
* @param array $filters
|
||||||
|
* @param int $perPage
|
||||||
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||||
|
*/
|
||||||
|
public function searchProducts(array $filters, int $perPage = 50);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,11 @@ class AdjustDocController extends Controller
|
|||||||
$query->where('warehouse_id', $request->warehouse_id);
|
$query->where('warehouse_id', $request->warehouse_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$docs = $query->orderByDesc('created_at')
|
$docs = $query->orderByDesc('created_at')
|
||||||
->paginate($perPage)
|
->paginate($perPage)
|
||||||
->withQueryString()
|
->withQueryString()
|
||||||
@@ -59,7 +63,7 @@ class AdjustDocController extends Controller
|
|||||||
|
|
||||||
return Inertia::render('Inventory/Adjust/Index', [
|
return Inertia::render('Inventory/Adjust/Index', [
|
||||||
'docs' => $docs,
|
'docs' => $docs,
|
||||||
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||||
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
|
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,9 +35,11 @@ class CountDocController extends Controller
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
if (!in_array($perPage, [10, 20, 50, 100])) {
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
$perPage = 10;
|
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
$countQuery = function ($query) {
|
$countQuery = function ($query) {
|
||||||
@@ -65,7 +67,7 @@ class CountDocController extends Controller
|
|||||||
|
|
||||||
return Inertia::render('Inventory/Count/Index', [
|
return Inertia::render('Inventory/Count/Index', [
|
||||||
'docs' => $docs,
|
'docs' => $docs,
|
||||||
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||||
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
|
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,11 @@ class GoodsReceiptController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 每頁筆數
|
// 每頁筆數
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
$receipts = $query->orderBy('created_at', 'desc')
|
$receipts = $query->orderBy('created_at', 'desc')
|
||||||
->paginate($perPage)
|
->paginate($perPage)
|
||||||
@@ -88,7 +92,7 @@ class GoodsReceiptController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show($id)
|
public function show(Request $request, $id)
|
||||||
{
|
{
|
||||||
$receipt = GoodsReceipt::with([
|
$receipt = GoodsReceipt::with([
|
||||||
'warehouse',
|
'warehouse',
|
||||||
@@ -105,7 +109,12 @@ class GoodsReceiptController extends Controller
|
|||||||
$receipt->items_sum_total_amount = $receipt->items->sum('total_amount');
|
$receipt->items_sum_total_amount = $receipt->items->sum('total_amount');
|
||||||
|
|
||||||
return Inertia::render('Inventory/GoodsReceipt/Show', [
|
return Inertia::render('Inventory/GoodsReceipt/Show', [
|
||||||
'receipt' => $receipt
|
'receipt' => $receipt,
|
||||||
|
'navigation' => [
|
||||||
|
'from' => $request->query('from'),
|
||||||
|
'from_id' => $request->query('from_id'),
|
||||||
|
'from_label' => $request->query('from_label'),
|
||||||
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +133,7 @@ class GoodsReceiptController extends Controller
|
|||||||
'id' => $po->id,
|
'id' => $po->id,
|
||||||
'code' => $po->code,
|
'code' => $po->code,
|
||||||
'status' => $po->status,
|
'status' => $po->status,
|
||||||
|
'supplierId' => $po->vendor_id, // Alias for frontend
|
||||||
'vendor_id' => $po->vendor_id,
|
'vendor_id' => $po->vendor_id,
|
||||||
'vendor_name' => $po->vendor?->name ?? '',
|
'vendor_name' => $po->vendor?->name ?? '',
|
||||||
'warehouse_id' => $po->warehouse_id,
|
'warehouse_id' => $po->warehouse_id,
|
||||||
@@ -131,15 +141,35 @@ class GoodsReceiptController extends Controller
|
|||||||
'items' => $po->items->map(function ($item) use ($products) {
|
'items' => $po->items->map(function ($item) use ($products) {
|
||||||
$product = $products->get($item->product_id);
|
$product = $products->get($item->product_id);
|
||||||
$remaining = max(0, $item->quantity - ($item->received_quantity ?? 0));
|
$remaining = max(0, $item->quantity - ($item->received_quantity ?? 0));
|
||||||
|
|
||||||
|
// 獲取單位名稱
|
||||||
|
$baseUnitName = $product?->baseUnit?->name ?? '個';
|
||||||
|
$largeUnitName = $product?->largeUnit?->name ?? '';
|
||||||
|
|
||||||
|
// 判斷當前採購使用的單位 (這需要從 PurchaseOrderItem 獲取 unit_id 並與產品的 large_unit_id 比較)
|
||||||
|
$selectedUnit = 'base';
|
||||||
|
if ($item->unit_id && $product && $item->unit_id == $product->large_unit_id) {
|
||||||
|
$selectedUnit = 'large';
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
|
'productId' => $item->product_id, // Alias for frontend
|
||||||
'product_id' => $item->product_id,
|
'product_id' => $item->product_id,
|
||||||
|
'productName' => $product?->name ?? '', // Alias for frontend
|
||||||
'product_name' => $product?->name ?? '',
|
'product_name' => $product?->name ?? '',
|
||||||
'product_code' => $product?->code ?? '',
|
'product_code' => $product?->code ?? '',
|
||||||
'unit' => $product?->baseUnit?->name ?? '個',
|
'unit' => $product?->baseUnit?->name ?? '個', // 預設顯示文字
|
||||||
|
'selectedUnit' => $selectedUnit,
|
||||||
|
'base_unit_id' => $product?->base_unit_id,
|
||||||
|
'base_unit_name' => $baseUnitName,
|
||||||
|
'large_unit_id' => $product?->large_unit_id,
|
||||||
|
'large_unit_name' => $largeUnitName,
|
||||||
|
'conversion_rate' => $product?->conversion_rate ?? 1,
|
||||||
'quantity' => $item->quantity,
|
'quantity' => $item->quantity,
|
||||||
'received_quantity' => $item->received_quantity ?? 0,
|
'received_quantity' => $item->received_quantity ?? 0,
|
||||||
'remaining' => $remaining,
|
'remaining' => $remaining,
|
||||||
|
'unitPrice' => $item->unit_price, // Alias for frontend
|
||||||
'unit_price' => $item->unit_price,
|
'unit_price' => $item->unit_price,
|
||||||
];
|
];
|
||||||
})->filter(fn($item) => $item['remaining'] > 0)->values(),
|
})->filter(fn($item) => $item['remaining'] > 0)->values(),
|
||||||
@@ -158,7 +188,190 @@ class GoodsReceiptController extends Controller
|
|||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$validated = $request->validate([
|
$validated = $request->validate($this->getItemValidationRules());
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->goodsReceiptService->store($request->all());
|
||||||
|
return redirect()->route('goods-receipts.index')->with('success', '進貨草稿已建立');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return back()->with('error', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 編輯進貨單(僅草稿/退回狀態)
|
||||||
|
*/
|
||||||
|
public function edit(GoodsReceipt $goodsReceipt)
|
||||||
|
{
|
||||||
|
if (!in_array($goodsReceipt->status, [GoodsReceipt::STATUS_DRAFT, 'rejected'])) {
|
||||||
|
return redirect()->route('goods-receipts.show', $goodsReceipt->id)
|
||||||
|
->with('error', '只有草稿或被退回的進貨單可以編輯。');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 載入品項與產品資訊
|
||||||
|
$goodsReceipt->load('items');
|
||||||
|
|
||||||
|
// 取得品項關聯的商品資訊
|
||||||
|
$productIds = $goodsReceipt->items->pluck('product_id')->unique()->toArray();
|
||||||
|
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
|
||||||
|
|
||||||
|
// 如果是標準採購,取得對應採購單的品項,以帶出預定數量與已收數量
|
||||||
|
$poItems = collect();
|
||||||
|
$po = null;
|
||||||
|
if ($goodsReceipt->type === 'standard' && $goodsReceipt->purchase_order_id) {
|
||||||
|
$po = clone $this->procurementService->getPurchaseOrdersByIds([$goodsReceipt->purchase_order_id], ['items', 'vendor'])->first();
|
||||||
|
if ($po) {
|
||||||
|
$poItems = $po->items->keyBy('id');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式化品項資料
|
||||||
|
$formattedItems = $goodsReceipt->items->map(function ($item) use ($products, $poItems) {
|
||||||
|
$product = $products->get($item->product_id);
|
||||||
|
$poItem = $poItems->get($item->purchase_order_item_id);
|
||||||
|
|
||||||
|
// 判斷單位
|
||||||
|
$selectedUnit = 'base';
|
||||||
|
if ($poItem && $product && $poItem->unit_id && $poItem->unit_id == $product->large_unit_id) {
|
||||||
|
$selectedUnit = 'large';
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'purchase_order_item_id' => $item->purchase_order_item_id,
|
||||||
|
'product_name' => $product?->name ?? '',
|
||||||
|
'product_code' => $product?->code ?? '',
|
||||||
|
'unit' => $poItem && $selectedUnit === 'large' ? ($product?->largeUnit?->name ?? '') : ($product?->baseUnit?->name ?? '個'),
|
||||||
|
'selectedUnit' => $selectedUnit,
|
||||||
|
'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' => $product?->conversion_rate ?? 1,
|
||||||
|
'quantity_ordered' => $poItem ? $poItem->quantity : null,
|
||||||
|
'quantity_received_so_far' => $poItem ? ($poItem->received_quantity ?? 0) : null,
|
||||||
|
'quantity_received' => (float) $item->quantity_received,
|
||||||
|
'unit_price' => (float) $item->unit_price,
|
||||||
|
'subtotal' => (float) $item->total_amount,
|
||||||
|
'batch_number' => $item->batch_number ?? '',
|
||||||
|
'batchMode' => 'existing',
|
||||||
|
'originCountry' => 'TW',
|
||||||
|
'expiry_date' => $item->expiry_date ?? '',
|
||||||
|
];
|
||||||
|
})->values();
|
||||||
|
|
||||||
|
// 同 create() 一樣傳入所需的 props
|
||||||
|
$pendingPOs = $this->procurementService->getPendingPurchaseOrders();
|
||||||
|
$productIdsForPOs = $pendingPOs->flatMap(fn($po) => $po->items->pluck('product_id'))->unique()->filter()->toArray();
|
||||||
|
$productsForPOs = $this->inventoryService->getProductsByIds($productIdsForPOs)->keyBy('id');
|
||||||
|
|
||||||
|
$formattedPOs = $pendingPOs->map(function ($po) use ($productsForPOs) {
|
||||||
|
return [
|
||||||
|
'id' => $po->id,
|
||||||
|
'code' => $po->code,
|
||||||
|
'status' => $po->status,
|
||||||
|
'supplierId' => $po->vendor_id,
|
||||||
|
'vendor_id' => $po->vendor_id,
|
||||||
|
'vendor_name' => $po->vendor?->name ?? '',
|
||||||
|
'warehouse_id' => $po->warehouse_id,
|
||||||
|
'order_date' => $po->order_date,
|
||||||
|
'items' => $po->items->map(function ($item) use ($productsForPOs) {
|
||||||
|
$product = $productsForPOs->get($item->product_id);
|
||||||
|
$remaining = max(0, $item->quantity - ($item->received_quantity ?? 0));
|
||||||
|
|
||||||
|
$selectedUnit = 'base';
|
||||||
|
if ($item->unit_id && $product && $item->unit_id == $product->large_unit_id) {
|
||||||
|
$selectedUnit = 'large';
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => $item->id,
|
||||||
|
'productId' => $item->product_id,
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'productName' => $product?->name ?? '',
|
||||||
|
'product_name' => $product?->name ?? '',
|
||||||
|
'product_code' => $product?->code ?? '',
|
||||||
|
'unit' => $item->unit_id && $product && $item->unit_id == $product->large_unit_id ? ($product?->largeUnit?->name ?? '') : ($product?->baseUnit?->name ?? '個'),
|
||||||
|
'selectedUnit' => $selectedUnit,
|
||||||
|
'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' => $product?->conversion_rate ?? 1,
|
||||||
|
'quantity' => $item->quantity,
|
||||||
|
'received_quantity' => $item->received_quantity ?? 0,
|
||||||
|
'remaining' => $remaining,
|
||||||
|
'unitPrice' => $item->unit_price,
|
||||||
|
'unit_price' => $item->unit_price,
|
||||||
|
];
|
||||||
|
})->filter(fn($item) => $item['remaining'] > 0)->values(),
|
||||||
|
];
|
||||||
|
})->filter(fn($po) => $po['items']->count() > 0)->values();
|
||||||
|
|
||||||
|
$vendors = $this->procurementService->getAllVendors();
|
||||||
|
|
||||||
|
// Manual Hydration for Vendor
|
||||||
|
$vendor = null;
|
||||||
|
if ($goodsReceipt->vendor_id) {
|
||||||
|
$vendor = $this->procurementService->getVendorsByIds([$goodsReceipt->vendor_id])->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 格式化 Purchase Order 給前端顯示
|
||||||
|
$formattedPO = null;
|
||||||
|
if ($po) {
|
||||||
|
$formattedPO = [
|
||||||
|
'id' => $po->id,
|
||||||
|
'code' => $po->code,
|
||||||
|
'status' => $po->status,
|
||||||
|
'vendor_id' => $po->vendor_id,
|
||||||
|
'vendor_name' => $po->vendor?->name ?? '',
|
||||||
|
'warehouse_id' => $po->warehouse_id,
|
||||||
|
'order_date' => $po->order_date,
|
||||||
|
'items' => $po->items->toArray(), // simplified since we just need items.length for display
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Inertia::render('Inventory/GoodsReceipt/Create', [
|
||||||
|
'warehouses' => $this->inventoryService->getAllWarehouses(),
|
||||||
|
'pendingPurchaseOrders' => $formattedPOs,
|
||||||
|
'vendors' => $vendors,
|
||||||
|
'receipt' => [
|
||||||
|
'id' => $goodsReceipt->id,
|
||||||
|
'code' => $goodsReceipt->code,
|
||||||
|
'type' => $goodsReceipt->type,
|
||||||
|
'warehouse_id' => $goodsReceipt->warehouse_id,
|
||||||
|
'vendor_id' => $goodsReceipt->vendor_id,
|
||||||
|
'vendor' => $vendor,
|
||||||
|
'purchase_order_id' => $goodsReceipt->purchase_order_id,
|
||||||
|
'purchase_order' => $formattedPO,
|
||||||
|
'received_date' => \Carbon\Carbon::parse($goodsReceipt->received_date)->format('Y-m-d'),
|
||||||
|
'remarks' => $goodsReceipt->remarks,
|
||||||
|
'items' => $formattedItems,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新進貨單
|
||||||
|
*/
|
||||||
|
public function update(Request $request, GoodsReceipt $goodsReceipt)
|
||||||
|
{
|
||||||
|
$validated = $request->validate($this->getItemValidationRules());
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->goodsReceiptService->update($goodsReceipt, $request->all());
|
||||||
|
return redirect()->route('goods-receipts.show', $goodsReceipt->id)->with('success', '進貨單已更新');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return back()->with('error', $e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取得品項驗證規則
|
||||||
|
*/
|
||||||
|
private function getItemValidationRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
'warehouse_id' => 'required|exists:warehouses,id',
|
'warehouse_id' => 'required|exists:warehouses,id',
|
||||||
'type' => 'required|in:standard,miscellaneous,other',
|
'type' => 'required|in:standard,miscellaneous,other',
|
||||||
'purchase_order_id' => 'nullable|required_if:type,standard|exists:purchase_orders,id',
|
'purchase_order_id' => 'nullable|required_if:type,standard|exists:purchase_orders,id',
|
||||||
@@ -169,18 +382,12 @@ class GoodsReceiptController extends Controller
|
|||||||
'items.*.product_id' => 'required|integer|exists:products,id',
|
'items.*.product_id' => 'required|integer|exists:products,id',
|
||||||
'items.*.purchase_order_item_id' => 'nullable|required_if:type,standard|integer',
|
'items.*.purchase_order_item_id' => 'nullable|required_if:type,standard|integer',
|
||||||
'items.*.quantity_received' => 'required|numeric|min:0',
|
'items.*.quantity_received' => 'required|numeric|min:0',
|
||||||
'items.*.unit_price' => 'required|numeric|min:0',
|
'items.*.unit_price' => 'nullable|numeric|min:0',
|
||||||
|
'items.*.subtotal' => 'nullable|numeric|min:0',
|
||||||
'items.*.batch_number' => 'nullable|string',
|
'items.*.batch_number' => 'nullable|string',
|
||||||
'items.*.expiry_date' => 'nullable|date',
|
'items.*.expiry_date' => 'nullable|date',
|
||||||
'force' => 'nullable|boolean',
|
'force' => 'nullable|boolean',
|
||||||
]);
|
];
|
||||||
|
|
||||||
try {
|
|
||||||
$this->goodsReceiptService->store($request->all());
|
|
||||||
return redirect()->route('goods-receipts.index')->with('success', '進貨草稿已建立');
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return back()->with('error', $e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -220,6 +427,7 @@ class GoodsReceiptController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// API to search Products for Manual Entry
|
// API to search Products for Manual Entry
|
||||||
|
// 支援 query='*' 回傳所有商品(用於 SearchableSelect 下拉選單)
|
||||||
public function searchProducts(Request $request)
|
public function searchProducts(Request $request)
|
||||||
{
|
{
|
||||||
$search = $request->input('query');
|
$search = $request->input('query');
|
||||||
@@ -227,7 +435,12 @@ class GoodsReceiptController extends Controller
|
|||||||
return response()->json([]);
|
return response()->json([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$products = $this->inventoryService->getProductsByName($search);
|
// 萬用字元:回傳所有商品
|
||||||
|
if ($search === '*') {
|
||||||
|
$products = $this->inventoryService->getProductsByName('');
|
||||||
|
} else {
|
||||||
|
$products = $this->inventoryService->getProductsByName($search);
|
||||||
|
}
|
||||||
|
|
||||||
// Format for frontend
|
// Format for frontend
|
||||||
$mapped = $products->map(function($product) {
|
$mapped = $products->map(function($product) {
|
||||||
@@ -235,8 +448,8 @@ class GoodsReceiptController extends Controller
|
|||||||
'id' => $product->id,
|
'id' => $product->id,
|
||||||
'name' => $product->name,
|
'name' => $product->name,
|
||||||
'code' => $product->code,
|
'code' => $product->code,
|
||||||
'unit' => $product->baseUnit?->name ?? '個', // Ensure unit is included
|
'unit' => $product->baseUnit?->name ?? '個',
|
||||||
'price' => $product->purchase_price ?? 0, // Suggest price from product info if available
|
'price' => $product->purchase_price ?? 0,
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,12 @@ class InventoryAnalysisController extends Controller
|
|||||||
'warehouse_id', 'category_id', 'search', 'per_page', 'sort_by', 'sort_order', 'status'
|
'warehouse_id', 'category_id', 'search', 'per_page', 'sort_by', 'sort_order', 'status'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$analysisData = $this->turnoverService->getAnalysisData($filters, $request->input('per_page', 10));
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = (int) $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
$analysisData = $this->turnoverService->getAnalysisData($filters, $perPage);
|
||||||
$kpis = $this->turnoverService->getKPIs($filters);
|
$kpis = $this->turnoverService->getKPIs($filters);
|
||||||
|
|
||||||
return Inertia::render('Inventory/Analysis/Index', [
|
return Inertia::render('Inventory/Analysis/Index', [
|
||||||
|
|||||||
@@ -22,10 +22,14 @@ use App\Modules\Core\Contracts\CoreServiceInterface;
|
|||||||
class InventoryController extends Controller
|
class InventoryController extends Controller
|
||||||
{
|
{
|
||||||
protected $coreService;
|
protected $coreService;
|
||||||
|
protected $inventoryService;
|
||||||
|
|
||||||
public function __construct(CoreServiceInterface $coreService)
|
public function __construct(
|
||||||
{
|
CoreServiceInterface $coreService,
|
||||||
|
\App\Modules\Inventory\Contracts\InventoryServiceInterface $inventoryService
|
||||||
|
) {
|
||||||
$this->coreService = $coreService;
|
$this->coreService = $coreService;
|
||||||
|
$this->inventoryService = $inventoryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function index(Request $request, Warehouse $warehouse)
|
public function index(Request $request, Warehouse $warehouse)
|
||||||
@@ -37,7 +41,7 @@ class InventoryController extends Controller
|
|||||||
'inventories.lastIncomingTransaction',
|
'inventories.lastIncomingTransaction',
|
||||||
'inventories.lastOutgoingTransaction'
|
'inventories.lastOutgoingTransaction'
|
||||||
]);
|
]);
|
||||||
$allProducts = Product::with('category')->get();
|
$allProducts = Product::select('id', 'name', 'code', 'category_id')->with('category:id,name')->get();
|
||||||
|
|
||||||
// 1. 準備 availableProducts
|
// 1. 準備 availableProducts
|
||||||
$availableProducts = $allProducts->map(function ($product) {
|
$availableProducts = $allProducts->map(function ($product) {
|
||||||
@@ -53,9 +57,31 @@ class InventoryController extends Controller
|
|||||||
->pluck('safety_stock', 'product_id')
|
->pluck('safety_stock', 'product_id')
|
||||||
->mapWithKeys(fn($val, $key) => [(string)$key => (float)$val]);
|
->mapWithKeys(fn($val, $key) => [(string)$key => (float)$val]);
|
||||||
|
|
||||||
$items = $warehouse->inventories()
|
$query = $warehouse->inventories()
|
||||||
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction'])
|
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction']);
|
||||||
->get();
|
|
||||||
|
// 加入搜尋過濾
|
||||||
|
if ($request->filled('search')) {
|
||||||
|
$search = $request->input('search');
|
||||||
|
$query->where(function ($q) use ($search) {
|
||||||
|
$q->where('batch_number', 'like', "%{$search}%")
|
||||||
|
->orWhere(\Illuminate\Support\Facades\DB::raw("CONCAT('BATCH-', inventories.id)"), 'like', "%{$search}%")
|
||||||
|
->orWhereHas('product', function ($pq) use ($search) {
|
||||||
|
$pq->where('name', 'like', "%{$search}%")
|
||||||
|
->orWhere('code', 'like', "%{$search}%");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入類型過濾
|
||||||
|
if ($request->filled('type') && $request->input('type') !== 'all') {
|
||||||
|
$type = $request->input('type');
|
||||||
|
$query->whereHas('product.category', function ($cq) use ($type) {
|
||||||
|
$cq->where('name', $type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$items = $query->get();
|
||||||
|
|
||||||
// 判斷是否為販賣機並調整分組
|
// 判斷是否為販賣機並調整分組
|
||||||
$isVending = $warehouse->type === 'vending';
|
$isVending = $warehouse->type === 'vending';
|
||||||
@@ -141,8 +167,8 @@ class InventoryController extends Controller
|
|||||||
public function create(Warehouse $warehouse)
|
public function create(Warehouse $warehouse)
|
||||||
{
|
{
|
||||||
// ... (unchanged) ...
|
// ... (unchanged) ...
|
||||||
$products = Product::with(['baseUnit', 'largeUnit'])
|
$products = Product::select('id', 'name', 'code', 'barcode', 'base_unit_id', 'large_unit_id', 'conversion_rate', 'cost_price')
|
||||||
->select('id', 'name', 'code', 'barcode', 'base_unit_id', 'large_unit_id', 'conversion_rate', 'cost_price')
|
->with(['baseUnit:id,name', 'largeUnit:id,name'])
|
||||||
->get()
|
->get()
|
||||||
->map(function ($product) {
|
->map(function ($product) {
|
||||||
return [
|
return [
|
||||||
@@ -182,97 +208,20 @@ class InventoryController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
return DB::transaction(function () use ($validated, $warehouse) {
|
return DB::transaction(function () use ($validated, $warehouse) {
|
||||||
foreach ($validated['items'] as $item) {
|
// 修正時間精度:使用 Carbon 解析,若含時間則保留並補上秒數,若只有日期則補上當前時間
|
||||||
// ... (略,傳遞 unit_cost 交給 Service 處理) ...
|
$dt = \Illuminate\Support\Carbon::parse($validated['inboundDate']);
|
||||||
// 這裡需要修改呼叫 Service 的地方或直接更新邏輯
|
if ($dt->hour === 0 && $dt->minute === 0 && $dt->second === 0) {
|
||||||
// 為求快速,我將在此更新邏輯
|
$dt->setTimeFrom(now());
|
||||||
|
} else {
|
||||||
$inventory = null;
|
$dt->setSecond(now()->second);
|
||||||
|
|
||||||
if ($item['batchMode'] === 'existing') {
|
|
||||||
// 模式 A:選擇現有批號 (包含已刪除的也要能找回來累加)
|
|
||||||
$inventory = Inventory::withTrashed()->findOrFail($item['inventoryId']);
|
|
||||||
if ($inventory->trashed()) {
|
|
||||||
$inventory->restore();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新成本 (若有傳入)
|
|
||||||
if (isset($item['unit_cost'])) {
|
|
||||||
$inventory->unit_cost = $item['unit_cost'];
|
|
||||||
}
|
|
||||||
} elseif ($item['batchMode'] === 'none') {
|
|
||||||
// 模式 C:不使用批號 (自動累加至 NO-BATCH)
|
|
||||||
$inventory = $warehouse->inventories()->withTrashed()->firstOrNew(
|
|
||||||
[
|
|
||||||
'product_id' => $item['productId'],
|
|
||||||
'batch_number' => 'NO-BATCH'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'quantity' => 0,
|
|
||||||
'unit_cost' => $item['unit_cost'] ?? 0,
|
|
||||||
'total_value' => 0,
|
|
||||||
'arrival_date' => $validated['inboundDate'],
|
|
||||||
'expiry_date' => null,
|
|
||||||
'origin_country' => 'TW',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($inventory->trashed()) {
|
|
||||||
$inventory->restore();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 模式 B:建立新批號
|
|
||||||
$originCountry = $item['originCountry'] ?? 'TW';
|
|
||||||
$product = Product::find($item['productId']);
|
|
||||||
|
|
||||||
$batchNumber = Inventory::generateBatchNumber(
|
|
||||||
$product->code ?? 'UNK',
|
|
||||||
$originCountry,
|
|
||||||
$validated['inboundDate']
|
|
||||||
);
|
|
||||||
|
|
||||||
// 檢查是否存在
|
|
||||||
$inventory = $warehouse->inventories()->withTrashed()->firstOrNew(
|
|
||||||
[
|
|
||||||
'product_id' => $item['productId'],
|
|
||||||
'batch_number' => $batchNumber
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'quantity' => 0,
|
|
||||||
'unit_cost' => $item['unit_cost'] ?? 0, // 新增
|
|
||||||
'total_value' => 0, // 稍後計算
|
|
||||||
'location' => $item['location'] ?? null,
|
|
||||||
'arrival_date' => $validated['inboundDate'],
|
|
||||||
'expiry_date' => $item['expiryDate'] ?? null,
|
|
||||||
'origin_country' => $originCountry,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($inventory->trashed()) {
|
|
||||||
$inventory->restore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$currentQty = $inventory->quantity;
|
|
||||||
$newQty = $currentQty + $item['quantity'];
|
|
||||||
|
|
||||||
$inventory->quantity = $newQty;
|
|
||||||
// 更新總價值
|
|
||||||
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
|
||||||
$inventory->save();
|
|
||||||
|
|
||||||
// 寫入異動紀錄
|
|
||||||
$inventory->transactions()->create([
|
|
||||||
'type' => '手動入庫',
|
|
||||||
'quantity' => $item['quantity'],
|
|
||||||
'unit_cost' => $inventory->unit_cost, // 記錄成本
|
|
||||||
'balance_before' => $currentQty,
|
|
||||||
'balance_after' => $newQty,
|
|
||||||
'reason' => $validated['reason'] . ($validated['notes'] ? ' - ' . $validated['notes'] : ''),
|
|
||||||
'actual_time' => $validated['inboundDate'],
|
|
||||||
'user_id' => auth()->id(),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
$inboundDateTime = $dt->toDateTimeString();
|
||||||
|
|
||||||
|
$this->inventoryService->processIncomingInventory($warehouse, $validated['items'], [
|
||||||
|
'inboundDate' => $inboundDateTime,
|
||||||
|
'reason' => $validated['reason'],
|
||||||
|
'notes' => $validated['notes'] ?? '',
|
||||||
|
]);
|
||||||
|
|
||||||
return redirect()->route('warehouses.inventory.index', $warehouse->id)
|
return redirect()->route('warehouses.inventory.index', $warehouse->id)
|
||||||
->with('success', '庫存記錄已儲存成功');
|
->with('success', '庫存記錄已儲存成功');
|
||||||
@@ -401,81 +350,7 @@ class InventoryController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
return DB::transaction(function () use ($validated, $inventory) {
|
return DB::transaction(function () use ($validated, $inventory) {
|
||||||
$currentQty = (float) $inventory->quantity;
|
$this->inventoryService->adjustInventory($inventory, $validated);
|
||||||
$newQty = (float) $validated['quantity'];
|
|
||||||
|
|
||||||
// 判斷是否來自調整彈窗 (包含 operation 參數)
|
|
||||||
$isAdjustment = isset($validated['operation']);
|
|
||||||
$changeQty = 0;
|
|
||||||
|
|
||||||
if ($isAdjustment) {
|
|
||||||
switch ($validated['operation']) {
|
|
||||||
case 'add':
|
|
||||||
$changeQty = (float) $validated['quantity'];
|
|
||||||
$newQty = $currentQty + $changeQty;
|
|
||||||
break;
|
|
||||||
case 'subtract':
|
|
||||||
$changeQty = -(float) $validated['quantity'];
|
|
||||||
$newQty = $currentQty + $changeQty;
|
|
||||||
break;
|
|
||||||
case 'set':
|
|
||||||
$changeQty = $newQty - $currentQty;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 來自編輯頁面,直接 Set
|
|
||||||
$changeQty = $newQty - $currentQty;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新成本 (若有傳)
|
|
||||||
if (isset($validated['unit_cost'])) {
|
|
||||||
$inventory->unit_cost = $validated['unit_cost'];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新庫存
|
|
||||||
$inventory->quantity = $newQty;
|
|
||||||
// 更新總值
|
|
||||||
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
|
||||||
$inventory->save();
|
|
||||||
|
|
||||||
// 異動類型映射
|
|
||||||
$type = $validated['type'] ?? ($isAdjustment ? 'manual_adjustment' : 'adjustment');
|
|
||||||
$typeMapping = [
|
|
||||||
'manual_adjustment' => '手動調整庫存',
|
|
||||||
'adjustment' => '盤點調整',
|
|
||||||
'purchase_in' => '採購進貨',
|
|
||||||
'sales_out' => '銷售出庫',
|
|
||||||
'return_in' => '退貨入庫',
|
|
||||||
'return_out' => '退貨出庫',
|
|
||||||
'transfer_in' => '撥補入庫',
|
|
||||||
'transfer_out' => '撥補出庫',
|
|
||||||
];
|
|
||||||
$chineseType = $typeMapping[$type] ?? $type;
|
|
||||||
|
|
||||||
// 如果是編輯頁面來的,且沒傳 type,設為手動編輯
|
|
||||||
if (!$isAdjustment && !isset($validated['type'])) {
|
|
||||||
$chineseType = '手動編輯';
|
|
||||||
}
|
|
||||||
|
|
||||||
// 整理原因
|
|
||||||
$reason = $validated['reason'] ?? ($isAdjustment ? '手動庫存調整' : '編輯頁面更新');
|
|
||||||
if (isset($validated['notes'])) {
|
|
||||||
$reason .= ' - ' . $validated['notes'];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 寫入異動紀錄
|
|
||||||
if (abs($changeQty) > 0.0001) {
|
|
||||||
$inventory->transactions()->create([
|
|
||||||
'type' => $chineseType,
|
|
||||||
'quantity' => $changeQty,
|
|
||||||
'unit_cost' => $inventory->unit_cost, // 記錄
|
|
||||||
'balance_before' => $currentQty,
|
|
||||||
'balance_after' => $newQty,
|
|
||||||
'reason' => $reason,
|
|
||||||
'actual_time' => now(),
|
|
||||||
'user_id' => auth()->id(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect()->route('warehouses.inventory.index', $inventory->warehouse_id)
|
return redirect()->route('warehouses.inventory.index', $inventory->warehouse_id)
|
||||||
->with('success', '庫存資料已更新');
|
->with('success', '庫存資料已更新');
|
||||||
|
|||||||
@@ -35,7 +35,12 @@ class InventoryReportController extends Controller
|
|||||||
$filters['date_to'] = date('Y-m-d');
|
$filters['date_to'] = date('Y-m-d');
|
||||||
}
|
}
|
||||||
|
|
||||||
$reportData = $this->reportService->getReportData($filters, $request->input('per_page', 10));
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = (int) $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
$reportData = $this->reportService->getReportData($filters, $perPage);
|
||||||
$summary = $this->reportService->getSummary($filters);
|
$summary = $this->reportService->getSummary($filters);
|
||||||
|
|
||||||
return Inertia::render('Inventory/Report/Index', [
|
return Inertia::render('Inventory/Report/Index', [
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ use App\Modules\Inventory\Imports\ProductImport;
|
|||||||
|
|
||||||
class ProductController extends Controller
|
class ProductController extends Controller
|
||||||
{
|
{
|
||||||
|
protected $productService;
|
||||||
|
|
||||||
|
public function __construct(\App\Modules\Inventory\Contracts\ProductServiceInterface $productService)
|
||||||
|
{
|
||||||
|
$this->productService = $productService;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 顯示資源列表。
|
* 顯示資源列表。
|
||||||
*/
|
*/
|
||||||
@@ -37,9 +43,11 @@ class ProductController extends Controller
|
|||||||
$query->where('category_id', $request->category_id);
|
$query->where('category_id', $request->category_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
if (!in_array($perPage, [10, 20, 50, 100])) {
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
$perPage = 10;
|
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sortField = $request->input('sort_field', 'id');
|
$sortField = $request->input('sort_field', 'id');
|
||||||
@@ -104,12 +112,12 @@ class ProductController extends Controller
|
|||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
$categories = Category::where('is_active', true)->get();
|
$categories = Category::select('id', 'name')->where('is_active', true)->get();
|
||||||
|
|
||||||
return Inertia::render('Product/Index', [
|
return Inertia::render('Product/Index', [
|
||||||
'products' => $products,
|
'products' => $products,
|
||||||
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
'categories' => $categories->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]),
|
'units' => Unit::select('id', 'name', 'code')->get()->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']),
|
'filters' => $request->only(['search', 'category_id', 'per_page', 'sort_field', 'sort_direction']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -164,8 +172,8 @@ class ProductController extends Controller
|
|||||||
public function create(): Response
|
public function create(): Response
|
||||||
{
|
{
|
||||||
return Inertia::render('Product/Create', [
|
return Inertia::render('Product/Create', [
|
||||||
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
'categories' => Category::select('id', 'name')->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]),
|
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,15 +201,7 @@ class ProductController extends Controller
|
|||||||
'is_active' => 'boolean',
|
'is_active' => 'boolean',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (empty($validated['code'])) {
|
$product = $this->productService->createProduct($validated);
|
||||||
$validated['code'] = $this->generateRandomCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($validated['barcode'])) {
|
|
||||||
$validated['barcode'] = $this->generateRandomBarcode();
|
|
||||||
}
|
|
||||||
|
|
||||||
$product = Product::create($validated);
|
|
||||||
|
|
||||||
return redirect()->route('products.index')->with('success', '商品已建立');
|
return redirect()->route('products.index')->with('success', '商品已建立');
|
||||||
}
|
}
|
||||||
@@ -231,8 +231,8 @@ class ProductController extends Controller
|
|||||||
'wholesale_price' => (float) $product->wholesale_price,
|
'wholesale_price' => (float) $product->wholesale_price,
|
||||||
'is_active' => (bool) $product->is_active,
|
'is_active' => (bool) $product->is_active,
|
||||||
],
|
],
|
||||||
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
'categories' => Category::select('id', 'name')->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]),
|
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,15 +260,7 @@ class ProductController extends Controller
|
|||||||
'is_active' => 'boolean',
|
'is_active' => 'boolean',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (empty($validated['code'])) {
|
$this->productService->updateProduct($product, $validated);
|
||||||
$validated['code'] = $this->generateRandomCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($validated['barcode'])) {
|
|
||||||
$validated['barcode'] = $this->generateRandomBarcode();
|
|
||||||
}
|
|
||||||
|
|
||||||
$product->update($validated);
|
|
||||||
|
|
||||||
if ($request->input('from') === 'show') {
|
if ($request->input('from') === 'show') {
|
||||||
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
|
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
|
||||||
@@ -292,7 +284,7 @@ class ProductController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function template()
|
public function template()
|
||||||
{
|
{
|
||||||
return Excel::download(new ProductTemplateExport, 'products_template.xlsx');
|
return Excel::download(new ProductTemplateExport, '商品匯入範本.xlsx');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -318,39 +310,4 @@ class ProductController extends Controller
|
|||||||
return redirect()->back()->withErrors(['file' => '匯入失敗: ' . $e->getMessage()]);
|
return redirect()->back()->withErrors(['file' => '匯入失敗: ' . $e->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成隨機 8 碼代號 (大寫英文+數字)
|
|
||||||
*/
|
|
||||||
private function generateRandomCode(): string
|
|
||||||
{
|
|
||||||
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
||||||
$code = '';
|
|
||||||
|
|
||||||
do {
|
|
||||||
$code = '';
|
|
||||||
for ($i = 0; $i < 8; $i++) {
|
|
||||||
$code .= $characters[rand(0, strlen($characters) - 1)];
|
|
||||||
}
|
|
||||||
} while (Product::where('code', $code)->exists());
|
|
||||||
|
|
||||||
return $code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成隨機 13 碼條碼 (純數字)
|
|
||||||
*/
|
|
||||||
private function generateRandomBarcode(): string
|
|
||||||
{
|
|
||||||
$barcode = '';
|
|
||||||
|
|
||||||
do {
|
|
||||||
$barcode = '';
|
|
||||||
for ($i = 0; $i < 13; $i++) {
|
|
||||||
$barcode .= rand(0, 9);
|
|
||||||
}
|
|
||||||
} while (Product::where('barcode', $barcode)->exists());
|
|
||||||
|
|
||||||
return $barcode;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class SafetyStockController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index(Warehouse $warehouse)
|
public function index(Warehouse $warehouse)
|
||||||
{
|
{
|
||||||
$allProducts = Product::with(['category', 'baseUnit'])->get();
|
$allProducts = Product::select('id', 'name', 'category_id', 'base_unit_id')->with(['category:id,name', 'baseUnit:id,name'])->get();
|
||||||
|
|
||||||
// 準備可選商品列表
|
// 準備可選商品列表
|
||||||
$availableProducts = $allProducts->map(function ($product) {
|
$availableProducts = $allProducts->map(function ($product) {
|
||||||
|
|||||||
@@ -24,7 +24,12 @@ class StockQueryController extends Controller
|
|||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$filters = $request->only(['warehouse_id', 'category_id', 'search', 'status', 'sort_by', 'sort_order', 'per_page']);
|
$filters = $request->only(['warehouse_id', 'category_id', 'search', 'status', 'sort_by', 'sort_order', 'per_page']);
|
||||||
$perPage = (int) ($filters['per_page'] ?? 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = (int) ($filters['per_page'] ?? $defaultPerPage);
|
||||||
|
|
||||||
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
$result = $this->inventoryService->getStockQueryData($filters, $perPage);
|
$result = $this->inventoryService->getStockQueryData($filters, $perPage);
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,11 @@ class StoreRequisitionController extends Controller
|
|||||||
$query->orderBy('id', 'desc');
|
$query->orderBy('id', 'desc');
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$requisitions = $query->paginate($perPage)->withQueryString();
|
$requisitions = $query->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
// 水和倉庫名稱與使用者名稱
|
// 水和倉庫名稱與使用者名稱
|
||||||
@@ -139,15 +143,16 @@ class StoreRequisitionController extends Controller
|
|||||||
'items.*.requested_qty.min' => '需求數量必須大於 0',
|
'items.*.requested_qty.min' => '需求數量必須大於 0',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$submitImmediately = $request->boolean('submit_immediately');
|
||||||
|
|
||||||
$requisition = $this->service->create(
|
$requisition = $this->service->create(
|
||||||
$request->only(['store_warehouse_id', 'remark']),
|
$request->only(['store_warehouse_id', 'remark']),
|
||||||
$request->items,
|
$request->items,
|
||||||
auth()->id()
|
auth()->id(),
|
||||||
|
$submitImmediately
|
||||||
);
|
);
|
||||||
|
|
||||||
// 如果需要直接提交
|
if ($submitImmediately) {
|
||||||
if ($request->boolean('submit_immediately')) {
|
|
||||||
$this->service->submit($requisition, auth()->id());
|
|
||||||
return redirect()->route('store-requisitions.index')
|
return redirect()->route('store-requisitions.index')
|
||||||
->with('success', '叫貨單已提交審核');
|
->with('success', '叫貨單已提交審核');
|
||||||
}
|
}
|
||||||
@@ -161,7 +166,10 @@ class StoreRequisitionController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$requisition = StoreRequisition::with(['items.product.baseUnit'])->findOrFail($id);
|
$requisition = StoreRequisition::with([
|
||||||
|
'items.product.baseUnit',
|
||||||
|
'transferOrder.items' // 載入產生的調撥單明細與批號
|
||||||
|
])->findOrFail($id);
|
||||||
|
|
||||||
// 水和倉庫
|
// 水和倉庫
|
||||||
$warehouses = Warehouse::select('id', 'name', 'type')->get();
|
$warehouses = Warehouse::select('id', 'name', 'type')->get();
|
||||||
@@ -194,8 +202,69 @@ class StoreRequisitionController extends Controller
|
|||||||
->get()
|
->get()
|
||||||
->keyBy('product_id');
|
->keyBy('product_id');
|
||||||
|
|
||||||
$requisition->items->transform(function ($item) use ($inventories) {
|
// 取得供貨倉庫的可用庫存
|
||||||
|
$supplyInventories = collect();
|
||||||
|
$supplyBatchesMap = collect();
|
||||||
|
if ($requisition->supply_warehouse_id) {
|
||||||
|
$supplyInventories = Inventory::where('warehouse_id', $requisition->supply_warehouse_id)
|
||||||
|
->whereIn('product_id', $productIds)
|
||||||
|
->select('product_id')
|
||||||
|
->selectRaw('SUM(quantity) as total_qty')
|
||||||
|
->selectRaw('SUM(reserved_quantity) as total_reserved')
|
||||||
|
->groupBy('product_id')
|
||||||
|
->get()
|
||||||
|
->keyBy('product_id');
|
||||||
|
|
||||||
|
// 取得各商品的批號庫存
|
||||||
|
$batches = Inventory::where('warehouse_id', $requisition->supply_warehouse_id)
|
||||||
|
->whereIn('product_id', $productIds)
|
||||||
|
->whereRaw('(quantity - reserved_quantity) > 0') // 僅撈出還有可用庫存的批號
|
||||||
|
->select('id', 'product_id', 'batch_number', 'expiry_date', 'location as position')
|
||||||
|
->selectRaw('quantity - reserved_quantity as available_qty')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$supplyBatchesMap = $batches->groupBy('product_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把調撥單明細 (核准的批號與數量) 整理成 map, key 為 product_id
|
||||||
|
$approvedBatchesMap = collect();
|
||||||
|
if ($requisition->transferOrder) {
|
||||||
|
$approvedBatchesMap = $requisition->transferOrder->items->groupBy('product_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
$requisition->items->transform(function ($item) use ($inventories, $supplyInventories, $supplyBatchesMap, $approvedBatchesMap) {
|
||||||
$item->current_stock = $inventories->get($item->product_id)?->total_qty ?? 0;
|
$item->current_stock = $inventories->get($item->product_id)?->total_qty ?? 0;
|
||||||
|
|
||||||
|
if ($supplyInventories->has($item->product_id)) {
|
||||||
|
$stock = $supplyInventories->get($item->product_id);
|
||||||
|
$item->supply_stock = max(0, $stock->total_qty - $stock->total_reserved);
|
||||||
|
|
||||||
|
// 附加該商品的批號可用庫存
|
||||||
|
$batches = $supplyBatchesMap->get($item->product_id) ?? collect();
|
||||||
|
$item->supply_batches = $batches->map(function ($batch) {
|
||||||
|
return [
|
||||||
|
'inventory_id' => $batch->id,
|
||||||
|
'batch_number' => $batch->batch_number,
|
||||||
|
'position' => $batch->position,
|
||||||
|
'available_qty' => $batch->available_qty,
|
||||||
|
'expiry_date' => $batch->expiry_date ? $batch->expiry_date->format('Y-m-d') : null,
|
||||||
|
];
|
||||||
|
})->values()->toArray();
|
||||||
|
} else {
|
||||||
|
$item->supply_stock = null;
|
||||||
|
$item->supply_batches = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 附加已核准的批號資訊
|
||||||
|
$approvedBatches = $approvedBatchesMap->get($item->product_id) ?? collect();
|
||||||
|
$item->approved_batches = $approvedBatches->map(function ($transferItem) {
|
||||||
|
// 如果是沒有批號管控的商品,batch_number 可能為 null
|
||||||
|
return [
|
||||||
|
'batch_number' => $transferItem->batch_number,
|
||||||
|
'qty' => $transferItem->quantity,
|
||||||
|
];
|
||||||
|
})->values()->toArray();
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -302,6 +371,10 @@ class StoreRequisitionController extends Controller
|
|||||||
'items' => 'required|array',
|
'items' => 'required|array',
|
||||||
'items.*.id' => 'required|exists:store_requisition_items,id',
|
'items.*.id' => 'required|exists:store_requisition_items,id',
|
||||||
'items.*.approved_qty' => 'required|numeric|min:0',
|
'items.*.approved_qty' => 'required|numeric|min:0',
|
||||||
|
'items.*.batches' => 'nullable|array',
|
||||||
|
'items.*.batches.*.inventory_id' => 'nullable|integer',
|
||||||
|
'items.*.batches.*.batch_number' => 'nullable|string',
|
||||||
|
'items.*.batches.*.qty' => 'required_with:items.*.batches|numeric|min:0.01',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (empty($requisition->supply_warehouse_id)) {
|
if (empty($requisition->supply_warehouse_id)) {
|
||||||
|
|||||||
42
app/Modules/Inventory/Controllers/TraceabilityController.php
Normal file
42
app/Modules/Inventory/Controllers/TraceabilityController.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Inventory\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
use App\Modules\Inventory\Services\TraceabilityService;
|
||||||
|
|
||||||
|
class TraceabilityController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected TraceabilityService $traceabilityService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 顯示批號溯源查詢的主頁面
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$batchNumber = $request->input('batch_number');
|
||||||
|
$direction = $request->input('direction', 'backward'); // backward 或 forward
|
||||||
|
|
||||||
|
$result = null;
|
||||||
|
|
||||||
|
if ($batchNumber) {
|
||||||
|
if ($direction === 'backward') {
|
||||||
|
$result = $this->traceabilityService->traceBackward($batchNumber);
|
||||||
|
} else {
|
||||||
|
$result = $this->traceabilityService->traceForward($batchNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Inertia::render('Inventory/Traceability/Index', [
|
||||||
|
'search' => [
|
||||||
|
'batch_number' => $batchNumber,
|
||||||
|
'direction' => $direction,
|
||||||
|
],
|
||||||
|
'result' => $result
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,7 +42,11 @@ class TransferOrderController extends Controller
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$orders = $query->orderByDesc('created_at')
|
$orders = $query->orderByDesc('created_at')
|
||||||
->paginate($perPage)
|
->paginate($perPage)
|
||||||
->withQueryString()
|
->withQueryString()
|
||||||
@@ -61,7 +65,7 @@ class TransferOrderController extends Controller
|
|||||||
|
|
||||||
return Inertia::render('Inventory/Transfer/Index', [
|
return Inertia::render('Inventory/Transfer/Index', [
|
||||||
'orders' => $orders,
|
'orders' => $orders,
|
||||||
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||||
'filters' => $request->only(['search', 'warehouse_id', 'per_page']),
|
'filters' => $request->only(['search', 'warehouse_id', 'per_page']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -95,6 +99,24 @@ class TransferOrderController extends Controller
|
|||||||
auth()->id(),
|
auth()->id(),
|
||||||
$transitWarehouseId
|
$transitWarehouseId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 手動發送「已建立」日誌,因為服務層使用了 saveQuietly 抑制自動日誌
|
||||||
|
activity()
|
||||||
|
->performedOn($order)
|
||||||
|
->causedBy(auth()->id())
|
||||||
|
->event('created')
|
||||||
|
->withProperties([
|
||||||
|
'attributes' => [
|
||||||
|
'doc_no' => $order->doc_no,
|
||||||
|
'from_warehouse_id' => $order->from_warehouse_id,
|
||||||
|
'to_warehouse_id' => $order->to_warehouse_id,
|
||||||
|
'transit_warehouse_id' => $order->transit_warehouse_id,
|
||||||
|
'remarks' => $order->remarks,
|
||||||
|
'status' => $order->status,
|
||||||
|
'created_by' => $order->created_by,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log('created');
|
||||||
|
|
||||||
if ($request->input('instant_post') === true) {
|
if ($request->input('instant_post') === true) {
|
||||||
try {
|
try {
|
||||||
@@ -211,6 +233,9 @@ class TransferOrderController extends Controller
|
|||||||
// 2. 先更新資料 (如果請求中包含 items,則先執行儲存)
|
// 2. 先更新資料 (如果請求中包含 items,則先執行儲存)
|
||||||
$itemsChanged = false;
|
$itemsChanged = false;
|
||||||
if ($request->has('items')) {
|
if ($request->has('items')) {
|
||||||
|
if ($order->storeRequisition()->exists()) {
|
||||||
|
return redirect()->back()->with('error', '由叫貨單自動產生的調撥單無法修改明細');
|
||||||
|
}
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'items' => 'array',
|
'items' => 'array',
|
||||||
'items.*.product_id' => 'required|exists:products,id',
|
'items.*.product_id' => 'required|exists:products,id',
|
||||||
@@ -259,6 +284,17 @@ class TransferOrderController extends Controller
|
|||||||
return redirect()->back()->with('error', '只能刪除草稿狀態的單據');
|
return redirect()->back()->with('error', '只能刪除草稿狀態的單據');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 刪除前必須先釋放預留庫存
|
||||||
|
foreach ($order->items as $item) {
|
||||||
|
$inv = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
||||||
|
->where('product_id', $item->product_id)
|
||||||
|
->where('batch_number', $item->batch_number)
|
||||||
|
->first();
|
||||||
|
if ($inv) {
|
||||||
|
$inv->releaseReservedQuantity($item->quantity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$order->items()->delete();
|
$order->items()->delete();
|
||||||
$order->delete();
|
$order->delete();
|
||||||
|
|
||||||
|
|||||||
@@ -24,9 +24,11 @@ class WarehouseController extends Controller
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
if (!in_array($perPage, [10, 20, 50, 100])) {
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
$perPage = 10;
|
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
$warehouses = $query->withSum('inventories as book_stock', 'quantity') // 帳面庫存 = 所有庫存總和
|
$warehouses = $query->withSum('inventories as book_stock', 'quantity') // 帳面庫存 = 所有庫存總和
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ class StockQueryExport implements FromCollection, WithHeadings, WithMapping, Sho
|
|||||||
$search = $this->filters['search'];
|
$search = $this->filters['search'];
|
||||||
$query->where(function ($q) use ($search) {
|
$query->where(function ($q) use ($search) {
|
||||||
$q->where('products.code', 'like', "%{$search}%")
|
$q->where('products.code', 'like', "%{$search}%")
|
||||||
->orWhere('products.name', 'like', "%{$search}%");
|
->orWhere('products.name', 'like', "%{$search}%")
|
||||||
|
->orWhere('inventories.batch_number', 'like', "%{$search}%")
|
||||||
|
->orWhere(\Illuminate\Support\Facades\DB::raw("CONCAT('BATCH-', inventories.id)"), 'like', "%{$search}%");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!empty($this->filters['status'])) {
|
if (!empty($this->filters['status'])) {
|
||||||
|
|||||||
@@ -23,7 +23,11 @@ class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMa
|
|||||||
{
|
{
|
||||||
HeadingRowFormatter::default('none');
|
HeadingRowFormatter::default('none');
|
||||||
$this->warehouse = $warehouse;
|
$this->warehouse = $warehouse;
|
||||||
$this->inboundDate = $inboundDate;
|
|
||||||
|
// 修正時間精度:將選定的日期與「現在的時分秒」結合
|
||||||
|
// 這樣既能保留使用者選的日期,又能提供精確的紀錄時點排順序
|
||||||
|
$this->inboundDate = \Illuminate\Support\Carbon::parse($inboundDate)->setTimeFrom(now())->toDateTimeString();
|
||||||
|
|
||||||
$this->notes = $notes;
|
$this->notes = $notes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +99,7 @@ class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMa
|
|||||||
// 更新單價與總價值
|
// 更新單價與總價值
|
||||||
$inventory->unit_cost = $unitCost;
|
$inventory->unit_cost = $unitCost;
|
||||||
$inventory->total_value = $inventory->quantity * $unitCost;
|
$inventory->total_value = $inventory->quantity * $unitCost;
|
||||||
$inventory->save();
|
$inventory->saveQuietly();
|
||||||
|
|
||||||
// 記錄交易歷史
|
// 記錄交易歷史
|
||||||
$inventory->transactions()->create([
|
$inventory->transactions()->create([
|
||||||
|
|||||||
@@ -9,47 +9,88 @@ use Illuminate\Validation\Rule;
|
|||||||
use Maatwebsite\Excel\Concerns\ToModel;
|
use Maatwebsite\Excel\Concerns\ToModel;
|
||||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||||
|
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||||
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
|
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
|
||||||
|
|
||||||
class ProductImport implements ToModel, WithHeadingRow, WithValidation, WithMapping
|
/**
|
||||||
|
* 商品匯入主類別
|
||||||
|
*
|
||||||
|
* 實作 WithMultipleSheets 以限定只讀取第一個工作表(資料頁),
|
||||||
|
* 跳過第二個工作表(填寫說明頁),避免說明頁的資料被誤匯入並觸發驗證錯誤。
|
||||||
|
*/
|
||||||
|
class ProductImport implements WithMultipleSheets
|
||||||
{
|
{
|
||||||
private $categories;
|
|
||||||
private $units;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
// 禁用標題格式化,保留中文標題
|
// 禁用標題格式化,保留中文標題
|
||||||
HeadingRowFormatter::default('none');
|
HeadingRowFormatter::default('none');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定只處理第一個工作表 (index 0)
|
||||||
|
*/
|
||||||
|
public function sheets(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
0 => new ProductDataSheetImport(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 商品匯入 - 資料工作表處理類別
|
||||||
|
*
|
||||||
|
* 負責實際的資料解析、驗證與儲存邏輯。
|
||||||
|
* 只會被套用到 Excel 的第一個工作表(資料頁)。
|
||||||
|
*/
|
||||||
|
class ProductDataSheetImport implements ToModel, WithHeadingRow, WithValidation, WithMapping, SkipsEmptyRows
|
||||||
|
{
|
||||||
|
private $categories;
|
||||||
|
private $units;
|
||||||
|
private $productService;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
// 快取所有類別與單位,避免 N+1 查詢
|
// 快取所有類別與單位,避免 N+1 查詢
|
||||||
$this->categories = Category::pluck('id', 'name');
|
$this->categories = Category::pluck('id', 'name');
|
||||||
$this->units = Unit::pluck('id', 'name');
|
$this->units = Unit::pluck('id', 'name');
|
||||||
|
$this->productService = app(\App\Modules\Inventory\Contracts\ProductServiceInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $row
|
* 資料映射:將 Excel 原始標題(含「(選填)」)對應到乾淨的鍵名
|
||||||
*
|
*
|
||||||
* @return array
|
* 注意:WithValidation 驗證的是 map() 之前的原始資料,
|
||||||
*/
|
* 因此 rules() 中的鍵名必須匹配 Excel 的原始標題。
|
||||||
|
* map() 的返回值只影響 model() 接收到的資料。
|
||||||
|
*/
|
||||||
public function map($row): array
|
public function map($row): array
|
||||||
{
|
{
|
||||||
// 強制將代號與條碼轉為字串,避免純數字被當作整數處理導致 max:5 驗證錯誤
|
$code = $row['商品代號(選填)'] ?? $row['商品代號'] ?? null;
|
||||||
if (isset($row['商品代號'])) {
|
$barcode = $row['條碼(選填)'] ?? $row['條碼'] ?? null;
|
||||||
$row['商品代號'] = (string) $row['商品代號'];
|
|
||||||
}
|
return [
|
||||||
if (isset($row['條碼'])) {
|
'商品代號' => $code !== null ? (string)$code : null,
|
||||||
$row['條碼'] = (string) $row['條碼'];
|
'條碼' => $barcode !== null ? (string)$barcode : null,
|
||||||
}
|
'商品名稱' => $row['商品名稱'] ?? null,
|
||||||
|
'類別名稱' => $row['類別名稱'] ?? null,
|
||||||
return $row;
|
'品牌' => $row['品牌'] ?? null,
|
||||||
|
'規格' => $row['規格'] ?? null,
|
||||||
|
'基本單位' => $row['基本單位'] ?? null,
|
||||||
|
'大單位' => $row['大單位'] ?? null,
|
||||||
|
'換算率' => isset($row['換算率']) ? (float)$row['換算率'] : null,
|
||||||
|
'成本價' => isset($row['成本價']) ? (float)$row['成本價'] : null,
|
||||||
|
'售價' => isset($row['售價']) ? (float)$row['售價'] : null,
|
||||||
|
'會員價' => isset($row['會員價']) ? (float)$row['會員價'] : null,
|
||||||
|
'批發價' => isset($row['批發價']) ? (float)$row['批發價'] : null,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $row
|
* @param array $row (map() 回傳的乾淨鍵名陣列)
|
||||||
*
|
*/
|
||||||
* @return \Illuminate\Database\Eloquent\Model|null
|
|
||||||
*/
|
|
||||||
public function model(array $row)
|
public function model(array $row)
|
||||||
{
|
{
|
||||||
// 查找關聯 ID
|
// 查找關聯 ID
|
||||||
@@ -65,15 +106,8 @@ class ProductImport implements ToModel, WithHeadingRow, WithValidation, WithMapp
|
|||||||
$code = $row['商品代號'] ?? null;
|
$code = $row['商品代號'] ?? null;
|
||||||
$barcode = $row['條碼'] ?? null;
|
$barcode = $row['條碼'] ?? null;
|
||||||
|
|
||||||
// Upsert 邏輯:優先以條碼查找,次之以商品代號查找
|
// Upsert 邏輯:透過 Service 統一查找與處理
|
||||||
$product = null;
|
$product = $this->productService->findByBarcodeOrCode($barcode, $code);
|
||||||
if (!empty($barcode)) {
|
|
||||||
$product = Product::where('barcode', $barcode)->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$product && !empty($code)) {
|
|
||||||
$product = Product::where('code', $code)->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'name' => $row['商品名稱'],
|
'name' => $row['商品名稱'],
|
||||||
@@ -91,65 +125,27 @@ class ProductImport implements ToModel, WithHeadingRow, WithValidation, WithMapp
|
|||||||
];
|
];
|
||||||
|
|
||||||
if ($product) {
|
if ($product) {
|
||||||
// 更新現有商品
|
$this->productService->updateProduct($product, $data);
|
||||||
$product->update($data);
|
} else {
|
||||||
return null; // 返回 null 以避免 Maatwebsite/Excel 嘗試再次 insert
|
if (!empty($code)) $data['code'] = $code;
|
||||||
|
if (!empty($barcode)) $data['barcode'] = $barcode;
|
||||||
|
$this->productService->createProduct($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 建立新商品:處理代碼與條碼自動生成
|
return null; // 返回 null,因為 Service 已經處理完儲存
|
||||||
if (empty($code)) {
|
|
||||||
$code = $this->generateRandomCode();
|
|
||||||
}
|
|
||||||
if (empty($barcode)) {
|
|
||||||
$barcode = $this->generateRandomBarcode();
|
|
||||||
}
|
|
||||||
|
|
||||||
$data['code'] = $code;
|
|
||||||
$data['barcode'] = $barcode;
|
|
||||||
|
|
||||||
return new Product($data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成隨機 8 碼代號 (大寫英文+數字)
|
* 驗證規則
|
||||||
|
*
|
||||||
|
* 鍵名必須匹配 Excel 原始標題(含「(選填)」後綴),
|
||||||
|
* 因為 WithValidation 驗證的是 map() 之前的原始資料。
|
||||||
*/
|
*/
|
||||||
private function generateRandomCode(): string
|
|
||||||
{
|
|
||||||
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
||||||
$code = '';
|
|
||||||
|
|
||||||
do {
|
|
||||||
$code = '';
|
|
||||||
for ($i = 0; $i < 8; $i++) {
|
|
||||||
$code .= $characters[rand(0, strlen($characters) - 1)];
|
|
||||||
}
|
|
||||||
} while (Product::where('code', $code)->exists());
|
|
||||||
|
|
||||||
return $code;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成隨機 13 碼條碼 (純數字)
|
|
||||||
*/
|
|
||||||
private function generateRandomBarcode(): string
|
|
||||||
{
|
|
||||||
$barcode = '';
|
|
||||||
|
|
||||||
do {
|
|
||||||
$barcode = '';
|
|
||||||
for ($i = 0; $i < 13; $i++) {
|
|
||||||
$barcode .= rand(0, 9);
|
|
||||||
}
|
|
||||||
} while (Product::where('barcode', $barcode)->exists());
|
|
||||||
|
|
||||||
return $barcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'商品代號' => ['nullable', 'string', 'min:2', 'max:8'],
|
'商品代號(選填)' => ['nullable', 'string', 'min:2', 'max:8'],
|
||||||
'條碼' => ['nullable', 'string'],
|
'條碼(選填)' => ['nullable', 'string'],
|
||||||
'商品名稱' => ['required', 'string'],
|
'商品名稱' => ['required', 'string'],
|
||||||
'類別名稱' => ['required', function($attribute, $value, $fail) {
|
'類別名稱' => ['required', function($attribute, $value, $fail) {
|
||||||
if (!isset($this->categories[$value])) {
|
if (!isset($this->categories[$value])) {
|
||||||
@@ -174,4 +170,16 @@ class ProductImport implements ToModel, WithHeadingRow, WithValidation, WithMapp
|
|||||||
'批發價' => ['nullable', 'numeric', 'min:0'],
|
'批發價' => ['nullable', 'numeric', 'min:0'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自訂驗證錯誤訊息的欄位名稱
|
||||||
|
* 把含 "(選填)" 後綴的欄位顯示為友善名稱
|
||||||
|
*/
|
||||||
|
public function customValidationAttributes(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'商品代號(選填)' => '商品代號',
|
||||||
|
'條碼(選填)' => '條碼',
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,27 @@ class Category extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$properties = $activity->properties;
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
$snapshot['name'] = $this->name;
|
$snapshot['name'] = $this->name;
|
||||||
$properties['snapshot'] = $snapshot;
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
foreach (['created_by', 'updated_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
$activity->properties = $properties;
|
$activity->properties = $properties;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,47 @@ class GoodsReceipt extends Model
|
|||||||
->dontSubmitEmptyLogs();
|
->dontSubmitEmptyLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
|
{
|
||||||
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
|
$snapshot['doc_no'] = $this->code;
|
||||||
|
$snapshot['warehouse_name'] = $this->warehouse?->name;
|
||||||
|
|
||||||
|
if (!isset($snapshot['vendor_name']) && $this->vendor_id) {
|
||||||
|
$vendor = app(\App\Modules\Procurement\Contracts\ProcurementServiceInterface::class)
|
||||||
|
->getVendorsByIds([$this->vendor_id])->first();
|
||||||
|
$snapshot['vendor_name'] = $vendor?->name;
|
||||||
|
}
|
||||||
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
foreach (['user_id', 'created_by', 'updated_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = app(\App\Modules\Core\Contracts\CoreServiceInterface::class)->getUser($data[$f])?->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
||||||
|
$data['warehouse_id'] = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id'])?->name;
|
||||||
|
}
|
||||||
|
if (isset($data['vendor_id']) && is_numeric($data['vendor_id'])) {
|
||||||
|
$vendor = app(\App\Modules\Procurement\Contracts\ProcurementServiceInterface::class)
|
||||||
|
->getVendorsByIds([$data['vendor_id']])->first();
|
||||||
|
$data['vendor_id'] = $vendor?->name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
|
$activity->properties = $properties;
|
||||||
|
}
|
||||||
|
|
||||||
public function items()
|
public function items()
|
||||||
{
|
{
|
||||||
return $this->hasMany(GoodsReceiptItem::class);
|
return $this->hasMany(GoodsReceiptItem::class);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class Inventory extends Model
|
|||||||
'warehouse_id',
|
'warehouse_id',
|
||||||
'product_id',
|
'product_id',
|
||||||
'quantity',
|
'quantity',
|
||||||
|
'reserved_quantity',
|
||||||
'location',
|
'location',
|
||||||
'unit_cost',
|
'unit_cost',
|
||||||
'total_value',
|
'total_value',
|
||||||
@@ -34,6 +35,8 @@ class Inventory extends Model
|
|||||||
protected $casts = [
|
protected $casts = [
|
||||||
'arrival_date' => 'date:Y-m-d',
|
'arrival_date' => 'date:Y-m-d',
|
||||||
'expiry_date' => 'date:Y-m-d',
|
'expiry_date' => 'date:Y-m-d',
|
||||||
|
'quantity' => 'decimal:4',
|
||||||
|
'reserved_quantity' => 'decimal:4',
|
||||||
'unit_cost' => 'decimal:4',
|
'unit_cost' => 'decimal:4',
|
||||||
'total_value' => 'decimal:4',
|
'total_value' => 'decimal:4',
|
||||||
];
|
];
|
||||||
@@ -55,8 +58,11 @@ class Inventory extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$properties = $activity->properties;
|
// 核心:轉換為陣列以避免 Indirect modification error
|
||||||
$attributes = $properties['attributes'] ?? [];
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
|
|
||||||
// 始終對名稱進行快照以便於上下文顯示,即使 ID 未更改
|
// 始終對名稱進行快照以便於上下文顯示,即使 ID 未更改
|
||||||
@@ -66,11 +72,28 @@ class Inventory extends Model
|
|||||||
|
|
||||||
// 如果已設定原因,則進行捕捉
|
// 如果已設定原因,則進行捕捉
|
||||||
if ($this->activityLogReason) {
|
if ($this->activityLogReason) {
|
||||||
$attributes['_reason'] = $this->activityLogReason;
|
$properties['attributes']['_reason'] = $this->activityLogReason;
|
||||||
}
|
}
|
||||||
|
|
||||||
$properties['attributes'] = $attributes;
|
|
||||||
$properties['snapshot'] = $snapshot;
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
// 全域 ID 轉名稱邏輯
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
// 倉庫 ID 轉換
|
||||||
|
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
||||||
|
$data['warehouse_id'] = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id'])?->name;
|
||||||
|
}
|
||||||
|
// 商品 ID 轉換
|
||||||
|
if (isset($data['product_id']) && is_numeric($data['product_id'])) {
|
||||||
|
$data['product_id'] = \App\Modules\Inventory\Models\Product::find($data['product_id'])?->name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
$activity->properties = $properties;
|
$activity->properties = $properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +132,33 @@ class Inventory extends Model
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可用庫存(實體庫存 - 預留庫存)
|
||||||
|
*/
|
||||||
|
public function getAvailableQuantityAttribute()
|
||||||
|
{
|
||||||
|
return max(0, $this->quantity - $this->reserved_quantity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加預留庫存(鎖定)
|
||||||
|
*/
|
||||||
|
public function reserveQuantity(float|int $amount)
|
||||||
|
{
|
||||||
|
if ($amount <= 0) return;
|
||||||
|
$this->reserved_quantity += $amount;
|
||||||
|
$this->saveQuietly();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 釋放預留庫存(解鎖)
|
||||||
|
*/
|
||||||
|
public function releaseReservedQuantity(float|int $amount)
|
||||||
|
{
|
||||||
|
if ($amount <= 0) return;
|
||||||
|
$this->reserved_quantity = max(0, $this->reserved_quantity - $amount);
|
||||||
|
$this->saveQuietly();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 產生批號
|
* 產生批號
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ class InventoryTransaction extends Model
|
|||||||
{
|
{
|
||||||
/** @use HasFactory<\Database\Factories\InventoryTransactionFactory> */
|
/** @use HasFactory<\Database\Factories\InventoryTransactionFactory> */
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
use \Spatie\Activitylog\Traits\LogsActivity;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'inventory_id',
|
'inventory_id',
|
||||||
@@ -41,4 +42,49 @@ class InventoryTransaction extends Model
|
|||||||
{
|
{
|
||||||
return $this->morphTo();
|
return $this->morphTo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
|
||||||
|
{
|
||||||
|
return \Spatie\Activitylog\LogOptions::defaults()
|
||||||
|
->logAll()
|
||||||
|
->dontLogIfAttributesChangedOnly(['updated_at'])
|
||||||
|
// 取消 logOnlyDirty,代表新增時(created)也要留紀錄
|
||||||
|
->dontSubmitEmptyLogs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
|
{
|
||||||
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
|
|
||||||
|
// 試著取得商品與倉庫名稱來作為主要顯示依據
|
||||||
|
$inventory = $this->inventory;
|
||||||
|
if ($inventory) {
|
||||||
|
$snapshot['warehouse_name'] = $inventory->warehouse ? $inventory->warehouse->name : null;
|
||||||
|
$snapshot['product_name'] = $inventory->product ? $inventory->product->name : null;
|
||||||
|
$snapshot['batch_number'] = $inventory->batch_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把異動類型與數量也拉到 snapshot
|
||||||
|
$snapshot['type'] = $this->type;
|
||||||
|
$snapshot['quantity'] = $this->quantity;
|
||||||
|
$snapshot['reason'] = $this->reason;
|
||||||
|
|
||||||
|
// 替換使用者名稱
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
if (isset($data['user_id']) && is_numeric($data['user_id'])) {
|
||||||
|
$data['user_id'] = \App\Modules\Core\Models\User::find($data['user_id'])?->name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
|
$properties['snapshot'] = $snapshot;
|
||||||
|
$activity->properties = $properties;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,21 +36,23 @@ class InventoryTransferOrder extends Model
|
|||||||
if ($eventName === 'created') {
|
if ($eventName === 'created') {
|
||||||
$activity->description = 'created';
|
$activity->description = 'created';
|
||||||
} elseif ($eventName === 'updated') {
|
} elseif ($eventName === 'updated') {
|
||||||
// 如果屬性中有 status 且變更為 completed,將描述改為 posted
|
|
||||||
if (isset($properties['attributes']['status']) && $properties['attributes']['status'] === 'completed') {
|
if (isset($properties['attributes']['status']) && $properties['attributes']['status'] === 'completed') {
|
||||||
$activity->description = 'posted';
|
$activity->description = 'posted';
|
||||||
$eventName = 'posted'; // 供後續快照邏輯判定
|
$eventName = 'posted';
|
||||||
} else {
|
} else {
|
||||||
$activity->description = 'updated';
|
$activity->description = 'updated';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 處理倉庫 ID 轉名稱
|
// 處理 ID 轉名稱 (核心:支援 attributes 與 old 的自動轉換)
|
||||||
$idToNameFields = [
|
$idToNameFields = [
|
||||||
'from_warehouse_id' => 'fromWarehouse',
|
'from_warehouse_id' => 'fromWarehouse',
|
||||||
'to_warehouse_id' => 'toWarehouse',
|
'to_warehouse_id' => 'toWarehouse',
|
||||||
|
'transit_warehouse_id' => 'transitWarehouse',
|
||||||
'created_by' => 'createdBy',
|
'created_by' => 'createdBy',
|
||||||
'posted_by' => 'postedBy',
|
'posted_by' => 'postedBy',
|
||||||
|
'dispatched_by' => 'dispatchedBy',
|
||||||
|
'received_by' => 'receivedBy',
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach (['attributes', 'old'] as $part) {
|
foreach (['attributes', 'old'] as $part) {
|
||||||
@@ -58,14 +60,20 @@ class InventoryTransferOrder extends Model
|
|||||||
foreach ($idToNameFields as $idField => $relation) {
|
foreach ($idToNameFields as $idField => $relation) {
|
||||||
if (isset($properties[$part][$idField])) {
|
if (isset($properties[$part][$idField])) {
|
||||||
$id = $properties[$part][$idField];
|
$id = $properties[$part][$idField];
|
||||||
$nameField = str_replace('_id', '_name', $idField);
|
if (!$id) continue;
|
||||||
|
|
||||||
|
$nameField = str_replace('_id', '_name', $idField);
|
||||||
$name = null;
|
$name = null;
|
||||||
if ($this->relationLoaded($relation) && $this->$relation && $this->$relation->id == $id) {
|
try {
|
||||||
$name = $this->$relation->name;
|
if ($this->relationLoaded($relation) && $this->$relation && $this->$relation->id == $id) {
|
||||||
} else {
|
$name = $this->$relation->name;
|
||||||
$model = $this->$relation()->getRelated()->find($id);
|
} else {
|
||||||
$name = $model ? $model->name : "ID: $id";
|
$relatedModel = $this->$relation()->getRelated();
|
||||||
|
$model = $relatedModel->find($id);
|
||||||
|
$name = $model ? ($model->name ?? $model->display_name ?? "ID: $id") : "ID: $id";
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$name = "ID: $id";
|
||||||
}
|
}
|
||||||
$properties[$part][$nameField] = $name;
|
$properties[$part][$nameField] = $name;
|
||||||
}
|
}
|
||||||
@@ -73,7 +81,7 @@ class InventoryTransferOrder extends Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 基本單據資訊快照 (包含單號、來源、目的地)
|
// 基本單據資訊快照
|
||||||
if (in_array($eventName, ['created', 'updated', 'posted', 'deleted'])) {
|
if (in_array($eventName, ['created', 'updated', 'posted', 'deleted'])) {
|
||||||
$properties['snapshot'] = [
|
$properties['snapshot'] = [
|
||||||
'doc_no' => $this->doc_no,
|
'doc_no' => $this->doc_no,
|
||||||
@@ -85,8 +93,6 @@ class InventoryTransferOrder extends Model
|
|||||||
|
|
||||||
// 移除輔助欄位與雜訊
|
// 移除輔助欄位與雜訊
|
||||||
if (isset($properties['attributes'])) {
|
if (isset($properties['attributes'])) {
|
||||||
unset($properties['attributes']['from_warehouse_name']);
|
|
||||||
unset($properties['attributes']['to_warehouse_name']);
|
|
||||||
unset($properties['attributes']['activityProperties']);
|
unset($properties['attributes']['activityProperties']);
|
||||||
unset($properties['attributes']['updated_at']);
|
unset($properties['attributes']['updated_at']);
|
||||||
}
|
}
|
||||||
@@ -94,7 +100,7 @@ class InventoryTransferOrder extends Model
|
|||||||
unset($properties['old']['updated_at']);
|
unset($properties['old']['updated_at']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 合併暫存屬性 (例如 items_diff)
|
// 合併暫存屬性 (重要:例如 items_diff)
|
||||||
if (!empty($this->activityProperties)) {
|
if (!empty($this->activityProperties)) {
|
||||||
$properties = array_merge($properties, $this->activityProperties);
|
$properties = array_merge($properties, $this->activityProperties);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,30 +85,50 @@ class Product extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$properties = $activity->properties;
|
// 核心:轉換為陣列以避免 Indirect modification error
|
||||||
$attributes = $properties['attributes'] ?? [];
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
|
|
||||||
// 處理分類名稱快照
|
|
||||||
if (isset($attributes['category_id'])) {
|
|
||||||
$category = Category::find($attributes['category_id']);
|
|
||||||
$snapshot['category_name'] = $category ? $category->name : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 處理單位名稱快照
|
|
||||||
$unitFields = ['base_unit_id', 'large_unit_id', 'purchase_unit_id'];
|
|
||||||
foreach ($unitFields as $field) {
|
|
||||||
if (isset($attributes[$field])) {
|
|
||||||
$unit = Unit::find($attributes[$field]);
|
|
||||||
$nameKey = str_replace('_id', '_name', $field);
|
|
||||||
$snapshot[$nameKey] = $unit ? $unit->name : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 始終對自身名稱進行快照以便於上下文顯示(這樣日誌總是顯示 "可樂")
|
// 始終對自身名稱進行快照以便於上下文顯示(這樣日誌總是顯示 "可樂")
|
||||||
$snapshot['name'] = $this->name;
|
$snapshot['name'] = $this->name;
|
||||||
|
|
||||||
$properties['attributes'] = $attributes;
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
// 全域 ID 轉名稱邏輯
|
||||||
|
$resolver = function (&$data) use (&$snapshot) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
// 處理分類名稱
|
||||||
|
if (isset($data['category_id']) && is_numeric($data['category_id'])) {
|
||||||
|
$categoryName = Category::find($data['category_id'])?->name;
|
||||||
|
$data['category_id'] = $categoryName;
|
||||||
|
if (!isset($snapshot['category_name']) && $categoryName) {
|
||||||
|
$snapshot['category_name'] = $categoryName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 處理單位名稱
|
||||||
|
$unitFields = ['base_unit_id', 'large_unit_id', 'purchase_unit_id'];
|
||||||
|
foreach ($unitFields as $field) {
|
||||||
|
if (isset($data[$field]) && is_numeric($data[$field])) {
|
||||||
|
$unitName = Unit::find($data[$field])?->name;
|
||||||
|
$data[$field] = $unitName;
|
||||||
|
|
||||||
|
$nameKey = str_replace('_id', '_name', $field);
|
||||||
|
if (!isset($snapshot[$nameKey]) && $unitName) {
|
||||||
|
$snapshot[$nameKey] = $unitName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
|
// 因為 resolver 內部可能更新了 snapshot,所以再覆寫一次
|
||||||
$properties['snapshot'] = $snapshot;
|
$properties['snapshot'] = $snapshot;
|
||||||
$activity->properties = $properties;
|
$activity->properties = $properties;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,11 @@ class StoreRequisition extends Model
|
|||||||
->dontSubmitEmptyLogs();
|
->dontSubmitEmptyLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array 暫存的活動紀錄屬性 (不會存入資料庫)
|
||||||
|
*/
|
||||||
|
public $activityProperties = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定義日誌屬性,解析 ID 為名稱
|
* 自定義日誌屬性,解析 ID 為名稱
|
||||||
*/
|
*/
|
||||||
@@ -48,22 +53,90 @@ class StoreRequisition extends Model
|
|||||||
{
|
{
|
||||||
$properties = $activity->properties->toArray();
|
$properties = $activity->properties->toArray();
|
||||||
|
|
||||||
|
// 處置日誌事件與狀態中文化
|
||||||
|
$statusMap = [
|
||||||
|
'draft' => '草稿',
|
||||||
|
'pending' => '待審核',
|
||||||
|
'approved' => '已核准',
|
||||||
|
'rejected' => '已駁回',
|
||||||
|
'completed' => '已完成',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 處理 ID 轉名稱
|
||||||
|
$idToNameFields = [
|
||||||
|
'store_warehouse_id' => 'storeWarehouse',
|
||||||
|
'supply_warehouse_id' => 'supplyWarehouse',
|
||||||
|
'created_by' => 'createdBy',
|
||||||
|
'approved_by' => 'approvedBy',
|
||||||
|
'transfer_order_id' => 'transferOrder',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach (['attributes', 'old'] as $part) {
|
||||||
|
if (isset($properties[$part])) {
|
||||||
|
// 1. 解析狀態中文並替換原始 status 欄位
|
||||||
|
if (isset($properties[$part]['status'])) {
|
||||||
|
$statusValue = $properties[$part]['status'];
|
||||||
|
$properties[$part]['status'] = $statusMap[$statusValue] ?? $statusValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 解析關連名稱
|
||||||
|
foreach ($idToNameFields as $idField => $relation) {
|
||||||
|
if (isset($properties[$part][$idField])) {
|
||||||
|
$id = $properties[$part][$idField];
|
||||||
|
if (!$id) continue;
|
||||||
|
|
||||||
|
$nameField = str_replace('_id', '_name', $idField);
|
||||||
|
if (str_contains($idField, '_by')) {
|
||||||
|
$nameField = str_replace('_by', '_user_name', $idField);
|
||||||
|
}
|
||||||
|
|
||||||
|
$name = null;
|
||||||
|
try {
|
||||||
|
if ($this->relationLoaded($relation) && $this->$relation && $this->$relation->id == $id) {
|
||||||
|
// 特別處理調撥單號
|
||||||
|
$name = ($relation === 'transferOrder') ? $this->$relation->doc_no : $this->$relation->name;
|
||||||
|
} else {
|
||||||
|
$relatedModel = $this->$relation()->getRelated();
|
||||||
|
$model = $relatedModel->find($id);
|
||||||
|
if ($model) {
|
||||||
|
$name = ($relation === 'transferOrder') ? ($model->doc_no ?? "ID: $id") : ($model->name ?? "ID: $id");
|
||||||
|
} else {
|
||||||
|
$name = "ID: $id";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$name = "ID: $id";
|
||||||
|
}
|
||||||
|
$properties[$part][$nameField] = $name;
|
||||||
|
// 移除原生的技術 ID 欄位,讓詳情更乾淨
|
||||||
|
unset($properties[$part][$idField]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 基本單據資訊快照
|
// 基本單據資訊快照
|
||||||
$properties['snapshot'] = [
|
$properties['snapshot'] = [
|
||||||
'doc_no' => $this->doc_no,
|
'doc_no' => $this->doc_no,
|
||||||
'store_warehouse_name' => $this->storeWarehouse?->name,
|
'store_warehouse_name' => $this->storeWarehouse?->name,
|
||||||
'supply_warehouse_name' => $this->supplyWarehouse?->name,
|
'supply_warehouse_name' => $this->supplyWarehouse?->name,
|
||||||
'status' => $this->status,
|
'status' => $statusMap[$this->status] ?? $this->status,
|
||||||
];
|
];
|
||||||
|
|
||||||
// 移除雜訊欄位
|
// 移除雜訊與重複欄位
|
||||||
if (isset($properties['attributes'])) {
|
if (isset($properties['attributes'])) {
|
||||||
unset($properties['attributes']['updated_at']);
|
unset($properties['attributes']['updated_at']);
|
||||||
|
unset($properties['attributes']['activityProperties']);
|
||||||
}
|
}
|
||||||
if (isset($properties['old'])) {
|
if (isset($properties['old'])) {
|
||||||
unset($properties['old']['updated_at']);
|
unset($properties['old']['updated_at']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 合併暫存屬性 (例如 items_diff)
|
||||||
|
if (!empty($this->activityProperties)) {
|
||||||
|
$properties = array_merge($properties, $this->activityProperties);
|
||||||
|
}
|
||||||
|
|
||||||
$activity->properties = collect($properties);
|
$activity->properties = collect($properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,12 +34,27 @@ class Unit extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$properties = $activity->properties;
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
$snapshot['name'] = $this->name;
|
$snapshot['name'] = $this->name;
|
||||||
$properties['snapshot'] = $snapshot;
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
foreach (['created_by', 'updated_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
$activity->properties = $properties;
|
$activity->properties = $properties;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,12 +37,31 @@ class Warehouse extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$properties = $activity->properties;
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
|
|
||||||
$snapshot = $properties['snapshot'] ?? [];
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
$snapshot['name'] = $this->name;
|
$snapshot['name'] = $this->name;
|
||||||
$properties['snapshot'] = $snapshot;
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
foreach (['created_by', 'updated_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['default_transit_warehouse_id']) && is_numeric($data['default_transit_warehouse_id'])) {
|
||||||
|
$data['default_transit_warehouse_id'] = self::find($data['default_transit_warehouse_id'])?->name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
$activity->properties = $properties;
|
$activity->properties = $properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,11 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::get('/inventory/analysis', [InventoryAnalysisController::class, 'index'])->name('inventory.analysis.index');
|
Route::get('/inventory/analysis', [InventoryAnalysisController::class, 'index'])->name('inventory.analysis.index');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 批號溯源 (Lot Traceability)
|
||||||
|
Route::middleware('permission:inventory_traceability.view')->group(function () {
|
||||||
|
Route::get('/inventory/traceability', [\App\Modules\Inventory\Controllers\TraceabilityController::class, 'index'])->name('inventory.traceability.index');
|
||||||
|
});
|
||||||
|
|
||||||
// 類別管理 (用於商品對話框) - 需要商品權限
|
// 類別管理 (用於商品對話框) - 需要商品權限
|
||||||
Route::middleware('permission:products.view')->group(function () {
|
Route::middleware('permission:products.view')->group(function () {
|
||||||
Route::get('/categories', [CategoryController::class, 'index'])->name('categories.index');
|
Route::get('/categories', [CategoryController::class, 'index'])->name('categories.index');
|
||||||
@@ -179,6 +184,8 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::get('/goods-receipts', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'index'])->name('goods-receipts.index');
|
Route::get('/goods-receipts', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'index'])->name('goods-receipts.index');
|
||||||
Route::get('/goods-receipts/create', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'create'])->middleware('permission:goods_receipts.create')->name('goods-receipts.create');
|
Route::get('/goods-receipts/create', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'create'])->middleware('permission:goods_receipts.create')->name('goods-receipts.create');
|
||||||
Route::get('/goods-receipts/{goods_receipt}', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'show'])->name('goods-receipts.show');
|
Route::get('/goods-receipts/{goods_receipt}', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'show'])->name('goods-receipts.show');
|
||||||
|
Route::get('/goods-receipts/{goods_receipt}/edit', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'edit'])->middleware('permission:goods_receipts.edit')->name('goods-receipts.edit');
|
||||||
|
Route::put('/goods-receipts/{goods_receipt}', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'update'])->middleware('permission:goods_receipts.edit')->name('goods-receipts.update');
|
||||||
Route::post('/goods-receipts/check-duplicate', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'checkDuplicate'])
|
Route::post('/goods-receipts/check-duplicate', [\App\Modules\Inventory\Controllers\GoodsReceiptController::class, 'checkDuplicate'])
|
||||||
->middleware('permission:goods_receipts.create')
|
->middleware('permission:goods_receipts.create')
|
||||||
->name('goods-receipts.check-duplicate');
|
->name('goods-receipts.check-duplicate');
|
||||||
|
|||||||
@@ -5,10 +5,17 @@ use App\Modules\Inventory\Models\Inventory;
|
|||||||
use App\Modules\Inventory\Models\InventoryCountDoc;
|
use App\Modules\Inventory\Models\InventoryCountDoc;
|
||||||
use App\Modules\Inventory\Models\InventoryAdjustDoc;
|
use App\Modules\Inventory\Models\InventoryAdjustDoc;
|
||||||
use App\Modules\Inventory\Models\InventoryAdjustItem;
|
use App\Modules\Inventory\Models\InventoryAdjustItem;
|
||||||
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class AdjustService
|
class AdjustService
|
||||||
{
|
{
|
||||||
|
protected InventoryServiceInterface $inventoryService;
|
||||||
|
|
||||||
|
public function __construct(InventoryServiceInterface $inventoryService)
|
||||||
|
{
|
||||||
|
$this->inventoryService = $inventoryService;
|
||||||
|
}
|
||||||
public function createDoc(string $warehouseId, string $reason, ?string $remarks = null, int $userId, ?int $countDocId = null): InventoryAdjustDoc
|
public function createDoc(string $warehouseId, string $reason, ?string $remarks = null, int $userId, ?int $countDocId = null): InventoryAdjustDoc
|
||||||
{
|
{
|
||||||
return InventoryAdjustDoc::create([
|
return InventoryAdjustDoc::create([
|
||||||
@@ -37,16 +44,23 @@ class AdjustService
|
|||||||
);
|
);
|
||||||
|
|
||||||
// 2. 抓取有差異的明細 (diff_qty != 0)
|
// 2. 抓取有差異的明細 (diff_qty != 0)
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($countDoc->items as $item) {
|
foreach ($countDoc->items as $item) {
|
||||||
if (abs($item->diff_qty) < 0.0001) continue;
|
if (abs($item->diff_qty) < 0.0001) continue;
|
||||||
|
|
||||||
$adjDoc->items()->create([
|
$itemsToInsert[] = [
|
||||||
|
'adjust_doc_id' => $adjDoc->id,
|
||||||
'product_id' => $item->product_id,
|
'product_id' => $item->product_id,
|
||||||
'batch_number' => $item->batch_number,
|
'batch_number' => $item->batch_number,
|
||||||
'qty_before' => $item->system_qty,
|
'qty_before' => $item->system_qty,
|
||||||
'adjust_qty' => $item->diff_qty,
|
'adjust_qty' => $item->diff_qty,
|
||||||
'notes' => "盤點差異: " . $item->diff_qty,
|
'notes' => "盤點差異: " . $item->diff_qty,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if (!empty($itemsToInsert)) {
|
||||||
|
InventoryAdjustItem::insert($itemsToInsert);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $adjDoc;
|
return $adjDoc;
|
||||||
@@ -77,25 +91,35 @@ class AdjustService
|
|||||||
|
|
||||||
$doc->items()->delete();
|
$doc->items()->delete();
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
|
$productIds = collect($itemsData)->pluck('product_id')->unique()->toArray();
|
||||||
|
$products = \App\Modules\Inventory\Models\Product::whereIn('id', $productIds)->get()->keyBy('id');
|
||||||
|
|
||||||
|
// 批次取得當前庫存
|
||||||
|
$inventories = Inventory::where('warehouse_id', $doc->warehouse_id)
|
||||||
|
->whereIn('product_id', $productIds)
|
||||||
|
->get();
|
||||||
|
|
||||||
foreach ($itemsData as $data) {
|
foreach ($itemsData as $data) {
|
||||||
// 取得當前庫存作為 qty_before 參考 (僅參考,實際扣減以過帳當下為準)
|
$inventory = $inventories->where('product_id', $data['product_id'])
|
||||||
$inventory = Inventory::where('warehouse_id', $doc->warehouse_id)
|
|
||||||
->where('product_id', $data['product_id'])
|
|
||||||
->where('batch_number', $data['batch_number'] ?? null)
|
->where('batch_number', $data['batch_number'] ?? null)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$qtyBefore = $inventory ? $inventory->quantity : 0;
|
$qtyBefore = $inventory ? $inventory->quantity : 0;
|
||||||
|
|
||||||
$newItem = $doc->items()->create([
|
$itemsToInsert[] = [
|
||||||
|
'adjust_doc_id' => $doc->id,
|
||||||
'product_id' => $data['product_id'],
|
'product_id' => $data['product_id'],
|
||||||
'batch_number' => $data['batch_number'] ?? null,
|
'batch_number' => $data['batch_number'] ?? null,
|
||||||
'qty_before' => $qtyBefore,
|
'qty_before' => $qtyBefore,
|
||||||
'adjust_qty' => $data['adjust_qty'],
|
'adjust_qty' => $data['adjust_qty'],
|
||||||
'notes' => $data['notes'] ?? null,
|
'notes' => $data['notes'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
|
||||||
// 更新日誌中的品項列表
|
// 更新日誌中的品項列表
|
||||||
$productName = \App\Modules\Inventory\Models\Product::find($data['product_id'])?->name;
|
$productName = $products->get($data['product_id'])?->name ?? '未知商品';
|
||||||
$found = false;
|
$found = false;
|
||||||
foreach ($updatedItems as $idx => $ui) {
|
foreach ($updatedItems as $idx => $ui) {
|
||||||
if ($ui['product_name'] === $productName && $ui['new'] === null) {
|
if ($ui['product_name'] === $productName && $ui['new'] === null) {
|
||||||
@@ -119,6 +143,10 @@ class AdjustService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($itemsToInsert)) {
|
||||||
|
InventoryAdjustItem::insert($itemsToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
// 清理沒被更新到的舊品項 (即真正被刪除的)
|
// 清理沒被更新到的舊品項 (即真正被刪除的)
|
||||||
$finalUpdatedItems = [];
|
$finalUpdatedItems = [];
|
||||||
foreach ($updatedItems as $ui) {
|
foreach ($updatedItems as $ui) {
|
||||||
@@ -155,35 +183,35 @@ class AdjustService
|
|||||||
foreach ($doc->items as $item) {
|
foreach ($doc->items as $item) {
|
||||||
if ($item->adjust_qty == 0) continue;
|
if ($item->adjust_qty == 0) continue;
|
||||||
|
|
||||||
$inventory = Inventory::firstOrNew([
|
// 補上 lockForUpdate() 防止併發衝突
|
||||||
|
$inventory = Inventory::where([
|
||||||
'warehouse_id' => $doc->warehouse_id,
|
'warehouse_id' => $doc->warehouse_id,
|
||||||
'product_id' => $item->product_id,
|
'product_id' => $item->product_id,
|
||||||
'batch_number' => $item->batch_number,
|
'batch_number' => $item->batch_number,
|
||||||
]);
|
])->lockForUpdate()->first();
|
||||||
|
|
||||||
// 如果是新建立的 object (id 為空),需要初始化 default
|
if (!$inventory) {
|
||||||
|
$inventory = new Inventory([
|
||||||
|
'warehouse_id' => $doc->warehouse_id,
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'batch_number' => $item->batch_number,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是新建立的 object (id 為空),需要初始化 default 並先行儲存
|
||||||
if (!$inventory->exists) {
|
if (!$inventory->exists) {
|
||||||
$inventory->unit_cost = $item->product->cost ?? 0;
|
$inventory->unit_cost = $item->product->cost ?? 0;
|
||||||
$inventory->quantity = 0;
|
$inventory->quantity = 0;
|
||||||
|
$inventory->total_value = 0;
|
||||||
|
$inventory->saveQuietly();
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldQty = $inventory->quantity;
|
$this->inventoryService->adjustInventory($inventory, [
|
||||||
$newQty = $oldQty + $item->adjust_qty;
|
'operation' => 'add',
|
||||||
|
|
||||||
$inventory->quantity = $newQty;
|
|
||||||
$inventory->total_value = $newQty * $inventory->unit_cost;
|
|
||||||
$inventory->save();
|
|
||||||
|
|
||||||
// 建立 Transaction
|
|
||||||
$inventory->transactions()->create([
|
|
||||||
'type' => '庫存調整',
|
|
||||||
'quantity' => $item->adjust_qty,
|
'quantity' => $item->adjust_qty,
|
||||||
'unit_cost' => $inventory->unit_cost,
|
'type' => 'adjustment',
|
||||||
'balance_before' => $oldQty,
|
|
||||||
'balance_after' => $newQty,
|
|
||||||
'reason' => "盤調單 {$doc->doc_no}: " . ($doc->reason ?? '手動調整'),
|
'reason' => "盤調單 {$doc->doc_no}: " . ($doc->reason ?? '手動調整'),
|
||||||
'actual_time' => now(),
|
'notes' => $item->notes,
|
||||||
'user_id' => $userId,
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,36 +38,75 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
$data['user_id'] = auth()->id();
|
$data['user_id'] = auth()->id();
|
||||||
$data['status'] = GoodsReceipt::STATUS_DRAFT; // 預設草稿
|
$data['status'] = GoodsReceipt::STATUS_DRAFT; // 預設草稿
|
||||||
|
|
||||||
// 2. Create Header
|
// 2. 靜默建立以抑制自動日誌(後續手動發送含品項明細的日誌)
|
||||||
$goodsReceipt = GoodsReceipt::create($data);
|
$goodsReceipt = new GoodsReceipt($data);
|
||||||
|
$goodsReceipt->saveQuietly();
|
||||||
|
|
||||||
// 3. Process Items
|
// 3. 建立品項並收集 items_diff
|
||||||
|
$diff = ['added' => [], 'removed' => [], 'updated' => []];
|
||||||
|
$productIds = collect($data['items'])->pluck('product_id')->unique()->toArray();
|
||||||
|
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($data['items'] as $itemData) {
|
foreach ($data['items'] as $itemData) {
|
||||||
// Create GR Item
|
// 非標準類型:使用手動輸入的小計;標準類型:自動計算
|
||||||
$grItem = new GoodsReceiptItem([
|
$totalAmount = !empty($itemData['subtotal']) && $data['type'] !== 'standard'
|
||||||
|
? (float) $itemData['subtotal']
|
||||||
|
: $itemData['quantity_received'] * $itemData['unit_price'];
|
||||||
|
|
||||||
|
$itemsToInsert[] = [
|
||||||
|
'goods_receipt_id' => $goodsReceipt->id,
|
||||||
'product_id' => $itemData['product_id'],
|
'product_id' => $itemData['product_id'],
|
||||||
'purchase_order_item_id' => $itemData['purchase_order_item_id'] ?? null,
|
'purchase_order_item_id' => $itemData['purchase_order_item_id'] ?? null,
|
||||||
'quantity_received' => $itemData['quantity_received'],
|
'quantity_received' => $itemData['quantity_received'],
|
||||||
'unit_price' => $itemData['unit_price'],
|
'unit_price' => $itemData['unit_price'],
|
||||||
'total_amount' => $itemData['quantity_received'] * $itemData['unit_price'],
|
'total_amount' => $totalAmount,
|
||||||
'batch_number' => $itemData['batch_number'] ?? null,
|
'batch_number' => $itemData['batch_number'] ?? null,
|
||||||
'expiry_date' => $itemData['expiry_date'] ?? null,
|
'expiry_date' => $itemData['expiry_date'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
$goodsReceipt->items()->save($grItem);
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$product = $products->get($itemData['product_id']);
|
||||||
|
$diff['added'][] = [
|
||||||
|
'product_name' => $product?->name ?? '未知商品',
|
||||||
|
'new' => [
|
||||||
|
'quantity_received' => (float)$itemData['quantity_received'],
|
||||||
|
'unit_price' => (float)$itemData['unit_price'],
|
||||||
|
'total_amount' => (float)$totalAmount,
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($itemsToInsert)) {
|
||||||
|
GoodsReceiptItem::insert($itemsToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 手動發送高品質日誌(包含品項明細)
|
||||||
|
activity()
|
||||||
|
->performedOn($goodsReceipt)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('created')
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => $diff,
|
||||||
|
'attributes' => [
|
||||||
|
'gr_number' => $goodsReceipt->code,
|
||||||
|
'type' => $goodsReceipt->type,
|
||||||
|
'warehouse_id' => $goodsReceipt->warehouse_id,
|
||||||
|
'vendor_id' => $goodsReceipt->vendor_id,
|
||||||
|
'purchase_order_id' => $goodsReceipt->purchase_order_id,
|
||||||
|
'received_date' => $goodsReceipt->received_date,
|
||||||
|
'status' => $goodsReceipt->status,
|
||||||
|
'remarks' => $goodsReceipt->remarks,
|
||||||
|
'user_id' => $goodsReceipt->user_id,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log('created');
|
||||||
|
|
||||||
return $goodsReceipt;
|
return $goodsReceipt;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update an existing Goods Receipt.
|
|
||||||
*
|
|
||||||
* @param GoodsReceipt $goodsReceipt
|
|
||||||
* @param array $data
|
|
||||||
* @return GoodsReceipt
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function update(GoodsReceipt $goodsReceipt, array $data)
|
public function update(GoodsReceipt $goodsReceipt, array $data)
|
||||||
{
|
{
|
||||||
if (!in_array($goodsReceipt->status, [GoodsReceipt::STATUS_DRAFT, GoodsReceipt::STATUS_REJECTED])) {
|
if (!in_array($goodsReceipt->status, [GoodsReceipt::STATUS_DRAFT, GoodsReceipt::STATUS_REJECTED])) {
|
||||||
@@ -75,28 +114,137 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DB::transaction(function () use ($goodsReceipt, $data) {
|
return DB::transaction(function () use ($goodsReceipt, $data) {
|
||||||
$goodsReceipt->update([
|
$goodsReceipt->fill([
|
||||||
'vendor_id' => $data['vendor_id'] ?? $goodsReceipt->vendor_id,
|
'vendor_id' => $data['vendor_id'] ?? $goodsReceipt->vendor_id,
|
||||||
'received_date' => $data['received_date'] ?? $goodsReceipt->received_date,
|
'received_date' => $data['received_date'] ?? $goodsReceipt->received_date,
|
||||||
'remarks' => $data['remarks'] ?? $goodsReceipt->remarks,
|
'remarks' => $data['remarks'] ?? $goodsReceipt->remarks,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$dirty = $goodsReceipt->getDirty();
|
||||||
|
$oldAttributes = [];
|
||||||
|
$newAttributes = [];
|
||||||
|
|
||||||
|
foreach ($dirty as $key => $value) {
|
||||||
|
$oldAttributes[$key] = $goodsReceipt->getOriginal($key);
|
||||||
|
$newAttributes[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 儲存但不觸發事件,以避免重複記錄
|
||||||
|
$goodsReceipt->saveQuietly();
|
||||||
|
|
||||||
|
// 捕捉包含商品名稱的舊項目以進行比對
|
||||||
|
$oldItemsCollection = $goodsReceipt->items()->get();
|
||||||
|
$oldProductIds = $oldItemsCollection->pluck('product_id')->unique()->toArray();
|
||||||
|
$oldProducts = $this->inventoryService->getProductsByIds($oldProductIds)->keyBy('id');
|
||||||
|
|
||||||
|
$oldItems = $oldItemsCollection->map(function($item) use ($oldProducts) {
|
||||||
|
$product = $oldProducts->get($item->product_id);
|
||||||
|
return [
|
||||||
|
'id' => $item->id,
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'product_name' => $product?->name ?? 'Unknown',
|
||||||
|
'quantity_received' => (float) $item->quantity_received,
|
||||||
|
'unit_price' => (float) $item->unit_price,
|
||||||
|
'total_amount' => (float) $item->total_amount,
|
||||||
|
];
|
||||||
|
})->keyBy('product_id');
|
||||||
|
|
||||||
if (isset($data['items'])) {
|
if (isset($data['items'])) {
|
||||||
// Simple strategy: delete existing items and recreate
|
|
||||||
$goodsReceipt->items()->delete();
|
$goodsReceipt->items()->delete();
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($data['items'] as $itemData) {
|
foreach ($data['items'] as $itemData) {
|
||||||
$grItem = new GoodsReceiptItem([
|
// 非標準類型:使用手動輸入的小計;標準類型:自動計算
|
||||||
|
$totalAmount = !empty($itemData['subtotal']) && $goodsReceipt->type !== 'standard'
|
||||||
|
? (float) $itemData['subtotal']
|
||||||
|
: $itemData['quantity_received'] * $itemData['unit_price'];
|
||||||
|
|
||||||
|
$itemsToInsert[] = [
|
||||||
|
'goods_receipt_id' => $goodsReceipt->id,
|
||||||
'product_id' => $itemData['product_id'],
|
'product_id' => $itemData['product_id'],
|
||||||
'purchase_order_item_id' => $itemData['purchase_order_item_id'] ?? null,
|
'purchase_order_item_id' => $itemData['purchase_order_item_id'] ?? null,
|
||||||
'quantity_received' => $itemData['quantity_received'],
|
'quantity_received' => $itemData['quantity_received'],
|
||||||
'unit_price' => $itemData['unit_price'],
|
'unit_price' => $itemData['unit_price'],
|
||||||
'total_amount' => $itemData['quantity_received'] * $itemData['unit_price'],
|
'total_amount' => $totalAmount,
|
||||||
'batch_number' => $itemData['batch_number'] ?? null,
|
'batch_number' => $itemData['batch_number'] ?? null,
|
||||||
'expiry_date' => $itemData['expiry_date'] ?? null,
|
'expiry_date' => $itemData['expiry_date'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
$goodsReceipt->items()->save($grItem);
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($itemsToInsert)) {
|
||||||
|
GoodsReceiptItem::insert($itemsToInsert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 計算項目差異
|
||||||
|
$itemDiffs = [
|
||||||
|
'added' => [],
|
||||||
|
'removed' => [],
|
||||||
|
'updated' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
$newItemsCollection = $goodsReceipt->items()->get();
|
||||||
|
$newProductIds = $newItemsCollection->pluck('product_id')->unique()->toArray();
|
||||||
|
$newProducts = $this->inventoryService->getProductsByIds($newProductIds)->keyBy('id');
|
||||||
|
|
||||||
|
$newItemsFormatted = $newItemsCollection->map(function($item) use ($newProducts) {
|
||||||
|
$product = $newProducts->get($item->product_id);
|
||||||
|
return [
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'product_name' => $product?->name ?? 'Unknown',
|
||||||
|
'quantity_received' => (float) $item->quantity_received,
|
||||||
|
'unit_price' => (float) $item->unit_price,
|
||||||
|
'total_amount' => (float) $item->total_amount,
|
||||||
|
];
|
||||||
|
})->keyBy('product_id');
|
||||||
|
|
||||||
|
foreach ($oldItems as $productId => $oldItem) {
|
||||||
|
if (!$newItemsFormatted->has($productId)) {
|
||||||
|
$itemDiffs['removed'][] = $oldItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($newItemsFormatted as $productId => $newItem) {
|
||||||
|
if (!$oldItems->has($productId)) {
|
||||||
|
$itemDiffs['added'][] = $newItem;
|
||||||
|
} else {
|
||||||
|
$oldItem = $oldItems[$productId];
|
||||||
|
if (
|
||||||
|
$oldItem['quantity_received'] != $newItem['quantity_received'] ||
|
||||||
|
$oldItem['unit_price'] != $newItem['unit_price'] ||
|
||||||
|
$oldItem['total_amount'] != $newItem['total_amount']
|
||||||
|
) {
|
||||||
|
$itemDiffs['updated'][] = [
|
||||||
|
'product_name' => $newItem['product_name'],
|
||||||
|
'old' => [
|
||||||
|
'quantity_received' => $oldItem['quantity_received'],
|
||||||
|
'unit_price' => $oldItem['unit_price'],
|
||||||
|
'total_amount' => $oldItem['total_amount'],
|
||||||
|
],
|
||||||
|
'new' => [
|
||||||
|
'quantity_received' => $newItem['quantity_received'],
|
||||||
|
'unit_price' => $newItem['unit_price'],
|
||||||
|
'total_amount' => $newItem['total_amount'],
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有變更,手動觸發單一合併日誌
|
||||||
|
if (!empty($newAttributes) || !empty($itemDiffs['added']) || !empty($itemDiffs['removed']) || !empty($itemDiffs['updated'])) {
|
||||||
|
activity()
|
||||||
|
->performedOn($goodsReceipt)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('updated')
|
||||||
|
->withProperties([
|
||||||
|
'attributes' => $newAttributes,
|
||||||
|
'old' => $oldAttributes,
|
||||||
|
'items_diff' => $itemDiffs,
|
||||||
|
])
|
||||||
|
->log('updated');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $goodsReceipt->fresh('items');
|
return $goodsReceipt->fresh('items');
|
||||||
@@ -113,11 +261,14 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
*/
|
*/
|
||||||
public function submit(GoodsReceipt $goodsReceipt)
|
public function submit(GoodsReceipt $goodsReceipt)
|
||||||
{
|
{
|
||||||
if (!in_array($goodsReceipt->status, [GoodsReceipt::STATUS_DRAFT, GoodsReceipt::STATUS_REJECTED])) {
|
|
||||||
throw new \Exception('只有草稿或被退回的進貨單可以確認點收。');
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::transaction(function () use ($goodsReceipt) {
|
return DB::transaction(function () use ($goodsReceipt) {
|
||||||
|
// Pessimistic locking to prevent double submission
|
||||||
|
$goodsReceipt = GoodsReceipt::lockForUpdate()->find($goodsReceipt->id);
|
||||||
|
|
||||||
|
if (!in_array($goodsReceipt->status, [GoodsReceipt::STATUS_DRAFT, GoodsReceipt::STATUS_REJECTED])) {
|
||||||
|
throw new \Exception('只有草稿或被退回的進貨單可以確認點收。');
|
||||||
|
}
|
||||||
|
|
||||||
$goodsReceipt->status = GoodsReceipt::STATUS_COMPLETED;
|
$goodsReceipt->status = GoodsReceipt::STATUS_COMPLETED;
|
||||||
$goodsReceipt->save();
|
$goodsReceipt->save();
|
||||||
|
|
||||||
@@ -131,10 +282,24 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
default => '進貨入庫',
|
default => '進貨入庫',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$quantityToRecord = $grItem->quantity_received;
|
||||||
|
|
||||||
|
// 單位換算邏輯:僅針對標準採購且有連結 PO Item 時
|
||||||
|
if ($goodsReceipt->type === 'standard' && $grItem->purchase_order_item_id) {
|
||||||
|
$poItem = \App\Modules\Procurement\Models\PurchaseOrderItem::find($grItem->purchase_order_item_id);
|
||||||
|
$product = $this->inventoryService->getProduct($grItem->product_id);
|
||||||
|
|
||||||
|
if ($poItem && $product && $poItem->unit_id && $product->large_unit_id && $poItem->unit_id == $product->large_unit_id) {
|
||||||
|
// 如果使用的是大單位,則換算為基本單位數量
|
||||||
|
$quantityToRecord = $grItem->quantity_received * ($product->conversion_rate ?: 1);
|
||||||
|
Log::info("Goods Receipt [{$goodsReceipt->code}] converted quantity for product [{$product->id}]: {$grItem->quantity_received} large unit -> {$quantityToRecord} base unit.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->inventoryService->createInventoryRecord([
|
$this->inventoryService->createInventoryRecord([
|
||||||
'warehouse_id' => $goodsReceipt->warehouse_id,
|
'warehouse_id' => $goodsReceipt->warehouse_id,
|
||||||
'product_id' => $grItem->product_id,
|
'product_id' => $grItem->product_id,
|
||||||
'quantity' => $grItem->quantity_received,
|
'quantity' => $quantityToRecord,
|
||||||
'unit_cost' => $grItem->unit_price,
|
'unit_cost' => $grItem->unit_price,
|
||||||
'batch_number' => $grItem->batch_number,
|
'batch_number' => $grItem->batch_number,
|
||||||
'expiry_date' => $grItem->expiry_date,
|
'expiry_date' => $grItem->expiry_date,
|
||||||
@@ -147,6 +312,7 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
|
|
||||||
// 2. Update PO if linked and type is standard
|
// 2. Update PO if linked and type is standard
|
||||||
if ($goodsReceipt->type === 'standard' && $goodsReceipt->purchase_order_id && $grItem->purchase_order_item_id) {
|
if ($goodsReceipt->type === 'standard' && $goodsReceipt->purchase_order_id && $grItem->purchase_order_item_id) {
|
||||||
|
// 更新採購單的實收數量 (維持原始單位數量,以便與採購數量比較)
|
||||||
$this->procurementService->updateReceivedQuantity(
|
$this->procurementService->updateReceivedQuantity(
|
||||||
$grItem->purchase_order_item_id,
|
$grItem->purchase_order_item_id,
|
||||||
$grItem->quantity_received
|
$grItem->quantity_received
|
||||||
@@ -162,23 +328,35 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function generateCode(string $date)
|
private function generateCode(string $date): string
|
||||||
{
|
{
|
||||||
// Format: GR-YYYYMMDD-NN
|
// 使用 Cache Lock 防止併發時產生重複單號
|
||||||
$prefix = 'GR-' . date('Ymd', strtotime($date)) . '-';
|
$lock = \Illuminate\Support\Facades\Cache::lock('gr_code_generation', 10);
|
||||||
|
|
||||||
$last = GoodsReceipt::where('code', 'like', $prefix . '%')
|
|
||||||
->orderBy('id', 'desc')
|
|
||||||
->lockForUpdate()
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if ($last) {
|
if (!$lock->get()) {
|
||||||
$seq = intval(substr($last->code, -2)) + 1;
|
throw new \Exception('系統忙碌中,進貨單號生成失敗,請稍後再試');
|
||||||
} else {
|
|
||||||
$seq = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $prefix . str_pad($seq, 2, '0', STR_PAD_LEFT);
|
try {
|
||||||
|
// Format: GR-YYYYMMDD-NN
|
||||||
|
$prefix = 'GR-' . date('Ymd', strtotime($date)) . '-';
|
||||||
|
|
||||||
|
$last = GoodsReceipt::where('code', 'like', $prefix . '%')
|
||||||
|
->orderBy('id', 'desc')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($last) {
|
||||||
|
$seq = intval(substr($last->code, -2)) + 1;
|
||||||
|
} else {
|
||||||
|
$seq = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$code = $prefix . str_pad($seq, 2, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
|
return $code;
|
||||||
|
} finally {
|
||||||
|
$lock->release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -23,8 +23,9 @@ class InventoryReportService
|
|||||||
* @param int|null $perPage 每頁筆數
|
* @param int|null $perPage 每頁筆數
|
||||||
* @return \Illuminate\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
|
* @return \Illuminate\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
public function getReportData(array $filters, ?int $perPage = 10)
|
public function getReportData(array $filters, ?int $perPage = null)
|
||||||
{
|
{
|
||||||
|
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
$dateFrom = $filters['date_from'] ?? null;
|
$dateFrom = $filters['date_from'] ?? null;
|
||||||
$dateTo = $filters['date_to'] ?? null;
|
$dateTo = $filters['date_to'] ?? null;
|
||||||
$warehouseId = $filters['warehouse_id'] ?? null;
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
@@ -197,8 +198,9 @@ class InventoryReportService
|
|||||||
/**
|
/**
|
||||||
* 取得特定商品的庫存異動明細
|
* 取得特定商品的庫存異動明細
|
||||||
*/
|
*/
|
||||||
public function getProductDetails($productId, array $filters, ?int $perPage = 20)
|
public function getProductDetails($productId, array $filters, ?int $perPage = null)
|
||||||
{
|
{
|
||||||
|
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
$dateFrom = $filters['date_from'] ?? null;
|
$dateFrom = $filters['date_from'] ?? null;
|
||||||
$dateTo = $filters['date_to'] ?? null;
|
$dateTo = $filters['date_to'] ?? null;
|
||||||
$warehouseId = $filters['warehouse_id'] ?? null;
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
{
|
{
|
||||||
public function getAllWarehouses()
|
public function getAllWarehouses()
|
||||||
{
|
{
|
||||||
return Warehouse::all();
|
return Warehouse::select('id', 'name', 'code', 'type')->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTopInventoryValue(int $limit = 5): \Illuminate\Support\Collection
|
public function getTopInventoryValue(int $limit = 5): \Illuminate\Support\Collection
|
||||||
@@ -38,12 +38,14 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
|
|
||||||
public function getAllProducts()
|
public function getAllProducts()
|
||||||
{
|
{
|
||||||
return Product::with(['baseUnit', 'largeUnit'])->get();
|
return Product::select('id', 'name', 'code', 'base_unit_id', 'large_unit_id')
|
||||||
|
->with(['baseUnit:id,name', 'largeUnit:id,name'])
|
||||||
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUnits()
|
public function getUnits()
|
||||||
{
|
{
|
||||||
return \App\Modules\Inventory\Models\Unit::all();
|
return \App\Modules\Inventory\Models\Unit::select('id', 'name')->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInventoriesByIds(array $ids, array $with = [])
|
public function getInventoriesByIds(array $ids, array $with = [])
|
||||||
@@ -85,41 +87,58 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
return $stock >= $quantity;
|
return $stock >= $quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null, bool $force = false, ?string $slot = null): void
|
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = null, bool $force = false, ?string $slot = null, ?string $referenceType = null, $referenceId = null, ?string $batchNumber = null): void
|
||||||
{
|
{
|
||||||
DB::transaction(function () use ($productId, $warehouseId, $quantity, $reason, $force, $slot) {
|
DB::transaction(function () use ($productId, $warehouseId, $quantity, $reason, $force, $slot, $referenceType, $referenceId, $batchNumber) {
|
||||||
$query = Inventory::where('product_id', $productId)
|
$defaultBatch = 'NO-BATCH';
|
||||||
->where('warehouse_id', $warehouseId)
|
$targetBatch = $batchNumber ?? $defaultBatch;
|
||||||
->where('quantity', '>', 0);
|
|
||||||
|
|
||||||
if ($slot) {
|
|
||||||
$query->where('location', $slot);
|
|
||||||
}
|
|
||||||
|
|
||||||
$inventories = $query->orderBy('arrival_date', 'asc')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$remainingToDecrease = $quantity;
|
$remainingToDecrease = $quantity;
|
||||||
|
|
||||||
|
// 1. 優先嘗試扣除指定批號(或預設的 NO-BATCH)
|
||||||
|
$inventories = Inventory::where('product_id', $productId)
|
||||||
|
->where('warehouse_id', $warehouseId)
|
||||||
|
->where('batch_number', $targetBatch)
|
||||||
|
->where('quantity', '>', 0)
|
||||||
|
->when($slot, fn($q) => $q->where('location', $slot))
|
||||||
|
->lockForUpdate()
|
||||||
|
->orderBy('arrival_date', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
foreach ($inventories as $inventory) {
|
foreach ($inventories as $inventory) {
|
||||||
if ($remainingToDecrease <= 0) break;
|
if ($remainingToDecrease <= 0) break;
|
||||||
|
|
||||||
$decreaseAmount = min($inventory->quantity, $remainingToDecrease);
|
$decreaseAmount = min($inventory->quantity, $remainingToDecrease);
|
||||||
$this->decreaseInventoryQuantity($inventory->id, $decreaseAmount, $reason);
|
$this->decreaseInventoryQuantity($inventory->id, $decreaseAmount, $reason, $referenceType, $referenceId);
|
||||||
$remainingToDecrease -= $decreaseAmount;
|
$remainingToDecrease -= $decreaseAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 如果還有剩餘且剛才不是扣 NO-BATCH,則嘗試從 NO-BATCH 補位
|
||||||
|
if ($remainingToDecrease > 0 && $targetBatch !== $defaultBatch) {
|
||||||
|
$fallbackInventories = Inventory::where('product_id', $productId)
|
||||||
|
->where('warehouse_id', $warehouseId)
|
||||||
|
->where('batch_number', $defaultBatch)
|
||||||
|
->where('quantity', '>', 0)
|
||||||
|
->when($slot, fn($q) => $q->where('location', $slot))
|
||||||
|
->lockForUpdate()
|
||||||
|
->orderBy('arrival_date', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($fallbackInventories as $inventory) {
|
||||||
|
if ($remainingToDecrease <= 0) break;
|
||||||
|
$decreaseAmount = min($inventory->quantity, $remainingToDecrease);
|
||||||
|
$this->decreaseInventoryQuantity($inventory->id, $decreaseAmount, $reason, $referenceType, $referenceId);
|
||||||
|
$remainingToDecrease -= $decreaseAmount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 處理最終仍不足的情況
|
||||||
if ($remainingToDecrease > 0) {
|
if ($remainingToDecrease > 0) {
|
||||||
if ($force) {
|
if ($force) {
|
||||||
// Find any existing inventory record in this warehouse/slot to subtract from, or create one
|
// 強制模式下,若指定批號或 NO-BATCH 均不足,統一在 NO-BATCH 建立/扣除負庫存
|
||||||
$query = Inventory::where('product_id', $productId)
|
$inventory = Inventory::where('product_id', $productId)
|
||||||
->where('warehouse_id', $warehouseId);
|
->where('warehouse_id', $warehouseId)
|
||||||
|
->where('batch_number', $defaultBatch)
|
||||||
if ($slot) {
|
->when($slot, fn($q) => $q->where('location', $slot))
|
||||||
$query->where('location', $slot);
|
->first();
|
||||||
}
|
|
||||||
|
|
||||||
$inventory = $query->first();
|
|
||||||
|
|
||||||
if (!$inventory) {
|
if (!$inventory) {
|
||||||
$inventory = Inventory::create([
|
$inventory = Inventory::create([
|
||||||
@@ -129,16 +148,19 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
'quantity' => 0,
|
'quantity' => 0,
|
||||||
'unit_cost' => 0,
|
'unit_cost' => 0,
|
||||||
'total_value' => 0,
|
'total_value' => 0,
|
||||||
'batch_number' => 'POS-AUTO-' . ($slot ? $slot . '-' : '') . time(),
|
'batch_number' => $defaultBatch,
|
||||||
'arrival_date' => now(),
|
'arrival_date' => now(),
|
||||||
'origin_country' => 'TW',
|
'origin_country' => 'TW',
|
||||||
'quality_status' => 'normal',
|
'quality_status' => 'normal',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->decreaseInventoryQuantity($inventory->id, $remainingToDecrease, $reason);
|
$this->decreaseInventoryQuantity($inventory->id, $remainingToDecrease, $reason, $referenceType, $referenceId);
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception("庫存不足,無法扣除所有請求的數量。");
|
$context = ($targetBatch !== $defaultBatch)
|
||||||
|
? "批號 {$targetBatch} 或 {$defaultBatch}"
|
||||||
|
: "{$defaultBatch}";
|
||||||
|
throw new \Exception("庫存不足,無法扣除所有請求的數量 ({$context})。");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -181,11 +203,11 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
|
|
||||||
// 更新其他可能變更的欄位 (如最後入庫日)
|
// 更新其他可能變更的欄位 (如最後入庫日)
|
||||||
$inventory->arrival_date = $data['arrival_date'] ?? $inventory->arrival_date;
|
$inventory->arrival_date = $data['arrival_date'] ?? $inventory->arrival_date;
|
||||||
$inventory->save();
|
$inventory->saveQuietly();
|
||||||
} else {
|
} else {
|
||||||
// 若不存在,則建立新紀錄
|
// 若不存在,則建立新紀錄
|
||||||
$unitCost = $data['unit_cost'] ?? 0;
|
$unitCost = $data['unit_cost'] ?? 0;
|
||||||
$inventory = Inventory::create([
|
$inventory = new Inventory([
|
||||||
'warehouse_id' => $data['warehouse_id'],
|
'warehouse_id' => $data['warehouse_id'],
|
||||||
'product_id' => $data['product_id'],
|
'product_id' => $data['product_id'],
|
||||||
'quantity' => $data['quantity'],
|
'quantity' => $data['quantity'],
|
||||||
@@ -199,9 +221,10 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
'quality_status' => $data['quality_status'] ?? 'normal',
|
'quality_status' => $data['quality_status'] ?? 'normal',
|
||||||
'source_purchase_order_id' => $data['source_purchase_order_id'] ?? null,
|
'source_purchase_order_id' => $data['source_purchase_order_id'] ?? null,
|
||||||
]);
|
]);
|
||||||
|
$inventory->saveQuietly();
|
||||||
}
|
}
|
||||||
|
|
||||||
\App\Modules\Inventory\Models\InventoryTransaction::create([
|
$transaction = new \App\Modules\Inventory\Models\InventoryTransaction([
|
||||||
'inventory_id' => $inventory->id,
|
'inventory_id' => $inventory->id,
|
||||||
'type' => '入庫',
|
'type' => '入庫',
|
||||||
'quantity' => $data['quantity'],
|
'quantity' => $data['quantity'],
|
||||||
@@ -214,6 +237,7 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
'user_id' => auth()->id(),
|
'user_id' => auth()->id(),
|
||||||
'actual_time' => now(),
|
'actual_time' => now(),
|
||||||
]);
|
]);
|
||||||
|
$transaction->saveQuietly();
|
||||||
|
|
||||||
return $inventory;
|
return $inventory;
|
||||||
});
|
});
|
||||||
@@ -225,13 +249,12 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
$inventory = Inventory::lockForUpdate()->findOrFail($inventoryId);
|
$inventory = Inventory::lockForUpdate()->findOrFail($inventoryId);
|
||||||
$balanceBefore = $inventory->quantity;
|
$balanceBefore = $inventory->quantity;
|
||||||
|
|
||||||
$inventory->decrement('quantity', $quantity); // decrement 不會自動觸發 total_value 更新
|
// 手動更新以配合 saveQuietly 消除日誌
|
||||||
// 需要手動更新總價值
|
$inventory->quantity -= $quantity;
|
||||||
$inventory->refresh();
|
|
||||||
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
||||||
$inventory->save();
|
$inventory->saveQuietly();
|
||||||
|
|
||||||
\App\Modules\Inventory\Models\InventoryTransaction::create([
|
$transaction = new \App\Modules\Inventory\Models\InventoryTransaction([
|
||||||
'inventory_id' => $inventory->id,
|
'inventory_id' => $inventory->id,
|
||||||
'type' => '出庫',
|
'type' => '出庫',
|
||||||
'quantity' => -$quantity,
|
'quantity' => -$quantity,
|
||||||
@@ -244,6 +267,7 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
'user_id' => auth()->id(),
|
'user_id' => auth()->id(),
|
||||||
'actual_time' => now(),
|
'actual_time' => now(),
|
||||||
]);
|
]);
|
||||||
|
$transaction->saveQuietly();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,10 +282,12 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
/**
|
/**
|
||||||
* 即時庫存查詢:統計卡片 + 分頁明細
|
* 即時庫存查詢:統計卡片 + 分頁明細
|
||||||
*/
|
*/
|
||||||
public function getStockQueryData(array $filters = [], int $perPage = 10): array
|
public function getStockQueryData(array $filters = [], ?int $perPage = null): array
|
||||||
{
|
{
|
||||||
|
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
$today = now()->toDateString();
|
$today = now()->toDateString();
|
||||||
$expiryThreshold = now()->addDays(30)->toDateString();
|
$expiryDays = \App\Modules\Core\Models\SystemSetting::getVal('inventory.expiry_warning_days', 30);
|
||||||
|
$expiryThreshold = now()->addDays($expiryDays)->toDateString();
|
||||||
|
|
||||||
// 基礎查詢
|
// 基礎查詢
|
||||||
$query = Inventory::query()
|
$query = Inventory::query()
|
||||||
@@ -299,12 +325,14 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
$query->where('products.category_id', $filters['category_id']);
|
$query->where('products.category_id', $filters['category_id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 篩選:關鍵字(商品代碼或名稱)
|
// 篩選:關鍵字(商品代碼或名稱或批號)
|
||||||
if (!empty($filters['search'])) {
|
if (!empty($filters['search'])) {
|
||||||
$search = $filters['search'];
|
$search = $filters['search'];
|
||||||
$query->where(function ($q) use ($search) {
|
$query->where(function ($q) use ($search) {
|
||||||
$q->where('products.code', 'like', "%{$search}%")
|
$q->where('products.code', 'like', "%{$search}%")
|
||||||
->orWhere('products.name', 'like', "%{$search}%");
|
->orWhere('products.name', 'like', "%{$search}%")
|
||||||
|
->orWhere('inventories.batch_number', 'like', "%{$search}%")
|
||||||
|
->orWhere(\Illuminate\Support\Facades\DB::raw("CONCAT('BATCH-', inventories.id)"), 'like', "%{$search}%");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -492,7 +520,8 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
public function getDashboardStats(): array
|
public function getDashboardStats(): array
|
||||||
{
|
{
|
||||||
$today = now()->toDateString();
|
$today = now()->toDateString();
|
||||||
$expiryThreshold = now()->addDays(30)->toDateString();
|
$expiryDays = \App\Modules\Core\Models\SystemSetting::getVal('inventory.expiry_warning_days', 30);
|
||||||
|
$expiryThreshold = now()->addDays($expiryDays)->toDateString();
|
||||||
|
|
||||||
// 1. 庫存品項數 (明細總數)
|
// 1. 庫存品項數 (明細總數)
|
||||||
$totalItems = DB::table('inventories')
|
$totalItems = DB::table('inventories')
|
||||||
@@ -641,4 +670,241 @@ class InventoryService implements InventoryServiceInterface
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取得特定倉庫代碼的所屬商品總庫存 (給 POS/外部系統同步使用)
|
||||||
|
*
|
||||||
|
* @param string $code
|
||||||
|
* @return \Illuminate\Support\Collection|null
|
||||||
|
*/
|
||||||
|
public function getPosInventoryByWarehouseCode(string $code, array $filters = [])
|
||||||
|
{
|
||||||
|
$warehouse = Warehouse::where('code', $code)->first();
|
||||||
|
|
||||||
|
if (!$warehouse) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = DB::table('inventories')
|
||||||
|
->join('products', 'inventories.product_id', '=', 'products.id')
|
||||||
|
->leftJoin('categories', 'products.category_id', '=', 'categories.id')
|
||||||
|
->leftJoin('units', 'products.base_unit_id', '=', 'units.id')
|
||||||
|
->where('inventories.warehouse_id', $warehouse->id)
|
||||||
|
->whereNull('inventories.deleted_at')
|
||||||
|
->whereNull('products.deleted_at')
|
||||||
|
->select(
|
||||||
|
'products.id as product_id',
|
||||||
|
'products.external_pos_id',
|
||||||
|
'products.code as product_code',
|
||||||
|
'products.name as product_name',
|
||||||
|
'products.barcode',
|
||||||
|
'categories.name as category_name',
|
||||||
|
'units.name as unit_name',
|
||||||
|
'products.price',
|
||||||
|
'products.brand',
|
||||||
|
'products.specification',
|
||||||
|
'inventories.batch_number',
|
||||||
|
'inventories.expiry_date',
|
||||||
|
DB::raw('SUM(inventories.quantity) as total_quantity')
|
||||||
|
);
|
||||||
|
|
||||||
|
// 加入條件篩選
|
||||||
|
if (!empty($filters['product_id'])) {
|
||||||
|
$query->where('products.id', $filters['product_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($filters['external_pos_id'])) {
|
||||||
|
$query->where('products.external_pos_id', $filters['external_pos_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($filters['barcode'])) {
|
||||||
|
$query->where('products.barcode', $filters['barcode']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($filters['code'])) {
|
||||||
|
$query->where('products.code', $filters['code']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->groupBy(
|
||||||
|
'inventories.product_id',
|
||||||
|
'products.external_pos_id',
|
||||||
|
'products.code',
|
||||||
|
'products.name',
|
||||||
|
'products.barcode',
|
||||||
|
'categories.name',
|
||||||
|
'units.name',
|
||||||
|
'products.price',
|
||||||
|
'products.brand',
|
||||||
|
'products.specification',
|
||||||
|
'inventories.batch_number',
|
||||||
|
'inventories.expiry_date'
|
||||||
|
)
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function processIncomingInventory(Warehouse $warehouse, array $items, array $meta): void
|
||||||
|
{
|
||||||
|
DB::transaction(function () use ($warehouse, $items, $meta) {
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$inventory = null;
|
||||||
|
|
||||||
|
if ($item['batchMode'] === 'existing') {
|
||||||
|
// 模式 A:選擇現有批號 (包含已刪除的也要能找回來累加)
|
||||||
|
$inventory = Inventory::withTrashed()->findOrFail($item['inventoryId']);
|
||||||
|
if ($inventory->trashed()) {
|
||||||
|
$inventory->restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新成本 (若有傳入)
|
||||||
|
if (isset($item['unit_cost'])) {
|
||||||
|
$inventory->unit_cost = $item['unit_cost'];
|
||||||
|
}
|
||||||
|
} elseif ($item['batchMode'] === 'none') {
|
||||||
|
// 模式 C:不使用批號 (自動累加至 NO-BATCH)
|
||||||
|
$inventory = $warehouse->inventories()->withTrashed()->firstOrNew(
|
||||||
|
[
|
||||||
|
'product_id' => $item['productId'],
|
||||||
|
'batch_number' => 'NO-BATCH'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'quantity' => 0,
|
||||||
|
'unit_cost' => $item['unit_cost'] ?? 0,
|
||||||
|
'total_value' => 0,
|
||||||
|
'arrival_date' => $meta['inboundDate'],
|
||||||
|
'origin_country' => 'TW',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($inventory->trashed()) {
|
||||||
|
$inventory->restore();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 模式 B:建立新批號
|
||||||
|
$originCountry = $item['originCountry'] ?? 'TW';
|
||||||
|
$product = Product::find($item['productId']);
|
||||||
|
|
||||||
|
$batchNumber = Inventory::generateBatchNumber(
|
||||||
|
$product->code ?? 'UNK',
|
||||||
|
$originCountry,
|
||||||
|
$meta['inboundDate']
|
||||||
|
);
|
||||||
|
|
||||||
|
// 檢查是否存在
|
||||||
|
$inventory = $warehouse->inventories()->withTrashed()->firstOrNew(
|
||||||
|
[
|
||||||
|
'product_id' => $item['productId'],
|
||||||
|
'batch_number' => $batchNumber
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'quantity' => 0,
|
||||||
|
'unit_cost' => $item['unit_cost'] ?? 0,
|
||||||
|
'total_value' => 0,
|
||||||
|
'location' => $item['location'] ?? null,
|
||||||
|
'arrival_date' => $meta['inboundDate'],
|
||||||
|
'expiry_date' => $item['expiryDate'] ?? null,
|
||||||
|
'origin_country' => $originCountry,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($inventory->trashed()) {
|
||||||
|
$inventory->restore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$currentQty = $inventory->quantity;
|
||||||
|
$newQty = $currentQty + $item['quantity'];
|
||||||
|
|
||||||
|
$inventory->quantity = $newQty;
|
||||||
|
// 更新總價值
|
||||||
|
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
||||||
|
$inventory->saveQuietly();
|
||||||
|
|
||||||
|
// 寫入異動紀錄
|
||||||
|
$inventory->transactions()->create([
|
||||||
|
'type' => '手動入庫',
|
||||||
|
'quantity' => $item['quantity'],
|
||||||
|
'unit_cost' => $inventory->unit_cost,
|
||||||
|
'balance_before' => $currentQty,
|
||||||
|
'balance_after' => $newQty,
|
||||||
|
'reason' => $meta['reason'] . (!empty($meta['notes']) ? ' - ' . $meta['notes'] : ''),
|
||||||
|
'actual_time' => $meta['inboundDate'],
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function adjustInventory(Inventory $inventory, array $data): void
|
||||||
|
{
|
||||||
|
DB::transaction(function () use ($inventory, $data) {
|
||||||
|
$currentQty = (float) $inventory->quantity;
|
||||||
|
$newQty = (float) $data['quantity'];
|
||||||
|
|
||||||
|
$isAdjustment = isset($data['operation']);
|
||||||
|
$changeQty = 0;
|
||||||
|
|
||||||
|
if ($isAdjustment) {
|
||||||
|
switch ($data['operation']) {
|
||||||
|
case 'add':
|
||||||
|
$changeQty = (float) $data['quantity'];
|
||||||
|
$newQty = $currentQty + $changeQty;
|
||||||
|
break;
|
||||||
|
case 'subtract':
|
||||||
|
$changeQty = -(float) $data['quantity'];
|
||||||
|
$newQty = $currentQty + $changeQty;
|
||||||
|
break;
|
||||||
|
case 'set':
|
||||||
|
$changeQty = $newQty - $currentQty;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$changeQty = $newQty - $currentQty;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['unit_cost'])) {
|
||||||
|
$inventory->unit_cost = $data['unit_cost'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$inventory->quantity = $newQty;
|
||||||
|
$inventory->total_value = $inventory->quantity * $inventory->unit_cost;
|
||||||
|
$inventory->saveQuietly();
|
||||||
|
|
||||||
|
$type = $data['type'] ?? ($isAdjustment ? 'manual_adjustment' : 'adjustment');
|
||||||
|
$typeMapping = [
|
||||||
|
'manual_adjustment' => '手動調整庫存',
|
||||||
|
'adjustment' => '盤點調整',
|
||||||
|
'purchase_in' => '採購進貨',
|
||||||
|
'sales_out' => '銷售出庫',
|
||||||
|
'return_in' => '退貨入庫',
|
||||||
|
'return_out' => '退貨出庫',
|
||||||
|
'transfer_in' => '撥補入庫',
|
||||||
|
'transfer_out' => '撥補出庫',
|
||||||
|
];
|
||||||
|
$chineseType = $typeMapping[$type] ?? $type;
|
||||||
|
|
||||||
|
if (!$isAdjustment && !isset($data['type'])) {
|
||||||
|
$chineseType = '手動編輯';
|
||||||
|
}
|
||||||
|
|
||||||
|
$reason = $data['reason'] ?? ($isAdjustment ? '手動庫存調整' : '編輯頁面更新');
|
||||||
|
if (!empty($data['notes'])) {
|
||||||
|
$reason .= ' - ' . $data['notes'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (abs($changeQty) > 0.0001) {
|
||||||
|
$transaction = new \App\Modules\Inventory\Models\InventoryTransaction([
|
||||||
|
'inventory_id' => $inventory->id,
|
||||||
|
'type' => $chineseType,
|
||||||
|
'quantity' => $changeQty,
|
||||||
|
'unit_cost' => $inventory->unit_cost,
|
||||||
|
'balance_before' => $currentQty,
|
||||||
|
'balance_after' => $newQty,
|
||||||
|
'reason' => $reason,
|
||||||
|
'actual_time' => now(),
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
]);
|
||||||
|
$transaction->saveQuietly();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,22 @@ class ProductService implements ProductServiceInterface
|
|||||||
|
|
||||||
// Map allowed fields
|
// Map allowed fields
|
||||||
$product->name = $data['name'];
|
$product->name = $data['name'];
|
||||||
$product->barcode = $data['barcode'] ?? $product->barcode;
|
|
||||||
$product->price = $data['price'] ?? 0;
|
$product->price = $data['price'] ?? 0;
|
||||||
|
|
||||||
|
// Handle Barcode
|
||||||
|
if (!empty($data['barcode'])) {
|
||||||
|
$product->barcode = $data['barcode'];
|
||||||
|
} elseif (empty($product->barcode)) {
|
||||||
|
$product->barcode = $this->generateRandomBarcode();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle Code (SKU)
|
||||||
|
if (!empty($data['code'])) {
|
||||||
|
$product->code = $data['code'];
|
||||||
|
} elseif (empty($product->code)) {
|
||||||
|
$product->code = $this->generateRandomCode();
|
||||||
|
}
|
||||||
|
|
||||||
// Map newly added extended fields
|
// Map newly added extended fields
|
||||||
if (isset($data['brand'])) $product->brand = $data['brand'];
|
if (isset($data['brand'])) $product->brand = $data['brand'];
|
||||||
if (isset($data['specification'])) $product->specification = $data['specification'];
|
if (isset($data['specification'])) $product->specification = $data['specification'];
|
||||||
@@ -48,11 +61,6 @@ class ProductService implements ProductServiceInterface
|
|||||||
if (isset($data['member_price'])) $product->member_price = $data['member_price'];
|
if (isset($data['member_price'])) $product->member_price = $data['member_price'];
|
||||||
if (isset($data['wholesale_price'])) $product->wholesale_price = $data['wholesale_price'];
|
if (isset($data['wholesale_price'])) $product->wholesale_price = $data['wholesale_price'];
|
||||||
|
|
||||||
// Generate Code if missing (use code or external_id)
|
|
||||||
if (empty($product->code)) {
|
|
||||||
$product->code = $data['code'] ?? $product->external_pos_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle Category — 每次同步都更新(若有傳入)
|
// Handle Category — 每次同步都更新(若有傳入)
|
||||||
if (!empty($data['category']) || empty($product->category_id)) {
|
if (!empty($data['category']) || empty($product->category_id)) {
|
||||||
$categoryName = $data['category'] ?? '未分類';
|
$categoryName = $data['category'] ?? '未分類';
|
||||||
@@ -100,6 +108,17 @@ class ProductService implements ProductServiceInterface
|
|||||||
return Product::whereIn('external_pos_id', $externalPosIds)->get();
|
return Product::whereIn('external_pos_id', $externalPosIds)->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 透過多個 ERP 內部 ID 查找產品。
|
||||||
|
*
|
||||||
|
* @param array $ids
|
||||||
|
* @return \Illuminate\Database\Eloquent\Collection
|
||||||
|
*/
|
||||||
|
public function findByIds(array $ids)
|
||||||
|
{
|
||||||
|
return Product::whereIn('id', $ids)->get();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 透過多個 ERP 商品代碼查找產品(供販賣機 API 使用)。
|
* 透過多個 ERP 商品代碼查找產品(供販賣機 API 使用)。
|
||||||
*
|
*
|
||||||
@@ -110,4 +129,122 @@ class ProductService implements ProductServiceInterface
|
|||||||
{
|
{
|
||||||
return Product::whereIn('code', $codes)->get();
|
return Product::whereIn('code', $codes)->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function createProduct(array $data)
|
||||||
|
{
|
||||||
|
if (empty($data['code'])) {
|
||||||
|
$data['code'] = $this->generateRandomCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($data['barcode'])) {
|
||||||
|
$data['barcode'] = $this->generateRandomBarcode();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Product::create($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateProduct(Product $product, array $data)
|
||||||
|
{
|
||||||
|
if (empty($data['code'])) {
|
||||||
|
$data['code'] = $this->generateRandomCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($data['barcode'])) {
|
||||||
|
$data['barcode'] = $this->generateRandomBarcode();
|
||||||
|
}
|
||||||
|
|
||||||
|
$product->update($data);
|
||||||
|
return $product;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateRandomCode()
|
||||||
|
{
|
||||||
|
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||||
|
$code = '';
|
||||||
|
|
||||||
|
do {
|
||||||
|
$code = '';
|
||||||
|
for ($i = 0; $i < 8; $i++) {
|
||||||
|
$code .= $characters[rand(0, strlen($characters) - 1)];
|
||||||
|
}
|
||||||
|
} while (Product::where('code', $code)->exists());
|
||||||
|
|
||||||
|
return $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateRandomBarcode()
|
||||||
|
{
|
||||||
|
$barcode = '';
|
||||||
|
|
||||||
|
do {
|
||||||
|
$barcode = '';
|
||||||
|
for ($i = 0; $i < 13; $i++) {
|
||||||
|
$barcode .= rand(0, 9);
|
||||||
|
}
|
||||||
|
} while (Product::where('barcode', $barcode)->exists());
|
||||||
|
|
||||||
|
return $barcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findByBarcodeOrCode(?string $barcode, ?string $code)
|
||||||
|
{
|
||||||
|
$product = null;
|
||||||
|
if (!empty($barcode)) {
|
||||||
|
$product = Product::where('barcode', $barcode)->first();
|
||||||
|
}
|
||||||
|
if (!$product && !empty($code)) {
|
||||||
|
$product = Product::where('code', $code)->first();
|
||||||
|
}
|
||||||
|
return $product;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜尋商品(供外部 API 使用)。
|
||||||
|
*
|
||||||
|
* @param array $filters
|
||||||
|
* @param int $perPage
|
||||||
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||||
|
*/
|
||||||
|
public function searchProducts(array $filters, int $perPage = 50)
|
||||||
|
{
|
||||||
|
$query = Product::query()
|
||||||
|
->with(['category', 'baseUnit'])
|
||||||
|
->where('is_active', true);
|
||||||
|
|
||||||
|
// 1. 精準過濾 (ID, 條碼, 代碼, 外部 ID)
|
||||||
|
if (!empty($filters['product_id'])) {
|
||||||
|
$query->where('id', $filters['product_id']);
|
||||||
|
}
|
||||||
|
if (!empty($filters['barcode'])) {
|
||||||
|
$query->where('barcode', $filters['barcode']);
|
||||||
|
}
|
||||||
|
if (!empty($filters['code'])) {
|
||||||
|
$query->where('code', $filters['code']);
|
||||||
|
}
|
||||||
|
if (!empty($filters['external_pos_id'])) {
|
||||||
|
$query->where('external_pos_id', $filters['external_pos_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 分類過濾 (優先使用 ID,若傳入字串則按名稱)
|
||||||
|
if (!empty($filters['category'])) {
|
||||||
|
$categoryVal = $filters['category'];
|
||||||
|
if (is_numeric($categoryVal)) {
|
||||||
|
$query->where('category_id', $categoryVal);
|
||||||
|
} else {
|
||||||
|
$query->whereHas('category', function ($q) use ($categoryVal) {
|
||||||
|
$q->where('name', $categoryVal);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 增量同步 (Updated After)
|
||||||
|
if (!empty($filters['updated_after'])) {
|
||||||
|
$query->where('updated_at', '>=', $filters['updated_after']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 排序 (預設按更新時間降冪)
|
||||||
|
$query->orderBy('updated_at', 'desc');
|
||||||
|
|
||||||
|
return $query->paginate($perPage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,23 +23,84 @@ class StoreRequisitionService
|
|||||||
/**
|
/**
|
||||||
* 建立叫貨單(含明細)
|
* 建立叫貨單(含明細)
|
||||||
*/
|
*/
|
||||||
public function create(array $data, array $items, int $userId): StoreRequisition
|
public function create(array $data, array $items, int $userId, bool $submitImmediately = false): StoreRequisition
|
||||||
{
|
{
|
||||||
return DB::transaction(function () use ($data, $items, $userId) {
|
return DB::transaction(function () use ($data, $items, $userId, $submitImmediately) {
|
||||||
$requisition = StoreRequisition::create([
|
$requisition = new StoreRequisition([
|
||||||
'store_warehouse_id' => $data['store_warehouse_id'],
|
'store_warehouse_id' => $data['store_warehouse_id'],
|
||||||
'status' => 'draft',
|
'status' => $submitImmediately ? 'pending' : 'draft',
|
||||||
|
'submitted_at' => $submitImmediately ? now() : null,
|
||||||
'remark' => $data['remark'] ?? null,
|
'remark' => $data['remark'] ?? null,
|
||||||
'created_by' => $userId,
|
'created_by' => $userId,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// 手動產生單號,因為 saveQuietly 會繞過模型事件
|
||||||
|
if (empty($requisition->doc_no)) {
|
||||||
|
$today = date('Ymd');
|
||||||
|
$prefix = 'SR-' . $today . '-';
|
||||||
|
$lastDoc = StoreRequisition::where('doc_no', 'like', $prefix . '%')
|
||||||
|
->orderBy('doc_no', 'desc')
|
||||||
|
->first();
|
||||||
|
if ($lastDoc) {
|
||||||
|
$lastNumber = substr($lastDoc->doc_no, -2);
|
||||||
|
$nextNumber = str_pad((int)$lastNumber + 1, 2, '0', STR_PAD_LEFT);
|
||||||
|
} else {
|
||||||
|
$nextNumber = '01';
|
||||||
|
}
|
||||||
|
$requisition->doc_no = $prefix . $nextNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 靜默建立以抑制自動日誌
|
||||||
|
$requisition->saveQuietly();
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
|
$productIds = collect($items)->pluck('product_id')->unique()->toArray();
|
||||||
|
$products = \App\Modules\Inventory\Models\Product::whereIn('id', $productIds)->get()->keyBy('id');
|
||||||
|
|
||||||
|
$diff = ['added' => [], 'removed' => [], 'updated' => []];
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$requisition->items()->create([
|
$itemsToInsert[] = [
|
||||||
|
'store_requisition_id' => $requisition->id,
|
||||||
'product_id' => $item['product_id'],
|
'product_id' => $item['product_id'],
|
||||||
'requested_qty' => $item['requested_qty'],
|
'requested_qty' => $item['requested_qty'],
|
||||||
'remark' => $item['remark'] ?? null,
|
'remark' => $item['remark'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$product = $products->get($item['product_id']);
|
||||||
|
$diff['added'][] = [
|
||||||
|
'product_name' => $product?->name ?? '未知商品',
|
||||||
|
'new' => [
|
||||||
|
'quantity' => (float)$item['requested_qty'],
|
||||||
|
'remark' => $item['remark'] ?? null,
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
StoreRequisitionItem::insert($itemsToInsert);
|
||||||
|
|
||||||
|
// 如果需直接提交,觸發通知
|
||||||
|
if ($submitImmediately) {
|
||||||
|
$this->notifyApprovers($requisition, 'submitted', $userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 手動發送高品質日誌
|
||||||
|
activity()
|
||||||
|
->performedOn($requisition)
|
||||||
|
->causedBy($userId)
|
||||||
|
->event('created')
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => $diff,
|
||||||
|
'attributes' => [
|
||||||
|
'doc_no' => $requisition->doc_no,
|
||||||
|
'store_warehouse_id' => $requisition->store_warehouse_id,
|
||||||
|
'status' => $requisition->status,
|
||||||
|
'remark' => $requisition->remark,
|
||||||
|
'created_by' => $requisition->created_by,
|
||||||
|
'submitted_at' => $requisition->submitted_at,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log('created');
|
||||||
|
|
||||||
return $requisition->load('items');
|
return $requisition->load('items');
|
||||||
});
|
});
|
||||||
@@ -57,20 +118,112 @@ class StoreRequisitionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DB::transaction(function () use ($requisition, $data, $items) {
|
return DB::transaction(function () use ($requisition, $data, $items) {
|
||||||
$requisition->update([
|
// 擷取舊狀態供日誌對照
|
||||||
'store_warehouse_id' => $data['store_warehouse_id'],
|
$oldAttributes = [
|
||||||
'remark' => $data['remark'] ?? null,
|
'store_warehouse_id' => $requisition->store_warehouse_id,
|
||||||
'reject_reason' => null, // 清除駁回原因
|
'remark' => $requisition->remark,
|
||||||
]);
|
];
|
||||||
|
|
||||||
// 重建明細
|
// 手動更新屬性
|
||||||
|
$requisition->store_warehouse_id = $data['store_warehouse_id'];
|
||||||
|
$requisition->remark = $data['remark'] ?? null;
|
||||||
|
$requisition->reject_reason = null; // 清除駁回原因
|
||||||
|
|
||||||
|
// 品項對比邏輯
|
||||||
|
$oldItems = $requisition->items()->with('product:id,name')->get();
|
||||||
|
$oldItemsMap = $oldItems->keyBy('product_id');
|
||||||
|
$newItemsMap = collect($items)->keyBy('product_id');
|
||||||
|
|
||||||
|
$diff = [
|
||||||
|
'added' => [],
|
||||||
|
'removed' => [],
|
||||||
|
'updated' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
// 1. 處理更新與新增
|
||||||
|
foreach ($items as $itemData) {
|
||||||
|
$productId = $itemData['product_id'];
|
||||||
|
$newQty = (float)$itemData['requested_qty'];
|
||||||
|
$newRemark = $itemData['remark'] ?? null;
|
||||||
|
|
||||||
|
if ($oldItemsMap->has($productId)) {
|
||||||
|
$oldItem = $oldItemsMap->get($productId);
|
||||||
|
if ((float)$oldItem->requested_qty !== $newQty || $oldItem->remark !== $newRemark) {
|
||||||
|
$diff['updated'][] = [
|
||||||
|
'product_name' => $oldItem->product?->name ?? '未知商品',
|
||||||
|
'old' => [
|
||||||
|
'quantity' => (float)$oldItem->requested_qty,
|
||||||
|
'remark' => $oldItem->remark,
|
||||||
|
],
|
||||||
|
'new' => [
|
||||||
|
'quantity' => $newQty,
|
||||||
|
'remark' => $newRemark,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$oldItemsMap->forget($productId);
|
||||||
|
} else {
|
||||||
|
$product = \App\Modules\Inventory\Models\Product::find($productId);
|
||||||
|
$diff['added'][] = [
|
||||||
|
'product_name' => $product?->name ?? '未知商品',
|
||||||
|
'new' => [
|
||||||
|
'quantity' => $newQty,
|
||||||
|
'remark' => $newRemark,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 處理移除
|
||||||
|
foreach ($oldItemsMap as $productId => $oldItem) {
|
||||||
|
$diff['removed'][] = [
|
||||||
|
'product_name' => $oldItem->product?->name ?? '未知商品',
|
||||||
|
'old' => [
|
||||||
|
'quantity' => (float)$oldItem->requested_qty,
|
||||||
|
'remark' => $oldItem->remark,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 儲存實際變動
|
||||||
$requisition->items()->delete();
|
$requisition->items()->delete();
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$requisition->items()->create([
|
$itemsToInsert[] = [
|
||||||
|
'store_requisition_id' => $requisition->id,
|
||||||
'product_id' => $item['product_id'],
|
'product_id' => $item['product_id'],
|
||||||
'requested_qty' => $item['requested_qty'],
|
'requested_qty' => $item['requested_qty'],
|
||||||
'remark' => $item['remark'] ?? null,
|
'remark' => $item['remark'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
StoreRequisitionItem::insert($itemsToInsert);
|
||||||
|
|
||||||
|
// 檢查是否有任何變動 (主表或明細)
|
||||||
|
$isDirty = $requisition->isDirty();
|
||||||
|
$hasItemsDiff = !empty($diff['added']) || !empty($diff['removed']) || !empty($diff['updated']);
|
||||||
|
|
||||||
|
if ($isDirty || $hasItemsDiff) {
|
||||||
|
// 擷取新狀態
|
||||||
|
$newAttributes = [
|
||||||
|
'store_warehouse_id' => $requisition->store_warehouse_id,
|
||||||
|
'remark' => $requisition->remark,
|
||||||
|
];
|
||||||
|
|
||||||
|
// 靜默更新
|
||||||
|
$requisition->saveQuietly();
|
||||||
|
|
||||||
|
// 手動發送紀錄
|
||||||
|
activity()
|
||||||
|
->performedOn($requisition)
|
||||||
|
->event('updated')
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => $diff,
|
||||||
|
'attributes' => $newAttributes,
|
||||||
|
'old' => $oldAttributes
|
||||||
|
])
|
||||||
|
->log('updated');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $requisition->load('items');
|
return $requisition->load('items');
|
||||||
@@ -118,17 +271,90 @@ class StoreRequisitionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DB::transaction(function () use ($requisition, $data, $userId) {
|
return DB::transaction(function () use ($requisition, $data, $userId) {
|
||||||
// 更新核准數量
|
// 處理前端傳來的明細與批號資料
|
||||||
|
$processedItems = []; // 暫存處理後的明細,用於轉入調撥單
|
||||||
|
|
||||||
if (isset($data['items'])) {
|
if (isset($data['items'])) {
|
||||||
|
$requisition->load('items.product');
|
||||||
|
$reqItemMap = $requisition->items->keyBy('id');
|
||||||
|
|
||||||
foreach ($data['items'] as $itemData) {
|
foreach ($data['items'] as $itemData) {
|
||||||
StoreRequisitionItem::where('id', $itemData['id'])
|
$reqItemId = $itemData['id'];
|
||||||
|
$reqItem = $reqItemMap->get($reqItemId);
|
||||||
|
$productName = $reqItem?->product?->name ?? '未知商品';
|
||||||
|
$totalApprovedQty = 0;
|
||||||
|
$batches = $itemData['batches'] ?? [];
|
||||||
|
|
||||||
|
// 如果有批號,根據批號展開。若有多個無批號(null)的批次(例如來自不同貨道),則將其數量加總
|
||||||
|
if (!empty($batches)) {
|
||||||
|
$batchGroups = [];
|
||||||
|
foreach ($batches as $batch) {
|
||||||
|
$qty = (float)($batch['qty'] ?? 0);
|
||||||
|
$bNum = $batch['batch_number'] ?? null;
|
||||||
|
$invId = $batch['inventory_id'] ?? null;
|
||||||
|
|
||||||
|
if ($qty > 0) {
|
||||||
|
if ($invId) {
|
||||||
|
$inventory = \App\Modules\Inventory\Models\Inventory::lockForUpdate()->find($invId);
|
||||||
|
if ($inventory) {
|
||||||
|
$available = max(0, $inventory->quantity - $inventory->reserved_quantity);
|
||||||
|
if ($qty > $available) {
|
||||||
|
$batchStr = $bNum ? "批號 {$bNum}" : "無批號";
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'items' => "「{$productName}」的 {$batchStr} 數量({$qty})不可大於可用庫存({$available})",
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalApprovedQty += $qty;
|
||||||
|
$batchKey = $bNum ?? '';
|
||||||
|
$batchGroups[$batchKey] = ($batchGroups[$batchKey] ?? 0) + $qty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($batchGroups as $bNumKey => $qty) {
|
||||||
|
$processedItems[] = [
|
||||||
|
'req_item_id' => $reqItemId,
|
||||||
|
'batch_number' => $bNumKey === '' ? null : $bNumKey,
|
||||||
|
'quantity' => $qty,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 無批號,傳統輸入
|
||||||
|
$qty = (float)($itemData['approved_qty'] ?? 0);
|
||||||
|
if ($qty > 0) {
|
||||||
|
$supplyWarehouseId = $requisition->supply_warehouse_id;
|
||||||
|
$totalAvailable = \App\Modules\Inventory\Models\Inventory::where('warehouse_id', $supplyWarehouseId)
|
||||||
|
->where('product_id', $reqItem->product_id)
|
||||||
|
->lockForUpdate() // 補上鎖定
|
||||||
|
->selectRaw('SUM(quantity - reserved_quantity) as available')
|
||||||
|
->value('available') ?? 0;
|
||||||
|
|
||||||
|
if ($qty > $totalAvailable) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'items' => "「{$productName}」的數量({$qty})不可大於供貨倉可用總庫存({$totalAvailable})",
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalApprovedQty += $qty;
|
||||||
|
$processedItems[] = [
|
||||||
|
'req_item_id' => $reqItemId,
|
||||||
|
'batch_number' => null,
|
||||||
|
'quantity' => $qty,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新叫貨單明細的核准數量總和
|
||||||
|
StoreRequisitionItem::where('id', $reqItemId)
|
||||||
->where('store_requisition_id', $requisition->id)
|
->where('store_requisition_id', $requisition->id)
|
||||||
->update(['approved_qty' => $itemData['approved_qty']]);
|
->update(['approved_qty' => $totalApprovedQty]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 優先使用傳入的供貨倉庫,若無則從單據中取得
|
// 優先使用傳入的供貨倉庫,若無則從單據中取得
|
||||||
$supplyWarehouseId = $data['supply_warehouse_id'] ?? $requisition->supply_warehouse_id;
|
$supplyWarehouseId = $requisition->supply_warehouse_id;
|
||||||
|
|
||||||
if (!$supplyWarehouseId) {
|
if (!$supplyWarehouseId) {
|
||||||
throw ValidationException::withMessages([
|
throw ValidationException::withMessages([
|
||||||
@@ -152,18 +378,44 @@ class StoreRequisitionService
|
|||||||
// 將核准的明細寫入調撥單
|
// 將核准的明細寫入調撥單
|
||||||
$requisition->load('items');
|
$requisition->load('items');
|
||||||
$transferItems = [];
|
$transferItems = [];
|
||||||
foreach ($requisition->items as $item) {
|
|
||||||
$qty = $item->approved_qty ?? $item->requested_qty;
|
// 建立 req_item_id 對應 product_id 的 lookup
|
||||||
if ($qty > 0) {
|
$reqItemMap = $requisition->items->keyBy('id');
|
||||||
|
|
||||||
|
foreach ($processedItems as $pItem) {
|
||||||
|
$reqItem = $reqItemMap->get($pItem['req_item_id']);
|
||||||
|
if ($reqItem) {
|
||||||
$transferItems[] = [
|
$transferItems[] = [
|
||||||
'product_id' => $item->product_id,
|
'product_id' => $reqItem->product_id,
|
||||||
'quantity' => $qty,
|
'batch_number' => $pItem['batch_number'],
|
||||||
|
'quantity' => $pItem['quantity'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($transferItems)) {
|
if (!empty($transferItems)) {
|
||||||
$this->transferService->updateItems($transferOrder, $transferItems);
|
$this->transferService->updateItems($transferOrder, $transferItems);
|
||||||
|
|
||||||
|
// 手動發送調撥單的「已建立」合併日誌,包含初始明細
|
||||||
|
activity()
|
||||||
|
->performedOn($transferOrder)
|
||||||
|
->causedBy($userId)
|
||||||
|
->event('created')
|
||||||
|
->withProperties(array_merge(
|
||||||
|
['items_diff' => $transferOrder->activityProperties['items_diff'] ?? []],
|
||||||
|
[
|
||||||
|
'attributes' => [
|
||||||
|
'doc_no' => $transferOrder->doc_no,
|
||||||
|
'from_warehouse_id' => $transferOrder->from_warehouse_id,
|
||||||
|
'to_warehouse_id' => $transferOrder->to_warehouse_id,
|
||||||
|
'transit_warehouse_id' => $transferOrder->transit_warehouse_id,
|
||||||
|
'remarks' => $transferOrder->remarks,
|
||||||
|
'status' => $transferOrder->status,
|
||||||
|
'created_by' => $transferOrder->created_by,
|
||||||
|
]
|
||||||
|
]
|
||||||
|
))
|
||||||
|
->log('created');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新叫貨單狀態
|
// 更新叫貨單狀態
|
||||||
|
|||||||
326
app/Modules/Inventory/Services/TraceabilityService.php
Normal file
326
app/Modules/Inventory/Services/TraceabilityService.php
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Inventory\Services;
|
||||||
|
|
||||||
|
use App\Modules\Inventory\Models\Inventory;
|
||||||
|
use App\Modules\Inventory\Models\InventoryTransaction;
|
||||||
|
use App\Modules\Inventory\Models\GoodsReceiptItem;
|
||||||
|
use App\Modules\Inventory\Models\GoodsReceipt;
|
||||||
|
use App\Modules\Production\Contracts\ProductionServiceInterface;
|
||||||
|
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
class TraceabilityService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected ProductionServiceInterface $productionService,
|
||||||
|
protected ProcurementServiceInterface $procurementService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 逆向溯源:從成品批號往前追溯用到的所有原料與廠商
|
||||||
|
*
|
||||||
|
* @param string $batchNumber 成品批號
|
||||||
|
* @return array 樹狀結構資料
|
||||||
|
*/
|
||||||
|
public function traceBackward(string $batchNumber): array
|
||||||
|
{
|
||||||
|
// 取得基本庫存資訊以作為根節點參考
|
||||||
|
$baseInventory = Inventory::with(['product', 'warehouse'])
|
||||||
|
->where('batch_number', $batchNumber)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
// 定義根節點
|
||||||
|
$rootNode = [
|
||||||
|
'id' => 'batch_' . $batchNumber,
|
||||||
|
'type' => 'target_batch',
|
||||||
|
'label' => '查詢批號: ' . $batchNumber,
|
||||||
|
'batch_number' => $batchNumber,
|
||||||
|
'product_name' => $baseInventory?->product?->name,
|
||||||
|
'spec' => $baseInventory?->product?->spec,
|
||||||
|
'warehouse_name' => $baseInventory?->warehouse?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 1. 尋找這個批號是不是生產出來的成品 (Production Order Output)
|
||||||
|
// 透過 ProductionService 獲取,以落實模組解耦
|
||||||
|
$productionOrders = $this->productionService->getProductionOrdersByOutputBatch($batchNumber);
|
||||||
|
|
||||||
|
foreach ($productionOrders as $po) {
|
||||||
|
$poNode = [
|
||||||
|
'id' => 'po_' . $po->id,
|
||||||
|
'type' => 'production_order',
|
||||||
|
'label' => '生產工單: ' . $po->code,
|
||||||
|
'date' => $po->production_date instanceof \DateTimeInterface
|
||||||
|
? $po->production_date->format('Y-m-d')
|
||||||
|
: $po->production_date,
|
||||||
|
'quantity' => $po->output_quantity,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 針對每一張工單,尋找它投料的原料批號
|
||||||
|
foreach ($po->items as $item) {
|
||||||
|
if (isset($item->inventory)) {
|
||||||
|
$materialNode = $this->buildMaterialBackwardNode($item->inventory, $item);
|
||||||
|
$poNode['children'][] = $materialNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$rootNode['children'][] = $poNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 如果這批號是直接採購進來的 (Goods Receipt)
|
||||||
|
// 或者是為了補足直接查詢原料批號的場景
|
||||||
|
$inventories = Inventory::with(['product', 'warehouse'])
|
||||||
|
->where('batch_number', $batchNumber)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($inventories as $inv) {
|
||||||
|
// 尋找進貨單
|
||||||
|
$grItems = GoodsReceiptItem::with(['goodsReceipt', 'product'])
|
||||||
|
->where('batch_number', $batchNumber)
|
||||||
|
->where('product_id', $inv->product_id)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($grItems as $grItem) {
|
||||||
|
$gr = $grItem->goodsReceipt;
|
||||||
|
if ($gr) {
|
||||||
|
$grNode = [
|
||||||
|
'id' => 'gr_' . $gr->id . '_' . $inv->id,
|
||||||
|
'type' => 'goods_receipt',
|
||||||
|
'label' => '進貨單: ' . $gr->code,
|
||||||
|
'date' => $gr->received_date instanceof \DateTimeInterface
|
||||||
|
? $gr->received_date->format('Y-m-d')
|
||||||
|
: $gr->received_date,
|
||||||
|
'vendor_id' => $gr->vendor_id,
|
||||||
|
'quantity' => $grItem->quantity,
|
||||||
|
'product_name' => $grItem->product?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 避免重複加入
|
||||||
|
$isDuplicate = false;
|
||||||
|
foreach ($rootNode['children'] as $child) {
|
||||||
|
if ($child['id'] === $grNode['id']) {
|
||||||
|
$isDuplicate = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$isDuplicate) {
|
||||||
|
$rootNode['children'][] = $grNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 補充廠商名稱 (跨模組)
|
||||||
|
$this->hydrateVendorNames($rootNode);
|
||||||
|
|
||||||
|
return $rootNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 建立原料的逆向溯源節點
|
||||||
|
*/
|
||||||
|
private function buildMaterialBackwardNode(Inventory $inventory, $poItem = null): array
|
||||||
|
{
|
||||||
|
$node = [
|
||||||
|
'id' => 'inv_' . $inventory->id,
|
||||||
|
'type' => 'material_batch',
|
||||||
|
'label' => '原料批號: ' . $inventory->batch_number,
|
||||||
|
'product_name' => $inventory->product?->name,
|
||||||
|
'spec' => $inventory->product?->spec,
|
||||||
|
'batch_number' => $inventory->batch_number,
|
||||||
|
'quantity' => $poItem ? $poItem->quantity_used : null,
|
||||||
|
'warehouse_name' => $inventory->warehouse?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 繼續往下追溯該原料是怎麼來的 (進貨單)
|
||||||
|
if ($inventory->batch_number) {
|
||||||
|
$grItems = GoodsReceiptItem::with(['goodsReceipt', 'product'])
|
||||||
|
->where('batch_number', $inventory->batch_number)
|
||||||
|
->where('product_id', $inventory->product_id)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($grItems as $grItem) {
|
||||||
|
$gr = $grItem->goodsReceipt;
|
||||||
|
if ($gr) {
|
||||||
|
$node['children'][] = [
|
||||||
|
'id' => 'gr_' . $gr->id,
|
||||||
|
'type' => 'goods_receipt',
|
||||||
|
'label' => '進貨單: ' . $gr->code,
|
||||||
|
'date' => $gr->received_date instanceof \DateTimeInterface
|
||||||
|
? $gr->received_date->format('Y-m-d')
|
||||||
|
: $gr->received_date,
|
||||||
|
'vendor_id' => $gr->vendor_id,
|
||||||
|
'quantity' => $grItem->quantity,
|
||||||
|
'product_name' => $grItem->product?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 順向追蹤:從原料批號往後追查被用在哪些成品及去向
|
||||||
|
*
|
||||||
|
* @param string $batchNumber 原料批號
|
||||||
|
* @return array 樹狀結構資料
|
||||||
|
*/
|
||||||
|
public function traceForward(string $batchNumber): array
|
||||||
|
{
|
||||||
|
$baseInventory = Inventory::with(['product', 'warehouse'])
|
||||||
|
->where('batch_number', $batchNumber)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$rootNode = [
|
||||||
|
'id' => 'batch_' . $batchNumber,
|
||||||
|
'type' => 'source_batch',
|
||||||
|
'label' => '查詢批號: ' . $batchNumber,
|
||||||
|
'batch_number' => $batchNumber,
|
||||||
|
'product_name' => $baseInventory?->product?->name,
|
||||||
|
'spec' => $baseInventory?->product?->spec,
|
||||||
|
'warehouse_name' => $baseInventory?->warehouse?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 1. 尋找這個批號被哪些工單使用了
|
||||||
|
$inventories = Inventory::with(['product', 'warehouse'])->where('batch_number', $batchNumber)->get();
|
||||||
|
|
||||||
|
foreach ($inventories as $inv) {
|
||||||
|
// 透過 ProductionService 獲取,以落實模組解耦
|
||||||
|
$poItems = $this->productionService->getProductionOrderItemsByInventoryId($inv->id, ['productionOrder']);
|
||||||
|
|
||||||
|
foreach ($poItems as $item) {
|
||||||
|
$po = $item->productionOrder;
|
||||||
|
if ($po) {
|
||||||
|
$poNode = [
|
||||||
|
'id' => 'po_' . $po->id,
|
||||||
|
'type' => 'production_order',
|
||||||
|
'label' => '投入工單: ' . $po->code,
|
||||||
|
'date' => $po->production_date instanceof \DateTimeInterface
|
||||||
|
? $po->production_date->format('Y-m-d')
|
||||||
|
: $po->production_date,
|
||||||
|
'quantity' => $item->quantity_used,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 該工單產出的成品批號
|
||||||
|
if ($po->output_batch_number) {
|
||||||
|
$outputInventory = Inventory::with(['product', 'warehouse'])
|
||||||
|
->where('batch_number', $po->output_batch_number)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$outputNode = [
|
||||||
|
'id' => 'output_batch_' . $po->output_batch_number,
|
||||||
|
'type' => 'target_batch',
|
||||||
|
'label' => '產出成品: ' . $po->output_batch_number,
|
||||||
|
'batch_number' => $po->output_batch_number,
|
||||||
|
'quantity' => $po->output_quantity,
|
||||||
|
'product_name' => $outputInventory?->product?->name,
|
||||||
|
'spec' => $outputInventory?->product?->spec,
|
||||||
|
'warehouse_name' => $outputInventory?->warehouse?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
// 追蹤成品的出庫紀錄 (銷貨、領料等)
|
||||||
|
$outTransactions = InventoryTransaction::with(['reference', 'inventory.product'])
|
||||||
|
->whereHas('inventory', function ($q) use ($po) {
|
||||||
|
$q->where('batch_number', $po->output_batch_number);
|
||||||
|
})
|
||||||
|
->where('quantity', '<', 0) // 出庫
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($outTransactions as $txn) {
|
||||||
|
$refType = class_basename($txn->reference_type);
|
||||||
|
$outputNode['children'][] = [
|
||||||
|
'id' => 'txn_' . $txn->id,
|
||||||
|
'type' => 'outbound_transaction',
|
||||||
|
'label' => '出庫單據: ' . $refType . ' #' . $txn->reference_id,
|
||||||
|
'date' => $txn->actual_time,
|
||||||
|
'quantity' => abs($txn->quantity),
|
||||||
|
'product_name' => $txn->inventory?->product?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$poNode['children'][] = $outputNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rootNode['children'][] = $poNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 如果這個批號自己本身就有出庫紀錄 (不是被生產掉,而是直接被領走或賣掉)
|
||||||
|
foreach ($inventories as $inv) {
|
||||||
|
$outTransactions = InventoryTransaction::with(['reference', 'inventory.product'])
|
||||||
|
->where('inventory_id', $inv->id)
|
||||||
|
->where('quantity', '<', 0)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($outTransactions as $txn) {
|
||||||
|
// 如果是生產工單領料,上面已經處理過,這裡濾掉
|
||||||
|
if ($txn->reference_type && str_contains($txn->reference_type, 'ProductionOrder')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$refType = $txn->reference_type ? class_basename($txn->reference_type) : '未知';
|
||||||
|
$rootNode['children'][] = [
|
||||||
|
'id' => 'txn_direct_' . $txn->id,
|
||||||
|
'type' => 'outbound_transaction',
|
||||||
|
'label' => '直接出庫: ' . $refType . ' #' . $txn->reference_id,
|
||||||
|
'date' => $txn->actual_time,
|
||||||
|
'quantity' => abs($txn->quantity),
|
||||||
|
'product_name' => $txn->inventory?->product?->name,
|
||||||
|
'children' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rootNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 水和廠商名稱 (跨模組)
|
||||||
|
*/
|
||||||
|
private function hydrateVendorNames(array &$node): void
|
||||||
|
{
|
||||||
|
$vendorIds = [];
|
||||||
|
$this->collectVendorIds($node, $vendorIds);
|
||||||
|
|
||||||
|
if (empty($vendorIds)) return;
|
||||||
|
|
||||||
|
$vendors = $this->procurementService->getVendorsByIds(array_unique($vendorIds))->keyBy('id');
|
||||||
|
|
||||||
|
$this->applyVendorNames($node, $vendors);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectVendorIds(array $node, array &$ids): void
|
||||||
|
{
|
||||||
|
if (isset($node['vendor_id'])) {
|
||||||
|
$ids[] = $node['vendor_id'];
|
||||||
|
}
|
||||||
|
if (!empty($node['children'])) {
|
||||||
|
foreach ($node['children'] as $child) {
|
||||||
|
$this->collectVendorIds($child, $ids);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function applyVendorNames(array &$node, Collection $vendors): void
|
||||||
|
{
|
||||||
|
if (isset($node['vendor_id']) && $vendors->has($node['vendor_id'])) {
|
||||||
|
$vendor = $vendors->get($node['vendor_id']);
|
||||||
|
$node['label'] .= ' (廠商: ' . $vendor->name . ')';
|
||||||
|
}
|
||||||
|
if (!empty($node['children'])) {
|
||||||
|
foreach ($node['children'] as &$child) {
|
||||||
|
$this->applyVendorNames($child, $vendors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,8 +9,17 @@ use App\Modules\Inventory\Models\Warehouse;
|
|||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||||||
|
|
||||||
class TransferService
|
class TransferService
|
||||||
{
|
{
|
||||||
|
protected InventoryServiceInterface $inventoryService;
|
||||||
|
|
||||||
|
public function __construct(InventoryServiceInterface $inventoryService)
|
||||||
|
{
|
||||||
|
$this->inventoryService = $inventoryService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 建立調撥單草稿
|
* 建立調撥單草稿
|
||||||
*/
|
*/
|
||||||
@@ -24,7 +33,7 @@ class TransferService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return InventoryTransferOrder::create([
|
$order = new InventoryTransferOrder([
|
||||||
'from_warehouse_id' => $fromWarehouseId,
|
'from_warehouse_id' => $fromWarehouseId,
|
||||||
'to_warehouse_id' => $toWarehouseId,
|
'to_warehouse_id' => $toWarehouseId,
|
||||||
'transit_warehouse_id' => $transitWarehouseId,
|
'transit_warehouse_id' => $transitWarehouseId,
|
||||||
@@ -32,6 +41,26 @@ class TransferService
|
|||||||
'remarks' => $remarks,
|
'remarks' => $remarks,
|
||||||
'created_by' => $userId,
|
'created_by' => $userId,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// 手動觸發單號產生邏輯,因為 saveQuietly 繞過了 Model Events
|
||||||
|
if (empty($order->doc_no)) {
|
||||||
|
$today = date('Ymd');
|
||||||
|
$prefix = 'TRF-' . $today . '-';
|
||||||
|
$lastDoc = InventoryTransferOrder::where('doc_no', 'like', $prefix . '%')
|
||||||
|
->orderBy('doc_no', 'desc')
|
||||||
|
->first();
|
||||||
|
if ($lastDoc) {
|
||||||
|
$lastNumber = substr($lastDoc->doc_no, -2);
|
||||||
|
$nextNumber = str_pad((int)$lastNumber + 1, 2, '0', STR_PAD_LEFT);
|
||||||
|
} else {
|
||||||
|
$nextNumber = '01';
|
||||||
|
}
|
||||||
|
$order->doc_no = $prefix . $nextNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
$order->saveQuietly();
|
||||||
|
|
||||||
|
return $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,55 +74,107 @@ class TransferService
|
|||||||
return [$key => $item];
|
return [$key => $item];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 釋放舊明細的預扣庫存 (必須加鎖,防止並發更新時數量出錯)
|
||||||
|
foreach ($order->items as $item) {
|
||||||
|
$inv = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
||||||
|
->where('product_id', $item->product_id)
|
||||||
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
|
->first();
|
||||||
|
if ($inv) {
|
||||||
|
$inv->releaseReservedQuantity($item->quantity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$diff = [
|
$diff = [
|
||||||
'added' => [],
|
'added' => [],
|
||||||
'removed' => [],
|
'removed' => [],
|
||||||
'updated' => [],
|
'updated' => [],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 先刪除舊明細
|
||||||
$order->items()->delete();
|
$order->items()->delete();
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
$newItemsKeys = [];
|
$newItemsKeys = [];
|
||||||
|
|
||||||
|
// 1. 批量收集待插入的明細數據
|
||||||
foreach ($itemsData as $data) {
|
foreach ($itemsData as $data) {
|
||||||
$key = $data['product_id'] . '_' . ($data['batch_number'] ?? '');
|
$key = $data['product_id'] . '_' . ($data['batch_number'] ?? '');
|
||||||
$newItemsKeys[] = $key;
|
$newItemsKeys[] = $key;
|
||||||
|
|
||||||
$item = $order->items()->create([
|
$itemsToInsert[] = [
|
||||||
|
'transfer_order_id' => $order->id,
|
||||||
'product_id' => $data['product_id'],
|
'product_id' => $data['product_id'],
|
||||||
'batch_number' => $data['batch_number'] ?? null,
|
'batch_number' => $data['batch_number'] ?? null,
|
||||||
'quantity' => $data['quantity'],
|
'quantity' => $data['quantity'],
|
||||||
'position' => $data['position'] ?? null,
|
'position' => $data['position'] ?? null,
|
||||||
'notes' => $data['notes'] ?? null,
|
'notes' => $data['notes'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
$item->load('product');
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 執行批量寫入 (提升效能:100 筆明細只需 1 次寫入)
|
||||||
|
if (!empty($itemsToInsert)) {
|
||||||
|
InventoryTransferItem::insert($itemsToInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 重新載入明細進行預扣處理與 Diff 計算 (因 insert 不返回 Model)
|
||||||
|
$order->load(['items.product.baseUnit']);
|
||||||
|
|
||||||
|
foreach ($order->items as $item) {
|
||||||
|
$key = $item->product_id . '_' . ($item->batch_number ?? '');
|
||||||
|
|
||||||
|
// 增加新明細的預扣庫存 (使用 lockForUpdate 確保並發安全)
|
||||||
|
$inv = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
||||||
|
->where('product_id', $item->product_id)
|
||||||
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$inv) {
|
||||||
|
$inv = Inventory::create([
|
||||||
|
'warehouse_id' => $order->from_warehouse_id,
|
||||||
|
'product_id' => $item->product_id,
|
||||||
|
'batch_number' => $item->batch_number,
|
||||||
|
'quantity' => 0,
|
||||||
|
'unit_cost' => 0,
|
||||||
|
'total_value' => 0,
|
||||||
|
]);
|
||||||
|
$inv = $inv->fresh()->lockForUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
$inv->reserveQuantity($item->quantity);
|
||||||
|
|
||||||
|
// 計算 Diff 用於日誌
|
||||||
|
$data = collect($itemsData)->first(fn($d) => $d['product_id'] == $item->product_id && ($d['batch_number'] ?? '') == ($item->batch_number ?? ''));
|
||||||
|
|
||||||
if ($oldItemsMap->has($key)) {
|
if ($oldItemsMap->has($key)) {
|
||||||
$oldItem = $oldItemsMap->get($key);
|
$oldItem = $oldItemsMap->get($key);
|
||||||
if ((float)$oldItem->quantity !== (float)$data['quantity'] ||
|
if ((float)$oldItem->quantity !== (float)$item->quantity ||
|
||||||
$oldItem->notes !== ($data['notes'] ?? null) ||
|
$oldItem->notes !== $item->notes ||
|
||||||
$oldItem->position !== ($data['position'] ?? null)) {
|
$oldItem->position !== $item->position) {
|
||||||
|
|
||||||
$diff['updated'][] = [
|
$diff['updated'][] = [
|
||||||
'product_name' => $item->product->name,
|
'product_name' => $item->product->name,
|
||||||
|
'unit_name' => $item->product->baseUnit?->name,
|
||||||
'old' => [
|
'old' => [
|
||||||
'quantity' => (float)$oldItem->quantity,
|
'quantity' => (float)$oldItem->quantity,
|
||||||
'position' => $oldItem->position,
|
'position' => $oldItem->position,
|
||||||
'notes' => $oldItem->notes,
|
'notes' => $oldItem->notes,
|
||||||
],
|
],
|
||||||
'new' => [
|
'new' => [
|
||||||
'quantity' => (float)$data['quantity'],
|
'quantity' => (float)$item->quantity,
|
||||||
'position' => $item->position,
|
'position' => $item->position,
|
||||||
'notes' => $item->notes,
|
'notes' => $item->notes,
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$diff['updated'][] = [
|
$diff['added'][] = [
|
||||||
'product_name' => $item->product->name,
|
'product_name' => $item->product->name,
|
||||||
'old' => [
|
'unit_name' => $item->product->baseUnit?->name,
|
||||||
'quantity' => 0,
|
|
||||||
'notes' => null,
|
|
||||||
],
|
|
||||||
'new' => [
|
'new' => [
|
||||||
'quantity' => (float)$item->quantity,
|
'quantity' => (float)$item->quantity,
|
||||||
'notes' => $item->notes,
|
'notes' => $item->notes,
|
||||||
@@ -105,7 +186,8 @@ class TransferService
|
|||||||
foreach ($oldItemsMap as $key => $oldItem) {
|
foreach ($oldItemsMap as $key => $oldItem) {
|
||||||
if (!in_array($key, $newItemsKeys)) {
|
if (!in_array($key, $newItemsKeys)) {
|
||||||
$diff['removed'][] = [
|
$diff['removed'][] = [
|
||||||
'product_name' => $oldItem->product->name,
|
'product_name' => $oldItem->product?->name ?? "未知商品 (ID: {$oldItem->product_id})",
|
||||||
|
'unit_name' => $oldItem->product?->baseUnit?->name,
|
||||||
'old' => [
|
'old' => [
|
||||||
'quantity' => (float)$oldItem->quantity,
|
'quantity' => (float)$oldItem->quantity,
|
||||||
'notes' => $oldItem->notes,
|
'notes' => $oldItem->notes,
|
||||||
@@ -125,9 +207,6 @@ class TransferService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 出貨 (Dispatch) - 根據是否有在途倉決定流程
|
* 出貨 (Dispatch) - 根據是否有在途倉決定流程
|
||||||
*
|
|
||||||
* 有在途倉:來源倉扣除 → 在途倉增加,狀態改為 dispatched
|
|
||||||
* 無在途倉:來源倉扣除 → 目的倉增加,狀態改為 completed(維持原有邏輯)
|
|
||||||
*/
|
*/
|
||||||
public function dispatch(InventoryTransferOrder $order, int $userId): void
|
public function dispatch(InventoryTransferOrder $order, int $userId): void
|
||||||
{
|
{
|
||||||
@@ -140,16 +219,16 @@ class TransferService
|
|||||||
$targetWarehouseId = $hasTransit ? $order->transit_warehouse_id : $order->to_warehouse_id;
|
$targetWarehouseId = $hasTransit ? $order->transit_warehouse_id : $order->to_warehouse_id;
|
||||||
$targetWarehouse = $hasTransit ? $order->transitWarehouse : $order->toWarehouse;
|
$targetWarehouse = $hasTransit ? $order->transitWarehouse : $order->toWarehouse;
|
||||||
|
|
||||||
$outType = '調撥出庫';
|
$itemsDiff = [];
|
||||||
$inType = $hasTransit ? '在途入庫' : '調撥入庫';
|
|
||||||
|
|
||||||
foreach ($order->items as $item) {
|
foreach ($order->items as $item) {
|
||||||
if ($item->quantity <= 0) continue;
|
if ($item->quantity <= 0) continue;
|
||||||
|
|
||||||
// 1. 處理來源倉 (扣除)
|
// 1. 處理來源倉 (扣除) - 使用 lockForUpdate 防止超賣
|
||||||
$sourceInventory = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
$sourceInventory = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
||||||
->where('product_id', $item->product_id)
|
->where('product_id', $item->product_id)
|
||||||
->where('batch_number', $item->batch_number)
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$sourceInventory || $sourceInventory->quantity < $item->quantity) {
|
if (!$sourceInventory || $sourceInventory->quantity < $item->quantity) {
|
||||||
@@ -160,67 +239,65 @@ class TransferService
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldSourceQty = $sourceInventory->quantity;
|
$sourceBefore = (float) $sourceInventory->quantity;
|
||||||
$newSourceQty = $oldSourceQty - $item->quantity;
|
|
||||||
|
|
||||||
$item->update(['snapshot_quantity' => $oldSourceQty]);
|
|
||||||
|
|
||||||
$sourceInventory->quantity = $newSourceQty;
|
// 釋放草稿階段預扣的庫存
|
||||||
$sourceInventory->total_value = $sourceInventory->quantity * $sourceInventory->unit_cost;
|
$sourceInventory->reserved_quantity = max(0, $sourceInventory->reserved_quantity - $item->quantity);
|
||||||
$sourceInventory->save();
|
$sourceInventory->saveQuietly();
|
||||||
|
|
||||||
$sourceInventory->transactions()->create([
|
$item->update(['snapshot_quantity' => $sourceBefore]);
|
||||||
'type' => $outType,
|
|
||||||
'quantity' => -$item->quantity,
|
|
||||||
'unit_cost' => $sourceInventory->unit_cost,
|
|
||||||
'balance_before' => $oldSourceQty,
|
|
||||||
'balance_after' => $newSourceQty,
|
|
||||||
'reason' => "調撥單 {$order->doc_no} 至 {$targetWarehouse->name}",
|
|
||||||
'actual_time' => now(),
|
|
||||||
'user_id' => $userId,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// 2. 處理目的倉/在途倉 (增加)
|
// 委託 InventoryService 處理扣庫與 Transaction
|
||||||
$targetInventory = Inventory::firstOrCreate(
|
$this->inventoryService->decreaseInventoryQuantity(
|
||||||
[
|
$sourceInventory->id,
|
||||||
'warehouse_id' => $targetWarehouseId,
|
$item->quantity,
|
||||||
'product_id' => $item->product_id,
|
"調撥單 {$order->doc_no} 至 {$targetWarehouse->name}",
|
||||||
'batch_number' => $item->batch_number,
|
InventoryTransferOrder::class,
|
||||||
'location' => $hasTransit ? null : ($item->position ?? null),
|
$order->id
|
||||||
],
|
|
||||||
[
|
|
||||||
'quantity' => 0,
|
|
||||||
'unit_cost' => $sourceInventory->unit_cost,
|
|
||||||
'total_value' => 0,
|
|
||||||
'expiry_date' => $sourceInventory->expiry_date,
|
|
||||||
'quality_status' => $sourceInventory->quality_status,
|
|
||||||
'origin_country' => $sourceInventory->origin_country,
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($targetInventory->wasRecentlyCreated && $targetInventory->unit_cost == 0) {
|
|
||||||
$targetInventory->unit_cost = $sourceInventory->unit_cost;
|
|
||||||
}
|
|
||||||
|
|
||||||
$oldTargetQty = $targetInventory->quantity;
|
$sourceAfter = $sourceBefore - (float) $item->quantity;
|
||||||
$newTargetQty = $oldTargetQty + $item->quantity;
|
|
||||||
|
|
||||||
$targetInventory->quantity = $newTargetQty;
|
// 2. 處理目的倉/在途倉 (增加) - 同樣需要鎖定,防止並發增加時出現 Race Condition
|
||||||
$targetInventory->total_value = $targetInventory->quantity * $targetInventory->unit_cost;
|
$targetInventoryBefore = Inventory::where('warehouse_id', $targetWarehouseId)
|
||||||
$targetInventory->save();
|
->where('product_id', $item->product_id)
|
||||||
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
|
->first();
|
||||||
|
$targetBefore = $targetInventoryBefore ? (float) $targetInventoryBefore->quantity : 0;
|
||||||
|
|
||||||
$targetInventory->transactions()->create([
|
$this->inventoryService->createInventoryRecord([
|
||||||
'type' => $inType,
|
'warehouse_id' => $targetWarehouseId,
|
||||||
|
'product_id' => $item->product_id,
|
||||||
'quantity' => $item->quantity,
|
'quantity' => $item->quantity,
|
||||||
'unit_cost' => $targetInventory->unit_cost,
|
'unit_cost' => $sourceInventory->unit_cost,
|
||||||
'balance_before' => $oldTargetQty,
|
'batch_number' => $item->batch_number,
|
||||||
'balance_after' => $newTargetQty,
|
'expiry_date' => $sourceInventory->expiry_date,
|
||||||
'reason' => "調撥單 {$order->doc_no} 來自 {$fromWarehouse->name}",
|
'reason' => "調撥單 {$order->doc_no} 來自 {$fromWarehouse->name}",
|
||||||
'actual_time' => now(),
|
'reference_type' => InventoryTransferOrder::class,
|
||||||
'user_id' => $userId,
|
'reference_id' => $order->id,
|
||||||
|
'location' => $hasTransit ? null : ($item->position ?? null),
|
||||||
|
'origin_country' => $sourceInventory->origin_country,
|
||||||
|
'quality_status' => $sourceInventory->quality_status,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$targetAfter = $targetBefore + (float) $item->quantity;
|
||||||
|
|
||||||
|
// 記錄異動明細供整合日誌使用
|
||||||
|
$itemsDiff[] = [
|
||||||
|
'product_name' => $item->product->name,
|
||||||
|
'batch_number' => $item->batch_number,
|
||||||
|
'quantity' => (float)$item->quantity,
|
||||||
|
'source_warehouse' => $fromWarehouse->name,
|
||||||
|
'source_before' => $sourceBefore,
|
||||||
|
'source_after' => $sourceAfter,
|
||||||
|
'target_warehouse' => $targetWarehouse->name,
|
||||||
|
'target_before' => $targetBefore,
|
||||||
|
'target_after' => $targetAfter,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$oldStatus = $order->status;
|
||||||
if ($hasTransit) {
|
if ($hasTransit) {
|
||||||
$order->status = 'dispatched';
|
$order->status = 'dispatched';
|
||||||
$order->dispatched_at = now();
|
$order->dispatched_at = now();
|
||||||
@@ -230,13 +307,32 @@ class TransferService
|
|||||||
$order->posted_at = now();
|
$order->posted_at = now();
|
||||||
$order->posted_by = $userId;
|
$order->posted_by = $userId;
|
||||||
}
|
}
|
||||||
$order->save();
|
$order->saveQuietly();
|
||||||
|
|
||||||
|
// 手動觸發單一合併日誌
|
||||||
|
activity()
|
||||||
|
->performedOn($order)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('updated')
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => $itemsDiff,
|
||||||
|
'attributes' => [
|
||||||
|
'status' => $order->status,
|
||||||
|
'dispatched_at' => $order->dispatched_at ? $order->dispatched_at->format('Y-m-d H:i:s') : null,
|
||||||
|
'posted_at' => $order->posted_at ? $order->posted_at->format('Y-m-d H:i:s') : null,
|
||||||
|
'dispatched_by' => $order->dispatched_by,
|
||||||
|
'posted_by' => $order->posted_by,
|
||||||
|
],
|
||||||
|
'old' => [
|
||||||
|
'status' => $oldStatus,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log($order->status == 'completed' ? 'posted' : 'dispatched');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 收貨確認 (Receive) - 在途倉扣除 → 目的倉增加
|
* 收貨確認 (Receive) - 在途倉扣除 → 目的倉增加
|
||||||
* 僅適用於有在途倉且狀態為 dispatched 的調撥單
|
|
||||||
*/
|
*/
|
||||||
public function receive(InventoryTransferOrder $order, int $userId): void
|
public function receive(InventoryTransferOrder $order, int $userId): void
|
||||||
{
|
{
|
||||||
@@ -254,13 +350,16 @@ class TransferService
|
|||||||
$transitWarehouse = $order->transitWarehouse;
|
$transitWarehouse = $order->transitWarehouse;
|
||||||
$toWarehouse = $order->toWarehouse;
|
$toWarehouse = $order->toWarehouse;
|
||||||
|
|
||||||
|
$itemsDiff = [];
|
||||||
|
|
||||||
foreach ($order->items as $item) {
|
foreach ($order->items as $item) {
|
||||||
if ($item->quantity <= 0) continue;
|
if ($item->quantity <= 0) continue;
|
||||||
|
|
||||||
// 1. 在途倉扣除
|
// 1. 在途倉扣除 - 使用 lockForUpdate 防止超賣
|
||||||
$transitInventory = Inventory::where('warehouse_id', $order->transit_warehouse_id)
|
$transitInventory = Inventory::where('warehouse_id', $order->transit_warehouse_id)
|
||||||
->where('product_id', $item->product_id)
|
->where('product_id', $item->product_id)
|
||||||
->where('batch_number', $item->batch_number)
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if (!$transitInventory || $transitInventory->quantity < $item->quantity) {
|
if (!$transitInventory || $transitInventory->quantity < $item->quantity) {
|
||||||
@@ -270,71 +369,84 @@ class TransferService
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldTransitQty = $transitInventory->quantity;
|
$transitBefore = (float) $transitInventory->quantity;
|
||||||
$newTransitQty = $oldTransitQty - $item->quantity;
|
|
||||||
|
|
||||||
$transitInventory->quantity = $newTransitQty;
|
// 委託 InventoryService 處理扣庫與 Transaction
|
||||||
$transitInventory->total_value = $transitInventory->quantity * $transitInventory->unit_cost;
|
$this->inventoryService->decreaseInventoryQuantity(
|
||||||
$transitInventory->save();
|
$transitInventory->id,
|
||||||
|
$item->quantity,
|
||||||
$transitInventory->transactions()->create([
|
"調撥單 {$order->doc_no} 配送至 {$toWarehouse->name}",
|
||||||
'type' => '在途出庫',
|
InventoryTransferOrder::class,
|
||||||
'quantity' => -$item->quantity,
|
$order->id
|
||||||
'unit_cost' => $transitInventory->unit_cost,
|
|
||||||
'balance_before' => $oldTransitQty,
|
|
||||||
'balance_after' => $newTransitQty,
|
|
||||||
'reason' => "調撥單 {$order->doc_no} 配送至 {$toWarehouse->name}",
|
|
||||||
'actual_time' => now(),
|
|
||||||
'user_id' => $userId,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// 2. 目的倉增加
|
|
||||||
$targetInventory = Inventory::firstOrCreate(
|
|
||||||
[
|
|
||||||
'warehouse_id' => $order->to_warehouse_id,
|
|
||||||
'product_id' => $item->product_id,
|
|
||||||
'batch_number' => $item->batch_number,
|
|
||||||
'location' => $item->position,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'quantity' => 0,
|
|
||||||
'unit_cost' => $transitInventory->unit_cost,
|
|
||||||
'total_value' => 0,
|
|
||||||
'expiry_date' => $transitInventory->expiry_date,
|
|
||||||
'quality_status' => $transitInventory->quality_status,
|
|
||||||
'origin_country' => $transitInventory->origin_country,
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$transitAfter = $transitBefore - (float) $item->quantity;
|
||||||
|
|
||||||
if ($targetInventory->wasRecentlyCreated && $targetInventory->unit_cost == 0) {
|
// 2. 目的倉增加 - 同樣需要鎖定
|
||||||
$targetInventory->unit_cost = $transitInventory->unit_cost;
|
$targetInventoryBefore = Inventory::where('warehouse_id', $order->to_warehouse_id)
|
||||||
}
|
->where('product_id', $item->product_id)
|
||||||
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
|
->first();
|
||||||
|
$targetBefore = $targetInventoryBefore ? (float) $targetInventoryBefore->quantity : 0;
|
||||||
|
|
||||||
$oldTargetQty = $targetInventory->quantity;
|
$this->inventoryService->createInventoryRecord([
|
||||||
$newTargetQty = $oldTargetQty + $item->quantity;
|
'warehouse_id' => $order->to_warehouse_id,
|
||||||
|
'product_id' => $item->product_id,
|
||||||
$targetInventory->quantity = $newTargetQty;
|
|
||||||
$targetInventory->total_value = $targetInventory->quantity * $targetInventory->unit_cost;
|
|
||||||
$targetInventory->save();
|
|
||||||
|
|
||||||
$targetInventory->transactions()->create([
|
|
||||||
'type' => '調撥入庫',
|
|
||||||
'quantity' => $item->quantity,
|
'quantity' => $item->quantity,
|
||||||
'unit_cost' => $targetInventory->unit_cost,
|
'unit_cost' => $transitInventory->unit_cost,
|
||||||
'balance_before' => $oldTargetQty,
|
'batch_number' => $item->batch_number,
|
||||||
'balance_after' => $newTargetQty,
|
'expiry_date' => $transitInventory->expiry_date,
|
||||||
'reason' => "調撥單 {$order->doc_no} 來自 {$transitWarehouse->name}",
|
'reason' => "調撥單 {$order->doc_no} 來自 {$transitWarehouse->name}",
|
||||||
'actual_time' => now(),
|
'reference_type' => InventoryTransferOrder::class,
|
||||||
'user_id' => $userId,
|
'reference_id' => $order->id,
|
||||||
|
'location' => $item->position,
|
||||||
|
'origin_country' => $transitInventory->origin_country,
|
||||||
|
'quality_status' => $transitInventory->quality_status,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$targetAfter = $targetBefore + (float) $item->quantity;
|
||||||
|
|
||||||
|
$itemsDiff[] = [
|
||||||
|
'product_name' => $item->product->name,
|
||||||
|
'batch_number' => $item->batch_number,
|
||||||
|
'quantity' => (float)$item->quantity,
|
||||||
|
'source_warehouse' => $transitWarehouse->name,
|
||||||
|
'source_before' => $transitBefore,
|
||||||
|
'source_after' => $transitAfter,
|
||||||
|
'target_warehouse' => $toWarehouse->name,
|
||||||
|
'target_before' => $targetBefore,
|
||||||
|
'target_after' => $targetAfter,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$oldStatus = $order->status;
|
||||||
$order->status = 'completed';
|
$order->status = 'completed';
|
||||||
$order->posted_at = now();
|
$order->posted_at = now();
|
||||||
$order->posted_by = $userId;
|
$order->posted_by = $userId;
|
||||||
$order->received_at = now();
|
$order->received_at = now();
|
||||||
$order->received_by = $userId;
|
$order->received_by = $userId;
|
||||||
$order->save();
|
$order->saveQuietly();
|
||||||
|
|
||||||
|
// 手動觸發單一合併日誌
|
||||||
|
activity()
|
||||||
|
->performedOn($order)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('updated')
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => $itemsDiff,
|
||||||
|
'attributes' => [
|
||||||
|
'status' => 'completed',
|
||||||
|
'posted_at' => $order->posted_at->format('Y-m-d H:i:s'),
|
||||||
|
'received_at' => $order->received_at->format('Y-m-d H:i:s'),
|
||||||
|
'posted_by' => $order->posted_by,
|
||||||
|
'received_by' => $order->received_by,
|
||||||
|
],
|
||||||
|
'old' => [
|
||||||
|
'status' => $oldStatus,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log('received');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,9 +458,37 @@ class TransferService
|
|||||||
if ($order->status !== 'draft') {
|
if ($order->status !== 'draft') {
|
||||||
throw new \Exception('只能作廢草稿狀態的單據');
|
throw new \Exception('只能作廢草稿狀態的單據');
|
||||||
}
|
}
|
||||||
$order->update([
|
|
||||||
'status' => 'voided',
|
DB::transaction(function () use ($order, $userId) {
|
||||||
'updated_by' => $userId
|
foreach ($order->items as $item) {
|
||||||
]);
|
$inv = Inventory::where('warehouse_id', $order->from_warehouse_id)
|
||||||
|
->where('product_id', $item->product_id)
|
||||||
|
->where('batch_number', $item->batch_number)
|
||||||
|
->lockForUpdate()
|
||||||
|
->first();
|
||||||
|
if ($inv) {
|
||||||
|
$inv->releaseReservedQuantity($item->quantity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$oldStatus = $order->status;
|
||||||
|
$order->status = 'voided';
|
||||||
|
$order->updated_by = $userId;
|
||||||
|
$order->saveQuietly();
|
||||||
|
|
||||||
|
activity()
|
||||||
|
->performedOn($order)
|
||||||
|
->causedBy(auth()->user())
|
||||||
|
->event('updated')
|
||||||
|
->withProperties([
|
||||||
|
'attributes' => [
|
||||||
|
'status' => 'voided',
|
||||||
|
],
|
||||||
|
'old' => [
|
||||||
|
'status' => $oldStatus,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log('voided');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ class TurnoverService
|
|||||||
/**
|
/**
|
||||||
* Get inventory turnover analysis data
|
* Get inventory turnover analysis data
|
||||||
*/
|
*/
|
||||||
public function getAnalysisData(array $filters, int $perPage = 20)
|
public function getAnalysisData(array $filters, ?int $perPage = null)
|
||||||
{
|
{
|
||||||
|
$perPage = $perPage ?? \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
$warehouseId = $filters['warehouse_id'] ?? null;
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
$categoryId = $filters['category_id'] ?? null;
|
$categoryId = $filters['category_id'] ?? null;
|
||||||
$search = $filters['search'] ?? null;
|
$search = $filters['search'] ?? null;
|
||||||
@@ -60,13 +61,20 @@ class TurnoverService
|
|||||||
// Given potentially large data, subquery per row might be slow, but for pagination it's okay-ish.
|
// Given potentially large data, subquery per row might be slow, but for pagination it's okay-ish.
|
||||||
// Better approach: Join with a subquery of aggregated transactions.
|
// Better approach: Join with a subquery of aggregated transactions.
|
||||||
|
|
||||||
$thirtyDaysAgo = Carbon::now()->subDays(30);
|
$analysisDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.analysis_period_days', 30);
|
||||||
|
$thirtyDaysAgo = Carbon::now()->subDays($analysisDays);
|
||||||
|
|
||||||
// Subquery for 30-day sales
|
// Subquery for 30-day sales
|
||||||
$salesSubquery = InventoryTransaction::query()
|
$salesSubquery = InventoryTransaction::query()
|
||||||
->select('inventories.product_id', DB::raw('ABS(SUM(inventory_transactions.quantity)) as sales_qty_30d'))
|
->select('inventories.product_id', DB::raw('ABS(SUM(inventory_transactions.quantity)) as sales_qty_30d'))
|
||||||
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
||||||
->where('inventory_transactions.type', '出庫') // Adjust type as needed based on actual data
|
->where('inventory_transactions.type', '出庫') // Adjust type as needed based on actual data
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->whereIn('inventory_transactions.reference_type', [
|
||||||
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
|
\App\Modules\Sales\Models\SalesImportBatch::class,
|
||||||
|
])->orWhereNull('inventory_transactions.reference_type');
|
||||||
|
})
|
||||||
->where('inventory_transactions.actual_time', '>=', $thirtyDaysAgo)
|
->where('inventory_transactions.actual_time', '>=', $thirtyDaysAgo)
|
||||||
->groupBy('inventories.product_id');
|
->groupBy('inventories.product_id');
|
||||||
|
|
||||||
@@ -85,6 +93,12 @@ class TurnoverService
|
|||||||
->select('inventories.product_id', DB::raw('MAX(actual_time) as last_sale_date'))
|
->select('inventories.product_id', DB::raw('MAX(actual_time) as last_sale_date'))
|
||||||
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
||||||
->where('inventory_transactions.type', '出庫')
|
->where('inventory_transactions.type', '出庫')
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->whereIn('inventory_transactions.reference_type', [
|
||||||
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
|
\App\Modules\Sales\Models\SalesImportBatch::class,
|
||||||
|
])->orWhereNull('inventory_transactions.reference_type');
|
||||||
|
})
|
||||||
->groupBy('inventories.product_id');
|
->groupBy('inventories.product_id');
|
||||||
|
|
||||||
if ($warehouseId) {
|
if ($warehouseId) {
|
||||||
@@ -111,7 +125,7 @@ class TurnoverService
|
|||||||
// Turnover Days Calculation in SQL: (stock / (sales_30d / 30)) => (stock * 30) / sales_30d
|
// Turnover Days Calculation in SQL: (stock / (sales_30d / 30)) => (stock * 30) / sales_30d
|
||||||
// Handle division by zero: if sales_30d is 0, turnover is 'Inf' (or very high number like 9999)
|
// Handle division by zero: if sales_30d is 0, turnover is 'Inf' (or very high number like 9999)
|
||||||
$turnoverDaysSql = "CASE WHEN COALESCE(sales_30d.sales_qty_30d, 0) > 0
|
$turnoverDaysSql = "CASE WHEN COALESCE(sales_30d.sales_qty_30d, 0) > 0
|
||||||
THEN (COALESCE(SUM(inventories.quantity), 0) * 30) / sales_30d.sales_qty_30d
|
THEN (COALESCE(SUM(inventories.quantity), 0) * $analysisDays) / sales_30d.sales_qty_30d
|
||||||
ELSE 9999 END";
|
ELSE 9999 END";
|
||||||
|
|
||||||
$query->addSelect(DB::raw("$turnoverDaysSql as turnover_days"));
|
$query->addSelect(DB::raw("$turnoverDaysSql as turnover_days"));
|
||||||
@@ -125,7 +139,8 @@ class TurnoverService
|
|||||||
// For dead stock, definitive IS stock > 0.
|
// For dead stock, definitive IS stock > 0.
|
||||||
|
|
||||||
if ($statusFilter === 'dead') {
|
if ($statusFilter === 'dead') {
|
||||||
$ninetyDaysAgo = Carbon::now()->subDays(90);
|
$deadStockDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.dead_stock_days', 90);
|
||||||
|
$ninetyDaysAgo = Carbon::now()->subDays($deadStockDays);
|
||||||
$query->havingRaw("current_stock > 0 AND (last_sale_date < ? OR last_sale_date IS NULL)", [$ninetyDaysAgo]);
|
$query->havingRaw("current_stock > 0 AND (last_sale_date < ? OR last_sale_date IS NULL)", [$ninetyDaysAgo]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,10 +161,13 @@ class TurnoverService
|
|||||||
$lastSale = $item->last_sale_date ? Carbon::parse($item->last_sale_date) : null;
|
$lastSale = $item->last_sale_date ? Carbon::parse($item->last_sale_date) : null;
|
||||||
$daysSinceSale = $lastSale ? $lastSale->diffInDays(Carbon::now()) : 9999;
|
$daysSinceSale = $lastSale ? $lastSale->diffInDays(Carbon::now()) : 9999;
|
||||||
|
|
||||||
if ($item->current_stock > 0 && $daysSinceSale > 90) {
|
$deadStockDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.dead_stock_days', 90);
|
||||||
|
$slowMovingDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.slow_moving_days', 60);
|
||||||
|
|
||||||
|
if ($item->current_stock > 0 && $daysSinceSale > $deadStockDays) {
|
||||||
$item->status = 'dead'; // 滯銷
|
$item->status = 'dead'; // 滯銷
|
||||||
$item->status_label = '滯銷';
|
$item->status_label = '滯銷';
|
||||||
} elseif ($item->current_stock > 0 && $item->turnover_days > 60) {
|
} elseif ($item->current_stock > 0 && $item->turnover_days > $slowMovingDays) {
|
||||||
$item->status = 'slow'; // 週轉慢
|
$item->status = 'slow'; // 週轉慢
|
||||||
$item->status_label = '週轉慢';
|
$item->status_label = '週轉慢';
|
||||||
} elseif ($item->current_stock == 0) {
|
} elseif ($item->current_stock == 0) {
|
||||||
@@ -187,12 +205,18 @@ class TurnoverService
|
|||||||
// 2. Dead Stock Value (No sale in 90 days)
|
// 2. Dead Stock Value (No sale in 90 days)
|
||||||
// Need last sale date for each product-location or just product?
|
// Need last sale date for each product-location or just product?
|
||||||
// Assuming dead stock is product-level logic for simplicity.
|
// Assuming dead stock is product-level logic for simplicity.
|
||||||
|
$deadStockDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.dead_stock_days', 90);
|
||||||
$ninetyDaysAgo = Carbon::now()->subDays(90);
|
$ninetyDaysAgo = Carbon::now()->subDays($deadStockDays);
|
||||||
|
|
||||||
// Get IDs of products sold in last 90 days
|
// Get IDs of products sold in last 90 days
|
||||||
$soldProductIds = InventoryTransaction::query()
|
$soldProductIds = InventoryTransaction::query()
|
||||||
->where('type', '出庫')
|
->where('type', '出庫')
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->whereIn('reference_type', [
|
||||||
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
|
\App\Modules\Sales\Models\SalesImportBatch::class,
|
||||||
|
])->orWhereNull('reference_type');
|
||||||
|
})
|
||||||
->where('actual_time', '>=', $ninetyDaysAgo)
|
->where('actual_time', '>=', $ninetyDaysAgo)
|
||||||
->distinct()
|
->distinct()
|
||||||
->pluck('inventory_id') // Wait, transaction links to inventory, inventory links to product.
|
->pluck('inventory_id') // Wait, transaction links to inventory, inventory links to product.
|
||||||
@@ -208,6 +232,12 @@ class TurnoverService
|
|||||||
$soldProductIdsQuery = DB::table('inventory_transactions')
|
$soldProductIdsQuery = DB::table('inventory_transactions')
|
||||||
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
||||||
->where('inventory_transactions.type', '出庫')
|
->where('inventory_transactions.type', '出庫')
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->whereIn('inventory_transactions.reference_type', [
|
||||||
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
|
\App\Modules\Sales\Models\SalesImportBatch::class,
|
||||||
|
])->orWhereNull('inventory_transactions.reference_type');
|
||||||
|
})
|
||||||
->where('inventory_transactions.actual_time', '>=', $ninetyDaysAgo)
|
->where('inventory_transactions.actual_time', '>=', $ninetyDaysAgo)
|
||||||
->select('inventories.product_id')
|
->select('inventories.product_id')
|
||||||
->distinct();
|
->distinct();
|
||||||
@@ -225,17 +255,23 @@ class TurnoverService
|
|||||||
// Simplified: (Total Stock / Total Sales 30d) * 30
|
// Simplified: (Total Stock / Total Sales 30d) * 30
|
||||||
|
|
||||||
$totalStock = (clone $buildInvQuery())->sum('inventories.quantity');
|
$totalStock = (clone $buildInvQuery())->sum('inventories.quantity');
|
||||||
|
$analysisDays = \App\Modules\Core\Models\SystemSetting::getVal('turnover.analysis_period_days', 30);
|
||||||
$totalSales30d = DB::table('inventory_transactions')
|
$totalSales30d = DB::table('inventory_transactions')
|
||||||
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
->join('inventories', 'inventory_transactions.inventory_id', '=', 'inventories.id')
|
||||||
->join('products', 'inventories.product_id', '=', 'products.id')
|
->join('products', 'inventories.product_id', '=', 'products.id')
|
||||||
->where('inventory_transactions.type', '出庫')
|
->where('inventory_transactions.type', '出庫')
|
||||||
->where('inventory_transactions.actual_time', '>=', Carbon::now()->subDays(30))
|
->where(function ($q) {
|
||||||
->when($warehouseId, fn($q) => $q->where('inventories.warehouse_id', $warehouseId))
|
$q->whereIn('inventory_transactions.reference_type', [
|
||||||
->when($categoryId, fn($q) => $q->where('products.category_id', $categoryId))
|
\App\Modules\Integration\Models\SalesOrder::class,
|
||||||
->sum(DB::raw('ABS(inventory_transactions.quantity)'));
|
\App\Modules\Sales\Models\SalesImportBatch::class,
|
||||||
|
])->orWhereNull('inventory_transactions.reference_type');
|
||||||
$avgTurnoverDays = $totalSales30d > 0 ? ($totalStock * 30) / $totalSales30d : 0;
|
})
|
||||||
|
->where('inventory_transactions.actual_time', '>=', Carbon::now()->subDays($analysisDays))
|
||||||
|
->when($warehouseId, fn($q) => $q->where('inventories.warehouse_id', $warehouseId))
|
||||||
|
->when($categoryId, fn($q) => $q->where('products.category_id', $categoryId))
|
||||||
|
->sum(DB::raw('ABS(inventory_transactions.quantity)'));
|
||||||
|
|
||||||
|
$avgTurnoverDays = $totalSales30d > 0 ? ($totalStock * $analysisDays) / $totalSales30d : 0;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'total_stock_value' => $totalValue,
|
'total_stock_value' => $totalValue,
|
||||||
|
|||||||
@@ -104,4 +104,12 @@ interface ProcurementServiceInterface
|
|||||||
* 移除供貨商品關聯
|
* 移除供貨商品關聯
|
||||||
*/
|
*/
|
||||||
public function detachProductFromVendor(int $vendorId, int $productId): void;
|
public function detachProductFromVendor(int $vendorId, int $productId): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 整批同步供貨商品
|
||||||
|
*
|
||||||
|
* @param int $vendorId
|
||||||
|
* @param array $productsData Format: [['product_id' => 1, 'last_price' => 100], ...]
|
||||||
|
*/
|
||||||
|
public function syncVendorProducts(int $vendorId, array $productsData): void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Procurement\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Modules\Procurement\Services\ProcurementAnalysisService;
|
||||||
|
use App\Modules\Procurement\Models\Vendor;
|
||||||
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
|
class ProcurementAnalysisController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected ProcurementAnalysisService $analysisService,
|
||||||
|
protected InventoryServiceInterface $inventoryService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$filters = $request->only([
|
||||||
|
'date_from', 'date_to', 'vendor_id', 'warehouse_id',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 取得各面向數據
|
||||||
|
$kpis = $this->analysisService->getKPIs($filters);
|
||||||
|
$deliveryAnalysis = $this->analysisService->getDeliveryAnalysis($filters);
|
||||||
|
$quantityAnalysis = $this->analysisService->getQuantityAnalysis($filters);
|
||||||
|
$priceTrendAnalysis = $this->analysisService->getPriceTrendAnalysis($filters);
|
||||||
|
|
||||||
|
// 取得篩選器選項(跨模組透過 Service 取得倉庫)
|
||||||
|
$warehouses = $this->inventoryService->getAllWarehouses();
|
||||||
|
$vendors = Vendor::select('id', 'name', 'code')->orderBy('name')->get();
|
||||||
|
|
||||||
|
return Inertia::render('Procurement/Analysis/Index', [
|
||||||
|
'kpis' => $kpis,
|
||||||
|
'deliveryAnalysis' => $deliveryAnalysis,
|
||||||
|
'quantityAnalysis' => $quantityAnalysis,
|
||||||
|
'priceTrendAnalysis' => $priceTrendAnalysis,
|
||||||
|
'vendors' => $vendors,
|
||||||
|
'warehouses' => $warehouses,
|
||||||
|
'filters' => $filters,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -66,7 +66,11 @@ class PurchaseOrderController extends Controller
|
|||||||
$query->orderBy($sortField, $sortDirection);
|
$query->orderBy($sortField, $sortDirection);
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$orders = $query->paginate($perPage)->withQueryString();
|
$orders = $query->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
// 2. 手動注入倉庫與使用者資料
|
// 2. 手動注入倉庫與使用者資料
|
||||||
@@ -114,7 +118,7 @@ class PurchaseOrderController extends Controller
|
|||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
// 1. 獲取廠商(無關聯)
|
// 1. 獲取廠商(無關聯)
|
||||||
$vendors = Vendor::all();
|
$vendors = Vendor::select('id', 'name')->get();
|
||||||
|
|
||||||
// 2. 手動注入:獲取 Pivot 資料
|
// 2. 手動注入:獲取 Pivot 資料
|
||||||
$vendorIds = $vendors->pluck('id')->toArray();
|
$vendorIds = $vendors->pluck('id')->toArray();
|
||||||
@@ -185,71 +189,128 @@ class PurchaseOrderController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DB::beginTransaction();
|
// 使用 Cache Lock 防止併發時產生重複單號
|
||||||
|
$lock = \Illuminate\Support\Facades\Cache::lock('po_code_generation', 10);
|
||||||
|
|
||||||
// 生成單號:PO-YYYYMMDD-01
|
if (!$lock->get()) {
|
||||||
$today = now()->format('Ymd');
|
return back()->withErrors(['error' => '系統忙碌中,請稍後再試']);
|
||||||
$prefix = 'PO-' . $today . '-';
|
|
||||||
$lastOrder = PurchaseOrder::where('code', 'like', $prefix . '%')
|
|
||||||
->lockForUpdate() // 鎖定以避免並發衝突
|
|
||||||
->orderBy('code', 'desc')
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if ($lastOrder) {
|
|
||||||
// 取得最後 2 碼序號並加 1
|
|
||||||
$lastSequence = intval(substr($lastOrder->code, -2));
|
|
||||||
$sequence = str_pad($lastSequence + 1, 2, '0', STR_PAD_LEFT);
|
|
||||||
} else {
|
|
||||||
$sequence = '01';
|
|
||||||
}
|
|
||||||
$code = $prefix . $sequence;
|
|
||||||
|
|
||||||
$totalAmount = 0;
|
|
||||||
foreach ($validated['items'] as $item) {
|
|
||||||
$totalAmount += $item['subtotal'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 稅額計算
|
try {
|
||||||
$taxAmount = isset($validated['tax_amount']) ? $validated['tax_amount'] : round($totalAmount * 0.05, 2);
|
DB::beginTransaction();
|
||||||
$grandTotal = $totalAmount + $taxAmount;
|
|
||||||
|
|
||||||
// 確保有一個有效的使用者 ID
|
// 生成單號:PO-YYYYMMDD-01
|
||||||
$userId = auth()->id();
|
$today = now()->format('Ymd');
|
||||||
if (!$userId) {
|
$prefix = 'PO-' . $today . '-';
|
||||||
$user = $this->coreService->ensureSystemUserExists(); $userId = $user->id;
|
$lastOrder = PurchaseOrder::where('code', 'like', $prefix . '%')
|
||||||
}
|
->orderBy('code', 'desc')
|
||||||
|
->first();
|
||||||
|
|
||||||
$order = PurchaseOrder::create([
|
if ($lastOrder) {
|
||||||
'code' => $code,
|
$lastSequence = intval(substr($lastOrder->code, -2));
|
||||||
'vendor_id' => $validated['vendor_id'],
|
$sequence = str_pad($lastSequence + 1, 2, '0', STR_PAD_LEFT);
|
||||||
'warehouse_id' => $validated['warehouse_id'],
|
} else {
|
||||||
'user_id' => $userId,
|
$sequence = '01';
|
||||||
'status' => 'draft',
|
}
|
||||||
'order_date' => $validated['order_date'], // 新增
|
$code = $prefix . $sequence;
|
||||||
'expected_delivery_date' => $validated['expected_delivery_date'],
|
|
||||||
'total_amount' => $totalAmount,
|
|
||||||
'tax_amount' => $taxAmount,
|
|
||||||
'grand_total' => $grandTotal,
|
|
||||||
'remark' => $validated['remark'],
|
|
||||||
'invoice_number' => $validated['invoice_number'] ?? null,
|
|
||||||
'invoice_date' => $validated['invoice_date'] ?? null,
|
|
||||||
'invoice_amount' => $validated['invoice_amount'] ?? null,
|
|
||||||
]);
|
|
||||||
|
|
||||||
foreach ($validated['items'] as $item) {
|
$totalAmount = 0;
|
||||||
// 反算單價
|
foreach ($validated['items'] as $item) {
|
||||||
$unitPrice = $item['quantity'] > 0 ? $item['subtotal'] / $item['quantity'] : 0;
|
$totalAmount += $item['subtotal'];
|
||||||
|
}
|
||||||
|
|
||||||
$order->items()->create([
|
// 稅額計算
|
||||||
'product_id' => $item['productId'],
|
$taxAmount = isset($validated['tax_amount']) ? $validated['tax_amount'] : round($totalAmount * 0.05, 2);
|
||||||
'quantity' => $item['quantity'],
|
$grandTotal = $totalAmount + $taxAmount;
|
||||||
'unit_id' => $item['unitId'] ?? null,
|
|
||||||
'unit_price' => $unitPrice,
|
// 確保有一個有效的使用者 ID
|
||||||
'subtotal' => $item['subtotal'],
|
$userId = auth()->id();
|
||||||
|
if (!$userId) {
|
||||||
|
$user = $this->coreService->ensureSystemUserExists();
|
||||||
|
$userId = $user->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 靜默建立以抑制自動日誌(後續手動發送含品項明細的日誌)
|
||||||
|
$order = new PurchaseOrder([
|
||||||
|
'code' => $code,
|
||||||
|
'vendor_id' => $validated['vendor_id'],
|
||||||
|
'warehouse_id' => $validated['warehouse_id'],
|
||||||
|
'user_id' => $userId,
|
||||||
|
'status' => 'draft',
|
||||||
|
'order_date' => $validated['order_date'],
|
||||||
|
'expected_delivery_date' => $validated['expected_delivery_date'],
|
||||||
|
'total_amount' => $totalAmount,
|
||||||
|
'tax_amount' => $taxAmount,
|
||||||
|
'grand_total' => $grandTotal,
|
||||||
|
'remark' => $validated['remark'],
|
||||||
|
'invoice_number' => $validated['invoice_number'] ?? null,
|
||||||
|
'invoice_date' => $validated['invoice_date'] ?? null,
|
||||||
|
'invoice_amount' => $validated['invoice_amount'] ?? null,
|
||||||
]);
|
]);
|
||||||
}
|
$order->saveQuietly();
|
||||||
|
|
||||||
DB::commit();
|
// 建立品項並收集 items_diff
|
||||||
|
$diff = ['added' => [], 'removed' => [], 'updated' => []];
|
||||||
|
$productIds = collect($validated['items'])->pluck('productId')->unique()->toArray();
|
||||||
|
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
|
foreach ($validated['items'] as $item) {
|
||||||
|
// 反算單價
|
||||||
|
$unitPrice = $item['quantity'] > 0 ? $item['subtotal'] / $item['quantity'] : 0;
|
||||||
|
|
||||||
|
$itemsToInsert[] = [
|
||||||
|
'purchase_order_id' => $order->id,
|
||||||
|
'product_id' => $item['productId'],
|
||||||
|
'quantity' => $item['quantity'],
|
||||||
|
'unit_id' => $item['unitId'] ?? null,
|
||||||
|
'unit_price' => $unitPrice,
|
||||||
|
'subtotal' => $item['subtotal'],
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
|
|
||||||
|
$product = $products->get($item['productId']);
|
||||||
|
$diff['added'][] = [
|
||||||
|
'product_name' => $product?->name ?? '未知商品',
|
||||||
|
'new' => [
|
||||||
|
'quantity' => (float)$item['quantity'],
|
||||||
|
'subtotal' => (float)$item['subtotal'],
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
\App\Modules\Procurement\Models\PurchaseOrderItem::insert($itemsToInsert);
|
||||||
|
|
||||||
|
// 手動發送高品質日誌(包含品項明細)
|
||||||
|
activity()
|
||||||
|
->performedOn($order)
|
||||||
|
->causedBy($userId)
|
||||||
|
->event('created')
|
||||||
|
->withProperties([
|
||||||
|
'items_diff' => $diff,
|
||||||
|
'attributes' => [
|
||||||
|
'po_number' => $order->code,
|
||||||
|
'vendor_id' => $order->vendor_id,
|
||||||
|
'warehouse_id' => $order->warehouse_id,
|
||||||
|
'user_id' => $order->user_id,
|
||||||
|
'status' => $order->status,
|
||||||
|
'order_date' => $order->order_date,
|
||||||
|
'expected_delivery_date' => $order->expected_delivery_date,
|
||||||
|
'total_amount' => $order->total_amount,
|
||||||
|
'tax_amount' => $order->tax_amount,
|
||||||
|
'grand_total' => $order->grand_total,
|
||||||
|
'remark' => $order->remark,
|
||||||
|
'invoice_number' => $order->invoice_number,
|
||||||
|
'invoice_date' => $order->invoice_date,
|
||||||
|
'invoice_amount' => $order->invoice_amount,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->log('created');
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
} finally {
|
||||||
|
$lock->release();
|
||||||
|
}
|
||||||
|
|
||||||
return redirect()->route('purchase-orders.index')->with('success', '採購單已成功建立');
|
return redirect()->route('purchase-orders.index')->with('success', '採購單已成功建立');
|
||||||
|
|
||||||
@@ -323,7 +384,7 @@ class PurchaseOrderController extends Controller
|
|||||||
$order = PurchaseOrder::with(['items'])->findOrFail($id);
|
$order = PurchaseOrder::with(['items'])->findOrFail($id);
|
||||||
|
|
||||||
// 2. 獲取廠商與商品(與 create 邏輯一致)
|
// 2. 獲取廠商與商品(與 create 邏輯一致)
|
||||||
$vendors = Vendor::all();
|
$vendors = Vendor::select('id', 'name')->get();
|
||||||
$vendorIds = $vendors->pluck('id')->toArray();
|
$vendorIds = $vendors->pluck('id')->toArray();
|
||||||
$pivots = DB::table('product_vendor')->whereIn('vendor_id', $vendorIds)->get();
|
$pivots = DB::table('product_vendor')->whereIn('vendor_id', $vendorIds)->get();
|
||||||
$productIds = $pivots->pluck('product_id')->unique()->toArray();
|
$productIds = $pivots->pluck('product_id')->unique()->toArray();
|
||||||
@@ -412,7 +473,8 @@ class PurchaseOrderController extends Controller
|
|||||||
|
|
||||||
public function update(Request $request, $id)
|
public function update(Request $request, $id)
|
||||||
{
|
{
|
||||||
$order = PurchaseOrder::findOrFail($id);
|
// 加上 lockForUpdate() 防止併發修改
|
||||||
|
$order = PurchaseOrder::lockForUpdate()->findOrFail($id);
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'vendor_id' => 'required|exists:vendors,id',
|
'vendor_id' => 'required|exists:vendors,id',
|
||||||
@@ -516,20 +578,23 @@ class PurchaseOrderController extends Controller
|
|||||||
// 同步項目(原始邏輯)
|
// 同步項目(原始邏輯)
|
||||||
$order->items()->delete();
|
$order->items()->delete();
|
||||||
|
|
||||||
$newItemsData = [];
|
$itemsToInsert = [];
|
||||||
foreach ($validated['items'] as $item) {
|
foreach ($validated['items'] as $item) {
|
||||||
// 反算單價
|
// 反算單價
|
||||||
$unitPrice = $item['quantity'] > 0 ? $item['subtotal'] / $item['quantity'] : 0;
|
$unitPrice = $item['quantity'] > 0 ? $item['subtotal'] / $item['quantity'] : 0;
|
||||||
|
|
||||||
$newItem = $order->items()->create([
|
$itemsToInsert[] = [
|
||||||
|
'purchase_order_id' => $order->id,
|
||||||
'product_id' => $item['productId'],
|
'product_id' => $item['productId'],
|
||||||
'quantity' => $item['quantity'],
|
'quantity' => $item['quantity'],
|
||||||
'unit_id' => $item['unitId'] ?? null,
|
'unit_id' => $item['unitId'] ?? null,
|
||||||
'unit_price' => $unitPrice,
|
'unit_price' => $unitPrice,
|
||||||
'subtotal' => $item['subtotal'],
|
'subtotal' => $item['subtotal'],
|
||||||
]);
|
'created_at' => now(),
|
||||||
$newItemsData[] = $newItem;
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
\App\Modules\Procurement\Models\PurchaseOrderItem::insert($itemsToInsert);
|
||||||
|
|
||||||
// 3. 計算項目差異
|
// 3. 計算項目差異
|
||||||
$itemDiffs = [
|
$itemDiffs = [
|
||||||
@@ -605,8 +670,6 @@ class PurchaseOrderController extends Controller
|
|||||||
'snapshot' => [
|
'snapshot' => [
|
||||||
'po_number' => $order->code,
|
'po_number' => $order->code,
|
||||||
'vendor_name' => $order->vendor?->name,
|
'vendor_name' => $order->vendor?->name,
|
||||||
'warehouse_name' => $order->warehouse?->name,
|
|
||||||
'user_name' => $order->user?->name,
|
|
||||||
]
|
]
|
||||||
])
|
])
|
||||||
->log('updated');
|
->log('updated');
|
||||||
@@ -659,8 +722,6 @@ class PurchaseOrderController extends Controller
|
|||||||
'snapshot' => [
|
'snapshot' => [
|
||||||
'po_number' => $order->code,
|
'po_number' => $order->code,
|
||||||
'vendor_name' => $order->vendor?->name,
|
'vendor_name' => $order->vendor?->name,
|
||||||
'warehouse_name' => $order->warehouse?->name,
|
|
||||||
'user_name' => $order->user?->name,
|
|
||||||
]
|
]
|
||||||
])
|
])
|
||||||
->log('deleted');
|
->log('deleted');
|
||||||
|
|||||||
@@ -35,11 +35,12 @@ class PurchaseReturnController extends Controller
|
|||||||
$query->where('status', $request->status);
|
$query->where('status', $request->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
$purchaseReturns = $query->paginate(15)->withQueryString();
|
$perPage = $request->input('per_page', 15);
|
||||||
|
$purchaseReturns = $query->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
return Inertia::render('PurchaseReturn/Index', [
|
return Inertia::render('PurchaseReturn/Index', [
|
||||||
'purchaseReturns' => $purchaseReturns,
|
'purchaseReturns' => $purchaseReturns,
|
||||||
'filters' => $request->only(['search', 'status']),
|
'filters' => $request->only(['search', 'status', 'per_page']),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +48,7 @@ class PurchaseReturnController extends Controller
|
|||||||
{
|
{
|
||||||
// 取得可用的倉庫與廠商資料供前端選單使用
|
// 取得可用的倉庫與廠商資料供前端選單使用
|
||||||
$warehouses = $this->inventoryService->getAllWarehouses();
|
$warehouses = $this->inventoryService->getAllWarehouses();
|
||||||
$vendors = Vendor::all();
|
$vendors = Vendor::select('id', 'name')->get();
|
||||||
|
|
||||||
// 手動注入:獲取廠商商品 (與 PurchaseOrderController 邏輯一致)
|
// 手動注入:獲取廠商商品 (與 PurchaseOrderController 邏輯一致)
|
||||||
$vendorIds = $vendors->pluck('id')->toArray();
|
$vendorIds = $vendors->pluck('id')->toArray();
|
||||||
@@ -156,7 +157,7 @@ class PurchaseReturnController extends Controller
|
|||||||
});
|
});
|
||||||
|
|
||||||
$warehouses = $this->inventoryService->getAllWarehouses();
|
$warehouses = $this->inventoryService->getAllWarehouses();
|
||||||
$vendors = Vendor::all();
|
$vendors = Vendor::select('id', 'name')->get();
|
||||||
|
|
||||||
// 手動注入:獲取廠商商品 (與 create 邏輯一致)
|
// 手動注入:獲取廠商商品 (與 create 邏輯一致)
|
||||||
$vendorIds = $vendors->pluck('id')->toArray();
|
$vendorIds = $vendors->pluck('id')->toArray();
|
||||||
|
|||||||
@@ -48,7 +48,11 @@ class ShippingOrderController extends Controller
|
|||||||
$query->where('status', $request->status);
|
$query->where('status', $request->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
$orders = $query->orderBy('id', 'desc')->paginate($perPage)->withQueryString();
|
$orders = $query->orderBy('id', 'desc')->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
// 水和倉庫與使用者
|
// 水和倉庫與使用者
|
||||||
|
|||||||
@@ -44,7 +44,11 @@ class VendorController extends Controller
|
|||||||
$sortDirection = 'desc';
|
$sortDirection = 'desc';
|
||||||
}
|
}
|
||||||
|
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
$vendors = $query->orderBy($sortField, $sortDirection)
|
$vendors = $query->orderBy($sortField, $sortDirection)
|
||||||
->paginate($perPage)
|
->paginate($perPage)
|
||||||
|
|||||||
@@ -133,4 +133,35 @@ class VendorProductController extends Controller
|
|||||||
|
|
||||||
return redirect()->back()->with('success', '供貨商品已移除');
|
return redirect()->back()->with('success', '供貨商品已移除');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 整批同步供貨商品
|
||||||
|
*/
|
||||||
|
public function sync(Request $request, Vendor $vendor)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'products' => 'present|array',
|
||||||
|
'products.*.product_id' => 'required|exists:products,id',
|
||||||
|
'products.*.last_price' => 'nullable|numeric|min:0',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->procurementService->syncVendorProducts($vendor->id, $validated['products']);
|
||||||
|
|
||||||
|
activity()
|
||||||
|
->performedOn($vendor)
|
||||||
|
->withProperties([
|
||||||
|
'attributes' => [
|
||||||
|
'products_count' => count($validated['products']),
|
||||||
|
],
|
||||||
|
'sub_subject' => '供貨商品',
|
||||||
|
'snapshot' => [
|
||||||
|
'name' => "{$vendor->name} 的供貨清單",
|
||||||
|
'vendor_name' => $vendor->name,
|
||||||
|
]
|
||||||
|
])
|
||||||
|
->event('updated')
|
||||||
|
->log('整批更新供貨商品');
|
||||||
|
|
||||||
|
return redirect()->back()->with('success', '供貨商品已更新');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ class PurchaseOrder extends Model
|
|||||||
'tax_amount',
|
'tax_amount',
|
||||||
'grand_total',
|
'grand_total',
|
||||||
'remark',
|
'remark',
|
||||||
|
'invoice_number',
|
||||||
|
'invoice_date',
|
||||||
|
'invoice_amount',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
@@ -42,19 +45,52 @@ class PurchaseOrder extends Model
|
|||||||
|
|
||||||
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
|
||||||
{
|
{
|
||||||
$snapshot = $activity->properties['snapshot'] ?? [];
|
// 🚩 核心:轉換為陣列以避免 Indirect modification error
|
||||||
|
$properties = $activity->properties instanceof \Illuminate\Support\Collection
|
||||||
$snapshot['po_number'] = $this->code;
|
? $activity->properties->toArray()
|
||||||
|
: $activity->properties;
|
||||||
if ($this->vendor) {
|
|
||||||
$snapshot['vendor_name'] = $this->vendor->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([
|
// 1. Snapshot 快照
|
||||||
'snapshot' => $snapshot
|
$snapshot = $properties['snapshot'] ?? [];
|
||||||
]);
|
$snapshot['po_number'] = $this->code;
|
||||||
|
$snapshot['vendor_name'] = $this->vendor?->name;
|
||||||
|
// 倉庫名稱需透過服務取得(跨模組),若已在 snapshot 中則保留
|
||||||
|
if (!isset($snapshot['warehouse_name']) && $this->warehouse_id) {
|
||||||
|
$warehouse = app(\App\Modules\Inventory\Contracts\InventoryServiceInterface::class)->getWarehouse($this->warehouse_id);
|
||||||
|
$snapshot['warehouse_name'] = $warehouse?->name ?? null;
|
||||||
|
}
|
||||||
|
$properties['snapshot'] = $snapshot;
|
||||||
|
|
||||||
|
// 2. 名稱解析:自動將 attributes 與 old 中的 ID 換成人名/物名
|
||||||
|
$resolver = function (&$data) {
|
||||||
|
if (empty($data) || !is_array($data)) return;
|
||||||
|
|
||||||
|
// 使用者 ID 轉換
|
||||||
|
foreach (['user_id', 'created_by', 'updated_by'] as $f) {
|
||||||
|
if (isset($data[$f]) && is_numeric($data[$f])) {
|
||||||
|
$data[$f] = \App\Modules\Core\Models\User::find($data[$f])?->name ?? $data[$f];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 廠商 ID 轉換
|
||||||
|
if (isset($data['vendor_id']) && is_numeric($data['vendor_id'])) {
|
||||||
|
$data['vendor_id'] = Vendor::find($data['vendor_id'])?->name ?? $data['vendor_id'];
|
||||||
|
}
|
||||||
|
// 倉庫 ID 轉換(跨模組,透過服務)
|
||||||
|
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
|
||||||
|
$warehouse = app(\App\Modules\Inventory\Contracts\InventoryServiceInterface::class)->getWarehouse($data['warehouse_id']);
|
||||||
|
$data['warehouse_id'] = $warehouse?->name ?? $data['warehouse_id'];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isset($properties['attributes'])) $resolver($properties['attributes']);
|
||||||
|
if (isset($properties['old'])) $resolver($properties['old']);
|
||||||
|
|
||||||
|
// 3. 合併 activityProperties (手動傳入的 items_diff 等)
|
||||||
|
if (!empty($this->activityProperties)) {
|
||||||
|
$properties = array_merge($properties, $this->activityProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
$activity->properties = $properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class PurchaseOrderItem extends Model
|
|||||||
'purchase_order_id',
|
'purchase_order_id',
|
||||||
'product_id',
|
'product_id',
|
||||||
'quantity',
|
'quantity',
|
||||||
|
'unit_id',
|
||||||
'unit_price',
|
'unit_price',
|
||||||
'subtotal',
|
'subtotal',
|
||||||
// 驗收欄位
|
// 驗收欄位
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Route;
|
|||||||
use App\Modules\Procurement\Controllers\VendorController;
|
use App\Modules\Procurement\Controllers\VendorController;
|
||||||
use App\Modules\Procurement\Controllers\VendorProductController;
|
use App\Modules\Procurement\Controllers\VendorProductController;
|
||||||
use App\Modules\Procurement\Controllers\PurchaseOrderController;
|
use App\Modules\Procurement\Controllers\PurchaseOrderController;
|
||||||
|
use App\Modules\Procurement\Controllers\ProcurementAnalysisController;
|
||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
// 廠商管理
|
// 廠商管理
|
||||||
@@ -16,6 +17,7 @@ Route::middleware('auth')->group(function () {
|
|||||||
|
|
||||||
// 供貨商品相關路由
|
// 供貨商品相關路由
|
||||||
Route::post('/vendors/{vendor}/products', [VendorProductController::class, 'store'])->middleware('permission:vendors.edit')->name('vendors.products.store');
|
Route::post('/vendors/{vendor}/products', [VendorProductController::class, 'store'])->middleware('permission:vendors.edit')->name('vendors.products.store');
|
||||||
|
Route::put('/vendors/{vendor}/products/sync', [VendorProductController::class, 'sync'])->middleware('permission:vendors.edit')->name('vendors.products.sync');
|
||||||
Route::put('/vendors/{vendor}/products/{product}', [VendorProductController::class, 'update'])->middleware('permission:vendors.edit')->name('vendors.products.update');
|
Route::put('/vendors/{vendor}/products/{product}', [VendorProductController::class, 'update'])->middleware('permission:vendors.edit')->name('vendors.products.update');
|
||||||
Route::delete('/vendors/{vendor}/products/{product}', [VendorProductController::class, 'destroy'])->middleware('permission:vendors.edit')->name('vendors.products.destroy');
|
Route::delete('/vendors/{vendor}/products/{product}', [VendorProductController::class, 'destroy'])->middleware('permission:vendors.edit')->name('vendors.products.destroy');
|
||||||
});
|
});
|
||||||
@@ -76,4 +78,9 @@ Route::middleware('auth')->group(function () {
|
|||||||
|
|
||||||
Route::delete('/delivery-notes/{id}', [\App\Modules\Procurement\Controllers\ShippingOrderController::class, 'destroy'])->middleware('permission:delivery_notes.delete')->name('delivery-notes.destroy');
|
Route::delete('/delivery-notes/{id}', [\App\Modules\Procurement\Controllers\ShippingOrderController::class, 'destroy'])->middleware('permission:delivery_notes.delete')->name('delivery-notes.destroy');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 採購統計分析
|
||||||
|
Route::get('/procurement/analysis', [ProcurementAnalysisController::class, 'index'])
|
||||||
|
->middleware('permission:procurement_analysis.view')
|
||||||
|
->name('procurement.analysis.index');
|
||||||
});
|
});
|
||||||
|
|||||||
280
app/Modules/Procurement/Services/ProcurementAnalysisService.php
Normal file
280
app/Modules/Procurement/Services/ProcurementAnalysisService.php
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Procurement\Services;
|
||||||
|
|
||||||
|
use App\Modules\Procurement\Models\PurchaseOrder;
|
||||||
|
use App\Modules\Procurement\Models\PurchaseOrderItem;
|
||||||
|
use App\Modules\Procurement\Models\Vendor;
|
||||||
|
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class ProcurementAnalysisService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected InventoryServiceInterface $inventoryService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取得 KPI 總覽數據
|
||||||
|
*/
|
||||||
|
public function getKPIs(array $filters): array
|
||||||
|
{
|
||||||
|
$dateFrom = $filters['date_from'] ?? Carbon::now()->subMonths(6)->format('Y-m-d');
|
||||||
|
$dateTo = $filters['date_to'] ?? Carbon::now()->format('Y-m-d');
|
||||||
|
$vendorId = $filters['vendor_id'] ?? null;
|
||||||
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
|
|
||||||
|
// 採購單基礎查詢(排除草稿與已取消)
|
||||||
|
$poQuery = PurchaseOrder::whereNotIn('status', ['draft', 'cancelled'])
|
||||||
|
->whereBetween('order_date', [$dateFrom, $dateTo]);
|
||||||
|
if ($vendorId) $poQuery->where('vendor_id', $vendorId);
|
||||||
|
if ($warehouseId) $poQuery->where('warehouse_id', $warehouseId);
|
||||||
|
|
||||||
|
$totalAmount = (clone $poQuery)->sum('grand_total') ?: (clone $poQuery)->sum('total_amount');
|
||||||
|
$totalOrders = (clone $poQuery)->count();
|
||||||
|
|
||||||
|
// 進貨單查詢
|
||||||
|
$grQuery = DB::table('goods_receipts')
|
||||||
|
->where('status', 'completed')
|
||||||
|
->whereBetween('received_date', [$dateFrom, $dateTo])
|
||||||
|
->whereNull('deleted_at');
|
||||||
|
if ($vendorId) $grQuery->where('vendor_id', $vendorId);
|
||||||
|
if ($warehouseId) $grQuery->where('warehouse_id', $warehouseId);
|
||||||
|
|
||||||
|
$totalReceipts = (clone $grQuery)->count();
|
||||||
|
|
||||||
|
// 平均交期天數(採購單→進貨單)
|
||||||
|
$deliveryStats = DB::table('purchase_orders as po')
|
||||||
|
->join('goods_receipts as gr', 'gr.purchase_order_id', '=', 'po.id')
|
||||||
|
->whereNotIn('po.status', ['draft', 'cancelled'])
|
||||||
|
->where('gr.status', 'completed')
|
||||||
|
->whereNull('gr.deleted_at')
|
||||||
|
->whereBetween('po.order_date', [$dateFrom, $dateTo]);
|
||||||
|
if ($vendorId) $deliveryStats->where('po.vendor_id', $vendorId);
|
||||||
|
if ($warehouseId) $deliveryStats->where('po.warehouse_id', $warehouseId);
|
||||||
|
|
||||||
|
$deliveryStats = $deliveryStats->selectRaw('
|
||||||
|
AVG(DATEDIFF(gr.received_date, po.order_date)) as avg_days,
|
||||||
|
COUNT(*) as total_linked,
|
||||||
|
SUM(CASE WHEN gr.received_date <= po.expected_delivery_date THEN 1 ELSE 0 END) as on_time_count
|
||||||
|
')->first();
|
||||||
|
|
||||||
|
$avgDays = round($deliveryStats->avg_days ?? 0, 1);
|
||||||
|
$onTimeRate = $deliveryStats->total_linked > 0
|
||||||
|
? round(($deliveryStats->on_time_count / $deliveryStats->total_linked) * 100, 1)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'total_amount' => (float) $totalAmount,
|
||||||
|
'total_orders' => $totalOrders,
|
||||||
|
'total_receipts' => $totalReceipts,
|
||||||
|
'avg_delivery_days' => $avgDays,
|
||||||
|
'on_time_rate' => $onTimeRate,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 廠商供貨時效分析
|
||||||
|
*/
|
||||||
|
public function getDeliveryAnalysis(array $filters): array
|
||||||
|
{
|
||||||
|
$dateFrom = $filters['date_from'] ?? Carbon::now()->subMonths(6)->format('Y-m-d');
|
||||||
|
$dateTo = $filters['date_to'] ?? Carbon::now()->format('Y-m-d');
|
||||||
|
$vendorId = $filters['vendor_id'] ?? null;
|
||||||
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
|
|
||||||
|
$query = DB::table('purchase_orders as po')
|
||||||
|
->join('goods_receipts as gr', 'gr.purchase_order_id', '=', 'po.id')
|
||||||
|
->whereNotIn('po.status', ['draft', 'cancelled'])
|
||||||
|
->where('gr.status', 'completed')
|
||||||
|
->whereNull('gr.deleted_at')
|
||||||
|
->whereBetween('po.order_date', [$dateFrom, $dateTo]);
|
||||||
|
if ($vendorId) $query->where('po.vendor_id', $vendorId);
|
||||||
|
if ($warehouseId) $query->where('po.warehouse_id', $warehouseId);
|
||||||
|
|
||||||
|
// 按廠商分組的交期統計
|
||||||
|
$vendorDelivery = (clone $query)
|
||||||
|
->join('vendors', 'vendors.id', '=', 'po.vendor_id')
|
||||||
|
->groupBy('po.vendor_id', 'vendors.name')
|
||||||
|
->selectRaw('
|
||||||
|
po.vendor_id,
|
||||||
|
vendors.name as vendor_name,
|
||||||
|
COUNT(*) as total_count,
|
||||||
|
ROUND(AVG(DATEDIFF(gr.received_date, po.order_date)), 1) as avg_days,
|
||||||
|
MIN(DATEDIFF(gr.received_date, po.order_date)) as min_days,
|
||||||
|
MAX(DATEDIFF(gr.received_date, po.order_date)) as max_days,
|
||||||
|
SUM(CASE WHEN gr.received_date <= po.expected_delivery_date THEN 1 ELSE 0 END) as on_time_count
|
||||||
|
')
|
||||||
|
->orderBy('avg_days', 'asc')
|
||||||
|
->get()
|
||||||
|
->map(function ($row) {
|
||||||
|
$row->on_time_rate = $row->total_count > 0
|
||||||
|
? round(($row->on_time_count / $row->total_count) * 100, 1) : 0;
|
||||||
|
return $row;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 延遲分佈統計
|
||||||
|
$delayDistribution = (clone $query)
|
||||||
|
->selectRaw("
|
||||||
|
CASE
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) <= 0 THEN '提前到貨'
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) BETWEEN 1 AND 3 THEN '1-3天'
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) BETWEEN 4 AND 7 THEN '4-7天'
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) BETWEEN 8 AND 14 THEN '8-14天'
|
||||||
|
ELSE '超過14天'
|
||||||
|
END as category,
|
||||||
|
COUNT(*) as count
|
||||||
|
")
|
||||||
|
->groupByRaw("
|
||||||
|
CASE
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) <= 0 THEN '提前到貨'
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) BETWEEN 1 AND 3 THEN '1-3天'
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) BETWEEN 4 AND 7 THEN '4-7天'
|
||||||
|
WHEN DATEDIFF(gr.received_date, po.order_date) BETWEEN 8 AND 14 THEN '8-14天'
|
||||||
|
ELSE '超過14天'
|
||||||
|
END
|
||||||
|
")
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'vendor_delivery' => $vendorDelivery,
|
||||||
|
'delay_distribution' => $delayDistribution,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 進貨數量分析
|
||||||
|
*/
|
||||||
|
public function getQuantityAnalysis(array $filters): array
|
||||||
|
{
|
||||||
|
$dateFrom = $filters['date_from'] ?? Carbon::now()->subMonths(6)->format('Y-m-d');
|
||||||
|
$dateTo = $filters['date_to'] ?? Carbon::now()->format('Y-m-d');
|
||||||
|
$vendorId = $filters['vendor_id'] ?? null;
|
||||||
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
|
|
||||||
|
$baseQuery = DB::table('goods_receipts as gr')
|
||||||
|
->join('goods_receipt_items as gri', 'gri.goods_receipt_id', '=', 'gr.id')
|
||||||
|
->where('gr.status', 'completed')
|
||||||
|
->whereNull('gr.deleted_at')
|
||||||
|
->whereBetween('gr.received_date', [$dateFrom, $dateTo]);
|
||||||
|
if ($vendorId) $baseQuery->where('gr.vendor_id', $vendorId);
|
||||||
|
if ($warehouseId) $baseQuery->where('gr.warehouse_id', $warehouseId);
|
||||||
|
|
||||||
|
// 月度進貨量趨勢
|
||||||
|
$monthlyTrend = (clone $baseQuery)
|
||||||
|
->selectRaw("
|
||||||
|
DATE_FORMAT(gr.received_date, '%Y-%m') as month,
|
||||||
|
ROUND(SUM(gri.quantity_received), 2) as total_quantity,
|
||||||
|
ROUND(SUM(gri.total_amount), 2) as total_amount,
|
||||||
|
COUNT(DISTINCT gr.id) as receipt_count
|
||||||
|
")
|
||||||
|
->groupByRaw("DATE_FORMAT(gr.received_date, '%Y-%m')")
|
||||||
|
->orderBy('month', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
// 廠商佔比(按進貨金額)
|
||||||
|
$vendorShare = (clone $baseQuery)
|
||||||
|
->join('vendors', 'vendors.id', '=', 'gr.vendor_id')
|
||||||
|
->selectRaw('
|
||||||
|
gr.vendor_id,
|
||||||
|
vendors.name as vendor_name,
|
||||||
|
ROUND(SUM(gri.total_amount), 2) as total_amount,
|
||||||
|
ROUND(SUM(gri.quantity_received), 2) as total_quantity
|
||||||
|
')
|
||||||
|
->groupBy('gr.vendor_id', 'vendors.name')
|
||||||
|
->orderByDesc('total_amount')
|
||||||
|
->limit(10)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
// 商品進貨排行 Top 10
|
||||||
|
$productRanking = (clone $baseQuery)
|
||||||
|
->join('products', 'products.id', '=', 'gri.product_id')
|
||||||
|
->selectRaw('
|
||||||
|
gri.product_id,
|
||||||
|
products.name as product_name,
|
||||||
|
products.code as product_code,
|
||||||
|
ROUND(SUM(gri.quantity_received), 2) as total_quantity,
|
||||||
|
ROUND(SUM(gri.total_amount), 2) as total_amount
|
||||||
|
')
|
||||||
|
->groupBy('gri.product_id', 'products.name', 'products.code')
|
||||||
|
->orderByDesc('total_amount')
|
||||||
|
->limit(10)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'monthly_trend' => $monthlyTrend,
|
||||||
|
'vendor_share' => $vendorShare,
|
||||||
|
'product_ranking' => $productRanking,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 單價趨勢分析
|
||||||
|
*/
|
||||||
|
public function getPriceTrendAnalysis(array $filters): array
|
||||||
|
{
|
||||||
|
$dateFrom = $filters['date_from'] ?? Carbon::now()->subMonths(6)->format('Y-m-d');
|
||||||
|
$dateTo = $filters['date_to'] ?? Carbon::now()->format('Y-m-d');
|
||||||
|
$vendorId = $filters['vendor_id'] ?? null;
|
||||||
|
$warehouseId = $filters['warehouse_id'] ?? null;
|
||||||
|
|
||||||
|
$baseQuery = DB::table('goods_receipts as gr')
|
||||||
|
->join('goods_receipt_items as gri', 'gri.goods_receipt_id', '=', 'gr.id')
|
||||||
|
->join('products', 'products.id', '=', 'gri.product_id')
|
||||||
|
->where('gr.status', 'completed')
|
||||||
|
->whereNull('gr.deleted_at')
|
||||||
|
->whereBetween('gr.received_date', [$dateFrom, $dateTo]);
|
||||||
|
if ($vendorId) $baseQuery->where('gr.vendor_id', $vendorId);
|
||||||
|
if ($warehouseId) $baseQuery->where('gr.warehouse_id', $warehouseId);
|
||||||
|
|
||||||
|
// 商品月平均單價趨勢(取進貨金額 Top 10 的商品)
|
||||||
|
$topProductIds = (clone $baseQuery)
|
||||||
|
->selectRaw('gri.product_id, SUM(gri.total_amount) as total')
|
||||||
|
->groupBy('gri.product_id')
|
||||||
|
->orderByDesc('total')
|
||||||
|
->limit(10)
|
||||||
|
->pluck('product_id');
|
||||||
|
|
||||||
|
$priceTrend = [];
|
||||||
|
if ($topProductIds->isNotEmpty()) {
|
||||||
|
$priceTrend = (clone $baseQuery)
|
||||||
|
->whereIn('gri.product_id', $topProductIds->toArray())
|
||||||
|
->selectRaw("
|
||||||
|
gri.product_id,
|
||||||
|
products.name as product_name,
|
||||||
|
DATE_FORMAT(gr.received_date, '%Y-%m') as month,
|
||||||
|
ROUND(AVG(gri.unit_price), 2) as avg_price,
|
||||||
|
ROUND(MIN(gri.unit_price), 2) as min_price,
|
||||||
|
ROUND(MAX(gri.unit_price), 2) as max_price
|
||||||
|
")
|
||||||
|
->groupBy('gri.product_id', 'products.name', DB::raw("DATE_FORMAT(gr.received_date, '%Y-%m')"))
|
||||||
|
->orderBy('month', 'asc')
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 跨廠商比價(同一商品不同廠商的最近價格)
|
||||||
|
$vendorComparison = (clone $baseQuery)
|
||||||
|
->join('vendors', 'vendors.id', '=', 'gr.vendor_id')
|
||||||
|
->whereIn('gri.product_id', $topProductIds->toArray())
|
||||||
|
->selectRaw('
|
||||||
|
gri.product_id,
|
||||||
|
products.name as product_name,
|
||||||
|
gr.vendor_id,
|
||||||
|
vendors.name as vendor_name,
|
||||||
|
ROUND(AVG(gri.unit_price), 2) as avg_price,
|
||||||
|
ROUND(MIN(gri.unit_price), 2) as min_price,
|
||||||
|
ROUND(MAX(gri.unit_price), 2) as max_price,
|
||||||
|
COUNT(*) as purchase_count
|
||||||
|
')
|
||||||
|
->groupBy('gri.product_id', 'products.name', 'gr.vendor_id', 'vendors.name')
|
||||||
|
->orderBy('products.name')
|
||||||
|
->orderBy('avg_price')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'price_trend' => $priceTrend,
|
||||||
|
'vendor_comparison' => $vendorComparison,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -147,4 +147,48 @@ class ProcurementService implements ProcurementServiceInterface
|
|||||||
->where('product_id', $productId)
|
->where('product_id', $productId)
|
||||||
->delete();
|
->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function syncVendorProducts(int $vendorId, array $productsData): void
|
||||||
|
{
|
||||||
|
\Illuminate\Support\Facades\DB::transaction(function () use ($vendorId, $productsData) {
|
||||||
|
$existingPivots = \Illuminate\Support\Facades\DB::table('product_vendor')
|
||||||
|
->where('vendor_id', $vendorId)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$existingProductIds = $existingPivots->pluck('product_id')->toArray();
|
||||||
|
$newProductIds = array_column($productsData, 'product_id');
|
||||||
|
|
||||||
|
$toDelete = array_diff($existingProductIds, $newProductIds);
|
||||||
|
|
||||||
|
if (!empty($toDelete)) {
|
||||||
|
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||||
|
->where('vendor_id', $vendorId)
|
||||||
|
->whereIn('product_id', $toDelete)
|
||||||
|
->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($productsData as $data) {
|
||||||
|
$exists = in_array($data['product_id'], $existingProductIds);
|
||||||
|
|
||||||
|
if ($exists) {
|
||||||
|
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||||
|
->where('vendor_id', $vendorId)
|
||||||
|
->where('product_id', $data['product_id'])
|
||||||
|
->update([
|
||||||
|
'last_price' => $data['last_price'] ?? null,
|
||||||
|
'updated_at' => now(),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||||
|
->insert([
|
||||||
|
'vendor_id' => $vendorId,
|
||||||
|
'product_id' => $data['product_id'],
|
||||||
|
'last_price' => $data['last_price'] ?? null,
|
||||||
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,20 +33,23 @@ class PurchaseReturnService
|
|||||||
|
|
||||||
$purchaseReturn = PurchaseReturn::create($data);
|
$purchaseReturn = PurchaseReturn::create($data);
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($data['items'] as $itemData) {
|
foreach ($data['items'] as $itemData) {
|
||||||
$amount = $itemData['quantity_returned'] * $itemData['unit_price'];
|
$amount = $itemData['quantity_returned'] * $itemData['unit_price'];
|
||||||
$totalAmount += $amount;
|
$totalAmount += $amount;
|
||||||
|
|
||||||
$prItem = new PurchaseReturnItem([
|
$itemsToInsert[] = [
|
||||||
|
'purchase_return_id' => $purchaseReturn->id,
|
||||||
'product_id' => $itemData['product_id'],
|
'product_id' => $itemData['product_id'],
|
||||||
'quantity_returned' => $itemData['quantity_returned'],
|
'quantity_returned' => $itemData['quantity_returned'],
|
||||||
'unit_price' => $itemData['unit_price'],
|
'unit_price' => $itemData['unit_price'],
|
||||||
'total_amount' => $amount,
|
'total_amount' => $amount,
|
||||||
'batch_number' => $itemData['batch_number'] ?? null,
|
'batch_number' => $itemData['batch_number'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
$purchaseReturn->items()->save($prItem);
|
];
|
||||||
}
|
}
|
||||||
|
PurchaseReturnItem::insert($itemsToInsert);
|
||||||
|
|
||||||
// 更新總計 (這裡假定不含額外稅金邏輯,或是由前端帶入 tax_amount)
|
// 更新總計 (這裡假定不含額外稅金邏輯,或是由前端帶入 tax_amount)
|
||||||
$taxAmount = $data['tax_amount'] ?? 0;
|
$taxAmount = $data['tax_amount'] ?? 0;
|
||||||
@@ -87,19 +90,23 @@ class PurchaseReturnService
|
|||||||
$purchaseReturn->items()->delete();
|
$purchaseReturn->items()->delete();
|
||||||
$totalAmount = 0;
|
$totalAmount = 0;
|
||||||
|
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($data['items'] as $itemData) {
|
foreach ($data['items'] as $itemData) {
|
||||||
$amount = $itemData['quantity_returned'] * $itemData['unit_price'];
|
$amount = $itemData['quantity_returned'] * $itemData['unit_price'];
|
||||||
$totalAmount += $amount;
|
$totalAmount += $amount;
|
||||||
|
|
||||||
$prItem = new PurchaseReturnItem([
|
$itemsToInsert[] = [
|
||||||
|
'purchase_return_id' => $purchaseReturn->id,
|
||||||
'product_id' => $itemData['product_id'],
|
'product_id' => $itemData['product_id'],
|
||||||
'quantity_returned' => $itemData['quantity_returned'],
|
'quantity_returned' => $itemData['quantity_returned'],
|
||||||
'unit_price' => $itemData['unit_price'],
|
'unit_price' => $itemData['unit_price'],
|
||||||
'total_amount' => $amount,
|
'total_amount' => $amount,
|
||||||
'batch_number' => $itemData['batch_number'] ?? null,
|
'batch_number' => $itemData['batch_number'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
$purchaseReturn->items()->save($prItem);
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
PurchaseReturnItem::insert($itemsToInsert);
|
||||||
|
|
||||||
$taxAmount = $purchaseReturn->tax_amount;
|
$taxAmount = $purchaseReturn->tax_amount;
|
||||||
$purchaseReturn->update([
|
$purchaseReturn->update([
|
||||||
@@ -117,11 +124,14 @@ class PurchaseReturnService
|
|||||||
*/
|
*/
|
||||||
public function submit(PurchaseReturn $purchaseReturn)
|
public function submit(PurchaseReturn $purchaseReturn)
|
||||||
{
|
{
|
||||||
if ($purchaseReturn->status !== PurchaseReturn::STATUS_DRAFT) {
|
|
||||||
throw new Exception('只有草稿狀態的退回單可以提交。');
|
|
||||||
}
|
|
||||||
|
|
||||||
return DB::transaction(function () use ($purchaseReturn) {
|
return DB::transaction(function () use ($purchaseReturn) {
|
||||||
|
// 加上 lockForUpdate() 防止併發提交
|
||||||
|
$purchaseReturn = PurchaseReturn::lockForUpdate()->find($purchaseReturn->id);
|
||||||
|
|
||||||
|
if ($purchaseReturn->status !== PurchaseReturn::STATUS_DRAFT) {
|
||||||
|
throw new Exception('只有草稿狀態的退回單可以提交。');
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 儲存狀態,避免觸發自動修改紀錄 (合併行為)
|
// 1. 儲存狀態,避免觸發自動修改紀錄 (合併行為)
|
||||||
$purchaseReturn->status = PurchaseReturn::STATUS_COMPLETED;
|
$purchaseReturn->status = PurchaseReturn::STATUS_COMPLETED;
|
||||||
$purchaseReturn->saveQuietly();
|
$purchaseReturn->saveQuietly();
|
||||||
|
|||||||
@@ -5,4 +5,21 @@ namespace App\Modules\Production\Contracts;
|
|||||||
interface ProductionServiceInterface
|
interface ProductionServiceInterface
|
||||||
{
|
{
|
||||||
public function getPendingProductionCount(): int;
|
public function getPendingProductionCount(): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尋找產出特定批號的生產工單
|
||||||
|
*
|
||||||
|
* @param string $batchNumber
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
|
*/
|
||||||
|
public function getProductionOrdersByOutputBatch(string $batchNumber): \Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尋找使用了特定庫存批號的生產工單項目
|
||||||
|
*
|
||||||
|
* @param int $inventoryId
|
||||||
|
* @param array $with
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
|
*/
|
||||||
|
public function getProductionOrderItemsByInventoryId(int $inventoryId, array $with = []): \Illuminate\Support\Collection;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,22 +62,51 @@ class ProductionOrderController extends Controller
|
|||||||
$query->orderBy($request->input('sort_field', 'created_at'), $request->input('sort_direction', 'desc'));
|
$query->orderBy($request->input('sort_field', 'created_at'), $request->input('sort_direction', 'desc'));
|
||||||
|
|
||||||
// 分頁
|
// 分頁
|
||||||
$perPage = $request->input('per_page', 10);
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
$productionOrders = $query->paginate($perPage)->withQueryString();
|
$perPage = $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array((int)$perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
$productionOrders = $query->with('items')->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
// --- 手動資料水和 (Manual Hydration) ---
|
// --- 手動資料水和 (Manual Hydration) ---
|
||||||
$productIds = $productionOrders->pluck('product_id')->unique()->filter()->toArray();
|
$productIds = collect();
|
||||||
$warehouseIds = $productionOrders->pluck('warehouse_id')->unique()->filter()->toArray();
|
$warehouseIds = collect();
|
||||||
$userIds = $productionOrders->pluck('user_id')->unique()->filter()->toArray();
|
$userIds = collect();
|
||||||
|
$inventoryIds = collect();
|
||||||
|
|
||||||
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
|
foreach ($productionOrders as $order) {
|
||||||
$warehouses = $this->inventoryService->getAllWarehouses()->whereIn('id', $warehouseIds)->keyBy('id');
|
$productIds->push($order->product_id);
|
||||||
$users = $this->coreService->getUsersByIds($userIds)->keyBy('id');
|
$warehouseIds->push($order->warehouse_id);
|
||||||
|
$userIds->push($order->user_id);
|
||||||
|
if ($order->items) {
|
||||||
|
$inventoryIds = $inventoryIds->merge($order->items->pluck('inventory_id'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$productionOrders->getCollection()->transform(function ($order) use ($products, $warehouses, $users) {
|
$products = $this->inventoryService->getProductsByIds($productIds->unique()->filter()->toArray())->keyBy('id');
|
||||||
|
$warehouses = $this->inventoryService->getAllWarehouses()->whereIn('id', $warehouseIds->unique()->filter()->toArray())->keyBy('id');
|
||||||
|
$users = $this->coreService->getUsersByIds($userIds->unique()->filter()->toArray())->keyBy('id');
|
||||||
|
$inventories = $this->inventoryService->getInventoriesByIds($inventoryIds->unique()->filter()->toArray())->keyBy('id');
|
||||||
|
|
||||||
|
$productionOrders->getCollection()->transform(function ($order) use ($products, $warehouses, $users, $inventories) {
|
||||||
$order->product = $products->get($order->product_id);
|
$order->product = $products->get($order->product_id);
|
||||||
$order->warehouse = $warehouses->get($order->warehouse_id);
|
$order->warehouse = $warehouses->get($order->warehouse_id);
|
||||||
$order->user = $users->get($order->user_id);
|
$order->user = $users->get($order->user_id);
|
||||||
|
|
||||||
|
$totalCost = 0;
|
||||||
|
if ($order->items) {
|
||||||
|
foreach ($order->items as $item) {
|
||||||
|
$inventory = $inventories->get($item->inventory_id);
|
||||||
|
if ($inventory) {
|
||||||
|
$totalCost += $item->quantity_used * ($inventory->unit_cost ?? 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$order->estimated_total_cost = $totalCost;
|
||||||
|
$order->estimated_unit_cost = $order->output_quantity > 0 ? $totalCost / $order->output_quantity : 0;
|
||||||
|
unset($order->items);
|
||||||
|
|
||||||
return $order;
|
return $order;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -105,15 +134,15 @@ class ProductionOrderController extends Controller
|
|||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$status = $request->input('status', 'draft');
|
$status = $request->input('status', 'draft');
|
||||||
|
|
||||||
$rules = [
|
$rules = [
|
||||||
'product_id' => 'required',
|
'product_id' => 'required',
|
||||||
'status' => 'nullable|in:draft,completed',
|
'status' => 'nullable|in:draft,pending,completed',
|
||||||
'warehouse_id' => $status === 'completed' ? 'required' : 'nullable',
|
'warehouse_id' => 'required',
|
||||||
'output_quantity' => $status === 'completed' ? 'required|numeric|min:0.01' : 'nullable|numeric',
|
'output_quantity' => 'required|numeric|min:0.01',
|
||||||
'items' => 'nullable|array',
|
'items' => 'nullable|array',
|
||||||
'items.*.inventory_id' => $status === 'completed' ? 'required' : 'nullable',
|
'items.*.inventory_id' => 'required',
|
||||||
'items.*.quantity_used' => $status === 'completed' ? 'required|numeric|min:0.0001' : 'nullable|numeric',
|
'items.*.quantity_used' => 'required|numeric|min:0.0001',
|
||||||
];
|
];
|
||||||
|
|
||||||
$validated = $request->validate($rules);
|
$validated = $request->validate($rules);
|
||||||
@@ -130,7 +159,7 @@ class ProductionOrderController extends Controller
|
|||||||
'production_date' => $request->production_date,
|
'production_date' => $request->production_date,
|
||||||
'expiry_date' => $request->expiry_date,
|
'expiry_date' => $request->expiry_date,
|
||||||
'user_id' => auth()->id(),
|
'user_id' => auth()->id(),
|
||||||
'status' => ProductionOrder::STATUS_DRAFT, // 一律存為草稿
|
'status' => $status ?: ProductionOrder::STATUS_DRAFT,
|
||||||
'remark' => $request->remark,
|
'remark' => $request->remark,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -141,14 +170,18 @@ class ProductionOrderController extends Controller
|
|||||||
|
|
||||||
// 2. 處理明細
|
// 2. 處理明細
|
||||||
if (!empty($request->items)) {
|
if (!empty($request->items)) {
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($request->items as $item) {
|
foreach ($request->items as $item) {
|
||||||
ProductionOrderItem::create([
|
$itemsToInsert[] = [
|
||||||
'production_order_id' => $productionOrder->id,
|
'production_order_id' => $productionOrder->id,
|
||||||
'inventory_id' => $item['inventory_id'],
|
'inventory_id' => $item['inventory_id'],
|
||||||
'quantity_used' => $item['quantity_used'] ?? 0,
|
'quantity_used' => $item['quantity_used'] ?? 0,
|
||||||
'unit_id' => $item['unit_id'] ?? null,
|
'unit_id' => $item['unit_id'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
ProductionOrderItem::insert($itemsToInsert);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -227,7 +260,9 @@ class ProductionOrderController extends Controller
|
|||||||
'unit_name' => $inv->product->baseUnit->name ?? '',
|
'unit_name' => $inv->product->baseUnit->name ?? '',
|
||||||
'base_unit_id' => $inv->product->base_unit_id ?? null,
|
'base_unit_id' => $inv->product->base_unit_id ?? null,
|
||||||
'large_unit_id' => $inv->product->large_unit_id ?? null,
|
'large_unit_id' => $inv->product->large_unit_id ?? null,
|
||||||
|
'purchase_unit_id' => $inv->product->purchase_unit_id ?? null,
|
||||||
'conversion_rate' => $inv->product->conversion_rate ?? 1,
|
'conversion_rate' => $inv->product->conversion_rate ?? 1,
|
||||||
|
'unit_cost' => (float) $inv->unit_cost,
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -254,7 +289,10 @@ class ProductionOrderController extends Controller
|
|||||||
'expiry_date' => $inv->expiry_date ? $inv->expiry_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 ?? '',
|
'unit_name' => $inv->product->baseUnit->name ?? '',
|
||||||
'base_unit_id' => $inv->product->base_unit_id ?? null,
|
'base_unit_id' => $inv->product->base_unit_id ?? null,
|
||||||
|
'large_unit_id' => $inv->product->large_unit_id ?? null,
|
||||||
|
'purchase_unit_id' => $inv->product->purchase_unit_id ?? null,
|
||||||
'conversion_rate' => $inv->product->conversion_rate ?? 1,
|
'conversion_rate' => $inv->product->conversion_rate ?? 1,
|
||||||
|
'unit_cost' => (float) $inv->unit_cost,
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -346,14 +384,18 @@ class ProductionOrderController extends Controller
|
|||||||
$productionOrder->items()->delete();
|
$productionOrder->items()->delete();
|
||||||
|
|
||||||
if (!empty($request->items)) {
|
if (!empty($request->items)) {
|
||||||
|
$itemsToInsert = [];
|
||||||
foreach ($request->items as $item) {
|
foreach ($request->items as $item) {
|
||||||
ProductionOrderItem::create([
|
$itemsToInsert[] = [
|
||||||
'production_order_id' => $productionOrder->id,
|
'production_order_id' => $productionOrder->id,
|
||||||
'inventory_id' => $item['inventory_id'],
|
'inventory_id' => $item['inventory_id'],
|
||||||
'quantity_used' => $item['quantity_used'] ?? 0,
|
'quantity_used' => $item['quantity_used'] ?? 0,
|
||||||
'unit_id' => $item['unit_id'] ?? null,
|
'unit_id' => $item['unit_id'] ?? null,
|
||||||
]);
|
'created_at' => now(),
|
||||||
|
'updated_at' => now(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
ProductionOrderItem::insert($itemsToInsert);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -372,9 +414,30 @@ class ProductionOrderController extends Controller
|
|||||||
return response()->json(['error' => '不合法的狀態轉移或權限不足'], 403);
|
return response()->json(['error' => '不合法的狀態轉移或權限不足'], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 送審前的資料完整性驗證
|
||||||
|
if ($productionOrder->status === ProductionOrder::STATUS_DRAFT && $newStatus === ProductionOrder::STATUS_PENDING) {
|
||||||
|
if (!$productionOrder->output_quantity || $productionOrder->output_quantity <= 0) {
|
||||||
|
return back()->with('error', '送審工單前,必須先編輯填寫「生產數量」');
|
||||||
|
}
|
||||||
|
if (!$productionOrder->warehouse_id) {
|
||||||
|
return back()->with('error', '送審工單前,必須先編輯選擇「預計入庫倉庫」');
|
||||||
|
}
|
||||||
|
if ($productionOrder->items()->count() === 0) {
|
||||||
|
return back()->with('error', '送審工單前,請至少新增一項原物料明細');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DB::transaction(function () use ($newStatus, $productionOrder, $request) {
|
DB::transaction(function () use ($newStatus, $productionOrder, $request) {
|
||||||
|
// 使用鎖定重新獲取單據,防止併發狀態修改
|
||||||
|
$productionOrder = ProductionOrder::where('id', $productionOrder->id)->lockForUpdate()->first();
|
||||||
|
|
||||||
$oldStatus = $productionOrder->status;
|
$oldStatus = $productionOrder->status;
|
||||||
|
|
||||||
|
// 再次檢查狀態轉移(在鎖定後)
|
||||||
|
if (!$productionOrder->canTransitionTo($newStatus)) {
|
||||||
|
throw new \Exception('不合法的狀態轉移或權限不足');
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 執行特定狀態的業務邏輯
|
// 1. 執行特定狀態的業務邏輯
|
||||||
if ($oldStatus === ProductionOrder::STATUS_APPROVED && $newStatus === ProductionOrder::STATUS_IN_PROGRESS) {
|
if ($oldStatus === ProductionOrder::STATUS_APPROVED && $newStatus === ProductionOrder::STATUS_IN_PROGRESS) {
|
||||||
// 開始製作 -> 扣除原料庫存
|
// 開始製作 -> 扣除原料庫存
|
||||||
@@ -394,6 +457,8 @@ class ProductionOrderController extends Controller
|
|||||||
$warehouseId = $request->input('warehouse_id'); // 由前端 Modal 傳來
|
$warehouseId = $request->input('warehouse_id'); // 由前端 Modal 傳來
|
||||||
$batchNumber = $request->input('output_batch_number'); // 由前端 Modal 傳來
|
$batchNumber = $request->input('output_batch_number'); // 由前端 Modal 傳來
|
||||||
$expiryDate = $request->input('expiry_date'); // 由前端 Modal 傳來
|
$expiryDate = $request->input('expiry_date'); // 由前端 Modal 傳來
|
||||||
|
$actualOutputQuantity = $request->input('actual_output_quantity'); // 實際產出數量
|
||||||
|
$lossReason = $request->input('loss_reason'); // 耗損原因
|
||||||
|
|
||||||
if (!$warehouseId) {
|
if (!$warehouseId) {
|
||||||
throw new \Exception('必須選擇入庫倉庫');
|
throw new \Exception('必須選擇入庫倉庫');
|
||||||
@@ -401,17 +466,41 @@ class ProductionOrderController extends Controller
|
|||||||
if (!$batchNumber) {
|
if (!$batchNumber) {
|
||||||
throw new \Exception('必須提供成品批號');
|
throw new \Exception('必須提供成品批號');
|
||||||
}
|
}
|
||||||
|
if (!$actualOutputQuantity || $actualOutputQuantity <= 0) {
|
||||||
|
throw new \Exception('實際產出數量必須大於 0');
|
||||||
|
}
|
||||||
|
if ($actualOutputQuantity > $productionOrder->output_quantity) {
|
||||||
|
throw new \Exception('實際產出數量不可大於預計產量');
|
||||||
|
}
|
||||||
|
|
||||||
// 更新單據資訊:批號、效期與自動記錄生產日期
|
// --- 計算原物料投入總成本 ---
|
||||||
|
$totalCost = 0;
|
||||||
|
$items = $productionOrder->items()->with('inventory')->get();
|
||||||
|
foreach ($items as $item) {
|
||||||
|
if ($item->inventory) {
|
||||||
|
$totalCost += ($item->quantity_used * ($item->inventory->unit_cost ?? 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 單位成本以「實際產出數量」為分母,反映真實生產效率
|
||||||
|
$unitCost = $actualOutputQuantity > 0
|
||||||
|
? $totalCost / $actualOutputQuantity
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
// 更新單據資訊:批號、效期、實際產量與耗損原因
|
||||||
$productionOrder->output_batch_number = $batchNumber;
|
$productionOrder->output_batch_number = $batchNumber;
|
||||||
$productionOrder->expiry_date = $expiryDate;
|
$productionOrder->expiry_date = $expiryDate;
|
||||||
$productionOrder->production_date = now()->toDateString();
|
$productionOrder->production_date = now()->toDateString();
|
||||||
$productionOrder->warehouse_id = $warehouseId;
|
$productionOrder->warehouse_id = $warehouseId;
|
||||||
|
$productionOrder->actual_output_quantity = $actualOutputQuantity;
|
||||||
|
$productionOrder->loss_reason = $lossReason;
|
||||||
|
|
||||||
|
// 成品入庫數量改用「實際產出數量」
|
||||||
$this->inventoryService->createInventoryRecord([
|
$this->inventoryService->createInventoryRecord([
|
||||||
'warehouse_id' => $warehouseId,
|
'warehouse_id' => $warehouseId,
|
||||||
'product_id' => $productionOrder->product_id,
|
'product_id' => $productionOrder->product_id,
|
||||||
'quantity' => $productionOrder->output_quantity,
|
'quantity' => $actualOutputQuantity,
|
||||||
|
'unit_cost' => $unitCost,
|
||||||
'batch_number' => $batchNumber,
|
'batch_number' => $batchNumber,
|
||||||
'box_number' => $productionOrder->output_box_count,
|
'box_number' => $productionOrder->output_box_count,
|
||||||
'arrival_date' => now()->toDateString(),
|
'arrival_date' => now()->toDateString(),
|
||||||
|
|||||||
@@ -40,14 +40,57 @@ class RecipeController extends Controller
|
|||||||
|
|
||||||
$query->orderBy($request->input('sort_field', 'created_at'), $request->input('sort_direction', 'desc'));
|
$query->orderBy($request->input('sort_field', 'created_at'), $request->input('sort_direction', 'desc'));
|
||||||
|
|
||||||
$recipes = $query->paginate($request->input('per_page', 10))->withQueryString();
|
$defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
|
||||||
|
$perPage = (int) $request->input('per_page', $defaultPerPage);
|
||||||
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
||||||
|
$perPage = $defaultPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
$recipes = $query->with('items')->paginate($perPage)->withQueryString();
|
||||||
|
|
||||||
// Manual Hydration
|
// Manual Hydration
|
||||||
$productIds = $recipes->pluck('product_id')->unique()->filter()->toArray();
|
$productIds = collect();
|
||||||
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
|
$itemProductIds = collect();
|
||||||
|
|
||||||
|
foreach ($recipes as $recipe) {
|
||||||
|
$productIds->push($recipe->product_id);
|
||||||
|
if ($recipe->items) {
|
||||||
|
$itemProductIds = $itemProductIds->merge($recipe->items->pluck('product_id'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$allProductIds = array_unique(array_merge(
|
||||||
|
$productIds->unique()->filter()->toArray(),
|
||||||
|
$itemProductIds->unique()->filter()->toArray()
|
||||||
|
));
|
||||||
|
|
||||||
|
$products = $this->inventoryService->getProductsByIds($allProductIds)->keyBy('id');
|
||||||
|
|
||||||
$recipes->getCollection()->transform(function ($recipe) use ($products) {
|
$recipes->getCollection()->transform(function ($recipe) use ($products) {
|
||||||
$recipe->product = $products->get($recipe->product_id);
|
$recipe->product = $products->get($recipe->product_id);
|
||||||
|
|
||||||
|
$totalCost = 0;
|
||||||
|
if ($recipe->items) {
|
||||||
|
foreach ($recipe->items as $item) {
|
||||||
|
$itemProduct = $products->get($item->product_id);
|
||||||
|
if ($itemProduct) {
|
||||||
|
$baseCost = $itemProduct->cost_price ?? 0;
|
||||||
|
$conversionRate = 1;
|
||||||
|
|
||||||
|
if ($item->unit_id == $itemProduct->large_unit_id && !is_null($itemProduct->conversion_rate)) {
|
||||||
|
$conversionRate = $itemProduct->conversion_rate;
|
||||||
|
} elseif ($item->unit_id == $itemProduct->purchase_unit_id && !is_null($itemProduct->conversion_rate_purchase)) {
|
||||||
|
$conversionRate = $itemProduct->conversion_rate_purchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalCost += ($item->quantity * $baseCost * $conversionRate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$recipe->estimated_total_cost = $totalCost;
|
||||||
|
$recipe->estimated_unit_cost = $recipe->yield_quantity > 0 ? $totalCost / $recipe->yield_quantity : 0;
|
||||||
|
unset($recipe->items);
|
||||||
|
|
||||||
return $recipe;
|
return $recipe;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ class ProductionOrder extends Model
|
|||||||
'product_id',
|
'product_id',
|
||||||
'warehouse_id',
|
'warehouse_id',
|
||||||
'output_quantity',
|
'output_quantity',
|
||||||
|
'actual_output_quantity',
|
||||||
|
'loss_reason',
|
||||||
'output_batch_number',
|
'output_batch_number',
|
||||||
'output_box_count',
|
'output_box_count',
|
||||||
'production_date',
|
'production_date',
|
||||||
@@ -82,6 +84,7 @@ class ProductionOrder extends Model
|
|||||||
'production_date' => 'date',
|
'production_date' => 'date',
|
||||||
'expiry_date' => 'date',
|
'expiry_date' => 'date',
|
||||||
'output_quantity' => 'decimal:2',
|
'output_quantity' => 'decimal:2',
|
||||||
|
'actual_output_quantity' => 'decimal:2',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function getActivitylogOptions(): LogOptions
|
public function getActivitylogOptions(): LogOptions
|
||||||
@@ -91,6 +94,8 @@ class ProductionOrder extends Model
|
|||||||
'code',
|
'code',
|
||||||
'status',
|
'status',
|
||||||
'output_quantity',
|
'output_quantity',
|
||||||
|
'actual_output_quantity',
|
||||||
|
'loss_reason',
|
||||||
'output_batch_number',
|
'output_batch_number',
|
||||||
'production_date',
|
'production_date',
|
||||||
'remark'
|
'remark'
|
||||||
|
|||||||
@@ -26,4 +26,9 @@ class ProductionOrderItem extends Model
|
|||||||
{
|
{
|
||||||
return $this->belongsTo(ProductionOrder::class);
|
return $this->belongsTo(ProductionOrder::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function inventory(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Modules\Inventory\Models\Inventory::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user