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
|
2022-02-24 12:52:26 +00:00
|
|
|
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;
|
2021-11-18 10:17:30 +00:00
|
|
|
const myOptions = {};
|
2021-03-29 13:15:58 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2019-06-12 15:31:45 +00:00
|
|
|
|
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);
|
2020-02-13 12:20:31 +00:00
|
|
|
const addressId = destination && destination.addressFk;
|
2018-10-08 05:31:55 +00:00
|
|
|
|
2020-02-13 12:20:31 +00:00
|
|
|
let address;
|
|
|
|
if (addressId)
|
2021-03-29 13:15:58 +00:00
|
|
|
address = await models.Address.findById(addressId, null, myOptions);
|
2020-02-13 12:20:31 +00:00
|
|
|
|
2020-09-15 17:43:07 +00:00
|
|
|
const salesPerson = sale.ticket().client().salesPersonUser();
|
2020-02-13 12:20:31 +00:00
|
|
|
if (salesPerson) {
|
|
|
|
const nickname = address && address.nickname || destination.description;
|
2023-10-19 06:37:28 +00:00
|
|
|
const url = await Self.app.models.Url.getUrl();
|
2020-02-13 12:20:31 +00:00
|
|
|
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`
|
2020-02-13 12:20:31 +00:00
|
|
|
});
|
|
|
|
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!address) continue;
|
2018-10-08 05:31:55 +00:00
|
|
|
|
|
|
|
let ticketFk = await getTicketId({
|
2020-02-13 12:20:31 +00:00
|
|
|
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 = {
|
2019-12-26 06:12:34 +00:00
|
|
|
clientId: address.clientFk,
|
|
|
|
warehouseId: sale.ticket().warehouseFk,
|
|
|
|
companyId: sale.ticket().companyFk,
|
2020-02-13 12:20:31 +00:00
|
|
|
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: {
|
2019-04-25 11:32:14 +00:00
|
|
|
fields: ['clientFk', 'warehouseFk', 'companyFk'],
|
2018-10-08 05:31:55 +00:00
|
|
|
include: {
|
2019-04-25 11:32:14 +00:00
|
|
|
relation: 'client',
|
2019-04-04 10:05:01 +00:00
|
|
|
scope: {
|
|
|
|
include: {
|
2020-09-15 17:43:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
async function getTicketId(params, options) {
|
2023-01-16 14:18:24 +00:00
|
|
|
const minDate = Date.vnNew();
|
2019-10-21 06:31:14 +00:00
|
|
|
minDate.setHours(0, 0, 0, 0);
|
|
|
|
|
2023-01-16 14:18:24 +00:00
|
|
|
const maxDate = Date.vnNew();
|
2019-10-21 06:31:14 +00:00
|
|
|
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,
|
2019-10-21 06:31:14 +00:00
|
|
|
shipped: {between: [minDate, maxDate]},
|
|
|
|
landed: {between: [minDate, maxDate]}
|
2018-10-08 05:31:55 +00:00
|
|
|
}
|
2019-06-12 15:31:45 +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
|
|
|
}
|
|
|
|
};
|