@tomei/finance
Version:
NestJS package for finance module
115 lines • 4.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const general_1 = require("@tomei/general");
const typeof_1 = require("../helpers/typeof");
const sequelize_1 = require("sequelize");
const __1 = require("../");
const decorators_1 = require("@nestjs/common/decorators");
const document_status_enum_1 = require("../enum/document-status.enum");
class PaymentItem extends general_1.ObjectBase {
get PaymentId() {
return this._PaymentId;
}
set PaymentId(id) {
this._PaymentId = id;
}
get ObjectType() {
return this._ObjectType;
}
get PayForObjectId() {
return this._PayForObjectId;
}
set PayForObjectId(id) {
this._PayForObjectId = id;
}
get PayForObjectType() {
return this._PayForObjectType;
}
set PayForObjectType(type) {
this._PayForObjectType = type;
}
get ObjectId() {
return '';
}
get ObjectName() {
return '';
}
get TableName() {
return 'finance_PaymentItem';
}
constructor(payment, objectBeingPaidFor) {
super();
this._PaymentId = '';
this._PayForObjectId = '';
this._PayForObjectType = '';
this.Currency = 'MYR';
this.Amount = 0;
this.Name = '';
this.Description = '';
this.Remarks = '';
this._ObjectType = 'PaymentItem';
this.PaymentId = payment.PaymentId;
this.PayForObjectId = objectBeingPaidFor.ObjectId;
this.PayForObjectType = (0, typeof_1.type)(objectBeingPaidFor);
}
async paid(dbTransaction) {
const sequelize = __1.FinanceDb.getConnection();
if (this.PayForObjectType === 'Document' ||
this.PayForObjectType === 'FinanceDocument') {
const selectQuery = `SELECT Amount FROM finance_Document WHERE DocNo = '${this.PayForObjectId}'`;
const invoice = await sequelize.query(selectQuery, {
type: sequelize_1.QueryTypes.SELECT,
raw: true,
transaction: dbTransaction,
});
let paidStatus = '';
if (invoice[0].Amount > this.Amount) {
const selectPaymentItem = `SELECT * FROM finance_PaymentItem WHERE PayForObjectId = '${this.PayForObjectId}' AND PayForObjectType = 'Document'`;
const documentPaymentItems = await sequelize.query(selectPaymentItem, {
type: sequelize_1.QueryTypes.SELECT,
raw: true,
transaction: dbTransaction,
});
let amountPreviouslyPaid = 0;
documentPaymentItems.forEach((paymentItem) => {
const amount = typeof paymentItem.Amount === 'string'
? parseFloat(paymentItem.Amount).toFixed(2)
: paymentItem.Amount;
amountPreviouslyPaid += amount;
});
if (invoice[0].Amount == amountPreviouslyPaid) {
paidStatus = document_status_enum_1.DocumentStatus.SETTLED;
}
else {
paidStatus = document_status_enum_1.DocumentStatus.PARTIALSETTLED;
}
}
else if (invoice[0].Amount == this.Amount) {
paidStatus = document_status_enum_1.DocumentStatus.SETTLED;
}
else {
throw new Error('Payment Item amount cannot be greater than its PayForObject amount.');
}
const updateQuery = `
UPDATE finance_Document
SET Status = '${paidStatus}'
WHERE DocNo = '${this.PayForObjectId}'
`;
await sequelize.query(updateQuery, {
type: sequelize_1.QueryTypes.UPDATE,
raw: true,
transaction: dbTransaction,
});
}
else if (this.PayForObjectType === 'LoanSchedule' ||
this.PayForObjectType === 'SimpleLoans' ||
this.PayForObjectType === 'Loans') {
(0, decorators_1.Next)();
}
else {
throw new Error('Unknown type');
}
}
}
exports.default = PaymentItem;
//# sourceMappingURL=payment-item.js.map