87 lines
3.0 KiB
JavaScript
87 lines
3.0 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('setWeight', {
|
|
description: 'Sets weight of a ticket',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The ticket id',
|
|
http: {source: 'path'}
|
|
}, {
|
|
arg: 'weight',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The weight value',
|
|
}],
|
|
returns: {
|
|
type: 'Array',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/setWeight`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.setWeight = async(ctx, ticketId, weight, options) => {
|
|
const models = Self.app.models;
|
|
const userId = ctx.req.accessToken.userId;
|
|
const myOptions = {userId};
|
|
let tx;
|
|
|
|
if (typeof options == 'object') Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const ticket = await Self.findById(ticketId, null, myOptions);
|
|
if (ticket.weight) throw new UserError('Weight already set');
|
|
|
|
const canEdit = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'updateAttributes');
|
|
const client = await models.Client.findById(ticket.clientFk, {
|
|
include: {relation: 'salesPersonUser'}},
|
|
myOptions);
|
|
|
|
if (!canEdit) {
|
|
const salesPersonUser = client.salesPersonUser();
|
|
const workerDepartments = await models.WorkerDepartment.find({
|
|
include: {relation: 'department'},
|
|
where: {workerFk: {inq: [userId, salesPersonUser.id]}}
|
|
}, myOptions);
|
|
|
|
if (
|
|
workerDepartments.length == 2 &&
|
|
workerDepartments[0].departmentFk != workerDepartments[1].departmentFk
|
|
)
|
|
throw new UserError('This ticket is not allocated to your department');
|
|
}
|
|
|
|
await ticket.updateAttribute('weight', weight, myOptions);
|
|
|
|
const packedState = await models.State.findOne({where: {code: 'PACKED'}}, myOptions);
|
|
const ticketState = await models.TicketState.findOne({where: {ticketFk: ticketId}}, myOptions);
|
|
|
|
const [{taxArea}] = await Self.rawSql('SELECT clientTaxArea(?,?) taxArea',
|
|
[ticket.clientFk, ticket.warehouseFk], myOptions);
|
|
|
|
const isInvoiceable = ticketState.alertLevel >= packedState.alertLevel &&
|
|
taxArea == 'WORLD' && client.hasDailyInvoice;
|
|
|
|
if (tx) await tx.commit();
|
|
let invoiceIds = [];
|
|
if (isInvoiceable) invoiceIds = [...await Self.invoiceTicketsAndPdf(ctx, [ticketId])];
|
|
|
|
return invoiceIds;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|