168 lines
7.6 KiB
JavaScript
168 lines
7.6 KiB
JavaScript
const closure = require('./closure');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('closeAll', {
|
|
description: 'Makes the closure process from all warehouses',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'options',
|
|
type: 'object',
|
|
http: {source: 'body'},
|
|
description: 'Optional parameters, including transaction.'
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/close-all`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.closeAll = async(ctx, options) => {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const myOptions = {userId};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
let tx;
|
|
// IMPORTANT: Due to its high cost in production, wrapping this process in a transaction may cause timeouts.
|
|
|
|
const toDate = Date.vnNew();
|
|
toDate.setHours(0, 0, 0, 0);
|
|
toDate.setDate(toDate.getDate() - 1);
|
|
|
|
const tickets = await Self.rawSql(`
|
|
SELECT t.id,
|
|
t.clientFk,
|
|
t.companyFk,
|
|
c.id clientFk,
|
|
c.name clientName,
|
|
c.email recipient,
|
|
c.salesPersonFk,
|
|
c.isToBeMailed,
|
|
c.hasToInvoice,
|
|
c.hasDailyInvoice,
|
|
eu.email salesPersonEmail,
|
|
t.addressFk
|
|
FROM ticket t
|
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
|
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
|
JOIN client c ON c.id = t.clientFk
|
|
JOIN province p ON p.id = c.provinceFk
|
|
JOIN country co ON co.id = p.countryFk
|
|
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
|
|
JOIN ticketConfig tc ON TRUE
|
|
WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered'))
|
|
AND t.shipped BETWEEN ? - INTERVAL tc.closureDaysAgo DAY AND util.dayEnd(?)
|
|
AND t.refFk IS NULL
|
|
GROUP BY t.id
|
|
`, [toDate, toDate], myOptions);
|
|
const ticketIds = tickets.map(ticket => ticket.id);
|
|
await Self.rawSql(`
|
|
INSERT INTO util.debug (variable, value)
|
|
VALUES ('nightInvoicing', ?)
|
|
`, [ticketIds.join(',')], myOptions);
|
|
|
|
await Self.rawSql(`
|
|
WITH ticketNotInvoiceable AS(
|
|
SELECT JSON_OBJECT(
|
|
'tickets',
|
|
JSON_ARRAYAGG(
|
|
JSON_OBJECT(
|
|
'ticketId', ticketFk,
|
|
'reason', reason,
|
|
'clientId', clientFk
|
|
)
|
|
)
|
|
)errors
|
|
FROM (
|
|
SELECT ticketFk,
|
|
CONCAT_WS(', ',
|
|
IF(hasErrorToInvoice, 'Facturar', NULL),
|
|
IF(hasErrorTaxDataChecked, 'Datos comprobados', NULL),
|
|
IF(hasErrorDeleted, 'Eliminado', NULL),
|
|
IF(hasErrorItemTaxCountry, 'Impuesto no informado', NULL),
|
|
IF(hasErrorAddress, 'Sin dirección', NULL),
|
|
IF(hasErrorInfoTaxAreaWorld, 'Datos exportaciones', NULL)) reason,
|
|
clientFk
|
|
FROM (
|
|
SELECT t.id ticketFk,
|
|
SUM(NOT c.hasToInvoice) hasErrorToInvoice,
|
|
SUM(NOT c.isTaxDataChecked) hasErrorTaxDataChecked,
|
|
SUM(t.isDeleted) hasErrorDeleted,
|
|
SUM(itc.id IS NULL) hasErrorItemTaxCountry,
|
|
SUM(a.id IS NULL) hasErrorAddress,
|
|
SUM(ios.code IS NOT NULL
|
|
AND(ad.customsAgentFk IS NULL
|
|
OR ad.incotermsFk IS NULL)) hasErrorInfoTaxAreaWorld,
|
|
t.clientFk clientFk
|
|
FROM ticket t
|
|
LEFT JOIN address ad ON ad.id = t.addressFk
|
|
JOIN sale s ON s.ticketFk = t.id
|
|
JOIN item i ON i.id = s.itemFk
|
|
JOIN supplier su ON su.id = t.companyFk
|
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
|
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
|
JOIN client c ON c.id = t.clientFk
|
|
JOIN province p ON p.id = c.provinceFk
|
|
JOIN ticketConfig tc ON TRUE
|
|
LEFT JOIN autonomy a ON a.id = p.autonomyFk
|
|
JOIN country co ON co.id = p.countryFk
|
|
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
|
|
LEFT JOIN itemTaxCountry itc ON itc.itemFk = i.id
|
|
AND itc.countryFk = su.countryFk
|
|
LEFT JOIN vn.invoiceOutSerial ios ON ios.taxAreaFk = 'WORLD'
|
|
AND ios.code = invoiceSerial(t.clientFk, t.companyFk, 'multiple')
|
|
WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered'))
|
|
AND t.shipped BETWEEN ? - INTERVAL tc.closureDaysAgo DAY AND util.dayEnd(?)
|
|
AND t.refFk IS NULL
|
|
AND c.hasDailyInvoice
|
|
GROUP BY ticketFk
|
|
HAVING hasErrorToInvoice
|
|
OR hasErrorTaxDataChecked
|
|
OR hasErrorDeleted
|
|
OR hasErrorItemTaxCountry
|
|
OR hasErrorAddress
|
|
OR hasErrorInfoTaxAreaWorld
|
|
)sub
|
|
)sub2
|
|
) SELECT IF(errors = '{"tickets": null}',
|
|
'No errors',
|
|
util.notification_send('invoice-ticket-closure', errors, NULL))
|
|
FROM ticketNotInvoiceable`, [toDate, toDate], myOptions);
|
|
|
|
await closure(ctx, Self, tickets, myOptions);
|
|
|
|
await Self.rawSql(`
|
|
UPDATE ticket t
|
|
JOIN ticketState ts ON t.id = ts.ticketFk
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
|
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
|
|
JOIN ticketConfig tc ON TRUE
|
|
LEFT JOIN ticketObservation tob ON tob.ticketFk = t.id
|
|
SET t.routeFk = NULL
|
|
WHERE t.shipped BETWEEN ? - INTERVAL tc.closureDaysAgo DAY AND util.dayEnd(?)
|
|
AND al.code NOT IN ('DELIVERED', 'PACKED')
|
|
AND NOT t.packages
|
|
AND tob.id IS NULL
|
|
AND t.routeFk`, [toDate, toDate], myOptions);
|
|
|
|
if (tx)
|
|
await tx.commit();
|
|
|
|
return {
|
|
message: 'Success'
|
|
};
|
|
};
|
|
};
|