86 lines
3.0 KiB
JavaScript
86 lines
3.0 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethod('createInvoiceIn', {
|
|
description: 'create a invoce in from one or more agency terms',
|
|
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,
|
|
description: 'the dms file'
|
|
}],
|
|
returns: {
|
|
},
|
|
http: {
|
|
path: `/createInvoiceIn`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.createInvoiceIn = async(rows, dms, options) => {
|
|
const models = Self.app.models;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const [firstRow] = rows;
|
|
const [firstDms] = dms;
|
|
|
|
const [reference] = await Self.rawSql(`SELECT reference FROM vn.dms WHERE id = ?`, [firstDms.id]);
|
|
|
|
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
|
|
});
|
|
|
|
const [expence] = await Self.rawSql(`SELECT expenceFk value FROM vn.agencyTermConfig`);
|
|
|
|
const [taxTypeSage] = await Self.rawSql(`
|
|
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
|
|
`, [firstRow.supplierFk]);
|
|
|
|
const [transactionTypeSage] = await Self.rawSql(`
|
|
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
|
|
`, [firstRow.supplierFk]);
|
|
|
|
await models.InvoiceInTax.create({
|
|
invoiceInFk: newInvoiceIn.id,
|
|
taxableBase: firstRow.totalPrice,
|
|
expenseFk: expence.value,
|
|
taxTypeSageFk: taxTypeSage.value,
|
|
transactionTypeSageFk: transactionTypeSage.value
|
|
});
|
|
|
|
await Self.rawSql(`CALL invoiceInDueDay_calculate(?)`, [newInvoiceIn.id]);
|
|
|
|
for (let agencyTerm of rows) {
|
|
const route = await models.Route.findById(agencyTerm.routeFk);
|
|
await Self.rawSql(`
|
|
UPDATE vn.route SET invoiceInFk = ? WHERE id = ?
|
|
`, [newInvoiceIn.id, route.id]);
|
|
}
|
|
};
|
|
};
|