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

58 lines
1.7 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},
fields: ['clientFk'],
include: [
{relation: 'client'}
]
2018-10-10 07:59:42 +00:00
});
let clientFk = address.clientFk;
console.log(address);
if (address.client().isFreezed)
throw new UserError(`You can't create an order for a frozen client`);
2018-07-09 11:54:43 +00:00
if (!address.client().isActive)
throw new UserError(`You can't create an order for a inactive client`);
2018-07-09 11:54:43 +00:00
if (!address.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]);
if (address.client().credit - 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;
};
};