salix/services/loopback/common/methods/order/new.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-11-12 12:12:25 +00:00
let UserError = require('../../helpers').UserError;
2018-07-09 11:54:43 +00:00
module.exports = Self => {
Self.remoteMethod('new', {
description: 'Create a new order and returns the new ID',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
Self.new = async params => {
2018-10-10 07:59:42 +00:00
let address = await Self.app.models.Address.findOne({
where: {id: params.addressFk},
2018-11-12 12:12:25 +00:00
fields: ['clientFk']
2018-10-10 07:59:42 +00:00
});
let clientFk = address.clientFk;
2018-07-09 11:54:43 +00:00
let client = await Self.app.models.Client.findOne({
2018-08-17 14:12:05 +00:00
where: {id: clientFk},
2018-07-09 11:54:43 +00:00
fields: ['isTaxDataChecked', 'isFreezed', 'isActive']
});
if (client.isFreezed)
throw new UserError(`You can't create an order for a frozen client`);
2018-07-09 11:54:43 +00:00
if (!client.isActive)
throw new UserError(`You can't create an order for a inactive client`);
2018-07-09 11:54:43 +00:00
if (!client.isTaxDataChecked)
throw new UserError(`You can't create an order for a client that doesn't has tax data verified`);
2018-07-09 11:54:43 +00:00
let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`;
2018-08-17 14:12:05 +00:00
let clientDebt = await Self.rawSql(query, [clientFk]);
2018-07-09 11:54:43 +00:00
if (clientDebt[0].debt > 0)
throw new UserError(`You can't create an order for a client that has a debt`);
2018-07-09 11:54:43 +00:00
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
[result] = await Self.rawSql(query, [
2018-07-09 11:54:43 +00:00
params.landed,
params.agencyModeFk,
params.addressFk,
2018-11-12 12:12:25 +00:00
'SALIX'
2018-07-09 11:54:43 +00:00
]);
2018-07-09 11:54:43 +00:00
return result[0].vOrderId;
};
};