2021-07-23 12:06:48 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('clone', {
|
|
|
|
description: 'Clone the invoiceIn and as many invoiceInTax and invoiceInDueDay referencing it',
|
|
|
|
accessType: 'WRITE',
|
2023-12-26 13:36:58 +00:00
|
|
|
accepts: [{
|
2021-07-23 12:06:48 +00:00
|
|
|
arg: 'id',
|
2021-07-23 13:17:30 +00:00
|
|
|
type: 'number',
|
2021-07-23 12:06:48 +00:00
|
|
|
required: true,
|
|
|
|
description: 'The invoiceIn id',
|
|
|
|
http: {source: 'path'}
|
2023-12-26 13:36:58 +00:00
|
|
|
}, {
|
|
|
|
arg: 'isRectification',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Clone quantities in negative and clone Intrastat'
|
|
|
|
}],
|
2021-07-23 12:06:48 +00:00
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: '/:id/clone',
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-26 13:36:58 +00:00
|
|
|
Self.clone = async(ctx, id, isRectification, options) => {
|
2021-07-23 12:06:48 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
let tx;
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const sourceInvoiceIn = await Self.findById(id, {
|
|
|
|
fields: [
|
|
|
|
'id',
|
|
|
|
'serial',
|
|
|
|
'supplierRef',
|
|
|
|
'supplierFk',
|
|
|
|
'issued',
|
|
|
|
'currencyFk',
|
|
|
|
'companyFk',
|
|
|
|
'isVatDeductible',
|
|
|
|
'withholdingSageFk',
|
|
|
|
'deductibleExpenseFk',
|
2023-12-26 13:36:58 +00:00
|
|
|
],
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'invoiceInTax',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'invoiceInDueDay',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'invoiceInIntrastat'
|
|
|
|
}
|
|
|
|
],
|
2021-07-23 12:06:48 +00:00
|
|
|
}, myOptions);
|
2023-12-26 13:36:58 +00:00
|
|
|
const invoiceInTax = sourceInvoiceIn.invoiceInTax();
|
|
|
|
const invoiceInDueDay = sourceInvoiceIn.invoiceInDueDay();
|
|
|
|
const invoiceInIntrastat = sourceInvoiceIn.invoiceInIntrastat();
|
2021-07-23 12:06:48 +00:00
|
|
|
|
|
|
|
const issued = new Date(sourceInvoiceIn.issued);
|
|
|
|
issued.setMonth(issued.getMonth() + 1);
|
2021-09-07 09:55:26 +00:00
|
|
|
const clonedRef = sourceInvoiceIn.supplierRef + '(2)';
|
2021-07-23 12:06:48 +00:00
|
|
|
|
|
|
|
const clone = await models.InvoiceIn.create({
|
|
|
|
serial: sourceInvoiceIn.serial,
|
2021-09-07 09:55:26 +00:00
|
|
|
supplierRef: clonedRef,
|
2021-07-23 12:06:48 +00:00
|
|
|
supplierFk: sourceInvoiceIn.supplierFk,
|
|
|
|
issued: issued,
|
|
|
|
currencyFk: sourceInvoiceIn.currencyFk,
|
|
|
|
companyFk: sourceInvoiceIn.companyFk,
|
|
|
|
isVatDeductible: sourceInvoiceIn.isVatDeductible,
|
|
|
|
withholdingSageFk: sourceInvoiceIn.withholdingSageFk,
|
|
|
|
deductibleExpenseFk: sourceInvoiceIn.deductibleExpenseFk,
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
|
2023-12-26 13:36:58 +00:00
|
|
|
for (let tax of invoiceInTax) {
|
2021-07-23 12:06:48 +00:00
|
|
|
promises.push(models.InvoiceInTax.create({
|
|
|
|
invoiceInFk: clone.id,
|
2023-12-26 13:36:58 +00:00
|
|
|
taxableBase: isRectification ? -tax.taxableBase : tax.taxableBase,
|
2021-08-12 05:39:26 +00:00
|
|
|
expenseFk: tax.expenseFk,
|
2023-12-26 13:36:58 +00:00
|
|
|
foreignValue: isRectification ? -tax.foreignValue : tax.foreignValue,
|
2021-07-23 12:06:48 +00:00
|
|
|
taxTypeSageFk: tax.taxTypeSageFk,
|
|
|
|
transactionTypeSageFk: tax.transactionTypeSageFk
|
|
|
|
}, myOptions));
|
|
|
|
}
|
|
|
|
|
2023-12-26 13:36:58 +00:00
|
|
|
if (!isRectification) {
|
|
|
|
for (let dueDay of invoiceInDueDay) {
|
|
|
|
const dueDated = dueDay.dueDated;
|
|
|
|
dueDated.setMonth(dueDated.getMonth() + 1);
|
2021-07-23 12:06:48 +00:00
|
|
|
|
2023-12-26 13:36:58 +00:00
|
|
|
promises.push(models.InvoiceInDueDay.create({
|
|
|
|
invoiceInFk: clone.id,
|
|
|
|
dueDated: dueDated,
|
|
|
|
bankFk: dueDay.bankFk,
|
|
|
|
amount: dueDay.amount,
|
|
|
|
foreignValue: dueDated.foreignValue,
|
|
|
|
}, myOptions));
|
|
|
|
}
|
2021-07-23 12:06:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 13:36:58 +00:00
|
|
|
if (isRectification) {
|
|
|
|
console.log(invoiceInIntrastat);
|
|
|
|
for (let intrastat of invoiceInIntrastat) {
|
|
|
|
promises.push(models.InvoiceInIntrastat.create({
|
|
|
|
invoiceInFk: clone.id,
|
|
|
|
net: -intrastat.net,
|
|
|
|
intrastatFk: intrastat.intrastatFk,
|
|
|
|
amount: -intrastat.amount,
|
|
|
|
stems: -intrastat.stems,
|
|
|
|
country: intrastat.countryFk,
|
|
|
|
dated: Date.vnNew(),
|
|
|
|
statisticalValue: intrastat.statisticalValue
|
|
|
|
}, myOptions));
|
|
|
|
}
|
|
|
|
}
|
2021-07-23 12:06:48 +00:00
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|