diff --git a/db/changes/230402/00-clienteCompensado.sql b/db/changes/230402/00-clienteCompensado.sql new file mode 100644 index 000000000..ff1982b93 --- /dev/null +++ b/db/changes/230402/00-clienteCompensado.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('Client', 'getClientOrSupplierReference', 'READ', 'ALLOW', 'ROLE', 'employee'); diff --git a/modules/client/back/methods/client/createReceipt.js b/modules/client/back/methods/client/createReceipt.js index a75ee8844..b268aba35 100644 --- a/modules/client/back/methods/client/createReceipt.js +++ b/modules/client/back/methods/client/createReceipt.js @@ -67,7 +67,7 @@ module.exports = function(Self) { try { delete args.ctx; // Remove unwanted properties - const newReceipt = await models.Receipt.create(args, myOptions); + const originalClient = await models.Client.findById(args.clientFk, null, myOptions); const bank = await models.Bank.findById(args.bankFk, null, myOptions); const accountingType = await models.AccountingType.findById(bank.accountingTypeFk, null, myOptions); @@ -76,23 +76,8 @@ module.exports = function(Self) { if (!args.compensationAccount) throw new UserError('Compensation account is empty'); - const supplierCompensation = await models.Supplier.findOne({ - where: { - account: args.compensationAccount - } - }, myOptions); - - let clientCompensation = {}; - - if (!supplierCompensation) { - clientCompensation = await models.Client.findOne({ - where: { - accountingAccount: args.compensationAccount - } - }, myOptions); - } - if (!supplierCompensation && !clientCompensation) - throw new UserError('Invalid account'); + // Check compensation account exists + await models.Client.getClientOrSupplierReference(args.compensationAccount, myOptions); await Self.rawSql( `CALL vn.ledger_doCompensation(?, ?, ?, ?, ?, ?, ?)`, @@ -151,7 +136,7 @@ module.exports = function(Self) { myOptions ); } - + const newReceipt = await models.Receipt.create(args, myOptions); if (tx) await tx.commit(); return newReceipt; diff --git a/modules/client/back/methods/client/getClientOrSupplierReference.js b/modules/client/back/methods/client/getClientOrSupplierReference.js new file mode 100644 index 000000000..afb427315 --- /dev/null +++ b/modules/client/back/methods/client/getClientOrSupplierReference.js @@ -0,0 +1,58 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethod('getClientOrSupplierReference', { + description: 'Returns the reference of a compensation providing a bank account', + accessType: 'READ', + accepts: { + arg: 'bankAccount', + type: 'number', + required: true, + description: 'The bank account of a client or a supplier', + http: {source: 'path'} + }, + returns: { + type: 'string', + root: true + }, + http: { + path: `/:bankAccount/getClientOrSupplierReference`, + verb: 'GET' + } + }); + + Self.getClientOrSupplierReference = async(bankAccount, options) => { + const models = Self.app.models; + const myOptions = {}; + let reference = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const supplierCompensation = await models.Supplier.findOne({ + where: { + account: bankAccount + } + }, myOptions); + + reference.supplierId = supplierCompensation?.id; + reference.supplierName = supplierCompensation?.name; + + let clientCompensation = {}; + + if (!supplierCompensation) { + clientCompensation = await models.Client.findOne({ + where: { + accountingAccount: bankAccount + } + }, myOptions); + reference.clientId = clientCompensation?.id; + reference.clientName = clientCompensation?.name; + } + + if (!supplierCompensation && !clientCompensation) + throw new UserError('Invalid account'); + + return reference; + }; +}; diff --git a/modules/client/back/models/client-methods.js b/modules/client/back/models/client-methods.js index 4b20a822c..9241d80cf 100644 --- a/modules/client/back/models/client-methods.js +++ b/modules/client/back/models/client-methods.js @@ -47,4 +47,5 @@ module.exports = Self => { require('../methods/client/incotermsAuthorizationEmail')(Self); require('../methods/client/consumptionSendQueued')(Self); require('../methods/client/filter')(Self); + require('../methods/client/getClientOrSupplierReference')(Self); }; diff --git a/modules/client/front/balance/create/index.html b/modules/client/front/balance/create/index.html index 56e505463..b76c09356 100644 --- a/modules/client/front/balance/create/index.html +++ b/modules/client/front/balance/create/index.html @@ -11,7 +11,7 @@ @@ -48,6 +48,14 @@ max="$ctrl.maxAmount"> + +
Compensation
+ + +
- -
Compensation
- - -
- @@ -89,4 +89,4 @@ - \ No newline at end of file + diff --git a/modules/client/front/balance/create/index.js b/modules/client/front/balance/create/index.js index ab97a7291..b22d15936 100644 --- a/modules/client/front/balance/create/index.js +++ b/modules/client/front/balance/create/index.js @@ -60,11 +60,15 @@ class Controller extends Dialog { const accountingType = value.accountingType; this.viewReceipt = accountingType.code == 'cash'; - if (accountingType.receiptDescription != null) { - this.receipt.description = accountingType.receiptDescription; - if (this.originalDescription) this.receipt.description += `, ${this.originalDescription}`; - } else if (this.originalDescription) - this.receipt.description = this.originalDescription; + if (accountingType.code == 'compensation') + this.receipt.description = ''; + else { + if (accountingType.receiptDescription != null) { + this.receipt.description = accountingType.receiptDescription; + if (this.originalDescription) this.receipt.description += `, ${this.originalDescription}`; + } else if (this.originalDescription) + this.receipt.description = this.originalDescription; + } this.maxAmount = accountingType && accountingType.maxAmount; this.receipt.payed = new Date(); @@ -110,7 +114,24 @@ class Controller extends Dialog { } accountShortToStandard(value) { - this.receipt.compensationAccount = value.replace('.', '0'.repeat(11 - value.length)); + if (value) { + this.receipt.compensationAccount = value.replace('.', '0'.repeat(11 - value.length)); + this.$http.get(`Clients/${this.receipt.compensationAccount}/getClientOrSupplierReference`) + .then(res => { + if (res.data.clientId) { + this.receipt.description = this.$t('Client Compensation Reference', { + clientId: res.data.clientId, + clientName: res.data.clientName + }); + } else { + this.receipt.description = this.$t('Supplier Compensation Reference', { + supplierId: res.data.supplierId, + supplierName: res.data.supplierName + }); + } + }); + } else + this.receipt.description = ''; } getAmountPaid() { diff --git a/modules/client/front/balance/create/locale/es.yml b/modules/client/front/balance/create/locale/es.yml index 056590966..8c407708a 100644 --- a/modules/client/front/balance/create/locale/es.yml +++ b/modules/client/front/balance/create/locale/es.yml @@ -1,2 +1,4 @@ View receipt: Ver recibo -Amount exceeded: Según ley contra el fraude no se puede recibir cobros por importe igual o superior a {{maxAmount}} \ No newline at end of file +Amount exceeded: Según ley contra el fraude no se puede recibir cobros por importe igual o superior a {{maxAmount}} +Client Compensation Reference: "({{clientId}}) Ntro Cliente: {{clientName}}" +Supplier Compensation Reference: "({{supplierId}}) Ntro Proveedor: {{supplierName}}" diff --git a/print/templates/reports/balance-compensation/balance-compensation.html b/print/templates/reports/balance-compensation/balance-compensation.html index 1e9aa5661..d1a2788ed 100644 --- a/print/templates/reports/balance-compensation/balance-compensation.html +++ b/print/templates/reports/balance-compensation/balance-compensation.html @@ -17,8 +17,8 @@

{{$t('Agree') | uppercase}}

- {{$t('Date')}} {{formatDate(client.payed, '%d-%m-%Y')}} {{$t('Compensate')}} {{client.amountPaid}} € - {{$t('From client')}} {{client.name}} {{$t('Toclient')}} {{company.name}}. + {{$t('Date')}} {{client.payed | date('%d-%m-%Y')}} {{$t('Compensate')}} {{client.amountPaid}} € + {{$t('From client')}} {{client.name}} {{$t('Against the balance of')}}: {{client.invoiceFk}}.

{{$t('Reception')}} administracion@verdnatura.es diff --git a/print/templates/reports/balance-compensation/locale/es.yml b/print/templates/reports/balance-compensation/locale/es.yml index 546e55f06..1c76274ae 100644 --- a/print/templates/reports/balance-compensation/locale/es.yml +++ b/print/templates/reports/balance-compensation/locale/es.yml @@ -4,13 +4,13 @@ Compensation: Compensación de saldos deudores y acreedores In one hand: De una parte CIF: con CIF NIF: con NIF -Home: y domicilio sito en +Home: y domicilio sito en In other hand: De la otra Sr: Don/Doña Agree: Acuerdan Date: En fecha de -Compensate: se ha compensado el saldo de +Compensate: se ha compensado el saldo de From client: del cliente/proveedor -To client: con el cliente/proveedor +Against the balance of: contra el saldo de Reception: Por favor, rogamos confirmen la recepción de esta compensación al email -Greetings: Saludos cordiales, \ No newline at end of file +Greetings: Saludos cordiales, diff --git a/print/templates/reports/balance-compensation/sql/client.sql b/print/templates/reports/balance-compensation/sql/client.sql index b4463be23..c3679b68a 100644 --- a/print/templates/reports/balance-compensation/sql/client.sql +++ b/print/templates/reports/balance-compensation/sql/client.sql @@ -4,6 +4,7 @@ SELECT c.street, c.fi, c.city, + r.invoiceFk, r.amountPaid, r.payed FROM client c