import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField } from "@tailor-platform/sdk"; // Cause-document types that record acquisition cost events. Generic references, not typed foreign keys. // ACCOUNT_PAYABLE_DOCUMENT rows declare an invoice price variance change; // PURCHASE_ORDER_REVISION rows declare an order price change; // INVENTORY_LEDGER rows anchor a receipt-triggered redistribution. export const ACQUISITION_COST_ADJUSTMENT_SOURCE_TYPES = [ "ACCOUNT_PAYABLE_DOCUMENT", "PURCHASE_ORDER_REVISION", "INVENTORY_LEDGER", ] as const; // Which clearing account a declared amount washes against. // INVOICE_PRICE: the billed price differed from the order price — washes the // invoice price variance account. // ORDER_PRICE: the order price changed for quantity the accrual still carries // — restates the accrual. export const ACQUISITION_COST_VARIANCE_KINDS = ["INVOICE_PRICE", "ORDER_PRICE"] as const; // A register row either declares a variance or, as REDISTRIBUTION, anchors the // zero-amount redistribution a receipt posting runs. export const ACQUISITION_COST_ADJUSTMENT_KINDS = [ ...ACQUISITION_COST_VARIANCE_KINDS, "REDISTRIBUTION", ] as const; const builtins = () => { return { sourceType: db .enum(ACQUISITION_COST_ADJUSTMENT_SOURCE_TYPES) .description("Cause-document type"), sourceId: db .uuid() .description("Cause-document ID. Generic reference, not a typed foreign key."), sourceLineId: db .uuid({ optional: true }) .description("Cause-document line ID. Generic reference, not a typed foreign key."), purchaseOrderId: db .uuid() .description( "Purchase order whose receipt layers were adjusted. Generic reference, not a typed foreign key.", ), purchaseOrderLineId: db .uuid() .description( "Purchase order line whose receipt layers were adjusted. Generic reference, not a typed foreign key.", ), amount: db .decimal() .description( "Signed amount this row adds to the order line's variance; the sum over the line is the amount to distribute. Zero on receipt-triggered rows.", ), kind: db.enum(ACQUISITION_COST_ADJUSTMENT_KINDS).description("Register row kind"), effectiveDate: db.date().description("Business effective date of the adjustment posting"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateAcquisitionCostAdjustmentTypeParams< F extends Record, > { fields?: NoReservedFields; } export function createAcquisitionCostAdjustmentType< const F extends Record, >(params: CreateAcquisitionCostAdjustmentTypeParams) { return db .table("AcquisitionCostAdjustment", { ...builtins(), ...((params.fields ?? {}) as F), createdAt: db.fields.timestamps().createdAt, }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const acquisitionCostAdjustment = createAcquisitionCostAdjustmentType({});