salix/modules/item/back/methods/item/regularize.js

128 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-11-12 12:12:25 +00:00
module.exports = Self => {
Self.remoteMethodCtx('regularize', {
description: 'Sends items to the trash',
accessType: 'WRITE',
2021-07-12 10:54:59 +00:00
accepts: [
{
arg: 'itemFk',
type: 'number',
required: true,
description: 'The item id',
},
{
arg: 'quantity',
type: 'number',
required: true,
description: 'The visible quantity',
},
{
arg: 'warehouseFk',
type: 'number',
required: true,
description: 'The id of the warehouse where the inventory happened',
}
],
2018-11-12 12:12:25 +00:00
returns: {
type: 'boolean',
root: true
},
http: {
path: `/regularize`,
verb: 'post'
}
});
2021-07-12 10:54:59 +00:00
Self.regularize = async(ctx, itemFk, quantity, warehouseFk, options) => {
2018-11-12 12:12:25 +00:00
const models = Self.app.models;
2021-07-12 10:54:59 +00:00
const myOptions = {};
let tx;
2018-11-12 12:12:25 +00:00
2021-07-12 10:54:59 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2018-11-12 12:12:25 +00:00
try {
2021-07-12 10:54:59 +00:00
const itemDestination = await models.ClaimDestination.findOne({
include: {
relation: 'address',
scope: {
fields: ['clientFk']
}
},
where: {description: 'Corregido'}
}, myOptions);
2021-07-12 10:54:59 +00:00
const item = await models.Item.findById(itemFk, null, myOptions);
2018-11-12 12:12:25 +00:00
2021-07-12 10:54:59 +00:00
let ticketId = await getTicketId({
2018-11-12 12:12:25 +00:00
clientFk: itemDestination.address.clientFk,
addressFk: itemDestination.addressFk,
warehouseFk: warehouseFk
2021-07-12 10:54:59 +00:00
}, myOptions);
2018-11-12 12:12:25 +00:00
2021-07-12 10:54:59 +00:00
if (!ticketId) {
2021-08-13 14:46:44 +00:00
ctx.args = {
clientId: itemDestination.address().clientFk,
warehouseId: warehouseFk,
addressId: itemDestination.addressFk
2021-08-13 14:46:44 +00:00
};
ticketId = await createTicket(ctx, myOptions);
2018-11-12 12:12:25 +00:00
}
2021-08-13 14:46:44 +00:00
const res = await models.Item.getVisibleAvailable(itemFk, warehouseFk, null, myOptions);
2018-11-12 12:12:25 +00:00
2021-08-13 14:46:44 +00:00
const newQuantity = res.visible - quantity;
2018-11-12 12:12:25 +00:00
await models.Sale.create({
2021-07-12 10:54:59 +00:00
ticketFk: ticketId,
2018-11-12 12:12:25 +00:00
itemFk: itemFk,
concept: item.name,
quantity: newQuantity,
discount: 100
2021-07-12 10:54:59 +00:00
}, myOptions);
if (tx) await tx.commit();
2018-11-12 12:12:25 +00:00
2021-07-12 10:54:59 +00:00
return ticketId;
2018-11-12 12:12:25 +00:00
} catch (e) {
2021-07-12 10:54:59 +00:00
if (tx) await tx.rollback();
2018-11-12 12:12:25 +00:00
throw e;
}
2021-08-13 14:46:44 +00:00
async function createTicket(ctx, options) {
ctx.args.shipped = new Date();
ctx.args.landed = new Date();
ctx.args.companyId = null;
ctx.args.agencyModeId = null;
ctx.args.routeId = null;
const ticket = await Self.app.models.Ticket.new(ctx, options);
2018-11-12 12:12:25 +00:00
return ticket.id;
}
async function getTicketId(params, options) {
const minDate = new Date();
minDate.setHours(0, 0, 0, 0);
const maxDate = new Date();
maxDate.setHours(23, 59, 59, 59);
2018-11-12 12:12:25 +00:00
let ticket = await Self.app.models.Ticket.findOne({
where: {
addressFk: params.addressFk,
warehouseFk: params.warehouseFk,
shipped: {between: [minDate, maxDate]},
landed: {between: [minDate, maxDate]}
2018-11-12 12:12:25 +00:00
}
}, options);
2018-11-12 12:12:25 +00:00
return ticket && ticket.id;
}
};
};