diff --git a/services/db/install/changes/1.0.10/03-clientGetDebtDiary.sql b/services/db/install/changes/1.0.10/03-clientGetDebtDiary.sql new file mode 100644 index 000000000..0ee4c653c --- /dev/null +++ b/services/db/install/changes/1.0.10/03-clientGetDebtDiary.sql @@ -0,0 +1,98 @@ +USE `vn`; +DROP procedure IF EXISTS `clientGetDebtDiary`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` PROCEDURE `clientGetDebtDiary`(vClientFK INT, vCompanyFk INT) +BEGIN +/** + * Devuelve el registro de deuda + * + * @param vClientFK Id del cliente + * @param vCompanyFk Id de la empresa + */ + DECLARE vDate DATETIME DEFAULT CURDATE(); + + SET @balance:= 0; + + SELECT MAX(issued) INTO vDate FROM + (SELECT + created, + issued, + @balance:= ROUND(amount, 2) + @balance AS balance + FROM invoiceOut + WHERE clientFk = vClientFk AND companyFk = vCompanyFk + + UNION ALL + + SELECT + created, + payed, + @balance:= ROUND(-1 * amountPaid, 2) + @balance AS balance + FROM receipt + WHERE clientFk = vClientFk AND companyFk = vCompanyFk + ORDER BY issued, created) balance + WHERE balance = 0; + + SELECT + issued, + CAST(debtOut AS DECIMAL(10,2)) debtOut, + CAST(debtIn AS DECIMAL(10,2)) debtIn, + ref, + companyFk, + priority + FROM + (SELECT + NULL AS issued, + SUM(amountUnpaid) AS debtOut, + NULL AS debtIn, + 'Saldo Anterior' AS ref, + companyFk, + 0 as priority + FROM + (SELECT SUM(amount) AS amountUnpaid, companyFk, 0 + FROM invoiceOut + WHERE clientFk = vClientFK + AND issued < vDate + GROUP BY companyFk + + UNION ALL + + SELECT SUM(-1 * amountPaid), companyFk, 0 + FROM receipt + WHERE clientFk = vClientFK + AND payed < vDate + GROUP BY companyFk) AS transactions + GROUP BY companyFk + + UNION ALL + + SELECT + issued, + amount as debtOut, + NULL AS debtIn, + id AS ref, + companyFk, + 1 + FROM invoiceOut + WHERE clientFk = vClientFK + AND issued >= vDate + UNION ALL + + SELECT + r.payed, + NULL as debtOut, + r.amountPaid, + r.id, + r.companyFk, + 0 + FROM receipt r + WHERE r.clientFk = vClientFK + AND r.payed >= vDate) t + INNER JOIN `client` c ON c.id = vClientFK + HAVING debtOut <> 0 OR debtIn <> 0 + ORDER BY issued, priority DESC, debtIn; +END$$ + +DELIMITER ; + diff --git a/services/print/application/template/letter-debtor/index.html b/services/print/application/template/letter-debtor/index.html index a2d92979b..68094db50 100644 --- a/services/print/application/template/letter-debtor/index.html +++ b/services/print/application/template/letter-debtor/index.html @@ -10,21 +10,16 @@ -
- +

EXTRACTO

-
+
CLIENTE:
{{clientId}}
-
-
FACTURA:
-
x
-
-
+
FECHA:
{{currentDate}}
@@ -32,49 +27,48 @@
+
Datos de cliente

- {{supplierName}} + {{clientName}}

- {{supplierStreet}} + {{street}}
- {{supplierPostCode}}, {{supplierCity}} ({{supplierProvince}}) + {{postcode}}, {{city}} ({{province}})
- {{supplierCountry}} + {{country}}
-
+
-
Fecha
-
Concepto
-
Debe
-
Haber
-
Saldo
+
Fecha
+
Concepto
+
Facturado
+
Pagado
+
Saldo
+ {{#lines}}
-
Fecha
-
Concepto
-
Debe
-
Haber
-
Saldo
+
{{issued}} 
+
{{ref}} 
+
{{debtOut}} 
+
{{debtIn}} 
+
{{balance}} 
+
+ {{/lines}} +
+
Total
+
{{totalDebtOut}}
+
{{totalDebtIn}}
+
{{totalBalance}}
- -
- -
-
- Subtotal -
-
-
- -
+
diff --git a/services/print/application/template/letter-debtor/letter-debtor.js b/services/print/application/template/letter-debtor/letter-debtor.js index 872a3a676..51ec0223e 100644 --- a/services/print/application/template/letter-debtor/letter-debtor.js +++ b/services/print/application/template/letter-debtor/letter-debtor.js @@ -1,59 +1,89 @@ var path = require('path'); var database = require(path.join(__dirname, '../../database.js')); -var format = require(path.join(__dirname, '../../util/format.js')); +let strftime = require('strftime'); module.exports = class LetterDebtor { async getData(params, cb) { - let query = `SELECT + let qryData = `SELECT c.id clientId, - m.code mandateCode, LOWER(ct.code) AS countryCode, c.email AS recipient, c.socialName AS clientName, - c.street AS clientStreet, - c.postcode AS clientPostCode, - c.city AS clientCity, - p.name AS clientProvince, - ct.country AS clientCountry, - s.name AS supplierName, - s.street AS supplierStreet, - sc.country AS supplierCountry, - s.postCode AS supplierPostCode, - s.city AS supplierCity, - sp.name AS supplierProvince + 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 - LEFT JOIN mandate m ON m.clientFk = c.id AND m.finished IS NULL - LEFT JOIN supplier s ON s.id = m.companyFk - LEFT JOIN country sc ON sc.id = s.countryFk - LEFT JOIN province sp ON sp.id = s.provinceFk WHERE c.id = ?`; - try { - let [result] = await database.pool.query(query, [params.clientId]); - if (!result) + let qryLines = `CALL vn.clientGetDebtDiary(?, ?)`; + + try { + let [data] = await database.pool.query(qryData, [params.clientId]); + + if (!data) throw new Error('No body data found'); - Object.assign(this, result); + 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); } } - // Swift BIC fields - get swiftFields() { - return new Array(11); - } - - // Account number fields - get accountNumberFields() { - return new Array(23); - } - get currentDate() { - return format.date(new Date(), '/'); + 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(); } };