260 Commits

Author SHA1 Message Date
f96d2870c3 [DOCS] 修正商品資料讀取 API 說明,移除關鍵字搜尋描述 2026-03-19 15:06:20 +08:00
96440f6b50 [DOCS] 修正商品資料讀取 API 關於關鍵字搜尋的誤導說明 2026-03-19 15:05:59 +08:00
8f6b8d55cc [FEAT] 銷售訂單管理:補齊欄位、即時搜尋、篩選與來源自動判定 2026-03-19 15:03:24 +08:00
60f5f00a9e [FEAT] 銷售訂單管理:補齊欄位、即時搜尋、篩選與來源自動判定 2026-03-19 15:00:33 +08:00
0b4aeacb55 [REFACTOR] 統一訂單同步 API 錯誤回應與修正 Linter 警告 2026-03-19 14:07:32 +08:00
e3ceedc579 [STYLE] 移除冗餘的簡報生成腳本,改由全域技能處理 2026-03-13 16:23:02 +08:00
7a1fc02dfc [REFACTOR] 移除 package.json 中不使用的 pptxgenjs 套件 2026-03-13 16:19:56 +08:00
bee8ecb55b [FIX] 修正盤調單明細插入時的欄位名稱錯誤並更新簡報/圖片處理套件 2026-03-13 16:18:13 +08:00
b57a4feeab [FIX] 嚴格限制 now-push 工作流的 main 合併鏈路
- 修改 now-push.md 確保 main 只能從 demo 合併
- 明列 dev -> demo -> main 的強制合併順序
2026-03-10 15:39:37 +08:00
6ca0bafd60 [FEAT] 新增生產工單實際產量欄位與 UI 規範
- 新增 database/migrations/tenant 實際產量與耗損原因
- ProductionOrder API 狀態推進與實際產量計算
- 完工入庫新增實際產出數量原生數字輸入框 (step=1)
- Create.tsx 補上前端資料驗證與狀態保護
- 建立並更新 UI 數字輸入框設計規範
2026-03-10 15:32:52 +08:00
adf13410ba [DOCS] 更新 API 文件,補充 api-test-01 測試倉說明
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
2026-03-10 11:57:34 +08:00
d52a215916 [FEAT] 優化庫存分析邏輯,增加銷售 Reference Type 追蹤並修正 InventoryService 閉包變數問題
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m20s
2026-03-10 11:15:55 +08:00
197df3bec4 [FIX] 修復所有 E2E 模組測試的標題定位器以及將測試帳號還原為 admin 權限
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
2026-03-09 16:53:06 +08:00
2437aa2672 [FIX] 更新技能觸發路徑:修正因技能更名導致的 broken links 並指向新的全域規範路徑。 2026-03-09 15:04:16 +08:00
a987f4345e [REFACTOR] 優化資料庫查詢效能:在多個 Service 與 Controller 中加入 select 欄位限制,並新增租戶資料表索引 Migration。 2026-03-09 14:59:37 +08:00
89291918fd [FEAT] 實作配方與生產工單自動搜尋,優化分頁 RWD,將倉庫地址改為選填並更新文件
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m22s
2026-03-09 13:48:06 +08:00
3f7a625191 [DOCS] 整理 README.md 多租戶架構說明
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m8s
2026-03-06 16:58:29 +08:00
e11193c2a7 [FEAT] 導入 Playwright E2E 測試環境與登入功能測試腳本
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m36s
2026-03-06 15:38:27 +08:00
02e5f5d4ea [DOCS] 重構 Git 發布規範:將安全性檢查規則收攏至 SKILL.md 並與 now-push 工作流解耦
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
2026-03-06 14:54:54 +08:00
36b90370a8 [FEAT] 優化會計報表:新增稅額、發票日期與付款方式等會計專用欄位並支援 CSV 完整匯出
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
2026-03-06 14:40:14 +08:00
5290dd2cbe [DOCS] 更新 AI 開發規範與 .gitignore 忽略多租戶儲存目錄
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m9s
2026-03-06 13:26:27 +08:00
8e0252e8fc [FEAT] 實作公共事業費附件上傳管理與更新 UI 協作規範防呆機制
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
2026-03-06 13:21:14 +08:00
951005c616 [DOCS] 更新開發框架規範說明書:加入 CI/CD 自動化部署與查修主機連線資訊
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
2026-03-06 11:06:27 +08:00
ee0bacafc2 [REFACTOR] 修正技能觸發規範文件檔名與內容
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m20s
2026-03-06 10:48:01 +08:00
93390aad80 docs: 調整技能觸發規範格式 2026-03-06 10:44:39 +08:00
6b6e840f35 docs: 整合與優化 Agent Skills 規範及新增技能觸發準則 2026-03-06 10:43:23 +08:00
e4c83ebd6d [DOCS] 優化 now-push 工作流:新增 main 分支從 demo 分支合併之規範提醒
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 58s
ERP-Deploy-Production / deploy-production (push) Successful in 57s
2026-03-05 16:59:46 +08:00
d9edc603c7 [FEAT] 新增專案專屬的 now-push 工作流
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m1s
2026-03-05 16:51:18 +08:00
dda92393d2 [STYLE] 調整生產環境部署腳本格式
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 57s
2026-03-05 16:32:25 +08:00
47deab9804 [FEAT] 優化公共事業費提醒信邏輯:到期前 7, 3, 0 天發信,逾期每日發信
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 57s
2026-03-05 16:23:57 +08:00
e921810f70 [FEAT] 在部署流程中加入 SystemSettingSeeder 以自動補齊系統設定資料
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
2026-03-05 16:09:15 +08:00
07b7d9b327 [FEAT] 實作公共事業費逾期提醒、租戶自訂通知設定及發送測試信功能
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
2026-03-05 16:01:00 +08:00
016366407c [FIX] 修正生產工單完成入庫時未寫入成本與總價值的問題
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
ERP-Deploy-Production / deploy-production (push) Has been cancelled
2026-03-05 13:34:02 +08:00
ba50905626 [FIX] 修正公共事業費清單日期顯示少一天的問題 2026-03-05 11:58:32 +08:00
f4ed358393 [FEAT] 實作跨倉庫及時庫存批號搜尋與 Debounce 搜尋體驗
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
2026-03-05 11:51:13 +08:00
7c395c89b5 [STYLE] 修正 git 工作流文件命名規範
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m3s
2026-03-05 11:22:32 +08:00
ed264b031a [DOCS] 優化 Git 規範:明確 dev->demo->main 鏈路與發布時段限制
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m21s
ERP-Deploy-Production / deploy-production (push) Successful in 1m11s
2026-03-05 09:02:46 +08:00
dd2e63c08b [DOCS] 移除冗餘規範文件並同步開發規則至所有分支
All checks were successful
ERP-Deploy-Production / deploy-production (push) Successful in 1m1s
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m22s
2026-03-05 08:53:41 +08:00
b498fe93ff [DOCS] 更新 Git 規範:加入新功能合併至 main 的時段限制 2026-03-05 08:51:43 +08:00
6b324b4bd0 [DOCS] 將 Git 規範整合至開發 Rules 並移除重複文件 2026-03-05 08:49:05 +08:00
a898873211 [FIX] 修正採購單大單位換算問題並建立 Git 開發規範
All checks were successful
ERP-Deploy-Production / deploy-production (push) Successful in 1m19s
2026-03-05 08:46:26 +08:00
f543b98d0f feat(inventory): 實作進貨單草稿編輯功能、價格雙向連動與批號 UI 優化
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 57s
ERP-Deploy-Production / deploy-production (push) Successful in 1m14s
1. 實作進貨單編輯功能,支援草稿資料預填與 PUT 更新。
2. 修復進貨單儲存時 received_date 與 expiry_date 的日期格式錯誤 (Y-m-d)。
3. 實作非標準進貨類型(雜項、其他)的單價與小計雙向連動邏輯。
4. 優化品項批號 UI 為 SearchableSelect 整合模式,支援不使用批號 (NO-BATCH) 與建立新批號,與倉庫管理頁面風格統一。
2026-03-03 16:57:28 +08:00
183583c739 feat: API調整訂單與販賣機訂單同步強制使用warehouse_code,更新API對接文件,及優化生產與配方模組UI顯示
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
2026-03-03 14:28:15 +08:00
58bd995cd8 feat: 新增採購統計分析功能並優化 API 文件顯示樣式
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m13s
ERP-Deploy-Production / deploy-production (push) Successful in 1m13s
- 在 RoleController 中新增 procurement_analysis 權限群組名稱
- 在 Procurement 模組中新增採購統計分析路由
- 在 PermissionSeeder 中新增 procurement_analysis.view 權限並分配給角色
- 在側邊欄「報表與分析」分組中新增「採購統計分析」項目
- 優化 API 文件視圖中的表格外觀樣式
2026-03-03 11:38:04 +08:00
036f4a4fb6 優化採購單與進貨單操作紀錄:新增品項明細、ID 轉名稱解析、前端多數量 key 通用顯示
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
ERP-Deploy-Production / deploy-production (push) Successful in 1m12s
- 重構 PurchaseOrder@tapActivity:支援 vendor_id/warehouse_id/user_id 自動解析為名稱
- 修改 PurchaseOrderController@store:改用 saveQuietly + 手動日誌,建立時紀錄品項明細
- 修正 PurchaseOrderController update/destroy snapshot 跨模組取值為 null 的問題
- 修改 GoodsReceiptService@store:改用 saveQuietly + 手動日誌,建立時紀錄品項明細
- 修改 ActivityDetailDialog.tsx:支援 quantity/quantity_received/requested_qty 多 key 通用渲染
- 新增項目顯示金額與備註,更新項目增加金額與備註變更對比
2026-03-02 17:30:55 +08:00
0a955fb993 feat: 整合門市領料日誌、API 文件存取、修改庫存與併發編號問題、供應商商品內聯編輯及日誌 UI 優化
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
2026-03-02 16:42:12 +08:00
7dac2d1f77 實作產品與庫存匯入邏輯 (ProductImport, InventoryImport) 並更新相關 Service 與 Controller
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
2026-03-02 11:58:04 +08:00
649af40919 實作 InventoryService 的批量入庫 (processIncomingInventory) 與庫存調整 (adjustInventory) 邏輯
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
2026-03-02 10:47:43 +08:00
5f8b2a1c2d 新增 POS 庫存查詢 API:實作 InventorySyncController 與相關 Service 邏輯,並更新 API 整合手冊
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m24s
2026-03-02 10:19:38 +08:00
4bbbde685d feat: 更新系統操作手冊內容並新增本地代理配置
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s
2026-02-26 13:54:35 +08:00
5e32526471 style(Frontend): 將側邊欄與麵包屑導覽的『報表管理』更名為『報表與分析』
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m16s
2026-02-26 10:44:43 +08:00
f960aaaeb2 feat(Inventory): 實作批號溯源完整功能與 UI 呈現,包含文字敘述卡片與更完整的關聯屬性 2026-02-26 10:39:24 +08:00
63e4f88a14 優化門市叫貨流程:實作庫存預扣機制、鎖定自動產生的調撥單明細、修復自動販賣機貨道數量連動 Bug 及狀態同步問題
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
2026-02-25 17:32:28 +08:00
e3df090afd feat: 統一各模組分頁組件佈局並新增系統設定功能相關檔案
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m5s
2026-02-25 16:16:49 +08:00
878b90e2ad UI: 統一各單據詳情頁面標題與基本資訊排版 2026-02-25 14:56:15 +08:00
299cf37054 fix: 修正全系統側邊欄捲軸重置問題
在所有報表與管理頁面的 router.get 調用中加入 preserveScroll: true。
受影響模組包括:
- 財務管理 (會計報表、公用事業費)
- 庫存管理 (庫存查詢、倉庫管理、進貨、調整、調撥)
- 生產管理 (工單管理、配方管理)
- 採購管理 (採購單)
- 銷售與發貨管理 (銷售單、發貨單、匯入管理)
- 系統管理 (使用者、角色、操作紀錄)
2026-02-25 14:04:22 +08:00
5668e17e61 style: 暫時隱藏採購退回單與出貨單側邊欄項目
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 52s
2026-02-25 13:51:41 +08:00
c4908533a8 feat(procurement): 實作採購退回單模組並修復商品選單報錯
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 58s
2026-02-25 13:49:02 +08:00
deef3baacc refactor: 重構模組通訊與調整儀表板功能
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 57s
- 依循跨模組通訊規範,將 Sales 與 Production 模組中對 Inventory 的直接模型關聯改為透過 InventoryServiceInterface 取得
- 於 InventoryService 實作獲取最高庫存價值、即將過期商品等方法,供儀表板使用
- 確保所有跨模組調用皆採用手動水和(Manual Hydration)方式組合資料
- 移除本地已歸檔的 .agent 規範檔案
2026-02-25 11:48:52 +08:00
ad91b08dbc refactor: 重構 VendorProduct API 與新增進貨單重複檢查前端邏輯
All checks were successful
ERP-Deploy-Production / deploy-production (push) Successful in 1m9s
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m10s
1. 將 VendorProductController 中的 Eloquent 關聯操作改為透過 ProcurementService 使用 DB 操作,解除跨模組 Model 直接依賴。
2. ProcurementService 加入 vendor product 的資料存取方法。
3. 進貨單建立前端 (GoodsReceipt/Create.tsx) 新增重複進貨檢查與警告對話框邏輯。
2026-02-25 11:11:28 +08:00
e406ecd63d feat: 實作應付帳款與銷售訂單權限管理與進貨單權限修正
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m17s
2026-02-24 17:29:09 +08:00
455f945296 feat: 完成進貨單自動拋轉應付帳款流程與AP介面優化
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m8s
1. 新增 AccountPayable (應付帳款) 模組,包含 Migration、Model、Service 與 Controller
2. 修改 GoodsReceipt (進貨單) 流程,在確認進貨時自動產生對應的應付帳款單 (AP-YYYYMMDD-XX)
3. 實作應付帳款詳細頁面 (Show.tsx),包含發票登記與標記付款功能
4. 修正應付帳款 Show 頁面的排版,將發票資訊套用標準的綠色背景區塊,並調整按鈕位置
5. 更新相關的 Service Provider 與 Routes
2026-02-24 16:46:55 +08:00
aaa93a921e feat: 實作 Demo 環境網域路由轉發,設定 ERP Proxy 統一接管 Port 80
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Has been cancelled
ERP-Deploy-Production / deploy-production (push) Successful in 1m28s
2026-02-24 09:58:21 +08:00
922b1fc877 fix: 統一容器名稱為 star-erp- 前綴,修正 Proxy 與 CI/CD 連線失敗問題
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m48s
ERP-Deploy-Production / deploy-production (push) Successful in 1m5s
2026-02-24 08:44:18 +08:00
3d4682a405 fix: 解決 Demo 環境埠號衝突並新增中央後台網域支援
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 39s
ERP-Deploy-Production / deploy-production (push) Successful in 1m21s
2026-02-24 08:42:16 +08:00
3ba6b3a1cd fix: 還原 compose.demo.yaml 的 port 映射為 80:80 + 8080:8080
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 40s
ERP-Deploy-Production / deploy-production (push) Successful in 59s
2026-02-23 17:35:37 +08:00
ec239279f4 fix: 修正 Demo 環境 port 映射,避免特權端口權限錯誤
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 29s
ERP-Deploy-Production / deploy-production (push) Successful in 1m14s
Demo 主機的 Docker 沒有綁定特權端口 (80) 的權限,
將映射從 80:80 改為 8080:80(主機 8080 → 容器 Nginx 80)。
2026-02-23 17:27:59 +08:00
e2c36e9c0f chore: 推送當前部署與配置修改到所有分支
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 25s
ERP-Deploy-Production / deploy-production (push) Successful in 1m9s
2026-02-23 17:25:40 +08:00
30bf8ef79c fix: 解決部署初期因缺少 vendor 導致容器啟動崩潰的問題
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 32s
ERP-Deploy-Production / deploy-production (push) Successful in 59s
2026-02-23 17:23:53 +08:00
590d1ea9e9 fix: 移除 compose.yaml 中重複的鍵值
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 41s
ERP-Deploy-Production / deploy-production (push) Successful in 1m4s
2026-02-23 17:20:58 +08:00
cd0f454c98 refactor: 根據環境資訊還原容器名稱並維持多檔案 Compose 結構
Some checks failed
ERP-Deploy-Production / deploy-production (push) Has been cancelled
ERP-Deploy-Demo / deploy-demo (push) Has been cancelled
2026-02-23 17:20:44 +08:00
54e1e5df5a fix: 隔離正式與 Demo 環境的容器名稱以修復 CI/CD 衝突
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 26s
2026-02-23 17:17:07 +08:00
8e3d951d0d feat: 為 demo 環境增加 80 埠口對應
Some checks failed
ERP-Deploy-Production / deploy-production (push) Successful in 57s
ERP-Deploy-Demo / deploy-demo (push) Failing after 51s
2026-02-23 17:12:43 +08:00
d04e5bbffb docs: 修正 demo-proxy.conf 中的環境註解文字
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 26s
2026-02-23 17:12:20 +08:00
27273bfee4 fix: 更新 demo-proxy.conf 以符合正式環境配置並優化 SSL 轉發
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Has been cancelled
2026-02-23 17:11:58 +08:00
2a88649f75 feat: 拆分 Docker Compose 配置為多檔案繼承模式並優化部署工作流
Some checks failed
ERP-Deploy-Production / deploy-production (push) Successful in 1m0s
ERP-Deploy-Demo / deploy-demo (push) Failing after 24s
2026-02-23 17:06:15 +08:00
e9313158ba 為了在 gitea_work (LXC) 順利部署 demo,將網路模式改為 host 並同步相關配置 2026-02-23 16:52:27 +08:00
f3da49a76a 觸發 Demo CI/CD 部署 (更新 SSH Key 後再次重試)
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 34s
2026-02-23 16:10:11 +08:00
747f70865d 修正 CI/CD 部署後 npm run build 殘留的 public/hot 導致 Vite HMR 及 CORS 報錯問題
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 24s
ERP-Deploy-Production / deploy-production (push) Successful in 58s
2026-02-23 16:02:40 +08:00
6bb2afa3b7 移除 Dockerfile 中的 setcap 以修復 LXC sysctl 權限問題,並將內部 port 改為 8080
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 25s
ERP-Deploy-Production / deploy-production (push) Successful in 53s
2026-02-23 15:58:52 +08:00
59008eb59e 更名 CI/CD 工作流名稱,移除 Koori 前綴
Some checks failed
ERP-Deploy-Demo / deploy-demo (push) Failing after 28s
ERP-Deploy-Production / deploy-production (push) Successful in 1m0s
2026-02-23 15:51:15 +08:00
a33e470e4d 拆分 CI/CD 流程:將 demo 與正式環境的部署拆分至獨立檔案
Some checks failed
Koori-ERP-Deploy-Production / deploy-production (push) Successful in 55s
Koori-ERP-Deploy-Demo / deploy-demo (push) Failing after 9m37s
2026-02-23 15:32:42 +08:00
71b676b533 修正 CI/CD deploy-production 連線埠號為 2224 (正式環境)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
2026-02-23 15:29:14 +08:00
406d03297a 再次觸發 CI/CD (修復正式機 Port 2227 上的 Docker 權限問題)
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 1m8s
2026-02-23 15:25:07 +08:00
4259c7745b 移除 deploy.yaml 結尾多餘的空白行
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 28s
2026-02-23 15:20:58 +08:00
8169ff3f59 還原 Dockerfile 與 Nginx proxy 設定至原始 port 80 配置
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 24s
2026-02-23 15:04:40 +08:00
1acc4daebb 修復正式機 sysctl 權限錯誤:移除 setcap,PHP 改用 port 8080 搭配 Nginx proxy
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 29s
2026-02-23 15:01:12 +08:00
1acbfb7246 移除 supervisord 的 npm program 區塊,修復正式機容器啟動權限錯誤
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 23s
2026-02-23 14:58:22 +08:00
e02d7c7125 chore: 微調 deploy.yaml 並準備同步至 main
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 23s
2026-02-23 14:53:12 +08:00
a133b94a05 fix(docker): 僅在 local/testing 環境執行 npm run dev,避免正式環境啟動
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 2m26s
2026-02-23 14:43:08 +08:00
acd0590a38 merge: 合併 demo 分支的 deploy.yaml 修正回 dev
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Has been cancelled
2026-02-23 14:33:28 +08:00
a2fe7b5a95 fix: 同步正式環境部署目標至 gitea_work (220.132.7.82:2227)
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Failing after 24s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-23 14:32:30 +08:00
5f1f08869f chore: deploy demo site to gitea_work
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Failing after 7m26s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-23 14:12:00 +08:00
e85c1fa95a fix: 移除不存在的 is_active 欄位引用
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
2026-02-23 13:52:59 +08:00
62dcf04e95 refactor: 調整倉庫自動建立機制,統一使用門市倉類型 (retail)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 54s
2026-02-23 13:49:36 +08:00
6dd3396fb7 fix: 修正 WarehouseType Enum 缺失 system_sales 導致正式機 500 錯誤
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 55s
2026-02-23 13:43:31 +08:00
2f30a78118 feat(integration): 實作並測試 POS 與販賣機訂單同步 API
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
主要變更:
- 實作 POS 與販賣機訂單同步邏輯,支援多租戶與 Sanctum 驗證。
- 修正多租戶識別中間件與 Sanctum 驗證順序問題。
- 切換快取驅動至 Redis 以支援 Tenancy 標籤功能。
- 新增商品同步 API (Upsert) 及相關單元測試。
- 新增手動測試腳本 tests/manual/test_integration_api.sh。
- 前端新增銷售訂單來源篩選與欄位顯示。
2026-02-23 13:27:12 +08:00
904132e460 feat(integration): 擴充產品同步 API 欄位與驗證強化
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m14s
1. ProductSync API 新增防護機制,為既有欄位加上字串長度與金額上限限制
2. 開放並接收 ERP Product Model 實用欄位(品牌、規格、成本價、會員價、批發價)
3. 更新 ProductService 寫入邏輯以支援新增的可選欄位
4. 同步更新 api-integration.md 手冊,加入新欄位說明與 JSON 範例
2026-02-23 11:02:25 +08:00
a05acd96dc feat(integration): 完善外部 API 對接邏輯與安全性
1. 新增 API Rate Limiting (每分鐘 60 次)
2. 實作 ProductServiceInterface 與 findOrCreateWarehouseByName 解決跨模組耦合問題
3. 強化 OrderSync API 驗證 (price 欄位限制最小 0、payment_method 加上允許白名單)
4. 實作 OrderSync API 冪等性處理,重複訂單直接回傳現有資訊
5. 修正 ProductSync API 同步邏輯,每次同步皆會更新產品分類與單位
6. 完善 integration API 對接手冊內容與 UI 排版
2026-02-23 10:10:03 +08:00
29cdf37b71 style: 簡化操作手冊標題為『操作指南』
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
2026-02-13 16:04:04 +08:00
d7d1be81a9 style: 強化操作手冊排版樣式鎖定,確保間距維持極簡緊湊
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m2s
2026-02-13 16:02:03 +08:00
227cfec0d2 style: 大幅壓縮操作手冊內容間距,提升資訊密度
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Has been cancelled
2026-02-13 16:01:16 +08:00
034a21cd31 style: 優化操作手冊排版間距,使其更緊湊
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Has been cancelled
2026-02-13 16:00:55 +08:00
6358e23816 fix: 修正操作手冊捲軸行為與容器高度
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 55s
2026-02-13 15:59:26 +08:00
ac149533f0 fix: 簡化 prose 類別以解決 Tailwind v4 排版失效問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m3s
2026-02-13 15:57:56 +08:00
b20a47f710 style: 優化操作手冊 Markdown 排版與 UI 佈局
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Has been cancelled
2026-02-13 15:57:23 +08:00
d017d7e5e0 fix: 修正操作手冊選單顯示邏輯並強化內容
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 54s
2026-02-13 15:55:56 +08:00
8207e6fe94 docs: 撰寫操作手冊正式內容 (新手上路、採購、庫存、FAQ)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
2026-02-13 15:54:37 +08:00
e6cf03b991 feat: 實作系統操作手冊模組 (Markdown 渲染與導覽)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 54s
2026-02-13 15:51:51 +08:00
8ef82d49cb feat(inventory): 新增庫存分析模組
- 實作 InventoryAnalysisController 與 TurnoverService
- 新增庫存分析前端頁面 (Inventory/Analysis/Index.tsx)
- 整合路由與選單
- 統一分頁邏輯與狀態顯示
- 更新 UI Consistency Skill 文件
2026-02-13 15:43:12 +08:00
bb2cf77ccb fix: 修正 deploy.yaml 重複定義錯誤並優化版本號注入腳本
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m6s
2026-02-13 15:05:08 +08:00
d543e6e810 docs: 稍微調整 README.md 結尾格式
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
2026-02-13 15:03:44 +08:00
64e039cc71 fix: 改用 YAML 模板變數直接注入 github.sha 以修復版本號為空的問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 53s
2026-02-13 15:02:12 +08:00
cce8dd3c8b fix: 修正部署工作流中的版本號變數名稱與計算方式
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m2s
2026-02-13 14:59:43 +08:00
6a0f57c86c fix: 重新格式化 deploy-demo 任務以修正 YAML 語法錯誤
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 24s
2026-02-13 14:58:02 +08:00
0251540365 fix: 修正 deploy.yaml 第 94 行縮排錯誤 2026-02-13 14:56:40 +08:00
ab5b4bde0b docs: 調整 README.md 格式 2026-02-13 14:55:38 +08:00
f85f06f3e1 fix: 修正 deploy.yaml 中的 YAML 縮排錯誤 2026-02-13 14:55:04 +08:00
6671e4221f docs: 在 .env.example 中加入 APP_VERSION 2026-02-13 14:53:33 +08:00
24f73a2585 fix: 修正部署腳本中的版本號注入邏輯 2026-02-13 14:50:10 +08:00
2e9ff6c832 feat: 實現版本號自動化更新與修復側邊欄 RWD 問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 50s
2026-02-13 14:46:26 +08:00
77a7d31dc1 fix(dashboard): 修正儀表板待處理數字邏輯與依賴更新
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 54s
2026-02-13 14:29:21 +08:00
e141a45eb9 feat(dashboard): 新增庫存積壓、熱銷數量與即將過期排行,優化熱銷商品顯示與 Tooltip
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 41s
2026-02-13 14:27:43 +08:00
4fa87925a2 UI優化: 全系統狀態標籤 (StatusBadge) 統一化重構完成 (Phase 3 & 4)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m8s
2026-02-13 13:16:05 +08:00
097708aab7 優化: 門市叫貨模組 UI 調整、權限標籤中文化及調撥單動態導覽 2026-02-13 10:39:10 +08:00
b8cbf0bb6d Merge branch 'dev'
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 51s
2026-02-12 17:13:57 +08:00
882091ce5f feat(notification): 實作通知輪詢與優化顯示名稱
- 新增通知輪詢 API 與前端自動更新機制
- 修正生產工單單號格式為 PRO-YYYYMMDD-XX
- 確保通知顯示實際建立者名稱而非系統
2026-02-12 17:13:09 +08:00
245553280a Merge branch 'dev'
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 50s
2026-02-12 16:41:08 +08:00
299602d3b1 docs: 微調 README 格式 2026-02-12 16:36:51 +08:00
96f2ccee95 fix(production): 移除 Create.tsx 中未使用的 units 變數與重複屬性 2026-02-12 16:34:51 +08:00
c9113544ee Merge branch 'dev'
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 51s
2026-02-12 16:31:46 +08:00
5be4d49679 feat: 修正 BOM 單位顯示與完工入庫彈窗 UI 統一規範 2026-02-12 16:30:34 +08:00
b118ea0c39 Merge branch 'dev'
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 52s
2026-02-12 13:41:43 +08:00
eb5ab58093 test: debug 日誌測試 2026-02-12 13:41:42 +08:00
57e633c3e9 Merge branch 'dev'
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m14s
2026-02-12 13:39:57 +08:00
448b37ca90 test: 驗證 Runner 重複掛載修復 2026-02-12 13:39:56 +08:00
6c146ac717 Merge branch 'dev'
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 0s
2026-02-12 13:37:14 +08:00
cb433035fe docs: 移除 README 尾部多餘空行 2026-02-12 13:37:12 +08:00
e646c6ffd8 Merge branch 'dev'
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 0s
2026-02-12 13:31:42 +08:00
83e1c82b11 trigger: definitive fix confirmed (CONFIG_FILE env applied) 2026-02-12 13:31:41 +08:00
19397db2e9 Merge branch 'dev'
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 2s
2026-02-12 13:29:07 +08:00
db285a6b69 trigger: final repair test (correct network key) 2026-02-12 13:29:06 +08:00
74eeb449f8 Merge branch 'dev'
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 4s
2026-02-12 13:27:43 +08:00
28ece9fda4 trigger: re-run deploy 3 (force config network host) 2026-02-12 13:27:15 +08:00
bd292b0868 Merge branch 'dev'
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 5s
2026-02-12 13:23:10 +08:00
ac705a1e58 docs: 更新 README 文檔格式 2026-02-12 13:22:27 +08:00
936abc943e trigger: re-run deploy 2 (valid config)
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 3s
2026-02-12 13:17:45 +08:00
eabde37d15 trigger: re-run deploy (fix runner permissions)
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 1s
2026-02-12 13:14:48 +08:00
921f6e48fb docs: 更新 README 文檔
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 2s
2026-02-12 13:10:49 +08:00
ba4ceb7ff6 fix(ci): 還原正式環境部署配置至 erp.koori.tw:2224
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 1s
2026-02-12 13:09:15 +08:00
3be5d099c9 test
Some checks failed
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Failing after 48s
2026-02-12 13:02:14 +08:00
9537e48f08 chore: 更新 CI/CD 部署目標至新主機 (220.132.7.82) 2026-02-12 11:56:04 +08:00
165737750c chore: 測試 Gitea 推送功能 2026-02-12 09:46:14 +08:00
220478641d feat: 更新庫存報表、銷售匯入及採購單相關功能
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m3s
2026-02-10 17:18:59 +08:00
593ce94734 修正庫存報表分頁參數衝突導致明細顯示為空的問題 2026-02-10 16:07:31 +08:00
8b950f6529 feat: 實作庫存列表展開狀態保留 (使用 sessionStorage) 改良顯示與修正相關問題 2026-02-10 13:02:11 +08:00
e098e40fb8 feat: 庫存紀錄顯示儲位並優化返回狀態保持 2026-02-10 11:28:58 +08:00
83d26de6f9 refactor: 調整統計基準為明細筆數,並恢復庫存查詢為單一細目顯示模式 2026-02-10 11:15:08 +08:00
38642cc58b feat: 統一度量衡,確保儀表板統計與庫存查詢清單數據精確一致 2026-02-10 11:09:22 +08:00
a6393e03d8 feat: 實作即時庫存查詢功能、儀表板庫存導盤,及優化手動入庫批號與儲位連動與選單顯示 2026-02-10 10:47:31 +08:00
6980eac1a4 fix(sales): correct import start row instruction to row 3
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 53s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-09 17:22:38 +08:00
08e360464e refactor(sales): remove import template download and update dialog instructions
Some checks failed
Koori-ERP-Deploy-System / deploy-production (push) Has been cancelled
Koori-ERP-Deploy-System / deploy-demo (push) Has been cancelled
2026-02-09 17:21:35 +08:00
7cf640b2f4 feat(sales): replace import page with dialog UI and support template download
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-09 17:16:00 +08:00
613eb555ba feat(inventory): 強化調撥單功能,支援販賣機貨道欄位、開放商品重複加入及優化過帳庫存檢核
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 59s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-09 16:52:35 +08:00
65eb1a1b64 feat: 實作銷售單匯入權限控管並全面精簡權限顯示名稱
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 59s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-09 15:04:08 +08:00
b6fe9ad9f3 feat: 實作銷售單匯入管理、貨道扣庫優化及 UI 細節調整
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m8s
2026-02-09 14:36:47 +08:00
590580e20a refactor: 移除 SKU 欄位,統一使用 code 作為商品代碼
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m22s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-09 11:15:52 +08:00
c2e0ff726d feat(inventory): 在庫存調整與調撥對話框中加入商品編號顯示,方便區分重複品名 2026-02-09 10:40:44 +08:00
5e542752ba feat(inventory): 販賣機視覺優化、修復匯入日期缺失與倉庫刪除權限錯誤 2026-02-09 10:19:46 +08:00
f22df90e01 fix(Inventory): 修復庫存列表批號欄位與新增庫存頁面儲位欄位遺失問題,並還原批號輸入佈局 2026-02-06 17:35:50 +08:00
e018b75783 feat(inventory): 開放倉庫編號編輯、優化調撥單條碼搜尋與庫存匯入範本雙分頁說明 2026-02-06 16:36:14 +08:00
200d1989bd feat(inventory): 支援庫存新增不使用批號模式與自動累加邏輯 2026-02-06 15:56:50 +08:00
6c259859cf feat(procurement): 開放具核准權限人員可在「待核准」狀態下編輯採購單 2026-02-06 15:38:47 +08:00
6bfdd92347 feat(procurement): 統一採購單按鈕樣式與術語更名為「作廢」,並加強權限控管
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m28s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-06 15:32:12 +08:00
70f1709bd0 修正:單位管理中的代碼 (code) 無法儲存的問題 2026-02-06 14:33:32 +08:00
3fd333085b feat: 實作 POS API 整合功能,包含商品與銷售訂單同步及韌性機制
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 57s
2026-02-06 11:56:29 +08:00
906b094c18 feat: [商品管理] 優化商品匯入邏輯,支援 13 碼條碼自動生成、Upsert 更新機制與 Excel 說明工作表
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m7s
2026-02-06 09:26:50 +08:00
e1aa452b3c fix(product): 補回清單頁面的 is_active 資料回傳並修正表格 colSpan
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 50s
2026-02-05 16:19:13 +08:00
397a8a6484 fix(product): 設定 is_active 欄位預設為 true 並更新現有資料為啟用 2026-02-05 16:18:03 +08:00
24aed44cd3 feat(product): 恢復並實作商品起停用狀態功能,包含表單開關與列表顯示 2026-02-05 16:15:06 +08:00
196fec3120 feat(product): 修正控制器邏輯並徹底移除商品起停用開關,統一設為啟用 2026-02-05 16:13:24 +08:00
096a114457 feat(product): 移除商品起停用功能,後端預設所有商品為啟用狀態 2026-02-05 16:12:55 +08:00
af06ca7695 fix(product): 修正商品詳情與編輯頁面的麵包屑層級,支援動態導航 2026-02-05 16:02:20 +08:00
1d5bc68444 feat(product): 優化編輯後的跳轉邏輯,支援依來源回傳詳情頁或列表 2026-02-05 16:01:29 +08:00
075b9f1c98 feat(product): 新增商品詳情查看功能 2026-02-05 15:58:59 +08:00
49bb05d85a style(warehouse): 根據使用者要求調整統計標籤文字 2026-02-05 15:54:24 +08:00
687af254bd style(warehouse): 優化瑕疵倉顯示邏輯並簡化標籤為「過期統計」 2026-02-05 15:53:24 +08:00
a518d390bd feat(inventory): 實作過期與瑕疵庫存總計顯示,並強化庫存明細過期提示 2026-02-05 15:50:14 +08:00
ba3c10ac13 feat(warehouse): 庫存統計卡片加入總金額顯示 (可用/帳面)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 48s
2026-02-05 13:18:22 +08:00
dada3a6512 feat(product): 商品代號加入隨機產生按鈕 (8碼大寫英數)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 58s
2026-02-05 13:12:52 +08:00
b99e391cc6 feat(inventory): 盤點單列印格式加入批號欄位(位於品名之後)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 53s
2026-02-05 13:05:50 +08:00
0aa7fd1f75 fix(supply-chain): 修正出貨單詳情頁組件匯入錯誤以修復正式站編譯失敗
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 46s
2026-02-05 12:08:25 +08:00
3ce96537b3 feat: 標準化全系統數值輸入欄位與擴充商品價格功能
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m0s
1. UI 標準化:
   - 針對全系統數值輸入欄位統一加上 step='any' 以支援小數點。
   - 表格形式 (Table) 的數值輸入欄位統一加上 text-right 靠右對齊。
   - 修正 Components 與 Pages 中所有涉及金額與數量的輸入框。

