59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
|
module.exports = Self => {
|
||
|
Self.remoteMethodCtx('rectification', {
|
||
|
description: 'Creates a rectificated invoice in',
|
||
|
accessType: 'WRITE',
|
||
|
accepts: [{
|
||
|
arg: 'id',
|
||
|
type: 'number'
|
||
|
}, {
|
||
|
arg: 'invoiceReason',
|
||
|
type: 'string',
|
||
|
}, {
|
||
|
arg: 'invoiceType',
|
||
|
type: 'string',
|
||
|
}, {
|
||
|
arg: 'invoiceClass',
|
||
|
type: 'string'
|
||
|
}],
|
||
|
returns: {
|
||
|
type: 'number',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: '/rectification',
|
||
|
verb: 'POST'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.rectification = async(ctx, id, invoiceReason, invoiceType, invoiceClass, options) => {
|
||
|
const models = Self.app.models;
|
||
|
const myOptions = {};
|
||
|
let tx;
|
||
|
|
||
|
if (typeof options == 'object')
|
||
|
Object.assign(myOptions, options);
|
||
|
|
||
|
if (!myOptions.transaction) {
|
||
|
tx = await Self.beginTransaction({});
|
||
|
myOptions.transaction = tx;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
const clone = await Self.clone(ctx, id, true, myOptions);
|
||
|
await models.InvoiceInCorrection.create({
|
||
|
correctingFk: id,
|
||
|
correctedFk: clone.id,
|
||
|
cplusRectificationTypeFk: invoiceReason,
|
||
|
siiTypeInvoiceOutFk: invoiceClass,
|
||
|
invoiceCorrectionTypeFk: invoiceType
|
||
|
}, myOptions);
|
||
|
|
||
|
if (tx) await tx.commit();
|
||
|
return clone.id;
|
||
|
} catch (e) {
|
||
|
if (tx) await tx.rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
};
|
||
|
};
|