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) => {
|
|
const models = Self.app.models;
|
|
|
|
const itemDestination = await models.ClaimDestination.findOne({
|
|
include: {
|
|
relation: 'address',
|
|
scope: {
|
|
fields: ['clientFk']
|
|
}
|
|
},
|
|
where: {description: 'Corregido'}
|
|
});
|
|
|
|
let tx = await Self.beginTransaction({});
|
|
try {
|
|
let options = {transaction: tx};
|
|
|
|
let item = await models.Item.findById(itemFk);
|
|
|
|
let ticketFk = await getTicketId({
|
|
clientFk: itemDestination.address.clientFk,
|
|
addressFk: itemDestination.addressFk,
|
|
warehouseFk: warehouseFk
|
|
}, options);
|
|
|
|
if (!ticketFk) {
|
|
ticketFk = await createTicket(ctx, {
|
|
clientId: itemDestination.address().clientFk,
|
|
warehouseId: warehouseFk,
|
|
addressId: itemDestination.addressFk
|
|
}, options);
|
|
}
|
|
|
|
let query = `
|
|
CALL vn.item_getVisibleAvailable(?,curdate(),?,?)`;
|
|
|
|
let params = [itemFk, warehouseFk, true];
|
|
let [res] = await Self.rawSql(query, params, options);
|
|
|
|
let newQuantity = res[0].visible - quantity;
|
|
|
|
await models.Sale.create({
|
|
ticketFk: ticketFk,
|
|
itemFk: itemFk,
|
|
concept: item.name,
|
|
quantity: newQuantity,
|
|
discount: 100
|
|
}, options);
|
|
|
|
await tx.commit();
|
|
return ticketFk;
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
|
|
async function createTicket(ctx, params, options) {
|
|
params.shipped = new Date();
|
|
params.landed = new Date();
|
|
params.companyId = null;
|
|
params.agencyModeId = null;
|
|
params.routeId = null;
|
|
|
|
const ticket = await Self.app.models.Ticket.new(ctx,
|
|
params.clientId,
|
|
params.shipped,
|
|
params.landed,
|
|
params.warehouseId,
|
|
params.companyId,
|
|
params.addressId,
|
|
params.agencyModeId,
|
|
params.routeId,
|
|
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;
|
|
}
|
|
};
|
|
};
|