salix/services/print/application/template/letter-debtor/letter-debtor.js

90 lines
2.4 KiB
JavaScript
Raw Normal View History

var path = require('path');
var database = require(path.join(__dirname, '../../database.js'));
2018-08-06 12:09:37 +00:00
let strftime = require('strftime');
module.exports = class LetterDebtor {
2018-07-31 09:08:22 +00:00
async getData(params, cb) {
2018-08-06 12:09:37 +00:00
let qryData = `SELECT
c.id clientId,
LOWER(ct.code) AS countryCode,
c.email AS recipient,
c.socialName AS clientName,
2018-08-06 12:09:37 +00:00
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 = ?`;
2018-08-06 12:09:37 +00:00
let qryLines = `CALL vn.clientGetDebtDiary(?, ?)`;
2018-07-31 09:08:22 +00:00
try {
2018-08-06 12:09:37 +00:00
let [data] = await database.pool.query(qryData, [params.clientId]);
2018-07-31 09:08:22 +00:00
2018-08-06 12:09:37 +00:00
if (!data)
2018-07-31 09:08:22 +00:00
throw new Error('No body data found');
2018-08-06 12:09:37 +00:00
let [lines] = await database.pool.query(qryLines, [params.clientId, params.companyId]);
2018-08-06 12:09:37 +00:00
Object.assign(this, data[0]);
this.lines = lines[0];
this.formatLines();
this.getBalance();
cb();
2018-07-31 09:08:22 +00:00
} catch (e) {
cb(e);
}
}
2018-08-06 12:09:37 +00:00
get currentDate() {
return strftime('%d-%m-%Y', new Date());
}
2018-08-06 12:09:37 +00:00
formatLines() {
this.lines.forEach(line => {
if (line.issued)
line.issued = strftime('%d-%m-%Y', line.issued);
});
}
2018-08-06 12:09:37 +00:00
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();
}
};