feat: add backTest
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-03-04 11:28:45 +01:00
parent cff1e6cfbf
commit d22be6620c
3 changed files with 57 additions and 57 deletions

View File

@ -24,15 +24,23 @@ module.exports = Self => {
Self.createInvoiceIn = async(rows, dms, options) => { Self.createInvoiceIn = async(rows, dms, options) => {
const models = Self.app.models; const models = Self.app.models;
let tx;
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const [firstRow] = rows; const [firstRow] = rows;
const [firstDms] = dms; const [firstDms] = dms;
const [reference] = await Self.rawSql(`SELECT reference FROM vn.dms WHERE id = ?`, [firstDms.id]); const [reference] = await Self.rawSql(`SELECT reference FROM vn.dms WHERE id = ?`, [firstDms.id], myOptions);
const newInvoiceIn = await models.InvoiceIn.create({ const newInvoiceIn = await models.InvoiceIn.create({
supplierFk: firstRow.supplierFk, supplierFk: firstRow.supplierFk,
@ -42,9 +50,9 @@ module.exports = Self => {
operated: firstRow.created, operated: firstRow.created,
bookEntried: firstRow.created, bookEntried: firstRow.created,
gestdoc_id: firstDms.id gestdoc_id: firstDms.id
}); }, myOptions);
const [expence] = await Self.rawSql(`SELECT expenceFk value FROM vn.agencyTermConfig`); const [expence] = await Self.rawSql(`SELECT expenceFk value FROM vn.agencyTermConfig`, null, myOptions);
const [taxTypeSage] = await Self.rawSql(` const [taxTypeSage] = await Self.rawSql(`
SELECT IFNULL(s.taxTypeSageFk, CodigoIva) value SELECT IFNULL(s.taxTypeSageFk, CodigoIva) value
@ -54,7 +62,7 @@ module.exports = Self => {
WHERE s.id = ? WHERE s.id = ?
AND ti.CuentaIvaSoportado = atg.vatAccountSupported AND ti.CuentaIvaSoportado = atg.vatAccountSupported
AND ti.PorcentajeIva = atg.vatPercentage AND ti.PorcentajeIva = atg.vatPercentage
`, [firstRow.supplierFk]); `, [firstRow.supplierFk], myOptions);
const [transactionTypeSage] = await Self.rawSql(` const [transactionTypeSage] = await Self.rawSql(`
SELECT IFNULL(s.transactionTypeSageFk, tt.CodigoTransaccion) value SELECT IFNULL(s.transactionTypeSageFk, tt.CodigoTransaccion) value
@ -63,7 +71,7 @@ module.exports = Self => {
JOIN vn.agencyTermConfig atg JOIN vn.agencyTermConfig atg
WHERE s.id = ? WHERE s.id = ?
AND tt.Transaccion = atg.transaction AND tt.Transaccion = atg.transaction
`, [firstRow.supplierFk]); `, [firstRow.supplierFk], myOptions);
await models.InvoiceInTax.create({ await models.InvoiceInTax.create({
invoiceInFk: newInvoiceIn.id, invoiceInFk: newInvoiceIn.id,
@ -71,15 +79,21 @@ module.exports = Self => {
expenseFk: expence.value, expenseFk: expence.value,
taxTypeSageFk: taxTypeSage.value, taxTypeSageFk: taxTypeSage.value,
transactionTypeSageFk: transactionTypeSage.value transactionTypeSageFk: transactionTypeSage.value
}); }, myOptions);
await Self.rawSql(`CALL invoiceInDueDay_calculate(?)`, [newInvoiceIn.id]); await Self.rawSql(`CALL invoiceInDueDay_calculate(?)`, [newInvoiceIn.id], myOptions);
for (let agencyTerm of rows) { for (let agencyTerm of rows) {
const route = await models.Route.findById(agencyTerm.routeFk); const route = await models.Route.findById(agencyTerm.routeFk, null, myOptions);
await Self.rawSql(` await Self.rawSql(`
UPDATE vn.route SET invoiceInFk = ? WHERE id = ? UPDATE vn.route SET invoiceInFk = ? WHERE id = ?
`, [newInvoiceIn.id, route.id]); `, [newInvoiceIn.id, route.id], myOptions);
}
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
} }
}; };
}; };

View File

@ -11,23 +11,9 @@ describe('AgencyTerm createInvoiceIn()', () => {
]; ];
const dms = [ const dms = [
{ {
id: 7, id: 6
file: '7.pdf',
contentType: 'application/pdf',
reference: '1',
description: 'Plants SL',
hasFile: false,
companyFk: 442,
dmsTypeFk: 1,
warehouseFk: 1,
workerFk: 9
} }
]; ];
const userId = 1;
const activeCtx = {
accessToken: {userId: userId},
};
const ctx = {req: activeCtx};
it('should make a global invoicing', async() => { it('should make a global invoicing', async() => {
const tx = await models.AgencyTerm.beginTransaction({}); const tx = await models.AgencyTerm.beginTransaction({});
@ -38,19 +24,19 @@ describe('AgencyTerm createInvoiceIn()', () => {
const invoiceInDueDayId = 11; const invoiceInDueDayId = 11;
const invoiceInTaxId = 12; const invoiceInTaxId = 12;
const oldInvoiceIn = await models.AgencyTerm.findById(invoiceInId, null, options); const oldInvoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
const oldInvoiceInDueDay = await models.AgencyTerm.findById(invoiceInDueDayId, null, options); const oldInvoiceInDueDay = await models.InvoiceInDueDay.findById(invoiceInDueDayId, null, options);
const oldInvoiceInTax = await models.AgencyTerm.findById(invoiceInTaxId, null, options); const oldInvoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);
const [newInvoiceIn] = await models.Ticket.rawSql('SELECT MAX(id) id FROM invoiceIn', null, options);
// const [newInvoiceInDueDay] = await models.AgencyTerm.rawSql('SELECT MAX(id) id FROM invoiceInDueDay', null, options);
// const [newInvoiceInTax] = await models.AgencyTerm.rawSql('SELECT MAX(id) id FROM invoiceInTax', null, options);
await models.AgencyTerm.createInvoiceIn(rows, dms, options); await models.AgencyTerm.createInvoiceIn(rows, dms, options);
// expect(newInvoiceIn.id).toEqual(oldInvoiceIn.id + 1); const [newInvoiceIn] = await models.InvoiceIn.rawSql('SELECT MAX(id) id FROM invoiceIn', null, options);
// expect(newInvoiceInDueDay.id).toEqual(oldInvoiceInDueDay.id + 1); const [newInvoiceInDueDay] = await models.InvoiceInDueDay.rawSql('SELECT MAX(id) id FROM invoiceInDueDay', null, options);
// expect(newInvoiceInTax.id).toEqual(oldInvoiceInTax.id + 1); const [newInvoiceInTax] = await models.InvoiceInTax.rawSql('SELECT MAX(id) id FROM invoiceInTax', null, options);
expect(newInvoiceIn.id).toBeGreaterThan(oldInvoiceIn.id);
expect(newInvoiceInDueDay.id).toBeGreaterThan(oldInvoiceInDueDay.id);
expect(newInvoiceInTax.id).toBeGreaterThan(oldInvoiceInTax.id);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
xdescribe('AgencyTerm filter()', () => { describe('AgencyTerm filter()', () => {
const authUserId = 9; const authUserId = 9;
it('should all return the tickets matching the filter', async() => { it('should all return the tickets matching the filter', async() => {
@ -9,9 +9,9 @@ xdescribe('AgencyTerm filter()', () => {
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
const filter = {}; const filter = {};
const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}}; const ctx = {req: {accessToken: {userId: authUserId}}};
const agencyTerms = await models.AgencyTerm.filter(ctx, null, options); const agencyTerms = await models.AgencyTerm.filter(ctx, filter, options);
const firstAgencyTerm = agencyTerms[0]; const firstAgencyTerm = agencyTerms[0];
expect(firstAgencyTerm.routeFk).toEqual(1); expect(firstAgencyTerm.routeFk).toEqual(1);