salix/modules/invoiceOut/back/methods/invoiceOut/negativeBases.js

112 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-04-20 06:24:43 +00:00
const UserError = require('vn-loopback/util/user-error');
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethodCtx('negativeBases', {
description: 'Find all negative bases',
accessType: 'READ',
accepts: [
{
arg: 'from',
type: 'date',
description: 'From date'
},
{
arg: 'to',
type: 'date',
description: 'To date'
},
{
arg: 'filter',
type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
},
],
returns: {
type: ['object'],
root: true
},
http: {
path: `/negativeBases`,
verb: 'GET'
}
});
Self.negativeBases = async(ctx, options) => {
const conn = Self.dataSource.connector;
const args = ctx.args;
if (!args.from || !args.to)
throw new UserError(`Insert a date range`);
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const stmts = [];
let stmt;
2024-02-15 06:49:17 +00:00
stmts.push(new ParameterizedSQL(
`CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(KEY (ticketFk))
ENGINE = MEMORY
SELECT id ticketFk
FROM ticket t
2024-02-15 06:52:33 +00:00
WHERE shipped BETWEEN ? AND util.dayEnd(?)
2024-02-15 06:49:17 +00:00
AND refFk IS NULL`, [args.from, args.to]));
stmts.push(`CALL vn.ticket_getTax(NULL)`);
2023-04-20 06:24:43 +00:00
stmts.push(new ParameterizedSQL(
`CREATE OR REPLACE TEMPORARY TABLE tmp.filter
2023-04-20 06:24:43 +00:00
ENGINE = MEMORY
SELECT co.code company,
2024-04-26 08:26:20 +00:00
cou.name country,
2023-04-20 06:24:43 +00:00
c.id clientId,
c.socialName clientSocialName,
2024-02-09 10:49:29 +00:00
u.nickname workerSocialName,
2023-04-20 06:24:43 +00:00
SUM(s.quantity * s.price * ( 100 - s.discount ) / 100) amount,
2024-02-15 06:49:17 +00:00
negativeBase.taxableBase,
negativeBase.ticketFk,
2023-04-20 06:24:43 +00:00
c.isActive,
c.hasToInvoice,
c.isTaxDataChecked,
w.id comercialId,
2024-04-22 05:37:47 +00:00
u.name workerName
FROM vn.ticket t
JOIN vn.company co ON co.id = t.companyFk
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.client c ON c.id = t.clientFk
JOIN vn.country cou ON cou.id = c.countryFk
LEFT JOIN vn.worker w ON w.id = c.salesPersonFk
JOIN account.user u ON u.id = w.id
2024-02-15 06:49:17 +00:00
LEFT JOIN (
SELECT ticketFk, taxableBase
FROM tmp.ticketAmount
GROUP BY ticketFk
HAVING taxableBase < 0
) negativeBase ON negativeBase.ticketFk = t.id
WHERE t.shipped BETWEEN ? AND util.dayEnd(?)
AND t.refFk IS NULL
AND c.typeFk IN ('normal','trust')
2024-02-15 08:38:34 +00:00
GROUP BY t.clientFk, negativeBase.taxableBase
HAVING amount < 0`, [args.from, args.to]));
2023-04-20 06:24:43 +00:00
stmt = new ParameterizedSQL(`SELECT * FROM tmp.filter`);
2023-04-20 06:24:43 +00:00
2023-04-21 06:20:38 +00:00
if (args.filter) {
stmt.merge(conn.makeWhere(args.filter.where));
stmt.merge(conn.makeOrderBy(args.filter.order));
stmt.merge(conn.makeLimit(args.filter));
}
2023-04-20 06:24:43 +00:00
const negativeBasesIndex = stmts.push(stmt) - 1;
2024-02-15 06:49:17 +00:00
stmts.push(`DROP TEMPORARY TABLE tmp.filter, tmp.ticket, tmp.ticketTax, tmp.ticketAmount`);
2023-04-20 06:24:43 +00:00
const sql = ParameterizedSQL.join(stmts, ';');
2023-04-21 06:20:38 +00:00
const result = await conn.executeStmt(sql);
2023-04-20 06:24:43 +00:00
return negativeBasesIndex === 0 ? result : result[negativeBasesIndex];
};
};