diff --git a/modules/monitor/back/methods/sales-monitor/salesFilter.js b/modules/monitor/back/methods/sales-monitor/salesFilter.js index af7ce02de..e5f992e2f 100644 --- a/modules/monitor/back/methods/sales-monitor/salesFilter.js +++ b/modules/monitor/back/methods/sales-monitor/salesFilter.js @@ -251,7 +251,7 @@ module.exports = Self => { // Remove positive risks stmts.push(` UPDATE tmp.tickets t - SET debt = NULL + SET debt = 0 WHERE t.debt + t.credit >= 0 `); @@ -267,9 +267,18 @@ module.exports = Self => { AND f.shipped >= CURDATE()`); stmts.push('CALL ticket_getProblems(FALSE)'); + stmts.push(` + INSERT INTO tmp.ticket_problems (ticketFk, risk, totalProblems) + SELECT t.id, t.debt + t.credit AS risk, 1 + FROM tmp.tickets t + WHERE (t.debt + t.credit) < 0 + ON DUPLICATE KEY UPDATE + risk = t.debt + t.credit, totalProblems = totalProblems + 1 + `); + stmt = new ParameterizedSQL(` - SELECT t.*, tp.*, t.debt + t.credit AS risk, - ((t.debt + t.credit) + cc.riskTolerance < 0) AS hasHighRisk + SELECT t.*, tp.*, + ((tp.risk) + cc.riskTolerance < 0) AS hasHighRisk FROM tmp.tickets t LEFT JOIN tmp.ticket_problems tp ON tp.ticketFk = t.id JOIN clientConfig cc`); @@ -283,7 +292,7 @@ module.exports = Self => { if (hasProblems === true) { whereProblems = {or: [ {'tp.isFreezed': true}, - {'tp.risk': {gt: 0}}, + {'tp.risk': {lt: 0}}, {'tp.hasTicketRequest': true}, {'tp.hasComponentLack': true}, {'tp.isTaxDataChecked': false}, diff --git a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js index 53cd9941e..8a5136c80 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js @@ -23,7 +23,7 @@ describe('SalesMonitor salesFilter()', () => { const filter = {}; const result = await app.models.SalesMonitor.salesFilter(ctx, filter); - expect(result.length).toEqual(9); + expect(result.length).toEqual(13); }); it('should return the tickets matching the problems on false', async() => {