salix/back/methods/collection/getSalesFromTicketOrCollect...

147 lines
5.1 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('getSalesFromTicketOrCollection', {
description: 'Get sales from ticket or collection',
accessType: 'READ',
accepts: [
{
arg: 'collectionOrTicketFk',
type: 'number',
required: true
},
{
arg: 'sectorFk',
type: 'number',
required: true
},
{
arg: 'printFk',
type: 'number',
required: true
},
{
arg: 'source',
type: 'string',
required: true
},
],
returns: {
type: 'Object',
root: true
},
http: {
path: `/getSalesFromTicketOrCollection`,
verb: 'GET'
},
});
Self.getSalesFromTicketOrCollection = async(ctx, collectionOrTicketFk, sectorFk, printFk, source, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const [collectionOrTicket] = await Self.rawSql('SELECT vn.ticket_get(?) as id', [collectionOrTicketFk]);
const [tickets] = await Self.rawSql('CALL vn.collection_getTickets(?)', [collectionOrTicket.id]);
if (source == 'PRECHECKER' || source == 'ON_CHECKING') {
await Self.rawSql(
'CALL vn.ticketStateToday_setState(?,?)',
[collectionOrTicket.id, source]
);
}
const [sales] = await Self.rawSql('CALL vn.sale_getFromTicketOrCollection(?)', [collectionOrTicket.id]);
const isPicker = source == 'CHECKER';
const [placements] = await Self.rawSql(
'CALL vn.collectionPlacement_get(?, ?)', [collectionOrTicket.id, isPicker]
);
if (printFk == 1) await Self.rawSql('CALL vn.collection_printSticker(?,NULL)', [collectionOrTicket.id]);
if (tickets.length) await sendRocketTickets(tickets);
return getCollection(collectionOrTicket.id, tickets, sales, placements);
async function sendRocketTickets(tickets) {
for (let ticket of tickets) {
let observations = ticket.observaciones.split(' ');
for (let observation of observations) {
const salesMan = ticket.salesPersonFk;
if (!observation.startsWith('#') && !observation.startsWith('@')) return;
await models.Chat.send(ctx,
observation,
`El ticket ${ticket.ticketFk} del comercial ${salesMan} está en preparación.(mensaje creado automáticamente)`
);
}
}
}
async function getCollection(id, tickets, sales, placements) {
const collection = {
collectionFk: id,
tickets: [],
};
for (let ticket of tickets) {
const {ticketFk} = ticket;
ticket.sales = [];
const barcodes = await getBarcodes(ticketFk);
await Self.rawSql(
'CALL util.log_add(?, ?, ?, ?, ?, ?, ?, ?)',
['vn', 'ticket', 'Ticket', ticketFk, ticketFk, 'select', null, null]
);
for (let sale of sales) {
if (sale.ticketFk == ticketFk) {
sale.placements = placements.filter(placement =>
placement.saleFk == sale.saleFk && placement.order
);
sale.barcodes = [];
for (const barcode of barcodes) {
if (barcode.movementId) {
if (barcode.code) {
sale.barcodes.push(barcode.code);
sale.barcodes.push(`0 ${barcode.code}`);
}
if (barcode.id) {
sale.barcodes.push(barcode.id);
sale.barcodes.push(`0 ${barcode.id}`);
}
}
}
ticket.sales.push(sale);
}
}
collection.tickets.push(ticket);
}
return collection;
}
async function getBarcodes(ticketId) {
const query =
`SELECT s.id movementId,
b.code,
c.id
FROM vn.sale s
LEFT JOIN vn.itemBarcode b ON b.itemFk = s.itemFk
LEFT JOIN vn.buy c ON c.itemFk = s.itemFk
LEFT JOIN vn.entry e ON e.id = c.entryFk
LEFT JOIN vn.travel tr ON tr.id = e.travelFk
WHERE s.ticketFk = ?
AND tr.landed >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)`;
return Self.rawSql(query, [ticketId]);
}
};
};