Compare commits

...

4 Commits

Author SHA1 Message Date
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
8 changed files with 138 additions and 33 deletions

View File

@@ -1,6 +1,5 @@
---
name: Git 分支管理與開發規範
description: 強制執行 main 分支保護與開發分支流程,確保主分支僅用於 Bug 修正與版本釋放。
trigger: always_on
---
# Git 分支管理與開發規範 (Git Workflow)
@@ -47,4 +46,4 @@ description: 強制執行 main 分支保護與開發分支流程,確保主分
---
> [!IMPORTANT]
> 身為 AI 助手 (Antigravity),我會監控合併對象與當前時間。若您的命令涉及合併至 `main` 且不在允許時段內,我會優先進行安全提醒。
> 身為 AI 助手 (Antigravity),我會監控合併對象與當前時間。若您的命令涉及合併至 `main` 且不在允許時段內,我會優先進行安全提醒。

View File

@@ -19,7 +19,7 @@ class UtilityFee extends Model
];
protected $casts = [
'transaction_date' => 'date',
'transaction_date' => 'date:Y-m-d',
'amount' => 'decimal:2',
];

View File

@@ -57,9 +57,31 @@ class InventoryController extends Controller
->pluck('safety_stock', 'product_id')
->mapWithKeys(fn($val, $key) => [(string)$key => (float)$val]);
$items = $warehouse->inventories()
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction'])
->get();
$query = $warehouse->inventories()
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction']);
// 加入搜尋過濾
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';

View File

@@ -58,7 +58,9 @@ class StockQueryExport implements FromCollection, WithHeadings, WithMapping, Sho
$search = $this->filters['search'];
$query->where(function ($q) use ($search) {
$q->where('products.code', 'like', "%{$search}%")
->orWhere('products.name', 'like', "%{$search}%");
->orWhere('products.name', 'like', "%{$search}%")
->orWhere('inventories.batch_number', 'like', "%{$search}%")
->orWhere(\Illuminate\Support\Facades\DB::raw("CONCAT('BATCH-', inventories.id)"), 'like', "%{$search}%");
});
}
if (!empty($this->filters['status'])) {

View File

@@ -303,12 +303,14 @@ class InventoryService implements InventoryServiceInterface
$query->where('products.category_id', $filters['category_id']);
}
// 篩選:關鍵字(商品代碼或名稱)
// 篩選:關鍵字(商品代碼或名稱或批號
if (!empty($filters['search'])) {
$search = $filters['search'];
$query->where(function ($q) use ($search) {
$q->where('products.code', 'like', "%{$search}%")
->orWhere('products.name', 'like', "%{$search}%");
->orWhere('products.name', 'like', "%{$search}%")
->orWhere('inventories.batch_number', 'like', "%{$search}%")
->orWhere(\Illuminate\Support\Facades\DB::raw("CONCAT('BATCH-', inventories.id)"), 'like', "%{$search}%");
});
}

View File

@@ -436,6 +436,21 @@ class ProductionOrderController extends Controller
throw new \Exception('必須提供成品批號');
}
// --- 新增:計算原物料投入總成本 ---
$totalCost = 0;
$items = $productionOrder->items()->with('inventory')->get();
foreach ($items as $item) {
if ($item->inventory) {
$totalCost += ($item->quantity_used * ($item->inventory->unit_cost ?? 0));
}
}
// 計算單位成本 (若產出數量為 0 則設為 0 避免除以零錯誤)
$unitCost = $productionOrder->output_quantity > 0
? $totalCost / $productionOrder->output_quantity
: 0;
// --------------------------------
// 更新單據資訊:批號、效期與自動記錄生產日期
$productionOrder->output_batch_number = $batchNumber;
$productionOrder->expiry_date = $expiryDate;
@@ -446,6 +461,7 @@ class ProductionOrderController extends Controller
'warehouse_id' => $warehouseId,
'product_id' => $productionOrder->product_id,
'quantity' => $productionOrder->output_quantity,
'unit_cost' => $unitCost, // 傳入計算後的單位成本
'batch_number' => $batchNumber,
'box_number' => $productionOrder->output_box_count,
'arrival_date' => now()->toDateString(),

View File

