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 | import { Injectable, BadRequestException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Asset, AssetComponent } from '../schemas'; import { EventBusService } from '../../../platform/events/event-bus.service'; @Injectable() export class AssetCapitalizationService { constructor( @InjectModel(Asset.name) private readonly assetModel: Model<Asset>, @InjectModel(AssetComponent.name) private readonly componentModel: Model<AssetComponent>, private readonly eventBus: EventBusService ) {} async capitalizeAsset( tenantId: string, assetId: string, costs: { freightMinor?: number; installationMinor?: number; sitePrepMinor?: number; professionalFeesMinor?: number }, userId: string ): Promise<Asset> { const asset = await this.assetModel.findOne({ _id: assetId, tenantId }).exec(); if (!asset) throw new BadRequestException('Asset not found'); if (asset.status !== 'capitalization_pending') { throw new BadRequestException(`Asset status is ${asset.status}. Only pending assets can be capitalized.`); } const freight = costs.freightMinor || 0; const installation = costs.installationMinor || 0; const sitePrep = costs.sitePrepMinor || 0; const professionalFees = costs.professionalFeesMinor || 0; // Attributable capitalization cost composition (excluding recoverable taxes) const capitalizationCost = asset.purchaseCostMinor + freight + installation + sitePrep + professionalFees; asset.capitalizationCostMinor = capitalizationCost; asset.currentBookValueMinor = capitalizationCost; asset.capitalizationDate = new Date(); asset.status = 'capitalization_pending'; // Remains pending until finance posting confirmation handshake await asset.save(); // Trigger Finance Integration Posting Handshake await this.eventBus.publish('asset.capitalization-ready.v1', { assetId: asset._id.toString(), assetNumber: asset.assetNumber, tenantId, capitalizationCostMinor: capitalizationCost, purchaseCostMinor: asset.purchaseCostMinor, freightMinor: freight, installationMinor: installation, currency: asset.currency, userId }, tenantId); return asset; } async handleFinanceCapitalizationPosted(tenantId: string, assetId: string): Promise<Asset> { const asset = await this.assetModel.findOne({ _id: assetId, tenantId }).exec(); if (!asset) throw new Error('Asset not found'); asset.status = 'capitalized'; return asset.save(); } // Component Accounting async registerComponent(tenantId: string, parentAssetId: string, childAssetId: string, allocatedCostMinor: number): Promise<AssetComponent> { if (parentAssetId === childAssetId) throw new BadRequestException('Circular hierarchy detected: parent cannot equal child.'); // Prevent deep circular relationships: check if parent is child of the child const reverseLink = await this.componentModel.findOne({ tenantId, parentAssetId: childAssetId, childAssetId: parentAssetId }).exec(); if (reverseLink) { throw new BadRequestException('Circular relationship detected in component hierarchy.'); } const comp = new this.componentModel({ tenantId, parentAssetId: new Types.ObjectId(parentAssetId), childAssetId: new Types.ObjectId(childAssetId), allocatedCostMinor }); return comp.save(); } } |