diff --git a/loopback/locale/es.json b/loopback/locale/es.json index dc662e8d3..cff5f652e 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -102,7 +102,7 @@ "Weekday cannot be blank": "El día de la semana no puede quedar en blanco", "You can't delete a confirmed order": "No puedes borrar un pedido confirmado", "Can't create stowaway for this ticket": "No se puede crear un polizon para este ticket", - "The socialName has an invalid format": "El nombre fiscal tiene un formato incorrecto", + "The social name has an invalid format": "El nombre fiscal tiene un formato incorrecto", "Invalid quantity": "Cantidad invalida", "This postal code is not valid": "This postal code is not valid", "is invalid": "is invalid", diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 7ab8bb477..3dc1e1f8d 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -55,15 +55,6 @@ module.exports = Self => { with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/ }); - Self.validate('businessTypeFk', hasBusinessType, { - message: `The type of business must be filled in basic data` - }); - - function hasBusinessType(err) { - if (!this.businessTypeFk) - err(); - } - Self.validatesLengthOf('postcode', { allowNull: true, allowBlank: true, @@ -189,6 +180,32 @@ module.exports = Self => { return regexp.test(value); } + Self.observe('before save', async ctx => { + const changes = ctx.data || ctx.instance; + const orgData = ctx.currentInstance; + + const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk; + const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked; + + let invalidBusinessType = false; + if (!ctx.isNewInstance) { + const isWorker = await Self.app.models.UserAccount.findById(orgData.id); + const changedFields = Object.keys(changes); + const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk'); + + if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields) + invalidBusinessType = true; + } + + if (ctx.isNewInstance) { + if (!businessTypeFk && !isTaxDataChecked) + invalidBusinessType = true; + } + + if (invalidBusinessType) + throw new UserError(`The type of business must be filled in basic data`); + }); + Self.observe('before save', async function(ctx) { const changes = ctx.data || ctx.instance; const orgData = ctx.currentInstance; @@ -206,7 +223,7 @@ module.exports = Self => { && orgData.isTaxDataChecked != isTaxDataChecked; if ((socialNameChanged || dataCheckedChanged) && !isAlpha(socialName)) - throw new UserError('The socialName has an invalid format'); + throw new UserError(`The social name has an invalid format`); if (changes.salesPerson === null) { changes.credit = 0; @@ -374,14 +391,11 @@ module.exports = Self => { throw new UserError(`You don't have enough privileges to set this credit amount`); } - const client = await models.Client.findById(finalState.id, null, ctx.options); - if (client.businessTypeFk) { - await models.ClientCredit.create({ - amount: changes.credit, - clientFk: finalState.id, - workerFk: userId - }, ctx.options); - } + await models.ClientCredit.create({ + amount: changes.credit, + clientFk: finalState.id, + workerFk: userId + }, ctx.options); }; const app = require('vn-loopback/server/server'); diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index 2912a3577..f8b096b30 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -114,6 +114,6 @@ module.exports = Self => { && orgData.socialName != socialName; if ((socialNameChanged) && !isAlpha(socialName)) - throw new UserError('The socialName has an invalid format'); + throw new UserError('The social name has an invalid format'); }); };