All files / src/domains/inventory/stock-ledger/schemas ledger.schema.ts

0% Statements 0/40
0% Branches 0/4
100% Functions 0/0
0% Lines 0/36

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                                                                                                                                                                                                     
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
 
@Schema({ timestamps: true })
export class StockLedgerEntry extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({ required: true, index: true })
  itemCode: string;
 
  @Prop({ required: true, index: true })
  warehouseId: string;
 
  @Prop({ default: null })
  locationId: string;
 
  @Prop({ required: true, index: true })
  transactionType: string;
 
  @Prop({ required: true })
  quantityChange: number; // positive = increase, negative = decrease
 
  @Prop({ required: true, default: 0 })
  unitCost: number;
 
  @Prop({ required: true, default: 0 })
  totalCost: number;
 
  @Prop({ required: true, default: 'USD' })
  currency: string;
 
  @Prop({ default: null })
  batchNumber: string;
 
  @Prop({ default: null })
  serialNumber: string;
 
  @Prop({ required: true, index: true })
  sourceEntityId: string;
 
  @Prop({ required: true })
  sourceEntityType: string;
 
  @Prop({ required: true, unique: true })
  correlationId: string; // for idempotency/reversals matching
 
  @Prop({ default: null })
  postedBy: string;
 
  @Prop({ required: true, default: Date.now })
  postingDate: Date;
}
 
export const StockLedgerEntrySchema = SchemaFactory.createForClass(StockLedgerEntry);
StockLedgerEntrySchema.index({ tenantId: 1, itemCode: 1, warehouseId: 1 });
 
@Schema({ timestamps: true })
export class StockBalance extends Document {
  @Prop({ required: true, index: true })
  tenantId: string;
 
  @Prop({ required: true, index: true })
  itemCode: string;
 
  @Prop({ required: true, index: true })
  warehouseId: string;
 
  @Prop({ default: null })
  locationId: string;
 
  @Prop({ required: true, default: 0 })
  quantityOnHand: number;
 
  @Prop({ required: true, default: 0 })
  quantityReserved: number;
 
  @Prop({ required: true, default: 0 })
  quantityAvailable: number;
 
  @Prop({ required: true, default: 0 })
  movingAverageUnitCost: number;
 
  @Prop({ required: true, default: 0 })
  totalValue: number;
 
  @Prop({ default: null })
  batchNumber: string;
 
  @Prop({ default: null })
  serialNumber: string;
 
  @Prop({ required: true, default: 0 })
  version: number; // for optimistic locking concurrency control
}
 
export const StockBalanceSchema = SchemaFactory.createForClass(StockBalance);
StockBalanceSchema.index({ tenantId: 1, itemCode: 1, warehouseId: 1, locationId: 1, batchNumber: 1, serialNumber: 1 }, { unique: true });