2. 功能擴充與修正:
   - 擴充 Product 模型與相關 Dialog 以支援多種價格設定。
   - 修正 Inventory/GoodsReceipt/Create.tsx 未使用的變數錯誤。
   - 優化庫存相關頁面的 UI 一致性。

3. 其他:
   - 更新相關的 Type 定義與 Controller 邏輯。
2026-02-05 11:45:08 +08:00
04f3891275 feat: 實作出貨單模組並暫時導向通用製作中頁面,同步優化盤點與調撥功能的活動日誌顯示
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m11s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-02-05 09:33:36 +08:00
4299e985e9 feat: 優化庫存調撥單操作紀錄與 UI 佈局 2026-02-04 17:51:29 +08:00
2eb136d280 feat(inventory): 完善庫存盤調更新與日誌邏輯,新增「無需盤調」狀態判定
1. 修正 AdjustDocController 缺失 update 方法導致的錯誤。
2. 修正 ActivityDetailDialog 前端 map 渲染 undefined 的 TypeError。
3. 優化盤調單「過帳」日誌,現在會同步包含當時的商品明細快照。
4. 實作盤點單「無需盤調」(no_adjust) 自動判定邏輯:
   - 當盤點數量與庫存完全一致時,自動標記為 no_adjust 結案。
   - 更新前端標籤樣式與操作按鈕對應邏輯。
   - 限制 no_adjust 單據不可重複建立盤調單。
5. 統一盤點單與盤調單的日誌配置,優化 ID 轉名稱顯示。
2026-02-04 16:56:08 +08:00
88415505fb docs(skill): 更新操作紀錄實作規範
整合全域 ID 轉名稱邏輯、日誌合併策略以及針對 Collection 修改錯誤的修復方案。
2026-02-04 15:39:05 +08:00
702af0a259 feat(inventory): 重構庫存盤點流程與優化操作日誌
1. 重構盤點流程:實作自動狀態轉換(盤點中/盤點完成)、整合按鈕為「儲存盤點結果」、更名 UI 狀態標籤。
2. 優化操作日誌:
   - 實作全域 ID 轉名稱邏輯(倉庫、使用者)。
   - 合併單次操作的日誌記錄,避免重複產生。
   - 修復日誌產生過程中的 Collection 修改錯誤。
3. 修正 TypeScript lint 錯誤(Index, Show 頁面)。
2026-02-04 15:12:10 +08:00
f4f597e96d fix(inventory): 修復 Controller 語法錯誤並補齊操作記錄 2026-02-04 13:25:49 +08:00
a8b88b3375 feat(inventory): 實作盤點、盤調與調撥操作紀錄,並支援前端本地化顯示 2026-02-04 13:24:33 +08:00
95fdec8a06 feat(procurement): 修正採購單與進貨單日期標籤、狀態與操作紀錄本地化 2026-02-04 13:20:18 +08:00
4ba85ce446 feat(production): 優化生產單 BOM 原物料選取邏輯,支援商品 -> 倉庫 -> 批號連動與 API 分佈查詢 2026-02-04 13:08:05 +08:00
a0c450d229 refactor(role): 重構角色權限選擇介面並新增快速搜尋功能
1. 新增 PermissionSelector 組件,採用 Accordion 折疊式設計
2. 實作全選/取消全選、展開/收合全部功能
3. 新增權限搜尋過濾器,支援自動展開與中文關鍵字搜尋
4. 優化 UI細節:修正邊框顯示、調整全選框位置與邏輯
2026-02-04 11:07:32 +08:00
16967fc25d ci: 修正 tenants:run 參數語法
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 52s
2026-02-03 17:48:49 +08:00
29842510c4 ci: 自動化權限同步與快取清理邏輯
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 52s
2026-02-03 17:41:01 +08:00
19216f5846 feat(Inventory): 同步調撥管理權限邏輯至盤點管理標準
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m5s
2026-02-03 17:29:32 +08:00
bd999c7bb6 feat: 統一庫存管理分頁 UI 與寬度規範,並更新 SKILL 規範文件 2026-02-03 17:24:34 +08:00
15aaa039e4 feat: 完成 2026-01 月會報告 PPT 製作與視覺美化 2026-02-03 15:11:30 +08:00
27626e6aa8 feat(inventory): 商品管理新增儲位欄位
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 47s
2026-02-03 13:17:46 +08:00
a160e3f15f fix: 修復 ProfileController 缺失的 Request 引用問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m2s
2026-02-03 13:05:47 +08:00
d671c08338 feat: 實作使用者啟停用功能與安全性強化
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m1s
- 新增使用者「啟用/停用」狀態切換功能 (含後端 API、權限控管、活動紀錄)
- 強化安全性:隱藏超級管理員角色的可見度與操作權限
- 更新開發規範:加入多租戶資料同步規範於 framework.md
- 前端優化:使用 Switch 元件進行狀態快速切換,調整表格欄位順序
2026-02-03 11:51:46 +08:00
0185843c62 style: 優化規格 Tooltip 支援多行換行顯示
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
2026-02-02 17:29:58 +08:00
be5c121146 feat: 優化商品管理規格顯示與修復重複通知問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 47s
2026-02-02 17:24:49 +08:00
f87310e707 fix: 更新商品代號驗證規則為 2-8 碼
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 52s
1. ProductImport.php: 匯入規則調整
2. ProductController.php: 新增/編輯 API 規則調整
3. UI: 匯入與編輯視窗提示更新
2026-02-02 15:07:12 +08:00
b0192e9b66 fix(nginx): 正確轉發 X-Forwarded-Proto 標頭 (解決 Mixed Content 根源問題)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 45s
2026-02-02 14:57:18 +08:00
8a34aae312 fix: 強制應用層 HTTPS (解決 Mixed Content 分頁問題)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 52s
2026-02-02 14:51:27 +08:00
6204f0d915 feat: 新增商品 Excel 匯入功能與修復 HTTPS 混合內容問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m4s
1. 新增商品 Excel 匯入功能 (ProductImport, Export Template)
2. 調整商品代號驗證規則為 1-5 碼 (Controller & Import)
3. 修正 HTTPS Mixed Content 問題 (AppServiceProvider)
2026-02-02 14:39:13 +08:00
df3db38dd4 預設分類 2026-02-02 13:16:06 +08:00
75c634ffe4 fix(inventory): 修復倉庫低庫存警告計算與全站租戶名稱動態化
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 44s
2026-02-02 11:03:09 +08:00
1748eb007e feat(warehouse): 合併撥補單至調撥單流程並移除舊組件 2026-02-02 10:07:36 +08:00
313b95ceb9 fix(activity-log): 補足庫存對象 unit_cost 與 total_value 欄位翻譯 2026-02-02 09:37:27 +08:00
5e897e4197 fix(inventory): 修復調撥單明細庫存顯示與統一過帳按鈕樣式 2026-02-02 09:34:24 +08:00
71458dd976 feat(inventory): 實作撥補單建立即自動過帳邏輯並修正參數對齊問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 53s
2026-02-02 09:27:02 +08:00
36ef411975 fix(inventory): 修正撥補單儲存時的 Ziggy 路由名稱錯誤
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 56s
2026-02-02 09:19:34 +08:00
bb78a432f5 fix(product): 修復條碼掃描自動送出問題並優化手動輸入體驗
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m1s
2026-02-02 09:06:06 +08:00
0d720f3515 Refactor: Standardize Transfer Order Doc Numbering
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 57s
- Updated InventoryTransferOrder boot method to use sequential numbering (TRF+Ymd+Seq) matching InventoryAdjustDoc logic.
2026-01-29 16:48:01 +08:00
2e71a1cb29 Feature: Tenant Short Name and Branding Implementation
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
- Added short_name to Tenant model and controller
- Updated Landlord/Tenant pages (Create, Edit, Show, Index)
- Implemented branding customization (Favicon, Login Copyright, Sidebar Title)
- Updated HandleInertiaRequests to share branding data
2026-01-29 16:28:34 +08:00
746eeb6f01 更新:優化配方詳情彈窗 UI 與一般修正
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
2026-01-29 16:13:56 +08:00
7619dc24f7 feat(inventory): 統一庫存調整與調撥模組 UI,實作多選、搜尋與明細欄位重構
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m4s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-29 14:37:21 +08:00
2efaded77b 統一庫存盤點與盤調 UI 及邏輯:修正狀態顯示、操作權限與列表樣式
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-29 13:41:31 +08:00
a31c8d6052 feat: add void action to inventory count index
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m9s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-29 13:12:02 +08:00
56e30a85bb refactor: changes to inventory status (approved/unapprove)
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m6s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-29 13:04:54 +08:00
46753cc3bc fix(auth): 使用 Inertia::location 修復登入後重定向失敗問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
2026-01-29 10:25:37 +08:00
7f726e80bd fix(config): 更新 Session Cookie 名稱以強制解決瀏覽器舊 Cookie 衝突
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 44s
2026-01-29 10:18:00 +08:00
8bc95db43d fix(auth): 登出時強制清除 Session Cookie 以解決二次登入問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 47s
2026-01-29 10:08:57 +08:00
95a1763d04 fix(framework): 修正 TrustProxies 配置以解決 HTTPS 識別問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 42s
2026-01-29 10:04:32 +08:00
90cb7a82de fix(deploy): 恢復 node_modules 排除清單
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 47s
2026-01-29 10:00:04 +08:00
bbb2c4c4a3 style(deploy): 移除多餘空行
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
2026-01-29 09:56:13 +08:00
8cb95e1a56 fix(deploy): 修正正式環境部署漏掉 storage 排除清單導致檔案遺失的問題 2026-01-29 09:51:36 +08:00
fc59c86305 fix(deploy): 確保每次部署後重建 storage 軟連結
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 44s
2026-01-29 09:48:59 +08:00
b613cdb796 chore(docker): 啟動時自動檢查並建立 storage 軟連結
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
2026-01-29 09:45:12 +08:00
b1745555cc feat(tenancy): 租戶初始化流程新增自動補全基本單位資料 2026-01-29 09:38:23 +08:00
1833ca192d feat(inventory): 優化盤點顯示與權限設定 2026-01-29 09:36:07 +08:00
e5edad4fd0 style: 修正盤點與盤調畫面 Table Padding 並統一 UI 規範
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 1m4s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-28 18:04:45 +08:00
852370cfe0 fix(db): add activity_log migrations to central database
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 49s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-28 14:10:53 +08:00
965418077b fix(ui): provide default branding for central admin
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 40s
2026-01-28 14:01:08 +08:00
c3af92c85c feat(ui): dynamic page title based on tenant context
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 49s
2026-01-28 13:58:54 +08:00
cca49b5fe8 feat(assets): add default tenant logo and login background
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 44s
2026-01-28 13:49:24 +08:00
d4cef2cd84 fix(tenancy): force seeders in production and set default branding
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 51s
2026-01-28 13:45:28 +08:00
4c959efc8b feat: 補齊生產管理與進貨單權限、功能實作及 UI 優化
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-27 17:40:56 +08:00
95d8dc2e84 feat: 統一進貨單 UI、修復庫存異動紀錄與廠商詳情顯示報錯
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 51s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-27 17:23:31 +08:00
a7c445bd3f fix: 修正部分進貨採購單更新失敗與狀態顯示問題
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-27 13:27:28 +08:00
293358df62 refactor(inventory): 重構倉庫管理邏輯,移除 is_sellable 欄位並改由類型判定可用庫存 2026-01-27 10:23:49 +08:00
1ed3d6a29d docs(ui-consistency): 優化規範文件,明確標準操作優先使用主題色 2026-01-27 10:15:50 +08:00
646435f87a style(production): 修正檢視按鈕樣式為主題色並保留權限控制 2026-01-27 10:11:16 +08:00
f10c31abd0 style(production): 補齊生產工單列表檢視按鈕 UI 與權限控制 2026-01-27 10:10:10 +08:00
046e0a028b style(production): 統一生產模組操作圖示 UI、權限控制與 AlertDialog 2026-01-27 10:09:43 +08:00
ce0a7b3409 feat(procurement): 採購單號格式增加 PO 前綴 2026-01-27 10:05:46 +08:00
084bbc9f53 docs: 再次確認 framework.md 更新內容並同步
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 50s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-27 09:18:07 +08:00
3af4a1e298 docs: 更新開發框架規範,加入嚴格模組化通訊規範
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 47s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
2026-01-27 09:15:05 +08:00
448 changed files with 53035 additions and 9769 deletions

View File

@@ -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`

View File

@@ -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` 進行顯示控制。

View File

@@ -2,10 +2,6 @@
trigger: always_on
---
---
trigger: always_on
---
# 開發框架規範說明書ERP 系統 (star-erp)
## 1. 專案概述
@@ -50,15 +46,36 @@ trigger: always_on
* Routes: `kebab-case` (小寫橫線分隔)
* **回傳格式** 所有的前後端溝通需維持一致的 JSON 結構,特別是驗證錯誤 (Validation Errors) 與閃存訊息 (Flash Messages)。
## 6. AI 協作規則 (給 Antigravity AI)
## 6. 嚴格模組化通訊規範 (Strict Modular Communication)
為了確保系統的可維護性與獨立性,所有模組必須遵守以下「實體解耦」規範:
* **禁止跨模組 Eloquent 關聯**:禁止在 Model 中定義指向其他模組的 `belongsTo`, `hasMany` 等關聯。
* **介面化通訊 (Contracts)**:模組間的資料交換與功能調用必須透過 `app/Modules/{ModuleName}/Contracts/` 下定義的介面進行。
* **禁止跨模組 Model 引用**Controller 與 Service 禁止 `use` 其他模組的 Model (除非是該模組自身的 Contracts)。
* **手動資料水和 (Manual Hydration)**若頁面需要顯示跨模組資料訂單顯示使用者名稱Controller 應透過 Service 獲取基本資料,再手動組合成前端所需的 JSON/Props 結構。
* **資料一致性**:跨模組的資料操作應由各模組的 Service 處理其內部的 transaction 完整性。
## 7. AI 協作規則 (給 Antigravity AI)
* **角色設定** 你是一位專業的全端開發工程師助手。
* **代碼生成指令**
* 所有的解釋說明請使用 **繁體中文**。
* 生成 React 組件時,必須符合專案現有的 Tailwind CSS 配置。
* 必須考慮 ERP 邏輯(例如:權限判斷、操作日誌、資料完整性)。
* 新增功能時,請先判斷應歸屬於哪個 Module並建立在 `app/Modules/` 對應目錄下。
* **核心要求UI 規範與彈性設計 (重要)**
* 在開發「新功能」或「新頁面」前,產出的 `implementation_plan.md` 中**必須包含「UI 規範核對清單」**,明確列出將使用哪些已定義於 `ui-consistency/SKILL.md` 的元件(例如:`AlertDialog`、特定圖示名稱等)。
* **已規範部分**:絕對遵循《客戶端後台 UI 統一規範》進行實作。
* **未規範部分**:若遇到規範外的新 UI 區塊,請保有設計彈性,運用 Tailwind 打造符合 ERP 調性的初版設計,依據使用者的實際感受進行後續調整與收錄。
## 7. 運行機制 (Docker / Sail)
## 8. 多租戶開發規範 (Multi-tenancy Standards)
本專案採用多租戶隔離架構,開發時必須遵守以下資料同步規則:
* **權限與選單同步**:新增 Permission 或修改系統設定時,必須確保中央資料庫 (Central) 與所有租戶資料庫 (Tenants) 均已同步。
* **指令執行**
* **Seeders**: 必須執行 `./vendor/bin/sail php artisan tenants:run db:seed` 以確保所有租戶均獲得更新。
* **Tinker**: 檢查租戶資料時應使用 `./vendor/bin/sail php artisan tenants:run tinker`
* **Migrations**: 租戶相關的 Schema 異動應放在 `database/migrations/tenant/` 並執行 `./vendor/bin/sail artisan tenants:migrate`
## 9. 運行機制 (Docker / Sail)
由於專案運行在 Docker 容器環境中,請勿直接在宿主機 (Host) 執行 php 或 composer 指令。請使用專案內建的 `sail` 指令:
* **啟動環境** `./vendor/bin/sail up -d`
@@ -66,3 +83,18 @@ trigger: always_on
* **執行 Artisan 指令** `./vendor/bin/sail artisan route:list`
* **執行 Composer** `./vendor/bin/sail composer install`
* **執行 Node/NPM** `./vendor/bin/sail npm run dev`
## 10. 部署與查修環境 (CI/CD & Troubleshooting)
* **自動化部署**:本專案使用 CI/CD 自動化部署,開發者只需 push 程式碼至對應分支即可。
* **Demo 環境 (對應 `demo` 分支)**:若需查修測試站問題(例如查看 Error Log 或資料庫),請連線 `ssh gitea_work`
* **Production 環境 (對應 `main` 分支)**:若需查修正式站問題,請連線 `ssh erp`
## 11. 瀏覽器測試規範 (Browser Testing)
當需要進行瀏覽器自動化測試或手動驗證時,請遵守以下連線資訊:
* **本地測試網址**`http://localhost:8081/`
* **預設管理員帳號**`admin`
* **預設管理員密碼**`password`
> [!IMPORTANT]
> 在執行 browser subagent 或進行 E2E 測試時,請務必確認為 `8081` Port以避免連線至錯誤的服務環境。

View 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 文件可能已經更新,且記憶中的內容可能不完整。

View 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` 兩處同步?

View File

@@ -0,0 +1,137 @@
---
name: 跨模組調用與通訊規範 (Cross-Module Communication)
description: 規範 Laravel Modular Monolith 架構下不同業務模組中如何彼此調用資料與邏輯包含禁止項目、Interface 實作、與 Service 綁定規則。
---
# 跨模組調用與通訊規範 (Cross-Module Communication)
為了確保專案的「模組化單體架構 (Modular Monolith)」的獨立性與可維護性,當遇到**需要跨越不同業務模組存取資料或調用功能**的情境時,請嚴格遵守以下規範。
## 🚫 絕對禁止的行為 (Strict Prohibitions)
* **禁止跨模組 Eloquent 關聯(例外除外)**
* **禁止跨模組直接引入 (use) Model**
* **禁止跨模組直接實例化 (new) Service**
---
## 🌟 允許的全域例外 (Global Exceptions)
雖然我們嚴格禁止跨模組直接相依,但為了開發效率與框架機制的完整性,**`Core` 模組下的特定基礎設施模型 (Infrastructure Models) 被視為全域例外**。
其他業務模組 **可以** 透過 Eloquent (`belongsTo` / `hasMany`) 直接關聯以下 Model
1. **`App\Modules\Core\Models\User`**
2. **`App\Modules\Core\Models\Role`**
3. **`App\Modules\Core\Models\Tenant`**
> **⚠️ 注意**:這項例外是單向的。`Core` 模組內的業務邏輯(如 `DashboardController`**絕對不能**反過來直接 `use` 外部業務模組的 Model仍必須透過外部模組的 Service Interface 來索取資料。
---
## ✅ 正確的跨模組調用流程:合約與依賴反轉
所有的跨模組資料交換與功能調用,必須透過**介面化通訊 (Contracts)** 進行。
### Step 1: 在被調用的模組定義合約 (Interface)
如果 `Inventory` 模組需要提供功能給外部使用,請在 `app/Modules/Inventory/Contracts/` 建立 Interface 檔案。
```php
namespace App\Modules\Inventory\Contracts;
use Illuminate\Support\Collection;
interface InventoryServiceInterface
{
public function getActiveWarehouses(): Collection;
}
```
### Step 2: 實作介面並在自己模組的 ServiceProvider 註冊
`Inventory` 模組自己的 Service 來實作上述介面。
```php
namespace App\Modules\Inventory\Services;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Models\Warehouse;
use Illuminate\Support\Collection;
class InventoryService implements InventoryServiceInterface
{
public function getActiveWarehouses(): Collection
{
return Warehouse::where('is_active', true)
->select(['id', 'name', 'code'])
->get();
}
}
```
然後進入 `app/Modules/Inventory/InventoryServiceProvider.php` 完成綁定:
```php
namespace App\Modules\Inventory;
use Illuminate\Support\ServiceProvider;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Services\InventoryService;
class InventoryServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(InventoryServiceInterface::class, InventoryService::class);
}
}
```
### Step 3: 調用方透過依賴注入 (DI) 使用服務
`Procurement` 模組需要取得倉庫資料時,必須透過**建構子注入**或**方法注入**取得 `InventoryServiceInterface`
```php
namespace App\Modules\Procurement\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use Inertia\Inertia;
class PurchaseOrderController extends Controller
{
public function __construct(
protected InventoryServiceInterface $inventoryService
) {}
public function create()
{
$warehouses = $this->inventoryService->getActiveWarehouses();
return Inertia::render('Procurement/PurchaseOrder/Create', [
'warehouses' => $warehouses
]);
}
}
```
---
## ⚠️ 跨模組資料回傳的注意事項 (Data Hydration)
* **回傳純粹資料**:建議在 Service 中用 `with()` 載入好關聯,或者直接轉為原生的 Array 或有具體結構的 DTO避免依賴 Lazy Loading。
* **手動組合 (Manual Hydration)**:若某個頁面需要合併兩個模組的資料,必須在 Controller 層級呼叫兩個不同的 Service Interface 後,手動合併。
### 範例:手動合併資料
```php
// 正確示範:在各自模組取資料,並手動組裝
$orders = $this->orderService->getOrders();
$userIds = $orders->pluck('user_id')->unique()->toArray();
$users = $this->coreUserService->getUsersByIds($userIds)->keyBy('id');
$mergedData = $orders->map(function ($order) use ($users) {
// 將使用者資料手動附加上去
$order->user_name = $users->get($order->user_id)->name ?? 'Unknown';
return $order;
});
```

View 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`

View 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` 且不在允許時段內,我會優先進行安全提醒。

View 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 統一規範)。

View File

@@ -80,7 +80,7 @@ tooltip
<div style={{ borderColor: 'var(--primary-main)' }}>...</div>
// ❌ 錯誤:寫死色碼 (會導致租戶無法換色)
<div className="text-[#01ab83]">...</div>
<div className="text-primary-main">...</div>
```
### 2.2 灰階 (Grey Scale)
@@ -123,8 +123,8 @@ tooltip
// ✅ 成功操作
<Button className="button-filled-success">確認</Button>
// ✅ 資訊操作
<Button className="button-filled-info">查看詳情</Button>
// ✅ 資訊操作(用於系統提示、說明等非業務主流程)
<Button className="button-filled-info">系統資訊</Button>
// ✅ 警告操作
<Button className="button-filled-warning">警告</Button>
@@ -177,6 +177,23 @@ tooltip
</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
@@ -230,6 +247,30 @@ tooltip
</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>
```
---
## 4. 圖標規範
@@ -278,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">
<Users className="h-6 w-6 text-[#01ab83]" />
<Users className="h-6 w-6 text-primary-main" />
使用者管理
</h1>
@@ -543,7 +584,7 @@ export default function ResourceIndex() {
<div className="flex items-center justify-between mb-6">
<div>
<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>
<p className="text-gray-500 mt-1">
@@ -740,7 +781,75 @@ import { SearchableSelect } from "@/Components/ui/searchable-select";
- **Select / SearchableSelect**: 必須確保 Trigger 按鈕高度為 `h-9`
- **禁止使用**: 除非有特殊設計需求,否則避免使用 `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)
日期輸入框應採用「**左側裝飾圖示 + 右側原生操作**」的配置,以保持視覺一致性並保留瀏覽器原生便利性。
@@ -764,7 +873,7 @@ import { Input } from "@/Components/ui/input";
</div>
```
## 11.7 搜尋選單樣式 (SearchableSelect Style)
## 11.8 搜尋選單樣式 (SearchableSelect Style)
`SearchableSelect` 元件在表單或篩選列中使用時,高度必須設定為 `h-9` 以與輸入框對齊。
@@ -775,7 +884,7 @@ import { Input } from "@/Components/ui/input";
/>
```
## 11.8 篩選列規範 (Filter Bar Norms)
## 11.9 篩選列規範 (Filter Bar Norms)
列表頁面的篩選區域Filter Bar應遵循以下規範以節省空間並保持層級清晰

