salix/modules/claim/back/methods/claim/regularizeClaim.js

168 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-10-08 05:31:55 +00:00
module.exports = Self => {
Self.remoteMethodCtx('regularizeClaim', {
2023-01-16 14:18:24 +00:00
description: `Imports lines from claimBeginning to a new ticket
with specific shipped, landed dates, agency and company`,
2018-10-08 05:31:55 +00:00
accessType: 'WRITE',
accepts: [{
2020-05-13 13:04:57 +00:00
arg: 'id',
type: 'number',
description: 'The claim id',
http: {source: 'path'}
2018-10-08 05:31:55 +00:00
}],
returns: {
type: ['Object'],
root: true
},
http: {
2020-05-13 13:04:57 +00:00
path: `/:id/regularizeClaim`,
2018-10-08 05:31:55 +00:00
verb: 'POST'
}
});
2021-03-29 13:15:58 +00:00
Self.regularizeClaim = async(ctx, claimFk, options) => {
2018-10-08 05:31:55 +00:00
const models = Self.app.models;
2020-01-23 12:31:07 +00:00
const $t = ctx.req.__; // $translate
2018-10-08 05:31:55 +00:00
const resolvedState = 3;
2021-03-29 13:15:58 +00:00
let tx;
const myOptions = {};
2021-03-29 13:15:58 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
2021-03-29 13:15:58 +00:00
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
2019-09-18 13:11:09 +00:00
const claimEnds = await models.ClaimEnd.find({
include: {
relation: 'claimDestination',
fields: ['addressFk']
},
2020-05-13 13:04:57 +00:00
where: {claimFk: claimFk}
2021-03-29 13:15:58 +00:00
}, myOptions);
2019-09-18 13:11:09 +00:00
2021-03-29 13:15:58 +00:00
for (let claimEnd of claimEnds) {
2018-10-08 05:31:55 +00:00
const destination = claimEnd.claimDestination();
2021-03-29 13:15:58 +00:00
const sale = await getSale(claimEnd.saleFk, myOptions);
const addressId = destination && destination.addressFk;
2018-10-08 05:31:55 +00:00
let address;
if (addressId)
2021-03-29 13:15:58 +00:00
address = await models.Address.findById(addressId, null, myOptions);
const salesPerson = sale.ticket().client().salesPersonUser();
if (salesPerson) {
const nickname = address && address.nickname || destination.description;
const url = await Self.app.models.Url.getUrl();
const message = $t('Sent units from ticket', {
quantity: sale.quantity,
concept: sale.concept,
itemId: sale.itemFk,
ticketId: sale.ticketFk,
nickname: nickname,
2023-10-16 16:24:58 +00:00
ticketUrl: `${url}ticket/${sale.ticketFk}/sale`,
itemUrl: `${url}item/${sale.itemFk}/summary`
});
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
}
if (!address) continue;
2018-10-08 05:31:55 +00:00
let ticketFk = await getTicketId({
addressFk: addressId,
2018-10-08 05:31:55 +00:00
companyFk: sale.ticket().companyFk,
warehouseFk: sale.ticket().warehouseFk
2021-03-29 13:15:58 +00:00
}, myOptions);
2018-10-08 05:31:55 +00:00
if (!ticketFk) {
2021-08-13 14:46:44 +00:00
ctx.args = {
clientId: address.clientFk,
warehouseId: sale.ticket().warehouseFk,
companyId: sale.ticket().companyFk,
addressId: addressId
2021-08-13 14:46:44 +00:00
};
ticketFk = await createTicket(ctx, myOptions);
2018-10-08 05:31:55 +00:00
}
await models.Sale.create({
ticketFk: ticketFk,
itemFk: sale.itemFk,
concept: sale.concept,
2022-06-01 12:01:19 +00:00
quantity: -sale.quantity,
2018-10-08 05:31:55 +00:00
price: sale.price,
discount: 100
2021-03-29 13:15:58 +00:00
}, myOptions);
2018-10-08 05:31:55 +00:00
}
2021-03-29 13:15:58 +00:00
let claim = await Self.findById(claimFk, null, myOptions);
2018-10-08 05:31:55 +00:00
claim = await claim.updateAttributes({
claimStateFk: resolvedState
2021-03-29 13:15:58 +00:00
}, myOptions);
2018-10-08 05:31:55 +00:00
2021-03-29 13:15:58 +00:00
if (tx) await tx.commit();
2018-10-08 05:31:55 +00:00
return claim;
} catch (e) {
2021-03-29 13:15:58 +00:00
if (tx) await tx.rollback();
2018-10-08 05:31:55 +00:00
throw e;
}
};
2019-09-18 13:11:09 +00:00
async function getSale(saleFk, options) {
2018-10-08 05:31:55 +00:00
return await Self.app.models.Sale.findOne({
include: [
{
relation: 'ticket',
scope: {
fields: ['clientFk', 'warehouseFk', 'companyFk'],
2018-10-08 05:31:55 +00:00
include: {
relation: 'client',
2019-04-04 10:05:01 +00:00
scope: {
include: {
relation: 'salesPersonUser',
scope: {
fields: ['id', 'name']
}
2019-04-04 10:05:01 +00:00
}
}
2018-10-08 05:31:55 +00:00
}
}
}],
where: {id: saleFk}
2019-09-18 13:11:09 +00:00
}, options);
2018-10-08 05:31:55 +00:00
}
async function getTicketId(params, options) {
2023-01-16 14:18:24 +00:00
const minDate = Date.vnNew();
minDate.setHours(0, 0, 0, 0);
2023-01-16 14:18:24 +00:00
const maxDate = Date.vnNew();
maxDate.setHours(23, 59, 59, 59);
2018-10-08 05:31:55 +00:00
let ticket = await Self.app.models.Ticket.findOne({
where: {
addressFk: params.addressFk,
companyFk: params.companyFk,
warehouseFk: params.warehouseFk,
shipped: {between: [minDate, maxDate]},
landed: {between: [minDate, maxDate]}
2018-10-08 05:31:55 +00:00
}
}, options);
2018-10-08 05:31:55 +00:00
return ticket && ticket.id;
}
2021-08-13 14:46:44 +00:00
async function createTicket(ctx, options) {
2023-01-16 14:18:24 +00:00
ctx.args.shipped = Date.vnNew();
ctx.args.landed = Date.vnNew();
2021-08-13 14:46:44 +00:00
ctx.args.agencyModeId = null;
ctx.args.routeId = null;
const ticket = await Self.app.models.Ticket.new(ctx, options);
2018-10-10 07:59:42 +00:00
return ticket.id;
2018-10-08 05:31:55 +00:00
}
};