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 153 154 155 156 157 158 159 160 161 | import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { LedgerAccount } from '../schemas/ledger-account.schema'; import { FinanceLegalEntity, AccountingBook } from '../schemas/legal-entity.schema'; import { FinanceFiscalYear, FinancePeriod } from '../schemas/fiscal-period.schema'; import { FinanceConfiguration } from '../schemas/finance-configuration.schema'; @Injectable() export class FinanceSeederService { constructor( @InjectModel(LedgerAccount.name) private readonly accountModel: Model<LedgerAccount>, @InjectModel(FinanceLegalEntity.name) private readonly entityModel: Model<FinanceLegalEntity>, @InjectModel(AccountingBook.name) private readonly bookModel: Model<AccountingBook>, @InjectModel(FinanceFiscalYear.name) private readonly yearModel: Model<FinanceFiscalYear>, @InjectModel(FinancePeriod.name) private readonly periodModel: Model<FinancePeriod>, @InjectModel(FinanceConfiguration.name) private readonly configModel: Model<FinanceConfiguration>, ) {} async seed(tenantId: string): Promise<any> { const tId = new Types.ObjectId(tenantId); // 1. Legal Entity let entity = await this.entityModel.findOne({ tenantId: tId }).exec(); if (!entity) { entity = await this.entityModel.create({ tenantId: tId, legalName: 'Be-Vision India Ltd', registrationNumber: 'REG-12345', country: 'India', taxId: '27AAAAA1111A1Z1', isActive: true }); } // 2. Accounting Book let book = await this.bookModel.findOne({ tenantId: tId }).exec(); if (!book) { book = await this.bookModel.create({ tenantId: tId, legalEntityId: entity._id, bookName: 'Statutory Book', bookType: 'statutory', baseCurrency: 'INR', reportingCurrency: 'USD', isActive: true }); } // 3. Fiscal Year & Open Period let year = await this.yearModel.findOne({ tenantId: tId }).exec(); if (!year) { const now = new Date(); const start = new Date(now.getFullYear(), 0, 1); const end = new Date(now.getFullYear(), 11, 31); year = await this.yearModel.create({ tenantId: tId, legalEntityId: entity._id, yearName: `FY-${now.getFullYear()}`, startDate: start, endDate: end, status: 'open' }); // Seed periods for (let m = 0; m < 12; m++) { const pStart = new Date(now.getFullYear(), m, 1); const pEnd = new Date(now.getFullYear(), m + 1, 0, 23, 59, 59); const pName = `${now.getFullYear()}-${(m + 1).toString().padStart(2, '0')}`; await this.periodModel.create({ tenantId: tId, fiscalYearId: year._id, periodName: pName, startDate: pStart, endDate: pEnd, status: 'open' }); } } // 4. Chart of Accounts (Basic control accounts & subtypes) const accountsData = [ { code: '10100', name: 'Cash Account', type: 'Asset', subtype: 'Cash', control: 'cash' }, { code: '10200', name: 'HDFC Bank Account', type: 'Asset', subtype: 'Bank', control: 'bank' }, { code: '11100', name: 'Accounts Receivable Control', type: 'Asset', subtype: 'Accounts Receivable', control: 'receivable' }, { code: '12100', name: 'Inventory Control', type: 'Asset', subtype: 'Inventory', control: 'inventory' }, { code: '21100', name: 'Accounts Payable Control', type: 'Liability', subtype: 'Accounts Payable', control: 'payable' }, { code: '22100', name: 'GST Payable', type: 'Liability', subtype: 'Tax Payable', control: 'tax' }, { code: '23100', name: 'Payroll Payable', type: 'Liability', subtype: 'Payroll Payable', control: 'payroll' }, { code: '30100', name: 'Retained Earnings', type: 'Equity', subtype: 'Retained Earnings', control: 'none' }, { code: '40100', name: 'SaaS Sales Revenue', type: 'Revenue', subtype: 'Revenue', control: 'none' }, { code: '50100', name: 'Cost of Goods Sold', type: 'Expense', subtype: 'Cost of Goods Sold', control: 'none' }, { code: '50200', name: 'Salary Operating Expense', type: 'Expense', subtype: 'Operating Expense', control: 'none' }, { code: '90100', name: 'Suspense Account', type: 'Asset', subtype: 'Suspense', control: 'none' } ]; for (const acc of accountsData) { const existing = await this.accountModel.findOne({ tenantId: tId, accountCode: acc.code }).exec(); if (!existing) { await this.accountModel.create({ tenantId: tId, legalEntityId: entity._id, accountCode: acc.code, accountName: acc.name, accountType: acc.type, accountSubtype: acc.subtype, isControlAccount: acc.control !== 'none', controlType: acc.control, currency: 'INR', isActive: true, openingBalanceMinor: 10000000, // 100,000 INR currentBalanceMinor: 10000000 }); } } // 5. Global Finance Configuration let config = await this.configModel.findOne({ tenantId: tId }).exec(); if (!config) { const arAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Accounts Receivable' }).exec(); const apAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Accounts Payable' }).exec(); const invAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Inventory' }).exec(); const cashAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Cash' }).exec(); const roundingAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Rounding' }).exec(); const suspenseAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Suspense' }).exec(); const retainedAcc = await this.accountModel.findOne({ tenantId: tId, accountSubtype: 'Retained Earnings' }).exec(); await this.configModel.create({ tenantId: tId, financeEnabled: true, baseCurrency: 'INR', reportingCurrency: 'USD', multiCurrencyEnabled: true, journalApprovalRequired: false, paymentApprovalRequired: false, budgetApprovalRequired: false, taxPostingEnabled: true, periodLockEnabled: true, budgetControlMode: 'warning', accountsReceivableControlAccountId: arAcc?._id, accountsPayableControlAccountId: apAcc?._id, inventoryControlAccountId: invAcc?._id, cashControlAccountId: cashAcc?._id, suspenseAccountId: suspenseAcc?._id, retainedEarningsAccountId: retainedAcc?._id, roundingAccountId: roundingAcc?._id, defaultAccountingBookId: book._id }); } return { legalEntityId: entity._id, statutoryBookId: book._id }; } } |