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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { PurchaseOrder } from '../schemas/purchase-order.schema'; import { EventBusService } from '../../../../platform/events/event-bus.service'; import { AuditLogService } from '../../../../platform/audit/audit-log.service'; import { TaxMasterService } from '../../../mdm/application/services/tax-master.service'; @Injectable() export class PurchaseOrderService { constructor( @InjectModel(PurchaseOrder.name) private readonly poModel: Model<PurchaseOrder>, private readonly taxMasterService: TaxMasterService, private readonly eventBus: EventBusService, private readonly auditLog: AuditLogService, ) {} async createPurchaseOrder(tenantId: string, data: any, userId: string): Promise<PurchaseOrder> { const count = await this.poModel.countDocuments({ tenantId }).exec(); const poNumber = `PO-${new Date().getFullYear()}-${(count + 1).toString().padStart(5, '0')}`; let subtotal = 0; let totalTax = 0; const itemsWithTax = await Promise.all((data.items || []).map(async (item: any) => { const lineSubtotal = item.quantity * item.unitPrice; subtotal += lineSubtotal; // Overhaul tax calculations using MDM Tax resolver const resolved = await this.taxMasterService.resolve({ countryCode: data.countryCode || 'US', regionCode: data.regionCode || 'CA', taxCategoryCode: item.taxCode || 'GST', transactionDate: new Date(), tenantId, }); // Map resolved rates to snapshots const snapshots = resolved.map(r => { const rateVal = parseFloat(r.rate) || 0; const taxVal = lineSubtotal * rateVal; return { taxCode: r.taxCode, rate: rateVal, taxableAmount: lineSubtotal, taxAmount: taxVal, jurisdiction: r.jurisdictionLevel, inclusive: false, calculationOrder: 1, }; }); const lineTax = snapshots.reduce((sum, s) => sum + s.taxAmount, 0); totalTax += lineTax; return { ...item, total: lineSubtotal, taxSnapshots: snapshots, }; })); const totalAmount = subtotal + totalTax; const po = await this.poModel.create({ ...data, items: itemsWithTax, tenantId, poNumber, subtotal, tax: totalTax, totalAmount, status: 'submitted', }); await this.eventBus.publish('procurement.purchase-order.created.v1', { poId: (po as any)._id.toString(), tenantId, totalAmount, }, tenantId); await this.auditLog.log({ tenantId, userId, action: 'CREATE', resource: 'PurchaseOrder', resourceId: (po as any)._id.toString(), newValues: { poNumber, totalAmount }, }); return po; } async getPurchaseOrders(tenantId: string): Promise<PurchaseOrder[]> { return this.poModel.find({ tenantId }).exec(); } async getPurchaseOrderById(tenantId: string, id: string): Promise<PurchaseOrder> { const po = await this.poModel.findOne({ _id: id, tenantId }).exec(); if (!po) throw new NotFoundException('Purchase Order not found'); return po; } async approvePurchaseOrder(tenantId: string, id: string, userId: string): Promise<PurchaseOrder> { const po = await this.poModel.findOneAndUpdate( { _id: id, tenantId, status: 'submitted' }, { status: 'approved', approvedBy: userId, approvalDate: new Date() }, { new: true } ).exec(); if (!po) throw new NotFoundException('Purchase Order not found or not in submitted status'); await this.eventBus.publish('procurement.purchase-order.approved.v1', { poId: id, tenantId, }, tenantId); await this.eventBus.publish('procurement.purchase-order.issued.v1', { eventId: `evt-po-iss-${po._id.toString()}`, occurredAt: new Date(), idempotencyKey: `idemp-po-iss-${po._id.toString()}`, tenantId, purchaseOrderId: po._id.toString(), poNumber: po.poNumber, vendorId: po.vendorId, totalAmount: po.totalAmount, }, tenantId); await this.auditLog.log({ tenantId, userId, action: 'APPROVE', resource: 'PurchaseOrder', resourceId: id, newValues: { poNumber: po.poNumber }, }); return po; } async recordReceipt(tenantId: string, id: string, status: string): Promise<PurchaseOrder> { const po = await this.poModel.findOneAndUpdate( { _id: id, tenantId }, { status }, { new: true } ).exec(); if (!po) throw new NotFoundException('Purchase Order not found'); return po; } } |