60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
let UserError = require('../../helpers').UserError;
|
|
|
|
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 => {
|
|
let address = await Self.app.models.Address.findOne({
|
|
where: {id: params.addressFk},
|
|
fields: ['clientFk']
|
|
});
|
|
let clientFk = address.clientFk;
|
|
|
|
let client = await Self.app.models.Client.findOne({
|
|
where: {id: clientFk},
|
|
fields: ['isTaxDataChecked', 'isFreezed', 'isActive']
|
|
});
|
|
|
|
if (client.isFreezed)
|
|
throw new UserError(`You can't create an order for a frozen client`);
|
|
|
|
if (!client.isActive)
|
|
throw new UserError(`You can't create an order for a inactive client`);
|
|
|
|
if (!client.isTaxDataChecked)
|
|
throw new UserError(`You can't create an order for a client that doesn't has tax data verified`);
|
|
|
|
let query = `SELECT vn.clientGetDebt(?, CURDATE()) AS debt`;
|
|
let clientDebt = await Self.rawSql(query, [clientFk]);
|
|
|
|
if (clientDebt[0].debt > 0)
|
|
throw new UserError(`You can't create an order for a client that has a debt`);
|
|
|
|
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
|
|
[result] = await Self.rawSql(query, [
|
|
params.landed,
|
|
params.agencyModeFk,
|
|
params.addressFk,
|
|
'SALIX'
|
|
]);
|
|
|
|
return result[0].vOrderId;
|
|
};
|
|
};
|