|
- "use client";
-
- import { NEXT_PUBLIC_API_URL } from "@/config/api";
- import { clientAuthFetch } from "@/app/utils/clientAuthFetch";
- import { exportChartToXlsx } from "@/app/(main)/chart/_components/exportChartToXlsx";
-
- export interface GrnReportRow {
- poCode?: string;
- deliveryNoteNo?: string;
- receiptDate?: string;
- itemCode?: string;
- itemName?: string;
- acceptedQty?: number;
- receivedQty?: number;
- demandQty?: number;
- uom?: string;
- purchaseUomDesc?: string;
- stockUomDesc?: string;
- productLotNo?: string;
- expiryDate?: string;
- supplierCode?: string;
- supplier?: string;
- status?: string;
- grnId?: number | string;
- [key: string]: unknown;
- }
-
- export interface GrnReportResponse {
- rows: GrnReportRow[];
- }
-
- /**
- * Fetch GRN (Goods Received Note) report data by date range.
- * Backend: GET /report/grn-report?receiptDateStart=&receiptDateEnd=&itemCode=
- */
- export async function fetchGrnReportData(
- criteria: Record<string, string>
- ): Promise<GrnReportRow[]> {
- const queryParams = new URLSearchParams(criteria).toString();
- const url = `${NEXT_PUBLIC_API_URL}/report/grn-report?${queryParams}`;
-
- const response = await clientAuthFetch(url, {
- method: "GET",
- headers: { Accept: "application/json" },
- });
-
- if (response.status === 401 || response.status === 403)
- throw new Error("Unauthorized");
- if (!response.ok)
- throw new Error(`HTTP error! status: ${response.status}`);
-
- const data = (await response.json()) as GrnReportResponse | GrnReportRow[];
- const rows = Array.isArray(data) ? data : (data as GrnReportResponse).rows ?? [];
- return rows;
- }
-
- /** Excel column headers (bilingual) for GRN report */
- function toExcelRow(r: GrnReportRow): Record<string, string | number | undefined> {
- return {
- "PO No. / 訂單編號": r.poCode ?? "",
- "Supplier Code / 供應商編號": r.supplierCode ?? "",
- "Delivery Note No. / 送貨單編號": r.deliveryNoteNo ?? "",
- "Receipt Date / 收貨日期": r.receiptDate ?? "",
- "Item Code / 物料編號": r.itemCode ?? "",
- "Item Name / 物料名稱": r.itemName ?? "",
- "Qty / 數量": r.acceptedQty ?? r.receivedQty ?? "",
- "Demand Qty / 訂單數量": r.demandQty ?? "",
- "UOM / 單位": r.uom ?? r.purchaseUomDesc ?? r.stockUomDesc ?? "",
- "Product Lot No. / 批次": r.productLotNo ?? "",
- "Expiry Date / 到期日": r.expiryDate ?? "",
- "Supplier / 供應商": r.supplier ?? "",
- "Status / 狀態": r.status ?? "",
- "GRN Id / M18 單號": r.grnId ?? "",
- };
- }
-
- /**
- * Generate and download GRN report as Excel.
- */
- export async function generateGrnReportExcel(
- criteria: Record<string, string>,
- reportTitle: string = "PO 入倉記錄"
- ): Promise<void> {
- const rows = await fetchGrnReportData(criteria);
- const excelRows = rows.map(toExcelRow);
- const start = criteria.receiptDateStart;
- const end = criteria.receiptDateEnd;
- let datePart: string;
- if (start && end && start === end) {
- datePart = start;
- } else if (start || end) {
- datePart = `${start || ""}_to_${end || ""}`;
- } else {
- datePart = new Date().toISOString().slice(0, 10);
- }
- const safeDatePart = datePart.replace(/[^\d\-_/]/g, "");
- const filename = `${reportTitle}_${safeDatePart}`;
- exportChartToXlsx(excelRows, filename, "GRN");
- }
|