import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField } from "@tailor-platform/sdk"; import { accountReceivableDueScheduleLine } from "./accountReceivableDueScheduleLine"; import { incomingPayment } from "./incomingPayment"; export const ACCOUNT_RECEIVABLE_SETTLEMENT_SOURCE_TYPES = ["INCOMING_PAYMENT"] as const; const builtins = { sourceType: db .enum(ACCOUNT_RECEIVABLE_SETTLEMENT_SOURCE_TYPES) .description("Settlement source discriminator"), incomingPaymentId: db .uuid() .relation({ type: "n-1", toward: { type: incomingPayment, as: "incomingPayment" }, backward: "settlements", }) .description("Source FK when sourceType is INCOMING_PAYMENT"), accountReceivableDueScheduleLineId: db .uuid() .relation({ type: "n-1", toward: { type: accountReceivableDueScheduleLine, as: "accountReceivableDueScheduleLine" }, backward: "accountReceivableSettlements", }) .description("Foreign key to the settled AccountReceivableDueScheduleLine"), settledAmount: db.decimal().description("Positive amount allocated to the due schedule line"), reversalOfSettlementId: db .uuid({ optional: true }) .relation({ type: "1-1", toward: { type: "self" }, backward: "reversals", }) .description("Optional self-reference to the original settlement being reversed"), }; type ReservedFields = keyof typeof builtins | CommonReservedFields; export interface CreateAccountReceivableSettlementTypeParams< F extends Record, > { fields?: NoReservedFields; } export function createAccountReceivableSettlementType< const F extends Record, >(params: CreateAccountReceivableSettlementTypeParams) { return db .table("AccountReceivableSettlement", { ...builtins, ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .indexes({ fields: ["sourceType", "incomingPaymentId", "accountReceivableDueScheduleLineId"], unique: false, name: "account_receivable_settlement_incoming_payment_due_schedule_idx", }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const accountReceivableSettlement = createAccountReceivableSettlementType({});