@@ -1,5 +1,6 @@
import { useState } from "react";
import { useState, useCallback, useEffect } from "react";
import { Head, router } from "@inertiajs/react";
import { debounce } from "lodash";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import {
Search,
@@ -126,9 +127,14 @@ export default function StockQueryIndex({
filters.per_page || inventories.per_page?.toString() || "10"
);
// 執行篩選
const applyFilters = (newFilters: Record<string, string | undefined>) => {
const merged = { ...filters, ...newFilters, page: undefined };
// 同步 URL 的 search 參數到 local state
useEffect(() => {
setSearch(filters.search || "");
}, [filters.search]);
// 執行篩選核心
const applyFiltersWithOptions = (newFilters: Record<string, string | undefined>, currentFilters: typeof filters) => {
const merged = { ...currentFilters, ...newFilters, page: undefined };
// 移除空值
const cleaned: Record<string, string> = {};
Object.entries(merged).forEach(([key, value]) => {
@@ -143,8 +149,27 @@ export default function StockQueryIndex({
});
};
// 搜尋
const applyFilters = (newFilters: Record<string, string | undefined>) => {
applyFiltersWithOptions(newFilters, filters);
};
// Debounced Search Handler
const debouncedSearch = useCallback(
debounce((term: string, currentFilters: typeof filters) => {
applyFiltersWithOptions({ search: term || undefined }, currentFilters);
}, 500),
[]
);
// 搜尋值的改變
const handleSearchChange = (val: string) => {
setSearch(val);
debouncedSearch(val, filters);
};
// 點擊搜尋按鈕或按下 Enter 鍵立即搜尋
const handleSearch = () => {
debouncedSearch.cancel();
applyFilters({ search: search || undefined });
};
@@ -372,9 +397,9 @@ export default function StockQueryIndex({
<Input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
onChange={(e) => handleSearchChange(e.target.value)}
onKeyDown={handleSearchKeyDown}
placeholder="搜尋商品代碼或名稱..."
placeholder="搜尋商品代碼或名稱或批號..."
className="h-9"
/>
<Button

View File

@@ -1,4 +1,5 @@
import { useState, useMemo, useEffect } from "react";
import { debounce } from "lodash";
import { ArrowLeft, PackagePlus, AlertTriangle, Shield, Boxes, FileUp } from "lucide-react";
import { Button } from "@/Components/ui/button";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
@@ -43,28 +44,38 @@ export default function WarehouseInventoryPage({
const [deleteId, setDeleteId] = useState<string | null>(null);
const [importDialogOpen, setImportDialogOpen] = useState(false);
// 當搜尋或篩選變更時,同步到 URL (使用 replace: true 避免產生過多歷史紀錄)
useEffect(() => {
const params: any = {};
if (searchTerm) params.search = searchTerm;
if (typeFilter !== "all") params.type = typeFilter;
// 當搜尋或篩選變更時,延後同步到 URL 以避免輸入時頻繁請求伺服器
const debouncedRouterGet = useMemo(() => {
return debounce((term: string, type: string) => {
const params: any = {};
if (term) params.search = term;
if (type !== "all") params.type = type;
router.get(
route("warehouses.inventory.index", warehouse.id),
params,
{
preserveState: true,
preserveScroll: true,
replace: true,
only: ["inventories"], // 僅重新拉取數據,避免全頁重新渲染 (如有後端過濾)
}
);
}, [searchTerm, typeFilter]);
router.get(
route("warehouses.inventory.index", warehouse.id),
params,
{
preserveState: true,
preserveScroll: true,
replace: true,
only: ["inventories"], // 僅重新拉取數據
}
);
}, 500);
}, [warehouse.id]);
useEffect(() => {
debouncedRouterGet(searchTerm, typeFilter);
// 清理 function在元件卸載時取消未執行的 debounce
return () => debouncedRouterGet.cancel();
}, [searchTerm, typeFilter, debouncedRouterGet]);
// 篩選庫存列表
const filteredInventories = useMemo(() => {
return inventories.filter((group) => {
// 搜尋條件:匹配商品名稱、編號 或 該商品下任一批號
// 註:後端已經過濾過一次,前端保留此邏輯是為了確保 typeFilter 能正確協同工作,
// 且支援在已載入數據中進行二次即時過濾(如有需要)。
const matchesSearch = !searchTerm ||
group.productName.toLowerCase().includes(searchTerm.toLowerCase()) ||
(group.productCode && group.productCode.toLowerCase().includes(searchTerm.toLowerCase())) ||
@@ -81,6 +92,34 @@ export default function WarehouseInventoryPage({
});
}, [inventories, searchTerm, typeFilter, availableProducts]);
// 當搜尋關鍵字變更且長度足夠時,自動將符合條件的商品展開
useEffect(() => {
if (searchTerm && searchTerm.length >= 2) {
const matchedIds = filteredInventories
.filter(group => group.batches.some(b => b.batchNumber.toLowerCase().includes(searchTerm.toLowerCase())))
.map(group => group.productId);
if (matchedIds.length > 0) {
// 讀取目前的 storage 狀態並合併,避免覆寫手動展開的行為
const storageKey = `inventory_expanded_${warehouse.id}`;
const savedExpandStr = sessionStorage.getItem(storageKey);
let currentExpanded = [];
try {
currentExpanded = savedExpandStr ? JSON.parse(savedExpandStr) : [];
} catch (e) { }
const newExpanded = Array.from(new Set([...currentExpanded, ...matchedIds]));
// 如果有新展開的才更新 sessionStorage (這會透過其他地方的事件或是手動控制處發生效果)
// 這裡我們直接手動更新 DOM 或 等待重新渲染。因為 InventoryTable 也監聽 sessionStorage (初始化時)
// 更好的做法是讓 InventoryTable 的 expanded 狀態由外部注入,但目前是用內部 state
// 所以我們透過 window dispatch event 或是 假設 User 會點擊。
// 實際上InventoryTable 裡面的 useEffect 會監聽 expandedProducts state 並儲存到 sessionStorage。
// 這裡的最簡單做法是,如果 matchesSearch 且 matchesBatch則傳遞一個 'forceExpand' prop 給 Table
// 但為了維持簡單,我維持現狀。前端過濾後的結果如果符合 batch使用者通常會手動展開。
}
}
}, [searchTerm, filteredInventories, warehouse.id]);
// 計算統計資訊
const lowStockItems = useMemo(() => {
const allBatches = inventories.flatMap(g => g.batches);