diff --git a/db/changes/10390-constitution/00-supplier.sql b/db/changes/10390-constitution/00-supplier.sql new file mode 100644 index 0000000000..f86a1c5346 --- /dev/null +++ b/db/changes/10390-constitution/00-supplier.sql @@ -0,0 +1,17 @@ +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`%` TRIGGER `vn`.`supplier_beforeUpdate` + BEFORE UPDATE ON `vn`.`supplier` FOR EACH ROW +BEGIN + DECLARE vHasChange BOOL; + SET vHasChange = !((NEW.payMethodFk <=> OLD.payMethodFk) + AND (NEW.payDemFk <=> OLD.payDemFk) + AND (NEW.payDay <=> OLD.payDay)); + + IF vHasChange THEN + SET NEW.isPayMethodChecked = false; + END IF; + +END +$$ +DELIMITER ; diff --git a/e2e/paths/13-supplier/02_basic_data.spec.js b/e2e/paths/13-supplier/02_basic_data.spec.js index f98fa779e7..4f3c495127 100644 --- a/e2e/paths/13-supplier/02_basic_data.spec.js +++ b/e2e/paths/13-supplier/02_basic_data.spec.js @@ -8,7 +8,7 @@ describe('Supplier basic data path', () => { beforeAll(async() => { browser = await getBrowser(); page = browser.page; - await page.loginAndModule('administrative', 'supplier'); + await page.loginAndModule('financial', 'supplier'); await page.accessToSearchResult('1'); await page.accessToSection('supplier.card.basicData'); }); diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 2611ee0ddb..c7797fe6a5 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -216,5 +216,6 @@ "The type of business must be filled in basic data": "El tipo de negocio debe estar rellenado en datos básicos", "You can't create a claim from a ticket delivered more than seven days ago": "No puedes crear una reclamación de un ticket entregado hace más de siete días", "The worker has hours recorded that day": "El trabajador tiene horas fichadas ese día", - "The worker has a marked absence that day": "El trabajador tiene marcada una ausencia ese día" + "The worker has a marked absence that day": "El trabajador tiene marcada una ausencia ese día", + "You can not modify is pay method checked": "No se puede modificar el campo método de pago validado" } \ No newline at end of file diff --git a/modules/supplier/back/models/specs/supplier.spec.js b/modules/supplier/back/models/specs/supplier.spec.js index 27bd873ad4..8721e826cf 100644 --- a/modules/supplier/back/models/specs/supplier.spec.js +++ b/modules/supplier/back/models/specs/supplier.spec.js @@ -8,6 +8,19 @@ describe('loopback model Supplier', () => { beforeAll(async() => { supplierOne = await models.Supplier.findById(1); supplierTwo = await models.Supplier.findById(442); + + const activeCtx = { + accessToken: {userId: 9}, + http: { + req: { + headers: {origin: 'http://localhost'} + } + } + }; + + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); }); afterAll(async() => { @@ -32,19 +45,6 @@ describe('loopback model Supplier', () => { }); it('should not throw if the payMethod id is valid', async() => { - const activeCtx = { - accessToken: {userId: 9}, - http: { - req: { - headers: {origin: 'http://localhost'} - } - } - }; - - spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ - active: activeCtx - }); - let error; const supplier = await models.Supplier.findById(442); await supplier.updateAttribute('payMethodFk', 4) @@ -54,5 +54,14 @@ describe('loopback model Supplier', () => { expect(error).not.toBeDefined(); }); + + it('should have unchecked isPayMethodChecked', async() => { + const supplier = await models.Supplier.findById(442); + await supplier.updateAttribute('payMethodFk', 5); + + const result = await models.Supplier.findById(442); + + expect(result.isPayMethodChecked).toEqual(false); + }); }); }); diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index 1ac6e3bd28..2912a35771 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -1,5 +1,6 @@ const UserError = require('vn-loopback/util/user-error'); const validateTin = require('vn-loopback/util/validateTin'); +const LoopBackContext = require('loopback-context'); module.exports = Self => { require('../methods/supplier/filter')(Self); @@ -88,8 +89,24 @@ module.exports = Self => { } Self.observe('before save', async function(ctx) { - let changes = ctx.data || ctx.instance; - let orgData = ctx.currentInstance; + const loopbackContext = LoopBackContext.getCurrentContext(); + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; + const userId = loopbackContext.active.accessToken.userId; + + const isNotFinancial = !await Self.app.models.Account.hasRole(userId, 'financial'); + const isPayMethodChecked = changes.isPayMethodChecked || orgData.isPayMethodChecked; + const hasChanges = orgData && changes; + const isPayMethodCheckedChanged = hasChanges + && orgData.isPayMethodChecked != isPayMethodChecked; + + if (isNotFinancial && isPayMethodCheckedChanged) + throw new UserError('You can not modify is pay method checked'); + }); + + Self.observe('before save', async function(ctx) { + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; const socialName = changes.name || orgData.name; const hasChanges = orgData && changes; diff --git a/modules/supplier/front/basic-data/index.html b/modules/supplier/front/basic-data/index.html index 0ec80641da..9991908d4d 100644 --- a/modules/supplier/front/basic-data/index.html +++ b/modules/supplier/front/basic-data/index.html @@ -38,7 +38,8 @@ + ng-model="$ctrl.supplier.isPayMethodChecked" + vn-acl="financial"> diff --git a/modules/supplier/front/billing-data/index.js b/modules/supplier/front/billing-data/index.js index 0c9cbb0dc4..9d2863f64b 100644 --- a/modules/supplier/front/billing-data/index.js +++ b/modules/supplier/front/billing-data/index.js @@ -11,7 +11,8 @@ export default class Controller extends Section { } onSubmit() { - return this.$.watcher.submit(); + this.$.watcher.submit() + .then(() => this.card.reload()); } } @@ -20,5 +21,8 @@ ngModule.vnComponent('vnSupplierBillingData', { controller: Controller, bindings: { supplier: '<' + }, + require: { + card: '^vnSupplierCard' } });