salix/modules/item/back/methods/item/createIntrastat.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-02-17 11:47:03 +00:00
module.exports = Self => {
Self.remoteMethod('createIntrastat', {
description: 'Creates a new item intrastat',
accessType: 'WRITE',
2021-07-12 10:54:59 +00:00
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The item id',
http: {source: 'path'}
},
{
arg: 'intrastatId',
type: 'number',
required: true
},
{
arg: 'description',
type: 'string',
required: true
}
],
2020-02-17 11:47:03 +00:00
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/createIntrastat`,
verb: 'PATCH'
}
});
2021-07-12 10:54:59 +00:00
Self.createIntrastat = async(id, intrastatId, description, options) => {
2020-02-17 11:47:03 +00:00
const models = Self.app.models;
2021-07-12 10:54:59 +00:00
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2021-07-12 13:00:20 +00:00
const country = await models.Country.findOne({
where: {code: 'ES'}
}, myOptions);
2021-07-12 10:54:59 +00:00
2021-07-12 13:00:20 +00:00
const itemTaxCountry = await models.ItemTaxCountry.findOne({
where: {
itemFk: id,
countryFk: country.id
},
order: 'effectived DESC'
}, myOptions);
2021-07-12 10:54:59 +00:00
2021-07-12 13:00:20 +00:00
const taxClassCode = await models.TaxClassCode.findOne({
where: {
taxClassFk: itemTaxCountry.taxClassFk
},
order: 'effectived DESC'
}, myOptions);
2021-07-12 10:54:59 +00:00
2021-07-12 13:00:20 +00:00
const intrastat = await models.Intrastat.create({
id: intrastatId,
description: description,
taxClassFk: itemTaxCountry.taxClassFk,
taxCodeFk: taxClassCode.taxCodeFk
}, myOptions);
2021-07-12 10:54:59 +00:00
2021-07-12 13:00:20 +00:00
return intrastat;
2020-02-17 11:47:03 +00:00
};
};