UNPKG

@tomei/finance

Version:

NestJS package for finance module

255 lines 10.5 kB
"use strict"; 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 account_repository_1 = require("./account.repository"); class Account extends account_system_entity_1.AccountSystemEntity { get IsNewRecord() { return this._IsNewRecord; } set IsNewRecord(record) { this._IsNewRecord = record; } get ObjectType() { return this._ObjectType; } get RepositoryBase() { return Account._RepositoryBase; } get ObjectId() { return this.AccountNo; } get ObjectName() { return this.Name; } get TableName() { return 'finance_Account'; } constructor(dbTransaction, accountNo) { super(); this.AccountNo = 'New'; this.ParentAccountNo = ''; this.Name = ''; this.Description = ''; this.AccountType = ''; this.AccountSubtype = ''; this.OwnerId = ''; this.OwnerType = ''; this.RelatedObjectId = ''; this.RelatedObjectType = ''; this.AccSystemRefId = 'REF'; this.PostedToAccSystemYN = 'N'; this.PostedById = ''; this.CreatedById = ''; this.UpdatedById = ''; this._ObjectType = 'Account'; this._IsNewRecord = true; if (dbTransaction) { this._DbTransaction = dbTransaction; } if (accountNo) { this.RepositoryBase.findOne({ where: { AccountNo: accountNo, }, transaction: dbTransaction, }) .then((accountData) => { if (accountData) { this.IsNewRecord = false; this.CompanyId = accountData.CompanyId; this.ParentAccountNo = accountData.ParentAccountNo; this.Name = accountData.Name; this.Description = accountData.Description; this.AccountType = accountData.AccountType; this.AccountSubtype = accountData.AccountSubType; this.OwnerId = accountData.OwnerId; this.OwnerType = accountData.OwnerType; this.RelatedObjectId = accountData.RelatedObjectId; this.RelatedObjectType = accountData.RelatedObjectType; this.CreatedById = accountData.CreatedById; this.CreatedAt = accountData.CreatedAt; this.UpdatedById = accountData.UpdatedById; this.UpdatedAt = accountData.UpdatedAt; this.AccSystemRefId = accountData.AccSystemRefId; this.PostedToAccSystemYN = accountData.PostedToAccSystemYN; this.PostedById = accountData.PostedById; this.PostedDateTime = accountData.PostedDateTime; } else { const notFoundError = new record_not_found_error_1.RecordNotFoundError('AccountErrMsg', 'No Record Found.'); throw notFoundError; } }) .catch((err) => { console.log('Account Class constructor err: ', err); }); } } init(accountData) { this.IsNewRecord = false; this.AccountNo = accountData.AccountNo; this.CompanyId = accountData.CompanyId; this.ParentAccountNo = accountData.ParentAccountNo; this.Name = accountData.Name; this.Description = accountData.Description; this.AccountType = accountData.AccountType; this.AccountSubtype = accountData.AccountSubType; this.OwnerId = accountData.OwnerId; this.OwnerType = accountData.OwnerType; this.RelatedObjectId = accountData.RelatedObjectId; this.RelatedObjectType = accountData.RelatedObjectType; this.CreatedById = accountData.CreatedById; this.CreatedAt = accountData.CreatedAt; this.UpdatedById = accountData.UpdatedById; this.UpdatedAt = accountData.UpdatedAt; this.AccSystemRefId = accountData.AccSystemRefId; this.PostedToAccSystemYN = accountData.PostedToAccSystemYN; this.PostedById = accountData.PostedById; this.PostedDateTime = accountData.PostedDateTime; } static async initAccount(dbTransaction, accountNo) { try { const accountData = await Account._RepositoryBase.findOne({ where: { AccountNo: accountNo, }, transaction: dbTransaction, }); if (accountData) { const account = new Account(dbTransaction); account.init(accountData.get({ plain: true })); return account; } else { const notFoundError = new record_not_found_error_1.RecordNotFoundError('AccountErrMsg', 'No Record Found.'); throw notFoundError; } } catch (error) { console.log('Account Class constructor err: ', error); throw error; } } static initialize() { const dbTransaction = null; let defaultAccountReceivable; let defaultAccountPayable; try { defaultAccountReceivable = new Account(dbTransaction, 'EZC-AR'); } catch (err) { defaultAccountReceivable = new Account(dbTransaction); defaultAccountReceivable.AccountNo = null; defaultAccountReceivable.AccountType = 'Account Receivable'; defaultAccountReceivable.OwnerId = 'System'; defaultAccountReceivable.OwnerType = ''; } try { defaultAccountPayable = new Account(dbTransaction, 'EZC-AP'); } catch (err) { defaultAccountPayable = new Account(dbTransaction); defaultAccountPayable.AccountNo = null; defaultAccountPayable.AccountType = 'Account Payable'; defaultAccountPayable.OwnerId = 'System'; } } validateAccountValue() { if (!this.AccSystemRefId) { throw new Error('AccSystemAccountId is required.'); } if (!this.AccountType) { throw new Error('AccountType is required.'); } } isParentAccountExists() { if (this.ParentAccountNo) return true; return false; } async save(CompanyId, AccSystemRefId, userId, dbTransaction) { this.AccSystemRefId = AccSystemRefId; let data = await this.RepositoryBase.findOne({ where: { AccountNo: this.AccountNo, }, transaction: dbTransaction, }); if (data) { console.log('Account Save: THERE IS DATA FOUND'); const oldAccountNo = data.AccountNo; this.AccountNo = this.AccountNo || data.AccountNo; this.AccSystemRefId = this.AccSystemRefId || data.AccSystemRefId; this.Name = this.Name || data.Name; this.Description = this.Description || data.Description; this.AccountType = this.AccountType || data.AccountType; this.AccountSubtype = this.AccountSubtype || data.AccountSubType; this.CompanyId = this.CompanyId || data.CompanyId; this.OwnerId = this.OwnerId || data.OwnerId; this.OwnerType = this.OwnerType || data.OwnerType; this.RelatedObjectId = this.RelatedObjectId || data.RelatedObjectId; this.RelatedObjectType = this.RelatedObjectType || data.RelatedObjectType; this.PostedToAccSystemYN = this.PostedToAccSystemYN || data.PostedToAccSystemYN; this.ParentAccountNo = this.ParentAccountNo || data.ParentAccountNo; this.CreatedAt = data.CreatedAt; this.CreatedById = data.CreatedById; this.UpdatedAt = new Date(); this.UpdatedById = userId; await this.RepositoryBase.update({ AccountNo: this.AccountNo, AccSystemRefId: this.AccSystemRefId, Name: this.Name, Description: this.Description, AccountType: this.AccountType, AccountSubtype: this.AccountSubtype, CompanyId: this.CompanyId, OwnerId: this.OwnerId, OwnerType: this.OwnerType, RelatedObjectId: this.RelatedObjectId, RelatedObjectType: this.RelatedObjectType, PostedToAccSystemYN: this.PostedToAccSystemYN, ParentAccountNo: this.ParentAccountNo, UpdatedAt: this.UpdatedAt, UpdatedById: this.UpdatedById, PostedById: this.PostedById, PostedDateTime: this.PostedDateTime, }, { where: { AccountNo: oldAccountNo, }, transaction: dbTransaction, }); } else { console.log('Account Save: THERE IS NO DATA FOUND'); data = await this.RepositoryBase.create({ AccountNo: this.AccountNo, AccSystemRefId: this.AccSystemRefId, Name: this.Name, Description: this.Description, AccountType: this.AccountType, AccountSubtype: this.AccountSubtype, CompanyId: CompanyId, OwnerId: this.OwnerId, OwnerType: this.OwnerType, RelatedObjectId: this.RelatedObjectId, RelatedObjectType: this.RelatedObjectType, PostedToAccSystemYN: this.PostedToAccSystemYN, ParentAccountNo: this.ParentAccountNo, CreatedAt: new Date(), UpdatedAt: new Date(), CreatedById: userId, }, { transaction: dbTransaction, }); console.log(data, '<><><><><><><> DATA THAT GETS CREATED <><><><><><><>'); } console.log(data, 'Account Save: Account details returned'); return this; } } Account._RepositoryBase = new account_repository_1.AccountRepository(); exports.default = Account; //# sourceMappingURL=account.js.map