View 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` 分支,維持全環境版本一致。

View File

@@ -4,6 +4,7 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_VERSION=v1.0.0
# Multi-tenancy 設定 (用逗號分隔多個中央網域)
CENTRAL_DOMAINS=localhost,127.0.0.1
@@ -43,7 +44,7 @@ BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database
CACHE_STORE=database
CACHE_STORE=redis
# CACHE_PREFIX=
MEMCACHED_HOST=127.0.0.1

View File

@@ -0,0 +1,101 @@
name: ERP-Deploy-Demo
on:
push:
branches:
- demo
jobs:
deploy-demo:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
github-server-url: https://gitea.taiwan-star.com.tw
repository: ${{ github.repository }}
- name: Step 1 - Push Code to Demo
run: |
apt-get update && apt-get install -y rsync openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.DEMO_SSH_KEY }}" > ~/.ssh/id_rsa_demo
chmod 600 ~/.ssh/id_rsa_demo
rsync -avz --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='vendor' \
--exclude='storage' \
--exclude='.env' \
--exclude='public/build' \
-e "ssh -p 2227 -i ~/.ssh/id_rsa_demo -o StrictHostKeyChecking=no" \
./ root@220.132.7.82:/var/www/star-erp-demo/
rm ~/.ssh/id_rsa_demo
- name: Step 2 - Check if Rebuild Needed
id: check_rebuild
uses: appleboy/ssh-action@master
with:
host: 220.132.7.82
port: 2227
username: root
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
cd /var/www/star-erp-demo
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|docker-compose\.yaml)'; then
echo "REBUILD_NEEDED=true"
else
echo "REBUILD_NEEDED=false"
fi
- name: Step 3 - Container Up & Health Check
uses: appleboy/ssh-action@master
with:
host: 220.132.7.82
port: 2227
username: root
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
cd /var/www/star-erp-demo
chown -R 1000:1000 .
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|compose\.demo\.yaml|docker-compose\.yaml)'; then
echo "🔄 偵測到 Docker 相關檔案變更,執行完整重建..."
WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.demo.yaml up -d --build --wait
else
echo "⚡ 無 Docker 檔案變更,僅重載服務..."
if ! docker ps --format '{{.Names}}' | grep -q 'star-erp-laravel'; then
echo "容器未運行,正在啟動..."
WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.demo.yaml up -d --wait
else
echo "容器已運行,跳過 docker compose直接進行程式碼部署..."
fi
fi
echo "容器狀態:" && docker ps --filter "name=star-erp-laravel"
- name: Step 4 - Composer & NPM Build
uses: appleboy/ssh-action@master
with:
host: 220.132.7.82
port: 2227
username: root
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
docker exec -u 1000:1000 -w /var/www/html star-erp-laravel sh -c "
composer install --no-dev --optimize-autoloader --no-interaction &&
npm install &&
npm run build &&
rm -f public/hot &&
php artisan storage:link &&
php artisan migrate --force &&
php artisan tenants:migrate --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=SystemSettingSeeder\" --option=\"force=true\" &&
php artisan permission:cache-reset &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache
"
docker exec star-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

View File

@@ -0,0 +1,94 @@
name: ERP-Deploy-Production
on:
push:
branches:
- main
jobs:
deploy-production:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
repository: ${{ github.repository }}
- name: Step 1 - Push Code to Production
run: |
apt-get update && apt-get install -y rsync openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.PROD_SSH_KEY }}" > ~/.ssh/id_rsa_prod
chmod 600 ~/.ssh/id_rsa_prod
rsync -avz --delete \
--exclude='.git' \
--exclude='.env' \
--exclude='node_modules' \
--exclude='vendor' \
--exclude='storage' \
--exclude='public/build' \
-e "ssh -p 2224 -i ~/.ssh/id_rsa_prod -o StrictHostKeyChecking=no" \
./ root@220.132.7.82:/var/www/star-erp/
rm ~/.ssh/id_rsa_prod
- name: Step 2 - Check if Rebuild Needed
id: check_rebuild_prod
uses: appleboy/ssh-action@master
with:
host: 220.132.7.82
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /var/www/star-erp
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|docker-compose\.yaml)'; then
echo "REBUILD_NEEDED=true"
else
echo "REBUILD_NEEDED=false"
fi
- name: Step 3 - Container Up & Health Check
uses: appleboy/ssh-action@master
with:
host: 220.132.7.82
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /var/www/star-erp
chown -R 1000:1000 .
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|compose\.prod\.yaml|docker-compose\.yaml)'; then
echo "🔄 偵測到 Docker 相關檔案變更,執行完整重建..."
WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.prod.yaml up -d --build --wait
else
echo "⚡ 無 Docker 檔案變更,僅重載服務..."
if ! docker ps --format '{{.Names}}' | grep -q 'star-erp-laravel'; then
echo "容器未運行,正在啟動..."
WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml -f compose.prod.yaml up -d --wait
else
echo "容器已運行,跳過 docker compose直接進行程式碼部署..."
fi
fi
echo "容器狀態:" && docker ps --filter "name=star-erp-laravel"
docker exec -u 1000:1000 -w /var/www/html star-erp-laravel sh -c "
composer install --no-dev --optimize-autoloader &&
npm install &&
npm run build &&
rm -f public/hot
php artisan storage:link &&
php artisan migrate --force &&
php artisan tenants:migrate --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=SystemSettingSeeder\" --option=\"force=true\" &&
php artisan permission:cache-reset &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache
"
docker exec star-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

View File

@@ -1,200 +0,0 @@
name: Koori-ERP-Deploy-System
on:
push:
branches:
- demo
- main
jobs:
# --- 1. Demo 環境部署 (103 本機) ---
deploy-demo:
if: github.ref == 'refs/heads/demo'
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
github-server-url: http://192.168.0.103:3000
repository: ${{ github.repository }}
- name: Step 1 - Push Code to Demo
run: |
apt-get update && apt-get install -y rsync openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.DEMO_SSH_KEY }}" > ~/.ssh/id_rsa_demo
chmod 600 ~/.ssh/id_rsa_demo
rsync -avz --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='vendor' \
--exclude='storage' \
--exclude='.env' \
--exclude='public/build' \
-e "ssh -i ~/.ssh/id_rsa_demo -o StrictHostKeyChecking=no" \
./ amba@192.168.0.103:/home/amba/star-erp/
rm ~/.ssh/id_rsa_demo
# 2. 檢查是否需要重建容器(只有 Dockerfile 或 compose.yaml 變動時才重建)
- name: Step 2 - Check if Rebuild Needed
id: check_rebuild
uses: appleboy/ssh-action@master
with:
host: 192.168.0.103
port: 22
username: amba
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
cd /home/amba/star-erp
# 檢查最近的 commit 是否包含 Dockerfile 或 compose.yaml 的變更
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|docker-compose\.yaml)'; then
echo "REBUILD_NEEDED=true"
else
echo "REBUILD_NEEDED=false"
fi
# 3. 啟動或重建容器(根據檢查結果決定是否加 --build
- name: Step 3 - Container Up & Health Check
uses: appleboy/ssh-action@master
with:
host: 192.168.0.103
port: 22
username: amba
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
cd /home/amba/koori-erp
chown -R 1000:1000 .
# 檢查是否需要重建
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|docker-compose\.yaml)'; then
echo "🔄 偵測到 Docker 相關檔案變更,執行完整重建..."
WWWGROUP=1000 WWWUSER=1000 docker compose up -d --build --wait
else
echo "⚡ 無 Docker 檔案變更,僅重載服務..."
# 確保容器正在運行(若未運行則啟動)
if ! docker ps --format '{{.Names}}' | grep -q 'koori-erp-laravel'; then
echo "容器未運行,正在啟動..."
WWWGROUP=1000 WWWUSER=1000 docker compose up -d --wait
else
echo "容器已運行,跳過 docker compose直接進行程式碼部署..."
fi
fi
echo "容器狀態:" && docker ps --filter "name=koori-erp-laravel"
- name: Step 4 - Composer & NPM Build
uses: appleboy/ssh-action@master
with:
host: 192.168.0.103
port: 22
username: amba
key: ${{ secrets.DEMO_SSH_KEY }}
script: |
docker exec -u 1000:1000 -w /var/www/html star-erp-laravel sh -c "
# 1. 後端依賴 (Demo 環境建議加上 --no-interaction 避免卡住)
composer install --no-dev --optimize-autoloader --no-interaction &&
# 2. 前端編譯
npm install &&
npm run build &&
# 3. Laravel 初始化與優化
php artisan migrate --force &&
php artisan db:seed --force &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache
"
docker exec star-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
# --- 2. 正式環境部署 (erp.koori.tw:2224) ---
deploy-production:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
github-server-url: http://192.168.0.103:3000
repository: ${{ github.repository }}
- name: Step 1 - Push Code to Production
run: |
apt-get update && apt-get install -y rsync openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.PROD_SSH_KEY }}" > ~/.ssh/id_rsa_prod
chmod 600 ~/.ssh/id_rsa_prod
rsync -avz --delete \
--exclude='.git' \
--exclude='.env' \
--exclude='node_modules' \
--exclude='vendor' \
--exclude='public/build' \
-e "ssh -p 2224 -i ~/.ssh/id_rsa_prod -o StrictHostKeyChecking=no" \
./ root@erp.koori.tw:/var/www/star-erp/
rm ~/.ssh/id_rsa_prod
# 2. 檢查是否需要重建容器(只有 Dockerfile 或 compose.yaml 變動時才重建)
- name: Step 2 - Check if Rebuild Needed
id: check_rebuild_prod
uses: appleboy/ssh-action@master
with:
host: erp.koori.tw
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /var/www/star-erp
# [Patch] 修正正式機 Nginx Proxy 配置 (對應外部 SSL/OpenResty)
sed -i "s/- '8080:8080'/- '80:80'\n - '8080:8080'/" compose.yaml
sed -i "s/demo-proxy.conf/prod-proxy.conf/" compose.yaml
# 檢查最近的 commit 是否包含 Dockerfile 或 compose.yaml 的變更
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|docker-compose\.yaml)'; then
echo "REBUILD_NEEDED=true"
else
echo "REBUILD_NEEDED=false"
fi
# 3. 啟動或重建容器(根據檢查結果決定是否加 --build
- name: Step 3 - Container Up & Health Check
uses: appleboy/ssh-action@master
with:
host: erp.koori.tw
port: 2224
username: root
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /var/www/star-erp
chown -R 1000:1000 .
# 檢查是否需要重建
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -qE '(Dockerfile|compose\.yaml|docker-compose\.yaml)'; then
echo "🔄 偵測到 Docker 相關檔案變更,執行完整重建..."
WWWGROUP=1000 WWWUSER=1000 docker compose up -d --build --wait
else
echo "⚡ 無 Docker 檔案變更,僅重載服務..."
# 確保容器正在運行(若未運行則啟動)
if ! docker ps --format '{{.Names}}' | grep -q 'star-erp-laravel'; then
echo "容器未運行,正在啟動..."
WWWGROUP=1000 WWWUSER=1000 docker compose up -d --wait
else
echo "容器已運行,跳過 docker compose直接進行程式碼部署..."
fi
fi
echo "容器狀態:" && docker ps --filter "name=star-erp-laravel"
docker exec -u 1000:1000 -w /var/www/html star-erp-laravel sh -c "
composer install --no-dev --optimize-autoloader &&
npm install &&
npm run build
php artisan migrate --force &&
php artisan optimize:clear &&
php artisan optimize &&
php artisan view:cache
"
docker exec star-erp-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

17
.gitignore vendored
View File

@@ -18,6 +18,7 @@
/public/storage
/storage/*.key
/storage/pail
/storage/tenant*
/vendor
Homestead.json
Homestead.yaml
@@ -26,3 +27,19 @@ Thumbs.db
智慧補貨系統分析報告.md
/docs/pptx_build
/docs/presentation
docs/Monthly_Report_2026_01.pptx
docs/f6_1770350984272.xlsx
公共事業費-描述.md
.gitignore
BOM表自動計算成本.md
公共事業費-類別維護.md
# Playwright
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
/playwright/.auth/
e2e/screenshots/

View File

@@ -13,7 +13,7 @@ Star ERP 是一個基於 **Laravel 12**、**Inertia.js (React)** 與 **Tailwind
## 📂 系統功能詳細說明
### 🌳 系統功能架構樹 (含 2.0 升級規劃)
### 🌳 預計系統功能架構樹 (含 2.0 升級規劃)
```text
Star ERP
├── 🏠 儀表板 (Dashboard)
@@ -107,6 +107,7 @@ Star ERP
git clone <repository_url> star-erp
cd star-erp
# 2. 設定環境變數
cp .env.example .env
# 請檢查 .env 內容,本機開發預設配置:
@@ -171,7 +172,6 @@ docker exec -it star-erp-laravel php artisan tinker
# 停止容器
docker compose down
```
## 🧪 開發規範
- **後端**: Follow Laravel 12 最佳實踐,使用 Service/Action 模式處理複雜邏輯。

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

View File

@@ -19,6 +19,7 @@ class TenantController extends Controller
return [
'id' => $tenant->id,
'name' => $tenant->name ?? $tenant->id,
'short_name' => $tenant->short_name ?? null,
'email' => $tenant->email ?? null,
'is_active' => $tenant->is_active ?? true,
'created_at' => $tenant->created_at->format('Y-m-d H:i'),
@@ -47,6 +48,7 @@ class TenantController extends Controller
$validated = $request->validate([
'id' => ['required', 'string', 'max:50', 'alpha_dash', Rule::unique('tenants', 'id')],
'name' => ['required', 'string', 'max:100'],
'short_name' => ['nullable', 'string', 'max:50'],
'email' => ['nullable', 'email', 'max:100'],
'domain' => ['nullable', 'string', 'max:100'],
]);
@@ -54,8 +56,14 @@ class TenantController extends Controller
$tenant = Tenant::create([
'id' => $validated['id'],
'name' => $validated['name'],
'short_name' => $validated['short_name'] ?? null,
'email' => $validated['email'] ?? null,
'is_active' => true,
'branding' => [
'logo_path' => 'defaults/logo.png', // 預設 Logo 路徑
'login_background_path' => 'defaults/login_bg.jpg', // 預設登入背景
'primary_color' => '#4F46E5', // 預設主色系 (Indigo-600)
],
]);
// 綁定網域(如果沒有輸入,使用預設網域)
@@ -76,10 +84,29 @@ class TenantController extends Controller
{
$tenant = Tenant::with('domains')->findOrFail($id);
$tokens = [];
try {
tenancy()->initialize($tenant);
$user = \App\Modules\Core\Models\User::first();
if ($user) {
$tokens = $user->tokens()->orderBy('created_at', 'desc')->get(['id', 'name', 'last_used_at', 'created_at'])->map(function($token) {
return [
'id' => $token->id,
'name' => $token->name,
'last_used_at' => $token->last_used_at ? $token->last_used_at->format('Y-m-d H:i') : '未使用',
'created_at' => $token->created_at->format('Y-m-d H:i'),
];
});
}
} catch (\Exception $e) {
\Log::warning("Failed to fetch tokens for tenant {$id}: " . $e->getMessage());
}
return Inertia::render('Landlord/Tenant/Show', [
'tenant' => [
'id' => $tenant->id,
'name' => $tenant->name ?? $tenant->id,
'short_name' => $tenant->short_name ?? null,
'email' => $tenant->email ?? null,
'is_active' => $tenant->is_active ?? true,
'created_at' => $tenant->created_at->format('Y-m-d H:i'),
@@ -89,6 +116,7 @@ class TenantController extends Controller
'domain' => $d->domain,
])->toArray(),
],
'tokens' => $tokens,
]);
}
@@ -123,6 +151,7 @@ class TenantController extends Controller
'tenant' => [
'id' => $tenant->id,
'name' => $tenant->name ?? $tenant->id,
'short_name' => $tenant->short_name ?? null,
'email' => $tenant->email ?? null,
'is_active' => $tenant->is_active ?? true,
],
@@ -138,6 +167,7 @@ class TenantController extends Controller
$validated = $request->validate([
'name' => ['required', 'string', 'max:100'],
'short_name' => ['nullable', 'string', 'max:50'],
'email' => ['nullable', 'email', 'max:100'],
'is_active' => ['boolean'],
]);
@@ -231,4 +261,58 @@ class TenantController extends Controller
return redirect()->back()->with('success', '樣式設定已更新');
}
/**
* 建立 API Token (用於 POS)
*/
public function createToken(Request $request, Tenant $tenant)
{
$request->validate([
'name' => 'required|string|max:50',
]);
try {
// 切換至租戶環境
tenancy()->initialize($tenant);
// 尋找超級管理員 (假設 ID 1, 或者根據 Role)
// 這裡簡單取第一個使用者,通常是 Admin
$user = \App\Modules\Core\Models\User::first();
if (!$user) {
return back()->with('error', '該租戶尚無使用者,無法建立 Token。');
}
// 建立 Token
$token = $user->createToken($request->name);
return back()->with('success', 'Token 建立成功')->with('new_token', $token->plainTextToken);
} catch (\Exception $e) {
\Log::error("Token creation failed: " . $e->getMessage());
return back()->with('error', 'Token 建立失敗');
} finally {
// tenancy()->end(); // Laravel Tenancy 自動處理 scope 結束? 通常 Controller request life-cycle?
// Landlord controller is Central. Tenancy initialization persists for request.
// We should explicit end if we want to be safe, but redirect ends request anyway.
}
}
/**
* 撤銷 API Token
*/
public function revokeToken(Request $request, Tenant $tenant, string $tokenId)
{
try {
tenancy()->initialize($tenant);
$user = \App\Modules\Core\Models\User::first();
if ($user) {
$user->tokens()->where('id', $tokenId)->delete();
}
return back()->with('success', 'Token 已撤銷');
} catch (\Exception $e) {
return back()->with('error', 'Token 撤銷失敗');
}
}
}

View File

@@ -37,8 +37,16 @@ class HandleInertiaRequests extends Middleware
{
$user = $request->user();
$tenant = tenancy()->tenant;
$appName = $tenant ? ($tenant->name ?? 'Star ERP') : 'Star ERP 中央後台';
// 分享給 Blade View (給 app.blade.php 使用)
\Illuminate\Support\Facades\View::share('appName', $appName);
return [
...parent::share($request),
'appName' => $appName,
'app_version' => config('app.version'),
'auth' => [
'user' => $user ? [
'id' => $user->id,
@@ -54,23 +62,40 @@ class HandleInertiaRequests extends Middleware
'flash' => [
'success' => $request->session()->get('success'),
'error' => $request->session()->get('error'),
'new_token' => $request->session()->get('new_token'),
],
'branding' => function () {
$tenant = tenancy()->tenant;
if (!$tenant) {
return null;
}
// 決定名稱顯示邏輯
$fullName = $tenant ? ($tenant->name ?? 'Star ERP') : 'Star ERP 中央後台';
$shortName = $tenant ? ($tenant->short_name ?? $fullName) : 'Start ERP';
$logoUrl = null;
if (isset($tenant->branding['logo_path'])) {
if ($tenant && isset($tenant->branding['logo_path'])) {
$logoUrl = \Storage::url($tenant->branding['logo_path']);
} elseif (!$tenant) {
$logoUrl = \Storage::url('defaults/logo.png');
}
return [
$brandingData = [
'name' => $fullName,
'short_name' => $shortName,
'logo_url' => $logoUrl,
'primary_color' => $tenant->branding['primary_color'] ?? '#01ab83',
'primary_color' => $tenant->branding['primary_color'] ?? ($tenant ? '#01ab83' : '#4F46E5'),
'text_color' => $tenant->branding['text_color'] ?? '#1a1a1a',
];
// 同步分享給 Blade View (給 app.blade.php 使用 Favicon)
\Illuminate\Support\Facades\View::share('branding', $brandingData);
return $brandingData;
},
'notifications' => function () use ($request) {
return $request->user() ? [
'latest' => $request->user()->notifications()->latest()->limit(10)->get(),
'unread_count' => $request->user()->unreadNotifications()->count(),
] : null;
},
];
}

View 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 [];
}
}

View 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 [];
}
}

View File

@@ -22,13 +22,27 @@ class ActivityLogController extends Controller
'App\Modules\Procurement\Models\PurchaseOrder' => '採購單',
'App\Modules\Inventory\Models\Warehouse' => '倉庫',
'App\Modules\Inventory\Models\Inventory' => '庫存',
'App\Modules\Inventory\Models\InventoryTransaction' => '庫存異動紀錄',
'App\Modules\Finance\Models\UtilityFee' => '公共事業費',
'App\Modules\Inventory\Models\GoodsReceipt' => '進貨單',
'App\Modules\Production\Models\ProductionOrder' => '生產工單',
'App\Modules\Production\Models\Recipe' => '生產配方',
'App\Modules\Production\Models\RecipeItem' => '配方品項',
'App\Modules\Production\Models\ProductionOrderItem' => '工單品項',
'App\Modules\Inventory\Models\InventoryCountDoc' => '庫存盤點單',
'App\Modules\Inventory\Models\InventoryAdjustDoc' => '庫存盤調單',
'App\Modules\Inventory\Models\InventoryTransferOrder' => '庫存調撥單',
'App\Modules\Inventory\Models\StoreRequisition' => '門市叫貨單',
];
}
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');
$sortOrder = $request->input('sort_order', 'desc');
@@ -76,6 +90,7 @@ class ActivityLogController extends Controller
}
$activities = $query->paginate($perPage)
->withQueryString()
->through(function ($activity) {
$subjectMap = $this->getSubjectMap();

View File

@@ -7,6 +7,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Cookie;
class LoginController extends Controller
{
@@ -42,17 +43,27 @@ class LoginController extends Controller
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials, $request->boolean('remember'))) {
// Check activation status
if (!Auth::user()->is_active) {
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
throw ValidationException::withMessages([
'username' => '此帳號已被停用,請聯繫管理員。',
]);
}
$request->session()->regenerate();
$centralDomains = config('tenancy.central_domains', []);
$centralDomains = config('tenancy.central_domains', []);
// [Hack] Demo 環境特殊規則
$demoPort = config('tenancy.demo_tenant_port');
if ((!$demoPort || $request->getPort() != $demoPort) && in_array($request->getHost(), $centralDomains)) {
return redirect()->intended(route('landlord.dashboard'));
return Inertia::location(route('landlord.dashboard'));
}
return redirect()->intended(route('dashboard'));
return Inertia::location(route('dashboard'));
}
throw ValidationException::withMessages([
@@ -71,6 +82,10 @@ class LoginController extends Controller
$request->session()->regenerateToken();
// 強制清除 Session Cookie (對付 HTTPS/Proxy 環境下的殘留問題)
$sessionCookieName = config('session.cookie');
Cookie::queue(Cookie::forget($sessionCookieName));
return redirect('/');
}
}

View File

@@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Sales\Contracts\SalesServiceInterface;
use App\Modules\Production\Contracts\ProductionServiceInterface;
use Inertia\Inertia;
use Illuminate\Http\Request;
@@ -13,13 +15,19 @@ class DashboardController extends Controller
{
protected $inventoryService;
protected $procurementService;
protected $salesService;
protected $productionService;
public function __construct(
InventoryServiceInterface $inventoryService,
ProcurementServiceInterface $procurementService
ProcurementServiceInterface $procurementService,
SalesServiceInterface $salesService,
ProductionServiceInterface $productionService
) {
$this->inventoryService = $inventoryService;
$this->procurementService = $procurementService;
$this->salesService = $salesService;
$this->productionService = $productionService;
}
public function index()
@@ -34,18 +42,91 @@ class DashboardController extends Controller
$invStats = $this->inventoryService->getDashboardStats();
$procStats = $this->procurementService->getDashboardStats();
$stats = [
'productsCount' => $invStats['productsCount'],
'vendorsCount' => $procStats['vendorsCount'],
'purchaseOrdersCount' => $procStats['purchaseOrdersCount'],
'warehousesCount' => $invStats['warehousesCount'],
'totalInventoryValue' => $invStats['totalInventoryQuantity'], // 原本前端命名是 totalInventoryValue 但實作是 Quantity暫且保留欄位名以不破壞前端
'pendingOrdersCount' => $procStats['pendingOrdersCount'],
'lowStockCount' => $invStats['lowStockCount'],
];
// 銷售統計 (本月營收)
$thisMonthRevenue = $this->salesService->getThisMonthRevenue();
// 生產統計 (待核准工單)
$pendingProductionCount = $this->productionService->getPendingProductionCount();
// 生產狀態分佈
// 近30日銷售趨勢 (Area Chart)
$salesTrend = $this->salesService->getSalesTrend();
// 本月熱銷商品 Top 5 (Bar Chart)
$topSellingItems = $this->salesService->getTopSellingProducts();
$productIds = $topSellingItems->pluck('product_id')->filter()->unique()->toArray();
$productsMap = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
$topSellingProducts = $topSellingItems->map(function ($item) use ($productsMap) {
$product = $productsMap->get($item->product_id);
return [
'name' => $product ? $product->name : $item->product_code,
'amount' => (int)$item->total_amount,
];
});
// 庫存積壓排行 (Top Inventory Value)
$topInventoryValueItems = $this->inventoryService->getTopInventoryValue();
$invProductIds = $topInventoryValueItems->pluck('product_id')->filter()->unique()->toArray();
$invProductsMap = $this->inventoryService->getProductsByIds($invProductIds)->keyBy('id');
$topInventoryValue = $topInventoryValueItems->map(function ($item) use ($invProductsMap) {
$product = $invProductsMap->get($item->product_id);
return [
'name' => $product ? $product->name : 'Unknown Product',
'code' => $product ? $product->code : '',
'value' => (int)$item->total_value,
];
});
// 熱銷數量排行 (Top Selling by Quantity)
$topSellingQtyItems = $this->salesService->getTopSellingByQuantity();
$qtyProductIds = $topSellingQtyItems->pluck('product_id')->filter()->unique()->toArray();
$qtyProductsMap = $this->inventoryService->getProductsByIds($qtyProductIds)->keyBy('id');
$topSellingByQuantity = $topSellingQtyItems->map(function ($item) use ($qtyProductsMap) {
$product = $qtyProductsMap->get($item->product_id);
return [
'name' => $product ? $product->name : $item->product_code,
'code' => $item->product_code,
'value' => (int)$item->total_quantity,
];
});
// 即將過期商品 (Expiring Soon)
$expiringItems = $this->inventoryService->getExpiringSoon();
$expiringProductIds = $expiringItems->pluck('product_id')->filter()->unique()->toArray();
$expiringProductsMap = $this->inventoryService->getProductsByIds($expiringProductIds)->keyBy('id');
$expiringSoon = $expiringItems->map(function ($item) use ($expiringProductsMap) {
$product = $expiringProductsMap->get($item->product_id);
return [
'name' => $product ? $product->name : 'Unknown Product',
'batch_number' => $item->batch_number,
'expiry_date' => $item->expiry_date->format('Y-m-d'),
'quantity' => (int)$item->quantity,
];
});
return Inertia::render('Dashboard', [
'stats' => $stats,
'stats' => [
'totalItems' => $invStats['productsCount'],
'lowStockCount' => $invStats['lowStockCount'],
'negativeCount' => $invStats['negativeCount'] ?? 0,
'expiringCount' => $invStats['expiringCount'] ?? 0,
'totalInventoryValue' => $invStats['totalInventoryValue'] ?? 0,
'thisMonthRevenue' => $thisMonthRevenue,
'pendingOrdersCount' => $procStats['pendingOrdersCount'] ?? 0,
'pendingTransferCount' => $invStats['pendingTransferCount'] ?? 0,
'pendingProductionCount' => $pendingProductionCount,
'todoCount' => ($procStats['pendingOrdersCount'] ?? 0) + ($invStats['pendingTransferCount'] ?? 0) + $pendingProductionCount,
'salesTrend' => $salesTrend,
'topSellingProducts' => $topSellingProducts,
'topInventoryValue' => $topInventoryValue,
'topSellingByQuantity' => $topSellingByQuantity,
'expiringSoon' => $expiringSoon,
],
]);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Modules\Core\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
/**
* Mark a specific notification as read.
*/
public function markAsRead(Request $request, string $id)
{
$notification = $request->user()->notifications()->findOrFail($id);
$notification->markAsRead();
return back();
}
/**
* Mark all notifications as read.
*/
public function markAllAsRead(Request $request)
{
$request->user()->unreadNotifications->markAsRead();
return back();
}
/**
* Check for new notifications.
*/
public function check(Request $request)
{
return response()->json([
'unread_count' => $request->user()->unreadNotifications()->count(),
'latest' => $request->user()->notifications()->latest()->limit(10)->get(),
]);
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Modules\Core\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Inertia\Inertia;

View File

@@ -123,7 +123,7 @@ class RoleController extends Controller
$role->syncPermissions($validated['permissions']);
}
return redirect()->route('roles.index')->with('success', '角色更新成功');
return back()->with('success', '角色更新成功');
}
/**
@@ -151,7 +151,7 @@ class RoleController extends Controller
*/
private function getGroupedPermissions()
{
$allPermissions = Permission::orderBy('name')->get();
$allPermissions = Permission::select('id', 'name', 'guard_name')->orderBy('name')->get();
$grouped = [];
foreach ($allPermissions as $permission) {
@@ -160,8 +160,13 @@ class RoleController extends Controller
$action = $parts[1] ?? '';
// 特定權限遷移邏輯
if ($permission->name === 'inventory.transfer') {
$group = 'warehouses'; // 調撥功能移至倉庫管理下
if ($permission->name === 'inventory.view_cost') {
$group = 'inventory';
}
// 移除不再使用的權限選項
if (in_array($permission->name, ['inventory.count', 'inventory.transfer'])) {
continue;
}
if (!isset($grouped[$group])) {
@@ -175,13 +180,29 @@ class RoleController extends Controller
$groupDefinitions = [
'products' => '商品資料管理',
'warehouses' => '倉庫管理',
'inventory' => '庫存管理',
'inventory' => '庫存資料管理',
'inventory_count' => '庫存盤點管理',
'inventory_adjust' => '庫存盤調管理',
'inventory_transfer' => '庫存調撥管理',
'inventory_report' => '庫存報表',
'inventory_traceability' => '批號溯源',
'vendors' => '廠商資料管理',
'purchase_orders' => '採購單管理',
'users' => '使用者管理',
'roles' => '角色與權限',
'purchase_returns' => '採購退回管理',
'goods_receipts' => '進貨單管理',
'delivery_notes' => '出貨單管理',
'recipes' => '配方管理',
'production_orders' => '生產工單管理',
'utility_fees' => '公共事業費管理',
'accounting' => '會計報表',
'account_payables' => '應付帳款',
'sales_imports' => '銷售單匯入管理',
'sales_orders' => '銷售訂單管理',
'store_requisitions' => '門市叫貨申請',
'procurement_analysis' => '採購統計分析',
'users' => '使用者管理',
'roles' => '角色與權限',
'system' => '系統管理',
];
$result = [];

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

View File

@@ -18,13 +18,35 @@ class UserController extends Controller
*/
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');
$sortOrder = $request->input('sort_order', 'asc');
$search = $request->input('search');
$roleId = $request->input('role');
$query = User::with(['roles:id,name,display_name']);
$roleId = $request->input('role');
$isActive = $request->input('is_active'); // 'all', '1', '0'
$query = User::query();
// 隱藏超級管理員:若非 super-admin則不可看到 super-admin 過往
if (!auth()->user()->hasRole('super-admin')) {
$query->whereDoesntHave('roles', function ($q) {
$q->where('name', 'super-admin');
});
// 預載入角色時也過濾掉 super-admin 標籤
$query->with(['roles' => function ($q) {
$q->select('id', 'name', 'display_name')
->where('name', '!=', 'super-admin');
}]);
} else {
$query->with(['roles:id,name,display_name']);
}
// 處理搜尋
if ($search) {
@@ -42,6 +64,11 @@ class UserController extends Controller
});
}
// 處理狀態篩選
if ($isActive !== null && $isActive !== 'all') {
$query->where('is_active', $isActive === '1' || $isActive === 'true');
}
// 處理排序
if (in_array($sortBy, ['name', 'created_at'])) {
$query->orderBy($sortBy, $sortOrder);
@@ -50,12 +77,19 @@ class UserController extends Controller
}
$users = $query->paginate($perPage)->withQueryString();
$roles = Role::select('id', 'name', 'display_name')->get();
// 只能看到自己權限以下的角色
$rolesQuery = Role::select('id', 'name', 'display_name');
if (!auth()->user()->hasRole('super-admin')) {
$rolesQuery->where('name', '!=', 'super-admin');
}
$roles = $rolesQuery->get();
return Inertia::render('Admin/User/Index', [
'users' => $users,
'users' => $users,
'roles' => $roles,
'filters' => $request->only(['per_page', 'sort_by', 'sort_order', 'search', 'role']),
'filters' => $request->only(['per_page', 'sort_by', 'sort_order', 'search', 'role', 'is_active']),
]);
}
@@ -64,7 +98,11 @@ class UserController extends Controller
*/
public function create()
{
$roles = Role::pluck('display_name', 'name');
$rolesQuery = Role::query();
if (!auth()->user()->hasRole('super-admin')) {
$rolesQuery->where('name', '!=', 'super-admin');
}
$roles = $rolesQuery->pluck('display_name', 'name');
return Inertia::render('Admin/User/Create', [
'roles' => $roles
@@ -80,8 +118,10 @@ class UserController extends Controller
'name' => ['required', 'string', 'max:255'],
'email' => ['nullable', 'string', 'email', 'max:255', 'unique:users'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'roles' => ['array'],
'is_active' => ['boolean'],
], [
'password.required' => '請輸入密碼',
'password.min' => '密碼長度至少需 :min 個字元',
@@ -92,10 +132,16 @@ class UserController extends Controller
'name' => $validated['name'],
'email' => $validated['email'],
'username' => $validated['username'],
'password' => Hash::make($validated['password']),
'is_active' => $request->boolean('is_active', true),
]);
if (!empty($validated['roles'])) {
// 安全檢查:非 super-admin 不能賦予 super-admin 角色
if (!auth()->user()->hasRole('super-admin') && in_array('super-admin', $validated['roles'])) {
abort(403, '您沒有權限指派系統管理員角色');
}
$user->syncRoles($validated['roles']);
// 更新 'created' 紀錄以包含角色資訊
@@ -123,7 +169,17 @@ class UserController extends Controller
public function edit(string $id)
{
$user = User::with('roles')->findOrFail($id);
$roles = Role::get(['id', 'name', 'display_name']);
// 安全檢查:非 super-admin 不能編輯 super-admin
if ($user->hasRole('super-admin') && !auth()->user()->hasRole('super-admin')) {
abort(403, '您沒有權限編輯系統管理員');
}
$rolesQuery = Role::select('id', 'name', 'display_name');
if (!auth()->user()->hasRole('super-admin')) {
$rolesQuery->where('name', '!=', 'super-admin');
}
$roles = $rolesQuery->get();
return Inertia::render('Admin/User/Edit', [
'user' => $user,
@@ -139,12 +195,19 @@ class UserController extends Controller
{
$user = User::findOrFail($id);
// 安全檢查:非 super-admin 不能更新 super-admin
if ($user->hasRole('super-admin') && !auth()->user()->hasRole('super-admin')) {
abort(403, '您沒有權限編輯系統管理員');
}
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'username' => ['required', 'string', 'max:255', Rule::unique('users')->ignore($user->id)],
'password' => ['nullable', 'string', 'min:8', 'confirmed'],
'roles' => ['array'],
'is_active' => ['boolean'],
], [
'password.min' => '密碼長度至少需 :min 個字元',
'password.confirmed' => '密碼確認不符',
@@ -157,10 +220,6 @@ class UserController extends Controller
'username' => $validated['username'],
];
if (!empty($validated['password'])) {
$userData['password'] = Hash::make($validated['password']);
}
$user->fill($userData);
// 捕捉變更屬性以進行手動記錄
@@ -179,6 +238,11 @@ class UserController extends Controller
// 2. 處理角色
$roleChanges = null;
if (isset($validated['roles'])) {
// 安全檢查:非 super-admin 不能賦予 super-admin 角色
if (!auth()->user()->hasRole('super-admin') && in_array('super-admin', $validated['roles'])) {
abort(403, '您沒有權限指派系統管理員角色');
}
$oldRoles = $user->roles()->pluck('display_name')->join(', ');
$user->syncRoles($validated['roles']);
$newRoles = $user->roles()->pluck('display_name')->join(', ');
@@ -230,6 +294,11 @@ class UserController extends Controller
{
$user = User::findOrFail($id);
// 安全檢查:非 super-admin 不能刪除 super-admin
if ($user->hasRole('super-admin') && !auth()->user()->hasRole('super-admin')) {
abort(403, '您沒有權限刪除系統管理員');
}
if ($user->hasRole('super-admin')) {
return back()->with('error', '無法刪除超級管理員帳號');
}
@@ -240,6 +309,46 @@ class UserController extends Controller
$user->delete();
return redirect()->route('users.index')->with('success', '使用者已刪除');
return redirect()->route('users.index')->with('success', "使用者「{$user->name}」已刪除");
}
/**
* 切換使用者啟用/停用狀態
*/
public function toggleActive(string $id)
{
$user = User::findOrFail($id);
// 安全檢查:不能停用自己
if ($user->id === auth()->id() && $user->is_active) {
return back()->with('error', '無法停用自己的帳號');
}
// 安全檢查:非 super-admin 不能停用 super-admin
if ($user->hasRole('super-admin') && !auth()->user()->hasRole('super-admin')) {
abort(403, '您沒有權限變更系統管理員狀態');
}
$oldStatus = $user->is_active;
$user->is_active = !$oldStatus;
$user->save();
// 記錄活動
activity()
->performedOn($user)
->causedBy(auth()->user())
->event('updated')
->withProperties([
'attributes' => ['is_active' => $user->is_active],
'old' => ['is_active' => $oldStatus],
'snapshot' => [
'name' => $user->name,
'username' => $user->username,
]
])
->log('updated');
$statusText = $user->is_active ? '已啟用' : '已停用';
return back()->with('success', "使用者「{$user->name}{$statusText}");
}
}

View 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 = [];
}
}

View File

@@ -10,10 +10,12 @@ use Spatie\Permission\Traits\HasRoles;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, HasRoles, LogsActivity;
use HasFactory, Notifiable, HasRoles, LogsActivity, HasApiTokens;
/**
* 可批量賦值的屬性。
@@ -35,6 +37,7 @@ class User extends Authenticatable
'email',
'username',
'password',
'is_active',
];
/**
@@ -56,7 +59,9 @@ class User extends Authenticatable
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_active' => 'boolean',
];
}

View File

@@ -7,6 +7,7 @@ use App\Modules\Core\Controllers\ProfileController;
use App\Modules\Core\Controllers\RoleController;
use App\Modules\Core\Controllers\UserController;
use App\Modules\Core\Controllers\ActivityLogController;
use App\Modules\Core\Controllers\SystemSettingController;
// 登入/登出路由
Route::get('/login', [LoginController::class, 'show'])->name('login');
@@ -14,6 +15,11 @@ Route::post('/login', [LoginController::class, 'store']);
Route::post('/logout', [LoginController::class, 'destroy'])->name('logout');
Route::middleware('auth')->group(function () {
// 通知
Route::post('/notifications/read-all', [\App\Modules\Core\Controllers\NotificationController::class, 'markAllAsRead'])->name('notifications.read-all');
Route::post('/notifications/{id}/read', [\App\Modules\Core\Controllers\NotificationController::class, 'markAsRead'])->name('notifications.read');
Route::get('/notifications/check', [\App\Modules\Core\Controllers\NotificationController::class, 'check'])->name('notifications.check');
// 儀表板 - 所有登入使用者皆可存取
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
@@ -43,6 +49,7 @@ Route::middleware('auth')->group(function () {
});
Route::get('/users/{user}/edit', [UserController::class, 'edit'])->middleware('permission:users.edit')->name('users.edit');
Route::put('/users/{user}', [UserController::class, 'update'])->middleware('permission:users.edit')->name('users.update');
Route::patch('/users/{user}/toggle-active', [UserController::class, 'toggleActive'])->middleware('permission:users.activate')->name('users.toggle-active');
Route::delete('/users/{user}', [UserController::class, 'destroy'])->middleware('permission:users.delete')->name('users.destroy');
});
@@ -50,5 +57,11 @@ Route::middleware('auth')->group(function () {
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');
});
});
});

View File

@@ -16,7 +16,7 @@ class CoreService implements CoreServiceInterface
*/
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
{
return User::all();
return User::select('id', 'name')->get();
}
public function ensureSystemUserExists()

View File

@@ -0,0 +1,160 @@
<?php
namespace App\Modules\Finance\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Finance\Models\AccountPayable;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
class AccountPayableController extends Controller
{
protected $procurementService;
protected $inventoryService;
public function __construct(
ProcurementServiceInterface $procurementService,
InventoryServiceInterface $inventoryService
) {
$this->procurementService = $procurementService;
$this->inventoryService = $inventoryService;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$query = AccountPayable::with(['creator']);
// 關鍵字搜尋 (單號、供應商名稱)
if ($request->filled('search')) {
$search = $request->search;
// 透過 ProcurementService 查詢符合關鍵字的 Vendor IDs
$matchedVendors = $this->procurementService->searchVendors($search);
$vendorIds = $matchedVendors->pluck('id')->toArray();
$query->where(function ($q) use ($search, $vendorIds) {
$q->where('document_number', 'like', "%{$search}%");
if (!empty($vendorIds)) {
$q->orWhereIn('vendor_id', $vendorIds);
}
});
}
// 狀態過濾
if ($request->filled('status') && $request->status !== 'all') {
$query->where('status', $request->status);
}
// 供應商過濾
if ($request->filled('vendor_id') && $request->vendor_id !== 'all') {
$query->where('vendor_id', $request->vendor_id);
}
// 日期區間過濾
if ($request->filled('date_start')) {
$query->where('due_date', '>=', $request->date_start);
}
if ($request->filled('date_end')) {
$query->where('due_date', '<=', $request->date_end);
}
$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();
// Manual Hydration for Vendors
$allVendorIds = collect($payables->items())->pluck('vendor_id')->unique()->filter()->toArray();
$vendorsMap = $this->procurementService->getVendorsByIds($allVendorIds)->keyBy('id');
$payables->getCollection()->transform(function ($item) use ($vendorsMap) {
$item->vendor = $vendorsMap->get($item->vendor_id);
return $item;
});
$vendors = $this->procurementService->getAllVendors();
return Inertia::render('AccountPayable/Index', [
'payables' => $payables,
'filters' => $request->all(['search', 'status', 'vendor_id', 'date_start', 'date_end', 'per_page']),
'vendors' => $vendors,
]);
}
/**
* Display the specified resource.
*/
public function show(AccountPayable $accountPayable)
{
$accountPayable->load(['creator']);
if ($accountPayable->vendor_id) {
$accountPayable->vendor = $this->procurementService->getVendorsByIds([$accountPayable->vendor_id])->first();
}
// 嘗試加載來源單據資訊 (目前支援 goods_receipt)
$sourceDocumentCode = null;
if ($accountPayable->source_document_type === 'goods_receipt') {
$receiptData = app(\App\Modules\Inventory\Contracts\GoodsReceiptServiceInterface::class)
->getGoodsReceiptData($accountPayable->source_document_id);
if ($receiptData) {
$sourceDocumentCode = $receiptData['code'] ?? null;
}
}
return Inertia::render('AccountPayable/Show', [
// 將 model 轉換成 array 加入額外資訊
'payable' => array_merge($accountPayable->toArray(), ['source_document_code' => $sourceDocumentCode]),
]);
}
/**
* 更新發票資訊
*/
public function updateInvoice(Request $request, AccountPayable $accountPayable)
{
$validated = $request->validate([
'invoice_number' => 'nullable|string|max:50',
'invoice_date' => 'nullable|date',
]);
$accountPayable->update([
'invoice_number' => $validated['invoice_number'],
'invoice_date' => $validated['invoice_date'],
]);
return back()->with('success', '發票資訊已更新');
}
/**
* 標記已付款
*/
public function pay(Request $request, AccountPayable $accountPayable)
{
$validated = $request->validate([
'payment_method' => 'required|string|max:50',
'paid_at' => 'required|date',
'payment_note' => 'nullable|string|max:255',
]);
if ($accountPayable->status === AccountPayable::STATUS_PAID) {
return back()->with('error', '該帳款已經標記為已付款');
}
$accountPayable->update([
'status' => AccountPayable::STATUS_PAID,
'payment_method' => $validated['payment_method'],
'paid_at' => $validated['paid_at'],
'payment_note' => $validated['payment_note'],
]);
return back()->with('success', '帳款已成功標記為已付款');
}
}

View File

@@ -27,7 +27,11 @@ class AccountingReportController extends Controller
$allRecords = $reportData['records'];
// 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);
$offset = ($page - 1) * $perPage;
@@ -65,14 +69,25 @@ class AccountingReportController extends Controller
}
$exportData = $allRecords->map(function ($record) {
$taxAmount = (float)($record['tax_amount'] ?? 0);
$totalAmount = (float)($record['amount'] ?? 0);
$untaxedAmount = $totalAmount - $taxAmount;
return [
$record['date'],
$record['source'],
$record['category'],
$record['item'],
$record['reference'],
$record['invoice_number'],
$record['amount'],
$record['invoice_date'] ?? '-',
$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
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
fputcsv($file, ['日期', '來源', '類別', '項目', '參考單號', '發票號碼', '金額']);
fputcsv($file, [
'日期', '來源', '類別', '項目', '參考單號',
'發票日期', '發票號碼', '未稅金額', '稅額', '總金額',
'付款方式', '付款備註', '內部備註', '狀態'
]);
foreach ($exportData as $row) {
fputcsv($file, $row);

View File

@@ -4,8 +4,10 @@ namespace App\Modules\Finance\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Finance\Models\UtilityFee;
use App\Modules\Finance\Models\UtilityFeeAttachment;
use App\Modules\Finance\Contracts\FinanceServiceInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
class UtilityFeeController extends Controller
@@ -34,13 +36,16 @@ class UtilityFeeController extends Controller
public function store(Request $request)
{
$validated = $request->validate([
'transaction_date' => 'required|date',
'transaction_date' => 'nullable|date',
'due_date' => 'required|date',
'category' => 'required|string|max:255',
'amount' => 'required|numeric|min:0',
'invoice_number' => 'nullable|string|max:255',
'description' => 'nullable|string',
]);
$validated['payment_status'] = $this->determineStatus($validated);
$fee = UtilityFee::create($validated);
activity()
@@ -55,13 +60,16 @@ class UtilityFeeController extends Controller
public function update(Request $request, UtilityFee $utility_fee)
{
$validated = $request->validate([
'transaction_date' => 'required|date',
'transaction_date' => 'nullable|date',
'due_date' => 'required|date',
'category' => 'required|string|max:255',
'amount' => 'required|numeric|min:0',
'invoice_number' => 'nullable|string|max:255',
'description' => 'nullable|string',
]);
$validated['payment_status'] = $this->determineStatus($validated);
$utility_fee->update($validated);
activity()
@@ -73,6 +81,22 @@ class UtilityFeeController extends Controller
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)
{
activity()
@@ -81,8 +105,82 @@ class UtilityFeeController extends Controller
->event('deleted')
->log('deleted');
// 刪除實體檔案 (如果 cascade 沒處理或是想要手動清理)
foreach ($utility_fee->attachments as $attachment) {
Storage::disk('public')->delete($attachment->file_path);
}
$utility_fee->delete();
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' => '刪除成功']);
}
}

View File

@@ -15,6 +15,9 @@ class FinanceServiceProvider extends ServiceProvider
public function boot(): void
{
//
\Illuminate\Support\Facades\Event::listen(
\App\Modules\Inventory\Events\GoodsReceiptApprovedEvent::class,
\App\Modules\Finance\Listeners\CreateAccountPayableFromGoodsReceipt::class
);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Modules\Finance\Listeners;
use App\Modules\Inventory\Events\GoodsReceiptApprovedEvent;
use App\Modules\Finance\Services\AccountPayableService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
class CreateAccountPayableFromGoodsReceipt
{
protected AccountPayableService $accountPayableService;
/**
* Create the event listener.
*/
public function __construct(AccountPayableService $accountPayableService)
{
$this->accountPayableService = $accountPayableService;
}
/**
* Handle the event.
*/
public function handle(GoodsReceiptApprovedEvent $event): void
{
try {
// 目前使用系統預設 User ID 或 0 作為自動生成的建立者,若能從 event 取得更好
$userId = auth()->id() ?? 1; // 假設 1 為系統管理員或預設使用者
$this->accountPayableService->createFromGoodsReceipt($event->goodsReceiptId, $userId);
Log::info("已成功為進貨單 ID: {$event->goodsReceiptId} 建立應付帳款");
} catch (\Exception $e) {
Log::error("建立應付帳款失敗 (進貨單 ID: {$event->goodsReceiptId}): " . $e->getMessage());
// 根據需求決定是否拋出 exception 或只記錄 log
throw $e;
}
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Modules\Finance\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class AccountPayable extends Model
{
use HasFactory;
public const STATUS_PENDING = 'pending';
public const STATUS_PARTIALLY_PAID = 'partially_paid';
public const STATUS_PAID = 'paid';
public const STATUS_CANCELLED = 'cancelled';
protected $fillable = [
'vendor_id',
'source_document_type',
'source_document_id',
'document_number',
'total_amount',
'tax_amount',
'status',
'due_date',
'invoice_number',
'invoice_date',
'paid_at',
'payment_method',
'payment_note',
'remarks',
'created_by',
];
protected $casts = [
'total_amount' => 'decimal:2',
'tax_amount' => 'decimal:2',
'due_date' => 'date',
'invoice_date' => 'date',
'paid_at' => 'datetime',
];
// vendor 關聯移至 service (跨模組)
/**
* 關聯:建立者
* @return BelongsTo
*/
public function creator(): BelongsTo
{
return $this->belongsTo(\App\Modules\Core\Models\User::class, 'created_by');
}
}

View File

@@ -7,29 +7,47 @@ use Illuminate\Database\Eloquent\Model;
class UtilityFee extends Model
{
/** @use HasFactory<\Database\Factories\UtilityFeeFactory> */
use HasFactory;
/**
* 此公共事業費的附件
*/
public function attachments()
{
return $this->hasMany(UtilityFeeAttachment::class);
}
// 狀態常數
const STATUS_PENDING = 'pending';
const STATUS_PAID = 'paid';
const STATUS_OVERDUE = 'overdue';
protected $fillable = [
'transaction_date',
'due_date',
'category',
'amount',
'payment_status',
'invoice_number',
'description',
];
protected $casts = [
'transaction_date' => 'date',
'transaction_date' => 'date:Y-m-d',
'due_date' => 'date:Y-m-d',
'amount' => 'decimal:2',
];
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$activity->properties = $activity->properties->put('snapshot', [
'transaction_date' => $this->transaction_date->format('Y-m-d'),
$snapshot = [
'transaction_date' => $this->transaction_date?->format('Y-m-d'),
'due_date' => $this->due_date?->format('Y-m-d'),
'category' => $this->category,
'amount' => $this->amount,
'payment_status' => $this->payment_status,
'invoice_number' => $this->invoice_number,
]);
];
$activity->properties = $activity->properties->put('snapshot', $snapshot);
}
}

View 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);
}
}

View File

@@ -4,7 +4,23 @@ use Illuminate\Support\Facades\Route;
use App\Modules\Finance\Controllers\UtilityFeeController;
use App\Modules\Finance\Controllers\AccountingReportController;
use App\Modules\Finance\Controllers\AccountPayableController;
Route::middleware('auth')->group(function () {
// 應付帳款
Route::group(['prefix' => 'finance'], function () {
Route::middleware('permission:account_payables.view')->group(function () {
Route::get('/account-payables', [AccountPayableController::class, 'index'])->name('account-payables.index');
Route::get('/account-payables/{accountPayable}', [AccountPayableController::class, 'show'])->name('account-payables.show');
});
Route::middleware('permission:account_payables.edit')->group(function () {
Route::post('/account-payables/{accountPayable}/invoice', [AccountPayableController::class, 'updateInvoice'])->name('account-payables.invoice');
});
Route::middleware('permission:account_payables.pay')->group(function () {
Route::post('/account-payables/{accountPayable}/pay', [AccountPayableController::class, 'pay'])->name('account-payables.pay');
});
});
// 公共事業費管理
Route::middleware('permission:utility_fees.view')->group(function () {
Route::get('/utility-fees', [UtilityFeeController::class, 'index'])->name('utility-fees.index');
@@ -14,6 +30,11 @@ Route::middleware('auth')->group(function () {
});
Route::middleware('permission:utility_fees.edit')->group(function () {
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::delete('/utility-fees/{utility_fee}', [UtilityFeeController::class, 'destroy'])->name('utility-fees.destroy');

View File

@@ -0,0 +1,86 @@
<?php
namespace App\Modules\Finance\Services;
use App\Modules\Finance\Models\AccountPayable;
use Illuminate\Support\Facades\DB;
use App\Modules\Inventory\Contracts\GoodsReceiptServiceInterface;
class AccountPayableService
{
protected GoodsReceiptServiceInterface $goodsReceiptService;
public function __construct(GoodsReceiptServiceInterface $goodsReceiptService)
{
$this->goodsReceiptService = $goodsReceiptService;
}
/**
* 根據進貨單建立應付帳款
*
* @param int $goodsReceiptId
* @param int $userId 執行操作的使用者 ID
* @return AccountPayable
* @throws \Exception
*/
public function createFromGoodsReceipt(int $goodsReceiptId, int $userId): AccountPayable
{
// 透過 Contract 取得 Inventory 模組的資料,避免直接依賴 Model
$receiptData = $this->goodsReceiptService->getGoodsReceiptData($goodsReceiptId);
if (!$receiptData) {
throw new \Exception("找不到對應的進貨單資料 (ID: {$goodsReceiptId})");
}
// 檢查是否已經建立過(密等性)
$existingAp = AccountPayable::where('source_document_type', 'goods_receipt')
->where('source_document_id', $goodsReceiptId)
->first();
if ($existingAp) {
return $existingAp;
}
return DB::transaction(function () use ($receiptData, $userId) {
$ap = AccountPayable::create([
'vendor_id' => $receiptData['vendor_id'],
'source_document_type' => 'goods_receipt',
'source_document_id' => $receiptData['id'],
'document_number' => $this->generateApNumber(),
'total_amount' => collect($receiptData['items'] ?? [])->sum('total_amount'),
'tax_amount' => 0, // 假設後續會實作稅額計算,目前預設為 0
'status' => AccountPayable::STATUS_PENDING,
// 設定應付日期,預設為進貨後天數 (由系統設定決定,預設 30 天)
'due_date' => now()->addDays(\App\Modules\Core\Models\SystemSetting::getVal('finance.ap_payment_days', 30))->toDateString(),
'created_by' => $userId,
'remarks' => "由進貨單 {$receiptData['code']} 自動生成",
]);
return $ap;
});
}
/**
* 產生應付帳款單號
*/
protected function generateApNumber(): string
{
$prefix = 'AP-' . date('Ymd') . '-';
$lastPrefix = "{$prefix}%";
$latest = AccountPayable::where('document_number', 'like', $lastPrefix)
->orderBy('document_number', 'desc')
->lockForUpdate()
->first();
if (!$latest) {
return $prefix . '01';
}
$parts = explode('-', $latest->document_number);
$lastNumber = intval(end($parts));
$newNumber = str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
return $prefix . $newNumber;
}
}

View File

@@ -19,23 +19,48 @@ class FinanceService implements FinanceServiceInterface
public function getAccountingReportData(string $start, string $end): array
{
// 1. 獲取採購單資料
$purchaseOrders = $this->procurementService->getPurchaseOrdersByDate($start, $end)
->map(function ($po) {
return [
'id' => 'PO-' . $po->id,
'date' => Carbon::parse($po->created_at)->timezone(config('app.timezone'))->toDateString(),
'source' => '採購單',
'category' => '進貨支出',
'item' => $po->vendor->name ?? '未知廠商',
'reference' => $po->code,
'invoice_number' => $po->invoice_number,
'amount' => (float)$po->grand_total,
];
});
// 1. 獲取應付帳款資料 (已付款)
$accountPayables = \App\Modules\Finance\Models\AccountPayable::where('status', \App\Modules\Finance\Models\AccountPayable::STATUS_PAID)
->whereNotNull('paid_at')
->whereBetween('paid_at', [$start, $end])
->get();
// 2. 獲取公共事業費 (注意:目前資料表欄位為 transaction_date)
$utilityFees = UtilityFee::whereBetween('transaction_date', [$start, $end])
// 取得供應商資料 (Manual Hydration)
$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()
->map(function ($fee) {
return [
@@ -45,12 +70,18 @@ class FinanceService implements FinanceServiceInterface
'category' => $fee->category,
'item' => $fee->description ?: $fee->category,
'reference' => '-',
'invoice_date' => null,
'invoice_number' => $fee->invoice_number,
'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')
->values();
@@ -58,7 +89,7 @@ class FinanceService implements FinanceServiceInterface
'records' => $allRecords,
'summary' => [
'total_amount' => $allRecords->sum('amount'),
'purchase_total' => $purchaseOrders->sum('amount'),
'payable_total' => $payableRecords->sum('amount'),
'utility_total' => $utilityFees->sum('amount'),
'record_count' => $allRecords->count(),
]
@@ -67,7 +98,7 @@ class FinanceService implements FinanceServiceInterface
public function getUtilityFees(array $filters)
{
$query = UtilityFee::query();
$query = UtilityFee::withCount('attachments');
if (!empty($filters['search'])) {
$search = $filters['search'];
@@ -94,7 +125,13 @@ class FinanceService implements FinanceServiceInterface
$sortDirection = $filters['sort_direction'] ?? 'desc';
$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

View File

@@ -0,0 +1,175 @@
<?php
namespace App\Modules\Integration\Actions;
use App\Modules\Integration\Models\SalesOrder;
use App\Modules\Integration\Models\SalesOrderItem;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Contracts\ProductServiceInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;
class SyncOrderAction
{
protected $inventoryService;
protected $productService;
public function __construct(
InventoryServiceInterface $inventoryService,
ProductServiceInterface $productService
) {
$this->inventoryService = $inventoryService;
$this->productService = $productService;
}
/**
* 執行訂單同步
*
* @param array $data
* @return array 包含 orders 建立結果的資訊
* @throws ValidationException
* @throws \Exception
*/
public function execute(array $data)
{
$externalOrderId = $data['external_order_id'];
// 使用 Cache::lock 防護高併發,鎖定該訂單號 10 秒
// 此處需要 cache store 支援鎖 (如 memcached, dynamodb, redis, database, file, array)
// Laravel 預設的 file/redis 都支援。若無法取得鎖,表示有另一個相同的請求正在處理
$lock = Cache::lock("sync_order_{$externalOrderId}", 10);
if (!$lock->get()) {
throw ValidationException::withMessages([
'external_order_id' => ["The order {$externalOrderId} is currently being processed by another transaction. Please try again later."]
]);
}
try {
// 冪等性處理:若訂單已存在,回傳已建立的訂單資訊
$existingOrder = SalesOrder::where('external_order_id', $externalOrderId)->first();
if ($existingOrder) {
return [
'status' => 'exists',
'message' => 'Order already exists',
'order_id' => $existingOrder->id,
];
}
// --- 預檢 (Pre-flight check) 僅使用 product_id ---
$items = $data['items'];
$targetErpIds = array_column($items, 'product_id');
// 一次性查出所有相關的 Product
$productsById = $this->productService->findByIds($targetErpIds)->keyBy('id');
$resolvedProducts = [];
$missingIds = [];
foreach ($items as $index => $item) {
$productId = $item['product_id'];
$product = $productsById->get($productId);
if ($product) {
$resolvedProducts[$index] = $product;
} else {
$missingIds[] = $productId;
}
}
if (!empty($missingIds)) {
throw ValidationException::withMessages([
'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, $resolvedProducts) {
// 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([
'external_order_id' => $data['external_order_id'],
'name' => $data['name'],
'status' => 'completed',
'payment_method' => $data['payment_method'] ?? 'cash',
'total_amount' => $data['total_amount'],
'total_qty' => $data['total_qty'],
'sold_at' => $data['sold_at'] ?? now(),
'raw_payload' => $data,
'source' => $source,
'source_label' => $data['source_label'] ?? null,
]);
$totalAmount = 0;
// 3. 處理訂單明細
$orderItemsData = [];
foreach ($items as $index => $itemData) {
$product = $resolvedProducts[$index];
$qty = $itemData['qty'];
$price = $itemData['price'];
$batchNumber = $itemData['batch_number'] ?? null;
$lineTotal = $qty * $price;
$totalAmount += $lineTotal;
$orderItemsData[] = [
'sales_order_id' => $order->id,
'product_id' => $product->id,
'product_name' => $product->name,
'quantity' => $qty,
'price' => $price,
'total' => $lineTotal,
'created_at' => now(),
'updated_at' => now(),
];
// 4. 扣除庫存(強制模式,允許負庫存)
$this->inventoryService->decreaseStock(
$product->id,
$warehouseId,
$qty,
"POS Order: " . $order->external_order_id,
true,
null, // Slot (location)
\App\Modules\Integration\Models\SalesOrder::class,
$order->id,
$batchNumber
);
}
// Batch insert order items
SalesOrderItem::insert($orderItemsData);
$order->update(['total_amount' => $totalAmount]);
return [
'status' => 'created',
'message' => 'Order synced and stock deducted successfully',
'order_id' => $order->id,
];
});
return $result;
} finally {
// 無論成功失敗,最後釋放鎖定
$lock->release();
}
}
}

View File

@@ -0,0 +1,157 @@
<?php
namespace App\Modules\Integration\Actions;
use App\Modules\Integration\Models\SalesOrder;
use App\Modules\Integration\Models\SalesOrderItem;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Contracts\ProductServiceInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;
class SyncVendingOrderAction
{
protected $inventoryService;
protected $productService;
public function __construct(
InventoryServiceInterface $inventoryService,
ProductServiceInterface $productService
) {
$this->inventoryService = $inventoryService;
$this->productService = $productService;
}
/**
* 執行販賣機訂單同步
*
* @param array $data
* @return array 包含訂單建立結果的資訊
* @throws ValidationException
* @throws \Exception
*/
public function execute(array $data)
{
$externalOrderId = $data['external_order_id'];
// 使用 Cache::lock 防護高併發
$lock = Cache::lock("sync_order_{$externalOrderId}", 10);
if (!$lock->get()) {
throw ValidationException::withMessages([
'external_order_id' => ["The order {$externalOrderId} is currently being processed by another transaction. Please try again later."]
]);
}
try {
// 冪等性處理:若訂單已存在,回傳已建立的訂單資訊
$existingOrder = SalesOrder::where('external_order_id', $externalOrderId)->first();
if ($existingOrder) {
return [
'status' => 'exists',
'message' => 'Order already exists',
'order_id' => $existingOrder->id,
];
}
// --- 預檢:以 ERP 商品代碼查詢 ---
$items = $data['items'];
$productCodes = array_column($items, 'product_code');
// 一次性查出所有相關的 Product以 code 查詢)
$products = $this->productService->findByCodes($productCodes)->keyBy('code');
$missingCodes = [];
foreach ($productCodes as $code) {
if (!$products->has($code)) {
$missingCodes[] = $code;
}
}
if (!empty($missingCodes)) {
throw ValidationException::withMessages([
'items' => ["The following products are not found by code: " . implode(', ', $missingCodes) . ". Please ensure these products exist in the system."]
]);
}
// --- 執行寫入交易 ---
$result = DB::transaction(function () use ($data, $items, $products) {
// 1. 建立訂單
$order = SalesOrder::create([
'external_order_id' => $data['external_order_id'],
'status' => 'completed',
'payment_method' => $data['payment_method'] ?? 'electronic',
'total_amount' => 0,
'sold_at' => $data['sold_at'] ?? now(),
'raw_payload' => $data,
'source' => 'vending',
'source_label' => $data['machine_id'] ?? null,
]);
// 2. 查找倉庫
$warehouseCode = $data['warehouse_code'];
$warehouses = $this->inventoryService->getWarehousesByCodes([$warehouseCode]);
if ($warehouses->isEmpty()) {
throw ValidationException::withMessages([
'warehouse_code' => ["Warehouse with code {$warehouseCode} not found."]
]);
}
$warehouseId = $warehouses->first()->id;
$totalAmount = 0;
// 3. 處理訂單明細
$orderItemsData = [];
foreach ($items as $itemData) {
$product = $products->get($itemData['product_code']);
$qty = $itemData['qty'];
$price = $itemData['price'];
$lineTotal = $qty * $price;
$totalAmount += $lineTotal;
$orderItemsData[] = [
'sales_order_id' => $order->id,
'product_id' => $product->id,
'product_name' => $product->name,
'quantity' => $qty,
'price' => $price,
'total' => $lineTotal,
'created_at' => now(),
'updated_at' => now(),
];
// 4. 扣除庫存(強制模式,允許負庫存)
$this->inventoryService->decreaseStock(
$product->id,
$warehouseId,
$qty,
"Vending Order: " . $order->external_order_id,
true,
null,
\App\Modules\Integration\Models\SalesOrder::class,
$order->id
);
}
// Batch insert order items
SalesOrderItem::insert($orderItemsData);
$order->update(['total_amount' => $totalAmount]);
return [
'status' => 'created',
'message' => 'Vending order synced and stock deducted successfully',
'order_id' => $order->id,
];
});
return $result;
} finally {
$lock->release();
}
}
}

View File

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

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Modules\Integration\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Integration\Requests\SyncOrderRequest;
use App\Modules\Integration\Actions\SyncOrderAction;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
class OrderSyncController extends Controller
{
protected $syncOrderAction;
public function __construct(SyncOrderAction $syncOrderAction)
{
$this->syncOrderAction = $syncOrderAction;
}
/**
* 接收並同步外部交易訂單
*
* @param SyncOrderRequest $request
* @return JsonResponse
*/
public function store(SyncOrderRequest $request): JsonResponse
{
try {
// 所有驗證皆已透過 SyncOrderRequest 自動處理
// 將通過驗證的資料交由 Action 處理(包含併發鎖、預先驗證、與資料庫異動)
$result = $this->syncOrderAction->execute($request->validated());
$statusCode = ($result['status'] === 'exists') ? 200 : 201;
return response()->json([
'message' => $result['message'],
'order_id' => $result['order_id'] ?? null,
], $statusCode);
} catch (\Illuminate\Validation\ValidationException $e) {
// 捕捉 Action 中拋出的預先驗證錯誤 (如查無商品、或鎖定逾時)
return response()->json([
'message' => 'Validation failed',
'errors' => $e->errors()
], 422);
} catch (\Exception $e) {
// 系統層級的錯誤
Log::error('Order Sync Failed', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'payload' => $request->all()
]);
return response()->json([
'message' => 'Sync failed: An unexpected error occurred.'
], 500);
}
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace App\Modules\Integration\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\Inventory\Contracts\ProductServiceInterface;
use Illuminate\Support\Facades\Log;
class ProductSyncController extends Controller
{
protected $productService;
public function __construct(ProductServiceInterface $productService)
{
$this->productService = $productService;
}
public function upsert(Request $request)
{
$request->validate([
'external_pos_id' => 'required|string|max:255',
'name' => 'required|string|max:255',
'price' => 'nullable|numeric|min:0|max:99999999.99',
'barcode' => 'nullable|string|max:100',
'category' => 'required|string|max:100',
'unit' => 'required|string|max:100',
'brand' => 'nullable|string|max:100',
'specification' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0|max:99999999.99',
'member_price' => 'nullable|numeric|min:0|max:99999999.99',
'wholesale_price' => 'nullable|numeric|min:0|max:99999999.99',
'updated_at' => 'nullable|date',
]);
try {
$product = $this->productService->upsertFromPos($request->all());
return response()->json([
'message' => 'Product synced successfully',
'data' => [
'id' => $product->id,
'external_pos_id' => $product->external_pos_id,
'code' => $product->code,
'barcode' => $product->barcode,
]
]);
} catch (\Exception $e) {
Log::error('Product Sync Failed', ['error' => $e->getMessage(), 'payload' => $request->all()]);
return response()->json([
'message' => 'Sync failed: ' . $e->getMessage(),
], 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);
}
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Modules\Integration\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Integration\Models\SalesOrder;
use Illuminate\Http\Request;
use Inertia\Inertia;
class SalesOrderController extends Controller
{
/**
* 顯示銷售訂單列表
*/
public function index(Request $request)
{
$query = SalesOrder::query();
// 搜尋篩選 (外部訂單號)
if ($request->filled('search')) {
$query->where(function ($q) use ($request) {
$q->where('external_order_id', 'like', '%' . $request->search . '%')
->orWhere('name', 'like', '%' . $request->search . '%');
});
}
// 來源篩選
if ($request->filled('source')) {
$query->where('source', $request->source);
}
// 付款方式篩選
if ($request->filled('payment_method')) {
$query->where('payment_method', $request->payment_method);
}
// 排序
$query->orderBy('sold_at', 'desc');
$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();
return Inertia::render('Integration/SalesOrders/Index', [
'orders' => $orders,
'filters' => $request->only(['search', 'per_page', 'source', 'status', 'payment_method']),
]);
}
/**
* 顯示單一銷售訂單詳情
*/
public function show(SalesOrder $salesOrder)
{
$salesOrder->load(['items']);
return Inertia::render('Integration/SalesOrders/Show', [
'order' => $salesOrder,
]);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Modules\Integration\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Integration\Requests\SyncVendingOrderRequest;
use App\Modules\Integration\Actions\SyncVendingOrderAction;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
class VendingOrderSyncController extends Controller
{
protected $syncVendingOrderAction;
public function __construct(SyncVendingOrderAction $syncVendingOrderAction)
{
$this->syncVendingOrderAction = $syncVendingOrderAction;
}
/**
* 接收並同步販賣機交易訂單
*
* @param SyncVendingOrderRequest $request
* @return JsonResponse
*/
public function store(SyncVendingOrderRequest $request): JsonResponse
{
try {
$result = $this->syncVendingOrderAction->execute($request->validated());
$statusCode = ($result['status'] === 'exists') ? 200 : 201;
return response()->json([
'message' => $result['message'],
'order_id' => $result['order_id'] ?? null,
], $statusCode);
} catch (\Illuminate\Validation\ValidationException $e) {
return response()->json([
'message' => 'Validation failed',
'errors' => $e->errors()
], 422);
} catch (\Exception $e) {
Log::error('Vending Order Sync Failed', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'payload' => $request->all()
]);
return response()->json([
'message' => 'Sync failed: An unexpected error occurred.'
], 500);
}
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Modules\Integration;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use App\Modules\Integration\Middleware\TenantIdentificationMiddleware;
class IntegrationServiceProvider extends ServiceProvider
{
public function boot()
{
$this->loadRoutesFrom(__DIR__ . '/Routes/api.php');
$this->loadRoutesFrom(__DIR__ . '/Routes/web.php');
$this->loadMigrationsFrom(__DIR__ . '/Database/Migrations');
// 註冊 Middleware 別名
Route::aliasMiddleware('integration.tenant', TenantIdentificationMiddleware::class);
// 定義 Integration API 速率限制(每分鐘 60 次,依 Token 使用者識別)
RateLimiter::for('integration', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
}
public function register()
{
//
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Modules\Integration\Middleware;
use Closure;
use Illuminate\Http\Request;
use Stancl\Tenancy\Facades\Tenancy;
use Symfony\Component\HttpFoundation\Response;
class TenantIdentificationMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// 1. Check for X-Tenant-Domain header
$domain = $request->header('X-Tenant-Domain');
if (! $domain) {
return response()->json([
'message' => 'Missing X-Tenant-Domain header.',
], 400);
}
// 2. Find Tenant by domain
// Assuming domains are stored in 'domains' table and linked to tenants
// Or using Stancl's tenant finder.
// Stancl Tenancy usually finds by domain automatically for web routes, but for API
// we are doing manual identification because we might not be using subdomains for API calls (or maybe we are).
// If the API endpoint is centrally hosted (e.g. api.star-erp.com/v1/...), we need this header.
// Let's try to initialize tenancy manually.
// We need to find the tenant model that has this domain.
try {
$tenant = \App\Modules\Core\Models\Tenant::whereHas('domains', function ($query) use ($domain) {
$query->where('domain', $domain);
})->first();
if (! $tenant) {
return response()->json([
'message' => 'Tenant not found.',
], 404);
}
Tenancy::initialize($tenant);
} catch (\Exception $e) {
return response()->json([
'message' => 'Tenant initialization failed: ' . $e->getMessage(),
], 500);
}
return $next($request);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Modules\Integration\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class SalesOrder extends Model
{
protected $table = 'sales_orders';
protected $fillable = [
'external_order_id',
'name',
'status',
'payment_method',
'total_amount',
'total_qty',
'sold_at',
'raw_payload',
'source',
'source_label',
];
protected $casts = [
'sold_at' => 'datetime',
'raw_payload' => 'array',
'total_amount' => 'decimal:4',
'total_qty' => 'decimal:4',
];
public function items(): HasMany
{
return $this->hasMany(SalesOrderItem::class);
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Modules\Integration\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class SalesOrderItem extends Model
{
protected $table = 'sales_order_items';
protected $fillable = [
'sales_order_id',
'product_id',
'product_name',
'quantity',
'price',
'total',
];
protected $casts = [
'quantity' => 'decimal:4',
'price' => 'decimal:4',
'total' => 'decimal:4',
];
public function order(): BelongsTo
{
return $this->belongsTo(SalesOrder::class, 'sales_order_id');
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Modules\Integration\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SyncOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'external_order_id' => 'required|string',
'name' => 'required|string|max:255',
'warehouse_code' => 'required|string',
'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',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required|integer',
'items.*.batch_number' => 'nullable|string',
'items.*.qty' => 'required|numeric|min:0.0001',
'items.*.price' => 'required|numeric|min:0',
];
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Modules\Integration\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SyncVendingOrderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* 販賣機訂單同步的驗證規則
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'external_order_id' => 'required|string',
'machine_id' => 'nullable|string',
'warehouse_code' => 'required|string',
'payment_method' => 'nullable|string|in:cash,electronic,line_pay,other',
'sold_at' => 'nullable|date',
'items' => 'required|array|min:1',
'items.*.product_code' => 'required|string', // 使用 ERP 商品代碼
'items.*.qty' => 'required|numeric|min:0.0001',
'items.*.price' => 'required|numeric|min:0',
];
}
}

View File

@@ -0,0 +1,17 @@
<?php
use Illuminate\Support\Facades\Route;
use App\Modules\Integration\Controllers\ProductSyncController;
use App\Modules\Integration\Controllers\OrderSyncController;
use App\Modules\Integration\Controllers\VendingOrderSyncController;
use App\Modules\Integration\Controllers\InventorySyncController;
Route::prefix('api/v1/integration')
->middleware(['api', 'throttle:integration', 'integration.tenant', 'auth:sanctum'])
->group(function () {
Route::get('products', [ProductSyncController::class, 'index']);
Route::post('products/upsert', [ProductSyncController::class, 'upsert']);
Route::post('orders', [OrderSyncController::class, 'store']);
Route::post('vending/orders', [VendingOrderSyncController::class, 'store']);
Route::get('inventory/{warehouse_code}', [InventorySyncController::class, 'show']);
});

View File

@@ -0,0 +1,13 @@
<?php
use App\Modules\Integration\Controllers\SalesOrderController;
use Illuminate\Support\Facades\Route;
Route::middleware(['web', 'auth', 'verified'])->group(function () {
Route::prefix('integration')->name('integration.')->group(function () {
Route::middleware('permission:sales_orders.view')->group(function () {
Route::get('sales-orders', [SalesOrderController::class, 'index'])->name('sales-orders.index');
Route::get('sales-orders/{salesOrder}', [SalesOrderController::class, 'show'])->name('sales-orders.show');
});
});
});

View File

@@ -0,0 +1,14 @@
<?php
namespace App\Modules\Inventory\Contracts;
interface GoodsReceiptServiceInterface
{
/**
* 獲取指定的進貨單資訊
*
* @param int $goodsReceiptId
* @return array|null 返回進貨單的純陣列資料,若找不到則回傳 null
*/
public function getGoodsReceiptData(int $goodsReceiptId): ?array;
}

View File

@@ -15,15 +15,18 @@ interface InventoryServiceInterface
public function checkStock(int $productId, int $warehouseId, float $quantity): bool;
/**
* Decrease stock for a product (e.g., when an order is placed).
*
* @param int $productId
* @param int $warehouseId
* @param float $quantity
* @param string|null $reason
* @param bool $force
* @param string|null $slot
* @param string|null $referenceType
* @param int|string|null $referenceId
* @param string|null $batchNumber
* @return void
*/
public function decreaseStock(int $productId, int $warehouseId, float $quantity, ?string $reason = 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.
@@ -40,6 +43,14 @@ interface InventoryServiceInterface
*/
public function getProductsByIds(array $ids);
/**
* Get multiple warehouses by their codes.
*
* @param array $codes
* @return \Illuminate\Support\Collection
*/
public function getWarehousesByCodes(array $codes);
/**
* Search products by name.
*
@@ -106,10 +117,75 @@ interface InventoryServiceInterface
*/
public function decreaseInventoryQuantity(int $inventoryId, float $quantity, ?string $reason = null, ?string $referenceType = null, $referenceId = null);
/**
* Find a specific inventory record by warehouse, product and batch.
*
* @param int $warehouseId
* @param int $productId
* @param string|null $batchNumber
* @return object|null
*/
public function findInventoryByBatch(int $warehouseId, int $productId, ?string $batchNumber);
/**
* 取得即時庫存查詢資料(含統計卡片 + 分頁明細)。
*
* @param array $filters 篩選條件
* @param int $perPage 每頁筆數
* @return array
*/
public function getStockQueryData(array $filters = [], ?int $perPage = null): array;
/**
* Get statistics for the dashboard.
*
* @return array
*/
public function getDashboardStats(): array;
/**
* 依倉庫名稱查找或建立倉庫(供外部整合用)。
*
* @param string $warehouseName
* @return object
*/
public function findOrCreateWarehouseByName(string $warehouseName);
/**
* Get top inventory value for dashboard.
*/
public function getTopInventoryValue(int $limit = 5): \Illuminate\Support\Collection;
/**
* Get items expiring soon for dashboard.
*/
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;
}

View File

@@ -0,0 +1,98 @@
<?php
namespace App\Modules\Inventory\Contracts;
/**
* 產品服務介面 供跨模組使用(如 Integration 模組)。
*/
interface ProductServiceInterface
{
/**
* 透過外部 POS ID 進行產品新增或更新Upsert
*
* @param array $data
* @return object
*/
public function upsertFromPos(array $data);
/**
* 透過外部 POS ID 查找產品。
*
* @param string $externalPosId
* @return object|null
*/
public function findByExternalPosId(string $externalPosId);
/**
* 透過多個外部 POS ID 查找產品。
*
* @param array $externalPosIds
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByExternalPosIds(array $externalPosIds);
/**
* 透過多個 ERP 內部 ID 查找產品。
*
* @param array $ids
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByIds(array $ids);
/**
* 透過多個 ERP 商品代碼查找產品(供販賣機 API 使用)。
*
* @param array $codes
* @return \Illuminate\Database\Eloquent\Collection
*/
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);
}

View File

@@ -0,0 +1,246 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\InventoryAdjustDoc;
use App\Modules\Inventory\Models\InventoryCountDoc;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Services\AdjustService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
class AdjustDocController extends Controller
{
protected $adjustService;
public function __construct(AdjustService $adjustService)
{
$this->adjustService = $adjustService;
}
public function index(Request $request)
{
$query = InventoryAdjustDoc::query()
->with(['createdBy', 'postedBy', 'warehouse']);
// 搜尋
if ($request->filled('search')) {
$search = $request->search;
$query->where(function($q) use ($search) {
$q->where('doc_no', 'like', "%{$search}%")
->orWhere('reason', 'like', "%{$search}%")
->orWhere('remarks', 'like', "%{$search}%");
});
}
if ($request->filled('warehouse_id')) {
$query->where('warehouse_id', $request->warehouse_id);
}
$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')
->paginate($perPage)
->withQueryString()
->through(function ($doc) {
return [
'id' => (string) $doc->id,
'doc_no' => $doc->doc_no,
'status' => $doc->status,
'warehouse_name' => $doc->warehouse->name,
'reason' => $doc->reason,
'created_at' => $doc->created_at->format('Y-m-d H:i'),
'posted_at' => $doc->posted_at ? $doc->posted_at->format('Y-m-d H:i') : '-',
'created_by' => $doc->createdBy?->name,
'remarks' => $doc->remarks,
];
});
return Inertia::render('Inventory/Adjust/Index', [
'docs' => $docs,
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
]);
}
public function store(Request $request)
{
// 模式 1: 從盤點單建立
if ($request->filled('count_doc_id')) {
$countDoc = InventoryCountDoc::findOrFail($request->count_doc_id);
if ($countDoc->status !== 'completed') {
$errorMsg = $countDoc->status === 'no_adjust'
? '此盤點單無庫存差異,無需建立盤調單'
: '只有已完成盤點的單據可以建立盤調單';
return redirect()->back()->with('error', $errorMsg);
}
// 檢查是否已存在對應的盤調單 (避免重複建立)
if (InventoryAdjustDoc::where('count_doc_id', $countDoc->id)->exists()) {
return redirect()->back()->with('error', '此盤點單已建立過盤調單');
}
$doc = $this->adjustService->createFromCountDoc($countDoc, auth()->id());
return redirect()->route('inventory.adjust.show', [$doc->id])
->with('success', '已從盤點單生成盤調單');
}
// 模式 2: 一般手動調整 (保留原始邏輯但更新訊息)
$validated = $request->validate([
'warehouse_id' => 'required',
'reason' => 'required|string',
'remarks' => 'nullable|string',
]);
$doc = $this->adjustService->createDoc(
$validated['warehouse_id'],
$validated['reason'],
$validated['remarks'],
auth()->id()
);
return redirect()->route('inventory.adjust.show', [$doc->id])
->with('success', '已建立盤調單');
}
/**
* API: 獲取可盤調的已完成盤點單 (支援掃描單號)
*/
public function getPendingCounts(Request $request)
{
$query = InventoryCountDoc::where('status', 'completed')
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('inventory_adjust_docs')
->whereColumn('inventory_adjust_docs.count_doc_id', 'inventory_count_docs.id');
});
if ($request->filled('search')) {
$search = $request->search;
$query->where('doc_no', 'like', "%{$search}%");
}
$counts = $query->limit(10)->get()->map(function($c) {
return [
'id' => (string)$c->id,
'doc_no' => $c->doc_no,
'warehouse_name' => $c->warehouse->name,
'completed_at' => $c->completed_at->format('Y-m-d H:i'),
];
});
return response()->json($counts);
}
public function update(Request $request, InventoryAdjustDoc $doc)
{
$action = $request->input('action', 'update');
if ($action === 'post') {
if ($doc->status !== 'draft') {
return redirect()->back()->with('error', '只有草稿狀態的單據可以過帳');
}
$this->adjustService->post($doc, auth()->id());
return redirect()->back()->with('success', '單據已過帳');
}
if ($action === 'void') {
if ($doc->status !== 'draft') {
return redirect()->back()->with('error', '只有草稿狀態的單據可以作廢');
}
$this->adjustService->void($doc, auth()->id());
return redirect()->back()->with('success', '單據已作廢');
}
// 一般更新 (更新品項與基本資訊)
if ($doc->status !== 'draft') {
return redirect()->back()->with('error', '只有草稿狀態的單據可以修改');
}
$request->validate([
'reason' => 'required|string',
'remarks' => 'nullable|string',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required',
'items.*.adjust_qty' => 'required|numeric',
]);
$doc->update([
'reason' => $request->reason,
'remarks' => $request->remarks,
]);
$this->adjustService->updateItems($doc, $request->items);
return redirect()->back()->with('success', '單據已更新');
}
public function show(InventoryAdjustDoc $doc)
{
$doc->load(['items.product.baseUnit', 'createdBy', 'postedBy', 'warehouse', 'countDoc']);
// Pre-fetch relevant Inventory information (mainly for expiry date)
$inventoryMap = \App\Modules\Inventory\Models\Inventory::withTrashed()
->where('warehouse_id', $doc->warehouse_id)
->whereIn('product_id', $doc->items->pluck('product_id'))
->whereIn('batch_number', $doc->items->pluck('batch_number'))
->get()
->mapWithKeys(function ($inv) {
return [$inv->product_id . '-' . $inv->batch_number => $inv];
});
$docData = [
'id' => (string) $doc->id,
'doc_no' => $doc->doc_no,
'warehouse_id' => (string) $doc->warehouse_id,
'warehouse_name' => $doc->warehouse->name,
'status' => $doc->status,
'reason' => $doc->reason,
'remarks' => $doc->remarks,
'created_at' => $doc->created_at->format('Y-m-d H:i'),
'created_by' => $doc->createdBy?->name,
'count_doc_id' => $doc->count_doc_id ? (string)$doc->count_doc_id : null,
'count_doc_no' => $doc->countDoc?->doc_no,
'items' => $doc->items->map(function ($item) use ($inventoryMap) {
$inv = $inventoryMap->get($item->product_id . '-' . $item->batch_number);
return [
'id' => (string) $item->id,
'product_id' => (string) $item->product_id,
'product_name' => $item->product->name,
'product_code' => $item->product->code,
'batch_number' => $item->batch_number,
'expiry_date' => $inv && $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
'unit' => $item->product->baseUnit?->name,
'qty_before' => (float) $item->qty_before,
'adjust_qty' => (float) $item->adjust_qty,
'notes' => $item->notes,
];
}),
];
return Inertia::render('Inventory/Adjust/Show', [
'doc' => $docData,
]);
}
public function destroy(InventoryAdjustDoc $doc)
{
if ($doc->status !== 'draft') {
return redirect()->back()->with('error', '只能刪除草稿狀態的單據');
}
$doc->items()->delete();
$doc->delete();
return redirect()->route('inventory.adjust.index')
->with('success', '盤調單已刪除');
}
}

View File

@@ -0,0 +1,239 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\InventoryCountDoc;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Services\CountService;
use Illuminate\Http\Request;
use Inertia\Inertia;
class CountDocController extends Controller
{
protected $countService;
public function __construct(CountService $countService)
{
$this->countService = $countService;
}
public function index(Request $request)
{
$query = InventoryCountDoc::query()
->with(['createdBy', 'completedBy', 'warehouse']);
if ($request->filled('warehouse_id')) {
$query->where('warehouse_id', $request->warehouse_id);
}
if ($request->filled('search')) {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('doc_no', 'like', "%{$search}%")
->orWhere('remarks', 'like', "%{$search}%");
});
}
$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;
}
$countQuery = function ($query) {
$query->whereNotNull('counted_qty');
};
$docs = $query->withCount(['items', 'items as counted_items_count' => $countQuery])
->orderByDesc('created_at')
->paginate($perPage)
->withQueryString()
->through(function ($doc) {
return [
'id' => (string) $doc->id,
'doc_no' => $doc->doc_no,
'status' => $doc->status,
'warehouse_name' => $doc->warehouse->name,
'snapshot_date' => $doc->snapshot_date ? $doc->snapshot_date->format('Y-m-d H:i') : '-',
'completed_at' => $doc->completed_at ? $doc->completed_at->format('Y-m-d H:i') : '-',
'created_by' => $doc->createdBy?->name,
'remarks' => $doc->remarks,
'total_items' => $doc->items_count,
'counted_items' => $doc->counted_items_count,
];
});
return Inertia::render('Inventory/Count/Index', [
'docs' => $docs,
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
]);
}
public function store(Request $request)
{
$validated = $request->validate([
'warehouse_id' => 'required|exists:warehouses,id',
'remarks' => 'nullable|string|max:255',
]);
$doc = $this->countService->createDoc(
$validated['warehouse_id'],
$validated['remarks'] ?? null,
auth()->id()
);
// 自動執行快照
$this->countService->snapshot($doc, false);
return redirect()->route('inventory.count.show', [$doc->id])
->with('success', '已建立盤點單並完成庫存快照');
}
public function show(InventoryCountDoc $doc)
{
$doc->load(['items.product.baseUnit', 'createdBy', 'completedBy', 'warehouse']);
// 預先抓取相關的 Inventory 資訊 (主要為了取得效期)
$inventoryMap = \App\Modules\Inventory\Models\Inventory::withTrashed()
->where('warehouse_id', $doc->warehouse_id)
->whereIn('product_id', $doc->items->pluck('product_id'))
->whereIn('batch_number', $doc->items->pluck('batch_number'))
->get()
->mapWithKeys(function ($inv) {
return [$inv->product_id . '-' . $inv->batch_number => $inv];
});
$docData = [
'id' => (string) $doc->id,
'doc_no' => $doc->doc_no,
'warehouse_id' => (string) $doc->warehouse_id,
'warehouse_name' => $doc->warehouse->name,
'status' => $doc->status,
'remarks' => $doc->remarks,
'snapshot_date' => $doc->snapshot_date ? $doc->snapshot_date->format('Y-m-d H:i') : null,
'created_by' => $doc->createdBy?->name,
'items' => $doc->items->map(function ($item) use ($inventoryMap) {
$key = $item->product_id . '-' . $item->batch_number;
$inv = $inventoryMap->get($key);
return [
'id' => (string) $item->id,
'product_name' => $item->product->name,
'product_code' => $item->product->code,
'batch_number' => $item->batch_number,
'expiry_date' => $inv && $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null, // 新增效期
'unit' => $item->product->baseUnit?->name,
'system_qty' => (float) $item->system_qty,
'counted_qty' => is_null($item->counted_qty) ? '' : (float) $item->counted_qty,
'diff_qty' => (float) $item->diff_qty,
'notes' => $item->notes,
];
}),
];
return Inertia::render('Inventory/Count/Show', [
'doc' => $docData,
]);
}
public function print(InventoryCountDoc $doc)
{
$doc->load(['items.product.baseUnit', 'createdBy', 'completedBy', 'warehouse']);
$docData = [
'id' => (string) $doc->id,
'doc_no' => $doc->doc_no,
'warehouse_name' => $doc->warehouse->name,
'snapshot_date' => $doc->snapshot_date ? $doc->snapshot_date->format('Y-m-d') : date('Y-m-d'), // Use date only
'created_at' => $doc->created_at->format('Y-m-d'),
'print_date' => date('Y-m-d'),
'created_by' => $doc->createdBy?->name,
'items' => $doc->items->map(function ($item) {
return [
'id' => (string) $item->id,
'product_name' => $item->product->name,
'product_code' => $item->product->code,
'specification' => $item->product->specification,
'unit' => $item->product->baseUnit?->name,
'quantity' => (float) ($item->counted_qty ?? $item->system_qty), // Default to system qty if counted is null, or just counted? User wants "Count Sheet" -> maybe blank if not counted?
// Actually, if it's "Completed", we show counted. If it's "Pending", we usually show blank or system.
// The 'Show' page logic suggests we show counted_qty.
'counted_qty' => $item->counted_qty,
'notes' => $item->notes,
];
}),
];
return Inertia::render('Inventory/Count/Print', [
'doc' => $docData,
]);
}
public function update(Request $request, InventoryCountDoc $doc)
{
if ($doc->status === 'completed') {
return redirect()->back()->with('error', '此盤點單已完成,無法修改');
}
$validated = $request->validate([
'items' => 'array',
'items.*.id' => 'required|exists:inventory_count_items,id',
'items.*.counted_qty' => 'nullable|numeric|min:0',
'items.*.notes' => 'nullable|string',
]);
if (isset($validated['items'])) {
$this->countService->updateCount($doc, $validated['items']);
}
// 重新讀取以獲取最新狀態
$doc->refresh();
if ($doc->status === 'completed') {
return redirect()->route('inventory.count.index')
->with('success', '盤點完成,單據已自動存檔並完成。');
}
return redirect()->back()->with('success', '盤點資料已暫存');
}
public function reopen(InventoryCountDoc $doc)
{
// 權限檢查 (通常僅允許有權限者執行,例如 inventory.adjust)
// 注意:前端已經用 <Can> 保護按鈕,後端這裡最好也加上檢查
if (!auth()->user()->can('inventory.adjust')) {
abort(403);
}
if (!in_array($doc->status, ['completed', 'no_adjust'])) {
return redirect()->back()->with('error', '僅能針對已完成或無需盤調的盤點單重新開啟盤點');
}
// 執行取消核准邏輯
$doc->update([
'status' => 'counting', // 回復為盤點中
'completed_at' => null, // 清除完成時間
'completed_by' => null, // 清除完成者
]);
return redirect()->back()->with('success', '已重新開啟盤點,單據回復為盤點中狀態');
}
public function destroy(InventoryCountDoc $doc)
{
if ($doc->status === 'completed') {
return redirect()->back()->with('error', '已完成的盤點單無法刪除');
}
// Activity Log handled by Model Trait
$doc->items()->delete();
$doc->delete();
return redirect()->route('inventory.count.index')
->with('success', '盤點單已刪除');
}
}

View File

@@ -0,0 +1,488 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Services\GoodsReceiptService;
use App\Modules\Inventory\Services\InventoryService;
use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Modules\Inventory\Models\GoodsReceipt;
use Illuminate\Support\Facades\DB;
use App\Modules\Inventory\Services\DuplicateCheckService;
class GoodsReceiptController extends Controller
{
protected $goodsReceiptService;
protected $inventoryService;
protected $procurementService;
protected $duplicateCheckService;
public function __construct(
GoodsReceiptService $goodsReceiptService,
InventoryService $inventoryService,
ProcurementServiceInterface $procurementService,
DuplicateCheckService $duplicateCheckService
) {
$this->goodsReceiptService = $goodsReceiptService;
$this->inventoryService = $inventoryService;
$this->procurementService = $procurementService;
$this->duplicateCheckService = $duplicateCheckService;
}
public function index(Request $request)
{
$query = GoodsReceipt::query()
->select(['id', 'code', 'type', 'warehouse_id', 'vendor_id', 'received_date', 'status', 'created_at'])
->with(['warehouse'])
->withSum('items', 'total_amount');
// 關鍵字搜尋(單號)
if ($request->filled('search')) {
$search = $request->input('search');
$query->where('code', 'like', "%{$search}%");
}
// 狀態篩選
if ($request->filled('status') && $request->input('status') !== 'all') {
$query->where('status', $request->input('status'));
}
// 倉庫篩選
if ($request->filled('warehouse_id') && $request->input('warehouse_id') !== 'all') {
$query->where('warehouse_id', $request->input('warehouse_id'));
}
// 日期範圍篩選
if ($request->filled('date_start')) {
$query->whereDate('received_date', '>=', $request->input('date_start'));
}
if ($request->filled('date_end')) {
$query->whereDate('received_date', '<=', $request->input('date_end'));
}
// 每頁筆數
$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')
->paginate($perPage)
->withQueryString();
// Manual Hydration for Vendors (Cross-Module)
$vendorIds = collect($receipts->items())->pluck('vendor_id')->unique()->filter()->toArray();
$vendors = $this->procurementService->getVendorsByIds($vendorIds)->keyBy('id');
$receipts->getCollection()->transform(function ($receipt) use ($vendors) {
$receipt->vendor = $vendors->get($receipt->vendor_id);
return $receipt;
});
// 取得倉庫列表用於篩選
$warehouses = $this->inventoryService->getAllWarehouses();
return Inertia::render('Inventory/GoodsReceipt/Index', [
'receipts' => $receipts,
'filters' => $request->only(['search', 'status', 'warehouse_id', 'date_start', 'date_end', 'per_page']),
'warehouses' => $warehouses,
]);
}
public function show(Request $request, $id)
{
$receipt = GoodsReceipt::with([
'warehouse',
'items.product.category',
'items.product.baseUnit'
])->findOrFail($id);
// Manual Hydration for Vendor (Cross-Module)
if ($receipt->vendor_id) {
$receipt->vendor = $this->procurementService->getVendorsByIds([$receipt->vendor_id])->first();
}
// 手動計算統計資訊 (如果 Model 沒有定義對應的 Attribute)
$receipt->items_sum_total_amount = $receipt->items->sum('total_amount');
return Inertia::render('Inventory/GoodsReceipt/Show', [
'receipt' => $receipt,
'navigation' => [
'from' => $request->query('from'),
'from_id' => $request->query('from_id'),
'from_label' => $request->query('from_label'),
]
]);
}
public function create()
{
// 取得待進貨的採購單列表(用於標準採購類型選擇)
$pendingPOs = $this->procurementService->getPendingPurchaseOrders();
// 提取所有產品 ID 以便跨模組水和資料
$productIds = $pendingPOs->flatMap(fn($po) => $po->items->pluck('product_id'))->unique()->filter()->toArray();
$products = $this->inventoryService->getProductsByIds($productIds)->keyBy('id');
// 處理採購單資料,計算剩餘可收貨數量
$formattedPOs = $pendingPOs->map(function ($po) use ($products) {
return [
'id' => $po->id,
'code' => $po->code,
'status' => $po->status,
'supplierId' => $po->vendor_id, // Alias for frontend
'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 ($products) {
$product = $products->get($item->product_id);
$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 [
'id' => $item->id,
'productId' => $item->product_id, // Alias for frontend
'product_id' => $item->product_id,
'productName' => $product?->name ?? '', // Alias for frontend
'product_name' => $product?->name ?? '',
'product_code' => $product?->code ?? '',
'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,
'received_quantity' => $item->received_quantity ?? 0,
'remaining' => $remaining,
'unitPrice' => $item->unit_price, // Alias for frontend
'unit_price' => $item->unit_price,
];
})->filter(fn($item) => $item['remaining'] > 0)->values(),
];
})->filter(fn($po) => $po['items']->count() > 0)->values();
// 取得所有廠商列表(用於雜項入庫/其他類型選擇)
$vendors = $this->procurementService->getAllVendors();
return Inertia::render('Inventory/GoodsReceipt/Create', [
'warehouses' => $this->inventoryService->getAllWarehouses(),
'pendingPurchaseOrders' => $formattedPOs,
'vendors' => $vendors,
]);
}
public function store(Request $request)
{
$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',
'type' => 'required|in:standard,miscellaneous,other',
'purchase_order_id' => 'nullable|required_if:type,standard|exists:purchase_orders,id',
'vendor_id' => 'nullable|integer',
'received_date' => 'required|date',
'remarks' => 'nullable|string',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required|integer|exists:products,id',
'items.*.purchase_order_item_id' => 'nullable|required_if:type,standard|integer',
'items.*.quantity_received' => 'required|numeric|min:0',
'items.*.unit_price' => 'nullable|numeric|min:0',
'items.*.subtotal' => 'nullable|numeric|min:0',
'items.*.batch_number' => 'nullable|string',
'items.*.expiry_date' => 'nullable|date',
'force' => 'nullable|boolean',
];
}
/**
* 預檢重複進貨 API
*/
public function checkDuplicate(Request $request)
{
$result = $this->duplicateCheckService->checkDuplicateReceipt($request->all());
return response()->json($result);
}
public function submit(GoodsReceipt $goodsReceipt)
{
if (!auth()->user()->can('goods_receipts.edit')) {
return back()->with('error', '您沒有權限確認點收');
}
try {
$this->goodsReceiptService->submit($goodsReceipt);
return back()->with('success', '進貨單已點收完成,庫存已增加並拋轉應付帳款');
} catch (\Exception $e) {
return back()->with('error', $e->getMessage());
}
}
// API to search POs
public function searchPOs(Request $request)
{
$search = $request->input('query');
if (!$search) {
return response()->json([]);
}
$pos = $this->procurementService->searchPendingPurchaseOrders($search);
return response()->json($pos);
}
// API to search Products for Manual Entry
// 支援 query='*' 回傳所有商品(用於 SearchableSelect 下拉選單)
public function searchProducts(Request $request)
{
$search = $request->input('query');
if (!$search) {
return response()->json([]);
}
// 萬用字元:回傳所有商品
if ($search === '*') {
$products = $this->inventoryService->getProductsByName('');
} else {
$products = $this->inventoryService->getProductsByName($search);
}
// Format for frontend
$mapped = $products->map(function($product) {
return [
'id' => $product->id,
'name' => $product->name,
'code' => $product->code,
'unit' => $product->baseUnit?->name ?? '個',
'price' => $product->purchase_price ?? 0,
];
});
return response()->json($mapped);
}
// API to search Vendors
public function searchVendors(Request $request)
{
$search = $request->input('query');
if (!$search) {
return response()->json([]);
}
$vendors = $this->procurementService->searchVendors($search);
return response()->json($vendors);
}
/**
* 刪除進貨單
*/
public function destroy(GoodsReceipt $goodsReceipt)
{
// 只有有權限的人可以刪除
if (!auth()->user()->can('goods_receipts.delete')) {
return redirect()->back()->with('error', '您沒有權限刪除進貨單');
}
// 簡單刪除邏輯:刪除進貨單(品項由資料庫級聯刪除或手動處理)
// 注意:實務上可能需要處理已入庫的庫存回滾,但在這個簡易 ERP 中通常是行政刪除
$goodsReceipt->delete();
return redirect()->route('goods-receipts.index')->with('success', '進貨單已刪除');
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Category;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Services\TurnoverService;
use Illuminate\Http\Request;
use Inertia\Inertia;
class InventoryAnalysisController extends Controller
{
protected $turnoverService;
public function __construct(TurnoverService $turnoverService)
{
$this->turnoverService = $turnoverService;
}
public function index(Request $request)
{
$filters = $request->only([
'warehouse_id', 'category_id', 'search', 'per_page', 'sort_by', 'sort_order', 'status'
]);
$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);
return Inertia::render('Inventory/Analysis/Index', [
'analysisData' => $analysisData,
'kpis' => $kpis,
'warehouses' => Warehouse::select('id', 'name')->get(),
'categories' => Category::select('id', 'name')->get(),
'filters' => $filters,
]);
}
}

View File

@@ -10,17 +10,26 @@ use Inertia\Inertia;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Models\InventoryTransaction;
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
use App\Modules\Inventory\Imports\InventoryImport;
use App\Modules\Inventory\Exports\InventoryTemplateExport;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use App\Modules\Core\Contracts\CoreServiceInterface;
class InventoryController extends Controller
{
protected $coreService;
protected $inventoryService;
public function __construct(CoreServiceInterface $coreService)
{
public function __construct(
CoreServiceInterface $coreService,
\App\Modules\Inventory\Contracts\InventoryServiceInterface $inventoryService
) {
$this->coreService = $coreService;
$this->inventoryService = $inventoryService;
}
public function index(Request $request, Warehouse $warehouse)
@@ -32,7 +41,7 @@ class InventoryController extends Controller
'inventories.lastIncomingTransaction',
'inventories.lastOutgoingTransaction'
]);
$allProducts = Product::with('category')->get();
$allProducts = Product::select('id', 'name', 'code', 'category_id')->with('category:id,name')->get();
// 1. 準備 availableProducts
$availableProducts = $allProducts->map(function ($product) {
@@ -48,12 +57,40 @@ class InventoryController extends Controller
->pluck('safety_stock', 'product_id')
->mapWithKeys(fn($val, $key) => [(string)$key => (float)$val]);
// 3. 準備 inventories (批號分組)
$items = $warehouse->inventories()
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction'])
->get();
$query = $warehouse->inventories()
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction']);
$inventories = $items->groupBy('product_id')->map(function ($batchItems) use ($safetyStockMap) {
// 加入搜尋過濾
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';
$inventories = $items->groupBy(function ($item) use ($isVending) {
return $isVending
? $item->product_id . '-' . ($item->location ?? 'NO-SLOT')
: $item->product_id;
})->map(function ($batchItems) use ($safetyStockMap, $isVending) {
$firstItem = $batchItems->first();
$product = $firstItem->product;
$totalQuantity = $batchItems->sum('quantity');
@@ -93,9 +130,10 @@ class InventoryController extends Controller
'safetyStock' => null, // 批號層級不再有安全庫存
'status' => '正常',
'batchNumber' => $inv->batch_number ?? 'BATCH-' . $inv->id,
'location' => $inv->location,
'expiryDate' => $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
'lastInboundDate' => $inv->lastIncomingTransaction ? ($inv->lastIncomingTransaction->actual_time ? $inv->lastIncomingTransaction->actual_time->format('Y-m-d') : $inv->lastIncomingTransaction->created_at->format('Y-m-d')) : null,
'lastOutboundDate' => $inv->lastOutgoingTransaction ? ($inv->lastOutgoingTransaction->actual_time ? $inv->lastOutgoingTransaction->actual_time->format('Y-m-d') : $inv->lastOutgoingTransaction->created_at->format('Y-m-d')) : null,
'lastInboundDate' => $inv->lastIncomingTransaction ? ($inv->lastIncomingTransaction->actual_time ? substr($inv->lastIncomingTransaction->actual_time, 0, 10) : $inv->lastIncomingTransaction->created_at->format('Y-m-d')) : null,
'lastOutboundDate' => $inv->lastOutgoingTransaction ? ($inv->lastOutgoingTransaction->actual_time ? substr($inv->lastOutgoingTransaction->actual_time, 0, 10) : $inv->lastOutgoingTransaction->created_at->format('Y-m-d')) : null,
];
})->values(),
];
@@ -129,17 +167,19 @@ class InventoryController extends Controller
public function create(Warehouse $warehouse)
{
// ... (unchanged) ...
$products = Product::with(['baseUnit', 'largeUnit'])
->select('id', 'name', 'code', 'base_unit_id', 'large_unit_id', 'conversion_rate')
$products = Product::select('id', 'name', 'code', 'barcode', 'base_unit_id', 'large_unit_id', 'conversion_rate', 'cost_price')
->with(['baseUnit:id,name', 'largeUnit:id,name'])
->get()
->map(function ($product) {
return [
'id' => (string) $product->id,
'name' => $product->name,
'code' => $product->code,
'barcode' => $product->barcode,
'baseUnit' => $product->baseUnit?->name ?? '個',
'largeUnit' => $product->largeUnit?->name, // 可能為 null
'conversionRate' => (float) $product->conversion_rate,
'costPrice' => (float) $product->cost_price,
];
});
@@ -160,83 +200,28 @@ class InventoryController extends Controller
'items.*.productId' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.01',
'items.*.unit_cost' => 'nullable|numeric|min:0', // 新增成本驗證
'items.*.batchMode' => 'required|in:existing,new',
'items.*.batchMode' => 'required|in:existing,new,none',
'items.*.inventoryId' => 'required_if:items.*.batchMode,existing|nullable|exists:inventories,id',
'items.*.originCountry' => 'required_if:items.*.batchMode,new|nullable|string|max:2',
'items.*.expiryDate' => 'nullable|date',
'items.*.location' => 'nullable|string|max:50',
]);
return DB::transaction(function () use ($validated, $warehouse) {
foreach ($validated['items'] as $item) {
// ... (略,傳遞 unit_cost 交給 Service 處理) ...
// 這裡需要修改呼叫 Service 的地方或直接更新邏輯
// 為求快速,我將在此更新邏輯
$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'];
}
} 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, // 稍後計算
'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(),
]);
// 修正時間精度:使用 Carbon 解析,若含時間則保留並補上秒數,若只有日期則補上當前時間
$dt = \Illuminate\Support\Carbon::parse($validated['inboundDate']);
if ($dt->hour === 0 && $dt->minute === 0 && $dt->second === 0) {
$dt->setTimeFrom(now());
} else {
$dt->setSecond(now()->second);
}
$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)
->with('success', '庫存記錄已儲存成功');
@@ -259,7 +244,8 @@ class InventoryController extends Controller
'originCountry' => $inventory->origin_country,
'expiryDate' => $inventory->expiry_date ? $inventory->expiry_date->format('Y-m-d') : null,
'quantity' => (float) $inventory->quantity,
'unitCost' => (float) $inventory->unit_cost, // 新增
'unitCost' => (float) $inventory->unit_cost,
'location' => $inventory->location,
];
});
@@ -323,7 +309,7 @@ class InventoryController extends Controller
'balanceAfter' => (float) $tx->balance_after,
'reason' => $tx->reason,
'userName' => $user ? $user->name : '系統', // 手動對應
'actualTime' => $tx->actual_time ? $tx->actual_time->format('Y-m-d H:i') : $tx->created_at->format('Y-m-d H:i'),
'actualTime' => $tx->actual_time ? substr($tx->actual_time, 0, 16) : $tx->created_at->format('Y-m-d H:i'),
];
});
@@ -364,81 +350,7 @@ class InventoryController extends Controller
]);
return DB::transaction(function () use ($validated, $inventory) {
$currentQty = (float) $inventory->quantity;
$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(),
]);
}
$this->inventoryService->adjustInventory($inventory, $validated);
return redirect()->route('warehouses.inventory.index', $inventory->warehouse_id)
->with('success', '庫存資料已更新');
@@ -482,7 +394,61 @@ class InventoryController extends Controller
$productId = $request->query('productId');
if ($productId) {
// ... (略) ...
$product = Product::findOrFail($productId);
// 取得該倉庫中該商品的所有批號 ID
$inventoryIds = Inventory::where('warehouse_id', $warehouse->id)
->where('product_id', $productId)
->pluck('id')
->toArray();
$transactionsRaw = InventoryTransaction::whereIn('inventory_id', $inventoryIds)
->with('inventory') // 需要批號資訊
->orderBy('actual_time', 'desc')
->orderBy('id', 'desc')
->get();
// 手動 Hydrate 使用者資料
$userIds = $transactionsRaw->pluck('user_id')->filter()->unique()->toArray();
$users = $this->coreService->getUsersByIds($userIds)->keyBy('id');
// 計算商品在該倉庫的總量(不分批號)
$currentRunningTotal = (float) Inventory::whereIn('id', $inventoryIds)->sum('quantity');
$transactions = $transactionsRaw->map(function ($tx) use ($users, &$currentRunningTotal) {
$user = $tx->user_id ? ($users[$tx->user_id] ?? null) : null;
$balanceAfter = $currentRunningTotal;
// 為下一筆(較舊的)紀錄更新 Running Total
$currentRunningTotal -= (float) $tx->quantity;
return [
'id' => (string) $tx->id,
'type' => $tx->type,
'quantity' => (float) $tx->quantity,
'unit_cost' => (float) $tx->unit_cost,
'balanceAfter' => (float) $balanceAfter, // 顯示該商品在倉庫的累計結餘
'reason' => $tx->reason,
'userName' => $user ? $user->name : '系統',
'actualTime' => $tx->actual_time ? substr($tx->actual_time, 0, 16) : $tx->created_at->format('Y-m-d H:i'),
'batchNumber' => $tx->inventory?->batch_number ?? '-', // 補上批號資訊
'slot' => $tx->inventory?->location, // 加入貨道資訊
];
});
// 重新計算目前的總量(用於 Header 顯示,確保一致性)
$totalQuantity = Inventory::whereIn('id', $inventoryIds)->sum('quantity');
return Inertia::render('Warehouse/InventoryHistory', [
'warehouse' => $warehouse,
'inventory' => [
'id' => null, // 跨批號查詢沒有單一 ID
'productName' => $product->name,
'productCode' => $product->code,
'batchNumber' => '所有批號',
'quantity' => (float) $totalQuantity,
],
'transactions' => $transactions
]);
}
if ($inventoryId) {
@@ -496,7 +462,7 @@ class InventoryController extends Controller
$userIds = $inventory->transactions->pluck('user_id')->filter()->unique()->toArray();
$users = $this->coreService->getUsersByIds($userIds)->keyBy('id');
$transactions = $inventory->transactions->map(function ($tx) use ($users) {
$transactions = $inventory->transactions->map(function ($tx) use ($users, $inventory) {
$user = $tx->user_id ? ($users[$tx->user_id] ?? null) : null;
return [
'id' => (string) $tx->id,
@@ -506,7 +472,8 @@ class InventoryController extends Controller
'balanceAfter' => (float) $tx->balance_after,
'reason' => $tx->reason,
'userName' => $user ? $user->name : '系統', // 手動對應
'actualTime' => $tx->actual_time ? $tx->actual_time->format('Y-m-d H:i') : $tx->created_at->format('Y-m-d H:i'),
'actualTime' => $tx->actual_time ? substr($tx->actual_time, 0, 16) : $tx->created_at->format('Y-m-d H:i'),
'slot' => $inventory->location, // 加入貨道資訊
];
});
@@ -527,4 +494,35 @@ class InventoryController extends Controller
return redirect()->back()->with('error', '未提供查詢參數');
}
/**
* 匯入入庫
*/
public function import(Request $request, Warehouse $warehouse)
{
$request->validate([
'file' => 'required|mimes:xlsx,xls,csv',
'inboundDate' => 'required|date',
'notes' => 'nullable|string',
]);
try {
Excel::import(
new InventoryImport($warehouse, $request->inboundDate, $request->notes),
$request->file('file')
);
return back()->with('success', '庫存資料匯入成功');
} catch (\Exception $e) {
return back()->withErrors(['file' => '匯入過程中發生錯誤: ' . $e->getMessage()]);
}
}
/**
* 下載匯入範本 (.xlsx)
*/
public function template()
{
return Excel::download(new InventoryTemplateExport, '庫存匯入範本.xlsx');
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Category;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Services\InventoryReportService;
use App\Modules\Inventory\Exports\InventoryReportExport;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Maatwebsite\Excel\Facades\Excel;
class InventoryReportController extends Controller
{
protected $reportService;
public function __construct(InventoryReportService $reportService)
{
$this->reportService = $reportService;
}
public function index(Request $request)
{
$filters = $request->only([
'date_from', 'date_to', 'warehouse_id', 'category_id', 'search', 'per_page',
'sort_by', 'sort_order'
]);
if (!isset($filters['date_from'])) {
$filters['date_from'] = date('Y-m-d');
}
if (!isset($filters['date_to'])) {
$filters['date_to'] = date('Y-m-d');
}
$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);
return Inertia::render('Inventory/Report/Index', [
'reportData' => $reportData,
'summary' => $summary,
'warehouses' => Warehouse::select('id', 'name')->get(),
'categories' => Category::select('id', 'name')->get(),
'filters' => $filters,
]);
}
public function export(Request $request)
{
$filters = $request->only([
'period', 'date_from', 'date_to', 'warehouse_id', 'category_id', 'search'
]);
return Excel::download(new InventoryReportExport($this->reportService, $filters), 'inventory_report_' . date('YmdHis') . '.xlsx');
}
public function show(Request $request, $productId)
{
// 明細頁面自身使用的篩選條件
$filters = $request->only([
'date_from', 'date_to', 'warehouse_id'
]);
// 報表頁面的完整篩選狀態(用於返回時恢復)
$reportFilters = $request->only([
'date_from', 'date_to', 'warehouse_id',
'category_id', 'search', 'per_page'
]);
// 將傳入的 report_page 轉回 page 以便 Link 元件正確生成回報表頁的連結
if ($request->has('report_page')) {
$reportFilters['page'] = $request->input('report_page');
}
// 取得商品資訊 (用於顯示標題,含基本單位)
$product = \App\Modules\Inventory\Models\Product::with('baseUnit')->findOrFail($productId);
$transactions = $this->reportService->getProductDetails($productId, $filters, 20);
return Inertia::render('Inventory/Report/Show', [
'product' => [
'id' => $product->id,
'code' => $product->code,
'name' => $product->name,
'unit_name' => $product->baseUnit?->name ?? '-',
],
'transactions' => $transactions,
'filters' => $filters,
'reportFilters' => $reportFilters,
'warehouses' => Warehouse::select('id', 'name')->get(),
]);
}
}

View File

@@ -10,9 +10,18 @@ use App\Modules\Inventory\Models\Category;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Maatwebsite\Excel\Facades\Excel;
use App\Modules\Inventory\Exports\ProductTemplateExport;
use App\Modules\Inventory\Imports\ProductImport;
class ProductController extends Controller
{
protected $productService;
public function __construct(\App\Modules\Inventory\Contracts\ProductServiceInterface $productService)
{
$this->productService = $productService;
}
/**
* 顯示資源列表。
*/
@@ -25,6 +34,7 @@ class ProductController extends Controller
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('code', 'like', "%{$search}%")
->orWhere('barcode', 'like', "%{$search}%")
->orWhere('brand', 'like', "%{$search}%");
});
}
@@ -33,9 +43,11 @@ class ProductController extends Controller
$query->where('category_id', $request->category_id);
}
$perPage = $request->input('per_page', 10);
if (!in_array($perPage, [10, 20, 50, 100])) {
$perPage = 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;
}
$sortField = $request->input('sort_field', 'id');
@@ -66,6 +78,7 @@ class ProductController extends Controller
return (object) [
'id' => (string) $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'name' => $product->name,
'categoryId' => $product->category_id,
'category' => $product->category ? (object) [
@@ -90,52 +103,137 @@ class ProductController extends Controller
'name' => $product->purchaseUnit->name,
] : null,
'conversionRate' => (float) $product->conversion_rate,
'location' => $product->location,
'cost_price' => (float) $product->cost_price,
'price' => (float) $product->price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
'is_active' => (bool) $product->is_active,
];
});
$categories = Category::where('is_active', true)->get();
$categories = Category::select('id', 'name')->where('is_active', true)->get();
return Inertia::render('Product/Index', [
'products' => $products,
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
'categories' => $categories->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
'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']),
]);
}
/**
* 顯示指定的資源。
*/
public function show(Product $product): Response
{
return Inertia::render('Product/Show', [
'product' => (object) [
'id' => (string) $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'name' => $product->name,
'categoryId' => $product->category_id,
'category' => $product->category ? (object) [
'id' => $product->category->id,
'name' => $product->category->name,
] : null,
'brand' => $product->brand,
'specification' => $product->specification,
'baseUnitId' => $product->base_unit_id,
'baseUnit' => $product->baseUnit ? (object) [
'id' => $product->baseUnit->id,
'name' => $product->baseUnit->name,
] : null,
'largeUnitId' => $product->large_unit_id,
'largeUnit' => $product->largeUnit ? (object) [
'id' => $product->largeUnit->id,
'name' => $product->largeUnit->name,
] : null,
'purchaseUnitId' => $product->purchase_unit_id,
'purchaseUnit' => $product->purchaseUnit ? (object) [
'id' => $product->purchaseUnit->id,
'name' => $product->purchaseUnit->name,
] : null,
'conversionRate' => (float) $product->conversion_rate,
'location' => $product->location,
'cost_price' => (float) $product->cost_price,
'price' => (float) $product->price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
'is_active' => (bool) $product->is_active,
]
]);
}
/**
* 顯示建立表單。
*/
public function create(): Response
{
return Inertia::render('Product/Create', [
'categories' => Category::select('id', 'name')->where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
]);
}
/**
* 將新建立的資源儲存到儲存體中。
*/
public function store(Request $request)
{
$validated = $request->validate([
'code' => 'required|string|max:2|unique:products,code',
'code' => 'nullable|unique:products,code',
'barcode' => 'nullable|unique:products,barcode',
'name' => 'required|string|max:255',
'category_id' => 'required|exists:categories,id',
'brand' => 'nullable|string|max:255',
'specification' => 'nullable|string',
'base_unit_id' => 'required|exists:units,id',
'large_unit_id' => 'nullable|exists:units,id',
'conversion_rate' => 'required_with:large_unit_id|nullable|numeric|min:0.0001',
'purchase_unit_id' => 'nullable|exists:units,id',
], [
'code.required' => '商品代號為必填',
'code.max' => '商品代號最多 2 碼',
'code.unique' => '商品代號已存在',
'name.required' => '商品名稱為必填',
'category_id.required' => '請選擇分類',
'category_id.exists' => '所選分類不存在',
'base_unit_id.required' => '基本庫存單位為必填',
'base_unit_id.exists' => '所選基本單位不存在',
'conversion_rate.required_with' => '填寫大單位時,換算率為必填',
'conversion_rate.numeric' => '換算率必須為數字',
'conversion_rate.min' => '換算率最小為 0.0001',
'conversion_rate' => 'nullable|numeric|min:0',
'location' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0',
'price' => 'nullable|numeric|min:0',
'member_price' => 'nullable|numeric|min:0',
'wholesale_price' => 'nullable|numeric|min:0',
'is_active' => 'boolean',
]);
$product = Product::create($validated);
$product = $this->productService->createProduct($validated);
return redirect()->back()->with('success', '商品已建立');
return redirect()->route('products.index')->with('success', '商品已建立');
}
/**
* 顯示編輯表單。
*/
public function edit(Product $product): Response
{
return Inertia::render('Product/Edit', [
'product' => (object) [
'id' => (string) $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'name' => $product->name,
'categoryId' => $product->category_id,
'brand' => $product->brand,
'specification' => $product->specification,
'baseUnitId' => $product->base_unit_id,
'largeUnitId' => $product->large_unit_id,
'conversionRate' => (float) $product->conversion_rate,
'purchaseUnitId' => $product->purchase_unit_id,
'location' => $product->location,
'cost_price' => (float) $product->cost_price,
'price' => (float) $product->price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
'is_active' => (bool) $product->is_active,
],
'categories' => Category::select('id', 'name')->where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
]);
}
/**
@@ -144,32 +242,31 @@ class ProductController extends Controller
public function update(Request $request, Product $product)
{
$validated = $request->validate([
'code' => 'required|string|max:2|unique:products,code,' . $product->id,
'code' => 'nullable|unique:products,code,' . $product->id,
'barcode' => 'nullable|unique:products,barcode,' . $product->id,
'name' => 'required|string|max:255',
'category_id' => 'required|exists:categories,id',
'brand' => 'nullable|string|max:255',
'specification' => 'nullable|string',
'base_unit_id' => 'required|exists:units,id',
'large_unit_id' => 'nullable|exists:units,id',
'conversion_rate' => 'required_with:large_unit_id|nullable|numeric|min:0.0001',
'purchase_unit_id' => 'nullable|exists:units,id',
], [
'code.required' => '商品代號為必填',
'code.max' => '商品代號最多 2 碼',
'code.unique' => '商品代號已存在',
'name.required' => '商品名稱為必填',
'category_id.required' => '請選擇分類',
'category_id.exists' => '所選分類不存在',
'base_unit_id.required' => '基本庫存單位為必填',
'base_unit_id.exists' => '所選基本單位不存在',
'conversion_rate.required_with' => '填寫大單位時,換算率為必填',
'conversion_rate.numeric' => '換算率必須為數字',
'conversion_rate.min' => '換算率最小為 0.0001',
'conversion_rate' => 'nullable|numeric|min:0',
'location' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0',
'price' => 'nullable|numeric|min:0',
'member_price' => 'nullable|numeric|min:0',
'wholesale_price' => 'nullable|numeric|min:0',
'is_active' => 'boolean',
]);
$product->update($validated);
$this->productService->updateProduct($product, $validated);
return redirect()->back()->with('success', '商品已更新');
if ($request->input('from') === 'show') {
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
}
return redirect()->route('products.index')->with('success', '商品已更新');
}
/**
@@ -181,4 +278,36 @@ class ProductController extends Controller
return redirect()->back()->with('success', '商品已刪除');
}
/**
* 下載匯入範本
*/
public function template()
{
return Excel::download(new ProductTemplateExport, '商品匯入範本.xlsx');
}
/**
* 匯入商品
*/
public function import(Request $request)
{
$request->validate([
'file' => 'required|file|mimes:xlsx,xls',
]);
try {
Excel::import(new ProductImport, $request->file('file'));
return redirect()->back()->with('success', '商品匯入成功');
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
$messages = [];
foreach ($failures as $failure) {
$messages[] = '第 ' . $failure->row() . ' 行: ' . implode(', ', $failure->errors());
}
return redirect()->back()->withErrors(['file' => implode("\n", $messages)]);
} catch (\Exception $e) {
return redirect()->back()->withErrors(['file' => '匯入失敗: ' . $e->getMessage()]);
}
}
}

View File

@@ -19,7 +19,7 @@ class SafetyStockController extends Controller
*/
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) {
@@ -31,7 +31,51 @@ class SafetyStockController extends Controller
];
});
// 準備現有庫存列表 (用於庫存量對比)
// 獲取現有庫存 (用於抓取「已在倉庫中」的商品)
$inventoryProductIds = Inventory::where('warehouse_id', $warehouse->id)->pluck('product_id')->unique();
// 準備安全庫存設定列表 (從資料庫讀取)
$existingSettings = WarehouseProductSafetyStock::where('warehouse_id', $warehouse->id)
->with(['product.category', 'product.baseUnit'])
->get();
$existingProductIds = $existingSettings->pluck('product_id')->toArray();
// 找出:有庫存但是「還沒設定過安全庫存」的商品
$missingProductIds = $inventoryProductIds->diff($existingProductIds);
$missingProducts = Product::whereIn('id', $missingProductIds)
->with(['category', 'baseUnit'])
->get();
// 合併:已設定的 + 有庫存未設定的 (預設值 0)
$safetyStockSettings = $existingSettings->map(function ($setting) {
return [
'id' => (string) $setting->id,
'warehouseId' => (string) $setting->warehouse_id,
'productId' => (string) $setting->product_id,
'productName' => $setting->product->name,
'productType' => $setting->product->category ? $setting->product->category->name : '其他',
'safetyStock' => (float) $setting->safety_stock,
'unit' => $setting->product->baseUnit?->name ?? '個',
'updatedAt' => $setting->updated_at->toIso8601String(),
'isNew' => false, // 標記為舊有設定
];
})->concat($missingProducts->map(function ($product) use ($warehouse) {
return [
'id' => 'temp_' . $product->id, // 暫時 ID
'warehouseId' => (string) $warehouse->id,
'productId' => (string) $product->id,
'productName' => $product->name,
'productType' => $product->category ? $product->category->name : '其他',
'safetyStock' => 0, // 預設 0
'unit' => $product->baseUnit?->name ?? '個',
'updatedAt' => now()->toIso8601String(),
'isNew' => true, // 標記為建議新增
];
}))->values();
// 原本的 inventories 映射 (供顯示對比)
$inventories = Inventory::where('warehouse_id', $warehouse->id)
->select('product_id', DB::raw('SUM(quantity) as total_quantity'))
->groupBy('product_id')
@@ -43,23 +87,6 @@ class SafetyStockController extends Controller
];
});
// 準備安全庫存設定列表 (從新表格讀取)
$safetyStockSettings = WarehouseProductSafetyStock::where('warehouse_id', $warehouse->id)
->with(['product.category', 'product.baseUnit'])
->get()
->map(function ($setting) {
return [
'id' => (string) $setting->id,
'warehouseId' => (string) $setting->warehouse_id,
'productId' => (string) $setting->product_id,
'productName' => $setting->product->name,
'productType' => $setting->product->category ? $setting->product->category->name : '其他',
'safetyStock' => (float) $setting->safety_stock,
'unit' => $setting->product->baseUnit?->name ?? '個',
'updatedAt' => $setting->updated_at->toIso8601String(),
];
});
return Inertia::render('Warehouse/SafetyStockSettings', [
'warehouse' => $warehouse,
'safetyStockSettings' => $safetyStockSettings,

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Models\Category;
use App\Modules\Inventory\Models\Warehouse;
use Illuminate\Http\Request;
use Inertia\Inertia;
class StockQueryController extends Controller
{
protected InventoryServiceInterface $inventoryService;
public function __construct(InventoryServiceInterface $inventoryService)
{
$this->inventoryService = $inventoryService;
}
/**
* 即時庫存查詢頁面
*/
public function index(Request $request)
{
$filters = $request->only(['warehouse_id', 'category_id', 'search', 'status', 'sort_by', 'sort_order', 'per_page']);
$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);
return Inertia::render('Inventory/StockQuery/Index', [
'filters' => $filters,
'summary' => $result['summary'],
'inventories' => [
'data' => $result['data'],
'total' => $result['pagination']['total'],
'per_page' => $result['pagination']['per_page'],
'current_page' => $result['pagination']['current_page'],
'last_page' => $result['pagination']['last_page'],
'links' => $result['pagination']['links'],
],
'warehouses' => Warehouse::select('id', 'name')->orderBy('name')->get(),
'categories' => Category::select('id', 'name')->orderBy('name')->get(),
]);
}
/**
* Excel 匯出
*/
public function export(Request $request)
{
$filters = $request->only(['warehouse_id', 'category_id', 'search', 'status']);
return \Maatwebsite\Excel\Facades\Excel::download(
new \App\Modules\Inventory\Exports\StockQueryExport($filters),
'即時庫存查詢_' . now()->format('Ymd_His') . '.xlsx'
);
}
}

View File

@@ -0,0 +1,448 @@
<?php
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\StoreRequisition;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Services\StoreRequisitionService;
use App\Modules\Core\Contracts\CoreServiceInterface;
use Illuminate\Http\Request;
use Inertia\Inertia;
class StoreRequisitionController extends Controller
{
protected StoreRequisitionService $service;
protected CoreServiceInterface $coreService;
public function __construct(
StoreRequisitionService $service,
CoreServiceInterface $coreService
) {
$this->service = $service;
$this->coreService = $coreService;
}
/**
* 叫貨單列表
*/
public function index(Request $request)
{
$query = StoreRequisition::query();
// 搜尋(單號)
if ($request->search) {
$query->where('doc_no', 'like', "%{$request->search}%");
}
// 狀態篩選
if ($request->status && $request->status !== 'all') {
$query->where('status', $request->status);
}
// 倉庫篩選
if ($request->warehouse_id) {
$query->where('store_warehouse_id', $request->warehouse_id);
}
// 日期範圍
if ($request->date_start) {
$query->whereDate('created_at', '>=', $request->date_start);
}
if ($request->date_end) {
$query->whereDate('created_at', '<=', $request->date_end);
}
// 排序
$sortField = $request->input('sort_by', 'id');
$sortOrder = $request->input('sort_order', 'desc');
$allowedSorts = ['id', 'doc_no', 'status', 'created_at', 'submitted_at'];
if (in_array($sortField, $allowedSorts)) {
$query->orderBy($sortField, $sortOrder);
} else {
$query->orderBy('id', 'desc');
}
$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();
// 水和倉庫名稱與使用者名稱
$warehouses = Warehouse::select('id', 'name', 'type')->get();
$warehouseMap = $warehouses->keyBy('id');
$userIds = $requisitions->getCollection()
->pluck('created_by')
->merge($requisitions->getCollection()->pluck('approved_by'))
->filter()
->unique()
->toArray();
$users = $this->coreService->getUsersByIds($userIds)->keyBy('id');
$requisitions->getCollection()->transform(function ($req) use ($warehouseMap, $users) {
$req->store_warehouse_name = $warehouseMap->get($req->store_warehouse_id)?->name ?? '-';
$req->supply_warehouse_name = $warehouseMap->get($req->supply_warehouse_id)?->name ?? '-';
$req->creator_name = $users->get($req->created_by)?->name ?? '-';
$req->approver_name = $users->get($req->approved_by)?->name ?? '-';
return $req;
});
return Inertia::render('StoreRequisition/Index', [
'requisitions' => $requisitions,
'filters' => $request->only(['search', 'status', 'warehouse_id', 'date_start', 'date_end', 'sort_by', 'sort_order', 'per_page']),
'warehouses' => $warehouses->map(fn($w) => ['id' => $w->id, 'name' => $w->name]),
]);
}
/**
* 新增頁面
*/
public function create()
{
$warehouses = Warehouse::select('id', 'name', 'type')->get();
$products = Product::select('id', 'name', 'code', 'base_unit_id')
->with('baseUnit:id,name')
->where('is_active', true)
->get();
return Inertia::render('StoreRequisition/Create', [
'warehouses' => $warehouses->map(fn($w) => [
'id' => $w->id,
'name' => $w->name,
'type' => $w->type?->value,
]),
'products' => $products->map(fn($p) => [
'id' => $p->id,
'name' => $p->name,
'code' => $p->code,
'unit_name' => $p->baseUnit?->name,
]),
]);
}
/**
* 儲存叫貨單
*/
public function store(Request $request)
{
$request->validate([
'store_warehouse_id' => 'required|exists:warehouses,id',
'remark' => 'nullable|string|max:500',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required|exists:products,id',
'items.*.requested_qty' => 'required|numeric|min:0.01',
'items.*.remark' => 'nullable|string|max:200',
], [
'items.required' => '至少需要一項商品',
'items.min' => '至少需要一項商品',
'items.*.requested_qty.min' => '需求數量必須大於 0',
]);
$submitImmediately = $request->boolean('submit_immediately');
$requisition = $this->service->create(
$request->only(['store_warehouse_id', 'remark']),
$request->items,
auth()->id(),
$submitImmediately
);
if ($submitImmediately) {
return redirect()->route('store-requisitions.index')
->with('success', '叫貨單已提交審核');
}
return redirect()->route('store-requisitions.show', $requisition->id)
->with('success', '叫貨單已儲存為草稿');
}
/**
* 叫貨單詳情
*/
public function show($id)
{
$requisition = StoreRequisition::with([
'items.product.baseUnit',
'transferOrder.items' // 載入產生的調撥單明細與批號
])->findOrFail($id);
// 水和倉庫
$warehouses = Warehouse::select('id', 'name', 'type')->get();
$warehouseMap = $warehouses->keyBy('id');
$requisition->store_warehouse_name = $warehouseMap->get($requisition->store_warehouse_id)?->name ?? '-';
$requisition->supply_warehouse_name = $warehouseMap->get($requisition->supply_warehouse_id)?->name ?? '-';
// 水和使用者
$userIds = collect([$requisition->created_by, $requisition->approved_by])->filter()->unique()->toArray();
$users = $this->coreService->getUsersByIds($userIds)->keyBy('id');
$requisition->creator_name = $users->get($requisition->created_by)?->name ?? '-';
$requisition->approver_name = $users->get($requisition->approved_by)?->name ?? '-';
// 水和明細商品資訊
$requisition->items->transform(function ($item) {
$item->product_name = $item->product?->name ?? '-';
$item->product_code = $item->product?->code ?? '-';
$item->unit_name = $item->product?->baseUnit?->name ?? '-';
return $item;
});
// 取得庫存資訊(顯示該商品在申請倉庫的現有庫存量)
$productIds = $requisition->items->pluck('product_id')->toArray();
$inventories = Inventory::where('warehouse_id', $requisition->store_warehouse_id)
->whereIn('product_id', $productIds)
->select('product_id')
->selectRaw('SUM(quantity) as total_qty')
->groupBy('product_id')
->get()
->keyBy('product_id');
// 取得供貨倉庫的可用庫存
$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;
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;
});
// 操作紀錄
$activities = \Spatie\Activitylog\Models\Activity::where('subject_type', StoreRequisition::class)
->where('subject_id', $requisition->id)
->orderBy('created_at', 'desc')
->get();
return Inertia::render('StoreRequisition/Show', [
'requisition' => $requisition,
'warehouses' => $warehouses->map(fn($w) => ['id' => $w->id, 'name' => $w->name]),
'activities' => $activities,
]);
}
/**
* 編輯頁面
*/
public function edit($id)
{
$requisition = StoreRequisition::with(['items.product.baseUnit'])->findOrFail($id);
if (!in_array($requisition->status, ['draft', 'rejected'])) {
return redirect()->route('store-requisitions.show', $id)
->with('error', '僅能編輯草稿或被駁回的叫貨單');
}
$warehouses = Warehouse::select('id', 'name', 'type')->get();
$products = Product::select('id', 'name', 'code', 'base_unit_id')
->with('baseUnit:id,name')
->where('is_active', true)
->get();
return Inertia::render('StoreRequisition/Create', [
'requisition' => $requisition,
'warehouses' => $warehouses->map(fn($w) => [
'id' => $w->id,
'name' => $w->name,
'type' => $w->type?->value,
]),
'products' => $products->map(fn($p) => [
'id' => $p->id,
'name' => $p->name,
'code' => $p->code,
'unit_name' => $p->baseUnit?->name,
]),
]);
}
/**
* 更新叫貨單
*/
public function update(Request $request, $id)
{
$requisition = StoreRequisition::findOrFail($id);
$request->validate([
'store_warehouse_id' => 'required|exists:warehouses,id',
'remark' => 'nullable|string|max:500',
'items' => 'required|array|min:1',
'items.*.product_id' => 'required|exists:products,id',
'items.*.requested_qty' => 'required|numeric|min:0.01',
'items.*.remark' => 'nullable|string|max:200',
]);
$requisition = $this->service->update(
$requisition,
$request->only(['store_warehouse_id', 'remark']),
$request->items
);
// 如果需要直接提交
if ($request->boolean('submit_immediately')) {
$this->service->submit($requisition, auth()->id());
return redirect()->route('store-requisitions.index')
->with('success', '叫貨單已重新提交審核');
}
return redirect()->route('store-requisitions.show', $requisition->id)
->with('success', '叫貨單已更新');
}
/**
* 提交審核
*/
public function submit($id)
{
$requisition = StoreRequisition::findOrFail($id);
$this->service->submit($requisition, auth()->id());
return redirect()->route('store-requisitions.show', $id)
->with('success', '叫貨單已提交審核');
}
/**
* 核准叫貨單
*/
public function approve(Request $request, $id)
{
$requisition = StoreRequisition::findOrFail($id);
$request->validate([
'items' => 'required|array',
'items.*.id' => 'required|exists:store_requisition_items,id',
'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)) {
return back()->withErrors(['supply_warehouse_id' => '請先選擇供貨倉庫']);
}
$this->service->approve($requisition, $request->only(['items']), auth()->id());
return redirect()->route('store-requisitions.show', $id)
->with('success', '叫貨單已核准,調撥單已自動產生');
}
/**
* 駁回叫貨單
*/
public function reject(Request $request, $id)
{
$requisition = StoreRequisition::findOrFail($id);
$request->validate([
'reject_reason' => 'required|string|max:500',
], [
'reject_reason.required' => '請填寫駁回原因',
]);
$this->service->reject($requisition, $request->reject_reason, auth()->id());
return redirect()->route('store-requisitions.show', $id)
->with('success', '叫貨單已駁回');
}
/**
* 更新供貨倉庫
*/
public function updateSupplyWarehouse(Request $request, $id)
{
$requisition = StoreRequisition::findOrFail($id);
if ($requisition->status !== 'pending') {
return back()->withErrors(['error' => '僅能在待審核狀態修改供貨倉庫']);
}
$request->validate([
'supply_warehouse_id' => 'required|exists:warehouses,id',
]);
$requisition->update([
'supply_warehouse_id' => $request->supply_warehouse_id,
]);
return redirect()->back()->with('success', '供貨倉庫已更新');
}
/**
* 刪除叫貨單(僅限草稿)
*/
public function destroy($id)
{
$requisition = StoreRequisition::findOrFail($id);
if ($requisition->status !== 'draft') {
return back()->withErrors(['error' => '僅能刪除草稿狀態的叫貨單']);
}
$requisition->items()->delete();
$requisition->delete();
return redirect()->route('store-requisitions.index')
->with('success', '叫貨單已刪除');
}
}

View 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
]);
}
}

View File

@@ -3,135 +3,323 @@
namespace App\Modules\Inventory\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Inventory;
use App\Enums\WarehouseType;
use App\Modules\Inventory\Models\InventoryTransferOrder;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Services\TransferService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
class TransferOrderController extends Controller
{
/**
* 儲存撥補單(建立調撥單並執行庫存轉移)
*/
protected $transferService;
public function __construct(TransferService $transferService)
{
$this->transferService = $transferService;
}
public function index(Request $request)
{
$query = InventoryTransferOrder::query()
->with(['fromWarehouse', 'toWarehouse', 'createdBy', 'postedBy']);
// 搜尋:單號或備註
if ($request->filled('search')) {
$query->where(function ($q) use ($request) {
$q->where('doc_no', 'like', "%{$request->search}%")
->orWhere('remarks', 'like', "%{$request->search}%");
});
}
// 篩選:若有選定倉庫,則顯示該倉庫作為來源或目的地的調撥單
if ($request->filled('warehouse_id')) {
$query->where(function ($q) use ($request) {
$q->where('from_warehouse_id', $request->warehouse_id)
->orWhere('to_warehouse_id', $request->warehouse_id);
});
}
$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')
->paginate($perPage)
->withQueryString()
->through(function ($order) {
return [
'id' => (string) $order->id,
'doc_no' => $order->doc_no,
'from_warehouse_name' => $order->fromWarehouse->name,
'to_warehouse_name' => $order->toWarehouse->name,
'status' => $order->status,
'created_at' => $order->created_at->format('Y-m-d H:i'),
'posted_at' => $order->posted_at ? $order->posted_at->format('Y-m-d H:i') : '-',
'created_by' => $order->createdBy?->name,
];
});
return Inertia::render('Inventory/Transfer/Index', [
'orders' => $orders,
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
'filters' => $request->only(['search', 'warehouse_id', 'per_page']),
]);
}
public function store(Request $request)
{
// 兼容前端不同的參數命名 (from/source, to/target)
$fromId = $request->input('from_warehouse_id') ?? $request->input('sourceWarehouseId');
$toId = $request->input('to_warehouse_id') ?? $request->input('targetWarehouseId');
$validated = $request->validate([
'sourceWarehouseId' => 'required|exists:warehouses,id',
'targetWarehouseId' => 'required|exists:warehouses,id|different:sourceWarehouseId',
'productId' => 'required|exists:products,id',
'quantity' => 'required|numeric|min:0.01',
'transferDate' => 'required|date',
'status' => 'required|in:待處理,處理中,已完成,已取消', // 目前僅支援立即完成或單純記錄
'from_warehouse_id' => 'required_without:sourceWarehouseId|exists:warehouses,id',
'to_warehouse_id' => 'required_without:targetWarehouseId|exists:warehouses,id|different:from_warehouse_id',
'transit_warehouse_id' => 'nullable|exists:warehouses,id',
'remarks' => 'nullable|string',
'notes' => 'nullable|string',
'batchNumber' => 'nullable|string', // 暫時接收,雖然 DB 可能沒存
'instant_post' => 'boolean',
// 支援單筆商品直接建立 (撥補單模式)
'product_id' => 'nullable|exists:products,id',
'quantity' => 'nullable|numeric|min:0.01',
'batch_number' => 'nullable|string',
]);
return DB::transaction(function () use ($validated) {
// 1. 檢查來源倉庫庫存 (精確匹配產品與批號)
$sourceInventory = Inventory::where('warehouse_id', $validated['sourceWarehouseId'])
->where('product_id', $validated['productId'])
->where('batch_number', $validated['batchNumber'])
->first();
$remarks = $validated['remarks'] ?? $validated['notes'] ?? null;
$transitWarehouseId = $validated['transit_warehouse_id'] ?? null;
if (!$sourceInventory || $sourceInventory->quantity < $validated['quantity']) {
throw ValidationException::withMessages([
'quantity' => ['來源倉庫指定批號庫存不足'],
]);
}
$order = $this->transferService->createOrder(
$fromId,
$toId,
$remarks,
auth()->id(),
$transitWarehouseId
);
// 2. 獲取或建立目標倉庫庫存 (精確匹配產品與批號,並繼承效期與品質狀態)
$targetInventory = Inventory::firstOrCreate(
[
'warehouse_id' => $validated['targetWarehouseId'],
'product_id' => $validated['productId'],
'batch_number' => $validated['batchNumber'],
],
[
'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,
// 手動發送「已建立」日誌,因為服務層使用了 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');
$sourceWarehouse = Warehouse::find($validated['sourceWarehouseId']);
$targetWarehouse = Warehouse::find($validated['targetWarehouseId']);
if ($request->input('instant_post') === true) {
try {
$this->transferService->dispatch($order, auth()->id());
// 3. 執行庫存轉移 (扣除來源)
$oldSourceQty = $sourceInventory->quantity;
$newSourceQty = $oldSourceQty - $validated['quantity'];
// 設定活動紀錄原因
$sourceInventory->activityLogReason = "撥補出庫 至 {$targetWarehouse->name}";
$sourceInventory->quantity = $newSourceQty;
$sourceInventory->total_value = $sourceInventory->quantity * $sourceInventory->unit_cost; // 更新總值
$sourceInventory->save();
// 記錄來源異動
$sourceInventory->transactions()->create([
'type' => '撥補出庫',
'quantity' => -$validated['quantity'],
'unit_cost' => $sourceInventory->unit_cost, // 記錄
'balance_before' => $oldSourceQty,
'balance_after' => $newSourceQty,
'reason' => "撥補至 {$targetWarehouse->name}" . ($validated['notes'] ? " ({$validated['notes']})" : ""),
'actual_time' => $validated['transferDate'],
'user_id' => auth()->id(),
]);
// 4. 執行庫存轉移 (增加目標)
$oldTargetQty = $targetInventory->quantity;
$newTargetQty = $oldTargetQty + $validated['quantity'];
// 設定活動紀錄原因
$targetInventory->activityLogReason = "撥補入庫 來自 {$sourceWarehouse->name}";
// 確保目標庫存也有成本 (如果是繼承來的)
if ($targetInventory->unit_cost == 0 && $sourceInventory->unit_cost > 0) {
$targetInventory->unit_cost = $sourceInventory->unit_cost;
return redirect()->back()->with('success', '撥補成功,庫存已更新');
} catch (\Exception $e) {
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
}
$targetInventory->quantity = $newTargetQty;
$targetInventory->total_value = $targetInventory->quantity * $targetInventory->unit_cost; // 更新總值
$targetInventory->save();
}
// 記錄目標異動
$targetInventory->transactions()->create([
'type' => '撥補入庫',
'quantity' => $validated['quantity'],
'unit_cost' => $targetInventory->unit_cost, // 記錄
'balance_before' => $oldTargetQty,
'balance_after' => $newTargetQty,
'reason' => "來自 {$sourceWarehouse->name} 的撥補" . ($validated['notes'] ? " ({$validated['notes']})" : ""),
'actual_time' => $validated['transferDate'],
'user_id' => auth()->id(),
return redirect()->route('inventory.transfer.show', [$order->id])
->with('success', '已建立調撥單');
}
public function show(InventoryTransferOrder $order)
{
$order->load(['items.product.baseUnit', 'fromWarehouse', 'toWarehouse', 'transitWarehouse', 'createdBy', 'postedBy', 'dispatchedBy', 'receivedBy', 'storeRequisition']);
$orderData = [
'id' => (string) $order->id,
'doc_no' => $order->doc_no,
'from_warehouse_id' => (string) $order->from_warehouse_id,
'from_warehouse_name' => $order->fromWarehouse->name,
'from_warehouse_default_transit' => $order->fromWarehouse->default_transit_warehouse_id ? (string)$order->fromWarehouse->default_transit_warehouse_id : null,
'to_warehouse_id' => (string) $order->to_warehouse_id,
'to_warehouse_name' => $order->toWarehouse->name,
'to_warehouse_type' => $order->toWarehouse->type->value,
// 在途倉資訊
'transit_warehouse_id' => $order->transit_warehouse_id ? (string) $order->transit_warehouse_id : null,
'transit_warehouse_name' => $order->transitWarehouse?->name,
'transit_warehouse_plate' => $order->transitWarehouse?->license_plate,
'transit_warehouse_driver' => $order->transitWarehouse?->driver_name,
'status' => $order->status,
'remarks' => $order->remarks,
'created_at' => $order->created_at->format('Y-m-d H:i'),
'created_by' => $order->createdBy?->name,
'posted_at' => $order->posted_at?->format('Y-m-d H:i'),
'posted_by' => $order->postedBy?->name,
'dispatched_at' => $order->dispatched_at?->format('Y-m-d H:i'),
'dispatched_by' => $order->dispatchedBy?->name,
'received_at' => $order->received_at?->format('Y-m-d H:i'),
'received_by' => $order->receivedBy?->name,
'requisition' => $order->storeRequisition ? [
'id' => (string) $order->storeRequisition->id,
'doc_no' => $order->storeRequisition->doc_no,
] : null,
'items' => $order->items->map(function ($item) use ($order) {
$stock = Inventory::where('warehouse_id', $order->from_warehouse_id)
->where('product_id', $item->product_id)
->where('batch_number', $item->batch_number)
->first();
return [
'id' => (string) $item->id,
'product_id' => (string) $item->product_id,
'product_name' => $item->product->name,
'product_code' => $item->product->code,
'batch_number' => $item->batch_number,
'expiry_date' => $stock && $stock->expiry_date ? $stock->expiry_date->format('Y-m-d') : null,
'unit' => $item->product->baseUnit?->name,
'quantity' => (float) $item->quantity,
'position' => $item->position,
'max_quantity' => $item->snapshot_quantity ? (float) $item->snapshot_quantity : ($stock ? (float) $stock->quantity : 0.0),
'notes' => $item->notes,
];
}),
];
// 取得在途倉庫列表供前端選擇
$transitWarehouses = Warehouse::where('type', WarehouseType::TRANSIT)
->get()
->map(fn($w) => [
'id' => (string) $w->id,
'name' => $w->name,
'license_plate' => $w->license_plate,
'driver_name' => $w->driver_name,
]);
// TODO: 未來若有獨立的 TransferOrder 模型,可在此建立紀錄
return Inertia::render('Inventory/Transfer/Show', [
'order' => $orderData,
'transitWarehouses' => $transitWarehouses,
]);
}
return redirect()->back()->with('success', '撥補單已建立且庫存已轉移');
});
public function update(Request $request, InventoryTransferOrder $order)
{
// 收貨動作:僅限 dispatched 狀態
if ($request->input('action') === 'receive') {
if ($order->status !== 'dispatched') {
return redirect()->back()->with('error', '僅能對已出貨的調撥單進行收貨確認');
}
try {
$this->transferService->receive($order, auth()->id());
return redirect()->route('inventory.transfer.index')
->with('success', '調撥單已收貨完成');
} catch (ValidationException $e) {
return redirect()->back()->withErrors($e->errors());
} catch (\Exception $e) {
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
}
}
// 以下操作僅限草稿
if ($order->status !== 'draft') {
return redirect()->back()->with('error', '只能修改草稿狀態的單據');
}
// 1. 更新在途倉庫(如果前端有傳)
if ($request->has('transit_warehouse_id')) {
$order->transit_warehouse_id = $request->input('transit_warehouse_id') ?: null;
}
// 2. 先更新資料 (如果請求中包含 items則先執行儲存)
$itemsChanged = false;
if ($request->has('items')) {
if ($order->storeRequisition()->exists()) {
return redirect()->back()->with('error', '由叫貨單自動產生的調撥單無法修改明細');
}
$validated = $request->validate([
'items' => 'array',
'items.*.product_id' => 'required|exists:products,id',
'items.*.quantity' => 'required|numeric|min:0.01',
'items.*.batch_number' => 'nullable|string',
'items.*.position' => 'nullable|string',
'items.*.notes' => 'nullable|string',
]);
$itemsChanged = $this->transferService->updateItems($order, $validated['items']);
}
$remarksChanged = false;
if ($request->has('remarks')) {
$remarksChanged = $order->remarks !== $request->input('remarks');
$order->remarks = $request->input('remarks');
}
if ($itemsChanged || $remarksChanged || $order->isDirty()) {
$order->touch();
$message = '儲存成功';
} else {
$message = '資料未變更';
}
// 3. 判斷是否需要出貨/過帳
if ($request->input('action') === 'post') {
try {
$this->transferService->dispatch($order, auth()->id());
$hasTransit = !empty($order->transit_warehouse_id);
$successMsg = $hasTransit ? '調撥單已出貨,庫存已轉入在途倉' : '調撥單已過帳完成';
return redirect()->route('inventory.transfer.index')
->with('success', $successMsg);
} catch (ValidationException $e) {
return redirect()->back()->withErrors($e->errors());
} catch (\Exception $e) {
return redirect()->back()->withErrors(['items' => $e->getMessage()]);
}
}
return redirect()->back()->with('success', $message);
}
public function destroy(InventoryTransferOrder $order)
{
if ($order->status !== 'draft') {
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->delete();
return redirect()->route('inventory.transfer.index')
->with('success', '調撥單已刪除');
}
/**
* 獲取特定倉庫的庫存列表 (API)
* 獲取特定倉庫的庫存列表 (API) - 保留給前端選擇商品用
*/
public function getWarehouseInventories(Warehouse $warehouse)
{
$inventories = $warehouse->inventories()
->with(['product.baseUnit', 'product.category'])
->where('quantity', '>', 0) // 只回傳有庫存的
->where('quantity', '>', 0)
->get()
->map(function ($inv) {
return [
'product_id' => (string) $inv->product_id,
'product_name' => $inv->product->name,
'product_code' => $inv->product->code,
'product_barcode' => $inv->product->barcode,
'batch_number' => $inv->batch_number,
'quantity' => (float) $inv->quantity,
'unit_cost' => (float) $inv->unit_cost, // 新增
'total_value' => (float) $inv->total_value, // 新增
'unit_cost' => (float) $inv->unit_cost,
'unit_name' => $inv->product->baseUnit?->name ?? '個',
'expiry_date' => $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
];
@@ -139,4 +327,30 @@ class TransferOrderController extends Controller
return response()->json($inventories);
}
public function importItems(Request $request, InventoryTransferOrder $order)
{
if ($order->status !== 'draft') {
return redirect()->back()->with('error', '只能在草稿狀態下匯入明細');
}
$request->validate([
'file' => 'required|file|mimes:xlsx,xls,csv',
]);
try {
\Maatwebsite\Excel\Facades\Excel::import(new \App\Modules\Inventory\Imports\InventoryTransferItemImport($order), $request->file('file'));
return redirect()->back()->with('success', '匯入成功');
} catch (\Exception $e) {
return redirect()->back()->with('error', '匯入失敗:' . $e->getMessage());
}
}
public function template()
{
return \Maatwebsite\Excel\Facades\Excel::download(
new \App\Modules\Inventory\Exports\InventoryTransferTemplateExport(),
'調撥單明細匯入範本.xlsx'
);
}
}

View File

@@ -24,69 +24,130 @@ class WarehouseController extends Controller
});
}
$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;
}
$warehouses = $query->withSum('inventories as book_stock', 'quantity') // 帳面庫存 = 所有庫存總和
->withSum('inventories as book_amount', 'total_value') // 帳面金額
->withSum(['inventories as available_stock' => function ($query) {
// 可用庫存 = 庫存 > 0 且 品質正常 且 (未過期 或 無效期)
// 可用庫存條件
$query->where('quantity', '>', 0)
->where('quality_status', 'normal')
->whereHas('warehouse', function ($q) {
$q->where('type', '!=', \App\Enums\WarehouseType::QUARANTINE);
})
->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
});
}], 'quantity')
->withSum(['inventories as available_amount' => function ($query) {
// 可用金額條件 (與可用庫存一致)
$query->where('quantity', '>', 0)
->where('quality_status', 'normal')
->whereHas('warehouse', function ($q) {
$q->where('type', '!=', \App\Enums\WarehouseType::QUARANTINE);
})
->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
});
}], 'total_value')
->withSum(['inventories as abnormal_amount' => function ($query) {
$query->where('quantity', '>', 0)
->where(function ($q) {
$q->where('quality_status', '!=', 'normal')
->orWhere(function ($sq) {
$sq->whereNotNull('expiry_date')
->where('expiry_date', '<', now());
})
->orWhereHas('warehouse', function ($wq) {
$wq->where('type', \App\Enums\WarehouseType::QUARANTINE);
});
});
}], 'total_value')
->addSelect(['low_stock_count' => function ($query) {
$query->selectRaw('count(*)')
->from('warehouse_product_safety_stocks as ss')
->whereColumn('ss.warehouse_id', 'warehouses.id')
->whereRaw('(SELECT COALESCE(SUM(quantity), 0) FROM inventories WHERE warehouse_id = ss.warehouse_id AND product_id = ss.product_id) < ss.safety_stock');
}])
->orderBy('created_at', 'desc')
->paginate(10)
->paginate($perPage)
->withQueryString();
// 修正各倉庫列表中的可用庫存計算:若倉庫不可銷售,則可用庫存為 0
$warehouses->getCollection()->transform(function ($w) {
if (!$w->is_sellable) {
$w->available_stock = 0;
}
return $w;
});
// 計算全域總計 (不分頁)
$totals = [
'available_stock' => \App\Modules\Inventory\Models\Inventory::where('quantity', '>', 0)
->where('quality_status', 'normal')
->whereHas('warehouse', function ($q) {
$q->where('is_sellable', true);
$q->where('type', '!=', \App\Enums\WarehouseType::QUARANTINE);
})
->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
})->sum('quantity'),
'available_amount' => \App\Modules\Inventory\Models\Inventory::where('quantity', '>', 0)
->where('quality_status', 'normal')
->whereHas('warehouse', function ($q) {
$q->where('type', '!=', \App\Enums\WarehouseType::QUARANTINE);
})
->where(function ($q) {
$q->whereNull('expiry_date')
->orWhere('expiry_date', '>=', now());
})->sum('total_value'),
'abnormal_amount' => \App\Modules\Inventory\Models\Inventory::where('quantity', '>', 0)
->where(function ($q) {
$q->where('quality_status', '!=', 'normal')
->orWhere(function ($sq) {
$sq->whereNotNull('expiry_date')
->where('expiry_date', '<', now());
})
->orWhereHas('warehouse', function ($wq) {
$wq->where('type', \App\Enums\WarehouseType::QUARANTINE);
});
})->sum('total_value'),
'book_stock' => \App\Modules\Inventory\Models\Inventory::sum('quantity'),
'book_amount' => \App\Modules\Inventory\Models\Inventory::sum('total_value'),
];
// 取得在途倉列表供前端選擇「預設在途倉」
$transitWarehouses = Warehouse::where('type', \App\Enums\WarehouseType::TRANSIT)
->select('id', 'name', 'license_plate', 'driver_name')
->orderBy('name')
->get()
->map(fn ($w) => [
'id' => (string) $w->id,
'name' => $w->name,
'license_plate' => $w->license_plate,
'driver_name' => $w->driver_name,
]);
return Inertia::render('Warehouse/Index', [
'warehouses' => $warehouses,
'totals' => $totals,
'filters' => $request->only(['search']),
'transitWarehouses' => $transitWarehouses,
'filters' => $request->only(['search', 'per_page']),
]);
}
public function store(Request $request)
{
$validated = $request->validate([
'code' => 'required|string|max:20|unique:warehouses,code',
'name' => 'required|string|max:50',
'address' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_sellable' => 'nullable|boolean',
'type' => 'required|string',
'license_plate' => 'nullable|string|max:20',
'driver_name' => 'nullable|string|max:50',
'default_transit_warehouse_id' => 'nullable|exists:warehouses,id',
]);
// 自動產生代碼
$prefix = 'WH';
$lastWarehouse = Warehouse::latest('id')->first();
$nextId = $lastWarehouse ? $lastWarehouse->id + 1 : 1;
$code = $prefix . str_pad($nextId, 3, '0', STR_PAD_LEFT);
$validated['code'] = $code;
Warehouse::create($validated);
return redirect()->back()->with('success', '倉庫已建立');
@@ -95,13 +156,14 @@ class WarehouseController extends Controller
public function update(Request $request, Warehouse $warehouse)
{
$validated = $request->validate([
'code' => 'required|string|max:20|unique:warehouses,code,' . $warehouse->id,
'name' => 'required|string|max:50',
'address' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_sellable' => 'nullable|boolean',
'type' => 'required|string',
'license_plate' => 'nullable|string|max:20',
'driver_name' => 'nullable|string|max:50',
'default_transit_warehouse_id' => 'nullable|exists:warehouses,id',
]);
$warehouse->update($validated);
@@ -111,8 +173,9 @@ class WarehouseController extends Controller
public function destroy(Warehouse $warehouse)
{
// 檢查是否有相關聯的採購單
if ($warehouse->purchaseOrders()->exists()) {
// 檢查是否有相關聯的採購單 (跨模組檢查,不使用模型關聯以符合解耦規範)
$hasPurchaseOrders = \App\Modules\Procurement\Models\PurchaseOrder::where('warehouse_id', $warehouse->id)->exists();
if ($hasPurchaseOrders) {
return redirect()->back()->with('error', '無法刪除:該倉庫有相關聯的採購單,請先處理採購單。');
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Modules\Inventory\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class GoodsReceiptApprovedEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $goodsReceiptId;
/**
* Create a new event instance.
*/
public function __construct(int $goodsReceiptId)
{
$this->goodsReceiptId = $goodsReceiptId;
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Modules\Inventory\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InstructionSheet implements FromCollection, WithHeadings, WithTitle, WithStyles
{
public function title(): string
{
return '填寫說明';
}
public function headings(): array
{
return [
'欄位名稱',
'是否必填',
'填寫說明',
];
}
public function collection()
{
return collect([
['商品代號', '選填', '2-8 碼,若未填寫系統將自動生成。若代號已存在,將更新該商品資料。'],
['條碼', '選填', '13 碼數字,若未填寫系統將自動生成。若條碼已存在(優先比對),將更新該商品資料。'],
['商品名稱', '必填', '請填寫完整商品名稱。'],
['類別名稱', '必填', '必須為系統中已存在的類別名稱(如:飲品)。'],
['品牌', '選填', '商品品牌名稱。'],
['規格', '選填', '商品規格描述25kg/袋)。'],
['基本單位', '必填', '必須為系統中已存在的單位名稱(如:瓶、個)。'],
['大單位', '選填', '若有大單位換算請填寫(如:箱)。'],
['換算率', '若有大單位則必填', '1 個大單位等於多少個基本單位。'],
['成本價', '選填', '數字,預設為 0。'],
['售價', '選填', '數字,預設為 0。'],
['會員價', '選填', '數字,預設為 0。'],
['批發價', '選填', '數字,預設為 0。'],
]);
}
public function styles(Worksheet $sheet)
{
return [
// 第一行標題粗體
1 => ['font' => ['bold' => true]],
// 欄位寬度自動
];
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Modules\Inventory\Exports;
use App\Modules\Inventory\Services\InventoryReportService;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InventoryReportExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles
{
protected $service;
protected $filters;
public function __construct(InventoryReportService $service, array $filters)
{
$this->service = $service;
$this->filters = $filters;
}
public function collection()
{
return $this->service->getReportData($this->filters, null); // perPage = null to get all
}
public function headings(): array
{
return [
'商品代碼',
'商品名稱',
'分類',
'進貨量',
'出貨量',
'調撥入',
'調撥出',
'調整量',
'淨變動',
];
}
public function map($row): array
{
return [
$row->product_code,
$row->product_name,
$row->category_name ?? '-',
$row->inbound_qty,
$row->outbound_qty,
$row->transfer_in_qty,
$row->transfer_out_qty,
$row->adjust_qty,
$row->net_change,
];
}
public function styles(Worksheet $sheet)
{
return [
1 => ['font' => ['bold' => true]],
];
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Modules\Inventory\Exports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class InventoryTemplateExport implements WithMultipleSheets
{
public function sheets(): array
{
return [
new InventoryDataSheet(),
new InventoryInstructionSheet(),
];
}
}
class InventoryDataSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize
{
public function array(): array
{
// 資料分頁保持完全空白
return [];
}
public function headings(): array
{
return [
'商品條碼',
'商品代號',
'商品名稱',
'數量',
'入庫單價',
'儲位/貨道',
'批號',
'產地',
'效期',
];
}
public function title(): string
{
return '資料填寫';
}
}
class InventoryInstructionSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize
{
public function array(): array
{
return [
['商品條碼', '擇一輸入', '系統會「優先」依據條碼匹配商品。若有填寫,條碼必須存在於系統中'],
['商品代號', '擇一輸入', '若條碼未填寫,系統會依據代號匹配商品'],
['商品名稱', '選填', '僅供對照參考,匯入時系統會自動忽略此欄位內容'],
['數量', '必填', '入庫的商品數量,須為大於 0 的數字'],
['入庫單價', '選填', '未填寫時將預設使用商品的「採購成本價」'],
['儲位/貨道', '選填', '一般倉庫請填寫「儲位(位址)」,販賣機倉庫請填寫「貨道編號」(如: A1)'],
['批號', '選填', '如需批次控管請填寫,若留空系統會自動標記為 "NO-BATCH"'],
['產地', '選填', '商品的生產地資訊 (如TW)'],
['效期', '選填', '格式請務必使用 YYYY-MM-DD (例如: 2026-12-31)'],
['', '', ''],
['倉庫類型參考', '', '系統支援以下倉庫性質:'],
['標準倉', '', '一般總倉、儲備倉'],
['生產倉', '', '加工廠、中央廚房、原材料存放處'],
['門市倉', '', '前台通路、店舖銷售現場'],
['販賣機', '', 'IoT 自動販賣機設備,建議搭配「貨道」填寫'],
['', '', ''],
['匹配與匯入規則', '', '1. 系統會優先比對「商品條碼」,其次為「商品代號」。'],
['', '', '2. 庫存將匯入至您在匯入前於系統介面所選擇的目標倉庫。'],
['', '', '3. 若需區分不同貨道或批次,請分行填寫。'],
];
}
public function headings(): array
{
return ['欄位名稱', '必要性', '填寫說明'];
}
public function title(): string
{
return '填寫規則';
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Modules\Inventory\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InventoryTransferTemplateExport implements WithMultipleSheets
{
use Exportable;
public function sheets(): array
{
return [
new class implements FromCollection, WithHeadings, WithTitle, WithStyles {
public function collection()
{
return collect([
['P001', 'BATCH-2024001', '10', 'A1', '範例:請刪除此列後填寫'],
]);
}
public function headings(): array
{
return ['商品代碼', '批號', '數量', '貨道/儲位', '備註'];
}
public function title(): string
{
return '明細匯入';
}
public function styles(Worksheet $sheet)
{
return [
1 => ['font' => ['bold' => true]],
];
}
},
new class implements FromCollection, WithHeadings, WithTitle, WithStyles {
public function collection()
{
return collect([
['商品代碼', '必填', '請填寫系統中已存在的商品代號'],
['數量', '必填', '必須為大於 0 的數字'],
['批號', '選填', '若不填寫將自動對應「NO-BATCH」庫存'],
['貨道/儲位', '選填', '主要用於目的倉庫為「販賣機」時指定貨道'],
['備註', '選填', '可填寫該筆明細的備註說明'],
['', '', ''],
['提示', '附加模式', '匯入的明細將附加至現有單據,不會覆蓋原有資料'],
]);
}
public function headings(): array
{
return ['欄位名稱', '必要性', '說明'];
}
public function title(): string
{
return '匯入規則說明';
}
public function styles(Worksheet $sheet)
{
$sheet->getColumnDimension('A')->setWidth(15);
$sheet->getColumnDimension('B')->setWidth(15);
$sheet->getColumnDimension('C')->setWidth(50);
return [
1 => ['font' => ['bold' => true]],
];
}
},
];
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Modules\Inventory\Exports;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class ProductImportSheet implements WithHeadings, WithColumnFormatting, WithTitle
{
public function title(): string
{
return '商品匯入';
}
public function headings(): array
{
return [
'商品代號(選填)',
'條碼(選填)',
'商品名稱',
'類別名稱',
'品牌',
'規格',
'基本單位',
'大單位',
'換算率',
'成本價',
'售價',
'會員價',
'批發價',
];
}
public function columnFormats(): array
{
return [
'A' => NumberFormat::FORMAT_TEXT, // 商品代號
'B' => NumberFormat::FORMAT_TEXT, // 條碼
];
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Modules\Inventory\Exports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class ProductTemplateExport implements WithMultipleSheets
{
public function sheets(): array
{
return [
new ProductImportSheet(),
new InstructionSheet(),
];
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace App\Modules\Inventory\Exports;
use App\Modules\Inventory\Models\Inventory;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class StockQueryExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles
{
protected array $filters;
public function __construct(array $filters = [])
{
$this->filters = $filters;
}
public function collection()
{
$today = now()->toDateString();
$expiryThreshold = now()->addDays(30)->toDateString();
$query = Inventory::query()
->join('products', 'inventories.product_id', '=', 'products.id')
->join('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
->leftJoin('categories', 'products.category_id', '=', 'categories.id')
->leftJoin('warehouse_product_safety_stocks as ss', function ($join) {
$join->on('inventories.warehouse_id', '=', 'ss.warehouse_id')
->on('inventories.product_id', '=', 'ss.product_id');
})
->whereNull('inventories.deleted_at')
->select([
'inventories.id',
'inventories.quantity',
'inventories.batch_number',
'inventories.expiry_date',
'inventories.quality_status',
'products.code as product_code',
'products.name as product_name',
'categories.name as category_name',
'warehouses.name as warehouse_name',
'ss.safety_stock',
]);
// 篩選
if (!empty($this->filters['warehouse_id'])) {
$query->where('inventories.warehouse_id', $this->filters['warehouse_id']);
}
if (!empty($this->filters['category_id'])) {
$query->where('products.category_id', $this->filters['category_id']);
}
if (!empty($this->filters['search'])) {
$search = $this->filters['search'];
$query->where(function ($q) use ($search) {
$q->where('products.code', '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'])) {
switch ($this->filters['status']) {
case 'low_stock':
$query->whereNotNull('ss.safety_stock')
->whereRaw('inventories.quantity <= ss.safety_stock')
->where('inventories.quantity', '>=', 0);
break;
case 'negative':
$query->where('inventories.quantity', '<', 0);
break;
case 'expiring':
$query->whereNotNull('inventories.expiry_date')
->where('inventories.expiry_date', '>', $today)
->where('inventories.expiry_date', '<=', $expiryThreshold);
break;
case 'expired':
$query->whereNotNull('inventories.expiry_date')
->where('inventories.expiry_date', '<=', $today);
break;
case 'abnormal':
$query->where(function ($q) use ($today, $expiryThreshold) {
$q->where('inventories.quantity', '<', 0)
->orWhere(function ($q2) {
$q2->whereNotNull('ss.safety_stock')
->whereRaw('inventories.quantity <= ss.safety_stock');
})
->orWhere(function ($q2) use ($expiryThreshold) {
$q2->whereNotNull('inventories.expiry_date')
->where('inventories.expiry_date', '<=', $expiryThreshold);
});
});
break;
}
}
return $query->orderBy('products.code', 'asc')->get();
}
public function headings(): array
{
return [
'商品代碼',
'商品名稱',
'分類',
'倉庫',
'批號',
'數量',
'安全庫存',
'到期日',
'品質狀態',
'狀態',
];
}
public function map($row): array
{
$today = now()->toDateString();
$expiryThreshold = now()->addDays(30)->toDateString();
$statuses = [];
if ($row->quantity < 0) {
$statuses[] = '負庫存';
}
if ($row->safety_stock !== null && $row->quantity <= $row->safety_stock && $row->quantity >= 0) {
$statuses[] = '低庫存';
}
if ($row->expiry_date) {
if ($row->expiry_date <= $today) {
$statuses[] = '已過期';
} elseif ($row->expiry_date <= $expiryThreshold) {
$statuses[] = '即將過期';
}
}
if (empty($statuses)) {
$statuses[] = '正常';
}
$qualityLabels = [
'normal' => '正常',
'inspecting' => '檢驗中',
'rejected' => '不合格',
];
return [
$row->product_code,
$row->product_name,
$row->category_name ?? '-',
$row->warehouse_name,
$row->batch_number ?? '-',
$row->quantity,
$row->safety_stock ?? '-',
$row->expiry_date ?? '-',
$qualityLabels[$row->quality_status] ?? $row->quality_status ?? '-',
implode('、', $statuses),
];
}
public function styles(Worksheet $sheet): array
{
return [
1 => ['font' => ['bold' => true]],
];
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace App\Modules\Inventory\Imports;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Inventory;
use App\Modules\Inventory\Models\Warehouse;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
use Illuminate\Support\Facades\DB;
class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMapping, SkipsEmptyRows
{
private $warehouse;
private $inboundDate;
private $notes;
public function __construct(Warehouse $warehouse, string $inboundDate, ?string $notes = null)
{
HeadingRowFormatter::default('none');
$this->warehouse = $warehouse;
// 修正時間精度:將選定的日期與「現在的時分秒」結合
// 這樣既能保留使用者選的日期,又能提供精確的紀錄時點排順序
$this->inboundDate = \Illuminate\Support\Carbon::parse($inboundDate)->setTimeFrom(now())->toDateTimeString();
$this->notes = $notes;
}
public function map($row): array
{
// 處理條碼或代號為字串
if (isset($row['商品條碼'])) {
$row['商品條碼'] = (string) $row['商品條碼'];
}
if (isset($row['商品代號'])) {
$row['商品代號'] = (string) $row['商品代號'];
}
if (isset($row['儲位/貨道'])) {
$row['儲位/貨道'] = (string) $row['儲位/貨道'];
}
return $row;
}
public function model(array $row)
{
// 查找商品
$product = null;
if (!empty($row['商品條碼'])) {
$product = Product::where('barcode', $row['商品條碼'])->first();
}
if (!$product && !empty($row['商品代號'])) {
$product = Product::where('code', $row['商品代號'])->first();
}
if (!$product) {
return null; // 透過 Validation 攔截
}
$quantity = (float) $row['數量'];
$unitCost = isset($row['入庫單價']) ? (float) $row['入庫單價'] : ($product->cost_price ?? 0);
$location = $row['儲位/貨道'] ?? null;
// 批號邏輯:若 Excel 留空則使用 NO-BATCH
$batchNumber = !empty($row['批號']) ? $row['批號'] : 'NO-BATCH';
$originCountry = $row['產地'] ?? 'TW';
$expiryDate = !empty($row['效期']) ? $row['效期'] : null;
return DB::transaction(function () use ($product, $quantity, $unitCost, $location, $batchNumber, $originCountry, $expiryDate) {
// 使用與 InventoryController 相同的 firstOrNew 邏輯
$inventory = $this->warehouse->inventories()->withTrashed()->firstOrNew(
[
'product_id' => $product->id,
'batch_number' => $batchNumber,
'location' => $location, // 加入儲位/貨道作為區分關鍵字
],
[
'quantity' => 0,
'unit_cost' => $unitCost,
'total_value' => 0,
'arrival_date' => $this->inboundDate,
'expiry_date' => $expiryDate,
'origin_country' => $originCountry,
]
);
if ($inventory->trashed()) {
$inventory->restore();
}
// 更新數量
$oldQty = $inventory->quantity;
$inventory->quantity += $quantity;
// 更新單價與總價值
$inventory->unit_cost = $unitCost;
$inventory->total_value = $inventory->quantity * $unitCost;
$inventory->saveQuietly();
// 記錄交易歷史
$inventory->transactions()->create([
'warehouse_id' => $this->warehouse->id,
'product_id' => $product->id,
'batch_number' => $inventory->batch_number,
'quantity' => $quantity,
'unit_cost' => $unitCost,
'type' => '手動入庫',
'reason' => 'Excel 匯入入庫',
'balance_before' => $oldQty,
'balance_after' => $inventory->quantity,
'actual_time' => $this->inboundDate,
'notes' => $this->notes,
'expiry_date' => $inventory->expiry_date,
]);
return $inventory;
});
}
public function rules(): array
{
return [
'商品條碼' => ['nullable', 'string'],
'商品代號' => ['nullable', 'string'],
'數量' => [
'required_with:商品條碼,商品代號', // 只有在有商品資訊時,數量才是必填
'numeric',
'min:0' // 允許數量為 0
],
'入庫單價' => ['nullable', 'numeric', 'min:0'],
'儲位/貨道' => ['nullable', 'string', 'max:50'],
'批號' => ['nullable', 'string'],
'效期' => ['nullable', 'date'],
'產地' => ['nullable', 'string', 'max:2'],
];
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace App\Modules\Inventory\Imports;
use App\Modules\Inventory\Models\InventoryTransferItem;
use App\Modules\Inventory\Models\InventoryTransferOrder;
use App\Modules\Inventory\Models\Product;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Exception;
class InventoryTransferItemImport implements ToCollection, WithMultipleSheets
{
protected $transferOrder;
public function __construct(InventoryTransferOrder $transferOrder)
{
$this->transferOrder = $transferOrder;
}
public function collection(Collection $rows)
{
if ($rows->isEmpty()) {
throw new Exception("檔案中沒有資料。");
}
// 移除標題列並解析索引
$headerRow = $rows->shift();
$headers = $headerRow->toArray();
// 建立標題對應索引 (支援中文與英文)
$colMap = [
'product_code' => -1,
'batch_number' => -1,
'quantity' => -1,
'position' => -1,
'notes' => -1,
];
foreach ($headers as $index => $label) {
$label = trim((string)$label);
if (in_array($label, ['商品代碼', 'product_code', 'shang_pin_dai_ma'])) $colMap['product_code'] = $index;
if (in_array($label, ['批號', 'batch_number', 'pi_hao'])) $colMap['batch_number'] = $index;
if (in_array($label, ['數量', 'quantity', 'shu_liang'])) $colMap['quantity'] = $index;
if (in_array($label, ['貨道/儲位', '貨道', 'position', 'slot', 'huo_dao'])) $colMap['position'] = $index;
if (in_array($label, ['備註', 'notes', 'bei_zhu'])) $colMap['notes'] = $index;
}
// 檢查必要欄位是否有找到
if ($colMap['product_code'] === -1 || $colMap['quantity'] === -1) {
$foundHeaders = implode(', ', array_filter($headers));
throw new Exception("找不到必要的欄位「商品代碼」或「數量」。讀取到的標題為:{$foundHeaders}。請確認使用的是正確的範本。");
}
// 預先載入商品 (優化效能)
$productCodes = $rows->map(fn($row) => trim((string)($row[$colMap['product_code']] ?? '')))->filter()->unique()->toArray();
$products = Product::whereIn('code', $productCodes)->get()->keyBy('code');
$newItems = [];
$errors = [];
foreach ($rows as $index => $row) {
$productCode = trim((string)($row[$colMap['product_code']] ?? ''));
$quantity = $row[$colMap['quantity']] ?? null;
$batchNumber = $colMap['batch_number'] !== -1 ? trim((string)($row[$colMap['batch_number']] ?? '')) : '';
$position = $colMap['position'] !== -1 ? trim((string)($row[$colMap['position']] ?? '')) : null;
$notes = $colMap['notes'] !== -1 ? ($row[$colMap['notes']] ?? null) : null;
// 跳過全空行
if (empty($productCode) && ($quantity === null || $quantity === '')) {
continue;
}
$lineNum = $index + 2; // 因為 shift 過,且 Excel 從 1 開始
if (empty($productCode)) {
$errors[] = "{$lineNum} 行:商品代碼不能為空";
continue;
}
$product = $products->get($productCode);
if (!$product) {
$errors[] = "{$lineNum} 行:找不到商品代碼 '{$productCode}'";
continue;
}
if (!is_numeric($quantity) || (float)$quantity <= 0) {
$errors[] = "{$lineNum} 行:數量必須為大於 0 的數字 (目前值: " . ($quantity ?? '空') . ")";
continue;
}
if (empty($batchNumber)) {
$batchNumber = 'NO-BATCH';
}
$newItems[] = [
'transfer_order_id' => $this->transferOrder->id,
'product_id' => $product->id,
'batch_number' => $batchNumber,
'quantity' => (float)$quantity,
'position' => $position,
'notes' => $notes,
'created_at' => now(),
'updated_at' => now(),
];
}
if (count($errors) > 0) {
throw new Exception(implode("\n", $errors));
}
if (count($newItems) === 0) {
throw new Exception("檔案中沒有可匯入的有效資料。");
}
InventoryTransferItem::insert($newItems);
$this->transferOrder->touch();
}
/**
* 指定只匯入第一個分頁 (明細匯入)
*/
public function sheets(): array
{
return [
0 => $this,
];
}
}

View File

@@ -0,0 +1,185 @@
<?php
namespace App\Modules\Inventory\Imports;
use App\Modules\Inventory\Models\Category;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Unit;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
/**
* 商品匯入主類別
*
* 實作 WithMultipleSheets 以限定只讀取第一個工作表(資料頁),
* 跳過第二個工作表(填寫說明頁),避免說明頁的資料被誤匯入並觸發驗證錯誤。
*/
class ProductImport implements WithMultipleSheets
{
public function __construct()
{
// 禁用標題格式化,保留中文標題
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 查詢
$this->categories = Category::pluck('id', 'name');
$this->units = Unit::pluck('id', 'name');
$this->productService = app(\App\Modules\Inventory\Contracts\ProductServiceInterface::class);
}
/**
* 資料映射:將 Excel 原始標題(含「(選填)」)對應到乾淨的鍵名
*
* 注意WithValidation 驗證的是 map() 之前的原始資料,
* 因此 rules() 中的鍵名必須匹配 Excel 的原始標題。
* map() 的返回值只影響 model() 接收到的資料。
*/
public function map($row): array
{
$code = $row['商品代號(選填)'] ?? $row['商品代號'] ?? null;
$barcode = $row['條碼(選填)'] ?? $row['條碼'] ?? null;
return [
'商品代號' => $code !== null ? (string)$code : null,
'條碼' => $barcode !== null ? (string)$barcode : null,
'商品名稱' => $row['商品名稱'] ?? null,
'類別名稱' => $row['類別名稱'] ?? null,
'品牌' => $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 (map() 回傳的乾淨鍵名陣列)
*/
public function model(array $row)
{
// 查找關聯 ID
$categoryId = $this->categories[$row['類別名稱']] ?? null;
$baseUnitId = $this->units[$row['基本單位']] ?? null;
$largeUnitId = isset($row['大單位']) ? ($this->units[$row['大單位']] ?? null) : null;
// 若必要關聯找不到,理論上 Validation 會攔截,但此處做防禦性編程
if (!$categoryId || !$baseUnitId) {
return null;
}
$code = $row['商品代號'] ?? null;
$barcode = $row['條碼'] ?? null;
// Upsert 邏輯:透過 Service 統一查找與處理
$product = $this->productService->findByBarcodeOrCode($barcode, $code);
$data = [
'name' => $row['商品名稱'],
'category_id' => $categoryId,
'brand' => $row['品牌'] ?? null,
'specification' => $row['規格'] ?? null,
'base_unit_id' => $baseUnitId,
'large_unit_id' => $largeUnitId,
'conversion_rate' => $row['換算率'] ?? null,
'purchase_unit_id' => null,
'cost_price' => $row['成本價'] ?? null,
'price' => $row['售價'] ?? null,
'member_price' => $row['會員價'] ?? null,
'wholesale_price' => $row['批發價'] ?? null,
];
if ($product) {
$this->productService->updateProduct($product, $data);
} else {
if (!empty($code)) $data['code'] = $code;
if (!empty($barcode)) $data['barcode'] = $barcode;
$this->productService->createProduct($data);
}
return null; // 返回 null因為 Service 已經處理完儲存
}
/**
* 驗證規則
*
* 鍵名必須匹配 Excel 原始標題(含「(選填)」後綴),
* 因為 WithValidation 驗證的是 map() 之前的原始資料。
*/
public function rules(): array
{
return [
'商品代號(選填)' => ['nullable', 'string', 'min:2', 'max:8'],
'條碼(選填)' => ['nullable', 'string'],
'商品名稱' => ['required', 'string'],
'類別名稱' => ['required', function($attribute, $value, $fail) {
if (!isset($this->categories[$value])) {
$fail("找不到類別: " . $value);
}
}],
'基本單位' => ['required', function($attribute, $value, $fail) {
if (!isset($this->units[$value])) {
$fail("找不到單位: " . $value);
}
}],
'大單位' => ['nullable', function($attribute, $value, $fail) {
if ($value && !isset($this->units[$value])) {
$fail("找不到單位: " . $value);
}
}],
'換算率' => ['nullable', 'numeric', 'min:0.0001', 'required_with:大單位'],
'成本價' => ['nullable', 'numeric', 'min:0'],
'售價' => ['nullable', 'numeric', 'min:0'],
'會員價' => ['nullable', 'numeric', 'min:0'],
'批發價' => ['nullable', 'numeric', 'min:0'],
];
}
/**
* 自訂驗證錯誤訊息的欄位名稱
* 把含 "(選填)" 後綴的欄位顯示為友善名稱
*/
public function customValidationAttributes(): array
{
return [
'商品代號(選填)' => '商品代號',
'條碼(選填)' => '條碼',
];
}
}

View File

@@ -4,13 +4,20 @@ namespace App\Modules\Inventory;
use Illuminate\Support\ServiceProvider;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Contracts\ProductServiceInterface;
use App\Modules\Inventory\Services\InventoryService;
use App\Modules\Inventory\Services\ProductService;
class InventoryServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(InventoryServiceInterface::class, InventoryService::class);
$this->app->bind(ProductServiceInterface::class, ProductService::class);
$this->app->bind(
\App\Modules\Inventory\Contracts\GoodsReceiptServiceInterface::class,
\App\Modules\Inventory\Services\GoodsReceiptService::class
);
}
public function boot(): void

View File

@@ -29,12 +29,27 @@ class Category extends Model
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['name'] = $this->name;
$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;
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class GoodsReceipt extends Model
{
use HasFactory, SoftDeletes;
use \Spatie\Activitylog\Traits\LogsActivity;
public const STATUS_DRAFT = 'draft';
public const STATUS_PENDING_AUDIT = 'pending_audit';
public const STATUS_COMPLETED = 'completed';
public const STATUS_REJECTED = 'rejected';
protected $fillable = [
'code',
'type',
'warehouse_id',
'purchase_order_id',
'vendor_id',
'received_date',
'status',
'remarks',
'user_id',
];
protected $casts = [
'received_date' => 'date:Y-m-d',
];
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
{
return \Spatie\Activitylog\LogOptions::defaults()
->logAll()
->logOnlyDirty()
->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()
{
return $this->hasMany(GoodsReceiptItem::class);
}
// Strict Mode: relationships to Warehouse is allowed (same module).
public function warehouse()
{
return $this->belongsTo(Warehouse::class);
}
// Strict Mode: cross-module relationship to Vendor/User/PurchaseOrder is restricted.
// They are accessed via IDs or Services.
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class GoodsReceiptItem extends Model
{
use HasFactory;
protected $fillable = [
'goods_receipt_id',
'product_id',
'purchase_order_item_id',
'quantity_received',
'unit_price',
'total_amount',
'batch_number',
'expiry_date',
];
protected $casts = [
'quantity_received' => 'decimal:2',
'unit_price' => 'decimal:2', // 暫定價格
'total_amount' => 'decimal:2',
'expiry_date' => 'date:Y-m-d',
];
public function goodsReceipt()
{
return $this->belongsTo(GoodsReceipt::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
}

View File

@@ -17,6 +17,7 @@ class Inventory extends Model
'warehouse_id',
'product_id',
'quantity',
'reserved_quantity',
'location',
'unit_cost',
'total_value',
@@ -34,6 +35,8 @@ class Inventory extends Model
protected $casts = [
'arrival_date' => 'date:Y-m-d',
'expiry_date' => 'date:Y-m-d',
'quantity' => 'decimal:4',
'reserved_quantity' => 'decimal:4',
'unit_cost' => 'decimal:4',
'total_value' => 'decimal:4',
];
@@ -55,8 +58,11 @@ class Inventory extends Model
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
// 核心:轉換為陣列以避免 Indirect modification error
$properties = $activity->properties instanceof \Illuminate\Support\Collection
? $activity->properties->toArray()
: $activity->properties;
$snapshot = $properties['snapshot'] ?? [];
// 始終對名稱進行快照以便於上下文顯示,即使 ID 未更改
@@ -66,11 +72,28 @@ class Inventory extends Model
// 如果已設定原因,則進行捕捉
if ($this->activityLogReason) {
$attributes['_reason'] = $this->activityLogReason;
$properties['attributes']['_reason'] = $this->activityLogReason;
}
$properties['attributes'] = $attributes;
$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;
}
@@ -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();
}
/**
* 產生批號

View File

@@ -0,0 +1,144 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Modules\Core\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class InventoryAdjustDoc extends Model
{
use HasFactory, LogsActivity;
protected $fillable = [
'doc_no',
'count_doc_id',
'warehouse_id',
'status',
'reason',
'remarks',
'posted_at',
'created_by',
'updated_by',
'posted_by',
];
protected $casts = [
'posted_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->doc_no)) {
$today = date('Ymd');
$prefix = 'ADJ-' . $today . '-';
$lastDoc = static::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';
}
$model->doc_no = $prefix . $nextNumber;
}
});
}
public function warehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class);
}
public function countDoc(): BelongsTo
{
return $this->belongsTo(InventoryCountDoc::class, 'count_doc_id');
}
public function items(): HasMany
{
return $this->hasMany(InventoryAdjustItem::class, 'adjust_doc_id');
}
public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function postedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'posted_by');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable()
->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 key information
$snapshot['doc_no'] = $this->doc_no;
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : null;
$snapshot['posted_at'] = $this->posted_at ? $this->posted_at->format('Y-m-d H:i:s') : null;
$snapshot['status'] = $this->status;
$snapshot['created_by_name'] = $this->createdBy ? $this->createdBy->name : null;
$snapshot['posted_by_name'] = $this->postedBy ? $this->postedBy->name : null;
$properties['snapshot'] = $snapshot;
// 全域 ID 轉名稱邏輯 (用於 attributes 與 old)
$convertIdsToNames = function (&$data) {
if (empty($data) || !is_array($data)) return;
// 倉庫 ID 轉換
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
$warehouse = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id']);
if ($warehouse) {
$data['warehouse_id'] = $warehouse->name;
}
}
// 使用者 ID 轉換
$userFields = ['created_by', 'updated_by', 'posted_by'];
foreach ($userFields as $field) {
if (isset($data[$field]) && is_numeric($data[$field])) {
$user = \App\Modules\Core\Models\User::find($data[$field]);
if ($user) {
$data[$field] = $user->name;
}
}
}
};
if (isset($properties['attributes'])) {
$convertIdsToNames($properties['attributes']);
}
if (isset($properties['old'])) {
$convertIdsToNames($properties['old']);
}
$activity->properties = $properties;
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class InventoryAdjustItem extends Model
{
use HasFactory;
protected $fillable = [
'adjust_doc_id',
'product_id',
'batch_number',
'qty_before',
'adjust_qty', // 增減數量
'notes',
];
protected $casts = [
'qty_before' => 'decimal:2',
'adjust_qty' => 'decimal:2',
];
public function doc(): BelongsTo
{
return $this->belongsTo(InventoryAdjustDoc::class, 'adjust_doc_id');
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Modules\Core\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class InventoryCountDoc extends Model
{
use HasFactory;
use LogsActivity;
protected $fillable = [
'doc_no',
'warehouse_id',
'status',
'snapshot_date',
'completed_at',
'remarks',
'created_by',
'updated_by',
'completed_by',
];
protected $casts = [
'snapshot_date' => 'datetime',
'completed_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->doc_no)) {
$today = date('Ymd');
$prefix = 'CNT-' . $today . '-';
// 查詢當天編號最大的單據
$lastDoc = static::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';
}
$model->doc_no = $prefix . $nextNumber;
}
});
}
public function warehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class);
}
public function items(): HasMany
{
return $this->hasMany(InventoryCountItem::class, 'count_doc_id');
}
public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function completedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'completed_by');
}
public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
{
return \Spatie\Activitylog\LogOptions::defaults()
->logFillable()
->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 key information
$snapshot['doc_no'] = $this->doc_no;
$snapshot['warehouse_name'] = $this->warehouse ? $this->warehouse->name : null;
$snapshot['completed_at'] = $this->completed_at ? $this->completed_at->format('Y-m-d H:i:s') : null;
$snapshot['status'] = $this->status;
$snapshot['created_by_name'] = $this->createdBy ? $this->createdBy->name : null;
$snapshot['completed_by_name'] = $this->completedBy ? $this->completedBy->name : null;
$properties['snapshot'] = $snapshot;
// 全域 ID 轉名稱邏輯 (用於 attributes 與 old)
$convertIdsToNames = function (&$data) {
if (empty($data) || !is_array($data)) return;
// 倉庫 ID 轉換
if (isset($data['warehouse_id']) && is_numeric($data['warehouse_id'])) {
$warehouse = \App\Modules\Inventory\Models\Warehouse::find($data['warehouse_id']);
if ($warehouse) {
$data['warehouse_id'] = $warehouse->name;
}
}
// 使用者 ID 轉換
$userFields = ['created_by', 'updated_by', 'completed_by'];
foreach ($userFields as $field) {
if (isset($data[$field]) && is_numeric($data[$field])) {
$user = \App\Modules\Core\Models\User::find($data[$field]);
if ($user) {
$data[$field] = $user->name;
}
}
}
};
if (isset($properties['attributes'])) {
$convertIdsToNames($properties['attributes']);
}
if (isset($properties['old'])) {
$convertIdsToNames($properties['old']);
}
$activity->properties = $properties;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class InventoryCountItem extends Model
{
use HasFactory;
protected $fillable = [
'count_doc_id',
'product_id',
'batch_number',
'system_qty',
'counted_qty',
'diff_qty',
'notes',
];
protected $casts = [
'system_qty' => 'decimal:2',
'counted_qty' => 'decimal:2',
'diff_qty' => 'decimal:2',
];
public function doc(): BelongsTo
{
return $this->belongsTo(InventoryCountDoc::class, 'count_doc_id');
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}

View File

@@ -10,6 +10,7 @@ class InventoryTransaction extends Model
{
/** @use HasFactory<\Database\Factories\InventoryTransactionFactory> */
use HasFactory;
use \Spatie\Activitylog\Traits\LogsActivity;
protected $fillable = [
'inventory_id',
@@ -26,7 +27,9 @@ class InventoryTransaction extends Model
];
protected $casts = [
'actual_time' => 'datetime',
// actual_time 不做時區轉換,保留原始字串格式(台北時間)
// 原因:資料庫儲存的是台北時間,但 MySQL 時區為 UTC
// 若使用 datetime castLaravel 會誤當作 UTC 再轉回台北時間,造成偏移
'unit_cost' => 'decimal:4',
];
@@ -39,4 +42,49 @@ class InventoryTransaction extends Model
{
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;
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class InventoryTransferItem extends Model
{
use HasFactory;
protected $fillable = [
'transfer_order_id',
'product_id',
'batch_number',
'quantity',
'position',
'snapshot_quantity',
'notes',
];
protected $casts = [
'quantity' => 'decimal:2',
];
public function order(): BelongsTo
{
return $this->belongsTo(InventoryTransferOrder::class, 'transfer_order_id');
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}

View File

@@ -0,0 +1,203 @@
<?php
namespace App\Modules\Inventory\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use App\Modules\Core\Models\User;
class InventoryTransferOrder extends Model
{
use HasFactory, LogsActivity;
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable()
->dontSubmitEmptyLogs();
}
/**
* @var array 暫存的活動紀錄屬性 (不會存入資料庫)
*/
public $activityProperties = [];
/**
* 自定義日誌屬性名稱解析
*/
public function tapActivity(\Spatie\Activitylog\Models\Activity $activity, string $eventName)
{
$properties = $activity->properties->toArray();
// 處置日誌事件說明
if ($eventName === 'created') {
$activity->description = 'created';
} elseif ($eventName === 'updated') {
if (isset($properties['attributes']['status']) && $properties['attributes']['status'] === 'completed') {
$activity->description = 'posted';
$eventName = 'posted';
} else {
$activity->description = 'updated';
}
}
// 處理 ID 轉名稱 (核心:支援 attributes 與 old 的自動轉換)
$idToNameFields = [
'from_warehouse_id' => 'fromWarehouse',
'to_warehouse_id' => 'toWarehouse',
'transit_warehouse_id' => 'transitWarehouse',
'created_by' => 'createdBy',
'posted_by' => 'postedBy',
'dispatched_by' => 'dispatchedBy',
'received_by' => 'receivedBy',
];
foreach (['attributes', 'old'] as $part) {
if (isset($properties[$part])) {
foreach ($idToNameFields as $idField => $relation) {
if (isset($properties[$part][$idField])) {
$id = $properties[$part][$idField];
if (!$id) continue;
$nameField = str_replace('_id', '_name', $idField);
$name = null;
try {
if ($this->relationLoaded($relation) && $this->$relation && $this->$relation->id == $id) {
$name = $this->$relation->name;
} else {
$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;
}
}
}
}
// 基本單據資訊快照
if (in_array($eventName, ['created', 'updated', 'posted', 'deleted'])) {
$properties['snapshot'] = [
'doc_no' => $this->doc_no,
'from_warehouse_name' => $this->fromWarehouse?->name,
'to_warehouse_name' => $this->toWarehouse?->name,
'status' => $this->status,
];
}
// 移除輔助欄位與雜訊
if (isset($properties['attributes'])) {
unset($properties['attributes']['activityProperties']);
unset($properties['attributes']['updated_at']);
}
if (isset($properties['old'])) {
unset($properties['old']['updated_at']);
}
// 合併暫存屬性 (重要:例如 items_diff)
if (!empty($this->activityProperties)) {
$properties = array_merge($properties, $this->activityProperties);
}
$activity->properties = collect($properties);
}
protected $fillable = [
'doc_no',
'from_warehouse_id',
'to_warehouse_id',
'transit_warehouse_id',
'status',
'remarks',
'posted_at',
'created_by',
'updated_by',
'posted_by',
'dispatched_at',
'dispatched_by',
'received_at',
'received_by',
];
protected $casts = [
'posted_at' => 'datetime',
'dispatched_at' => 'datetime',
'received_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->doc_no)) {
$today = date('Ymd');
$prefix = 'TRF-' . $today . '-';
$lastDoc = static::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';
}
$model->doc_no = $prefix . $nextNumber;
}
});
}
public function fromWarehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class, 'from_warehouse_id');
}
public function toWarehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class, 'to_warehouse_id');
}
public function items(): HasMany
{
return $this->hasMany(InventoryTransferItem::class, 'transfer_order_id');
}
public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function storeRequisition(): \Illuminate\Database\Eloquent\Relations\HasOne
{
return $this->hasOne(StoreRequisition::class, 'transfer_order_id');
}
public function postedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'posted_by');
}
public function transitWarehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class, 'transit_warehouse_id');
}
public function dispatchedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'dispatched_by');
}
public function receivedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'received_by');
}
}

View File

@@ -17,7 +17,9 @@ class Product extends Model
protected $fillable = [
'code',
'barcode',
'name',
'external_pos_id',
'category_id',
'brand',
'specification',
@@ -25,10 +27,17 @@ class Product extends Model
'large_unit_id',
'conversion_rate',
'purchase_unit_id',
'location',
'cost_price',
'price',
'member_price',
'wholesale_price',
'is_active',
];
protected $casts = [
'conversion_rate' => 'decimal:4',
'is_active' => 'boolean',
];
/**
@@ -76,30 +85,50 @@ class Product extends Model
public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
{
$properties = $activity->properties;
$attributes = $properties['attributes'] ?? [];
// 核心:轉換為陣列以避免 Indirect modification error
$properties = $activity->properties instanceof \Illuminate\Support\Collection
? $activity->properties->toArray()
: $activity->properties;
$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;
$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;
$activity->properties = $properties;
}

Some files were not shown because too many files have changed in this diff Show More