feat: refs #8077 defaulter filter #3103

Open
robert wants to merge 11 commits from 8077-sumDefaulter into dev
1 changed files with 16 additions and 10 deletions

View File

@ -18,6 +18,11 @@ module.exports = Self => {
arg: 'search', arg: 'search',
type: 'string', type: 'string',
description: `If it's and integer searchs by id, otherwise it searchs by name` description: `If it's and integer searchs by id, otherwise it searchs by name`
},
{
arg: 'sumAmount',
type: 'boolean',
description: `Indicates whether to calculate the total sum of amounts or to return detailed records`
robert marked this conversation as resolved Outdated

si nohi ha descripcio llevau, pero intenta posar algo descriptiu

si nohi ha descripcio llevau, pero intenta posar algo descriptiu
} }
], ],
returns: { returns: {
@ -28,6 +33,7 @@ module.exports = Self => {
path: `/filter`, path: `/filter`,
verb: 'GET' verb: 'GET'
} }
}); });
Self.filter = async(ctx, filter, options) => { Self.filter = async(ctx, filter, options) => {
@ -50,14 +56,7 @@ module.exports = Self => {
filter = mergeFilters(ctx.args.filter, {where}); filter = mergeFilters(ctx.args.filter, {where});
const stmts = []; const stmts = [];
const fields = ctx.args.sumAmount ? 'sum(d.amount) sumAmount' : ` DISTINCT c.id clientFk,
const date = Date.vnNew();
date.setHours(0, 0, 0, 0);
const stmt = new ParameterizedSQL(
`SELECT *
FROM (
SELECT
DISTINCT c.id clientFk,
c.name clientName, c.name clientName,
c.salesPersonFk, c.salesPersonFk,
c.businessTypeFk = 'worker' isWorker, c.businessTypeFk = 'worker' isWorker,
@ -75,7 +74,14 @@ module.exports = Self => {
r.finished IS NULL hasRecovery, r.finished IS NULL hasRecovery,
dp.id departmentFk, dp.id departmentFk,
dp.name departmentName, dp.name departmentName,
dp.notificationEmail departmentEmail dp.notificationEmail departmentEmail`;
const date = Date.vnNew();
date.setHours(0, 0, 0, 0);
const stmt = new ParameterizedSQL(
`SELECT *
FROM (
SELECT
${fields}
FROM defaulter d FROM defaulter d
JOIN client c ON c.id = d.clientFk JOIN client c ON c.id = d.clientFk
JOIN country cn ON cn.id = c.countryFk JOIN country cn ON cn.id = c.countryFk
@ -102,7 +108,7 @@ module.exports = Self => {
, [date]); , [date]);
stmt.merge(conn.makeWhere(filter.where)); stmt.merge(conn.makeWhere(filter.where));
stmt.merge(`GROUP BY d.clientFk`); !ctx.args.sumAmount && stmt.merge(`GROUP BY d.clientFk`);
stmt.merge(conn.makeOrderBy(filter.order)); stmt.merge(conn.makeOrderBy(filter.order));
stmt.merge(conn.makeLimit(filter)); stmt.merge(conn.makeLimit(filter));