import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { accountReceivableDocumentLifecycle } from "./accountReceivableDocument.lifecycle.generated"; export const ACCOUNT_RECEIVABLE_DOCUMENT_TYPES = ["INVOICE", "CREDIT_MEMO"] as const; const builtins = (params: { companyType?: TailorAnyDBType; customerAccountType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; }) => { const companyId = db.uuid().description("Foreign key to Company"); const customerAccountId = db.uuid().description("Foreign key to CustomerAccount"); const currencyId = db.uuid().description("Foreign key to transaction Currency"); const receivableControlAccountId = db.uuid().description("Foreign key to AR control Account"); return { companyId: params.companyType ? companyId.relation({ type: "n-1", toward: { type: params.companyType, as: "company" }, backward: "accountReceivableDocuments", }) : companyId, customerAccountId: params.customerAccountType ? customerAccountId.relation({ type: "n-1", toward: { type: params.customerAccountType, as: "customerAccount" }, backward: "accountReceivableDocuments", }) : customerAccountId, currencyId: params.currencyType ? currencyId.relation({ type: "n-1", toward: { type: params.currencyType, as: "currency" }, backward: "accountReceivableDocuments", }) : currencyId, receivableControlAccountId: params.accountType ? receivableControlAccountId.relation({ type: "n-1", toward: { type: params.accountType, as: "receivableControlAccount" }, backward: "accountReceivableDocuments", }) : receivableControlAccountId, documentType: db.enum(ACCOUNT_RECEIVABLE_DOCUMENT_TYPES).description("AR document type"), correctionOfId: db .uuid({ optional: true }) .relation({ type: "n-1", toward: { type: "self" }, backward: "correctionDocuments", }) .description("Optional self-reference to the posted AR document being corrected"), documentNumber: db .string() .unique() .serial({ start: 1, maxValue: 99999999, format: "AR-%08d" }) .description("System-generated AR invoice number"), externalDocumentNumber: db .string({ optional: true }) .description("Optional external customer document number"), documentDate: db.date().description("Customer invoice date"), postingDate: db.date().description("Accounting posting date"), totalAmount: db.decimal().description("Invoice gross amount in transaction currency"), description: db.string({ optional: true }).description("Optional invoice description"), status: db .enum(accountReceivableDocumentLifecycle.states) .description("AR invoice lifecycle status"), registeredAt: db.datetime({ optional: true }).description("Timestamp when registered"), postedAt: db.datetime({ optional: true }).description("Timestamp when posted"), cancelledAt: db.datetime({ optional: true }).description("Timestamp when cancelled"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateAccountReceivableDocumentTypeParams< F extends Record, > { fields?: NoReservedFields; companyType?: TailorAnyDBType; customerAccountType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; } export function createAccountReceivableDocumentType< const F extends Record, >(params: CreateAccountReceivableDocumentTypeParams) { return db .table("AccountReceivableDocument", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const accountReceivableDocument = createAccountReceivableDocumentType({});