2018-11-12 12:12:25 +00:00
|
|
|
let UserError = require('../../helpers').UserError;
|
2018-08-02 07:49:00 +00:00
|
|
|
|
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-29 15:02:54 +00:00
|
|
|
fields: ['clientFk'],
|
|
|
|
include: [
|
|
|
|
{relation: 'client'}
|
|
|
|
]
|
2018-10-10 07:59:42 +00:00
|
|
|
});
|
|
|
|
let clientFk = address.clientFk;
|
2018-11-29 15:02:54 +00:00
|
|
|
console.log(address);
|
|
|
|
if (address.client().isFreezed)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`You can't create an order for a frozen client`);
|
2018-07-09 11:54:43 +00:00
|
|
|
|
2018-11-29 15:02:54 +00:00
|
|
|
if (!address.client().isActive)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new UserError(`You can't create an order for a inactive client`);
|
2018-07-09 11:54:43 +00:00
|
|
|
|
2018-11-29 15:02:54 +00:00
|
|
|
if (!address.client().isTaxDataChecked)
|
2018-08-02 07:49:00 +00:00
|
|
|
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-11-29 15:02:54 +00:00
|
|
|
if (address.client().credit - clientDebt[0].debt <= 0)
|
2018-08-02 07:49:00 +00:00
|
|
|
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(?, ?, ?, ?);`;
|
2018-07-24 11:48:31 +00:00
|
|
|
[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-24 11:48:31 +00:00
|
|
|
|
2018-07-09 11:54:43 +00:00
|
|
|
return result[0].vOrderId;
|
|
|
|
};
|
|
|
|
};
|