90 lines
2.4 KiB
JavaScript
90 lines
2.4 KiB
JavaScript
var path = require('path');
|
|
var database = require(path.join(__dirname, '../../database.js'));
|
|
let strftime = require('strftime');
|
|
|
|
module.exports = class LetterDebtor {
|
|
async getData(params, cb) {
|
|
let qryData = `SELECT
|
|
c.id clientId,
|
|
LOWER(ct.code) AS countryCode,
|
|
c.email AS recipient,
|
|
c.socialName AS clientName,
|
|
c.street,
|
|
c.postcode,
|
|
c.city,
|
|
c.fi,
|
|
p.name AS province,
|
|
ct.country
|
|
FROM client c
|
|
JOIN country ct ON ct.id = c.countryFk
|
|
LEFT JOIN province p ON p.id = c.provinceFk
|
|
WHERE c.id = ?`;
|
|
|
|
let qryLines = `CALL vn.clientGetDebtDiary(?, ?)`;
|
|
|
|
try {
|
|
let [data] = await database.pool.query(qryData, [params.clientId]);
|
|
|
|
if (!data)
|
|
throw new Error('No body data found');
|
|
|
|
let [lines] = await database.pool.query(qryLines, [params.clientId, params.companyId]);
|
|
|
|
Object.assign(this, data[0]);
|
|
|
|
this.lines = lines[0];
|
|
this.formatLines();
|
|
this.getBalance();
|
|
cb();
|
|
} catch (e) {
|
|
cb(e);
|
|
}
|
|
}
|
|
|
|
get currentDate() {
|
|
return strftime('%d-%m-%Y', new Date());
|
|
}
|
|
|
|
formatLines() {
|
|
this.lines.forEach(line => {
|
|
if (line.issued)
|
|
line.issued = strftime('%d-%m-%Y', line.issued);
|
|
});
|
|
}
|
|
|
|
getBalance() {
|
|
let balance = 0.00;
|
|
this.lines.forEach(line => {
|
|
if (line.debtOut)
|
|
balance += parseFloat(line.debtOut);
|
|
|
|
if (line.debtIn)
|
|
balance -= parseFloat(line.debtIn);
|
|
|
|
line.balance = parseFloat(balance.toFixed(2));
|
|
});
|
|
}
|
|
|
|
totalDebtOut() {
|
|
let debtOut = 0.00;
|
|
this.lines.forEach(line => {
|
|
debtOut += line.debtOut ? parseFloat(line.debtOut) : 0;
|
|
});
|
|
|
|
return debtOut.toFixed(2);
|
|
}
|
|
|
|
totalDebtIn() {
|
|
let debtIn = 0.00;
|
|
this.lines.forEach(line => {
|
|
debtIn += line.debtIn ? parseFloat(line.debtIn) : 0;
|
|
});
|
|
|
|
return debtIn.toFixed(2);
|
|
}
|
|
|
|
totalBalance() {
|
|
return this.totalDebtOut() - this.totalDebtIn();
|
|
}
|
|
};
|