import { createContext, DomainError } from "@tailor-platform/erp-kit/app"; import { createResolver, t } from "@tailor-platform/sdk"; import { getDB } from "@/generated/kysely-tailordb"; import { arModulesExport } from "@/modules"; /** * postAccountReceivableDocument — post a REGISTERED customer invoice to the * general ledger. * * The native command builds a balanced JournalEntry from the invoice's * distribution lines and the receivable control account (an INVOICE debits the * receivable control account and credits the distribution/revenue accounts; a * CREDIT_MEMO reverses it), posts it directly in financial-accounting under * sourceDocumentType SALES_INVOICE, and moves the AR document REGISTERED -> * POSTED in the same transaction. Posting requires the document currency to * equal the company's base currency. `id` is the AccountReceivableDocument id. * This is the AR mirror of `postAccountPayableDocument`. */ export default createResolver({ name: "postAccountReceivableDocument", operation: "mutation", input: { id: t.string().description("Account receivable document ID"), }, body: async (context) => { const ctx = createContext(context); const db = getDB("main-db"); return db .transaction() .execute(async (trx) => { const result = await arModulesExport.commands.postAccountReceivableDocument( trx, { id: context.input.id }, ctx, ); if (!result.ok) { switch (result.error.code) { case "ACCOUNT_RECEIVABLE_DOCUMENT_NOT_FOUND": throw new DomainError(`Account receivable document not found: ${context.input.id}`); case "ACCOUNT_RECEIVABLE_INVALID_DOCUMENT_STATUS": throw new DomainError(`Only REGISTERED invoices can be posted: ${context.input.id}`); case "ACCOUNT_RECEIVABLE_POSTING_CURRENCY_MISMATCH": throw new DomainError( `The invoice currency must equal the company base currency to post.`, ); case "ACCOUNT_RECEIVABLE_ACCOUNTING_PERIOD_NOT_FOUND": throw new DomainError(`No accounting period covers the invoice posting date.`); case "ACCOUNT_RECEIVABLE_LINE_TOTAL_MISMATCH": throw new DomainError( `Line/distribution totals do not reconcile with the document total.`, ); case "ACCOUNT_RECEIVABLE_MINIMUM_LINES_NOT_MET": throw new DomainError( `The invoice must have at least one line with one distribution.`, ); case "ACCOUNT_RECEIVABLE_ACCOUNT_NOT_FOUND": throw new DomainError(`A referenced GL account does not exist.`); case "ACCOUNT_RECEIVABLE_ACCOUNT_INACTIVE": throw new DomainError(`A referenced GL account is not ACTIVE.`); case "ACCOUNT_RECEIVABLE_COMPANY_NOT_FOUND": throw new DomainError(`The invoice company does not exist.`); case "ACCOUNT_RECEIVABLE_JOURNAL_ENTRY_CREATE_FAILED": throw new DomainError(`Failed to create the journal entry for this invoice.`); case "ACCOUNT_RECEIVABLE_JOURNAL_ENTRY_POST_FAILED": throw new DomainError(`Failed to post the journal entry for this invoice.`); case "UNAUTHENTICATED": throw new DomainError("Authentication is required"); case "INSUFFICIENT_PERMISSION": throw new DomainError("You do not have permission to perform this action"); default: throw result.error satisfies never; } } return { id: result.value.accountReceivableDocument.id, status: result.value.accountReceivableDocument.status, }; }) .catch((err: unknown) => { if (err instanceof DomainError) throw err; throw new Error("Failed to post the account receivable document", { cause: err }); }); }, output: t .object({ id: t.string().description("Account receivable document ID"), status: t.string().description("New document status (POSTED)"), }) .description("PostAccountReceivableDocument response"), });