salix/modules/route/back/methods/agency-term/createInvoiceIn.js

100 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-03-02 13:59:30 +00:00
module.exports = Self => {
Self.remoteMethod('createInvoiceIn', {
2022-03-03 13:42:12 +00:00
description: 'create a invoce in from one or more agency terms',
2022-03-02 13:59:30 +00:00
accessType: 'WRITE',
accepts: [{
arg: 'rows',
type: ['object'],
required: true,
description: `the rows from which the invoice in will be created`,
},
{
arg: 'dms',
type: ['object'],
required: true,
2022-03-03 13:42:12 +00:00
description: 'the dms file'
2022-03-02 13:59:30 +00:00
}],
returns: {
},
http: {
path: `/createInvoiceIn`,
verb: 'POST'
}
});
Self.createInvoiceIn = async(rows, dms, options) => {
const models = Self.app.models;
2022-03-04 10:28:45 +00:00
let tx;
2022-03-02 13:59:30 +00:00
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2022-03-04 10:28:45 +00:00
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const [firstRow] = rows;
const [firstDms] = dms;
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
const [reference] = await Self.rawSql(`SELECT reference FROM vn.dms WHERE id = ?`, [firstDms.id], myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
const newInvoiceIn = await models.InvoiceIn.create({
supplierFk: firstRow.supplierFk,
supplierRef: reference.value,
issued: firstRow.created,
booked: firstRow.created,
operated: firstRow.created,
bookEntried: firstRow.created,
gestdoc_id: firstDms.id
}, myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
const [expence] = await Self.rawSql(`SELECT expenceFk value FROM vn.agencyTermConfig`, null, myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
const [taxTypeSage] = await Self.rawSql(`
2022-03-02 13:59:30 +00:00
SELECT IFNULL(s.taxTypeSageFk, CodigoIva) value
FROM vn.supplier s
JOIN sage.TiposIva ti ON TRUE
JOIN vn.agencyTermConfig atg
WHERE s.id = ?
AND ti.CuentaIvaSoportado = atg.vatAccountSupported
AND ti.PorcentajeIva = atg.vatPercentage
2022-03-04 10:28:45 +00:00
`, [firstRow.supplierFk], myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
const [transactionTypeSage] = await Self.rawSql(`
2022-03-02 13:59:30 +00:00
SELECT IFNULL(s.transactionTypeSageFk, tt.CodigoTransaccion) value
FROM vn.supplier s
JOIN sage.TiposTransacciones tt ON TRUE
JOIN vn.agencyTermConfig atg
WHERE s.id = ?
AND tt.Transaccion = atg.transaction
2022-03-04 10:28:45 +00:00
`, [firstRow.supplierFk], myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
await models.InvoiceInTax.create({
invoiceInFk: newInvoiceIn.id,
taxableBase: firstRow.totalPrice,
expenseFk: expence.value,
taxTypeSageFk: taxTypeSage.value,
transactionTypeSageFk: transactionTypeSage.value
}, myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
await Self.rawSql(`CALL invoiceInDueDay_calculate(?)`, [newInvoiceIn.id], myOptions);
2022-03-02 13:59:30 +00:00
2022-03-04 10:28:45 +00:00
for (let agencyTerm of rows) {
const route = await models.Route.findById(agencyTerm.routeFk, null, myOptions);
await Self.rawSql(`
2022-03-02 13:59:30 +00:00
UPDATE vn.route SET invoiceInFk = ? WHERE id = ?
2022-03-04 10:28:45 +00:00
`, [newInvoiceIn.id, route.id], myOptions);
}
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
2022-03-02 13:59:30 +00:00
}
};
};