create rectification: refs #4466
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2023-12-26 14:36:58 +01:00
parent 703e16ffcd
commit a89d1e883a
6 changed files with 176 additions and 19 deletions

View File

@ -0,0 +1,21 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES
('InvoiceIn', 'rectification', 'WRITE', 'ALLOW', 'ROLE', 'employee');
CREATE TABLE `vn`.`invoiceInCorrection` (
`correctingFk` mediumint(8) UNSIGNED NOT NULL COMMENT 'Factura rectificativa',
`correctedFk` mediumint(8) UNSIGNED NOT NULL COMMENT 'Factura rectificada',
`cplusRectificationTypeFk` int(10) UNSIGNED NOT NULL,
`siiTypeInvoiceOutFk` int(10) UNSIGNED NOT NULL,
`invoiceCorrectionTypeFk` int(11) NOT NULL DEFAULT 3,
PRIMARY KEY (`correctingFk`),
KEY `invoiceInCorrection_correctedFk` (`correctedFk`),
KEY `invoiceInCorrection_cplusRectificationTypeFk` (`cplusRectificationTypeFk`),
KEY `invoiceInCorrection_siiTypeInvoiceOut` (`siiTypeInvoiceOutFk`),
KEY `invoiceInCorrection_invoiceCorrectionTypeFk` (`invoiceCorrectionTypeFk`),
CONSTRAINT `invoiceInCorrection_correctedFk` FOREIGN KEY (`correctedFk`) REFERENCES `invoiceIn` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_correctingFk` FOREIGN KEY (`correctingFk`) REFERENCES `invoiceIn` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_siiTypeInvoiceOut` FOREIGN KEY (`siiTypeInvoiceOutFk`) REFERENCES `siiTypeInvoiceOut` (`id`) ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_invoiceCorrectionTypeFk` FOREIGN KEY (`invoiceCorrectionTypeFk`) REFERENCES `invoiceCorrectionType` (`id`) ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_cplusRectificationTypeFk` FOREIGN KEY (`cplusRectificationTypeFk`) REFERENCES `cplusRectificationType` (`id`) ON UPDATE CASCADE
);

View File

