import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { purchaseOrderLifecycle } from "./purchaseOrder.lifecycle.generated"; export const RECEIPT_STATUSES = ["NOT_RECEIVED", "PARTIALLY_RECEIVED", "RECEIVED"] as const; export const BILLING_STATUSES = ["NOT_BILLED", "PARTIALLY_BILLED", "BILLED"] as const; const builtins = (params: { companyType?: TailorAnyDBType; supplierAccountType?: TailorAnyDBType; siteType?: TailorAnyDBType; currencyType?: TailorAnyDBType; }) => { const companyId = db.uuid().description("Foreign key to Company"); const supplierAccountId = db.uuid().description("Foreign key to SupplierAccount"); const currencyId = db .uuid() .description("Transaction currency, which must be the company's base currency"); const receivingSiteId = db .uuid({ optional: true }) .description("Default receiving Site for the order"); return { companyId: params.companyType ? companyId.relation({ type: "n-1", toward: { type: params.companyType, as: "company" }, backward: "purchaseOrders", }) : companyId, supplierAccountId: params.supplierAccountType ? supplierAccountId.relation({ type: "n-1", toward: { type: params.supplierAccountType, as: "supplierAccount" }, backward: "purchaseOrders", }) : supplierAccountId, currencyId: params.currencyType ? currencyId.relation({ type: "n-1", toward: { type: params.currencyType, as: "currency" }, backward: "purchaseOrders", }) : currencyId, receivingSiteId: params.siteType ? receivingSiteId.relation({ type: "n-1", toward: { type: params.siteType, as: "receivingSite" }, backward: "purchaseOrders", }) : receivingSiteId, orderStatus: db .enum(purchaseOrderLifecycle.states) .description("Purchase-order lifecycle status"), receiptStatus: db .enum(RECEIPT_STATUSES, { optional: true }) .description("Receipt fulfillment status — set on approval"), billingStatus: db .enum(BILLING_STATUSES, { optional: true }) .description("Billing fulfillment status — set on approval"), orderDate: db.date().description("Purchase-order document date"), externalSupplierOrderReference: db .string({ optional: true }) .description("Optional external supplier order reference"), supplierSnapshotName: db .string({ optional: true }) .description("Snapshotted supplier display name at order time"), rejectionReason: db.string({ optional: true }).description("Latest rejection reason"), closeReason: db.string({ optional: true }).description("Close or write-off reason"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreatePurchaseOrderTypeParams> { fields?: NoReservedFields; companyType?: TailorAnyDBType; supplierAccountType?: TailorAnyDBType; siteType?: TailorAnyDBType; currencyType?: TailorAnyDBType; } export function createPurchaseOrderType>( params: CreatePurchaseOrderTypeParams, ) { return db .table("PurchaseOrder", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const purchaseOrder = createPurchaseOrderType({});