import { api as apiClient } from "@/auth/http"; import { URL_CONSTANTS } from "@/constants/URLS"; import type { Invoice, InvoiceCollectedSummary, InvoiceListFilter, PaginatedInvoices, PaginatedOfflineUsdInvoices, } from "@/types/invoice"; const cleanParams = (params: object) => Object.fromEntries( Object.entries(params).filter( ([, value]) => value !== undefined && value !== "" && value !== null, ), ); export const invoicesService = { list(filter: InvoiceListFilter): Promise { return apiClient .get(URL_CONSTANTS.BILLING.INVOICES, { params: cleanParams(filter), }) .then((r) => r.data); }, /** Total collected (paidAmount) across every filtered invoice, by currency. */ collectedSummary( filter: Omit, ): Promise { return apiClient .get(URL_CONSTANTS.BILLING.INVOICES_SUMMARY, { params: cleanParams(filter), }) .then((r) => r.data); }, getById(id: string): Promise { return apiClient .get(URL_CONSTANTS.BILLING.INVOICE_BY_ID(id)) .then((r) => r.data); }, /** `format` omitted or "a4" → standard A4 PDF; "thermal" → 80mm thermal layout (ADD-P001). */ downloadDocument(id: string, format?: "a4" | "thermal") { return apiClient.get(URL_CONSTANTS.BILLING.INVOICE_DOCUMENT(id), { responseType: "blob", params: format ? { format } : undefined, }); }, /** Issue a credit/debit memo against a registered invoice (MoR DEB/CRE) — filing-equivalent. */ issueMemo( id: string, input: { type: "CRE" | "DEB"; reason: string }, ): Promise { return apiClient .post(URL_CONSTANTS.BILLING.INVOICE_MEMO(id), input) .then((r) => r.data); }, /** Finance worklist: USD and ETB invoices awaiting manual payment confirmation. */ listOfflineUsd( filter: InvoiceListFilter, ): Promise { return apiClient .get(URL_CONSTANTS.BILLING.OFFLINE_USD, { params: cleanParams(filter), }) .then((r) => r.data); }, /** Confirm an invoice (USD or ETB) paid manually — the slip file is required. */ confirmOffline(id: string, file: File, reference?: string): Promise { const body = new FormData(); body.append("file", file); if (reference) body.append("reference", reference); return apiClient .post(URL_CONSTANTS.BILLING.CONFIRM_OFFLINE(id), body) .then((r) => r.data); }, };