@ -2,13 +2,17 @@ module.exports = Self => {
Self.remoteMethodCtx('clone', { Self.remoteMethodCtx('clone', {
description: 'Clone the invoiceIn and as many invoiceInTax and invoiceInDueDay referencing it', description: 'Clone the invoiceIn and as many invoiceInTax and invoiceInDueDay referencing it',
accessType: 'WRITE', accessType: 'WRITE',
accepts: { accepts: [{
arg: 'id', arg: 'id',
type: 'number', type: 'number',
required: true, required: true,
description: 'The invoiceIn id', description: 'The invoiceIn id',
http: {source: 'path'} http: {source: 'path'}
}, }, {
arg: 'isRectification',
type: 'boolean',
description: 'Clone quantities in negative and clone Intrastat'
}],
returns: { returns: {
type: 'object', type: 'object',
root: true root: true
@ -19,7 +23,7 @@ module.exports = Self => {
} }
}); });
Self.clone = async(ctx, id, options) => { Self.clone = async(ctx, id, isRectification, options) => {
const models = Self.app.models; const models = Self.app.models;
let tx; let tx;
const myOptions = {}; const myOptions = {};
@ -45,10 +49,22 @@ module.exports = Self => {
'isVatDeductible', 'isVatDeductible',
'withholdingSageFk', 'withholdingSageFk',
'deductibleExpenseFk', 'deductibleExpenseFk',
] ],
include: [
{
relation: 'invoiceInTax',
},
{
relation: 'invoiceInDueDay',
},
{
relation: 'invoiceInIntrastat'
}
],
}, myOptions); }, myOptions);
const sourceInvoiceInTax = await models.InvoiceInTax.find({where: {invoiceInFk: id}}, myOptions); const invoiceInTax = sourceInvoiceIn.invoiceInTax();
const sourceInvoiceInDueDay = await models.InvoiceInDueDay.find({where: {invoiceInFk: id}}, myOptions); const invoiceInDueDay = sourceInvoiceIn.invoiceInDueDay();
const invoiceInIntrastat = sourceInvoiceIn.invoiceInIntrastat();
const issued = new Date(sourceInvoiceIn.issued); const issued = new Date(sourceInvoiceIn.issued);
issued.setMonth(issued.getMonth() + 1); issued.setMonth(issued.getMonth() + 1);
@ -68,18 +84,19 @@ module.exports = Self => {
const promises = []; const promises = [];
for (let tax of sourceInvoiceInTax) { for (let tax of invoiceInTax) {
promises.push(models.InvoiceInTax.create({ promises.push(models.InvoiceInTax.create({
invoiceInFk: clone.id, invoiceInFk: clone.id,
taxableBase: tax.taxableBase, taxableBase: isRectification ? -tax.taxableBase : tax.taxableBase,
expenseFk: tax.expenseFk, expenseFk: tax.expenseFk,
foreignValue: tax.foreignValue, foreignValue: isRectification ? -tax.foreignValue : tax.foreignValue,
taxTypeSageFk: tax.taxTypeSageFk, taxTypeSageFk: tax.taxTypeSageFk,
transactionTypeSageFk: tax.transactionTypeSageFk transactionTypeSageFk: tax.transactionTypeSageFk
}, myOptions)); }, myOptions));
} }
for (let dueDay of sourceInvoiceInDueDay) { if (!isRectification) {
for (let dueDay of invoiceInDueDay) {
const dueDated = dueDay.dueDated; const dueDated = dueDay.dueDated;
dueDated.setMonth(dueDated.getMonth() + 1); dueDated.setMonth(dueDated.getMonth() + 1);
@ -91,7 +108,23 @@ module.exports = Self => {
foreignValue: dueDated.foreignValue, foreignValue: dueDated.foreignValue,
}, myOptions)); }, myOptions));
} }
}
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));
}
}
await Promise.all(promises); await Promise.all(promises);
if (tx) await tx.commit(); if (tx) await tx.commit();

View File

@ -0,0 +1,58 @@
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;
}
};
};

View File

@ -5,6 +5,9 @@
"InvoiceInConfig": { "InvoiceInConfig": {
"dataSource": "vn" "dataSource": "vn"
}, },
"InvoiceInCorrection": {
"dataSource": "vn"
},
"InvoiceInDueDay": { "InvoiceInDueDay": {
"dataSource": "vn" "dataSource": "vn"
}, },

View File

@ -0,0 +1,41 @@
{
"name": "InvoiceInCorrection",
"base": "VnModel",
"options": {
"mysql": {
"table": "invoiceInCorrection"
}
},
"properties": {
"correctingFk": {
"id": true,
"type": "number"
},
"correctedFk": {
"type": "number"
}
},
"relations": {
"invoiceIn": {
"type": "belongsTo",
"model": "InvoiceIn",
"foreignKey": "correctingFk"
},
"cplusRectificationType": {
"type": "belongsTo",
"model": "CplusRectificationType",
"foreignKey": "cplusRectificationTypeFk"
},
"invoiceCorrectionType": {
"type": "belongsTo",
"model": "InvoiceCorrectionType",
"foreignKey": "invoiceCorrectionTypeFk"
},
"siiTypeInvoiceOutFk": {
"type": "belongsTo",
"model": "SiiTypeInvoiceOut",
"foreignKey": "siiTypeInvoiceOutFk"
}
}
}

View File

@ -9,6 +9,7 @@ module.exports = Self => {
require('../methods/invoice-in/invoiceInPdf')(Self); require('../methods/invoice-in/invoiceInPdf')(Self);
require('../methods/invoice-in/invoiceInEmail')(Self); require('../methods/invoice-in/invoiceInEmail')(Self);
require('../methods/invoice-in/getSerial')(Self); require('../methods/invoice-in/getSerial')(Self);
require('../methods/invoice-in/rectification')(Self);
Self.rewriteDbError(function(err) { Self.rewriteDbError(function(err) {
if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn')) if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn'))
return new UserError(`This invoice has a linked vehicle.`); return new UserError(`This invoice has a linked vehicle.`);