diff --git a/back/models/dms.json b/back/models/dms.json index d5ec4b8b73..dc421a7a12 100644 --- a/back/models/dms.json +++ b/back/models/dms.json @@ -1,3 +1,4 @@ + { "name": "Dms", "description": "Documental Managment system", @@ -22,6 +23,12 @@ "description": { "type": "string" }, + "hardCopyNumber": { + "type": "Number" + }, + "hasFile": { + "type": "boolean" + }, "created": { "type": "Date" } diff --git a/back/models/dmsType.json b/back/models/dmsType.json index 9efc92cf52..b51c810acd 100644 --- a/back/models/dmsType.json +++ b/back/models/dmsType.json @@ -20,6 +20,10 @@ "path": { "type": "string", "required": true + }, + "code": { + "type": "string", + "required": true } }, "relations": { diff --git a/db/changes/10004-mother/00-acl.sql b/db/changes/10040-mother/00-acl.sql similarity index 100% rename from db/changes/10004-mother/00-acl.sql rename to db/changes/10040-mother/00-acl.sql diff --git a/db/changes/10004-mother/00-clest.sql b/db/changes/10040-mother/00-clest.sql similarity index 100% rename from db/changes/10004-mother/00-clest.sql rename to db/changes/10040-mother/00-clest.sql diff --git a/db/changes/10004-mother/00-clientDms.sql b/db/changes/10040-mother/00-clientDms.sql similarity index 100% rename from db/changes/10004-mother/00-clientDms.sql rename to db/changes/10040-mother/00-clientDms.sql diff --git a/db/changes/10004-mother/00-dmsView.sql b/db/changes/10040-mother/00-dmsView.sql similarity index 100% rename from db/changes/10004-mother/00-dmsView.sql rename to db/changes/10040-mother/00-dmsView.sql diff --git a/db/changes/10004-mother/00-mandateType.sql b/db/changes/10040-mother/00-mandateType.sql similarity index 100% rename from db/changes/10004-mother/00-mandateType.sql rename to db/changes/10040-mother/00-mandateType.sql diff --git a/db/changes/10004-mother/00-sample.sql b/db/changes/10040-mother/00-sample.sql similarity index 100% rename from db/changes/10004-mother/00-sample.sql rename to db/changes/10040-mother/00-sample.sql diff --git a/db/changes/10004-mother/00-tarifaComponentes.sql b/db/changes/10040-mother/00-tarifaComponentes.sql similarity index 100% rename from db/changes/10004-mother/00-tarifaComponentes.sql rename to db/changes/10040-mother/00-tarifaComponentes.sql diff --git a/db/changes/10004-mother/00-ticket.sql b/db/changes/10040-mother/00-ticket.sql similarity index 100% rename from db/changes/10004-mother/00-ticket.sql rename to db/changes/10040-mother/00-ticket.sql diff --git a/db/changes/10004-mother/01-claimState.sql b/db/changes/10040-mother/01-claimState.sql similarity index 100% rename from db/changes/10004-mother/01-claimState.sql rename to db/changes/10040-mother/01-claimState.sql diff --git a/db/changes/10004-mother/01-componentRate.sql b/db/changes/10040-mother/01-componentRate.sql similarity index 100% rename from db/changes/10004-mother/01-componentRate.sql rename to db/changes/10040-mother/01-componentRate.sql diff --git a/db/changes/10004-mother/01-viewTicket.sql b/db/changes/10040-mother/01-viewTicket.sql similarity index 100% rename from db/changes/10004-mother/01-viewTicket.sql rename to db/changes/10040-mother/01-viewTicket.sql diff --git a/db/changes/10050-pentecostes/00-ACL.sql b/db/changes/10050-pentecostes/00-ACL.sql new file mode 100644 index 0000000000..d578266051 --- /dev/null +++ b/db/changes/10050-pentecostes/00-ACL.sql @@ -0,0 +1,2 @@ +INSERT INTO `salix`.`ACL` ( `model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ( 'ClientDms', 'remove', 'WRITE', 'ALLOW', 'ROLE', 'employee'); +INSERT INTO `salix`.`ACL` ( `model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ( 'ClientDms', '*', 'READ', 'ALLOW', 'ROLE', 'employee'); diff --git a/db/changes/10050-pentecostes/00-clientesgestdoc.sql b/db/changes/10050-pentecostes/00-clientesgestdoc.sql new file mode 100644 index 0000000000..3e7a023c91 --- /dev/null +++ b/db/changes/10050-pentecostes/00-clientesgestdoc.sql @@ -0,0 +1,19 @@ +ALTER TABLE `vn2008`.`clientes_gestdoc` +DROP FOREIGN KEY `clientes_gestdoc_ibfk_3`; +ALTER TABLE `vn2008`.`clientes_gestdoc` +DROP PRIMARY KEY, +ADD PRIMARY KEY (`gest_doc_id`); + + +ALTER TABLE `vn2008`.`clientes_gestdoc` +DROP INDEX `fk_clientes_gestdoc_1_idx` ; + +ALTER TABLE `vn2008`.`clientes_gestdoc` +ADD INDEX `fk_clientes_gestdoc_1_idx` (`Id_Cliente` ASC); + +ALTER TABLE `vn2008`.`clientes_gestdoc` +ADD CONSTRAINT `fk_clientes_gestdoc_3` + FOREIGN KEY (`Id_Cliente`) + REFERENCES `vn2008`.`Clientes` (`id_cliente`) + ON DELETE RESTRICT + ON UPDATE CASCADE; diff --git a/db/changes/10050-pentecostes/01-clienteesGestdoc.sql b/db/changes/10050-pentecostes/01-clienteesGestdoc.sql new file mode 100644 index 0000000000..294a39577f --- /dev/null +++ b/db/changes/10050-pentecostes/01-clienteesGestdoc.sql @@ -0,0 +1,9 @@ +ALTER TABLE `vn2008`.`clientes_gestdoc` +ADD INDEX `fk_clientes_gestdoc_1_idx` (`Id_Cliente` ASC); + +ALTER TABLE `vn2008`.`clientes_gestdoc` +ADD CONSTRAINT `fk_clientes_gestdoc_3` + FOREIGN KEY (`Id_Cliente`) + REFERENCES `vn2008`.`Clientes` (`id_cliente`) + ON DELETE RESTRICT + ON UPDATE CASCADE; \ No newline at end of file diff --git a/db/changes/10005-pentecostes/01-gesttipData.sql b/db/changes/10050-pentecostes/01-gesttipData.sql similarity index 100% rename from db/changes/10005-pentecostes/01-gesttipData.sql rename to db/changes/10050-pentecostes/01-gesttipData.sql diff --git a/db/changes/10005-pentecostes/01-tarifaComponentesData.sql b/db/changes/10050-pentecostes/01-tarifaComponentesData.sql similarity index 100% rename from db/changes/10005-pentecostes/01-tarifaComponentesData.sql rename to db/changes/10050-pentecostes/01-tarifaComponentesData.sql diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 47202d7df1..b1cc35f63c 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -654,7 +654,7 @@ INSERT INTO `vn`.`sale`(`id`, `itemFk`, `ticketFk`, `concept`, `quantity`, `pric (11, 1, 16, 'Ranged weapon longbow 2m', 2, 9.10, 0, 0, 0, CURDATE()), (12, 4, 16, 'Melee weapon heavy shield 1x0.5m', 20, 3.06, 0, 0, 0, CURDATE()), (13, 2, 8, 'Melee weapon combat fist 15cm', 15, 1.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)), - (14, 1, 8, 'Ranged weapon longbow 2m', 10, 2.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)), + (14, 1, 8, 'Ranged weapon longbow 2m', 10, 9.28, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)), (15, 1, 19, 'Ranged weapon longbow 2m', 10, 1.50, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 DAY)), (16, 2, 20, 'Melee weapon combat fist 15cm', 15, 1.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 DAY)), (17, 2, 22, 'Melee weapon combat fist 15cm', 30, 2.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 DAY)), @@ -1457,8 +1457,9 @@ INSERT INTO `vn`.`dmsType`(`id`, `name`, `path`, `readRoleFk`, `writeRoleFk`, `c INSERT INTO `vn`.`dms`(`id`, `dmsTypeFk`, `file`, `workerFk`, `warehouseFk`, `companyFk`, `hardCopyNumber`, `hasFile`, `reference`, `description`, `created`) VALUES - (1, 14, '1.pdf', 5, 1, 442, NULL, FALSE, 'Ticket:11', 'Ticket:11 dms for the ticket', CURDATE()), - (2, 5, '2.pdf', 5, 1, 442, 1, TRUE, 'Client:101', 'Client:101 dms for the client', CURDATE()); + (1, 14, '1.pdf', 5, 1, 442, NULL, FALSE, 'Ticket:11', 'Ticket:11 dms for the ticket', CURDATE()), + (2, 5, '2.pdf', 5, 1, 442, 1, TRUE, 'Client:101', 'Client:101 dms for the client', CURDATE()), + (3, 5, '3.pdf', 5, 1, 442, NULL, TRUE, 'Client: 101', 'Client:101 readme', CURDATE()); INSERT INTO `vn`.`ticketDms`(`ticketFk`, `dmsFk`) VALUES @@ -1466,4 +1467,5 @@ INSERT INTO `vn`.`ticketDms`(`ticketFk`, `dmsFk`) INSERT INTO `vn`.`clientDms`(`clientFk`, `dmsFk`) VALUES - (101, 2); \ No newline at end of file + (101, 2), + (101, 3); \ No newline at end of file diff --git a/e2e/paths/06-claim-module/03_detail.spec.js b/e2e/paths/06-claim-module/03_detail.spec.js index 9eb3fee1cf..551701c78a 100644 --- a/e2e/paths/06-claim-module/03_detail.spec.js +++ b/e2e/paths/06-claim-module/03_detail.spec.js @@ -45,7 +45,7 @@ describe('Claim detail', () => { .waitToGetProperty(selectors.claimDetail.totalClaimed, 'innerText'); expect(claimedQuantity).toEqual('10'); - expect(totalClaimed).toContain('29.50'); + expect(totalClaimed).toContain('99.30'); }); it('should delete the second item from the claim', async() => { diff --git a/modules/agency/back/methods/zone/clone.js b/modules/agency/back/methods/zone/clone.js index 0fb8b5ad4d..0f406c49ab 100644 --- a/modules/agency/back/methods/zone/clone.js +++ b/modules/agency/back/methods/zone/clone.js @@ -22,6 +22,7 @@ module.exports = Self => { Self.clone = async id => { const models = Self.app.models; const transaction = await Self.beginTransaction({}); + const options = {transaction}; // Find original zone const zone = await models.Zone.findOne({ @@ -35,22 +36,36 @@ module.exports = Self => { 'bonus', 'isVolumetric'], where: {id} - }); + }, options); + + const hour = zone.hour; + const offset = hour.getTimezoneOffset() * 60000; + hour.setTime(hour.getTime() + offset); // Find all original included geolocations const includedGeo = await models.ZoneIncluded.find({ fields: ['geoFk', 'isIncluded'], where: {zoneFk: id} - }); + }, options); + + // Find all original selected days + const calendarDays = await models.ZoneCalendar.find({ + where: {zoneFk: id} + }, options); try { - const newZone = await Self.create(zone, {transaction}); + const newZone = await Self.create(zone, options); const newIncludedGeo = includedGeo.map(included => { included.zoneFk = newZone.id; return included; }); + const newCalendayDays = calendarDays.map(day => { + day.zoneFk = newZone.id; + return day; + }); - await models.ZoneIncluded.create(newIncludedGeo, {transaction}); + await models.ZoneIncluded.create(newIncludedGeo, options); + await models.ZoneCalendar.create(newCalendayDays, options); await transaction.commit(); return newZone; diff --git a/modules/agency/front/descriptor/index.html b/modules/agency/front/descriptor/index.html index 5abfab4cf7..7f122bcf6c 100644 --- a/modules/agency/front/descriptor/index.html +++ b/modules/agency/front/descriptor/index.html @@ -48,6 +48,6 @@ + question="Are you sure you want to delete this zone?" + message="This zone will be removed"> \ No newline at end of file diff --git a/modules/agency/front/index/locale/es.yml b/modules/agency/front/index/locale/es.yml index c0a97c819f..14195e8692 100644 --- a/modules/agency/front/index/locale/es.yml +++ b/modules/agency/front/index/locale/es.yml @@ -1,2 +1,2 @@ -Do you want to clone this zone?: ¿Seguro que quieres eliminar esta zona? +Do you want to clone this zone?: ¿Seguro que quieres clonar esta zona? All it's properties will be copied: Todas sus propiedades serán copiadas \ No newline at end of file diff --git a/modules/agency/front/locale/es.yml b/modules/agency/front/locale/es.yml index d34ab1f70e..ef226ef107 100644 --- a/modules/agency/front/locale/es.yml +++ b/modules/agency/front/locale/es.yml @@ -4,8 +4,8 @@ Hour: Hora (ETD) ETD: Tiempo de salida estimado Price: Precio Locations: Localizaciones -Delete zone: Eliminar zona -Are you sure you want to delete this zone?: ¿Estás seguro de querer eliminar esta zona? +This zone will be removed: La zona será eliminada +Are you sure you want to delete this zone?: ¿Seguro de que quieres eliminar esta zona? Zones: Zonas New zone: Nueva zona Volumetric: Volumétrico diff --git a/modules/agency/front/summary/index.html b/modules/agency/front/summary/index.html index e37ba365e3..54adc3586c 100644 --- a/modules/agency/front/summary/index.html +++ b/modules/agency/front/summary/index.html @@ -26,7 +26,7 @@ value="{{$ctrl.summary.price | currency: 'EUR': 2}}"> + value="{{$ctrl.summary.bonus | currency: 'EUR': 2}}"> { } }); - Self.createWithInsurance = async (data, ctx) => { + Self.createWithInsurance = async(data, ctx) => { let transaction = await Self.beginTransaction({}); try { diff --git a/modules/client/back/methods/dms/removes.js b/modules/client/back/methods/dms/removes.js new file mode 100644 index 0000000000..c3aeeed525 --- /dev/null +++ b/modules/client/back/methods/dms/removes.js @@ -0,0 +1,35 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethod('removes', { + description: 'Delete an client dms', + accessType: 'WRITE', + accepts: { + arg: 'dmsId', + type: 'number', + required: true, + description: 'dms identifier', + }, + returns: { + type: 'string', + root: true + }, + http: { + path: `/removes`, + verb: 'POST' + } + }); + + Self.removes = async dmsId => { + if (!dmsId) + throw new UserError('There is nothing to delete'); + + let targetClientDms = await Self.app.models.ClientDms.findOne({where: {dmsFk: dmsId}}); + let targetDms = await Self.app.models.Dms.findById(dmsId); + let trashDmsType = await Self.app.models.DmsType.findOne({where: {code: 'trash'}}); + + await targetClientDms.destroy(); + await targetDms.updateAttribute('dmsTypeFk', trashDmsType.id); + }; +}; + diff --git a/modules/client/back/methods/dms/removes.spec.js b/modules/client/back/methods/dms/removes.spec.js new file mode 100644 index 0000000000..a5d653f7ee --- /dev/null +++ b/modules/client/back/methods/dms/removes.spec.js @@ -0,0 +1,29 @@ +const app = require('vn-loopback/server/server'); + +describe('Client dms stuff', () => { + let dmsToRestore; + let dmsTypeToRestore; + afterAll(async done => { + await app.models.ClientDms.create(dmsToRestore); + await dmsTypeToRestore.save(); + + done(); + }); + + + it('should delete a dms from a client and update the dmsType to trash', async() => { + const dmsId = 2; + + dmsToRestore = await app.models.ClientDms.findOne({where: {dmsFk: dmsId}}); + dmsTypeToRestore = await app.models.Dms.findById(dmsToRestore.dmsFk); + + await app.models.ClientDms.removes(dmsId); + + let destroyedDms = await app.models.ClientDms.findOne({where: {dmsFk: dmsId}}); + + let alteredDmsType = await app.models.Dms.findById(dmsId); + + expect(destroyedDms).toBeNull(); + expect(alteredDmsType.dmsTypeFk).toEqual(13); + }); +}); diff --git a/modules/client/back/models/client-dms.js b/modules/client/back/models/client-dms.js new file mode 100644 index 0000000000..ef74b3c393 --- /dev/null +++ b/modules/client/back/models/client-dms.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/dms/removes')(Self); +}; diff --git a/modules/client/back/models/client-dms.json b/modules/client/back/models/client-dms.json index 31fc830060..c919c22233 100644 --- a/modules/client/back/models/client-dms.json +++ b/modules/client/back/models/client-dms.json @@ -1,25 +1,24 @@ { "name": "ClientDms", "base": "Loggable", - "log": { - "model": "ClientLog", - "relation": "ticket", + "log": { + "model":"ClientLog", + "relation": "client", "showField": "dmsFk" - }, + }, "options": { "mysql": { "table": "clientDms" } }, "properties": { - "clientFk": { + "dmsFk": { "type": "Number", "id": true, "required": true }, - "dmsFk": { + "clientFk": { "type": "Number", - "id": true, "required": true } }, @@ -35,4 +34,5 @@ "foreignKey": "dmsFk" } } -} \ No newline at end of file +} + diff --git a/modules/client/front/dms/index.html b/modules/client/front/dms/index.html new file mode 100644 index 0000000000..df8bcfcb6a --- /dev/null +++ b/modules/client/front/dms/index.html @@ -0,0 +1,74 @@ + + + + + + + + + File + Description + Reference + Hard copy + Worker + Created + + + + + + + {{::document.dmsFk}} + + {{::document.dms.description}} + + {{::document.dms.reference}} + {{::document.dms.hardCopyNumber | dashIfEmpty}} + + + {{::document.dms.worker.user.nickname | dashIfEmpty}} + + + {{::document.dms.created | dateTime:'dd/MM/yyyy HH:mm'}} + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/modules/client/front/dms/index.js b/modules/client/front/dms/index.js new file mode 100644 index 0000000000..9a3c274b75 --- /dev/null +++ b/modules/client/front/dms/index.js @@ -0,0 +1,71 @@ +import ngModule from '../module'; +import './style.scss'; + +class Controller { + constructor($stateParams, $scope, vnToken, $http, vnApp, $translate) { + this.$stateParams = $stateParams; + this.$ = $scope; + this.accessToken = vnToken.token; + this.$http = $http; + this.vnApp = vnApp; + this.$translate = $translate; + this.filter = { + include: { + relation: 'dms', + scope: { + fields: ['dmsTypeFk', 'workerFk', 'file', 'created', 'description', 'reference', 'hardCopyNumber'], + include: [{ + relation: 'dmsType', + scope: { + fields: ['name'] + } + }, + { + relation: 'worker', + scope: { + fields: ['userFk'], + include: { + relation: 'user', + scope: { + fields: ['nickname'] + } + }, + } + }] + }, + } + }; + } + + showWorkerDescriptor(event, workerFk) { + event.preventDefault(); + event.stopImmediatePropagation(); + this.$.workerDescriptor.parent = event.target; + this.$.workerDescriptor.workerFk = workerFk; + this.$.workerDescriptor.show(); + } + + showDeleteConfirm(dmsId) { + this.selectedDms = dmsId; + this.$.confirm.show(); + } + + deleteDms(response) { + if (response === 'ACCEPT') { + let dmsId = this.selectedDms; + let query = `/client/api/ClientDms/removes`; + + this.$http.post(query, {dmsId}).then(() => { + this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + this.$.model.refresh(); + }); + } + } +} + +Controller.$inject = ['$stateParams', '$scope', 'vnToken', '$http', 'vnApp', '$translate']; + +ngModule.component('vnClientDms', { + template: require('./index.html'), + controller: Controller, +}); diff --git a/modules/client/front/dms/locale/es.yml b/modules/client/front/dms/locale/es.yml new file mode 100644 index 0000000000..0994c7d868 --- /dev/null +++ b/modules/client/front/dms/locale/es.yml @@ -0,0 +1,9 @@ +Type: Tipo +File management: Gestión documental +File: Fichero +Hard copy: Copia +This file will be deleted: Este fichero va a ser borrado +Are you sure?: Estas seguro? +File deleted: Fichero eliminado +Remove file: Eliminar fichero +Download file: Descargar fichero \ No newline at end of file diff --git a/modules/client/front/dms/style.scss b/modules/client/front/dms/style.scss new file mode 100644 index 0000000000..a6758e2e61 --- /dev/null +++ b/modules/client/front/dms/style.scss @@ -0,0 +1,6 @@ +vn-client-risk-index { + .totalBox { + display: table; + float: right; + } +} \ No newline at end of file diff --git a/modules/client/front/index.js b/modules/client/front/index.js index d6a910c869..b124aab7bf 100644 --- a/modules/client/front/index.js +++ b/modules/client/front/index.js @@ -35,4 +35,5 @@ import './sample/create'; import './web-payment'; import './log'; import './sms'; +import './dms'; diff --git a/modules/client/front/routes.json b/modules/client/front/routes.json index a927d0aa73..8f01f57710 100644 --- a/modules/client/front/routes.json +++ b/modules/client/front/routes.json @@ -24,7 +24,8 @@ {"state": "client.card.creditInsurance.index", "icon": "icon-solunion"}, {"state": "client.card.contact", "icon": "contact_phone"}, {"state": "client.card.sample.index", "icon": "mail"}, - {"state": "client.card.webPayment", "icon": "icon-onlinepayment"} + {"state": "client.card.webPayment", "icon": "icon-onlinepayment"}, + {"state": "client.card.dms", "icon": "cloud_download"} ] } ], @@ -314,6 +315,12 @@ "state": "client.card.log", "component": "vn-client-log", "description": "Log" + }, + { + "url": "/dms", + "state": "client.card.dms", + "component": "vn-client-dms", + "description": "File management" } ] } diff --git a/modules/ticket/back/methods/sale/updateDiscount.js b/modules/ticket/back/methods/sale/updateDiscount.js index 6411820768..b5811085cf 100644 --- a/modules/ticket/back/methods/sale/updateDiscount.js +++ b/modules/ticket/back/methods/sale/updateDiscount.js @@ -1,15 +1,19 @@ let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('updateDiscount', { + Self.remoteMethod('updateDiscount', { description: 'Changes the discount of a sale', accessType: 'WRITE', accepts: [{ - arg: 'params', - type: 'object', + arg: 'ticketFk', + type: 'number', required: true, - description: 'sale ID, newDiscount, price', - http: {source: 'body'} + description: 'ticket id', + }, { + arg: 'sales', + type: ['object'], + required: true, + description: 'sale ID, newDiscount, price, ticket', }], returns: { type: 'string', @@ -21,12 +25,16 @@ module.exports = Self => { } }); - Self.updateDiscount = async(ctx, params) => { - if (isNaN(params.editLines[0].discount)) + Self.updateDiscount = async(ticketFk, sales) => { + const validDiscounts = sales.every(sale => { + return !isNaN(sale.discount); + }); + + if (!validDiscounts) throw new UserError(`The value should be a number`); let model = Self.app.models; - let ticket = await model.Ticket.findById(params.editLines[0].ticketFk, { + let ticket = await model.Ticket.findById(ticketFk, { include: { relation: 'client', scope: { @@ -38,25 +46,27 @@ module.exports = Self => { if (ticket.refFk) throw new UserError(`The sales of this ticket can't be modified`); - let componentToUse; + let manaDiscount; + let buyerDiscount = await model.ComponentRate.findOne({where: {code: 'buyerDiscount'}}); let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket.client().salesPersonFk}, fields: 'amount'}); + let componentId = buyerDiscount.id; + if (usesMana) { + manaDiscount = await model.ComponentRate.findOne({where: {code: 'mana'}}); + componentId = manaDiscount.id; + } - if (usesMana) - componentToUse = 37; - else - componentToUse = 34; + for (let i = 0; i < sales.length; i++) { + let currentLine = await model.Sale.findOne({where: {id: sales[i].id}, fields: 'price'}); + let value = ((-currentLine.price * sales[i].discount) / 100); - for (let i = 0; i < params.editLines.length; i++) { - let currentLine = await model.Sale.findOne({where: {id: params.editLines[i].id}, fields: 'price'}); - let value = ((-currentLine.price * params.editLines[i].discount) / 100); - await model.SaleComponent.upsert({saleFk: params.editLines[i].id, value: value, componentFk: componentToUse}); + await model.SaleComponent.upsert({saleFk: sales[i].id, value: value, componentFk: componentId}); - await model.Sale.update({id: params.editLines[i].id}, {discount: params.editLines[i].discount}); + await model.Sale.update({id: sales[i].id}, {discount: sales[i].discount}); } if (usesMana) { - query = ` - call vn.manaSpellersRequery(?)`; + query = `call vn.manaSpellersRequery(?)`; + await Self.rawSql(query, [ticket.client().salesPersonFk]); } }; diff --git a/modules/ticket/back/methods/sale/updatePrice.js b/modules/ticket/back/methods/sale/updatePrice.js index aa4df9d289..246a9a4e56 100644 --- a/modules/ticket/back/methods/sale/updatePrice.js +++ b/modules/ticket/back/methods/sale/updatePrice.js @@ -1,14 +1,14 @@ let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('updatePrice', { + Self.remoteMethod('updatePrice', { description: 'Changes the discount of a sale', accessType: 'WRITE', accepts: [{ arg: 'params', type: 'object', required: true, - description: 'sale ID, newPrice', + description: 'sale ID, newPrice, ticketFk', http: {source: 'body'} }], returns: { @@ -21,10 +21,51 @@ module.exports = Self => { } }); - Self.getTicket = async params => { + Self.updatePrice = async params => { + if (!params.price) params.price = 0; + + let model = Self.app.models; + let manaDiscount; + let buyerDiscount = await model.ComponentRate.findOne({where: {code: 'buyerDiscount'}}); + let ticket = await getTicket(params); + let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'}); + let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}}); + let componentId = buyerDiscount.id; + + if (usesMana) { + manaDiscount = await model.ComponentRate.findOne({where: {code: 'mana'}}); + componentId = manaDiscount.id; + } + let value = (params.price - currentLine.price); + + let saleComponent = await Self.app.models.SaleComponent.findOne({ + where: { + componentFk: componentId, + saleFk: params.id + } + }); + + if (saleComponent) { + saleComponent.updateAttributes({value: saleComponent.value + value}).catch(() => { + throw new UserError(`Enter a valid number`); + }); + } else { + await Self.app.models.SaleComponent.create({ + saleFk: params.id, + componentFk: componentId, + value: value + }); + } + + await currentLine.updateAttributes({price: params.price}); + + query = `call vn.manaSpellersRequery(?)`; + await Self.rawSql(query, [ticket[0].client().salesPersonFk]); + }; + + async function getTicket(params) { let model = Self.app.models; let thisTicketIsEditable = await model.Ticket.isEditable(params.ticketFk); - if (!thisTicketIsEditable) throw new UserError(`The sales of this ticket can't be modified`); @@ -40,48 +81,5 @@ module.exports = Self => { }], fields: ['id', 'clientFk'] }); - }; - - Self.updatePrice = async params => { - if (isNaN(params.price)) - throw new UserError(`The value should be a number`); - - if (!params.price) params.price = 0; - - let model = Self.app.models; - - let ticket = await Self.getTicket(params); - let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'}); - let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}}); - - let componentToUse; - if (usesMana) - componentToUse = 37; - else - componentToUse = 34; - - let value = (params.price - currentLine.price); - - let saleComponent = await Self.app.models.SaleComponent.findOne({ - where: { - componentFk: componentToUse, - saleFk: params.id - } - }); - - if (saleComponent) - saleComponent.updateAttributes({value: saleComponent.value + value}); - else { - await Self.app.models.SaleComponent.create({ - saleFk: params.id, - componentFk: componentToUse, - value: value - }); - } - - await currentLine.updateAttributes({price: params.price}); - - query = `call vn.manaSpellersRequery(?)`; - await Self.rawSql(query, [ticket[0].client().salesPersonFk]); - }; + } }; diff --git a/modules/ticket/back/models/component-rate.json b/modules/ticket/back/models/component-rate.json index 6e796cbb20..3a69c6eb6c 100644 --- a/modules/ticket/back/models/component-rate.json +++ b/modules/ticket/back/models/component-rate.json @@ -26,6 +26,9 @@ }, "componentTypeRate": { "type": "Number" + }, + "code": { + "type": "String" } }, "relations": { diff --git a/modules/ticket/front/sale/editDiscount.html b/modules/ticket/front/sale/editDiscount.html index cdc5f0839d..7648d2ddcb 100644 --- a/modules/ticket/front/sale/editDiscount.html +++ b/modules/ticket/front/sale/editDiscount.html @@ -2,16 +2,15 @@
MANÁ: {{$ctrl.mana | currency: 'EUR':0}}
- % - +

New price

{{($ctrl.edit[0].quantity * $ctrl.edit[0].price) diff --git a/modules/ticket/front/sale/editDiscount.js b/modules/ticket/front/sale/editDiscount.js index 3df684ff3d..0694c67d52 100644 --- a/modules/ticket/front/sale/editDiscount.js +++ b/modules/ticket/front/sale/editDiscount.js @@ -38,12 +38,15 @@ class Controller { let modified = false; for (let i = 0; i < this.edit.length; i++) { if (this.newDiscount != this.edit[0].discount || this.bulk || !this.newDiscount) { - editLines.push({id: this.edit[i].id, discount: this.newDiscount, ticketFk: this.$state.params.id}); + editLines.push({id: this.edit[i].id, discount: this.newDiscount}); modified = true; } } + if (modified) { - this.$http.post(`/ticket/api/Sales/updateDiscount`, {editLines}).then(() => { + const ticketId = parseInt(this.$state.params.id); + const params = {ticketFk: ticketId, sales: editLines}; + this.$http.post(`/ticket/api/Sales/updateDiscount`, params).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.clear(); modified = false; @@ -53,7 +56,7 @@ class Controller { this.onHide(); } else - this.vnApp.showError(this.$translate.instant('There is no changes to save')); + this.vnApp.showError(this.$translate.instant('There are no changes to save')); } clear() { diff --git a/modules/ticket/front/sale/editDiscount.spec.js b/modules/ticket/front/sale/editDiscount.spec.js index 2fef13106c..e48fdbb630 100644 --- a/modules/ticket/front/sale/editDiscount.spec.js +++ b/modules/ticket/front/sale/editDiscount.spec.js @@ -69,7 +69,7 @@ describe('Ticket', () => { spyOn(controller.vnApp, 'showError'); controller.updateDiscount(); - expect(controller.vnApp.showError).toHaveBeenCalledWith('There is no changes to save'); + expect(controller.vnApp.showError).toHaveBeenCalledWith('There are no changes to save'); }); }); diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index 19b5414f85..b15d04090e 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -173,16 +173,17 @@

MANÁ: {{$ctrl.mana | currency: 'EUR':0}}
- - +

New price

{{($ctrl.sale.quantity * $ctrl.editedPrice)