2022-03-02 13:59:30 +00:00
|
|
|
module.exports = Self => {
|
2023-06-01 06:32:06 +00:00
|
|
|
Self.remoteMethodCtx('createInvoiceIn', {
|
2022-03-08 12:42:44 +00:00
|
|
|
description: 'Creates an invoiceIn from one or more agency terms',
|
2022-03-02 13:59:30 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'rows',
|
|
|
|
type: ['object'],
|
|
|
|
required: true,
|
2022-03-08 12:42:44 +00:00
|
|
|
description: `The rows from which the invoiceIn will be created`,
|
2022-03-02 13:59:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'dms',
|
|
|
|
type: ['object'],
|
|
|
|
required: true,
|
2022-03-08 12:42:44 +00:00
|
|
|
description: 'The dms file attached'
|
2022-03-02 13:59:30 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2022-03-08 12:42:44 +00:00
|
|
|
type: 'object',
|
|
|
|
root: true
|
2022-03-02 13:59:30 +00:00
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/createInvoiceIn`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-01 06:32:06 +00:00
|
|
|
Self.createInvoiceIn = async(ctx, rows, dms, options) => {
|
2022-03-02 13:59:30 +00:00
|
|
|
const models = Self.app.models;
|
2022-03-04 10:28:45 +00:00
|
|
|
|
|
|
|
let tx;
|
2023-06-01 06:32:06 +00:00
|
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
2022-03-02 13:59:30 +00:00
|
|
|
|
|
|
|
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-07 12:54:18 +00:00
|
|
|
const reference = await models.Dms.findById([firstDms.id], null, 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,
|
2022-03-07 12:54:18 +00:00
|
|
|
supplierRef: reference.reference,
|
2022-03-04 10:28:45 +00:00
|
|
|
issued: firstRow.created,
|
|
|
|
booked: firstRow.created,
|
|
|
|
operated: firstRow.created,
|
|
|
|
bookEntried: firstRow.created,
|
2022-03-08 12:42:44 +00:00
|
|
|
dmsFk: firstDms.id,
|
2022-03-04 10:28:45 +00:00
|
|
|
}, myOptions);
|
2022-03-02 13:59:30 +00:00
|
|
|
|
2022-03-07 12:54:18 +00:00
|
|
|
const expence = await models.AgencyTermConfig.findOne(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,
|
2022-03-07 12:54:18 +00:00
|
|
|
expenseFk: expence.expenceFk,
|
2022-03-04 10:28:45 +00:00
|
|
|
taxTypeSageFk: taxTypeSage.value,
|
|
|
|
transactionTypeSageFk: transactionTypeSage.value
|
|
|
|
}, myOptions);
|
2022-03-02 13:59:30 +00:00
|
|
|
|
2022-03-08 12:42:44 +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();
|
2022-03-08 12:42:44 +00:00
|
|
|
|
|
|
|
return newInvoiceIn;
|
2022-03-04 10:28:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
2022-03-02 13:59:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|