All files / src/domains/procurement/purchase-orders/schemas purchase-order.schema.ts

0% Statements 0/47
0% Branches 0/4
100% Functions 0/0
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                                                                   
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
 
@Schema({ _id: false })
export class PurchaseOrderTaxSnapshot {
  @Prop({ required: true })
  taxCode: string;
 
  @Prop({ required: true })
  rate: number; // e.g., 0.12 for 12%
 
  @Prop({ required: true })
  taxableAmount: number;
 
  @Prop({ required: true })
  taxAmount: number;
 
  @Prop({ required: true })
  jurisdiction: string;
 
  @Prop({ required: true, default: false })
  inclusive: boolean;
 
  @Prop({ required: true, default: 1 })
  calculationOrder: number;
}
 
@Schema({ _id: false })
export class PurchaseOrderLineItem {
  @Prop({ required: true })
  description: string;
 
  @Prop({ required: true })
  quantity: number;
 
  @Prop({ required: true })
  unitPrice: number;
 
  @Prop({ required: true })
  total: number;
 
  @Prop({ default: null })
  taxCode: string;
 
  @Prop({ type: [PurchaseOrderTaxSnapshot], default: [] })
  taxSnapshots: PurchaseOrderTaxSnapshot[];
}
 
@Schema({ timestamps: true })
export class PurchaseOrder extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({ required: true, index: true })
  poNumber: string;
 
  @Prop({ default: null, index: true })
  requisitionId: string;
 
  @Prop({ default: null, index: true })
  rfqId: string;
 
  @Prop({ required: true, index: true })
  vendorId: string;
 
  @Prop({ required: true })
  title: string;
 
  @Prop({ type: [PurchaseOrderLineItem], default: [] })
  items: PurchaseOrderLineItem[];
 
  @Prop({ required: true })
  subtotal: number;
 
  @Prop({ required: true })
  tax: number;
 
  @Prop({ required: true })
  totalAmount: number;
 
  @Prop({ required: true, default: 'USD' })
  currency: string;
 
  @Prop({
    required: true,
    default: 'draft',
    enum: [
      'draft',
      'approval_pending',
      'approved',
      'issued',
      'acknowledged',
      'partially_received',
      'fully_received',
      'partially_invoiced',
      'fully_invoiced',
      'amended',
      'cancelled',
      'closed',
    ],
  })
  status: string;
 
  @Prop({ default: null })
  approvedBy: string;
 
  @Prop({ default: null })
  approvalDate: Date;
 
  @Prop({ default: 'standard', enum: ['standard', 'blanket', 'release_order'] })
  poType: string;
 
  @Prop({ default: null })
  blanketPoId: string; // Ref to blanket PO if release_order
 
  @Prop({ default: null })
  paymentTerms: string; // MDM reference or text
 
  @Prop({ default: null })
  deliveryTerms: string; // e.g. EXW, FOB
 
  @Prop({ type: [PurchaseOrderTaxSnapshot], default: [] })
  documentTaxSnapshots: PurchaseOrderTaxSnapshot[];
}
 
export const PurchaseOrderSchema = SchemaFactory.createForClass(PurchaseOrder);
PurchaseOrderSchema.index({ tenantId: 1, poNumber: 1 }, { unique: true });
PurchaseOrderSchema.index({ tenantId: 1, vendorId: 1 });
PurchaseOrderSchema.index({ tenantId: 1, status: 1 });