154 lines
7.1 KiB
JavaScript
154 lines
7.1 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
const closure = require('./closure');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('closeAll', {
|
|
description: 'Makes the closure process from all warehouses',
|
|
accessType: 'WRITE',
|
|
accepts: [],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/close-all`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.closeAll = async ctx => {
|
|
const toDate = Date.vnNew();
|
|
toDate.setHours(0, 0, 0, 0);
|
|
toDate.setDate(toDate.getDate() - 1);
|
|
|
|
const todayMinDate = Date.vnNew();
|
|
todayMinDate.setHours(0, 0, 0, 0);
|
|
|
|
const todayMaxDate = Date.vnNew();
|
|
todayMaxDate.setHours(23, 59, 59, 59);
|
|
|
|
// Prevent closure for current day
|
|
if (toDate >= todayMinDate && toDate <= todayMaxDate)
|
|
throw new UserError('You cannot close tickets for today');
|
|
|
|
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,
|
|
co.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
|
|
WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered'))
|
|
AND DATE(t.shipped) BETWEEN ? - INTERVAL 2 DAY AND util.dayEnd(?)
|
|
AND t.refFk IS NULL
|
|
GROUP BY t.id
|
|
`, [toDate, toDate]);
|
|
const ticketIds = tickets.map(ticket => ticket.id);
|
|
await Self.rawSql(`
|
|
INSERT INTO util.debug (variable, value)
|
|
VALUES ('nightInvoicing', ?)
|
|
`, [ticketIds.join(',')]);
|
|
|
|
await Self.rawSql(`
|
|
WITH ticketNotInvoiceable AS(
|
|
SELECT JSON_OBJECT(
|
|
'tickets',
|
|
JSON_ARRAYAGG(
|
|
JSON_OBJECT(
|
|
'ticketId', ticketFk,
|
|
'reason', reason
|
|
)
|
|
)
|
|
)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
|
|
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
|
|
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
|
|
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, 'M')
|
|
WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered'))
|
|
AND DATE(t.shipped) BETWEEN ? - INTERVAL 2 DAY AND util.dayEnd(?)
|
|
AND t.refFk IS NULL
|
|
AND IFNULL(a.hasDailyInvoice, co.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]);
|
|
|
|
await closure(ctx, Self, tickets);
|
|
|
|
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
|
|
LEFT JOIN ticketObservation tob ON tob.ticketFk = t.id
|
|
SET t.routeFk = NULL
|
|
WHERE DATE(t.shipped) BETWEEN ? - INTERVAL 2 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], {userId: ctx.req.accessToken.userId});
|
|
|
|
return {
|
|
message: 'Success'
|
|
};
|
|
};
|
|
};
|