UNPKG

@tomei/finance

Version:

NestJS package for finance module

286 lines 13.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const record_not_found_error_1 = require("@tomei/general/dist/class/exceptions/record-not-found.error"); const account_system_entity_1 = require("../account-system-entity/account-system-entity"); const finance_customer_repository_1 = require("./finance-customer.repository"); const account_1 = __importDefault(require("../account/account")); const account_repository_1 = require("../account/account.repository"); const typeof_1 = require("../helpers/typeof"); const FinanceDb = __importStar(require("../database")); const ledger_transaction_repository_1 = require("../ledger-transaction/ledger-transaction.repository"); class FinanceCustomerBase extends account_system_entity_1.AccountSystemEntity { async getDetails() { return { FullName: this.FullName, IDNo: this.IDNo, IDType: this.IDType, Email: this.Email, ContactNo: this.ContactNo, }; } constructor(custSystemRefId, custSystemCode, sFinanceCompanyId) { super(); this.CustomerId = 'New'; this.CustSystemCode = ''; this.CustSystemRefId = ''; this.AccSystemRefId = 'REF'; this.PostedToAccSystemYN = 'N'; this.PostedById = ''; if (custSystemRefId && custSystemCode && sFinanceCompanyId) { this.CustSystemRefId = custSystemRefId; this.CustSystemCode = custSystemCode; this.CompanyId = sFinanceCompanyId; } } static async initCustomer(custSystemRefId, custSystemCode, sFinanceCompanyId, dbTransaction) { const financeCustomerData = await FinanceCustomerBase._FinanceCustomerRepository.findOne({ where: { CompanyId: sFinanceCompanyId, CustSystemCode: custSystemCode, CustSystemRefId: custSystemRefId, }, transaction: dbTransaction, }); if (financeCustomerData) { const self = new this(); self._DbTransaction = dbTransaction; self.CustomerId = financeCustomerData.CustomerId; self.CompanyId = sFinanceCompanyId; self.CustSystemCode = custSystemCode; self.CustSystemRefId = custSystemRefId; self.AccSystemRefId = financeCustomerData.AccSystemRefId; self.PostedToAccSystemYN = financeCustomerData.PostedToAccSystemYN; self.PostedById = financeCustomerData.PostedById; self.PostedDateTime = financeCustomerData.PostedDateTime; return self; } else { const notFoundError = new record_not_found_error_1.RecordNotFoundError('FinanceCustomerErrMsg', 'No Record Found'); throw notFoundError; } } async getAccountReceivable(dbTransaction) { try { if (this._AccountReceivable) { return this._AccountReceivable; } else { const accountData = await FinanceCustomerBase._AccountRepository.findOne({ where: { AccountNo: this.ARAccountNo, AccountType: 'Account Receivable', OwnerId: this.CustomerId, OwnerType: (0, typeof_1.type)(this), }, transaction: dbTransaction, }); this._AccountReceivable = new account_1.default(dbTransaction); if (accountData) { this._AccountReceivable.AccountNo = accountData.AccountNo; this._AccountReceivable.AccSystemRefId = accountData.AccSystemRefId; this._AccountReceivable.PostedById = accountData.PostedById; this._AccountReceivable.PostedDateTime = accountData.PostedDateTime; this._AccountReceivable.PostedToAccSystemYN = accountData.PostedToAccSystemYN; this._AccountReceivable.Name = accountData.Name; this._AccountReceivable.Description = accountData.Description; this._AccountReceivable.ParentAccountNo = accountData.ParentAccountNo; this._AccountReceivable.AccountType = accountData.AccountType; this._AccountReceivable.OwnerId = accountData.OwnerId; this._AccountReceivable.OwnerType = accountData.OwnerType; this._AccountReceivable.RelatedObjectId = accountData.RelatedObjectId; this._AccountReceivable.RelatedObjectType = accountData.RelatedObjectType; this._AccountReceivable.CreatedById = accountData.CreatedById; this._AccountReceivable.CreatedAt = accountData.CreatedAt; this._AccountReceivable.UpdatedById = accountData.UpdatedById; this._AccountReceivable.UpdatedAt = accountData.UpdatedAt; } else { this._AccountReceivable.AccountNo = this.ARAccountNo; this._AccountReceivable.ParentAccountNo = this.ParentARAccountNo; this._AccountReceivable.AccountType = 'Account Receivable'; this._AccountReceivable.OwnerId = this.CustomerId; this._AccountReceivable.OwnerType = (0, typeof_1.type)(this); } return this._AccountReceivable; } } catch (error) { throw error; } } async getAccountPayable(dbTransaction) { try { if (this._AccountPayable) { return this._AccountPayable; } else { const accountData = await FinanceCustomerBase._AccountRepository.findOne({ where: { AccountNo: this.APAccountNo, AccountType: 'Account Payable', OwnerId: this.CustomerId, OwnerType: (0, typeof_1.type)(this), }, transaction: dbTransaction, }); this._AccountPayable = new account_1.default(dbTransaction); if (accountData) { this._AccountPayable.AccountNo = accountData.AccountNo; this._AccountPayable.AccSystemRefId = accountData.AccSystemRefId; this._AccountPayable.PostedById = accountData.PostedById; this._AccountPayable.PostedDateTime = accountData.PostedDateTime; this._AccountPayable.PostedToAccSystemYN = accountData.PostedToAccSystemYN; this._AccountPayable.Name = accountData.Name; this._AccountPayable.Description = accountData.Description; this._AccountPayable.ParentAccountNo = accountData.ParentAccountNo; this._AccountPayable.AccountType = accountData.AccountType; this._AccountPayable.OwnerId = accountData.OwnerId; this._AccountPayable.OwnerType = accountData.OwnerType; this._AccountPayable.RelatedObjectId = accountData.RelatedObjectId; this._AccountPayable.RelatedObjectType = accountData.RelatedObjectType; this._AccountPayable.CreatedById = accountData.CreatedById; this._AccountPayable.CreatedAt = accountData.CreatedAt; this._AccountPayable.UpdatedById = accountData.UpdatedById; this._AccountPayable.UpdatedAt = accountData.UpdatedAt; } else { this._AccountPayable.AccountNo = this.APAccountNo; this._AccountPayable.ParentAccountNo = this.ParentAPAccountNo; this._AccountPayable.AccountType = 'Account Payable'; this._AccountPayable.OwnerId = this.CustomerId; this._AccountPayable.OwnerType = (0, typeof_1.type)(this); } return this._AccountPayable; } } catch (error) { throw error; } } init(person) { this.FullName = person.FullName; this.IDNo = person.IDNo; this.IDType = person.IDType; this.Email = person.Email; this.ContactNo = person.ContactNo; this.DefaultAddress = person.DefaultAddress; } async save(accSystemRefId, custSystemCode, custSystemRefId, dbTransaction) { this.CustSystemCode = custSystemCode; this.CustSystemRefId = custSystemRefId; this.AccSystemRefId = accSystemRefId; this.CustomerId = this.createId(); const data = await FinanceCustomerBase._FinanceCustomerRepository.create({ CompanyId: this.CompanyId, CustomerId: this.CustomerId, CustSystemCode: this.CustSystemCode, CustSystemRefId: this.CustSystemRefId, AccSystemRefId: this.AccSystemRefId, PostedToAccSystemYN: this.PostedToAccSystemYN, PostedById: this.PostedById, PostedDateTime: this.PostedDateTime, }, { transaction: dbTransaction }); return data; } async update(accSystemRefId, dbTransaction) { try { if (accSystemRefId) { this.AccSystemRefId = accSystemRefId; } const data = await FinanceCustomerBase._FinanceCustomerRepository.update({ CompanyId: this.CompanyId, CustSystemCode: this.CustSystemCode, CustSystemRefId: this.CustSystemRefId, AccSystemRefId: this.AccSystemRefId, PostedToAccSystemYN: this.PostedToAccSystemYN, PostedById: this.PostedById, PostedDateTime: this.PostedDateTime, }, { where: { CustomerId: this.CustomerId }, transaction: dbTransaction, }); return this; } catch (error) { throw error; } } static async isFinanceCustomerExist(custSystemRefId, custSystemCode, sFinanceCompanyId, dbTransaction) { const data = await FinanceCustomerBase._FinanceCustomerRepository.findAll({ where: { CustSystemRefId: custSystemRefId, CustSystemCode: custSystemCode, CompanyId: sFinanceCompanyId, }, transaction: dbTransaction, }); if (data.length > 0) { return true; } return false; } async getOutstandingBalance(dbTransaction) { if (!dbTransaction) { dbTransaction = await FinanceDb.getConnection().transaction(); } const AR = await this.getAccountReceivable(); const ledgerTransactions = await FinanceCustomerBase._LedgerTransactionRepository.findAll({ where: { AccountNo: AR.AccountNo, }, transaction: dbTransaction, }); const totalDebitAmount = ledgerTransactions.reduce((accumulator, currentValue) => accumulator + +currentValue.DebitAmount, 0); const totalCreditAmount = ledgerTransactions.reduce((accumulator, currentValue) => accumulator + +currentValue.CreditAmount, 0); if (totalCreditAmount > totalDebitAmount) { return 0; } const balance = totalDebitAmount - totalCreditAmount; return balance; } } FinanceCustomerBase._AccountRepository = new account_repository_1.AccountRepository(); FinanceCustomerBase._LedgerTransactionRepository = new ledger_transaction_repository_1.LedgerTransactionRepository(); FinanceCustomerBase._FinanceCustomerRepository = new finance_customer_repository_1.FinanceCustomerRepository(); exports.default = FinanceCustomerBase; //# sourceMappingURL=customer.js.map