128 lines
3.8 KiB
JavaScript
128 lines
3.8 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('regularize', {
|
|
description: 'Sends items to the trash',
|
|
accessType: 'WRITE',
|
|
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',
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'boolean',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/regularize`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.regularize = async(ctx, itemFk, quantity, warehouseFk, options) => {
|
|
const models = Self.app.models;
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const itemDestination = await models.ClaimDestination.findOne({
|
|
include: {
|
|
relation: 'address',
|
|
scope: {
|
|
fields: ['clientFk']
|
|
}
|
|
},
|
|
where: {description: 'Corregido'}
|
|
}, myOptions);
|
|
|
|
const item = await models.Item.findById(itemFk, null, myOptions);
|
|
|
|
let ticketId = await getTicketId({
|
|
clientFk: itemDestination.address.clientFk,
|
|
addressFk: itemDestination.addressFk,
|
|
warehouseFk: warehouseFk
|
|
}, myOptions);
|
|
|
|
if (!ticketId) {
|
|
ctx.args = {
|
|
clientId: itemDestination.address().clientFk,
|
|
warehouseId: warehouseFk,
|
|
addressId: itemDestination.addressFk
|
|
};
|
|
ticketId = await createTicket(ctx, myOptions);
|
|
}
|
|
|
|
const res = await models.Item.getVisibleAvailable(itemFk, warehouseFk, null, myOptions);
|
|
|
|
const newQuantity = res.visible - quantity;
|
|
|
|
await models.Sale.create({
|
|
ticketFk: ticketId,
|
|
itemFk: itemFk,
|
|
concept: item.name,
|
|
quantity: newQuantity,
|
|
discount: 100
|
|
}, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return ticketId;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
let ticket = await Self.app.models.Ticket.findOne({
|
|
where: {
|
|
addressFk: params.addressFk,
|
|
warehouseFk: params.warehouseFk,
|
|
shipped: {between: [minDate, maxDate]},
|
|
landed: {between: [minDate, maxDate]}
|
|
}
|
|
}, options);
|
|
|
|
return ticket && ticket.id;
|
|
}
|
|
};
|
|
};
|