UNPKG

@tomei/finance

Version:

NestJS package for finance module

220 lines 8.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const enum_1 = require("../enum"); const account_system_entity_1 = require("../account-system-entity/account-system-entity"); const ledger_transaction_repository_1 = require("../ledger-transaction/ledger-transaction.repository"); const ledger_transaction_1 = __importDefault(require("../ledger-transaction/ledger-transaction")); const journal_entry_repository_1 = require("./journal-entry.repository"); const record_not_found_error_1 = require("@tomei/general/dist/class/exceptions/record-not-found.error"); class JournalEntry extends account_system_entity_1.AccountSystemEntity { get JournalEntryId() { return this._JournalEntryId; } set JournalEntryId(id) { this._JournalEntryId = id; } get ObjectType() { return this._ObjectType; } get RepositoryBase() { return JournalEntry._RepositoryBase; } get TableName() { return 'finance_JournalEntry'; } get ObjectName() { return this.Name; } get ObjectId() { return this._JournalEntryId; } constructor(dbTransaction, journalEntryId) { super(); this._JournalEntryId = 'New'; this.Date = new Date(); this.Name = ''; this.Description = ''; this._DebitTransactions = []; this._CreditTransactions = []; if (dbTransaction) { this._DbTransaction = dbTransaction; } if (!journalEntryId) { this.init({ JournalEntryId: 'New', CompanyId: '', Date: new Date(), Name: '', Description: '', AccSystemRefId: '', PostedToAccSystemYN: 'N', PostedById: '', PostedDateTime: null, }); } } static async initJournalEntry(dbTransaction, journalEntryId) { try { if (journalEntryId) { const journalEntryData = await JournalEntry._RepositoryBase.findOne({ where: { JournalEntryId: journalEntryId, }, transaction: dbTransaction, }); if (journalEntryData) { const journalEntry = new JournalEntry(); journalEntry.init({ JournalEntryId: journalEntryData.JournalEntryId, CompanyId: journalEntryData.CompanyId, Date: new Date(), Name: journalEntryData.Name, Description: journalEntryData.Description, AccSystemRefId: journalEntryData.AccSystemRefId, PostedToAccSystemYN: journalEntryData.PostedToAccSystemYN, PostedById: journalEntryData.PostedById, PostedDateTime: journalEntryData.PostedDateTime, }); return journalEntry; } else { const notFoundError = new record_not_found_error_1.RecordNotFoundError('JournalEntryErrMsg', 'No Record Found.'); throw notFoundError; } } else { const journalEntry = new JournalEntry(dbTransaction); return journalEntry; } } catch (error) { throw error; } } get DebitTransactions() { return new Promise((resolve, reject) => { if (this.JournalEntryId !== 'New') { JournalEntry._LedgerTransactionRepository .findAll({ where: { TransactionType: enum_1.TransactionTypeOptions.DEBIT, }, transaction: this._DbTransaction, }) .then((debitTransactions) => { const debitTransactionObjects = debitTransactions.map((debitTransactionData) => new ledger_transaction_1.default(this._DbTransaction, debitTransactionData.LedgerNo)); return Promise.all(debitTransactionObjects); }) .then((debitTransactionObjects) => { this._DebitTransactions = debitTransactionObjects; resolve(this._DebitTransactions); }) .catch((err) => { reject(err); }); } else { resolve(this._DebitTransactions); } }); } get CreditTransactions() { return new Promise((resolve, reject) => { if (this.JournalEntryId !== 'New') { JournalEntry._LedgerTransactionRepository .findAll({ where: { TransactionType: enum_1.TransactionTypeOptions.CREDIT, }, transaction: this._DbTransaction, }) .then((creditTransaction) => { const creditTransactionObjects = creditTransaction.map((creditTransactionData) => new ledger_transaction_1.default(this._DbTransaction, creditTransactionData.LedgerNo)); return Promise.all(creditTransactionObjects); }) .then((creditTransactionObjects) => { this._CreditTransactions = creditTransactionObjects; resolve(this._CreditTransactions); }) .catch((err) => { reject(err); }); } else { resolve(this._CreditTransactions); } }); } init(params) { if (params.JournalEntryId) this.JournalEntryId = params.JournalEntryId; if (params.CompanyId) this.CompanyId = params.CompanyId; if (params.Date) this.Date = params.Date; if (params.Name) this.Name = params.Name; if (params.Description) this.Description = params.Description; if (params.PostedById) this.PostedById = params.PostedById; if (params.PostedToAccSystemYN) this.PostedToAccSystemYN = params.PostedToAccSystemYN; if (params.PostedDateTime) this.PostedDateTime = params.PostedDateTime; } getData() { return { JournalEntryId: this.JournalEntryId, Date: this.Date, Name: this.Name, Description: this.Description, PostedById: this.PostedById, PostedToAccSystemYN: this.PostedToAccSystemYN, PostedDateTime: this.PostedDateTime, }; } async create(dbTransaction) { return await this.RepositoryBase.create(this.getData, { transaction: dbTransaction, }); } async save(userId, dbTransaction) { try { const count = await this.RepositoryBase.count(dbTransaction); this.JournalEntryId = `${count + 1}`; const data = await this.RepositoryBase.create({ JournalEntryId: this.JournalEntryId, CompanyId: this.CompanyId, Date: this.Date, Name: this.Name, Description: this.Description, PostedById: this.PostedToAccSystemYN == 'Y' ? userId : null, PostedToAccSystemYN: this.PostedToAccSystemYN, PostedDateTime: this.PostedToAccSystemYN == 'Y' ? this.PostedDateTime : null, }, { transaction: dbTransaction }); return data; } catch (error) { throw error; } } async newLedgerTransaction(transactionType) { const ledgerTransaction = new ledger_transaction_1.default(transactionType, this._DbTransaction); ledgerTransaction.JournalEntryId = this.JournalEntryId; if (transactionType === enum_1.TransactionTypeOptions.DEBIT) { this._DebitTransactions.push(ledgerTransaction); } else if (transactionType === enum_1.TransactionTypeOptions.CREDIT) { this._CreditTransactions.push(ledgerTransaction); } return ledgerTransaction; } } JournalEntry._RepositoryBase = new journal_entry_repository_1.JournalEntryRepository(); JournalEntry._LedgerTransactionRepository = new ledger_transaction_repository_1.LedgerTransactionRepository(); exports.default = JournalEntry; //# sourceMappingURL=journal-entry.js.map