diff --git a/db/changes/10480-june/00-route.sql b/db/changes/10480-june/00-route.sql new file mode 100644 index 000000000..beb7d5e41 --- /dev/null +++ b/db/changes/10480-june/00-route.sql @@ -0,0 +1,10 @@ +UPDATE `vn`.`route` r + JOIN(SELECT r.id, wl.workcenterFk + FROM `vn`.`route` r + JOIN `vn`.`routeLog` rl ON rl.originFk = r.id + JOIN `vn`.`workerLabour` wl ON wl.workerFk = rl.userFk + AND r.created BETWEEN wl.started AND IFNULL(wl.ended, r.created) + WHERE r.created BETWEEN '2021-12-01' AND CURDATE() + AND rl.action = 'insert' + )sub ON sub.id = r.id + SET r.commissionWorkCenterFk = sub.workcenterFk; \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 2cf087957..db60210bd 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2620,3 +2620,7 @@ INSERT INTO `vn`.`sectorCollectionSaleGroup` (`sectorCollectionFk`, `saleGroupFk INSERT INTO `vn`.`workerTimeControlConfig` (`id`, `dayBreak`, `dayBreakDriver`, `shortWeekBreak`, `longWeekBreak`, `weekScope`, `mailPass`, `mailHost`, `mailSuccessFolder`, `mailErrorFolder`, `mailUser`, `minHoursToBreak`, `breakHours`, `hoursCompleteWeek`, `startNightlyHours`, `endNightlyHours`, `maxTimePerDay`, `breakTime`, `timeToBreakTime`, `dayMaxTime`, `shortWeekDays`, `longWeekDays`) VALUES (1, 43200, 32400, 129600, 259200, 604800, '', '', 'Leidos.exito', 'Leidos.error', 'timeControl', 5.33, 0.33, 40, '22:00:00', '06:00:00', 57600, 1200, 18000, 57600, 6, 13); + +INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) + VALUES + (1, 9); diff --git a/db/dump/structure.sql b/db/dump/structure.sql index bb58c1955..da7e77124 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -34349,6 +34349,7 @@ CREATE TABLE `route` ( `priority` int(11) NOT NULL DEFAULT '0', `invoiceInFk` mediumint(8) unsigned DEFAULT NULL, `beachFk` int(11) DEFAULT NULL, + `commissionWorkCenterFk` int(11) DEFAULT NULL COMMENT 'WorkerCenter que gestiona la ruta', PRIMARY KEY (`id`), KEY `Id_Agencia` (`agencyModeFk`), KEY `Fecha` (`created`), @@ -34357,7 +34358,9 @@ CREATE TABLE `route` ( KEY `fk_route_1_idx` (`zoneFk`), KEY `asdfasdf_idx` (`invoiceInFk`), KEY `route_idxIsOk` (`isOk`), + KEY `route_WorkCenterFk_idx` (`commissionWorkCenterFk`), CONSTRAINT `fk_route_1` FOREIGN KEY (`zoneFk`) REFERENCES `zone` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `route_WorkCenterFk` FOREIGN KEY (`commissionWorkCenterFk`) REFERENCES `workCenter` (`id`) ON UPDATE CASCADE, CONSTRAINT `route_fk5` FOREIGN KEY (`agencyModeFk`) REFERENCES `agencyMode` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `route_ibfk_1` FOREIGN KEY (`gestdocFk`) REFERENCES `dms` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `route_ibfk_2` FOREIGN KEY (`workerFk`) REFERENCES `worker` (`id`) ON UPDATE CASCADE, @@ -34514,6 +34517,7 @@ CREATE TABLE `routeConfig` ( `plusCategory1Concept` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `plusCategory2Concept` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL, `defaultCompanyFk` smallint(5) unsigned DEFAULT '442', + `defaultWorkCenterFk` int(11) DEFAULT 9 COMMENT 'Para el cálculo de las comisiones, en caso de el creador de la ruta no tenga workCenter', PRIMARY KEY (`id`), KEY `routeConfig_FK` (`defaultCompanyFk`), CONSTRAINT `routeConfig_FK` FOREIGN KEY (`defaultCompanyFk`) REFERENCES `company` (`id`) diff --git a/modules/route/back/methods/route/specs/updateWorkCenter.spec.js b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js new file mode 100644 index 000000000..5328dc240 --- /dev/null +++ b/modules/route/back/methods/route/specs/updateWorkCenter.spec.js @@ -0,0 +1,51 @@ +const models = require('vn-loopback/server/server').models; + +describe('route updateWorkCenter()', () => { + const routeId = 1; + + it('should set the commission work center if the worker has workCenter', async() => { + const tx = await models.Route.beginTransaction({}); + try { + const developerId = 9; + const ctx = { + req: { + accessToken: {userId: developerId} + } + }; + const options = {transaction: tx}; + + const expectedResult = 1; + const updatedRoute = await models.Route.updateWorkCenter(ctx, routeId, options); + + expect(updatedRoute.commissionWorkCenterFk).toEqual(expectedResult); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it(`shoul set the default commision work center if that worker didn't have one yet`, async() => { + const tx = await models.Route.beginTransaction({}); + try { + const userWithoutWorkCenter = 2; + const ctx = { + req: { + accessToken: {userId: userWithoutWorkCenter} + } + }; + const options = {transaction: tx}; + + const expectedResult = 9; + const updatedRoute = await models.Route.updateWorkCenter(ctx, routeId, options); + + expect(updatedRoute.commissionWorkCenterFk).toEqual(expectedResult); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/route/back/methods/route/updateWorkCenter.js b/modules/route/back/methods/route/updateWorkCenter.js new file mode 100644 index 000000000..7796fba41 --- /dev/null +++ b/modules/route/back/methods/route/updateWorkCenter.js @@ -0,0 +1,54 @@ +module.exports = Self => { + Self.remoteMethodCtx('updateWorkCenter', { + description: 'Update the commission work center through user salix connected', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'number', + description: 'Route Id', + http: {source: 'path'} + }, + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/updateWorkCenter`, + verb: 'POST' + } + }); + + Self.updateWorkCenter = async(ctx, id, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + const userId = ctx.req.accessToken.userId; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const [result] = await Self.rawSql(` + SELECT IFNULL(wl.workCenterFk, r.defaultWorkCenterFk) AS commissionWorkCenter + FROM vn.routeConfig r + LEFT JOIN vn.workerLabour wl ON wl.workerFk = ? + AND CURDATE() BETWEEN wl.started AND IFNULL(wl.ended, CURDATE()); + `, [userId], myOptions); + + const route = await models.Route.findById(id, null, myOptions); + await route.updateAttribute('commissionWorkCenterFk', result.commissionWorkCenter, myOptions); + + if (tx) await tx.commit(); + + return route; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index c82d1722e..4050e62fe 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -9,6 +9,7 @@ module.exports = Self => { require('../methods/route/clone')(Self); require('../methods/route/getSuggestedTickets')(Self); require('../methods/route/unlink')(Self); + require('../methods/route/updateWorkCenter')(Self); Self.validate('kmStart', validateDistance, { message: 'Distance must be lesser than 1000' diff --git a/modules/route/back/models/route.json b/modules/route/back/models/route.json index 46fb6b76f..3b33cc028 100644 --- a/modules/route/back/models/route.json +++ b/modules/route/back/models/route.json @@ -47,6 +47,9 @@ }, "description": { "type": "string" + }, + "commissionWorkCenterFk": { + "type": "number" } }, "relations": { diff --git a/modules/route/front/create/index.js b/modules/route/front/create/index.js index 56c8cc25a..c81394c10 100644 --- a/modules/route/front/create/index.js +++ b/modules/route/front/create/index.js @@ -4,7 +4,12 @@ import Section from 'salix/components/section'; export default class Controller extends Section { onSubmit() { this.$.watcher.submit().then( - res => this.$state.go('route.card.summary', {id: res.data.id}) + res => { + this.$http.post(`Routes/${res.data.id}/updateWorkCenter`, null) + .then(() => { + this.$state.go('route.card.summary', {id: res.data.id}); + }); + } ); } }