salix/back/methods/collection/newCollection.js

134 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-03-23 07:24:57 +00:00
module.exports = Self => {
2020-03-26 12:12:59 +00:00
Self.remoteMethodCtx('newCollection', {
2020-03-23 07:24:57 +00:00
description: 'Make a new collection of tickets',
accessType: 'WRITE',
accepts: [{
arg: 'collectionFk',
type: 'Number',
required: false,
description: 'The collection id'
}, {
arg: 'sectorFk',
type: 'Number',
required: true,
description: 'The sector of worker'
2020-03-23 14:22:48 +00:00
}, {
arg: 'vWagons',
type: 'Number',
required: true,
description: 'The number of wagons'
2020-03-23 07:24:57 +00:00
}],
returns: {
type: 'Object',
root: true
},
http: {
2020-03-26 12:12:59 +00:00
path: `/newCollection`,
2020-03-23 07:24:57 +00:00
verb: 'POST'
}
});
2020-03-26 12:12:59 +00:00
Self.newCollection = async(ctx, collectionFk, sectorFk, vWagons) => {
2020-03-23 07:24:57 +00:00
let query = '';
2023-06-01 06:32:06 +00:00
const userId = ctx.req.accessToken.userId;
2020-03-23 07:24:57 +00:00
if (!collectionFk) {
2020-03-23 14:22:48 +00:00
query = `CALL vn.collectionTrain_newBeta(?,?,?)`;
2023-06-01 06:32:06 +00:00
const [result] = await Self.rawSql(query, [sectorFk, vWagons, userId], {userId});
2020-03-23 07:24:57 +00:00
if (result.length == 0)
throw new Error(`No collections for today`);
collectionFk = result[0].vCollectionFk;
}
query = `CALL vn.collectionTicket_get(?)`;
2023-06-01 06:32:06 +00:00
const [tickets] = await Self.rawSql(query, [collectionFk], {userId});
2020-03-23 07:24:57 +00:00
query = `CALL vn.collectionSale_get(?)`;
2023-06-01 06:32:06 +00:00
const [sales] = await Self.rawSql(query, [collectionFk], {userId});
2020-03-23 07:24:57 +00:00
query = `CALL vn.collectionPlacement_get(?)`;
2023-06-01 06:32:06 +00:00
const [placements] = await Self.rawSql(query, [collectionFk], {userId});
2020-03-23 07:24:57 +00:00
2020-03-23 12:26:45 +00:00
query = `CALL vn.collectionSticker_print(?,?)`;
2023-06-01 06:32:06 +00:00
await Self.rawSql(query, [collectionFk, sectorFk], {userId});
2020-03-23 12:26:45 +00:00
2020-03-23 14:22:48 +00:00
return makeCollection(tickets, sales, placements, collectionFk);
2020-03-23 07:24:57 +00:00
};
/**
* Returns a collection json
* @param {*} tickets - Request tickets
* @param {*} sales - Request sales
* @param {*} placements - Request placements
2020-03-23 14:34:13 +00:00
* @param {*} collectionFk - Request placements
2020-03-23 07:24:57 +00:00
* @return {Object} Collection JSON
*/
2020-03-23 14:22:48 +00:00
async function makeCollection(tickets, sales, placements, collectionFk) {
2020-03-23 07:24:57 +00:00
let collection = [];
for (let i = 0; i < tickets.length; i++) {
let ticket = {};
ticket['ticketFk'] = tickets[i]['ticketFk'];
ticket['level'] = tickets[i]['level'];
ticket['agencyName'] = tickets[i]['agencyName'];
ticket['warehouseFk'] = tickets[i]['warehouseFk'];
ticket['salesPersonFk'] = tickets[i]['salesPersonFk'];
let ticketSales = [];
for (let x = 0; x < sales.length; x++) {
if (sales[x]['ticketFk'] == ticket['ticketFk']) {
let sale = {};
2020-03-23 14:22:48 +00:00
sale['collectionFk'] = collectionFk;
2020-03-23 07:24:57 +00:00
sale['ticketFk'] = sales[x]['ticketFk'];
sale['saleFk'] = sales[x]['saleFk'];
sale['itemFk'] = sales[x]['itemFk'];
sale['quantity'] = sales[x]['quantity'];
2020-03-26 12:12:59 +00:00
if (sales[x]['quantityPicked'] != null)
sale['quantityPicked'] = sales[x]['quantityPicked'];
else
sale['quantityPicked'] = 0;
2020-03-23 07:24:57 +00:00
sale['longName'] = sales[x]['longName'];
sale['size'] = sales[x]['size'];
sale['color'] = sales[x]['color'];
sale['discount'] = sales[x]['discount'];
sale['price'] = sales[x]['price'];
sale['stems'] = sales[x]['stems'];
sale['category'] = sales[x]['category'];
sale['origin'] = sales[x]['origin'];
sale['clientFk'] = sales[x]['clientFk'];
sale['productor'] = sales[x]['productor'];
sale['reserved'] = sales[x]['reserved'];
sale['isPreviousPrepared'] = sales[x]['isPreviousPrepared'];
sale['isPrepared'] = sales[x]['isPrepared'];
sale['isControlled'] = sales[x]['isControlled'];
let salePlacements = [];
for (let z = 0; z < placements.length; z++) {
if (placements[z]['saleFk'] == sale['saleFk']) {
let placement = {};
placement['saleFk'] = placements[z]['saleFk'];
placement['itemFk'] = placements[z]['itemFk'];
placement['placement'] = placements[z]['placement'];
placement['shelving'] = placements[z]['shelving'];
placement['created'] = placements[z]['created'];
placement['visible'] = placements[z]['visible'];
placement['order'] = placements[z]['order'];
placement['grouping'] = placements[z]['grouping'];
salePlacements.push(placement);
}
}
sale['placements'] = salePlacements;
ticketSales.push(sale);
}
}
ticket['sales'] = ticketSales;
collection.push(ticket);
}
return collection;
}
};