salix/back/methods/collection/getSales.js

143 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 10:43:15 +00:00
module.exports = Self => {
Self.remoteMethodCtx('getSales', {
2024-04-11 10:16:00 +00:00
description: 'Get sales from ticket, collection or sectorCollection',
2023-12-18 10:43:15 +00:00
accessType: 'READ',
accepts: [
{
arg: 'collectionOrTicketFk',
type: 'number',
required: true
}, {
arg: 'print',
type: 'boolean',
2023-12-18 10:43:15 +00:00
required: true
}, {
2023-12-18 10:43:15 +00:00
arg: 'source',
type: 'string',
required: true
},
],
returns: {
type: 'Object',
root: true
},
http: {
path: `/getSales`,
2023-12-18 10:43:15 +00:00
verb: 'GET'
},
});
Self.getSales = async(ctx, collectionOrTicketFk, print, source, options) => {
const userId = ctx.req.accessToken.userId;
const myOptions = {userId};
2023-12-18 10:43:15 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
const [{id}] = await Self.rawSql('SELECT vn.ticket_get(?) as id',
[collectionOrTicketFk],
myOptions);
2023-12-18 10:43:15 +00:00
const [tickets] = await Self.rawSql('CALL vn.collection_getTickets(?)', [id], myOptions);
2023-12-18 10:43:15 +00:00
if (source) {
await Self.rawSql(
2024-03-05 09:09:32 +00:00
'CALL vn.ticketStateToday_setState(?,?)', [id, source], myOptions
);
}
const [sales] = await Self.rawSql('CALL vn.sale_getFromTicketOrCollection(?)',
[id], myOptions);
2023-12-18 10:43:15 +00:00
2024-02-27 09:29:49 +00:00
const isPicker = source != 'CHECKER';
const [placements] = await Self.rawSql('CALL vn.collectionPlacement_get(?, ?)',
[id, isPicker], myOptions
2023-12-18 10:43:15 +00:00
);
if (print) await Self.rawSql('CALL vn.collection_printSticker(?,NULL)', [id], myOptions);
2023-12-18 10:43:15 +00:00
return getCollection(id, tickets, sales, placements, myOptions);
};
2024-01-25 07:13:47 +00:00
async function getCollection(id, tickets, sales, placements, options) {
const collection = {
collectionFk: id,
tickets: [],
};
for (let ticket of tickets) {
const {ticketFk} = ticket;
ticket.sales = [];
const barcodes = await getBarcodes(ticketFk, options);
2024-01-25 07:13:47 +00:00
await Self.rawSql(
'CALL util.log_add(?, ?, ?, ?, ?, ?, ?, ?)',
['vn', 'ticket', 'Ticket', ticketFk, ticketFk, 'select', null, null],
options
);
for (let sale of sales) {
if (sale.ticketFk == ticketFk) {
sale.placements = [];
for (const salePlacement of placements) {
if (salePlacement.saleFk == sale.saleFk && salePlacement.order) {
const placement = {
2024-01-25 07:13:47 +00:00
saleFk: salePlacement.saleFk,
itemFk: salePlacement.itemFk,
placement: salePlacement.placement,
shelving: salePlacement.shelving,
created: salePlacement.created,
visible: salePlacement.visible,
order: salePlacement.order,
grouping: salePlacement.grouping,
priority: salePlacement.priority,
saleOrder: salePlacement.saleOrder,
isPreviousPrepared: salePlacement.isPreviousPrepared,
itemShelvingSaleFk: salePlacement.itemShelvingSaleFk,
ticketFk: salePlacement.ticketFk,
id: salePlacement.id
};
sale.placements.push(placement);
}
}
2023-12-18 10:43:15 +00:00
2024-01-25 07:13:47 +00:00
sale.barcodes = [];
for (const barcode of barcodes) {
if (barcode.movementId == sale.saleFk) {
if (barcode.code) {
sale.barcodes.push(barcode.code);
sale.barcodes.push(`0 ${barcode.code}`);
}
2023-12-18 10:43:15 +00:00
2024-01-25 07:13:47 +00:00
if (barcode.id) {
sale.barcodes.push(barcode.id);
sale.barcodes.push(`0 ${barcode.id}`);
2023-12-18 10:43:15 +00:00
}
}
}
2024-01-25 07:13:47 +00:00
ticket.sales.push(sale);
2023-12-18 10:43:15 +00:00
}
}
2024-01-25 07:13:47 +00:00
collection.tickets.push(ticket);
2023-12-18 10:43:15 +00:00
}
2024-01-25 07:13:47 +00:00
return collection;
}
async function getBarcodes(ticketId, options) {
2024-01-25 07:13:47 +00:00
const query =
2023-12-18 10:43:15 +00:00
`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], options);
2024-01-25 07:13:47 +00:00
}
2023-12-18 10:43:15 +00:00
};