diff --git a/back/methods/collection/getSales.js b/back/methods/collection/getSales.js index a9e5f2e60..f78804840 100644 --- a/back/methods/collection/getSales.js +++ b/back/methods/collection/getSales.js @@ -64,11 +64,11 @@ module.exports = Self => { let observations = ticket.observaciones.split(' '); for (let observation of observations) { - const salesPerson = ticket.salesPersonFk; + const salesDepartment = ticket.salesDepartmentFk; if (observation.startsWith('#') || observation.startsWith('@')) { await models.Chat.send(ctx, observation, - $t('ticketCommercial', {ticket: ticket.ticketFk, salesPerson}) + $t('ticketCommercial', {ticket: ticket.ticketFk, salesDepartment}) ); } } diff --git a/back/methods/collection/getTickets.js b/back/methods/collection/getTickets.js index 663b70e94..4bc761965 100644 --- a/back/methods/collection/getTickets.js +++ b/back/methods/collection/getTickets.js @@ -132,7 +132,7 @@ module.exports = Self => { $t('The ticket is in preparation', { ticketId: ticketId, ticketUrl: `${url}ticket/${ticketId}/summary`, - salesPersonId: ticket.salesPersonFk + salesDepartmentId: ticket.salesDepartmentFk }))); } } diff --git a/db/dump/.dump/data.sql b/db/dump/.dump/data.sql index 43f686022..1f3b0a51a 100644 --- a/db/dump/.dump/data.sql +++ b/db/dump/.dump/data.sql @@ -1314,7 +1314,7 @@ INSERT INTO `ACL` VALUES (73,'Expedition','*','READ','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (74,'Expedition','*','WRITE','ALLOW','ROLE','deliveryAssistant'); INSERT INTO `ACL` VALUES (75,'Expedition','*','WRITE','ALLOW','ROLE','production'); INSERT INTO `ACL` VALUES (76,'AnnualAverageInvoiced','*','READ','ALLOW','ROLE','employee'); -INSERT INTO `ACL` VALUES (77,'WorkerMana','*','READ','ALLOW','ROLE','employee'); +INSERT INTO `ACL` VALUES (77,'DepartmentMana','*','READ','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (78,'TicketTracking','*','WRITE','ALLOW','ROLE','production'); INSERT INTO `ACL` VALUES (79,'Ticket','state','*','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (80,'Sale','deleteSales','*','ALLOW','ROLE','employee'); @@ -1768,7 +1768,7 @@ INSERT INTO `ACL` VALUES (595,'Ticket','isEditable','READ','ALLOW','ROLE','emplo INSERT INTO `ACL` VALUES (596,'Ticket','setDeleted','WRITE','ALLOW','ROLE','salesPerson'); INSERT INTO `ACL` VALUES (597,'Ticket','restore','WRITE','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (598,'Ticket','getSales','READ','ALLOW','ROLE','employee'); -INSERT INTO `ACL` VALUES (599,'Ticket','getSalesPersonMana','READ','ALLOW','ROLE','employee'); +INSERT INTO `ACL` VALUES (599,'Ticket','getSalesDepartmentMana','READ','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (600,'Ticket','filter','READ','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (601,'Ticket','makeInvoice','WRITE','ALLOW','ROLE','employee'); INSERT INTO `ACL` VALUES (602,'Ticket','updateEditableTicket','WRITE','ALLOW','ROLE','employee'); diff --git a/db/dump/.dump/privileges.sql b/db/dump/.dump/privileges.sql index c063df7ae..214f5394b 100644 --- a/db/dump/.dump/privileges.sql +++ b/db/dump/.dump/privileges.sql @@ -1831,7 +1831,6 @@ INSERT IGNORE INTO `procs_priv` VALUES ('','vn','marketingBoss','clientTaxArea' INSERT IGNORE INTO `procs_priv` VALUES ('','vn','administrative','clientTaxArea','FUNCTION','alexm@%','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','administrative','client_checkBalance','PROCEDURE','alexm@%','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','buyer','absoluteInventoryHistory','PROCEDURE','jenkins@db-proxy1.servers.dc.verdnatura.es','Execute','0000-00-00 00:00:00'); -INSERT IGNORE INTO `procs_priv` VALUES ('','vn','salesPerson','client_getSalesPersonByTicket','FUNCTION','alexm@%','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','pbx','developer','clientFromPhone','FUNCTION','juan@db-proxy1.servers.dc.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','util','production','log_addWithUser','PROCEDURE','juan@db-proxy2.servers.dc.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','adminBoss','balanceNestTree_addChild','PROCEDURE','jenkins@db-proxy1.servers.dc.verdnatura.es','Execute','0000-00-00 00:00:00'); @@ -1846,7 +1845,6 @@ INSERT IGNORE INTO `procs_priv` VALUES ('','vn','claimManager','item_comparativ INSERT IGNORE INTO `procs_priv` VALUES ('','vn','entryEditor','item_getVolume','FUNCTION','guillermo@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','employee','clientgetmana','FUNCTION','alexm@%','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','employee','buy_scan','PROCEDURE','alexm@%','Execute','0000-00-00 00:00:00'); -INSERT IGNORE INTO `procs_priv` VALUES ('','vn','buyer','client_getSalesPersonByTicket','FUNCTION','alexm@%','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','hr','client_create','PROCEDURE','guillermo@db-proxy2.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','entryEditor','entry_isintrastat','FUNCTION','guillermo@db-proxy2.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','manager','collection_make','PROCEDURE','alexm@%','Execute','0000-00-00 00:00:00'); @@ -2070,7 +2068,6 @@ INSERT IGNORE INTO `procs_priv` VALUES ('','vn','production','cmrpallet_add','P INSERT IGNORE INTO `procs_priv` VALUES ('','vn','production','item_getbalance','PROCEDURE','alexm@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','production','expedition_checkroute','FUNCTION','alexm@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','production','addnotefromdelivery','PROCEDURE','alexm@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); -INSERT IGNORE INTO `procs_priv` VALUES ('','vn','employee','client_getSalesPerson','FUNCTION','guillermo@db-proxy2.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','delivery','buy_updatepacking','PROCEDURE','guillermo@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','vn','delivery','buy_updategrouping','PROCEDURE','guillermo@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); INSERT IGNORE INTO `procs_priv` VALUES ('','srt','delivery','buffer_settypebyname','PROCEDURE','guillermo@db-proxy1.static.verdnatura.es','Execute','0000-00-00 00:00:00'); diff --git a/db/dump/.dump/triggers.sql b/db/dump/.dump/triggers.sql index 41525b2eb..24a27d550 100644 --- a/db/dump/.dump/triggers.sql +++ b/db/dump/.dump/triggers.sql @@ -2588,11 +2588,11 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`calendar_beforeUpdate` - BEFORE UPDATE ON `calendar` - FOR EACH ROW -BEGIN - SET NEW.editorFk = account.myUser_getId(); +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`calendar_beforeUpdate` + BEFORE UPDATE ON `calendar` + FOR EACH ROW +BEGIN + SET NEW.editorFk = account.myUser_getId(); END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -2608,15 +2608,15 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`calendar_afterDelete` - AFTER DELETE ON `calendar` - FOR EACH ROW -BEGIN - INSERT INTO workerLog - SET `action` = 'delete', - `changedModel` = 'Calendar', - `changedModelId` = OLD.id, - `userFk` = account.myUser_getId(); +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`calendar_afterDelete` + AFTER DELETE ON `calendar` + FOR EACH ROW +BEGIN + INSERT INTO workerLog + SET `action` = 'delete', + `changedModel` = 'Calendar', + `changedModelId` = OLD.id, + `userFk` = account.myUser_getId(); END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -2743,7 +2743,7 @@ BEGIN INSERT INTO claimLog SET `action` = 'delete', `changedModel` = 'ClaimBeginning', - `changedModelId` = OLD.id, + `userFk` = account.myUser_getId(); END */;; DELIMITER ; @@ -4523,31 +4523,31 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`dms_beforeUpdate` - BEFORE UPDATE ON `dms` - FOR EACH ROW -BEGIN - DECLARE vHardCopyNumber INT; - - IF (NEW.hasFile <> 0) AND (OLD.hasFile = 0) AND (NEW.hardCopyNumber IS NULL) - OR - (NEW.hardCopyNumber = OLD.hardCopyNumber AND OLD.warehouseFk <> NEW.warehouseFk) THEN - - IF (SELECT NOT hasDms FROM warehouse WHERE id = NEW.warehouseFk) THEN - SET NEW.warehouseFk = (SELECT id FROM warehouse WHERE name = 'Algemesi'); - END IF; - - SELECT 1 + MAX(hardCopyNumber) INTO vHardCopyNumber - FROM dms - WHERE warehouseFk = NEW.warehouseFk; - - SET NEW.hardCopyNumber = IFNULL(vHardCopyNumber,1); - END IF; - - IF ((NEW.hardCopyNumber = 0) OR NEW.hardCopyNumber IS NULL) AND (OLD.hardCopyNumber <> 0) THEN - - SET NEW.hasFile = 0; - END IF; +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`dms_beforeUpdate` + BEFORE UPDATE ON `dms` + FOR EACH ROW +BEGIN + DECLARE vHardCopyNumber INT; + + IF (NEW.hasFile <> 0) AND (OLD.hasFile = 0) AND (NEW.hardCopyNumber IS NULL) + OR + (NEW.hardCopyNumber = OLD.hardCopyNumber AND OLD.warehouseFk <> NEW.warehouseFk) THEN + + IF (SELECT NOT hasDms FROM warehouse WHERE id = NEW.warehouseFk) THEN + SET NEW.warehouseFk = (SELECT id FROM warehouse WHERE name = 'Algemesi'); + END IF; + + SELECT 1 + MAX(hardCopyNumber) INTO vHardCopyNumber + FROM dms + WHERE warehouseFk = NEW.warehouseFk; + + SET NEW.hardCopyNumber = IFNULL(vHardCopyNumber,1); + END IF; + + IF ((NEW.hardCopyNumber = 0) OR NEW.hardCopyNumber IS NULL) AND (OLD.hardCopyNumber <> 0) THEN + + SET NEW.hasFile = 0; + END IF; END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -7239,19 +7239,19 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`province_afterUpdate` - AFTER UPDATE ON `province` - FOR EACH ROW -BEGIN - IF !(OLD.autonomyFk <=> NEW.autonomyFk) THEN - CALL zoneGeo_setParent(NEW.geoFk, - (SELECT geoFk FROM autonomy WHERE id = NEW.autonomyFk)); - END IF; - - IF !(OLD.`name` <=> NEW.`name`) THEN - UPDATE zoneGeo SET `name` = NEW.`name` - WHERE id = NEW.geoFk; - END IF; +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`province_afterUpdate` + AFTER UPDATE ON `province` + FOR EACH ROW +BEGIN + IF !(OLD.autonomyFk <=> NEW.autonomyFk) THEN + CALL zoneGeo_setParent(NEW.geoFk, + (SELECT geoFk FROM autonomy WHERE id = NEW.autonomyFk)); + END IF; + + IF !(OLD.`name` <=> NEW.`name`) THEN + UPDATE zoneGeo SET `name` = NEW.`name` + WHERE id = NEW.geoFk; + END IF; END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -7351,20 +7351,20 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`receipt_beforeInsert` - BEFORE INSERT ON `receipt` - FOR EACH ROW -BEGIN - DECLARE vIsAutoConciliated BOOLEAN; - - IF NEW.isConciliate = FALSE THEN - SELECT isAutoConciliated INTO vIsAutoConciliated - FROM accounting a - JOIN accountingType at2 ON at2.id = a.accountingTypeFk - WHERE a.id = NEW.bankFk; - - SET NEW.isConciliate = vIsAutoConciliated; - END IF; +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`receipt_beforeInsert` + BEFORE INSERT ON `receipt` + FOR EACH ROW +BEGIN + DECLARE vIsAutoConciliated BOOLEAN; + + IF NEW.isConciliate = FALSE THEN + SELECT isAutoConciliated INTO vIsAutoConciliated + FROM accounting a + JOIN accountingType at2 ON at2.id = a.accountingTypeFk + WHERE a.id = NEW.bankFk; + + SET NEW.isConciliate = vIsAutoConciliated; + END IF; END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -7420,18 +7420,18 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`receipt_afterUpdate` - AFTER UPDATE ON `receipt` - FOR EACH ROW -BEGIN - IF NEW.isConciliate = FALSE AND NEW.payed > OLD.payed THEN - CALL mail_insert( - 'finanzas@verdnatura.es', - NULL, - CONCAT('Cambios de recibos del cliente: ', NEW.clientFk), - CONCAT('Se ha cambiado el recibo: ', NEW.Id, ' de ', OLD.payed, ' a ', NEW.payed) - ); - END IF; +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`receipt_afterUpdate` + AFTER UPDATE ON `receipt` + FOR EACH ROW +BEGIN + IF NEW.isConciliate = FALSE AND NEW.payed > OLD.payed THEN + CALL mail_insert( + 'finanzas@verdnatura.es', + NULL, + CONCAT('Cambios de recibos del cliente: ', NEW.clientFk), + CONCAT('Se ha cambiado el recibo: ', NEW.Id, ' de ', OLD.payed, ' a ', NEW.payed) + ); + END IF; END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -9172,31 +9172,31 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER ticketCollection_afterDelete -AFTER DELETE -ON ticketCollection FOR EACH ROW -BEGIN - - DECLARE vSalesRemaining INT; - - SELECT count(*) INTO vSalesRemaining - FROM vn.ticketCollection tc - JOIN sale s ON s.ticketFk = tc.ticketFk - WHERE collectionFk = OLD.collectionFk - AND tc.id != OLD.id; - - IF NOT vSalesRemaining THEN - - DELETE FROM vn.collection WHERE id = OLD.collectionFk; - - ELSE - - UPDATE vn.collection - SET saleTotalCount = vSalesRemaining - WHERE id = OLD.collectionFk; - - END IF; - +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER ticketCollection_afterDelete +AFTER DELETE +ON ticketCollection FOR EACH ROW +BEGIN + + DECLARE vSalesRemaining INT; + + SELECT count(*) INTO vSalesRemaining + FROM vn.ticketCollection tc + JOIN sale s ON s.ticketFk = tc.ticketFk + WHERE collectionFk = OLD.collectionFk + AND tc.id != OLD.id; + + IF NOT vSalesRemaining THEN + + DELETE FROM vn.collection WHERE id = OLD.collectionFk; + + ELSE + + UPDATE vn.collection + SET saleTotalCount = vSalesRemaining + WHERE id = OLD.collectionFk; + + END IF; + END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -10459,11 +10459,11 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`workerTimeControl_beforeInsert` - BEFORE INSERT ON `workerTimeControl` - FOR EACH ROW -BEGIN - SET NEW.editorFk = account.myUser_getId(); +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`workerTimeControl_beforeInsert` + BEFORE INSERT ON `workerTimeControl` + FOR EACH ROW +BEGIN + SET NEW.editorFk = account.myUser_getId(); END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -10501,11 +10501,11 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`workerTimeControl_beforeUpdate` - BEFORE UPDATE ON `workerTimeControl` - FOR EACH ROW -BEGIN - SET NEW.editorFk = account.myUser_getId(); +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`workerTimeControl_beforeUpdate` + BEFORE UPDATE ON `workerTimeControl` + FOR EACH ROW +BEGIN + SET NEW.editorFk = account.myUser_getId(); END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; @@ -10521,15 +10521,15 @@ DELIMITER ; /*!50003 SET @saved_sql_mode = @@sql_mode */ ; /*!50003 SET sql_mode = 'IGNORE_SPACE,NO_ENGINE_SUBSTITUTION' */ ; DELIMITER ;; -/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`workerTimeControl_afterDelete` - AFTER DELETE ON `workerTimeControl` - FOR EACH ROW -BEGIN - INSERT INTO workerLog - SET `action` = 'delete', - `changedModel` = 'WorkerTimeControl', - `changedModelId` = OLD.id, - `userFk` = account.myUser_getId(); +/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `vn`.`workerTimeControl_afterDelete` + AFTER DELETE ON `workerTimeControl` + FOR EACH ROW +BEGIN + INSERT INTO workerLog + SET `action` = 'delete', + `changedModel` = 'WorkerTimeControl', + `changedModelId` = OLD.id, + `userFk` = account.myUser_getId(); END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 4f85db98a..0707cfd5e 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -371,20 +371,20 @@ INSERT INTO `vn`.`contactChannel`(`id`, `name`) (4, 'GCN Channel'), (5, 'The Newspaper'); -INSERT INTO `vn`.`client`(`id`,`name`,`fi`,`socialName`,`contact`,`street`,`city`,`postcode`,`phone`,`mobile`,`isRelevant`,`email`,`iban`,`dueDay`,`accountingAccount`,`isEqualizated`,`provinceFk`,`hasToInvoice`,`credit`,`countryFk`,`isActive`,`gestdocFk`,`quality`,`payMethodFk`,`created`,`isToBeMailed`,`contactChannelFk`,`hasSepaVnl`,`hasCoreVnl`,`hasCoreVnh`,`riskCalculated`, `hasToInvoiceByAddress`,`isTaxDataChecked`,`isFreezed`,`creditInsurance`,`isCreatedAsServed`,`hasInvoiceSimplified`,`salesPersonFk`,`isVies`,`businessTypeFk`,`typeFk`) +INSERT INTO `vn`.`client`(`id`,`name`,`fi`,`socialName`,`contact`,`street`,`city`,`postcode`,`phone`,`mobile`,`isRelevant`,`email`,`iban`,`dueDay`,`accountingAccount`,`isEqualizated`,`provinceFk`,`hasToInvoice`,`credit`,`countryFk`,`isActive`,`gestdocFk`,`quality`,`payMethodFk`,`created`,`isToBeMailed`,`contactChannelFk`,`hasSepaVnl`,`hasCoreVnl`,`hasCoreVnh`,`riskCalculated`, `hasToInvoiceByAddress`,`isTaxDataChecked`,`isFreezed`,`creditInsurance`,`isCreatedAsServed`,`hasInvoiceSimplified`,`salesPersonFk`,`isVies`,`businessTypeFk`,`typeFk`, `salesDepartmentFk`) VALUES - (1101, 'Bruce Wayne', '84612325V', 'BATMAN', 'Alfred', '1007 MOUNTAIN DRIVE, GOTHAM', 'Gotham', 46460, 1111111111, 222222222, 1, 'BruceWayne@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal'), - (1102, 'Petter Parker', '87945234L', 'SPIDER MAN', 'Aunt May', '20 INGRAM STREET, QUEENS, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'PetterParker@mydomain.com', NULL, 0, 1234567890, 0, 2, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal'), - (1103, 'Clark Kent', '06815934E', 'SUPER MAN', 'lois lane', '344 CLINTON STREET, APARTAMENT 3-D', 'Gotham', 46460, 1111111111, 222222222, 1, 'ClarkKent@mydomain.com', NULL, 0, 1234567890, 0, 3, 1, 0, 19, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal'), - (1104, 'Tony Stark', '06089160W', 'IRON MAN', 'Pepper Potts', '10880 MALIBU POINT, 90265', 'Gotham', 46460, 1111111111, 222222222, 1, 'TonyStark@mydomain.com', NULL, 0, 1234567890, 0, 2, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal'), - (1105, 'Max Eisenhardt', '251628698', 'MAGNETO', 'Rogue', 'UNKNOWN WHEREABOUTS', 'Gotham', 46460, 1111111111, 222222222, 1, 'MaxEisenhardt@mydomain.com', NULL, 0, 1234567890, 0, 3, 1, 300, 8, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 1, NULL, 0, 0, 18, 0, 'florist','normal'), - (1106, 'DavidCharlesHaller', '53136686Q', 'LEGION', 'Charles Xavier', 'CITY OF NEW YORK, NEW YORK, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'DavidCharlesHaller@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 0, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 19, 0, 'florist','normal'), - (1107, 'Hank Pym', '09854837G', 'ANT MAN', 'Hawk', 'ANTHILL, SAN FRANCISCO, CALIFORNIA', 'Gotham', 46460, 1111111111, 222222222, 1, 'HankPym@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, 19, 0, 'florist','normal'), - (1108, 'Charles Xavier', '22641921P', 'PROFESSOR X', 'Beast', '3800 VICTORY PKWY, CINCINNATI, OH 45207, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'CharlesXavier@mydomain.com', NULL, 0, 1234567890, 0, 5, 1, 300, 13, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 1, NULL, 0, 0, 19, 0, 'florist','normal'), - (1109, 'Bruce Banner', '16104829E', 'HULK', 'Black widow', 'SOMEWHERE IN NEW YORK', 'Gotham', 46460, 1111111111, 222222222, 1, 'BruceBanner@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, 9, 0, 'florist','normal'), - (1110, 'Jessica Jones', '58282869H', 'JESSICA JONES', 'Luke Cage', 'NYCC 2015 POSTER', 'Gotham', 46460, 1111111111, 222222222, 1, 'JessicaJones@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, NULL, 0, 'florist','normal'), - (1111, 'Missing', NULL, 'MISSING MAN', 'Anton', 'THE SPACE, UNIVERSE FAR AWAY', 'Gotham', 46460, 1111111111, 222222222, 1, NULL, NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 0, 1, 0, NULL, 1, 0, NULL, 0, 'others','loses'), - (1112, 'Trash', NULL, 'GARBAGE MAN', 'Unknown name', 'NEW YORK CITY, UNDERGROUND', 'Gotham', 46460, 1111111111, 222222222, 1, NULL, NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 0, 1, 0, NULL, 1, 0, NULL, 0, 'others','loses'); + (1101, 'Bruce Wayne', '84612325V', 'BATMAN', 'Alfred', '1007 MOUNTAIN DRIVE, GOTHAM', 'Gotham', 46460, 1111111111, 222222222, 1, 'BruceWayne@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal', 80), + (1102, 'Petter Parker', '87945234L', 'SPIDER MAN', 'Aunt May', '20 INGRAM STREET, QUEENS, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'PetterParker@mydomain.com', NULL, 0, 1234567890, 0, 2, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal', 80), + (1103, 'Clark Kent', '06815934E', 'SUPER MAN', 'lois lane', '344 CLINTON STREET, APARTAMENT 3-D', 'Gotham', 46460, 1111111111, 222222222, 1, 'ClarkKent@mydomain.com', NULL, 0, 1234567890, 0, 3, 1, 0, 19, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal', 80), + (1104, 'Tony Stark', '06089160W', 'IRON MAN', 'Pepper Potts', '10880 MALIBU POINT, 90265', 'Gotham', 46460, 1111111111, 222222222, 1, 'TonyStark@mydomain.com', NULL, 0, 1234567890, 0, 2, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 18, 0, 'florist','normal', 80), + (1105, 'Max Eisenhardt', '251628698', 'MAGNETO', 'Rogue', 'UNKNOWN WHEREABOUTS', 'Gotham', 46460, 1111111111, 222222222, 1, 'MaxEisenhardt@mydomain.com', NULL, 0, 1234567890, 0, 3, 1, 300, 8, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 1, NULL, 0, 0, 18, 0, 'florist','normal', 80), + (1106, 'DavidCharlesHaller', '53136686Q', 'LEGION', 'Charles Xavier', 'CITY OF NEW YORK, NEW YORK, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'DavidCharlesHaller@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 0, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 0, NULL, 0, 0, 19, 0, 'florist','normal', 80), + (1107, 'Hank Pym', '09854837G', 'ANT MAN', 'Hawk', 'ANTHILL, SAN FRANCISCO, CALIFORNIA', 'Gotham', 46460, 1111111111, 222222222, 1, 'HankPym@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, 19, 0, 'florist','normal', 80), + (1108, 'Charles Xavier', '22641921P', 'PROFESSOR X', 'Beast', '3800 VICTORY PKWY, CINCINNATI, OH 45207, USA', 'Gotham', 46460, 1111111111, 222222222, 1, 'CharlesXavier@mydomain.com', NULL, 0, 1234567890, 0, 5, 1, 300, 13, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 1, 1, NULL, 0, 0, 19, 0, 'florist','normal', 80), + (1109, 'Bruce Banner', '16104829E', 'HULK', 'Black widow', 'SOMEWHERE IN NEW YORK', 'Gotham', 46460, 1111111111, 222222222, 1, 'BruceBanner@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, 9, 0, 'florist','normal', 80), + (1110, 'Jessica Jones', '58282869H', 'JESSICA JONES', 'Luke Cage', 'NYCC 2015 POSTER', 'Gotham', 46460, 1111111111, 222222222, 1, 'JessicaJones@mydomain.com', NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 1, 0, 0, NULL, 0, 0, NULL, 0, 'florist','normal', 80), + (1111, 'Missing', NULL, 'MISSING MAN', 'Anton', 'THE SPACE, UNIVERSE FAR AWAY', 'Gotham', 46460, 1111111111, 222222222, 1, NULL, NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 0, 1, 0, NULL, 1, 0, NULL, 0, 'others','loses', 80), + (1112, 'Trash', NULL, 'GARBAGE MAN', 'Unknown name', 'NEW YORK CITY, UNDERGROUND', 'Gotham', 46460, 1111111111, 222222222, 1, NULL, NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1, NULL, 10, 5, util.VN_CURDATE(), 1, 5, 1, 1, 1, '0000-00-00', 0, 1, 0, NULL, 1, 0, NULL, 0, 'others','loses', 80); INSERT INTO `vn`.`client`(`id`, `name`, `fi`, `socialName`, `contact`, `street`, `city`, `postcode`, `isRelevant`, `email`, `iban`,`dueDay`,`accountingAccount`, `isEqualizated`, `provinceFk`, `hasToInvoice`, `credit`, `countryFk`, `isActive`, `gestdocFk`, `quality`, `payMethodFk`,`created`, `isTaxDataChecked`) SELECT id, name, CONCAT(RPAD(CONCAT(id,9),8,id),'A'), UPPER(CONCAT(name, 'Social')), CONCAT(name, 'Contact'), CONCAT(name, 'Street'), 'GOTHAM', 46460, 1, CONCAT(name,'@mydomain.com'), NULL, 0, 1234567890, 0, 1, 1, 300, 1, 1,NULL, 10, 5, util.VN_CURDATE(), 1 @@ -1816,9 +1816,8 @@ INSERT INTO `vn`.`clientContact`(`id`, `clientFk`, `name`, `phone`) (3, 1101, 'contact 3', 222333444), (4, 1102, 'contact 1', 876543219); -INSERT INTO `vn`.`workerManaExcluded`(`workerFk`) - VALUES - (9); +INSERT INTO `vn`.`departmentManaExcluded`(`salesDepartmentFk`) + VALUES (31); /* el mana de los trabajadores lo podemos poner a mano en la tabla si lo calculamos antes, pero si hazemos alguna modificacion en alguna tabla que utiliza para calcularlo ya no seria correcto @@ -1826,8 +1825,8 @@ INSERT INTO `vn`.`workerManaExcluded`(`workerFk`) La otra manera es poner el calculo con los 2 trabajadores que utilizamos ahora mismo para los tickets */ -call vn.manaSpellersRequery(19); -call vn.manaSpellersRequery(18); +call vn.manaSpellers_requery(80); +call vn.manaSpellers_requery(31); INSERT INTO `vn`.`clientSample`(`id`, `clientFk`, `typeFk`, `created`, `workerFk`, `userFk`, `companyFk`) VALUES diff --git a/db/routines/bi/procedures/analisis_ventas_update.sql b/db/routines/bi/procedures/analisis_ventas_update.sql index ef3e165a0..d4c6feb75 100644 --- a/db/routines/bi/procedures/analisis_ventas_update.sql +++ b/db/routines/bi/procedures/analisis_ventas_update.sql @@ -12,7 +12,7 @@ BEGIN INSERT INTO analisis_ventas ( Familia, Reino, - Comercial, + salesDepartmentFk, Comprador, Provincia, almacen, @@ -25,8 +25,8 @@ BEGIN SELECT it.name, ic.name, + c.salesDepartmentFk, w.code, - w2.code, p.name, wa.name, tm.year, @@ -38,8 +38,7 @@ BEGIN LEFT JOIN vn.itemType it ON it.id = bt.tipo_id LEFT JOIN vn.itemCategory ic ON ic.id = it.categoryFk LEFT JOIN vn.client c on c.id = bt.Id_Cliente - LEFT JOIN vn.worker w ON w.id = c.salesPersonFk - LEFT JOIN vn.worker w2 ON w2.id = it.workerFk + LEFT JOIN vn.worker w ON w.id = it.workerFk JOIN vn.time tm ON tm.dated = bt.fecha JOIN vn.sale s ON s.id = bt.Id_Movimiento LEFT JOIN vn.ticket t ON t.id = s.ticketFk diff --git a/db/routines/bi/procedures/defaultersFromDate.sql b/db/routines/bi/procedures/defaultersFromDate.sql index bfe133750..43b2a2bf2 100644 --- a/db/routines/bi/procedures/defaultersFromDate.sql +++ b/db/routines/bi/procedures/defaultersFromDate.sql @@ -1,24 +1,43 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`defaultersFromDate`(IN vDate DATE) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`defaultersFromDate`( + IN vDated DATE +) BEGIN - - SELECT t1.*, c.name Cliente, w.code workerCode, c.payMethodFk pay_met_id, c.dueDay Vencimiento - FROM ( - -- Filtramos aquellos clientes cuyo saldo se ha incrementado de ayer a hoy - select * from( - select today.client, today.amount todayAmount, yesterday.amount yesterdayAmount, round(yesterday.amount - today.amount,2) as difference, defaulterSince - from - (select client, amount, defaulterSince - from defaulters - where date = vDate and hasChanged) today - join - (select client, amount - from defaulters - where date = TIMESTAMPADD(DAY,-1,vDate)) yesterday using(client) - - having today.amount > 0 and difference <> 0 - ) newDefaulters - )t1 left join vn.client c ON c.id = t1.client - left join vn.worker w ON w.id = c.salesPersonFk; +/** + * Retorna la info de clientes morosos a una fecha + * + * @param vDated Fecha a comprobar + */ + WITH todayDefaulters AS( + SELECT client, amount, defaulterSince + FROM bi.defaulters + WHERE date = vDated + AND hasChanged + ), yesterdayDefaulters AS( + SELECT client, amount + FROM bi.defaulters + WHERE date = vDated - INTERVAL 1 DAY + ), newDefaulters AS( + SELECT td.client, + td.amount todayAmount, + yd.amount yesterdayAmount, + ROUND(yd.amount - td.amount, 2) difference, + defaulterSince + FROM todayDefaulters td + JOIN yesterdayDefaulters yd ON yd.client = td.client + WHERE td.amount > 0 + HAVING difference <> 0 + ) SELECT nd.client, + nd.todayAmount, + nd.yesterdayAmount, + nd.difference, + nd.defaulterSince, + c.name Cliente, + d.name salesDepartmentName, + c.payMethodFk pay_met_id, + c.dueDay Vencimiento + FROM newDefaulters nd + LEFT JOIN vn.client c ON c.id = nd.client + LEFT JOIN vn.department d ON d.id = c.salesDepartmentFk; END$$ DELIMITER ; diff --git a/db/routines/bs/procedures/campaignComparative.sql b/db/routines/bs/procedures/campaignComparative.sql deleted file mode 100644 index 6b4b983b5..000000000 --- a/db/routines/bs/procedures/campaignComparative.sql +++ /dev/null @@ -1,42 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`campaignComparative`(vDateFrom DATE, vDateTo DATE) -BEGIN - SELECT - workerName, - id, - name, - CAST(SUM(previousAmmount) AS DECIMAL(10, 0)) AS previousAmmount, - CAST(SUM(currentAmmount) AS DECIMAL(10, 0)) AS currentAmmount - FROM ( - (SELECT - CONCAT(w.firstname, ' ', w.lastName) AS workerName, - c.id, - c.name, - SUM(v.importe) AS previousAmmount, - 0 currentAmmount - FROM bs.ventas v - INNER JOIN vn.`client` c ON v.Id_Cliente = c.id - INNER JOIN vn.worker w ON c.salesPersonFk = w.id - WHERE v.fecha BETWEEN DATE_ADD(vDateFrom, INTERVAL - 1 YEAR) - AND DATE_ADD(vDateTo, INTERVAL - 1 YEAR) - GROUP BY w.id, v.Id_Cliente) - UNION ALL - (SELECT - CONCAT(w.firstname, ' ', w.lastName) AS workerName, - c.id, - c.name, - 0 AS previousAmmount, - SUM(s.quantity * s.price) AS currentAmmount - FROM vn.sale s - JOIN vn.ticket t ON t.id = s.ticketFk - JOIN vn.client c ON c.id = t.clientFk - JOIN vn.worker w ON c.salesPersonFk = w.id - WHERE t.shipped BETWEEN vDateFrom - AND vDateTo - GROUP BY w.id, c.id) - ) comparative - GROUP BY workerName, id - HAVING (previousAmmount <> 0 OR currentAmmount <> 0) - ORDER BY workerName, id; -END$$ -DELIMITER ; diff --git a/db/routines/bs/procedures/carteras_add.sql b/db/routines/bs/procedures/carteras_add.sql deleted file mode 100644 index 6de377371..000000000 --- a/db/routines/bs/procedures/carteras_add.sql +++ /dev/null @@ -1,27 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`carteras_add`() -BEGIN -/** - * Inserta en la tabla @bs.carteras las ventas desde el año pasado - * agrupadas por trabajador, año y mes - */ - DECLARE vYear INT DEFAULT YEAR(util.VN_CURDATE()) - 1; - - DELETE FROM bs.carteras WHERE Año >= vYear; - - CALL util.time_generate( - MAKEDATE(vYear, 1), - (SELECT MAX(fecha) FROM ventas) - ); - - INSERT INTO carteras(Año, Mes , CodigoTrabajador, Peso) - SELECT t.`year`, t.`month`, w.code, SUM(v.importe) - FROM tmp.time t - JOIN ventas v on t.dated = v.fecha - JOIN vn.client c on c.id = v.Id_Cliente - JOIN vn.worker w ON w.id = c.salesPersonFk - GROUP BY w.code, t.`year`, t.`month`; - - DROP TEMPORARY TABLE tmp.time; -END$$ -DELIMITER ; diff --git a/db/routines/bs/procedures/clean.sql b/db/routines/bs/procedures/clean.sql index eff2faadb..e3ecf975a 100644 --- a/db/routines/bs/procedures/clean.sql +++ b/db/routines/bs/procedures/clean.sql @@ -20,8 +20,8 @@ BEGIN DELETE FROM payMethodClientEvolution WHERE dated < vFourYearsAgo; - DELETE FROM salesByclientSalesPerson - WHERE dated < vFourYearsAgo; + DELETE FROM salesByClientDepartment + WHERE dated < vFourYearsAgo; DELETE FROM m3 WHERE fecha < vTwoYearAgo; diff --git a/db/routines/bs/procedures/clientDied_calc.sql b/db/routines/bs/procedures/clientDied_calc.sql new file mode 100644 index 000000000..3cb93e74a --- /dev/null +++ b/db/routines/bs/procedures/clientDied_calc.sql @@ -0,0 +1,61 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`clientDied_calc`( + vDays INT, + vCountryCode VARCHAR(2) +) +BEGIN +/** + * Recalcula los clientes inactivos y hace insert en la tabla clientDied + * estableciendo hasta 3 avisos en función del periodo y el código de país. + * + * @param vDays El número de días a considerar para la inactividad del cliente + * @param vCountryCode El código del país para filtrar los clientes + */ + DECLARE vFirstPeriod , vSecondPeriod, vThridPeriod DATE; + SET vFirstPeriod = util.VN_CURDATE() - INTERVAL vDays DAY; + SET vSecondPeriod = util.VN_CURDATE() - INTERVAL vDays * 2 DAY; + SET vThridPeriod = util.VN_CURDATE() - INTERVAL vDays * 3 DAY; + + DELETE cd.* FROM clientDied cd + JOIN ( + SELECT c.id FROM vn.client c + JOIN vn.country co ON co.id = c.countryFk + WHERE co.code = vCountryCode + ) sub ON sub.id = cd.clientFk; + + INSERT INTO clientDied (clientFk, lastInvoiced, warning) + SELECT c.id, + sub.lastShipped, + CASE + WHEN lastShipped < vThridPeriod OR lastShipped IS NULL THEN 'third' + WHEN lastShipped < vSecondPeriod THEN 'second' + WHEN lastShipped < vFirstPeriod THEN 'first' + END + FROM vn.client c + JOIN vn.country co ON co .id = c.countryFk + JOIN vn.department w ON w.id = c.salesDepartmentFk + JOIN vn.departmentMana dm ON dm.salesDepartmentFk = c.salesDepartmentFk + LEFT JOIN ( + SELECT c.id, DATE(MAX(t.shipped)) lastShipped + FROM vn.client c + LEFT JOIN vn.ticket t ON t.clientFk = c.id + LEFT JOIN vn.country co ON co.id = c.countryFk + WHERE co.code = vCountryCode + AND (t.shipped <= util.VN_CURDATE() OR t.shipped IS NULL) + GROUP BY c.id + ) sub ON sub.id = c.id + LEFT JOIN vn.clientObservation cob ON cob.clientFk = c.id + AND cob.created > vThridPeriod + WHERE (sub.lastShipped < vFirstPeriod OR sub.lastShipped IS NULL) + AND c.created < vThridPeriod + AND co.code = vCountryCode + AND cob.`text` IS NULL + AND c.id NOT IN ( + SELECT DISTINCT clientFk + FROM vn.ticket + WHERE refFk IS NULL + AND shipped >= vFirstPeriod + ) + GROUP BY c.id; +END$$ +DELIMITER ; diff --git a/db/routines/bs/procedures/clientDied_recalc.sql b/db/routines/bs/procedures/clientDied_recalc.sql index 1b5cb5ac8..89e82b7fb 100644 --- a/db/routines/bs/procedures/clientDied_recalc.sql +++ b/db/routines/bs/procedures/clientDied_recalc.sql @@ -33,9 +33,7 @@ BEGIN END FROM vn.client c JOIN vn.country co ON co .id = c.countryFk - JOIN vn.worker w ON w.id = c.salesPersonFk - JOIN vn.worker b ON b.id = w.bossFk - JOIN vn.workerMana wm ON wm.workerFk = c.salesPersonFk + JOIN vn.departmentMana dm ON dm.salesDepartmentFk = c.salesDepartmentFk LEFT JOIN ( SELECT c.id, DATE(MAX(t.shipped)) lastShipped FROM vn.client c diff --git a/db/routines/bs/procedures/comparativeCampaign.sql b/db/routines/bs/procedures/comparativeCampaign.sql new file mode 100644 index 000000000..c05b20921 --- /dev/null +++ b/db/routines/bs/procedures/comparativeCampaign.sql @@ -0,0 +1,39 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root``localhost` PROCEDURE `bs`.`comparativeCampaign`(vDateFrom DATE, vDateTo DATE) +BEGIN + SELECT deparmentName, + id clientFk, + name clientName, + CAST(SUM(previousAmmount) AS DECIMAL(10, 0)) previousAmmount, + CAST(SUM(currentAmmount) AS DECIMAL(10, 0)) currentAmmount + FROM ((SELECT + d.name deparmentName, + c.id, + c.name, + SUM(s.amount) previousAmmount, + 0 currentAmmount + FROM sale s + JOIN vn.`client` c ON s.clientFk = c.id + JOIN vn.department d ON d.id = c.salesDepartmentFk + WHERE s.dated BETWEEN DATE_ADD(vDateFrom, INTERVAL - 1 YEAR) + AND DATE_ADD(vDateTo, INTERVAL - 1 YEAR) + GROUP BY d.id, s.clientFk) + UNION ALL + (SELECT + d.name deparmentName, + c.id, + c.name, + 0 AS previousAmmount, + SUM(s.quantity * s.price) currentAmmount + FROM vn.sale s + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.client c ON c.id = t.clientFk + JOIN vn.department d ON d.id = c.salesDepartmentFk + WHERE t.shipped BETWEEN vDateFrom AND vDateTo + GROUP BY d.id, c.id) + ) comparative + GROUP BY deparmentName, id + HAVING previousAmmount OR currentAmmount + ORDER BY deparmentName, id; +END$$ +DELIMITER ; diff --git a/db/routines/bs/procedures/manaCustomerUpdate.sql b/db/routines/bs/procedures/manaCustomerUpdate.sql index e9ba70423..146c1e686 100644 --- a/db/routines/bs/procedures/manaCustomerUpdate.sql +++ b/db/routines/bs/procedures/manaCustomerUpdate.sql @@ -27,9 +27,9 @@ BEGIN SELECT id INTO vManaGreugeTypeId FROM vn.greugeType WHERE code = 'mana'; - SELECT manaFromDays, manaToDays - INTO vManaFromDays, vManaToDays - FROM vn.salespersonConfig; + SELECT manaFromDays, manaToDays + INTO vManaFromDays, vManaToDays + FROM vn.salesDepartmentConfig; SELECT MAX(dated) INTO vFromDated FROM vn.clientManaCache; @@ -43,7 +43,7 @@ BEGIN IF vFromDated IS NULL THEN SELECT manaDateFrom INTO vFromDated - FROM vn.salespersonConfig; + FROM vn.salesDepartmentConfig; END IF; WHILE vFromDated + INTERVAL vManaToDays DAY < util.VN_CURDATE() DO diff --git a/db/routines/bs/procedures/manaSpellers_actualize.sql b/db/routines/bs/procedures/manaSpellers_actualize.sql index 20b0f84f8..8190adb5f 100644 --- a/db/routines/bs/procedures/manaSpellers_actualize.sql +++ b/db/routines/bs/procedures/manaSpellers_actualize.sql @@ -5,23 +5,23 @@ BEGIN * Recalcula el valor del campo con el modificador de precio * para el componente de maná automático. */ - UPDATE vn.workerMana wm + UPDATE vn.departmentMana dm JOIN ( - SELECT c.lastSalesPersonFk, + SELECT c.lastSalesDepartmentFk, FLOOR(SUM(s.amount) / 12) amount - FROM salesByclientSalesPerson s + FROM salesByClientDepartment s JOIN vn.client c ON c.id = s.clientFk WHERE s.dated BETWEEN util.VN_CURDATE() - INTERVAL 1 YEAR AND util.VN_CURDATE() - GROUP BY c.lastSalesPersonFk - )avgPortfolioWeight ON avgPortfolioWeight.lastSalesPersonFk = wm.workerFk - JOIN vn.salespersonConfig spc - SET wm.pricesModifierRate = + GROUP BY c.lastSalesDepartmentFk + )avgPortfolioWeight ON avgPortfolioWeight.lastSalesDepartmentFk = dm.salesDepartmentFk + JOIN vn.salesDepartmentConfig sdc + SET dm.pricesModifierRate = IFNULL( GREATEST( - spc.manaMinRate, + sdc.manaMinRate, LEAST( - spc.manaMaxRate, - ROUND( - wm.amount / avgPortfolioWeight.amount, 3) + sdc.manaMaxRate, + ROUND( - dm.amount / avgPortfolioWeight.amount, 3) ) ) ,0); diff --git a/db/routines/bs/procedures/porfolio_add.sql b/db/routines/bs/procedures/porfolio_add.sql new file mode 100644 index 000000000..5257ab8e8 --- /dev/null +++ b/db/routines/bs/procedures/porfolio_add.sql @@ -0,0 +1,27 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`porfolio_add`() +BEGIN +/** + * Inserta en la tabla @bs.portfolio las ventas desde el año pasado + * agrupadas por equipo, año y mes + */ + DECLARE vYear INT DEFAULT YEAR(util.VN_CURDATE()) - 1; + + CALL util.time_generate( + MAKEDATE(vYear, 1), + (SELECT MAX(dated) FROM sale) + ); + + INSERT INTO portfolio(yeared, monthed , saleDepartmentFk, Amount) + SELECT t.`year`, t.`month`, w.code, SUM(s.amount) + FROM tmp.time t + JOIN sale s on t.dated = s.dated + JOIN vn.client c on c.id = s.clientFk + JOIN vn.department d ON d.id = c.salesDepartmentFk + GROUP BY d.id, t.`year`, t.`month`; + + DROP TEMPORARY TABLE tmp.time; +END$$ +DELIMITER ; + + diff --git a/db/routines/bs/procedures/salePersonEvolutionAdd.sql b/db/routines/bs/procedures/salePersonEvolutionAdd.sql deleted file mode 100644 index 33e31b699..000000000 --- a/db/routines/bs/procedures/salePersonEvolutionAdd.sql +++ /dev/null @@ -1,20 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`salePersonEvolutionAdd`(IN vDateStart DATETIME) -BEGIN - DELETE FROM bs.salePersonEvolution - WHERE dated <= DATE_SUB(util.VN_CURDATE(), INTERVAL 1 YEAR); - - - INSERT INTO bs.salePersonEvolution (dated, amount, equalizationTax, salesPersonFk) - SELECT fecha dated, - CAST(SUM(importe) AS DECIMAL(10,2) ) amount, - CAST(SUM(recargo) AS DECIMAL(10,2) ) equalizationTax , - IFNULL(salesPersonFk,0) salesPersonFk - FROM bs.ventas v - JOIN vn.client c ON v.Id_Cliente = c.id - JOIN vn.company co ON co.id = v.empresa_id - WHERE co.code = "VNL" AND fecha >= vDateStart - GROUP BY v.fecha,c.salesPersonFk - ORDER BY salesPersonFk,dated ASC; -END$$ -DELIMITER ; diff --git a/db/routines/bs/procedures/salesByclientSalesPerson_add.sql b/db/routines/bs/procedures/salesByClientDepartment_add.sql similarity index 74% rename from db/routines/bs/procedures/salesByclientSalesPerson_add.sql rename to db/routines/bs/procedures/salesByClientDepartment_add.sql index eb441c07b..96b5ec7b0 100644 --- a/db/routines/bs/procedures/salesByclientSalesPerson_add.sql +++ b/db/routines/bs/procedures/salesByClientDepartment_add.sql @@ -1,8 +1,8 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`salesByclientSalesPerson_add`(vDatedFrom DATE) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`salesByClientDepartments_add`(vDatedFrom DATE) BEGIN /** - * Agrupa las ventas por cliente/comercial/fecha en la tabla bs.salesByclientSalesPerson + * Agrupa las ventas por cliente/departamento/fecha en la tabla bs.salesByClientDepartment * El asociación cliente/comercial/fecha, se mantiene correcta en el tiempo * * @param vDatedFrom el cálculo se realizará desde la fecha introducida hasta ayer @@ -12,31 +12,31 @@ BEGIN SET vDatedFrom = util.VN_CURDATE() - INTERVAL 1 MONTH; END IF; - UPDATE salesByclientSalesPerson + UPDATE salesByClientDepartment SET amount = 0, equalizationTax = 0, amountNewBorn = 0 WHERE dated BETWEEN vDatedFrom AND util.yesterday(); - INSERT INTO salesByclientSalesPerson( + INSERT INTO salesByClientDepartment( dated, - salesPersonFk, + salesDepartmentFk, clientFk, amount, equalizationTax) SELECT s.dated, - c.salesPersonFk, + c.salesDepartmentFk, s.clientFk, SUM(s.amount), SUM(s.surcharge) FROM sale s JOIN vn.client c on s.clientFk = c.id WHERE s.dated BETWEEN vDatedFrom AND util.yesterday() - GROUP BY s.dated, c.salesPersonFk, s.clientFk + GROUP BY s.dated, c.salesDepartmentFk, s.clientFk ON DUPLICATE KEY UPDATE amount= VALUES(amount), equalizationTax= VALUES(equalizationTax); - UPDATE salesByclientSalesPerson s + UPDATE salesByClientDepartment s JOIN vn.newBornSales n ON n.dated = s.dated AND n.clientFk = s.clientFk SET s.amountNewBorn = n.amount diff --git a/db/routines/bs/procedures/salesDepartmentEvolution_add.sql b/db/routines/bs/procedures/salesDepartmentEvolution_add.sql new file mode 100644 index 000000000..b0bd7b3ea --- /dev/null +++ b/db/routines/bs/procedures/salesDepartmentEvolution_add.sql @@ -0,0 +1,64 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`salesDepartmentEvolution_add`() +BEGIN +/** + * Calcula los datos para los gráficos de evolución agrupado por salesDepartmentFk y día. + * Recalcula automáticamente los 3 últimos meses para comprobar si hay algún cambio. + */ + DECLARE vDated DATE; + + SELECT MAX(dated) - INTERVAL 3 MONTH INTO vDated + FROM salesDepartmentEvolution; + + DELETE FROM salesDepartmentEvolution + WHERE dated >= vDated; + + IF ISNULL(vDated) THEN + SELECT MIN(dated) INTO vDated + FROM salesByClientDepartment; + + INSERT INTO salesByClientDepartment( + salesDepartmentFk, + dated, + amount, + equalizationTax, + amountNewBorn + ) + SELECT salesDepartmentFk, + dated, + amount, + equalizationTax, + amountNewBorn + FROM salesByClientDepartment + WHERE dated = vDated + GROUP BY salesDepartmentFk; + + SET vDated = vDated + INTERVAL 1 DAY; + END IF; + + WHILE vDated < util.VN_CURDATE() DO + + REPLACE salesByClientDepartment(salesDepartmentFk, dated, amount) + SELECT salesDepartmentFk, vDated, amount + FROM(SELECT salesDepartmentFk, SUM(amount) amount + FROM(SELECT salesDepartmentFk, amount + FROM salesByClientDepartment + WHERE dated = vDated - INTERVAL 1 DAY + UNION ALL + SELECT salesDepartmentFk, amount + FROM salesByClientDepartment + WHERE dated = vDated + UNION ALL + SELECT salesDepartmentFk, - amount + FROM salesByClientDepartment + WHERE dated = vDated - INTERVAL 1 YEAR + )sub + GROUP BY salesDepartmentFk + )sub + GROUP BY salesDepartmentFk; + + SET vDated = vDated + INTERVAL 1 DAY; + + END WHILE; +END$$ +DELIMITER ; diff --git a/db/routines/bs/procedures/salesPersonEvolution_add.sql b/db/routines/bs/procedures/salesPersonEvolution_add.sql deleted file mode 100644 index ea150e182..000000000 --- a/db/routines/bs/procedures/salesPersonEvolution_add.sql +++ /dev/null @@ -1,65 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`salesPersonEvolution_add`() -BEGIN -/** - * Calcula los datos para los gráficos de evolución agrupado por salesPersonFk y día. - * Recalcula automáticamente los 3 últimos meses para comprobar si hay algún cambio. - */ - DECLARE vDated DATE; - DECLARE vCont INT DEFAULT 1; - - SELECT MAX(dated) - INTERVAL 3 MONTH INTO vDated - FROM salesPersonEvolution; - - DELETE FROM salesPersonEvolution - WHERE dated >= vDated; - - IF ISNULL(vDated) THEN - SELECT MIN(dated) INTO vDated - FROM salesByclientSalesPerson; - - INSERT INTO salesPersonEvolution( - salesPersonFk, - dated, - amount, - equalizationTax, - amountNewBorn - ) - SELECT salesPersonFk, - dated, - amount, - equalizationTax, - amountNewBorn - FROM salesByclientSalesPerson - WHERE dated = vDated - GROUP BY salesPersonFk; - - SET vDated = vDated + INTERVAL 1 DAY; - END IF; - - WHILE vDated < util.VN_CURDATE() DO - - SET vCont = vCont + 1; - REPLACE salesPersonEvolution(salesPersonFk, dated, amount) - SELECT salesPersonFk, vDated, amount - FROM(SELECT salesPersonFk, SUM(amount) amount - FROM(SELECT salesPersonFk, amount - FROM salesPersonEvolution - WHERE dated = vDated - INTERVAL 1 DAY - UNION ALL - SELECT salesPersonFk, amount - FROM salesByclientSalesPerson - WHERE dated = vDated - UNION ALL - SELECT salesPersonFk, - amount - FROM salesByclientSalesPerson - WHERE dated = vDated - INTERVAL 1 YEAR - )sub - GROUP BY salesPersonFk - )sub - GROUP BY salesPersonFk; - - SET vDated = vDated + INTERVAL 1 DAY; - END WHILE; -END$$ -DELIMITER ; diff --git a/db/routines/bs/procedures/vendedores_add_launcher.sql b/db/routines/bs/procedures/vendedores_add_launcher.sql index c0718a659..eb87e7969 100644 --- a/db/routines/bs/procedures/vendedores_add_launcher.sql +++ b/db/routines/bs/procedures/vendedores_add_launcher.sql @@ -2,7 +2,7 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`vendedores_add_launcher`() BEGIN - CALL bs.salesByclientSalesPerson_add(util.VN_CURDATE()- INTERVAL 45 DAY); + CALL bs.salesByclientSalesDepartment_add(util.VN_CURDATE()- INTERVAL 45 DAY); END$$ DELIMITER ; diff --git a/db/routines/vn/events/client_unassignSalesPerson.sql b/db/routines/vn/events/client_unassignSalesDepartment.sql similarity index 73% rename from db/routines/vn/events/client_unassignSalesPerson.sql rename to db/routines/vn/events/client_unassignSalesDepartment.sql index 46ad414b1..558ac4194 100644 --- a/db/routines/vn/events/client_unassignSalesPerson.sql +++ b/db/routines/vn/events/client_unassignSalesDepartment.sql @@ -1,8 +1,8 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`client_unassignSalesPerson` +CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`client_unassignSalesDepartment` ON SCHEDULE EVERY 1 DAY STARTS '2023-06-01 03:30:00.000' ON COMPLETION PRESERVE ENABLE -DO CALL client_unassignSalesPerson$$ +DO CALL client_unassignSalesDepartment$$ DELIMITER ; diff --git a/db/routines/vn/functions/catalog_componentReverse.sql b/db/routines/vn/functions/catalog_componentReverse.sql index f37b20890..e4bac208f 100644 --- a/db/routines/vn/functions/catalog_componentReverse.sql +++ b/db/routines/vn/functions/catalog_componentReverse.sql @@ -69,10 +69,10 @@ BEGIN -- Componente de maná automático, en función del maná acumulado por el comercial. INSERT INTO tmp.catalog_component (warehouseFk, itemFk, componentFk, cost) - SELECT vWarehouse, vItem, vComponentMana, ROUND(wm.pricesModifierRate, 3) + SELECT vWarehouse, vItem, vComponentMana, ROUND(dm.pricesModifierRate, 3) FROM client c - JOIN vn.workerMana wm ON c.salesPersonFk = wm.workerFk - WHERE wm.isPricesModifierActivated AND c.id = vCustomer LIMIT 1; + JOIN vn.departmentMana dm ON c.salesDepartmentFk = dm.salesDepartmentFk + WHERE dm.isPricesModifierActivated AND c.id = vCustomer LIMIT 1; -- Reparto INSERT INTO tmp.catalog_component (warehouseFk, itemFk, componentFk, cost) diff --git a/db/routines/vn/functions/clientGetSalesPerson.sql b/db/routines/vn/functions/clientGetSalesPerson.sql deleted file mode 100644 index 4b8601be3..000000000 --- a/db/routines/vn/functions/clientGetSalesPerson.sql +++ /dev/null @@ -1,11 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`clientGetSalesPerson`(vClientFk INT, vDated DATE) - RETURNS int(11) - DETERMINISTIC -BEGIN -/** - * DEPRECATED: use client_getSalesPerson - **/ - RETURN client_getSalesPerson(vClientFk, vDated); -END$$ -DELIMITER ; diff --git a/db/routines/vn/functions/client_getSalesPerson.sql b/db/routines/vn/functions/client_getSalesPerson.sql deleted file mode 100644 index c53816f7f..000000000 --- a/db/routines/vn/functions/client_getSalesPerson.sql +++ /dev/null @@ -1,74 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`client_getSalesPerson`(vClientFk INT, vDated DATE) - RETURNS int(11) - DETERMINISTIC -BEGIN -/** - * Dado un id cliente y una fecha, devuelve su comercial para ese dia, teniendo - * en cuenta la jerarquía de las tablas: 1º la de sharingclient, 2º la de - * sharingcart y tercero la de clientes. - * - * @param vClientFk El id del cliente - * @param vDated Fecha a comprobar - * @return El id del comercial para la fecha dada - **/ - DECLARE vSalesPersonFk INT DEFAULT NULL; - DECLARE vWorkerSubstituteFk INT DEFAULT NULL; - DECLARE vLoop BOOLEAN; - - -- Obtiene el comercial original y el de sharingclient - - SELECT c.salesPersonFk, s.workerFk - INTO vSalesPersonFk, vWorkerSubstituteFk - FROM client c - LEFT JOIN sharingClient s - ON c.id = s.clientFk - AND vDated BETWEEN s.started AND s.ended - WHERE c.id = vClientFk - ORDER BY s.id - LIMIT 1; - - -- Si no hay ninguno en sharingclient busca en sharingcart - - IF vWorkerSubstituteFk IS NOT NULL - THEN - SET vSalesPersonFk = vWorkerSubstituteFk; - ELSEIF vSalesPersonFk IS NOT NULL - THEN - DROP TEMPORARY TABLE IF EXISTS tmp.stack; - CREATE TEMPORARY TABLE tmp.stack - (INDEX (substitute)) - ENGINE = MEMORY - SELECT vSalesPersonFk substitute; - - l: LOOP - SELECT workerSubstitute INTO vWorkerSubstituteFk - FROM sharingCart - WHERE util.VN_CURDATE() BETWEEN started AND ended - AND workerFk = vSalesPersonFk - ORDER BY id - LIMIT 1; - - IF vWorkerSubstituteFk IS NULL THEN - LEAVE l; - END IF; - - SELECT COUNT(*) > 0 INTO vLoop - FROM tmp.stack WHERE substitute = vWorkerSubstituteFk; - - IF vLoop THEN - LEAVE l; - END IF; - - INSERT INTO tmp.stack SET - substitute = vWorkerSubstituteFk; - - SET vSalesPersonFk = vWorkerSubstituteFk; - END LOOP; - - DROP TEMPORARY TABLE tmp.stack; - END IF; - - RETURN vSalesPersonFk; -END$$ -DELIMITER ; diff --git a/db/routines/vn/functions/client_getSalesPersonByTicket.sql b/db/routines/vn/functions/client_getSalesPersonByTicket.sql deleted file mode 100644 index 640df11ce..000000000 --- a/db/routines/vn/functions/client_getSalesPersonByTicket.sql +++ /dev/null @@ -1,23 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`client_getSalesPersonByTicket`(vTicketFk INT) - RETURNS int(11) - DETERMINISTIC -BEGIN -/** - * Dado un id ticket, devuelve su comercial. - * Para más información ir a client_getSalesPerson() - * - * @param vClientFk El id del cliente - * @param vDated Fecha a comprobar - * @return El id del comercial para la fecha dada - **/ - DECLARE vClientFk INT; - DECLARE vDated DATE; - - SELECT clientFk, shipped - INTO vClientFk, vDated - FROM ticket WHERE id = vTicketFk; - - RETURN client_getSalesPerson(vClientFk, vDated); -END$$ -DELIMITER ; diff --git a/db/routines/vn/functions/client_getSalesPersonCode.sql b/db/routines/vn/functions/client_getSalesPersonCode.sql deleted file mode 100644 index 69b8424d8..000000000 --- a/db/routines/vn/functions/client_getSalesPersonCode.sql +++ /dev/null @@ -1,26 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`client_getSalesPersonCode`(vClientFk INT, vDated DATE) - RETURNS varchar(3) CHARSET utf8mb3 COLLATE utf8mb3_general_ci - DETERMINISTIC -BEGIN -/** - * Dado un id cliente y una fecha, devuelve su comercial. - * Para más información ir a client_getSalesPerson() - * - * @param vClientFk El id del cliente - * @param vDated Fecha a comprobar - * @return El código del comercial para la fecha dada - **/ - DECLARE vWorkerCode CHAR(3); - DECLARE vSalesPersonFk INT; - - SET vSalesPersonFk = client_getSalesPerson(vClientFk, vDated); - - SELECT code - INTO vWorkerCode - FROM worker - WHERE id = vSalesPersonFk; - - RETURN vWorkerCode; -END$$ -DELIMITER ; diff --git a/db/routines/vn/functions/client_getSalesPersonCodeByTicket.sql b/db/routines/vn/functions/client_getSalesPersonCodeByTicket.sql deleted file mode 100644 index 3ec5a8e9d..000000000 --- a/db/routines/vn/functions/client_getSalesPersonCodeByTicket.sql +++ /dev/null @@ -1,23 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`client_getSalesPersonCodeByTicket`(vTicketFk INT) - RETURNS varchar(3) CHARSET utf8mb3 COLLATE utf8mb3_general_ci - DETERMINISTIC -BEGIN -/** - * Dado un id ticket, devuelve su comercial. - * Para más información ir a client_getSalesPerson() - * - * @param vClientFk El id del cliente - * @param vDated Fecha a comprobar - * @return El código del comercial para la fecha dada - **/ - DECLARE vClientFk INT; - DECLARE vDated DATE; - - SELECT clientFk, shipped - INTO vClientFk, vDated - FROM ticket WHERE id = vTicketFk; - - RETURN client_getSalesPersonCode(vClientFk, vDated); -END$$ -DELIMITER ; diff --git a/db/routines/vn/procedures/catalog_componentCalculate.sql b/db/routines/vn/procedures/catalog_componentCalculate.sql index 92fe233c5..2912db558 100644 --- a/db/routines/vn/procedures/catalog_componentCalculate.sql +++ b/db/routines/vn/procedures/catalog_componentCalculate.sql @@ -138,12 +138,12 @@ BEGIN SELECT tcb.warehouseFk, tcb.itemFk, c2.id, - ROUND(base * wm.pricesModifierRate, 3) manaAuto + ROUND(base * dm.pricesModifierRate, 3) manaAuto FROM tmp.ticketComponentBase tcb JOIN `client` c on c.id = vClientFk - JOIN workerMana wm ON c.salesPersonFk = wm.workerFk + JOIN departmentMana dm ON c.salesDepartmentFk = dm.salesDepartmentFk JOIN vn.component c2 ON c2.code = 'autoMana' - WHERE wm.isPricesModifierActivated + WHERE dm.isPricesModifierActivated HAVING manaAuto <> 0; -- Precios especiales diff --git a/db/routines/vn/procedures/clientDebtSpray.sql b/db/routines/vn/procedures/clientDebtSpray.sql index 687c08fe2..a1a48cd1b 100644 --- a/db/routines/vn/procedures/clientDebtSpray.sql +++ b/db/routines/vn/procedures/clientDebtSpray.sql @@ -22,7 +22,7 @@ BEGIN WHERE clientFk = vClientFk; UPDATE vn.client - SET salesPersonFk = NULL + SET salesDepartmentFk = NULL WHERE id = vClientFk; END$$ diff --git a/db/routines/vn/procedures/clientGreugeSpray.sql b/db/routines/vn/procedures/clientGreugeSpray.sql index c337e2dd3..70a5e4105 100644 --- a/db/routines/vn/procedures/clientGreugeSpray.sql +++ b/db/routines/vn/procedures/clientGreugeSpray.sql @@ -1,7 +1,11 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`clientGreugeSpray`(IN vClientFk INT, IN onlyForHisOwner BOOL, IN vWorkerCode VARCHAR(3), IN vWithMana BOOLEAN) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`clientGreugeSpray`( + IN vClientFk INT, + IN vIsOnlyForHisOwner BOOL, + IN vDepartmentCode VARCHAR(45), + IN vWithMana BOOLEAN +) BEGIN - DECLARE vGreuge DECIMAL(10,2); DECLARE vOwner INT; DECLARE vTotalSale INT; @@ -9,65 +13,62 @@ BEGIN DECLARE vGreugeTypeMana INT DEFAULT 3;-- Maná DECLARE vMana DECIMAL(10,2); - SELECT vn.clientGetMana(vClientFk) INTO vMana; + SELECT clientGetMana(vClientFk) INTO vMana; IF vWithMana AND vMana THEN - - INSERT INTO vn.greuge( clientFk, - description, - amount, - shipped, - greugeTypeFk) - VALUES( vClientFk, - 'Desasignación', - -1 * vMana, - util.VN_CURDATE(), - vGreugeTypeMana); - + INSERT INTO greuge + SET clientFk = vClientFk, + description = 'Desasignación', + amount = - vMana, + shipped = util.VN_CURDATE(), + greugeTypeFk = vGreugeTypeMana; END IF; - SELECT sum(amount) INTO vGreuge - FROM vn.greuge + SELECT SUM(amount) INTO vGreuge + FROM greuge WHERE clientFk = vClientFk; - IF vGreuge != 0 THEN - - IF LENGTH(vWorkerCode) = 0 THEN - - SELECT salesPersonFk INTO vOwner - FROM vn.client + IF vGreuge THEN + IF LENGTH(vDepartmentCode) THEN + SELECT salesDepartmentFk INTO vOwner + FROM client WHERE id = vClientFk; - ELSE - SELECT id INTO vOwner - FROM vn.worker - WHERE code = vWorkerCode COLLATE utf8_general_ci; - + FROM department + WHERE code = vDepartmentCode; END IF; - DROP TEMPORARY TABLE IF EXISTS tmp.clientList; - CREATE TEMPORARY TABLE tmp.clientList - SELECT DISTINCT t.clientFk, floor(cr.yearSale / 12) monthSale + IF vOwner IS NULL THEN + CALL util.throw('The department is incorrect'); + END IF; + + INSERT INTO greuge(clientFk, description, amount, shipped, greugeTypeFk) + WITH greuges AS( + SELECT DISTINCT t.clientFk, FLOOR(cr.yearSale / 12) monthSale FROM vn.ticket t JOIN vn.client c ON c.id = t.clientFk - JOIN vn.workerMana wm ON wm.workerFk = c.salesPersonFk + JOIN vn.departmentMana dm ON dm.salesDepartmentFk = c.salesDepartmentFk JOIN vn.claimRatio cr ON cr.clientFk = c.id - WHERE wm.workerFk = IF(onlyForHisOwner, vOwner, wm.workerFk) - AND t.shipped >= TIMESTAMPADD(MONTH,-1,util.VN_CURDATE()) - AND c.id != vClientFk - HAVING monthSale > 100; - - SELECT SUM(monthSale) INTO vTotalSale - FROM tmp.clientList; - - INSERT INTO vn.greuge(clientFk, description, amount, shipped, greugeTypeFk) - SELECT clientFk, CONCAT('Cliente: ',vClientFk), vGreuge * monthSale / vTotalSale, util.VN_CURDATE(), vGreugeTypeFk - FROM tmp.clientList + WHERE dm.salesDepartmentFk = IF(vIsOnlyForHisOwner, vOwner, dm.salesDepartmentFk) + AND t.shipped >= util.VN_CURDATE() - INTERVAL 1 MONTH + AND c.id <> vClientFk + HAVING monthSale > 100 + ), totalGreuge AS( + SELECT SUM(monthSale) totalSale FROM greuges + )SELECT g.clientFk, + CONCAT('Cliente: ', vClientFk), + vGreuge * g.monthSale / tgtotalSale, + util.VN_CURDATE(), + vGreugeTypeFk + FROM greuges g + JOIN totalGreuge tg UNION ALL - SELECT vClientFk, 'Reparto greuge', -vGreuge, util.VN_CURDATE(), vGreugeTypeFk; - + SELECT vClientFk, + 'Reparto greuge', + -vGreuge, + util.VN_CURDATE(), + vGreugeTypeFk; END IF; - END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/clientRemoveWorker.sql b/db/routines/vn/procedures/clientRemoveWorker.sql index 15d247c67..b4d6e3e81 100644 --- a/db/routines/vn/procedures/clientRemoveWorker.sql +++ b/db/routines/vn/procedures/clientRemoveWorker.sql @@ -8,7 +8,7 @@ BEGIN FROM tmp.clientGetDebt c LEFT JOIN clientRisk r ON r.clientFk = c.clientFk GROUP BY c.clientFk - HAVING SUM(IFNULL(r.amount,0)) = 0; + HAVING SUM(IFNULL(r.amount, 0)) = 0; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; @@ -16,22 +16,26 @@ BEGIN CREATE TEMPORARY TABLE tmp.clientGetDebt SELECT cd.clientFk FROM bs.clientDied cd - LEFT JOIN clientProtected cp ON cp.clientFk = cd.clientFk - JOIN client c ON c.id = cd.clientFk - JOIN province p ON p.id = c.provinceFk - LEFT JOIN autonomy a ON a.id = p.autonomyFk - JOIN country co ON co.id = p.countryFk - WHERE cd.warning = 'third' - AND cp.clientFk IS NULL - AND co.code NOT IN ('PT') - AND a.name <> 'Canarias' - AND c.salesPersonFk IS NOT NULL; + LEFT JOIN clientProtected cp ON cp.clientFk = cd.clientFk + JOIN client c ON c.id = cd.clientFk + JOIN province p ON p.id = c.provinceFk + LEFT JOIN autonomy a ON a.id = p.autonomyFk + JOIN country co ON co.id = p.countryFk + WHERE cd.warning = 'third' + AND cp.clientFk IS NULL + AND co.code NOT IN ('PT') + AND a.name <> 'Canarias' + AND c.salesDepartmentFk IS NOT NULL; OPEN rs; FETCH rs INTO vClientFk; WHILE NOT vDone DO CALL vn.clientGreugeSpray(vClientFk, TRUE, '',TRUE); - UPDATE vn.client SET salesPersonFk = NULL WHERE id = vClientFk; + + UPDATE vn.client + SET salesDepartmentFk = NULL + WHERE id = vClientFk; + FETCH rs INTO vClientFk; END WHILE; CLOSE rs; diff --git a/db/routines/vn/procedures/client_unassignSalesPerson.sql b/db/routines/vn/procedures/client_unassignSalesDepartment.sql similarity index 82% rename from db/routines/vn/procedures/client_unassignSalesPerson.sql rename to db/routines/vn/procedures/client_unassignSalesDepartment.sql index 8773104ca..0ac6e2923 100644 --- a/db/routines/vn/procedures/client_unassignSalesPerson.sql +++ b/db/routines/vn/procedures/client_unassignSalesDepartment.sql @@ -1,8 +1,8 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`client_unassignSalesPerson`() +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`client_unassignSalesDepartment`() BEGIN /** - * Elimina la asignación de salesPersonFk de la ficha del clientes + * Elimina la asignación de salesDepartmentFk de la ficha del clientes * que no han realizado una compra en los últimos 3 meses y reparte * su greuge entre el resto de clientes */ @@ -22,16 +22,15 @@ BEGIN FROM bs.clientDied cd JOIN client c ON c.id = cd.clientFk LEFT JOIN clientProtected cp ON cp.clientFk = c.id - LEFT JOIN salesPersonProtected sp ON sp.salesPersonFk = c.salesPersonFk + LEFT JOIN salesDepartmentProtected sd ON sp.salesDepartmentFk = c.salesDepartmentFk JOIN province p ON p.id = c.provinceFk LEFT JOIN autonomy a ON a.id = p.autonomyFk JOIN country co ON co.id = p.countryFk JOIN bs.clientDiedPeriod cdp ON cdp.countryFk = co.id WHERE cd.warning = 'third' AND cp.clientFk IS NULL - AND sp.salesPersonFk IS NULL AND a.name <> 'Canarias' - AND c.salesPersonFk IS NOT NULL; + AND c.salesDepartmentFk IS NOT NULL; OPEN vCursor; l: LOOP @@ -42,7 +41,7 @@ BEGIN END IF; CALL clientGreugeSpray(vClientFk, TRUE, '', TRUE); UPDATE client - SET salesPersonFk = NULL + SET salesDepartmentFk = NULL WHERE id = vClientFk; END LOOP; CLOSE vCursor; diff --git a/db/routines/vn/procedures/collection_getTickets.sql b/db/routines/vn/procedures/collection_getTickets.sql index 7ecff571a..9b3e0b3d4 100644 --- a/db/routines/vn/procedures/collection_getTickets.sql +++ b/db/routines/vn/procedures/collection_getTickets.sql @@ -1,10 +1,13 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_getTickets`(vParamFk INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_getTickets`( + vParamFk INT +) BEGIN /** * Selecciona los tickets de una colección/ticket/sectorCollection * @param vParamFk ticketFk/collectionFk/sectorCollection - * @return Retorna ticketFk, level, agencyName, warehouseFk, salesPersonFk, observation + * @return Retorna (ticketFk, level, agencyName, warehouseFk, observation, + * rgb, salesDepartmentFk) */ DECLARE vItemPackingTypeFk VARCHAR(1); DECLARE vYesterday DATE; @@ -29,7 +32,7 @@ BEGIN IF(!(vItemPackingTypeFk <=> 'V'), cc.code, CONCAT(SUBSTRING('ABCDEFGH', tc.wagon, 1), '-', tc.`level`)) `level`, am.name agencyName, t.warehouseFk, - w.id salesPersonFk, + c.salesDepartmentFk, IFNULL(ob.description,'') observaciones, cc.rgb FROM vn.ticket t @@ -51,7 +54,7 @@ BEGIN IF(NOT(vItemPackingTypeFk <=> 'V'), cc.code, CONCAT(SUBSTRING('ABCDEFGH', tc.wagon, 1), '-', tc.`level`)) `level`, am.name agencyName, t.warehouseFk, - w.id salesPersonFk, + c.salesDepartmentFk, ob.description, IF(NOT (vItemPackingTypeFk <=> 'V'), cc.rgb, NULL) `rgb` FROM vn.ticket t @@ -72,7 +75,7 @@ BEGIN NULL `level`, am.name agencyName, t.warehouseFk, - c.salesPersonFk, + c.salesDepartmentFk, ob.description, NULL `rgb` FROM vn.sectorCollection sc diff --git a/db/routines/vn/procedures/itemSale_byWeek.sql b/db/routines/vn/procedures/itemSale_byWeek.sql index bc43b7b16..40585d426 100644 --- a/db/routines/vn/procedures/itemSale_byWeek.sql +++ b/db/routines/vn/procedures/itemSale_byWeek.sql @@ -14,7 +14,6 @@ BEGIN s.ticketFk, t.nickname client, am.name agencyName, - wk.code salesPerson, s.itemFk, IFNULL(CONCAT(ig.longName,' ',ig.`size`,' ',ig.subName), s.concept) AS concept, s.quantity, @@ -24,14 +23,15 @@ BEGIN tls.name stateName, sb.buyFk, s.id saleFk, - wk.id salesPersonFk + d.id salesDepartmentFk, + d.name salesDepartment FROM sale s JOIN ticket t ON t.id = s.ticketFk JOIN warehouse w ON w.id = t.warehouseFk JOIN address a ON a.id = t.addressFk LEFT JOIN agencyMode am ON am.id = t.agencyModeFk JOIN `client` c ON c.id = a.clientFk - LEFT JOIN worker wk ON wk.id = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN ticketLastState tls ON tls.ticketFk = t.id LEFT JOIN saleBuy sb ON sb.saleFk = s.id LEFT JOIN buy b ON b.id = sb.buyFk diff --git a/db/routines/vn/procedures/manaSpellersRequery.sql b/db/routines/vn/procedures/manaSpellersRequery.sql deleted file mode 100644 index 30b91b4f1..000000000 --- a/db/routines/vn/procedures/manaSpellersRequery.sql +++ /dev/null @@ -1,36 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`manaSpellersRequery`(vWorkerFk INTEGER) -`whole_proc`: -BEGIN -/** - * Guarda en workerMana el mana consumido por un trabajador - * - * @param vWorkerFk Id Trabajador - */ - DECLARE vWorkerIsExcluded BOOLEAN; - - SELECT COUNT(*) INTO vWorkerIsExcluded - FROM workerManaExcluded - WHERE workerFk = vWorkerFk; - - IF vWorkerIsExcluded THEN - LEAVE whole_proc; - END IF; - - CREATE OR REPLACE TEMPORARY TABLE tmp.client - SELECT id - FROM client - WHERE salesPersonFk = vWorkerFk; - - CALL client_getMana(); - - INSERT INTO workerMana (workerFk, amount) - SELECT vWorkerFk, sum(mana) - FROM tmp.clientMana - ON DUPLICATE KEY UPDATE amount = VALUES(amount); - - DROP TEMPORARY TABLE - tmp.client, - tmp.clientMana; -END$$ -DELIMITER ; diff --git a/db/routines/vn/procedures/manaSpellers_requery.sql b/db/routines/vn/procedures/manaSpellers_requery.sql new file mode 100644 index 000000000..cb9f032b9 --- /dev/null +++ b/db/routines/vn/procedures/manaSpellers_requery.sql @@ -0,0 +1,38 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`manaSpellers_requery`( + vSalesDepartmentFk INTEGER +) +`whole_proc`: +BEGIN +/** + * Guarda en departmentMana el mana consumido por un departamento + * + * @param vSalesDepartmentFk Id department + */ + DECLARE vIsDepartmentExcluded BOOLEAN; + + SELECT COUNT(*) INTO vIsDepartmentExcluded + FROM departmentManaExcluded + WHERE salesDepartmentFk = vSalesDepartmentFk; + + IF vIsDepartmentExcluded THEN + LEAVE whole_proc; + END IF; + + CREATE OR REPLACE TEMPORARY TABLE tmp.client + SELECT id + FROM client + WHERE salesDepartmentFk = vSalesDepartmentFk; + + CALL client_getMana(); + + INSERT INTO departmentMana (salesDepartmentFk, amount) + SELECT vSalesDepartmentFk, SUM(mana) + FROM tmp.clientMana + ON DUPLICATE KEY UPDATE amount = VALUES(amount); + + DROP TEMPORARY TABLE + tmp.client, + tmp.clientMana; +END$$ +DELIMITER ; diff --git a/db/routines/vn/procedures/productionControl.sql b/db/routines/vn/procedures/productionControl.sql index dad46393d..53102e69e 100644 --- a/db/routines/vn/procedures/productionControl.sql +++ b/db/routines/vn/procedures/productionControl.sql @@ -70,7 +70,7 @@ proc: BEGIN IFNULL(tls.state,2) state, w.code workerCode, DATE(t.shipped) shipped, - wk.code salesPersonCode, + d.code salesDepartmentCode, p.id provinceFk, tls.productionOrder, IFNULL(tls.alertLevel, al.id) alertLevel, @@ -91,7 +91,7 @@ proc: BEGIN LEFT JOIN ticketStateToday tst ON tst.ticketFk = t.id LEFT JOIN `state` st ON st.id = tst.state LEFT JOIN client c ON c.id = t.clientFk - LEFT JOIN worker wk ON wk.id = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk JOIN address a ON a.id = t.addressFk LEFT JOIN province p ON p.id = a.provinceFk JOIN agencyMode am ON am.id = t.agencyModeFk diff --git a/db/routines/vn/procedures/route_getTickets.sql b/db/routines/vn/procedures/route_getTickets.sql index 55b08208f..20b6d8464 100644 --- a/db/routines/vn/procedures/route_getTickets.sql +++ b/db/routines/vn/procedures/route_getTickets.sql @@ -8,7 +8,7 @@ BEGIN * @param vRouteFk * @select Información de los tickets */ -SELECT t.id Id, + SELECT t.id Id, t.clientFk Client, a.id Address, a.nickname ClientName, @@ -23,7 +23,7 @@ SELECT t.id Id, a.mobile AddressMobile, d.longitude Longitude, d.latitude Latitude, - wm.mediaValue SalePersonPhone, + CONCAT_WS(' - ', 'adfa', de.pbxQueue ) salesDepartmentPhone, tob.description Note, t.isSigned Signed, t.priority, @@ -31,7 +31,8 @@ SELECT t.id Id, FROM ticket t JOIN client c ON t.clientFk = c.id JOIN address a ON t.addressFk = a.id - LEFT JOIN workerMedia wm ON wm.workerFk = c.salesPersonFk + LEFT JOIN vn.department de ON de.id = c.salesDepartmentFk + LEFT JOIN vn.company co ON co.`code` = 'VNL' LEFT JOIN ( SELECT t.addressFk, MAX(d.ticketFk) lastTicketFk FROM ticket t diff --git a/db/routines/vn/procedures/sectorCollection_getSale.sql b/db/routines/vn/procedures/sectorCollection_getSale.sql index e1636895b..2f1dd2cae 100644 --- a/db/routines/vn/procedures/sectorCollection_getSale.sql +++ b/db/routines/vn/procedures/sectorCollection_getSale.sql @@ -15,7 +15,7 @@ BEGIN w.code workerCode, sgd.saleFk, iss.quantity pickedQuantity, - c.salesPersonFk + c.salesDepartmentFk FROM vn.sale s JOIN item i ON i.id = s.itemFk JOIN saleGroupDetail sgd ON sgd.saleFk = s.id diff --git a/db/routines/vn/procedures/ticket_canAdvance.sql b/db/routines/vn/procedures/ticket_canAdvance.sql index d1ca7b5e2..81db37c9c 100644 --- a/db/routines/vn/procedures/ticket_canAdvance.sql +++ b/db/routines/vn/procedures/ticket_canAdvance.sql @@ -1,5 +1,9 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_canAdvance`(vDateFuture DATE, vDateToAdvance DATE, vWarehouseFk INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_canAdvance`( + vDateFuture DATE, + vDateToAdvance DATE, + vWarehouseFk INT +) BEGIN /** * Devuelve los tickets y la cantidad de lineas de venta que se pueden adelantar. @@ -26,7 +30,7 @@ BEGIN origin.futureState, origin.futureIpt, dest.ipt, - origin.workerFk, + origin.departmentFk, origin.futureLiters, origin.futureLines, dest.shipped, @@ -55,7 +59,7 @@ BEGIN FROM ( SELECT s.ticketFk, - c.salesPersonFk workerFk, + c.salesDepartmentFk departmentFk, t.shipped, t.totalWithVat, st.name futureState, diff --git a/db/routines/vn/procedures/ticket_cloneWeekly.sql b/db/routines/vn/procedures/ticket_cloneWeekly.sql index fd45dc9fa..941f04383 100644 --- a/db/routines/vn/procedures/ticket_cloneWeekly.sql +++ b/db/routines/vn/procedures/ticket_cloneWeekly.sql @@ -15,21 +15,21 @@ BEGIN DECLARE vAgencyModeFk INT; DECLARE vNewTicket INT; DECLARE vYear INT; - DECLARE vSalesPersonFK INT; + DECLARE vObservationTypeFkForSalesPerson INT; DECLARE vItemPicker INT; DECLARE rsTicket CURSOR FOR - SELECT tt.ticketFk, - t.clientFk, - t.warehouseFk, - t.companyFk, - t.addressFk, - tt.agencyModeFk, - ti.dated - FROM ticketWeekly tt - JOIN ticket t ON tt.ticketFk = t.id - JOIN tmp.time ti - WHERE WEEKDAY(ti.dated) = tt.weekDay; + SELECT tt.ticketFk, + t.clientFk, + t.warehouseFk, + t.companyFk, + t.addressFk, + tt.agencyModeFk, + ti.dated + FROM ticketWeekly tt + JOIN ticket t ON tt.ticketFk = t.id + JOIN tmp.time ti + WHERE WEEKDAY(ti.dated) = tt.weekDay; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vIsDone = TRUE; @@ -38,7 +38,7 @@ BEGIN OPEN rsTicket; myLoop: LOOP BEGIN - DECLARE vSalesPersonEmail VARCHAR(150); + DECLARE vSalesDepartmentEmail VARCHAR(150); DECLARE vIsDuplicateMail BOOL; DECLARE vSubject VARCHAR(150); DECLARE vMessage TEXT; @@ -154,7 +154,7 @@ BEGIN FROM ticketRequest WHERE ticketFk =vTicketFk; - SELECT id INTO vSalesPersonFK + SELECT id INTO vObservationTypeFkForSalesPerson FROM observationType WHERE code = 'salesPerson'; @@ -168,7 +168,7 @@ BEGIN description) VALUES( vNewTicket, - vSalesPersonFK, + vObservationTypeFkForSalesPerson, CONCAT('turno desde ticket: ',vTicketFk)) ON DUPLICATE KEY UPDATE description = CONCAT(ticketObservation.description,VALUES(description),' '); @@ -185,9 +185,9 @@ BEGIN IF (vLanding IS NULL) THEN - SELECT e.email INTO vSalesPersonEmail + SELECT d.notificationEmail INTO vSalesDepartmentEmail FROM client c - JOIN account.emailUser e ON e.userFk = c.salesPersonFk + LEFT JOIN department d ON d.id = c.saleDepartmentFk WHERE c.id = vClientFk; SET vSubject = CONCAT('Turnos - No se ha podido clonar correctamente el ticket ', @@ -199,11 +199,11 @@ BEGIN SELECT COUNT(*) INTO vIsDuplicateMail FROM mail - WHERE receiver = vSalesPersonEmail + WHERE receiver = vSalesDepartmentEmail AND subject = vSubject; IF NOT vIsDuplicateMail THEN - CALL mail_insert(vSalesPersonEmail, NULL, vSubject, vMessage); + CALL mail_insert(vSalesDepartmentEmail, NULL, vSubject, vMessage); END IF; CALL ticket_setState(vNewTicket, 'FIXING'); ELSE diff --git a/db/routines/vn/procedures/workerDisable.sql b/db/routines/vn/procedures/workerDisable.sql index 04612a6dc..03ebed062 100644 --- a/db/routines/vn/procedures/workerDisable.sql +++ b/db/routines/vn/procedures/workerDisable.sql @@ -1,33 +1,28 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerDisable`(vUserId int) -mainLabel:BEGIN +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`workerDisable`(vUserFk INT) +l:BEGIN - IF (SELECT COUNT(*) FROM workerDisableExcluded WHERE workerFk = vUserId AND (dated > util.VN_CURDATE() OR dated IS NULL)) > 0 THEN - LEAVE mainLabel; + IF (SELECT COUNT(*) + FROM workerDisableExcluded + WHERE workerFk = vUserFk + AND (dated > util.VN_CURDATE() OR dated IS NULL)) > 0 THEN + LEAVE l; END IF; - DELETE cp FROM clientProtected cp - JOIN client c ON c.id = cp.clientFk - WHERE c.salesPersonFk = vUserId; - - DELETE FROM account.account - WHERE id = vUserId; + DELETE FROM account.account WHERE id = vUserFk; UPDATE account.user SET role = 2 - WHERE id = vUserId; + WHERE id = vUserFk; - DELETE FROM pbx.sip - WHERE user_id = vUserId; + DELETE FROM pbx.sip WHERE user_id = vUserFk; UPDATE `client` c - JOIN payMethod p ON p.name = 'CONTADO' - SET c.credit = 0, c.payMethodFk = p.id, hasCoreVnl = FALSE - WHERE c.id = vUserId; - - UPDATE `client` c - SET c.salesPersonFk = null - WHERE c.salesPersonFk = vUserId; + JOIN payMethod p ON p.name = 'CONTADO' + SET c.credit = 0, + c.payMethodFk = p.id, + hasCoreVnl = FALSE + WHERE c.id = vUserFk; UPDATE locker l SET l.workerFk = NULL diff --git a/db/routines/vn/procedures/zone_getAddresses.sql b/db/routines/vn/procedures/zone_getAddresses.sql index ce7b0204e..b3195c435 100644 --- a/db/routines/vn/procedures/zone_getAddresses.sql +++ b/db/routines/vn/procedures/zone_getAddresses.sql @@ -29,13 +29,13 @@ BEGIN c.name, c.phone, bt.description, - c.salesPersonFk, - u.name username, + c.salesDepartmentFk, + d.name departmentName, aai.invoiced, cnb.lastShipped FROM vn.client c JOIN notHasTicket ON notHasTicket.id = c.id - LEFT JOIN account.`user` u ON u.id = c.salesPersonFk + LEFT JOIN vn.department d ON d.id = c.salesDepartmentFk JOIN vn.`address` a ON a.clientFk = c.id JOIN vn.postCode pc ON pc.code = a.postalCode JOIN vn.town t ON t.id = pc.townFk AND t.provinceFk = a.provinceFk diff --git a/db/routines/vn/triggers/client_beforeInsert.sql b/db/routines/vn/triggers/client_beforeInsert.sql index 75b69c7dd..9e3189362 100644 --- a/db/routines/vn/triggers/client_beforeInsert.sql +++ b/db/routines/vn/triggers/client_beforeInsert.sql @@ -16,6 +16,6 @@ BEGIN SET NEW.accountingAccount = 4300000000 + NEW.id; - SET NEW.lastSalesPersonFk = NEW.salesPersonFk; + SET NEW.lastSalesDepartmentFk = NEW.salesDepartmentFk; END$$ DELIMITER ; diff --git a/db/routines/vn/triggers/client_beforeUpdate.sql b/db/routines/vn/triggers/client_beforeUpdate.sql index 914ae5b8a..4d1580144 100644 --- a/db/routines/vn/triggers/client_beforeUpdate.sql +++ b/db/routines/vn/triggers/client_beforeUpdate.sql @@ -45,18 +45,16 @@ BEGIN IF vText IS NOT NULL THEN INSERT INTO mail(receiver, replyTo, `subject`, body) - SELECT - CONCAT(IF(ac.id,u.name, 'jgallego'), '@verdnatura.es'), + SELECT IFNULL(d.notificationEmail, 'jgallego@verdnatura.es'), 'administracion@verdnatura.es', CONCAT('Cliente ', NEW.id), CONCAT('Recibida la documentación: ', vText) - FROM worker w - LEFT JOIN account.user u ON w.id = u.id AND u.active - LEFT JOIN account.account ac ON ac.id = u.id - WHERE w.id = NEW.salesPersonFk; + FROM department d + WHERE d.id = NEW.salesDepartmentFk; END IF; - IF NEW.salespersonFk IS NULL AND OLD.salespersonFk IS NOT NULL THEN + + IF NEW.salesDepartmentFk IS NULL AND OLD.salesDepartmentFk IS NOT NULL THEN IF (SELECT COUNT(clientFk) FROM clientProtected WHERE clientFk = NEW.id @@ -65,8 +63,9 @@ BEGIN END IF; END IF; - IF NOT (NEW.salesPersonFk <=> OLD.salesPersonFk) THEN - SET NEW.lastSalesPersonFk = IFNULL(NEW.salesPersonFk, OLD.salesPersonFk); + + IF NOT (NEW.salesDepartmentFk <=> OLD.salesDepartmentFk) THEN + SET NEW.lastSalesDepartmentFk = IFNULL(NEW.salesDepartmentFk, OLD.salesDepartmentFk); END IF; IF NOT (NEW.businessTypeFk <=> OLD.businessTypeFk) AND (NEW.businessTypeFk = 'individual' OR OLD.businessTypeFk = 'individual') THEN diff --git a/db/routines/vn/views/newBornSales.sql b/db/routines/vn/views/newBornSales.sql index d34eff4ef..662eebd20 100644 --- a/db/routines/vn/views/newBornSales.sql +++ b/db/routines/vn/views/newBornSales.sql @@ -1,26 +1,16 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vn`.`newBornSales` -AS SELECT `v`.`importe` AS `amount`, - `v`.`Id_Cliente` AS `clientFk`, - `c`.`salesPersonFk` AS `userFk`, - `v`.`fecha` AS `dated`, - `cn`.`firstShipped` AS `firstShipped` -FROM ( - ( - ( - ( - `bs`.`clientNewBorn` `cn` - JOIN `bs`.`ventas` `v` ON( - `cn`.`firstShipped` + INTERVAL 1 year > `v`.`fecha` - AND `v`.`Id_Cliente` = `cn`.`clientFk` - ) - ) - JOIN `vn`.`client` `c` ON(`c`.`id` = `v`.`Id_Cliente`) - ) - JOIN `account`.`user` `u` ON(`u`.`id` = `c`.`salesPersonFk`) - ) - JOIN `account`.`role` `r` ON(`r`.`id` = `u`.`role`) - ) -WHERE `r`.`name` = 'salesPerson' - AND `u`.`name` NOT IN ('ismaelalcolea', 'ruben') +AS SELECT + `s`.`amount` AS `amount`, + `s`.`clientFk` AS `clientFk`, + `c`.`salesDepartmentFk` AS `salesDepartmentFk`, + `s`.`dated` AS `dated`, + `cn`.`firstShipped` AS `firstShipped` +FROM + ((`bs`.`clientNewBorn` `cn` +JOIN `bs`.`sale` `s` ON + (`cn`.`firstShipped` + interval 1 year > `s`.`dated` + and `s`.`clientFk` = `cn`.`clientFk`)) +JOIN `vn`.`client` `c` ON + (`c`.`id` = `s`.`clientFk`)); \ No newline at end of file diff --git a/db/routines/vn2008/views/Clientes.sql b/db/routines/vn2008/views/Clientes.sql index 153d875bc..b44acf52b 100644 --- a/db/routines/vn2008/views/Clientes.sql +++ b/db/routines/vn2008/views/Clientes.sql @@ -40,7 +40,7 @@ AS SELECT `c`.`id` AS `id_cliente`, `c`.`creditInsurance` AS `creditInsurance`, `c`.`isCreatedAsServed` AS `isCreatedAsServed`, `c`.`hasInvoiceSimplified` AS `hasInvoiceSimplified`, - `c`.`salesPersonFk` AS `Id_Trabajador`, + `c`.`salesDepartmentFk` AS `salesDepartmentFk`, `c`.`isVies` AS `vies`, `c`.`bankEntityFk` AS `bankEntityFk`, `c`.`typeFk` AS `typeFk` diff --git a/db/versions/11032-blackRose/00-firstScript.sql b/db/versions/11032-blackRose/00-firstScript.sql new file mode 100644 index 000000000..a01ca1780 --- /dev/null +++ b/db/versions/11032-blackRose/00-firstScript.sql @@ -0,0 +1,126 @@ +ALTER TABLE vn.client + ADD IF NOT EXISTS salesDepartmentFk INT(11) DEFAULT NULL NULL; + +ALTER TABLE vn.client + ADD IF NOT EXISTS lastSalesDepartmentFk INT(11) DEFAULT NULL NULL; + +ALTER TABLE vn.client + ADD CONSTRAINT client_department_FK FOREIGN KEY IF NOT EXISTS (salesDepartmentFk) + REFERENCES vn.department(id) ON DELETE RESTRICT ON UPDATE CASCADE; + +ALTER TABLE vn.client + ADD CONSTRAINT client_lastDepartment_FK FOREIGN KEY IF NOT EXISTS (lastSalesDepartmentFk) + REFERENCES vn.department(id) ON DELETE RESTRICT ON UPDATE CASCADE; + +UPDATE vn.client c + JOIN vn.worker w ON w.id = c.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk + SET c.salesDepartmentFk = b.departmentFk; + +UPDATE vn.client c + JOIN vn.worker w ON w.id = c.lastSalesPersonFk + JOIN vn.business b ON b.id = w.businessFk + SET c.lastSalesDepartmentFk = b.departmentFk; + +DROP TABLE IF EXISTS vn.departmentMana; +CREATE OR REPLACE TABLE `vn`.`departmentMana` ( + `salesDepartmentFk` int(10) NOT NULL, + `size` int(11) NOT NULL DEFAULT 300, + `amount` int(11) NOT NULL DEFAULT 0, + `pricesModifierRate` double NOT NULL DEFAULT 0, + `isPricesModifierActivated` tinyint(1) NOT NULL DEFAULT 1, + PRIMARY KEY (`salesDepartmentFk`), + KEY `departmentMana_idx` (`salesDepartmentFk`), + CONSTRAINT `departmentMana_salesDepartment_FK` FOREIGN KEY (`salesDepartmentFk`) + REFERENCES `vn`.`department` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; + +DROP TABLE IF EXISTS bs.salesByClientDepartment; +CREATE OR REPLACE TABLE `bs`.`salesByClientDepartment` ( + `dated` date NOT NULL DEFAULT '0000-00-00', + `salesDepartmentFk` int(10) DEFAULT NULL, + `clientFk` int(11) NOT NULL, + `amount` decimal(10,3) NOT NULL DEFAULT 0.000, + `equalizationTax` decimal(10,3) NOT NULL DEFAULT 0.000, + `amountNewBorn` decimal(10,3) NOT NULL DEFAULT 0.000, + PRIMARY KEY (`dated`,`clientFk`), + KEY `salesByClientDepartment_clientFk` (`clientFk`), + KEY `salesByClientDepartment_salesDepartmentFk` (`salesDepartmentFk`), + KEY `salesByClientDepartment_dated` (`dated`,`clientFk`,`amount`), + CONSTRAINT `salesByClientDepartment_clientFk_FK` + FOREIGN KEY (`clientFk`) REFERENCES `vn`.`client` (`id`) ON UPDATE CASCADE, + CONSTRAINT `salesByClientDepartment_department_FK` + FOREIGN KEY (`salesDepartmentFk`) REFERENCES `vn`.`department` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci COMMENT='Ventas diarias por cliente y departamento'; + +INSERT INTO bs.salesByClientDepartment( + dated, + salesDepartmentFk, + clientFk, + amount, + equalizationTax, + amountNewBorn) + SELECT ss.dated, b.departmentFk, ss.clientFk, ss.amount, ss.equalizationTax, ss.amountNewBorn + FROM bs.salesByclientSalesPerson ss + JOIN vn.worker w ON w.id = ss.salesPersonFk + JOIN vn.business b ON b.id = w.businessFk; + +DROP TABLE IF EXISTS `vn`.`salesDepartmentProtected`; + +CREATE OR REPLACE TABLE `vn`.`salesDepartmentProtected` ( + `salesDepartmentFk` int(10) NOT NULL, + PRIMARY KEY (`salesDepartmentFk`), + CONSTRAINT `salesDepartmentProtected_FK` FOREIGN KEY (`salesDepartmentFk`) REFERENCES `vn`.`department` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci + COMMENT='Lista de departamentos comerciales que no se desasignarán automáticamente sus clientes'; + + +UPDATE vn.observationType + SET description='Dto. Comercial',code='salesDepartment' + WHERE code = 'salesPerson'; + +DROP TABLE IF EXISTS `bs`.`portfolio`; + +CREATE OR REPLACE TABLE `bs`.`portfolio` ( + `salesDepartmentFk` int(10) NOT NULL, + `yeared` int(4) NOT NULL, + `monthed` int(2) NOT NULL, + `amount` decimal(10,2) DEFAULT NULL, + PRIMARY KEY (`salesDepartmentFk`,`yeared`,`monthed`), + KEY `portfolio_salesDepartmentFk` (`salesDepartmentFk`), + CONSTRAINT `portfolio_salesDepartment_department_FK` + FOREIGN KEY (`salesDepartmentFk`) REFERENCES `vn`.`department` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; + + +CREATE OR REPLACE TABLE `bs`.`salesDepartmentEvolution` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `dated` date NOT NULL DEFAULT '0000-00-00', + `salesDepartmentFk` int(10) DEFAULT NULL, + `amount` decimal(10,3) NOT NULL DEFAULT 0.000, + `equalizationTax` decimal(10,3) NOT NULL DEFAULT 0.000, + `amountNewBorn` decimal(10,3) NOT NULL DEFAULT 0.000, + PRIMARY KEY (`id`), + KEY `salesDepartmentEvolution_salesDepartment` (`salesDepartmentFk`), + CONSTRAINT `salesDepartmentEvolution_salesDepartment_department_FK` FOREIGN KEY (`salesDepartmentFk`) + REFERENCES `vn`.`department` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; + +RENAME TABLE vn.salespersonConfig TO vn.salesDepartmentConfig; + +ALTER TABLE vn.company ADD IF NOT EXISTS phone varchar(15) DEFAULT NULL NULL; + + +CREATE OR REPLACE TABLE `vn`.`departmentManaExcluded` ( + `salesDepartmentFk` int(10) NOT NULL, + PRIMARY KEY (`salesDepartmentFk`), + CONSTRAINT `departmentManaExcluded_FK` FOREIGN KEY (`salesDepartmentFk`) + REFERENCES `vn`.`department` (`id`) ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci + COMMENT='Departamentos de venta que tienen que estar excluidos del cálculo del maná'; + + +RENAME TABLE vn.workerManaExcluded TO vn.workerManaExcluded__; +ALTER TABLE vn.workerManaExcluded__ + COMMENT='Usuarios que tienen que estar excluidos del cálculo del maná + @deprecated 2024-07-16'; diff --git a/loopback/locale/es.json b/loopback/locale/es.json index e2be5d013..448292c14 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -19,7 +19,7 @@ "That payment method requires a BIC": "El método de pago seleccionado requiere un BIC", "State cannot be blank": "El estado no puede estar en blanco", "Worker cannot be blank": "El trabajador no puede estar en blanco", - "Cannot change the payment method if no salesperson": "No se puede cambiar la forma de pago si no hay comercial asignado", + "Cannot change the payment method if no salesdepartment": "No se puede cambiar la forma de pago si no hay departamento comercial asignado", "can't be blank": "El campo no puede estar vacío", "Observation type must be unique": "El tipo de observación no puede repetirse", "The credit must be an integer greater than or equal to zero": "The credit must be an integer greater than or equal to zero", diff --git a/loopback/locale/fr.json b/loopback/locale/fr.json index 49584ef0e..d818c4e01 100644 --- a/loopback/locale/fr.json +++ b/loopback/locale/fr.json @@ -19,7 +19,7 @@ "That payment method requires a BIC": "Este método de pagamento requer um BIC", "State cannot be blank": "O estado não pode ficar em branco", "Worker cannot be blank": "O trabalhador não pode ficar em branco", - "Cannot change the payment method if no salesperson": "Não é possível alterar o método de pagamento se não houver vendedor", + "Cannot change the payment method if no salesdepartment": "Impossible de changer le mode de paiement s'il n'y a pas de service commercial", "can't be blank": "Não pode ficar em branco", "Observation type must be unique": "O tipo de observação deve ser único", "The credit must be an integer greater than or equal to zero": "O crédito deve ser um inteiro maior ou igual a zero", diff --git a/loopback/locale/pt.json b/loopback/locale/pt.json index 95c1fff0a..4490f8a05 100644 --- a/loopback/locale/pt.json +++ b/loopback/locale/pt.json @@ -19,7 +19,7 @@ "That payment method requires a BIC": "Esse método de pagamento requer um BIC", "State cannot be blank": "O estado não pode estar em branco", "Worker cannot be blank": "O trabalhador não pode estar em branco", - "Cannot change the payment method if no salesperson": "Não é possível alterar o método de pagamento se não houver vendedor", + "Cannot change the payment method if no salesdepartment": "Não é possível alterar o método de pagamento se não houver departamento de vendas", "can't be blank": "não pode estar em branco", "Observation type must be unique": "O tipo de observação deve ser único", "The credit must be an integer greater than or equal to zero": "O crédito deve ser um número inteiro maior ou igual a zero", diff --git a/modules/claim/back/methods/claim/claimPickupEmail.js b/modules/claim/back/methods/claim/claimPickupEmail.js index 596102a24..d5244981a 100644 --- a/modules/claim/back/methods/claim/claimPickupEmail.js +++ b/modules/claim/back/methods/claim/claimPickupEmail.js @@ -61,7 +61,13 @@ module.exports = Self => { include: { relation: 'client', scope: { - fields: ['name', 'salesPersonFk'] + fields: ['name', 'salesDepartmentFk'], + include: { + relation: 'salesDepartment', + scope: { + fields: ['id', 'chatName'] + } + } } } }); @@ -71,9 +77,9 @@ module.exports = Self => { clientName: claim.client().name, claimUrl: `${url}claim/${args.id}/summary`, }); - const salesPersonId = claim.client().salesPersonFk; - if (salesPersonId) - await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); + const salesDepartment = claim.client().salesDepartment(); + if (salesDepartment) + await models.Chat.sendCheckingPresence(ctx, salesDepartment.chatName, message); const email = new Email('claim-pickup-order', params); diff --git a/modules/claim/back/methods/claim/createFromSales.js b/modules/claim/back/methods/claim/createFromSales.js index 1af479dbb..3b52ec886 100644 --- a/modules/claim/back/methods/claim/createFromSales.js +++ b/modules/claim/back/methods/claim/createFromSales.js @@ -47,10 +47,11 @@ module.exports = Self => { include: { relation: 'client', scope: { + fields: ['id', 'salesDepartmentFk'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } @@ -91,8 +92,8 @@ module.exports = Self => { await Promise.all(promises); - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t('Created claim', { @@ -102,7 +103,7 @@ module.exports = Self => { claimUrl: `${url}claim/${newClaim.id}/summary`, changes: changesMade }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } if (tx) await tx.commit(); diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index f60b6572e..657ba2799 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -51,9 +51,9 @@ module.exports = Self => { http: {source: 'query'} }, { - arg: 'salesPersonFk', + arg: 'salesDepartmetnFk', type: 'number', - description: 'The salesPerson id', + description: 'The salesDepartment id', http: {source: 'query'} }, { @@ -164,8 +164,8 @@ module.exports = Self => { return {'cl.id': {inq: claimIdsByItemFk}}; case 'claimResponsibleFk': return {'cl.id': {inq: claimIdsByClaimResponsibleFk}}; - case 'salesPersonFk': - return {'c.salesPersonFk': value}; + case 'salesDepartmentFk': + return {'c.salesDepartmentFk': value}; case 'attenderFk': return {'cl.workerFk': value}; case 'created': diff --git a/modules/claim/back/methods/claim/getSummary.js b/modules/claim/back/methods/claim/getSummary.js index 2731f1e8f..34151e5c6 100644 --- a/modules/claim/back/methods/claim/getSummary.js +++ b/modules/claim/back/methods/claim/getSummary.js @@ -53,9 +53,9 @@ module.exports = Self => { { relation: 'client', scope: { - fields: ['salesPersonFk', 'name'], + fields: ['salesDepartmentFk', 'name'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { fields: ['id', 'name'] } diff --git a/modules/claim/back/methods/claim/regularizeClaim.js b/modules/claim/back/methods/claim/regularizeClaim.js index a51b114ef..ea1a7a813 100644 --- a/modules/claim/back/methods/claim/regularizeClaim.js +++ b/modules/claim/back/methods/claim/regularizeClaim.js @@ -41,7 +41,7 @@ module.exports = Self => { relation: 'claimDestination', fields: ['addressFk'] }, - where: {claimFk: claimFk} + where: {claimFk} }, myOptions); for (let claimEnd of claimEnds) { @@ -53,8 +53,8 @@ module.exports = Self => { if (addressId) address = await models.Address.findById(addressId, null, myOptions); - const salesPerson = sale.ticket().client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = sale.ticket().client().salesDepartment(); + if (salesDepartment) { const nickname = address && address.nickname || destination.description; const url = await Self.app.models.Url.getUrl(); const message = $t('Sent units from ticket', { @@ -66,7 +66,7 @@ module.exports = Self => { ticketUrl: `${url}ticket/${sale.ticketFk}/sale`, itemUrl: `${url}item/${sale.itemFk}/summary` }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } if (!address) continue; @@ -121,9 +121,9 @@ module.exports = Self => { relation: 'client', scope: { include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } diff --git a/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js b/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js index 55d76ed7a..c3fe7daa6 100644 --- a/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js @@ -39,7 +39,7 @@ describe('claim regularizeClaim()', () => { try { const options = {transaction: tx}; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + spyOn(chatModel, 'send').and.callThrough(); claimEnds = await importTicket(ticketId, claimId, userId, options); @@ -55,8 +55,8 @@ describe('claim regularizeClaim()', () => { expect(trashTicket.addressFk).toEqual(trashAddress); expect(claimBefore.claimStateFk).toEqual(pendentState); expect(claimAfter.claimStateFk).toEqual(resolvedState); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledWith(ctx, 18, 'Trash'); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(4); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es5_equipo', 'Trash'); + expect(chatModel.send).toHaveBeenCalledTimes(4); await tx.rollback(); } catch (e) { @@ -71,7 +71,7 @@ describe('claim regularizeClaim()', () => { try { const options = {transaction: tx}; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + spyOn(chatModel, 'send').and.callThrough(); claimEnds = await importTicket(ticketId, claimId, userId, options); @@ -80,8 +80,8 @@ describe('claim regularizeClaim()', () => { await models.Claim.regularizeClaim(ctx, claimId, options); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledWith(ctx, 18, 'Bueno'); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(4); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es5_equipo', 'Bueno'); + expect(chatModel.send).toHaveBeenCalledTimes(4); await tx.rollback(); } catch (e) { @@ -96,7 +96,7 @@ describe('claim regularizeClaim()', () => { try { const options = {transaction: tx}; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + spyOn(chatModel, 'send').and.callThrough(); claimEnds = await importTicket(ticketId, claimId, userId, options); @@ -105,8 +105,8 @@ describe('claim regularizeClaim()', () => { await models.Claim.regularizeClaim(ctx, claimId, options); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledWith(ctx, 18, 'Bueno'); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(4); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es5_equipo', 'Bueno'); + expect(chatModel.send).toHaveBeenCalledTimes(4); await tx.rollback(); } catch (e) { diff --git a/modules/claim/back/methods/claim/specs/updateClaim.spec.js b/modules/claim/back/methods/claim/specs/updateClaim.spec.js index e1eec59d1..96355edd9 100644 --- a/modules/claim/back/methods/claim/specs/updateClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/updateClaim.spec.js @@ -63,7 +63,7 @@ describe('Update Claim', () => { const newClaim = await app.models.Claim.create(originalData, options); const chatModel = app.models.Chat; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + spyOn(chatModel, 'send').and.callThrough(); const pendingState = claimStatesMap.pending; const ctx = { @@ -83,7 +83,7 @@ describe('Update Claim', () => { let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options); expect(updatedClaim.observation).toEqual(ctx.args.observation); - expect(chatModel.sendCheckingPresence).toHaveBeenCalled(); + expect(chatModel.send).toHaveBeenCalled(); await tx.rollback(); } catch (e) { @@ -101,7 +101,7 @@ describe('Update Claim', () => { const newClaim = await app.models.Claim.create(originalData, options); const chatModel = app.models.Chat; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + spyOn(chatModel, 'send').and.callThrough(); const canceledState = claimStatesMap.canceled; const ctx = { @@ -121,7 +121,7 @@ describe('Update Claim', () => { let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options); expect(updatedClaim.observation).toEqual(ctx.args.observation); - expect(chatModel.sendCheckingPresence).toHaveBeenCalledTimes(2); + expect(chatModel.send).toHaveBeenCalledTimes(2); await tx.rollback(); } catch (e) { @@ -139,7 +139,7 @@ describe('Update Claim', () => { const newClaim = await app.models.Claim.create(originalData, options); const chatModel = app.models.Chat; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + spyOn(chatModel, 'send').and.callThrough(); const claimManagerId = 72; const ctx = { @@ -162,7 +162,7 @@ describe('Update Claim', () => { expect(updatedClaim.observation).toEqual(ctx.args.observation); expect(updatedClaim.claimStateFk).toEqual(ctx.args.claimStateFk); expect(updatedClaim.workerFk).toEqual(ctx.args.workerFk); - expect(chatModel.sendCheckingPresence).toHaveBeenCalled(); + expect(chatModel.send).toHaveBeenCalled(); await tx.rollback(); } catch (e) { diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index 326192385..bc36f12bc 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -64,11 +64,16 @@ module.exports = Self => { relation: 'client', scope: { include: { - relation: 'salesPersonUser' + relation: 'salesDepartment', + scope: { + fields: ['id', 'chatName'] + } } } } }, myOptions); + // Get sales person from claim client + const salesDepartment = claim.client().salesDepartment(); const changedPickup = args.pickup && args.pickup != claim.pickup; @@ -84,14 +89,13 @@ module.exports = Self => { delete args.ctx; const updatedClaim = await claim.updateAttributes(args, myOptions); - const salesPerson = claim.client().salesPersonUser(); - if (salesPerson) { + if (salesDepartment) { if (changedPickup && updatedClaim.pickup) - await notifyPickUp(ctx, salesPerson.id, claim); + await notifyPickUp(ctx, salesDepartment, claim); if (args.claimStateFk) { const newState = await models.ClaimState.findById(args.claimStateFk, null, myOptions); - await notifyStateChange(ctx, salesPerson.id, claim, newState.description); + await notifyStateChange(ctx, salesDepartment, claim, newState.description); if (newState.code == 'canceled') await notifyStateChange(ctx, claim.workerFk, claim, newState.description); } @@ -106,7 +110,7 @@ module.exports = Self => { } }; - async function notifyStateChange(ctx, workerId, claim, newState) { + async function notifyStateChange(ctx, salesDepartment, claim, newState) { const models = Self.app.models; const url = await models.Url.getUrl(); const $t = ctx.req.__; @@ -117,10 +121,10 @@ module.exports = Self => { claimUrl: `${url}claim/${claim.id}/summary`, newState }); - await models.Chat.sendCheckingPresence(ctx, workerId, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } - async function notifyPickUp(ctx, workerId, claim) { + async function notifyPickUp(ctx, salesDepartment, claim) { const models = Self.app.models; const url = await models.Url.getUrl(); const $t = ctx.req.__; // $translate @@ -131,6 +135,6 @@ module.exports = Self => { claimUrl: `${url}claim/${claim.id}/summary`, claimPickup: $t(claim.pickup) }); - await models.Chat.sendCheckingPresence(ctx, workerId, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } }; diff --git a/modules/client/back/locale/client/en.yml b/modules/client/back/locale/client/en.yml index 75ff0f7ac..31e595ea7 100644 --- a/modules/client/back/locale/client/en.yml +++ b/modules/client/back/locale/client/en.yml @@ -35,7 +35,7 @@ columns: sageTaxTypeFk: sage tax type sageTransactionTypeFk: sage transaction type businessTypeFk: business type - salesPersonFk: sales person + salesDepartmentFk: sales department hasElectronicInvoice: electronic invoice payMethodFk: pay method provinceFk: province @@ -50,6 +50,6 @@ columns: isCreatedAsServed: created as served hasInvoiceSimplified: simplified invoice typeFk: type - lastSalesPersonFk: last salesperson + lastSalesDepartmentFk: last sales department rating: rating recommendedCredit: recommended credit diff --git a/modules/client/back/locale/client/es.yml b/modules/client/back/locale/client/es.yml index 7ff6a5c30..944b8302d 100644 --- a/modules/client/back/locale/client/es.yml +++ b/modules/client/back/locale/client/es.yml @@ -35,7 +35,7 @@ columns: sageTaxTypeFk: tipo impuesto sage sageTransactionTypeFk: tipo transacción Sage businessTypeFk: tipo negocio - salesPersonFk: comercial + salesDepartmentFk: departamento comercial hasElectronicInvoice: factura electrónica payMethodFk: método pago provinceFk: provincia @@ -50,6 +50,6 @@ columns: isCreatedAsServed: creado como servido hasInvoiceSimplified: factura simple typeFk: tipo - lastSalesPersonFk: último comercial + lastSalesDepartmentFk: último departmaneto comercial rating: clasificación recommendedCredit: crédito recomendado diff --git a/modules/client/back/methods/client/consumptionSendQueued.js b/modules/client/back/methods/client/consumptionSendQueued.js index a7081c550..6b43d2a2b 100644 --- a/modules/client/back/methods/client/consumptionSendQueued.js +++ b/modules/client/back/methods/client/consumptionSendQueued.js @@ -29,26 +29,26 @@ module.exports = Self => { const clients = await Self.rawSql(` SELECT - c.id AS clientFk, - c.email AS clientEmail, - eu.email salesPersonEmail - FROM client c - JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk - JOIN ticket t ON t.clientFk = c.id - JOIN sale s ON s.ticketFk = t.id - JOIN item i ON i.id = s.itemFk - JOIN itemType it ON it.id = i.typeFk - WHERE c.id IN(?) - AND it.isPackaging = FALSE - AND DATE(t.shipped) BETWEEN ? AND ? - GROUP BY c.id`, [params.clients, params.from, params.to]); + c.id clientFk, + c.email clientEmail, + d.notificationEmail salesDepartmentEmail + FROM client c + JOIN department d ON d.id = c.salesDepartmentFk + JOIN ticket t ON t.clientFk = c.id + JOIN sale s ON s.ticketFk = t.id + JOIN item i ON i.id = s.itemFk + JOIN itemType it ON it.id = i.typeFk + WHERE c.id IN(?) + AND NOT it.isPackaging + AND DATE(t.shipped) BETWEEN ? AND ? + GROUP BY c.id`, [params.clients, params.from, params.to]); for (const client of clients) { try { const args = { id: client.clientFk, recipient: client.clientEmail, - replyTo: client.salesPersonEmail, + replyTo: client.salesDepartmentEmail, from: params.from, to: params.to }; diff --git a/modules/client/back/methods/client/createWithUser.js b/modules/client/back/methods/client/createWithUser.js index 06b885ab8..8f7ae6064 100644 --- a/modules/client/back/methods/client/createWithUser.js +++ b/modules/client/back/methods/client/createWithUser.js @@ -50,7 +50,7 @@ module.exports = function(Self) { fi: data.fi, socialName: data.socialName, email: data.email, - salesPersonFk: data.salesPersonFk, + salesDepartmentFk: data.salesDepartmentFk, postcode: data.postcode, street: data.street, city: data.city, diff --git a/modules/client/back/methods/client/extendedListFilter.js b/modules/client/back/methods/client/extendedListFilter.js index 174970a2f..aa4832a28 100644 --- a/modules/client/back/methods/client/extendedListFilter.js +++ b/modules/client/back/methods/client/extendedListFilter.js @@ -23,7 +23,7 @@ module.exports = Self => { description: 'The client name', }, { - arg: 'salesPersonFk', + arg: 'salesDepartmentFk', type: 'number', }, { @@ -80,7 +80,7 @@ module.exports = Self => { ? {'c.id': {inq: value}} : {'c.name': {like: `%${value}%`}}; case 'name': - case 'salesPersonFk': + case 'salesDepartmentFk': case 'fi': case 'socialName': case 'city': @@ -126,8 +126,8 @@ module.exports = Self => { ct.name country, p.id AS provinceFk, p.name AS province, - u.id AS salesPersonFk, - u.name AS salesPerson, + d.id salesDepartmentFk, + d.name salesDeparment, bt.code AS businessTypeFk, bt.description AS businessType, pm.id AS payMethodFk, @@ -137,7 +137,7 @@ module.exports = Self => { stt.CodigoTransaccion AS sageTransactionTypeFk, stt.Transaccion AS sageTransactionType FROM client c - LEFT JOIN account.user u ON u.id = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN country ct ON ct.id = c.countryFk LEFT JOIN province p ON p.id = c.provinceFk LEFT JOIN businessType bt ON bt.code = c.businessTypeFk diff --git a/modules/client/back/methods/client/filter.js b/modules/client/back/methods/client/filter.js index f805c4be9..01918c075 100644 --- a/modules/client/back/methods/client/filter.js +++ b/modules/client/back/methods/client/filter.js @@ -23,7 +23,7 @@ module.exports = Self => { description: 'The client name', }, { - arg: 'salesPersonFk', + arg: 'salesDeparmentFk', type: 'number', }, { @@ -127,7 +127,7 @@ module.exports = Self => { {'a.provinceFk': value} ]}; case 'name': - case 'salesPersonFk': + case 'salesDepartmentFk': case 'fi': case 'socialName': case 'email': @@ -147,7 +147,6 @@ module.exports = Self => { c.socialName, c.phone, a.phone, - c.mobile, c.city, a.city, c.postcode, @@ -155,13 +154,13 @@ module.exports = Self => { c.email, c.isActive, c.isFreezed, - p.id AS provinceClientFk, - a.provinceFk AS provinceAddressFk, - p.name AS province, - u.id AS salesPersonFk, - u.name AS salesPerson + p.id provinceClientFk, + a.provinceFk provinceAddressFk, + p.name province, + d.id salesDepartmentFk, + d.name salesDepartment FROM client c - LEFT JOIN account.user u ON u.id = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN province p ON p.id = c.provinceFk JOIN address a ON a.clientFk = c.id ` diff --git a/modules/client/back/methods/client/getCard.js b/modules/client/back/methods/client/getCard.js index c15c260bc..d409f561a 100644 --- a/modules/client/back/methods/client/getCard.js +++ b/modules/client/back/methods/client/getCard.js @@ -42,7 +42,7 @@ module.exports = function(Self) { } }, { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { fields: ['id', 'name'] } diff --git a/modules/client/back/methods/client/specs/createWithUser.spec.js b/modules/client/back/methods/client/specs/createWithUser.spec.js index 5b1ff5da9..6161c5c59 100644 --- a/modules/client/back/methods/client/specs/createWithUser.spec.js +++ b/modules/client/back/methods/client/specs/createWithUser.spec.js @@ -9,6 +9,7 @@ describe('Client Create', () => { street: 'WALL STREET', city: 'New York', businessTypeFk: 'florist', + salesDepartmentFk: 80, provinceFk: 1 }; const newAccountWithoutEmail = JSON.parse(JSON.stringify(newAccount)); diff --git a/modules/client/back/methods/client/specs/extendedListFilter.spec.js b/modules/client/back/methods/client/specs/extendedListFilter.spec.js index 9a0441656..d678a574f 100644 --- a/modules/client/back/methods/client/specs/extendedListFilter.spec.js +++ b/modules/client/back/methods/client/specs/extendedListFilter.spec.js @@ -86,14 +86,14 @@ describe('client extendedListFilter()', () => { } }); - it('should return the clients matching the "salesPersonFk" argument', async() => { + it('should return the clients matching the "salesDepartmementFk" argument', async() => { const tx = await models.Client.beginTransaction({}); - const salesPersonId = 18; + const salesDepartmementId = 80; try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 1}}, args: {salesPersonFk: salesPersonId}}; + const ctx = {req: {accessToken: {userId: 1}}, args: {salesDepartmentFk: salesDepartmementId}}; const filter = {}; const result = await models.Client.extendedListFilter(ctx, filter, options); @@ -101,7 +101,7 @@ describe('client extendedListFilter()', () => { const randomResultClient = result[randomIndex]; expect(result.length).toBeGreaterThanOrEqual(5); - expect(randomResultClient.salesPersonFk).toEqual(salesPersonId); + expect(randomResultClient.salesDepartmentFk).toEqual(salesDepartmementId); await tx.rollback(); } catch (e) { diff --git a/modules/client/back/methods/client/specs/filter.spec.js b/modules/client/back/methods/client/specs/filter.spec.js index 679585050..dd3b21d8b 100644 --- a/modules/client/back/methods/client/specs/filter.spec.js +++ b/modules/client/back/methods/client/specs/filter.spec.js @@ -86,14 +86,14 @@ describe('client filter()', () => { } }); - it('should return the clients matching the "salesPersonFk" argument', async() => { + it('should return the clients matching the "salesDepartmentFk" argument', async() => { const tx = await models.Client.beginTransaction({}); - const salesPersonId = 18; + const salesDepartmentId = 80; try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 1}}, args: {salesPersonFk: salesPersonId}}; + const ctx = {req: {accessToken: {userId: 1}}, args: {salesDepartmentFk: salesDepartmentId}}; const filter = {}; const result = await models.Client.filter(ctx, filter, options); @@ -101,7 +101,7 @@ describe('client filter()', () => { const randomResultClient = result[randomIndex]; expect(result.length).toBeGreaterThanOrEqual(5); - expect(randomResultClient.salesPersonFk).toEqual(salesPersonId); + expect(randomResultClient.salesDepartmentFk).toEqual(salesDepartmentId); await tx.rollback(); } catch (e) { diff --git a/modules/client/back/methods/client/summary.js b/modules/client/back/methods/client/summary.js index 8162096f0..9b1e2c209 100644 --- a/modules/client/back/methods/client/summary.js +++ b/modules/client/back/methods/client/summary.js @@ -46,7 +46,7 @@ module.exports = Self => { } }, { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { fields: ['id', 'name'] } diff --git a/modules/client/back/methods/defaulter/filter.js b/modules/client/back/methods/defaulter/filter.js index ac9504ec2..e8bfc0a90 100644 --- a/modules/client/back/methods/defaulter/filter.js +++ b/modules/client/back/methods/defaulter/filter.js @@ -59,9 +59,9 @@ module.exports = Self => { SELECT DISTINCT c.id clientFk, c.name clientName, - c.salesPersonFk, + c.salesDepartmentFk, c.businessTypeFk, - u.name salesPersonName, + de.name salesDepartmentName, d.amount, co.created, co.text observation, @@ -77,7 +77,7 @@ module.exports = Self => { JOIN vn.country cn ON cn.id = c.countryFk JOIN vn.payMethod pm ON pm.id = c.payMethodFk LEFT JOIN vn.clientObservation co ON co.clientFk = c.id - LEFT JOIN account.user u ON u.id = c.salesPersonFk + LEFT JOIN department de ON de.id = c.salesDepartmentFk LEFT JOIN account.user uw ON uw.id = co.workerFk WHERE d.created = ? diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 0a8ebcae5..294ca043b 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -126,12 +126,12 @@ module.exports = Self => { done(); } - Self.validate('payMethod', hasSalesMan, { - message: 'Cannot change the payment method if no salesperson' + Self.validate('payMethod', hasSalesDepartment, { + message: 'Cannot change the payment method if no salesdepartment' }); - function hasSalesMan(err) { - if (this.payMethod && !this.salesPersonUser) + function hasSalesDepartment(err) { + if (this.payMethod && !this.salesDepartment) err(); } @@ -356,18 +356,18 @@ module.exports = Self => { const $t = httpRequest.__; const url = await Self.app.models.Url.getUrl(); - const salesPersonId = instance.salesPersonFk; + const salesDepartmentId = instance.salesDepartmentFk; - if (salesPersonId) { + if (salesDepartmentId) { // Send email to client if (instance.email) { const {Email} = require('vn-print'); - const worker = await models.EmailUser.findById(salesPersonId); + const salesDepartment = await models.department.findById(salesDepartmentId); const params = { id: instance.id, recipientId: instance.id, recipient: instance.email, - replyTo: worker.email + replyTo: salesDepartment.notificationEmail }; const email = new Email('payment-update', params); await email.send(); @@ -379,19 +379,16 @@ module.exports = Self => { clientName: instance.name, url: fullUrl }); - await models.Chat.sendCheckingPresence(httpCtx, salesPersonId, message); + await models.Chat.send(httpCtx, `@${salesDepartment.chatName}`, message); } } - const workerIdBefore = oldInstance.salesPersonFk; - const workerIdAfter = newInstance.salesPersonFk; - const assignmentChanged = workerIdBefore != workerIdAfter; - if (assignmentChanged) - await Self.notifyAssignment(instance, workerIdBefore, workerIdAfter); + if (oldInstance.salesDepartmentFk != newInstance.salesDepartmentFk) + await Self.notifyAssignment(instance, oldInstance.salesDepartmentFk, newInstance.salesDepartmentFk); }); - // Send notification on client worker assignment - Self.notifyAssignment = async function notifyAssignment(client, previousWorkerId, currentWorkerId) { + // Send notification on client department assignment + Self.notifyAssignment = async function notifyAssignment(client, previousDepartmentId, currentDepartmentId) { const loopBackContext = LoopBackContext.getCurrentContext(); const httpCtx = {req: loopBackContext.active}; const httpRequest = httpCtx.req.http.req; @@ -399,38 +396,38 @@ module.exports = Self => { const url = await Self.app.models.Url.getUrl(); const models = Self.app.models; - let previousWorker = {name: $t('None')}; - let currentWorker = {name: $t('None')}; - if (previousWorkerId) { - const worker = await models.Worker.findById(previousWorkerId, { - include: {relation: 'user'} + let previousDepartment = {name: $t('None'), chatName: $t('None')}; + let currentDepartment = {name: $t('None'), chatName: $t('None')}; + if (previousDepartmentId) { + previousDepartment = await models.Department.findOne({ + field: ['name', 'chatName'], + where: { + id: previousDepartmentId + } }); - previousWorker.user = worker && worker.user().name; - previousWorker.name = worker && worker.user().nickname; } - - if (currentWorkerId) { - const worker = await models.Worker.findById(currentWorkerId, { - include: {relation: 'user'} + if (currentDepartmentId) { + currentDepartment = await models.Department.findOne({ + field: ['name', 'chatName'], + where: { + id: currentDepartmentId + } }); - currentWorker.user = worker && worker.user().name; - currentWorker.name = worker && worker.user().nickname; } - const fullUrl = `${url}client/${client.id}/basic-data`; const message = $t('Client assignment has changed', { clientId: client.id, clientName: client.name, url: fullUrl, - previousWorkerName: previousWorker.name, - currentWorkerName: currentWorker.name + previousDepartmentName: previousDepartment.name, + currentDepartmentName: currentDepartment.name }); - if (previousWorkerId) - await models.Chat.send(httpCtx, `@${previousWorker.user}`, message); + if (previousDepartmentId) + await models.Chat.send(httpCtx, `@${previousDepartment.chatName}`, message); - if (currentWorkerId) - await models.Chat.send(httpCtx, `@${currentWorker.user}`, message); + if (currentDepartmentId) + await models.Chat.send(httpCtx, `@${currentDepartment.chatName}`, message); }; // Credit change validations diff --git a/modules/client/back/models/client.json b/modules/client/back/models/client.json index f3eb9919b..36ee484ab 100644 --- a/modules/client/back/models/client.json +++ b/modules/client/back/models/client.json @@ -133,7 +133,7 @@ "columnName": "businessTypeFk" } }, - "salesPersonFk": { + "salesDepartmentFk": { "type": "number" }, "hasElectronicInvoice": { @@ -163,10 +163,10 @@ "model": "PayMethod", "foreignKey": "payMethodFk" }, - "salesPersonUser": { + "salesDepartment": { "type": "belongsTo", - "model": "VnUser", - "foreignKey": "salesPersonFk" + "model": "Department", + "foreignKey": "salesDepartmentFk" }, "province": { "type": "belongsTo", diff --git a/modules/client/back/models/credit-insurance.js b/modules/client/back/models/credit-insurance.js index 84bd90424..08da124a6 100644 --- a/modules/client/back/models/credit-insurance.js +++ b/modules/client/back/models/credit-insurance.js @@ -51,9 +51,9 @@ module.exports = function(Self) { const data = ctx.instance; const insurance = await Self.findById(data.id, null, options); const client = insurance.classification().customer(); - const salesPerson = client.salesPersonUser(); + const salesDepartment = client.salesDepartment(); - if (!salesPerson) return; + if (!salesDepartment) return; const httpRequest = httpCtx.req.http.req; const $t = httpRequest.__; @@ -65,6 +65,6 @@ module.exports = function(Self) { credit: data.credit, url: fullPath }); - await models.Chat.sendCheckingPresence(httpCtx, salesPerson.id, message); + await models.Chat.send(httpCtx, `@${salesDepartment.chatName}`, message); }); }; diff --git a/modules/client/back/models/credit-insurance.json b/modules/client/back/models/credit-insurance.json index 08b2e3d60..dd2906087 100644 --- a/modules/client/back/models/credit-insurance.json +++ b/modules/client/back/models/credit-insurance.json @@ -41,11 +41,11 @@ "include": { "relation": "customer", "scope": { - "fields": ["name", "salesPersonFk"], + "fields": ["name", "salesDepartmentFk"], "include": { - "relation": "salesPersonUser", + "relation": "salesDepartment", "scope": { - "fields": ["id", "name"] + "fields": ["id", "chatName"] } } } diff --git a/modules/client/back/models/specs/client.spec.js b/modules/client/back/models/specs/client.spec.js index 1b5132304..af0e19c7b 100644 --- a/modules/client/back/models/specs/client.spec.js +++ b/modules/client/back/models/specs/client.spec.js @@ -16,8 +16,8 @@ describe('Client Model', () => { const ctx = {req: activeCtx}; const chatModel = models.Chat; const instance = {id: 1101, name: 'Bruce Banner'}; - const previousWorkerId = 1106; // DavidCharlesHaller - const currentWorkerId = 1107; // HankPym + const previousDepartmetnId = 80; // es5_equipo + const currentDepartmetnId = 94; // es2_equipo beforeEach(() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ @@ -29,26 +29,26 @@ describe('Client Model', () => { it('should call to the Chat send() method for both workers', async() => { spyOn(chatModel, 'send').and.callThrough(); - await models.Client.notifyAssignment(instance, previousWorkerId, currentWorkerId); + await models.Client.notifyAssignment(instance, previousDepartmetnId, currentDepartmetnId); - expect(chatModel.send).toHaveBeenCalledWith(ctx, '@davidcharleshaller', `Client assignment has changed`); - expect(chatModel.send).toHaveBeenCalledWith(ctx, '@hankpym', `Client assignment has changed`); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es5_equipo', `Client assignment has changed`); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es2_equipo', `Client assignment has changed`); }); it('should call to the Chat send() method for the previous worker', async() => { spyOn(chatModel, 'send').and.callThrough(); - await models.Client.notifyAssignment(instance, null, currentWorkerId); + await models.Client.notifyAssignment(instance, null, currentDepartmetnId); - expect(chatModel.send).toHaveBeenCalledWith(ctx, '@hankpym', `Client assignment has changed`); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es2_equipo', `Client assignment has changed`); }); it('should call to the Chat send() method for the current worker', async() => { spyOn(chatModel, 'send').and.callThrough(); - await models.Client.notifyAssignment(instance, previousWorkerId, null); + await models.Client.notifyAssignment(instance, previousDepartmetnId, null); - expect(chatModel.send).toHaveBeenCalledWith(ctx, '@davidcharleshaller', `Client assignment has changed`); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '@es5_equipo', `Client assignment has changed`); }); }); diff --git a/modules/entry/back/methods/entry/latestBuysFilter.js b/modules/entry/back/methods/entry/latestBuysFilter.js index dbf7d2b17..f8ed6a882 100644 --- a/modules/entry/back/methods/entry/latestBuysFilter.js +++ b/modules/entry/back/methods/entry/latestBuysFilter.js @@ -35,7 +35,7 @@ module.exports = Self => { description: 'The item description', }, { - arg: 'salesPersonFk', + arg: 'buyerFkFk', type: 'integer', description: 'The buyer of the item', }, @@ -118,7 +118,7 @@ module.exports = Self => { return {'i.description': {like: `%${value}%`}}; case 'categoryFk': return {'ic.id': value}; - case 'salesPersonFk': + case 'buyerFk': return {'it.workerFk': value}; case 'supplierFk': return {'s.id': value}; diff --git a/modules/invoiceOut/back/methods/invoiceOut/makePdfAndNotify.js b/modules/invoiceOut/back/methods/invoiceOut/makePdfAndNotify.js index e2dc15993..62fcb0c13 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/makePdfAndNotify.js +++ b/modules/invoiceOut/back/methods/invoiceOut/makePdfAndNotify.js @@ -41,7 +41,13 @@ module.exports = Self => { include: { relation: 'client', scope: { - fields: ['id', 'email', 'isToBeMailed', 'salesPersonFk'] + fields: ['id', 'email', 'isToBeMailed', 'salesDepartmentFk'], + include: { + relation: 'salesDepartment', + scope: { + fields: ['id', 'chatName'] + } + } } } }, options); @@ -63,11 +69,10 @@ module.exports = Self => { clientId: client.id, clientUrl: `${url}claim/${id}/summary` }); - const salesPersonId = client.salesPersonFk; - - if (salesPersonId) - await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment) + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); throw new UserError('Error when sending mail to client', 'mailNotSent'); } } else { diff --git a/modules/invoiceOut/back/methods/invoiceOut/negativeBases.js b/modules/invoiceOut/back/methods/invoiceOut/negativeBases.js index 76ef29604..970406ef0 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/negativeBases.js +++ b/modules/invoiceOut/back/methods/invoiceOut/negativeBases.js @@ -62,22 +62,20 @@ module.exports = Self => { cou.name country, c.id clientId, c.socialName clientSocialName, - u.nickname workerSocialName, SUM(s.quantity * s.price * ( 100 - s.discount ) / 100) amount, negativeBase.taxableBase, negativeBase.ticketFk, c.isActive, c.hasToInvoice, c.isTaxDataChecked, - w.id comercialId, - u.name workerName + c.salesDepartmentFk, + d.name salesDepartmentName FROM vn.ticket t JOIN vn.company co ON co.id = t.companyFk JOIN vn.sale s ON s.ticketFk = t.id JOIN vn.client c ON c.id = t.clientFk JOIN vn.country cou ON cou.id = c.countryFk - LEFT JOIN vn.worker w ON w.id = c.salesPersonFk - JOIN account.user u ON u.id = w.id + JOIN vn.department d ON d.id = c.salesDepartmentFk LEFT JOIN ( SELECT ticketFk, taxableBase FROM tmp.ticketAmount diff --git a/modules/monitor/back/methods/sales-monitor/clientsFilter.js b/modules/monitor/back/methods/sales-monitor/clientsFilter.js index 13e38f8e1..ab5a9a10a 100644 --- a/modules/monitor/back/methods/sales-monitor/clientsFilter.js +++ b/modules/monitor/back/methods/sales-monitor/clientsFilter.js @@ -34,31 +34,24 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - const date = Date.vnNew(); - date.setHours(0, 0, 0, 0); const stmt = new ParameterizedSQL(` - SELECT - v.id, - u.name AS salesPerson, - IFNULL(sc.workerSubstitute, c.salesPersonFk) AS salesPersonFk, - c.id AS clientFk, - c.name AS clientName, - TIME(v.stamp) AS hour, - DATE(v.stamp) AS dated, - wtc.workerFk - FROM hedera.visitUser v - JOIN client c ON c.id = v.userFk - JOIN account.user u ON c.salesPersonFk = u.id - LEFT JOIN sharingCart sc ON sc.workerFk = c.salesPersonFk - AND ? BETWEEN sc.started AND sc.ended - LEFT JOIN workerTeamCollegues wtc - ON wtc.collegueFk = IFNULL(sc.workerSubstitute, c.salesPersonFk)`, - [date]); + SELECT v.id, + d.name salesDepartment, + c.salesDepartmentFk, + c.id clientFk, + c.name clientName, + TIME(v.stamp) hour, + DATE(v.stamp) dated, + wd.workerFk + FROM hedera.visitUser v + JOIN client c ON c.id = v.userFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk + LEFT JOIN workerDepartment wd ON wd.departmentFk = d.id`); if (!filter.where) filter.where = {}; const where = filter.where; - where['wtc.workerFk'] = userId; + where['wd.workerFk'] = userId; stmt.merge(conn.makeWhere(filter.where)); stmt.merge(`GROUP BY clientFk, v.stamp`); diff --git a/modules/monitor/back/methods/sales-monitor/ordersFilter.js b/modules/monitor/back/methods/sales-monitor/ordersFilter.js index a80ea822e..9724f9854 100644 --- a/modules/monitor/back/methods/sales-monitor/ordersFilter.js +++ b/modules/monitor/back/methods/sales-monitor/ordersFilter.js @@ -36,32 +36,30 @@ module.exports = Self => { const stmt = new ParameterizedSQL(` SELECT - c.id AS clientFk, - c.name AS clientName, + c.id clientFk, + c.name clientName, a.nickname, o.id, o.date_make, o.date_send, o.customer_id, - COUNT(item_id) AS totalRows, - ROUND(SUM(amount * price)) * 1 AS import, - u.id AS salesPersonFk, - u.name AS salesPerson, - am.name AS agencyName + COUNT(item_id) totalRows, + ROUND(SUM(amount * price)) * 1 import, + c.salesDepartmentFk, + d.name salesDepartment, + am.name agencyName FROM hedera.order o JOIN hedera.order_row orw ON o.id = orw.order_id JOIN client c ON c.id = o.customer_id JOIN address a ON a.id = o.address_id JOIN agencyMode am ON am.id = o.agency_id - JOIN account.user u ON u.id = c.salesPersonFk - JOIN workerTeamCollegues wtc ON c.salesPersonFk = wtc.collegueFk`); + LEFT JOIN department d ON d.id = c.salesDepartmentFk`); if (!filter.where) filter.where = {}; const where = filter.where; where['o.confirmed'] = false; where['o.date_send'] = {gt: '2001-01-01'}; - where['wtc.workerFk'] = userId; stmt.merge(conn.makeWhere(filter.where)); stmt.merge(conn.makeGroupBy('o.id')); diff --git a/modules/monitor/back/methods/sales-monitor/salesFilter.js b/modules/monitor/back/methods/sales-monitor/salesFilter.js index 33b37d8a4..9c82ffd2a 100644 --- a/modules/monitor/back/methods/sales-monitor/salesFilter.js +++ b/modules/monitor/back/methods/sales-monitor/salesFilter.js @@ -49,9 +49,9 @@ module.exports = Self => { type: 'number', description: `The warehouse id filter` }, { - arg: 'salesPersonFk', + arg: 'salesDepartmentFk', type: 'number', - description: `The salesperson id filter` + description: `The salesDepartment id filter` }, { arg: 'provinceFk', type: 'number', @@ -60,10 +60,6 @@ module.exports = Self => { arg: 'stateFk', type: 'number', description: `The state id filter` - }, { - arg: 'myTeam', - type: 'boolean', - description: `Whether to show only tickets for the current logged user team (currently user tickets)` }, { arg: 'problems', type: 'boolean', @@ -112,22 +108,6 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - // Apply filter by team - const teamMembersId = []; - if (args.myTeam != null) { - const worker = await models.Worker.findById(userId, { - include: { - relation: 'collegues' - } - }, myOptions); - const collegues = worker.collegues() || []; - for (let collegue of collegues) - teamMembersId.push(collegue.collegueFk); - - if (teamMembersId.length == 0) - teamMembersId.push(userId); - } - if (ctx.args && args.to) { const dateTo = args.to; dateTo.setHours(23, 59, 0, 0); @@ -143,15 +123,8 @@ module.exports = Self => { return {'t.shipped': {gte: value}}; case 'to': return {'t.shipped': {lte: value}}; - case 'salesPersonFk': - return {'c.salesPersonFk': value}; - case 'mine': - case 'myTeam': - if (value) - return {'c.salesPersonFk': {inq: teamMembersId}}; - else - return {'c.salesPersonFk': {nin: teamMembersId}}; - case 'id': + case 'salesDepartmentFk': + return value ? {'c.salesDepartmentFk': value} : {}; case 'clientFk': param = `t.${param}`; return {[param]: value}; @@ -187,12 +160,11 @@ module.exports = Self => { am.name agencyMode, am.id agencyModeFk, st.name state, - wk.lastName salesPerson, + d.name salesDepartmentName, ts.stateFk stateFk, ts.alertLevel alertLevel, ts.code alertLevelCode, - u.name userName, - c.salesPersonFk, + c.salesDepartmentFk, c.credit, z.hour zoneLanding, z.name zoneName, @@ -211,8 +183,7 @@ module.exports = Self => { LEFT JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN client c ON c.id = t.clientFk - LEFT JOIN worker wk ON wk.id = c.salesPersonFk - LEFT JOIN account.user u ON u.id = wk.id + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN ( SELECT zoneFk, CAST( diff --git a/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js index febfc5357..2fc8781aa 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/clientsFilter.spec.js @@ -6,7 +6,7 @@ describe('SalesMonitor clientsFilter()', () => { try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 18}}, args: {}}; + const ctx = {req: {accessToken: {}}, args: {}}; const from = Date.vnNew(); const to = Date.vnNew(); @@ -20,7 +20,7 @@ describe('SalesMonitor clientsFilter()', () => { }; const result = await models.SalesMonitor.clientsFilter(ctx, filter, options); - expect(result.length).toEqual(3); + expect(result.length).toEqual(1); await tx.rollback(); } catch (e) { @@ -34,7 +34,7 @@ describe('SalesMonitor clientsFilter()', () => { try { const options = {transaction: tx}; - const ctx = {req: {accessToken: {userId: 18}}, args: {}}; + const ctx = {req: {accessToken: {}}, args: {}}; const yesterday = Date.vnNew(); yesterday.setDate(yesterday.getDate() - 1); const today = Date.vnNew(); @@ -42,13 +42,15 @@ describe('SalesMonitor clientsFilter()', () => { today.setHours(23, 59, 59, 59); const filter = { + where: { 'v.stamp': {between: [yesterday, today]} } }; + const result = await models.SalesMonitor.clientsFilter(ctx, filter, options); - expect(result.length).toEqual(5); + expect(result.length).toEqual(2); await tx.rollback(); } catch (e) { diff --git a/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js index 16a5514dd..500c0338b 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/ordersFilter.spec.js @@ -11,7 +11,7 @@ describe('SalesMonitor ordersFilter()', () => { const filter = {order: 'date_make DESC'}; const result = await models.SalesMonitor.ordersFilter(ctx, filter, options); - expect(result.length).toEqual(12); + expect(result.length).toEqual(15); await tx.rollback(); } catch (e) { diff --git a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js index c3da7f08b..c3ccdfad0 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js @@ -161,7 +161,7 @@ describe('SalesMonitor salesFilter()', () => { } }); - it('should now return the tickets from the worker team', async() => { + it('should now return the tickets from the worker department', async() => { const tx = await models.SalesMonitor.beginTransaction({}); try { @@ -180,25 +180,6 @@ describe('SalesMonitor salesFilter()', () => { } }); - it('should now return the tickets that are not from the worker team', async() => { - const tx = await models.SalesMonitor.beginTransaction({}); - - try { - const options = {transaction: tx}; - - const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}}; - const filter = {}; - const result = await models.SalesMonitor.salesFilter(ctx, filter, options); - - expect(result.length).toEqual(4); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - it('should return the tickets sorted by problems descendant', async() => { const tx = await models.SalesMonitor.beginTransaction({}); diff --git a/modules/order/back/methods/order/filter.js b/modules/order/back/methods/order/filter.js index 592ed11e6..110a94e2d 100644 --- a/modules/order/back/methods/order/filter.js +++ b/modules/order/back/methods/order/filter.js @@ -44,13 +44,9 @@ module.exports = Self => { type: 'integer', description: 'The agency mode id' }, { - arg: 'workerFk', + arg: 'departmentFk', type: 'integer', - description: 'The salesperson id' - }, { - arg: 'myTeam', - type: 'boolean', - description: 'Whether to show only tickets for the current logged user team (currently user tickets)' + description: 'The department id' }, { arg: 'isConfirmed', type: 'boolean', @@ -79,26 +75,6 @@ module.exports = Self => { Object.assign(myOptions, options); const args = ctx.args; - - // Apply filter by team - const teamMembersId = []; - if (args.myTeam != null) { - const worker = await models.Worker.findById(userId, { - include: { - relation: 'collegues' - } - }, myOptions); - const collegues = worker.collegues() || []; - for (let collegue of collegues) - teamMembersId.push(collegue.collegueFk); - - if (teamMembersId.length == 0) - teamMembersId.push(userId); - } - - if (args && args.myTeam) - args.teamIds = teamIds; - const where = buildFilter(args, (param, value) => { switch (param) { case 'search': @@ -111,8 +87,8 @@ module.exports = Self => { return {'o.date_send': {gte: value}}; case 'to': return {'o.date_send': {lte: value}}; - case 'workerFk': - return {'c.salesPersonFk': value}; + case 'departmentFk': + return {'c.salesDepartmentFk': value}; case 'clientFk': return {'o.customer_id': value}; case 'agencyModeFk': @@ -123,11 +99,6 @@ module.exports = Self => { return {'o.id': value}; case 'isConfirmed': return {'o.confirmed': value ? 1 : 0}; - case 'myTeam': - if (value) - return {'c.salesPersonFk': {inq: teamMembersId}}; - else - return {'c.salesPersonFk': {nin: teamMembersId}}; case 'showEmpty': return {'o.total': {neq: value}}; case 'id': @@ -157,9 +128,8 @@ module.exports = Self => { o.source_app sourceApp, o.confirmed isConfirmed, c.name clientName, - c.salesPersonFk, - u.nickname workerNickname, - u.name name, + c.salesDepartmentFk, + d.name salesDepartmentName, co.code companyCode, zed.zoneFk, zed.hourTheoretical, @@ -169,11 +139,10 @@ module.exports = Self => { LEFT JOIN address a ON a.id = o.address_id LEFT JOIN agencyMode am ON am.id = o.agency_id LEFT JOIN client c ON c.id = o.customer_id - LEFT JOIN worker wk ON wk.id = c.salesPersonFk - LEFT JOIN account.user u ON u.id = wk.id LEFT JOIN company co ON co.id = o.company_id LEFT JOIN orderTicket ot ON ot.orderFk = o.id LEFT JOIN ticket t ON t.id = ot.ticketFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN ( SELECT zoneFk, CAST( diff --git a/modules/order/back/methods/order/summary.js b/modules/order/back/methods/order/summary.js index c48abb78f..954820b28 100644 --- a/modules/order/back/methods/order/summary.js +++ b/modules/order/back/methods/order/summary.js @@ -41,9 +41,9 @@ module.exports = Self => { { relation: 'client', scope: { - fields: ['salesPersonFk', 'name'], + fields: ['salesDepartmentFk', 'name'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { fields: ['id', 'name'] } diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js index c0b952b70..92133d4ab 100644 --- a/modules/route/back/methods/route/getTickets.js +++ b/modules/route/back/methods/route/getTickets.js @@ -51,7 +51,7 @@ module.exports = Self => { a.city, am.name agencyModeName, u.nickname userNickname, - vn.ticketTotalVolume(t.id) volume, + ticketTotalVolume(t.id) volume, GROUP_CONCAT(DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk) ipt, c.phone clientPhone, c.mobile clientMobile, @@ -59,14 +59,14 @@ module.exports = Self => { a.mobile addressMobile, a.longitude, a.latitude, - wm.mediaValue salePersonPhone, + d.pbxQueue saleDepartmentPhone, t.cmrFk, t.isSigned signed FROM vn.route r JOIN ticket t ON t.routeFk = r.id JOIN client c ON t.clientFk = c.id - LEFT JOIN vn.sale s ON s.ticketFk = t.id - LEFT JOIN vn.item i ON i.id = s.itemFk + LEFT JOIN sale s ON s.ticketFk = t.id + LEFT JOIN item i ON i.id = s.itemFk LEFT JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN warehouse wh ON wh.id = t.warehouseFk @@ -80,7 +80,7 @@ module.exports = Self => { LEFT JOIN agencyMode am ON am.id = t.agencyModeFk LEFT JOIN account.user u ON u.id = r.workerFk LEFT JOIN vehicle v ON v.id = r.vehicleFk - LEFT JOIN workerMedia wm ON wm.workerFk = c.salesPersonFk` + LEFT JOIN department d ON d.id = c.salesDepartmentFk` ); if (!filter.where) filter.where = {}; diff --git a/modules/ticket/back/methods/sale/deleteSales.js b/modules/ticket/back/methods/sale/deleteSales.js index 0290b2b9d..07cf592cf 100644 --- a/modules/ticket/back/methods/sale/deleteSales.js +++ b/modules/ticket/back/methods/sale/deleteSales.js @@ -48,9 +48,9 @@ module.exports = Self => { relation: 'client', scope: { include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } @@ -67,11 +67,10 @@ module.exports = Self => { } const deletedSales = await Promise.all(promises); - if (tx) await tx.commit(); - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t('Deleted sales from ticket', { @@ -79,7 +78,7 @@ module.exports = Self => { ticketUrl: `${url}ticket/${ticketId}/sale`, deletions: deletions }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message, myOptions); } return deletedSales; diff --git a/modules/ticket/back/methods/sale/getFromSectorCollection.js b/modules/ticket/back/methods/sale/getFromSectorCollection.js index c8925c3a9..703be608f 100644 --- a/modules/ticket/back/methods/sale/getFromSectorCollection.js +++ b/modules/ticket/back/methods/sale/getFromSectorCollection.js @@ -50,7 +50,7 @@ module.exports = Self => { saldo: sale.quantity, trabajador: sale.workerCode, idMovimiento: sale.saleFk, - salesPersonFk: sale.salesPersonFk, + salesDepartmentFk: sale.salesDepartmentFk, picked: sale.pickedQuantity, carros }); diff --git a/modules/ticket/back/methods/sale/reserve.js b/modules/ticket/back/methods/sale/reserve.js index 36db791fc..a3b309766 100644 --- a/modules/ticket/back/methods/sale/reserve.js +++ b/modules/ticket/back/methods/sale/reserve.js @@ -74,17 +74,17 @@ module.exports = Self => { relation: 'client', scope: { include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } } }, myOptions); - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t('Changed sale reserved state', { @@ -92,7 +92,7 @@ module.exports = Self => { ticketUrl: `${url}ticket/${ticketId}/sale`, changes: changesMade }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions); + await models.Chat.send(ctx, `@${salesDepartment.chatName.chatName}`, message, myOptions); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/sale/specs/updatePrice.spec.js b/modules/ticket/back/methods/sale/specs/updatePrice.spec.js index 9d1403df0..22ea52ed9 100644 --- a/modules/ticket/back/methods/sale/specs/updatePrice.spec.js +++ b/modules/ticket/back/methods/sale/specs/updatePrice.spec.js @@ -1,4 +1,5 @@ const models = require('vn-loopback/server/server').models; +const {ConsoleReporter} = require('jasmine'); const LoopBackContext = require('loopback-context'); describe('sale updatePrice()', () => { @@ -85,14 +86,16 @@ describe('sale updatePrice()', () => { } }); - it('should set price as a decimal number and check the sale has the mana component changing the salesPersonMana', async() => { + it(`should set price as a decimal number and check the sale + has the mana component changing the SalesDepartmentMana`, async() => { const tx = await models.Sale.beginTransaction({}); try { const options = {transaction: tx}; + const departmentId = 80; const price = 5.4; - const originalSalesPersonMana = await models.WorkerMana.findById(18, null, options); + const originalSalesDepartmentMana = await models.DepartmentMana.findById(departmentId, null, options); const manaComponent = await models.Component.findOne({where: {code: 'mana'}}, options); const teamOne = 96; const userId = ctx.req.accessToken.userId; @@ -110,9 +113,9 @@ describe('sale updatePrice()', () => { expect(updatedSale.price).toBe(price); expect(createdSaleComponent.value).toEqual(-2.34); - const updatedSalesPersonMana = await models.WorkerMana.findById(18, null, options); + const updatedSalesDepartmentMana = await models.DepartmentMana.findById(departmentId, null, options); - expect(updatedSalesPersonMana.amount).not.toEqual(originalSalesPersonMana.amount); + expect(updatedSalesDepartmentMana?.amount).not.toEqual(originalSalesDepartmentMana?.amount); await tx.rollback(); } catch (e) { diff --git a/modules/ticket/back/methods/sale/specs/usesMana.spec.js b/modules/ticket/back/methods/sale/specs/usesMana.spec.js index 74465ab27..6679b85a7 100644 --- a/modules/ticket/back/methods/sale/specs/usesMana.spec.js +++ b/modules/ticket/back/methods/sale/specs/usesMana.spec.js @@ -1,7 +1,7 @@ const models = require('vn-loopback/server/server').models; describe('sale usesMana()', () => { - const ctx = {req: { accessToken: {userId: 18}}}; + const ctx = {req: {accessToken: {userId: 18}}}; it('should return that the worker uses mana', async() => { const tx = await models.Sale.beginTransaction({}); @@ -45,14 +45,13 @@ describe('sale usesMana()', () => { it('should return that the worker does not use mana because it is excluded', async() => { const tx = await models.Sale.beginTransaction({}); const buyerId = 35; - const franceDepartmentId = 133; const buyerCtx = {req: {accessToken: {userId: buyerId}}}; try { - const options = {transaction: tx} + const options = {transaction: tx}; - await models.WorkerManaExcluded.create({workerFk: buyerId}, options); - await models.Business.updateAll({workerFk: buyerId}, {departmentFk: franceDepartmentId}, options); + const {departmentFk} = await models.WorkerDepartment.findById(buyerId, null, options); + await models.DepartmentManaExcluded.create({'salesDepartmentFk': departmentFk}, options); const usesMana = await models.Sale.usesMana(buyerCtx, options); diff --git a/modules/ticket/back/methods/sale/updatePrice.js b/modules/ticket/back/methods/sale/updatePrice.js index 191fd09e3..a31a3c515 100644 --- a/modules/ticket/back/methods/sale/updatePrice.js +++ b/modules/ticket/back/methods/sale/updatePrice.js @@ -48,11 +48,10 @@ module.exports = Self => { include: { relation: 'client', scope: { - fields: ['salesPersonFk'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } @@ -63,22 +62,16 @@ module.exports = Self => { }; const sale = await models.Sale.findById(id, filter, myOptions); - await models.Sale.canEdit(ctx, [id], myOptions); const oldPrice = sale.price; - const userId = ctx.req.accessToken.userId; - const usesMana = await models.Sale.usesMana(ctx, myOptions); const componentCode = usesMana ? 'mana' : 'buyerDiscount'; const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions); const componentId = discount.id; const componentValue = newPrice - sale.price; - const where = { - componentFk: componentId, - saleFk: id - }; + const where = {componentFk: componentId, saleFk: id}; const saleComponent = await models.SaleComponent.findOne({where}, myOptions); if (saleComponent) { await models.SaleComponent.updateAll(where, { @@ -92,12 +85,12 @@ module.exports = Self => { }, myOptions); } await sale.updateAttributes({price: newPrice}, myOptions); + const salesDepartment = sale.ticket().client().salesDepartment(); - await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions); + await Self.rawSql('CALL vn.manaSpellers_requery(?)', [salesDepartment.id], myOptions); await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [sale.ticketFk], myOptions); - const salesPerson = sale.ticket().client().salesPersonUser(); - if (salesPerson) { + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t('Changed sale price', { ticketId: sale.ticket().id, @@ -109,7 +102,7 @@ module.exports = Self => { ticketUrl: `${url}ticket/${sale.ticket().id}/sale`, itemUrl: `${url}item/${sale.itemFk}/summary` }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message, myOptions); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/sale/updateQuantity.js b/modules/ticket/back/methods/sale/updateQuantity.js index 36d75ffed..9f18defad 100644 --- a/modules/ticket/back/methods/sale/updateQuantity.js +++ b/modules/ticket/back/methods/sale/updateQuantity.js @@ -50,9 +50,9 @@ module.exports = Self => { relation: 'client', scope: { include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } @@ -69,8 +69,8 @@ module.exports = Self => { originalQuantity: newQuantity }, myOptions); - const salesPerson = sale.ticket().client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = sale.ticket().client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const change = $t('Changes in sales', { itemId: sale.itemFk, @@ -85,8 +85,7 @@ module.exports = Self => { changes: change, ticketUrl: `${url}ticket/${sale.ticket().id}/sale`, }); - - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message, myOptions); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/sale/usesMana.js b/modules/ticket/back/methods/sale/usesMana.js index 31beb3a4c..e5cfc6055 100644 --- a/modules/ticket/back/methods/sale/usesMana.js +++ b/modules/ticket/back/methods/sale/usesMana.js @@ -21,15 +21,16 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - const isManaExcluded = await models.WorkerManaExcluded.findById(userId, null, myOptions); + const {departmentFk} = await models.WorkerDepartment.findById(userId, null, myOptions); + const isManaExcluded = await models.DepartmentManaExcluded.findById(departmentFk, null, myOptions); + if (isManaExcluded) return false; const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions); const departments = await models.Department.getLeaves(ctx, salesDepartment.id, null, myOptions); - const workerDepartment = await models.WorkerDepartment.findById(userId, null, myOptions); - if (!workerDepartment) return false; + if (!departmentFk) return false; - const usesMana = departments.find(department => department.id == workerDepartment.departmentFk); + const usesMana = departments.find(department => department.id == departmentFk); return usesMana ? true : false; }; diff --git a/modules/ticket/back/methods/ticket-request/filter.js b/modules/ticket/back/methods/ticket-request/filter.js index 5364cef9a..aaf9e95ae 100644 --- a/modules/ticket/back/methods/ticket-request/filter.js +++ b/modules/ticket/back/methods/ticket-request/filter.js @@ -111,8 +111,8 @@ module.exports = Self => { return {'t.shipped': {lte: value}}; case 'warehouse': return {'w.id': value}; - case 'salesPersonFk': - return {'c.salesPersonFk': value}; + case 'salesDepartmentFk': + return {'c.salesDepartmentFk': value}; } }); @@ -143,9 +143,9 @@ module.exports = Self => { t.warehouseFk, t.clientFk, w.name warehouse, - u.nickname salesPersonNickname, + d.name salesDepartmentName, ua.name attenderName, - c.salesPersonFk, + c.salesDepartmentFk, ua2.name requesterName FROM ticketRequest tr LEFT JOIN ticketWeekly tw on tw.ticketFk = tr.ticketFk @@ -154,8 +154,7 @@ module.exports = Self => { LEFT JOIN client c ON c.id = t.clientFk LEFT JOIN item i ON i.id = tr.itemFk LEFT JOIN sale s ON s.id = tr.saleFk - LEFT JOIN worker wk ON wk.id = c.salesPersonFk - LEFT JOIN account.user u ON u.id = wk.id + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN worker wka ON wka.id = tr.attenderFk LEFT JOIN account.user ua ON ua.id = wka.id LEFT JOIN account.user ua2 ON ua2.id = tr.requesterFk`); diff --git a/modules/ticket/back/methods/ticket-weekly/filter.js b/modules/ticket/back/methods/ticket-weekly/filter.js index 0bf92d542..3b1ecdffa 100644 --- a/modules/ticket/back/methods/ticket-weekly/filter.js +++ b/modules/ticket/back/methods/ticket-weekly/filter.js @@ -53,13 +53,13 @@ module.exports = Self => { const stmts = []; const stmt = new ParameterizedSQL( - `SELECT t.id AS ticketFk, c.id AS clientFk, c.name AS clientName, tw.weekDay, - wh.name AS warehouseName, u.id AS workerFk, u.name AS userName, u.nickName, tw.agencyModeFk + `SELECT t.id ticketFk, c.id clientFk, c.name clientName, tw.weekDay, + wh.name warehouseName, d.id AS departmentFk, d.name departmentName, tw.agencyModeFk FROM ticketWeekly tw JOIN ticket t ON t.id = tw.ticketFk JOIN client c ON c.id = t.clientFk - JOIN account.user u ON u.id = c.salesPersonFk - JOIN warehouse wh ON wh.id = t.warehouseFk` + JOIN warehouse wh ON wh.id = t.warehouseFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk` ); stmt.merge(conn.makeSuffix(filter)); diff --git a/modules/ticket/back/methods/ticket/addSale.js b/modules/ticket/back/methods/ticket/addSale.js index 8dc7a633c..19e9f4e86 100644 --- a/modules/ticket/back/methods/ticket/addSale.js +++ b/modules/ticket/back/methods/ticket/addSale.js @@ -54,9 +54,9 @@ module.exports = Self => { relation: 'client', scope: { include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } @@ -81,8 +81,8 @@ module.exports = Self => { const addition = `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`; - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t('Added sale to ticket', { @@ -90,7 +90,7 @@ module.exports = Self => { ticketUrl: `${url}ticket/${id}/sale`, addition: addition }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/methods/ticket/closeAll.js b/modules/ticket/back/methods/ticket/closeAll.js index 35b9b1e37..11e047f2f 100644 --- a/modules/ticket/back/methods/ticket/closeAll.js +++ b/modules/ticket/back/methods/ticket/closeAll.js @@ -38,11 +38,11 @@ module.exports = Self => { c.id clientFk, c.name clientName, c.email recipient, - c.salesPersonFk, + c.salesDepartmentFk, c.isToBeMailed, c.hasToInvoice, co.hasDailyInvoice, - eu.email salesPersonEmail, + d.notificationEmail salesDepartmentEmail, t.addressFk FROM ticket t JOIN agencyMode am ON am.id = t.agencyModeFk @@ -52,7 +52,7 @@ module.exports = Self => { JOIN client c ON c.id = t.clientFk JOIN province p ON p.id = c.provinceFk JOIN country co ON co.id = p.countryFk - LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code <> 'delivered')) AND DATE(t.shipped) BETWEEN ? - INTERVAL 2 DAY AND util.dayEnd(?) AND t.refFk IS NULL @@ -107,7 +107,6 @@ module.exports = Self => { JOIN province p ON p.id = c.provinceFk LEFT JOIN autonomy a ON a.id = p.autonomyFk JOIN country co ON co.id = p.countryFk - LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk LEFT JOIN itemTaxCountry itc ON itc.itemFk = i.id AND itc.countryFk = su.countryFk LEFT JOIN vn.invoiceOutSerial ios ON ios.taxAreaFk = 'WORLD' diff --git a/modules/ticket/back/methods/ticket/closeByAgency.js b/modules/ticket/back/methods/ticket/closeByAgency.js index eb1aee349..0eb04d294 100644 --- a/modules/ticket/back/methods/ticket/closeByAgency.js +++ b/modules/ticket/back/methods/ticket/closeByAgency.js @@ -44,11 +44,11 @@ module.exports = Self => { t.companyFk, c.name clientName, c.email recipient, - c.salesPersonFk, + c.salesDepartmentFk, c.isToBeMailed, c.hasToInvoice, co.hasDailyInvoice, - eu.email salesPersonEmail + d.notificationEmail salesDepartmentEmail FROM expedition e JOIN ticket t ON t.id = e.ticketFk JOIN ticketState ts ON ts.ticketFk = t.id @@ -56,7 +56,7 @@ module.exports = Self => { JOIN client c ON c.id = t.clientFk JOIN province p ON p.id = c.provinceFk JOIN country co ON co.id = p.countryFk - LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk WHERE al.code = 'PACKED' AND t.agencyModeFk IN(?) AND t.warehouseFk = ? diff --git a/modules/ticket/back/methods/ticket/closeByRoute.js b/modules/ticket/back/methods/ticket/closeByRoute.js index 58e130b8e..0a50cd081 100644 --- a/modules/ticket/back/methods/ticket/closeByRoute.js +++ b/modules/ticket/back/methods/ticket/closeByRoute.js @@ -33,11 +33,11 @@ module.exports = Self => { t.companyFk, c.name clientName, c.email recipient, - c.salesPersonFk, + c.salesDepartmentFk, c.isToBeMailed, c.hasToInvoice, co.hasDailyInvoice, - eu.email salesPersonEmail + d.notificationEmail salesDepartmentEmail FROM expedition e JOIN ticket t ON t.id = e.ticketFk JOIN ticketState ts ON ts.ticketFk = t.id @@ -45,7 +45,7 @@ module.exports = Self => { JOIN client c ON c.id = t.clientFk JOIN province p ON p.id = c.provinceFk JOIN country co ON co.id = p.countryFk - LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk WHERE al.code = 'PACKED' AND t.routeFk = ? AND t.refFk IS NULL diff --git a/modules/ticket/back/methods/ticket/closeByTicket.js b/modules/ticket/back/methods/ticket/closeByTicket.js index 8884897c2..794c6b5f5 100644 --- a/modules/ticket/back/methods/ticket/closeByTicket.js +++ b/modules/ticket/back/methods/ticket/closeByTicket.js @@ -33,11 +33,11 @@ module.exports = Self => { t.companyFk, c.name clientName, c.email recipient, - c.salesPersonFk, + c.salesDepartmentFk, c.isToBeMailed, c.hasToInvoice, co.hasDailyInvoice, - eu.email salesPersonEmail + d.notificationEmail salesDepartmentEmail FROM expedition e JOIN ticket t ON t.id = e.ticketFk JOIN ticketState ts ON ts.ticketFk = t.id @@ -45,7 +45,7 @@ module.exports = Self => { JOIN client c ON c.id = t.clientFk JOIN province p ON p.id = c.provinceFk JOIN country co ON co.id = p.countryFk - LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk + LEFT JOIN department d ON d.id = c.salesDepartmentFk WHERE al.code = 'PACKED' AND t.id = ? AND t.refFk IS NULL diff --git a/modules/ticket/back/methods/ticket/closure.js b/modules/ticket/back/methods/ticket/closure.js index fba39f18f..812871c98 100644 --- a/modules/ticket/back/methods/ticket/closure.js +++ b/modules/ticket/back/methods/ticket/closure.js @@ -36,7 +36,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) { attachments: [], }; - const isToBeMailed = ticket.recipient && ticket.salesPersonFk && ticket.isToBeMailed; + const isToBeMailed = ticket.recipient && ticket.salesDepartmentFk && ticket.isToBeMailed; if (invoiceOut) { const args = { diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 8bea731b7..990e9b0e1 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -182,16 +182,14 @@ module.exports = Self => { { relation: 'client', scope: { - fields: 'salesPersonFk' + fields: 'salesDepartmentFk' }, - include: [ - { - relation: 'address', - scope: { - fields: 'nickname' - } + include: { + relation: 'salesDepartment', + scope: { + fields: ['id', 'chatName'] } - ] + } }, ] }, myOptions); @@ -261,8 +259,8 @@ module.exports = Self => { const oldProperties = await loggable.translateValues(Self, changes.old); const newProperties = await loggable.translateValues(Self, changes.new); - const salesPersonId = ticketToChange.client().salesPersonFk; - if (salesPersonId) { + const salesDepartment = ticketToChange.client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); let changesMade = ''; @@ -278,7 +276,7 @@ module.exports = Self => { ticketUrl: `${url}ticket/${args.id}/sale`, changes: changesMade }); - await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } } diff --git a/modules/ticket/back/methods/ticket/filter.js b/modules/ticket/back/methods/ticket/filter.js index 0823b38b8..76cbb3fa6 100644 --- a/modules/ticket/back/methods/ticket/filter.js +++ b/modules/ticket/back/methods/ticket/filter.js @@ -59,9 +59,9 @@ module.exports = Self => { description: `The warehouse id filter` }, { - arg: 'salesPersonFk', + arg: 'salesDepartmentFk', type: 'number', - description: `The salesperson id filter` + description: `The salesDepartment id filter` }, { arg: 'provinceFk', @@ -73,11 +73,6 @@ module.exports = Self => { type: 'number', description: `The state id filter` }, - { - arg: 'myTeam', - type: 'boolean', - description: `Whether to show only tickets for the current logged user team (For now it shows only the current user tickets)` - }, { arg: 'problems', type: 'boolean', @@ -183,21 +178,14 @@ module.exports = Self => { return {'t.nickname': {like: `%${value}%`}}; case 'refFk': return {'t.refFk': value}; - case 'salesPersonFk': - return {'c.salesPersonFk': value}; + case 'salesDepartmentFk': + return {'c.salesDepartmentFk': value}; case 'provinceFk': return {'a.provinceFk': value}; case 'stateFk': return {'ts.stateFk': value}; case 'collectionFk': return {'cll.id': value}; - case 'mine': - case 'myTeam': - if (value) - return {'c.salesPersonFk': {inq: teamMembersId}}; - else - return {'c.salesPersonFk': {nin: teamMembersId}}; - case 'alertLevel': return {'ts.alertLevel': value}; case 'hasRoute': @@ -210,9 +198,6 @@ module.exports = Self => { return {'t.refFk': null}; case 'pending': return {'st.isNotValidated': value}; - case 'id': - case 'clientFk': - case 'agencyModeFk': case 'warehouseFk': param = `t.${param}`; return {[param]: value}; @@ -249,12 +234,11 @@ module.exports = Self => { am.id AS agencyModeFk, st.name AS state, st.classColor, - wk.lastName AS salesPerson, + d.name AS salesDepartment, ts.stateFk AS stateFk, ts.alertLevel AS alertLevel, ts.code AS alertLevelCode, - u.name AS userName, - c.salesPersonFk, + c.salesDepartmentFk, z.hour AS zoneLanding, HOUR(z.hour) AS zoneHour, MINUTE(z.hour) AS zoneMinute, @@ -271,8 +255,7 @@ module.exports = Self => { LEFT JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN client c ON c.id = t.clientFk - LEFT JOIN worker wk ON wk.id = c.salesPersonFk - LEFT JOIN account.user u ON u.id = wk.id + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN route r ON r.id = t.routeFk`); if (args.orderFk) { diff --git a/modules/ticket/back/methods/ticket/getSalesPersonMana.js b/modules/ticket/back/methods/ticket/getSalesDepartmentMana.js similarity index 67% rename from modules/ticket/back/methods/ticket/getSalesPersonMana.js rename to modules/ticket/back/methods/ticket/getSalesDepartmentMana.js index c200f869a..b6b58b340 100644 --- a/modules/ticket/back/methods/ticket/getSalesPersonMana.js +++ b/modules/ticket/back/methods/ticket/getSalesDepartmentMana.js @@ -1,6 +1,6 @@ module.exports = Self => { - Self.remoteMethod('getSalesPersonMana', { - description: 'Returns the mana amount of a salesperson for a ticket', + Self.remoteMethod('getSalesDepartmentMana', { + description: 'Returns the mana amount of a salesDepartment for a ticket', accessType: 'READ', accepts: [{ arg: 'id', @@ -13,12 +13,12 @@ module.exports = Self => { root: true }, http: { - path: `/:id/getSalesPersonMana`, + path: `/:id/getSalesDepartmentMana`, verb: 'GET' } }); - Self.getSalesPersonMana = async(ticketId, options) => { + Self.getSalesDepartmentMana = async(ticketId, options) => { const myOptions = {}; const models = Self.app.models; @@ -29,7 +29,7 @@ module.exports = Self => { include: [{ relation: 'client', scope: { - fields: ['salesPersonFk'] + fields: ['salesDepartmentFk'] } }], fields: ['id', 'clientFk'] @@ -37,10 +37,7 @@ module.exports = Self => { if (!ticket) return 0; - const mana = await models.WorkerMana.findOne({ - where: { - workerFk: ticket.client().salesPersonFk - }, + const mana = await models.DepartmentMana.findOne({ fields: 'amount' }, myOptions); diff --git a/modules/ticket/back/methods/ticket/restore.js b/modules/ticket/back/methods/ticket/restore.js index 01b5d1652..43ed47dea 100644 --- a/modules/ticket/back/methods/ticket/restore.js +++ b/modules/ticket/back/methods/ticket/restore.js @@ -42,7 +42,13 @@ module.exports = Self => { include: [{ relation: 'client', scope: { - fields: ['id', 'salesPersonFk'] + fields: ['id', 'salesDepartmentFk'], + include: { + relation: 'salesDepartment', + scope: { + fields: ['id', 'chatName'] + } + } } }] }, myOptions); @@ -53,15 +59,15 @@ module.exports = Self => { if (!ticketLog || now > maxDate) throw new UserError(`You can only restore a ticket within the first hour after deletion`); - // Send notification to salesPerson - const salesPersonId = ticket.client().salesPersonFk; - if (salesPersonId) { + // Send notification to department + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t(`I have restored the ticket id`, { id: id, url: `${url}ticket/${id}/summary` }); - await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } const fullYear = Date.vnNew().getFullYear(); diff --git a/modules/ticket/back/methods/ticket/setDeleted.js b/modules/ticket/back/methods/ticket/setDeleted.js index 2afdf44ac..492759892 100644 --- a/modules/ticket/back/methods/ticket/setDeleted.js +++ b/modules/ticket/back/methods/ticket/setDeleted.js @@ -108,26 +108,26 @@ module.exports = Self => { include: [{ relation: 'client', scope: { - fields: ['id', 'salesPersonFk'], + fields: ['id', 'salesDepartmentFk'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } }] }, myOptions); - // Send notification to salesPerson - const salesPersonUser = ticket.client().salesPersonUser(); - if (salesPersonUser && sales.length) { + // Send notification to salesDepartment + const salesDepartment = ticket.client().salesDepartment(); + if (salesDepartment && sales.length) { const url = await Self.app.models.Url.getUrl(); const message = $t(`I have deleted the ticket id`, { id: id, url: `${url}ticket/${id}/summary` }); - await models.Chat.send(ctx, `@${salesPersonUser.name}`, message); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } const updatedTicket = await ticket.updateAttribute('isDeleted', true, myOptions); diff --git a/modules/ticket/back/methods/ticket/specs/filter.spec.js b/modules/ticket/back/methods/ticket/specs/filter.spec.js index 1d050931b..04f4725cd 100644 --- a/modules/ticket/back/methods/ticket/specs/filter.spec.js +++ b/modules/ticket/back/methods/ticket/specs/filter.spec.js @@ -172,15 +172,14 @@ describe('ticket filter()', () => { } }); - it('should return the tickets from the worker team', async() => { + it('should return the tickets from the salesDeparment', async() => { const tx = await models.Ticket.beginTransaction({}); const ctx = beforeAll.getCtx(18); try { const options = {transaction: tx}; - const args = {myTeam: true}; - ctx.args = args; + const ctx = {req: {accessToken: {userId: 18}}, args: {salesDepartmentFk: 80}}; const filter = {}; const result = await models.Ticket.filter(ctx, filter, options); @@ -193,15 +192,13 @@ describe('ticket filter()', () => { } }); - it('should return the tickets that are not from the worker team', async() => { - const ctx = beforeAll.getCtx(18); + it('should return the tickets that are not from the salesDeparment', async() => { const tx = await models.Ticket.beginTransaction({}); try { const options = {transaction: tx}; - const args = {myTeam: false}; - ctx.args = args; + const ctx = {req: {accessToken: {userId: 18}}, args: {salesDepartmentFk: 80}}; const filter = {}; const result = await models.Ticket.filter(ctx, filter, options); diff --git a/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js b/modules/ticket/back/methods/ticket/specs/getSalesDepartmentMana.spec.js similarity index 67% rename from modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js rename to modules/ticket/back/methods/ticket/specs/getSalesDepartmentMana.spec.js index 6029ca4a7..4e05a2420 100644 --- a/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getSalesDepartmentMana.spec.js @@ -1,15 +1,15 @@ const models = require('vn-loopback/server/server').models; -describe('ticket getSalesPersonMana()', () => { - it('should get the mana of a salesperson of a given ticket', async() => { +describe('ticket getSalesDepartmentMana()', () => { + it('should get the mana of a salesDepartment of a given ticket', async() => { const tx = await models.Ticket.beginTransaction({}); try { const options = {transaction: tx}; - const mana = await models.Ticket.getSalesPersonMana(1, options); + const mana = await models.Ticket.getSalesDepartmentMana(1, options); - expect(mana).toEqual(124); + expect(mana).toEqual(129); await tx.rollback(); } catch (e) { @@ -24,7 +24,7 @@ describe('ticket getSalesPersonMana()', () => { try { const options = {transaction: tx}; - const mana = await models.Ticket.getSalesPersonMana(99, options); + const mana = await models.Ticket.getSalesDepartmentMana(99, options); expect(mana).toEqual(0); diff --git a/modules/ticket/back/methods/ticket/specs/updateDiscount.spec.js b/modules/ticket/back/methods/ticket/specs/updateDiscount.spec.js index 272387a30..6467e2d8e 100644 --- a/modules/ticket/back/methods/ticket/specs/updateDiscount.spec.js +++ b/modules/ticket/back/methods/ticket/specs/updateDiscount.spec.js @@ -71,7 +71,7 @@ describe('sale updateDiscount()', () => { expect(error.message).toEqual(`The sales of this ticket can't be modified`); }); - it('should update the discount if the salesPerson has mana and manaCode = "mana"', async() => { + it('should update the discount with component mana', async() => { const tx = await models.Ticket.beginTransaction({}); const ctx = beforeAll.getCtx(18); @@ -85,11 +85,11 @@ describe('sale updateDiscount()', () => { const componentId = manaDiscount.id; const manaCode = 'mana'; - const teamOne = 96; + const departmentId = 96; const userId = ctx.req.accessToken.userId; - const business = await models.Business.findOne({where: {workerFk: userId}}, options); - await business.updateAttribute('departmentFk', teamOne, options); + const business = await models.Business.findOne({where: {workerFk: userId}}, options); + await business.updateAttribute('departmentFk', departmentId, options); await models.Ticket.updateDiscount(ctx, ticketId, sales, newDiscount, manaCode, options); const updatedSale = await models.Sale.findById(originalSaleId, null, options); diff --git a/modules/ticket/back/methods/ticket/summary.js b/modules/ticket/back/methods/ticket/summary.js index 6bfa478ec..4132312e5 100644 --- a/modules/ticket/back/methods/ticket/summary.js +++ b/modules/ticket/back/methods/ticket/summary.js @@ -61,9 +61,9 @@ module.exports = Self => { {relation: 'zone', scope: {fields: ['name']}}, {relation: 'client', scope: { - fields: ['salesPersonFk', 'name', 'phone', 'mobile'], + fields: ['salesDepartmentFk', 'name', 'phone', 'mobile'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { fields: ['id', 'name'] } diff --git a/modules/ticket/back/methods/ticket/updateDiscount.js b/modules/ticket/back/methods/ticket/updateDiscount.js index 2e8bec27a..f527318e7 100644 --- a/modules/ticket/back/methods/ticket/updateDiscount.js +++ b/modules/ticket/back/methods/ticket/updateDiscount.js @@ -66,7 +66,7 @@ module.exports = Self => { include: { relation: 'client', scope: { - fields: ['salesPersonFk'] + fields: ['salesDepartmentFk'] } }, fields: ['id', 'clientFk'] @@ -83,7 +83,6 @@ module.exports = Self => { if (!allFromSameTicket) throw new UserError('All sales must belong to the same ticket'); - const userId = ctx.req.accessToken.userId; const isLocked = await models.Ticket.isLocked(id, myOptions); const canEditDiscount = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'editDiscount'); @@ -146,32 +145,34 @@ module.exports = Self => { await Promise.all(promises); - await Self.rawSql('CALL vn.manaSpellersRequery(?)', [userId], myOptions); - await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [id], myOptions); - const ticket = await models.Ticket.findById(id, { include: { relation: 'client', scope: { + fields: ['salesDepartmentFk', 'name'], include: { - relation: 'salesPersonUser', + relation: 'salesDepartment', scope: { - fields: ['id', 'name'] + fields: ['id', 'chatName'] } } } } }, myOptions); - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { + const salesDepartment = ticket.client().salesDepartment(); + + await Self.rawSql('CALL vn.manaSpellers_requery(?)', [salesDepartment.id], myOptions); + await Self.rawSql('CALL vn.ticket_recalc(?, NULL)', [id], myOptions); + + if (salesDepartment) { const url = await Self.app.models.Url.getUrl(); const message = $t('Changed sale discount', { ticketId: id, ticketUrl: `${url}ticket/${id}/sale`, changes: changesMade }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message, myOptions); + await models.Chat.send(ctx, `@${salesDepartment.chatName}`, message); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 5582dde5c..7b9ed2714 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -10,7 +10,7 @@ module.exports = function(Self) { require('../methods/ticket/setDeleted')(Self); require('../methods/ticket/restore')(Self); require('../methods/ticket/getSales')(Self); - require('../methods/ticket/getSalesPersonMana')(Self); + require('../methods/ticket/getSalesDepartmentMana')(Self); require('../methods/ticket/filter')(Self); require('../methods/ticket/canBeInvoiced')(Self); require('../methods/ticket/makeInvoice')(Self); diff --git a/modules/worker/back/model-config.json b/modules/worker/back/model-config.json index 651228c04..fb636707f 100644 --- a/modules/worker/back/model-config.json +++ b/modules/worker/back/model-config.json @@ -20,6 +20,9 @@ "Department": { "dataSource": "vn" }, + "DepartmentMana": { + "dataSource": "vn" + }, "Device": { "dataSource": "vn" }, @@ -86,12 +89,6 @@ "WorkerLog": { "dataSource": "vn" }, - "WorkerMana": { - "dataSource": "vn" - }, - "WorkerManaExcluded": { - "dataSource": "vn" - }, "WorkerMistake": { "dataSource": "vn" }, @@ -139,6 +136,9 @@ }, "TrainingCenter": { "dataSource": "vn" + }, + "DepartmentManaExcluded": { + "dataSource": "vn" } } diff --git a/modules/worker/back/models/worker-mana.json b/modules/worker/back/models/department-mana.json similarity index 59% rename from modules/worker/back/models/worker-mana.json rename to modules/worker/back/models/department-mana.json index 0e50b8f63..7f553fa65 100644 --- a/modules/worker/back/models/worker-mana.json +++ b/modules/worker/back/models/department-mana.json @@ -1,25 +1,25 @@ { - "name": "WorkerMana", + "name": "DepartmentMana", "base": "VnModel", "options": { "mysql": { - "table": "workerMana" + "table": "departmentMana" } }, "properties": { "amount": { "type": "number" }, - "workerFk": { + "salesDepartmentFk": { "id": true, "type": "number" } }, "relations": { - "worker": { + "department": { "type": "belongsTo", - "model": "Worker", - "foreignKey": "workerFk" + "model": "Department", + "foreignKey": "salesDepartmentFk" } } } diff --git a/modules/worker/back/models/sales-department-mana-excluded.json b/modules/worker/back/models/sales-department-mana-excluded.json new file mode 100644 index 000000000..9ff27eae2 --- /dev/null +++ b/modules/worker/back/models/sales-department-mana-excluded.json @@ -0,0 +1,22 @@ +{ + "name": "DepartmentManaExcluded", + "base": "VnModel", + "options": { + "mysql": { + "table": "departmentManaExcluded" + } + }, + "properties": { + "salesDepartmentFk": { + "id": true, + "type": "number" + } + }, + "relations": { + "department": { + "type": "belongsTo", + "model": "Department", + "foreignKey": "salesDepartmentFk" + } + } +} diff --git a/modules/worker/back/models/worker-mana-excluded.json b/modules/worker/back/models/worker-mana-excluded.json deleted file mode 100644 index 6610b701b..000000000 --- a/modules/worker/back/models/worker-mana-excluded.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "WorkerManaExcluded", - "base": "VnModel", - "options": { - "mysql": { - "table": "workerManaExcluded" - } - }, - "properties": { - "workerFk": { - "id": true, - "type": "number" - } - }, - "relations": { - "worker": { - "type": "belongsTo", - "model": "Worker", - "foreignKey": "workerFk" - } - } -} diff --git a/print/templates/email/client-welcome/client-welcome.html b/print/templates/email/client-welcome/client-welcome.html index 3554b6e92..75b740f82 100644 --- a/print/templates/email/client-welcome/client-welcome.html +++ b/print/templates/email/client-welcome/client-welcome.html @@ -39,16 +39,16 @@

-

- {{$t('salesPersonName')}}: {{client.salesPersonName}} +
+ {{$t('salesDepartmentName')}}: {{client.salesDepartmentName}}
-
- {{$t('salesPersonPhone')}}: {{client.salesPersonPhone}} +
+ {{$t('salesDepartmentPhone')}}: {{client.salesDepartmentPhone}}
-
- {{$t('salesPersonEmail')}}: - {{client.salesPersonEmail}} +
+ {{$t('salesDepartmentEmail')}}: + {{client.salesDepartmentEmail}}

diff --git a/print/templates/email/client-welcome/locale/es.yml b/print/templates/email/client-welcome/locale/es.yml index 1f3ef3704..c3ec7fc91 100644 --- a/print/templates/email/client-welcome/locale/es.yml +++ b/print/templates/email/client-welcome/locale/es.yml @@ -50,6 +50,6 @@ sections: día de la recepción). Pasado este plazo no se aceptará ninguna reclamación. help: Cualquier duda que te surja, no dudes en consultarla, ¡estamos para atenderte! -salesPersonName: Soy tu comercial y mi nombre es -salesPersonPhone: Teléfono y whatsapp -salesPersonEmail: Dirección de e-mail +salesDepartmentName: Somos tu equipo comercial +salesDepartmentPhone: Teléfono y whatsapp +salesDepartmentEmail: Dirección de e-mail diff --git a/print/templates/email/client-welcome/locale/fr.yml b/print/templates/email/client-welcome/locale/fr.yml index be064d80d..c7729d289 100644 --- a/print/templates/email/client-welcome/locale/fr.yml +++ b/print/templates/email/client-welcome/locale/fr.yml @@ -46,6 +46,6 @@ sections: description: Verdnatura acceptera les réclamations effectuées dans les deux jours civils suivant la réception de la commande (y compris le jour même de la réception). Passé ce délai, aucune réclamation ne sera acceptée. help: Si vous avez des questions, n'hésitez pas à nous contacter, nous sommes là pour vous aider ! -salesPersonName: Je suis votre commercial et mon nom est -salesPersonPhone: Téléphone et Whatsapp -salesPersonEmail: Adresse e-mail +salesDepartmentName: Nous sommes votre service commercial +salesDepartmentPhone: Téléphone et Whatsapp +salesDepartmentEmail: Adresse e-mail diff --git a/print/templates/email/client-welcome/locale/pt.yml b/print/templates/email/client-welcome/locale/pt.yml index a03b2f848..96a52d8ea 100644 --- a/print/templates/email/client-welcome/locale/pt.yml +++ b/print/templates/email/client-welcome/locale/pt.yml @@ -44,7 +44,7 @@ sections: dia da receção). Passado este prazo não se aceitará nenhuma reclamação. help: Qualquer dúvida que lhe surja, não hesite em consultá-la estamos para atender-te! -salesPersonName: Sou o seu asesor comercial e o meu nome é -salesPersonPhone: Telemovel e whatsapp -salesPersonEmail: Correio eletrônico +salesDepartmentName: Nós somos sua equipe comercial +salesDepartmentPhone: Telemovel e whatsapp +salesDepartmentEmail: Correio eletrônico diff --git a/print/templates/email/client-welcome/sql/client.sql b/print/templates/email/client-welcome/sql/client.sql index 59bdeae4a..c0ac811f4 100644 --- a/print/templates/email/client-welcome/sql/client.sql +++ b/print/templates/email/client-welcome/sql/client.sql @@ -1,11 +1,11 @@ SELECT c.id, - u.name AS userName, - CONCAT(w.lastName, ' ', w.firstName) salesPersonName, - w.phone AS salesPersonPhone, - CONCAT(wu.name, '@verdnatura.es') AS salesPersonEmail + u.name userName, + c.email recipient, + CONCAT(d.name) salesDepartmentName, + d.pbxQueue salesDepartmentPhone, + d.notificationEmail salesDeparmentEmail FROM client c JOIN account.user u ON u.id = c.id - LEFT JOIN worker w ON w.id = c.salesPersonFk - LEFT JOIN account.user wu ON wu.id = w.id + LEFT JOIN department d ON d.id = c.salesDepartmentFk WHERE c.id = ? diff --git a/print/templates/email/printer-setup/locale/es.yml b/print/templates/email/printer-setup/locale/es.yml index 39e83b1a8..46b5b1c17 100644 --- a/print/templates/email/printer-setup/locale/es.yml +++ b/print/templates/email/printer-setup/locale/es.yml @@ -38,6 +38,6 @@ sections: title='Soporte Verdnatura' target='_blank' style='color:#8dba25'>http://soporte.verdnatura.es
. help: Cualquier duda que te surja, no dudes en consultarla, ¡estamos para atenderte! -salesPersonName: Soy tu comercial y mi nombre es -salesPersonPhone: Teléfono y whatsapp -salesPersonEmail: Dirección de e-mail +salesDepartmentName: Somos tu equipo comerical +salesDepartmentPhone: Teléfono y whatsapp +salesDepartmentEmail: Dirección de e-mail diff --git a/print/templates/email/printer-setup/printer-setup.html b/print/templates/email/printer-setup/printer-setup.html index fe4db90d3..0be0254a8 100644 --- a/print/templates/email/printer-setup/printer-setup.html +++ b/print/templates/email/printer-setup/printer-setup.html @@ -26,16 +26,16 @@
-
- {{$t('salesPersonName')}}: {{client.salesPersonName}} +
+ {{$t('salesDepartmentName')}}: {{client.salesDepartmentName}}
-
- {{$t('salesPersonPhone')}}: {{client.salesPersonPhone}} +
+ {{$t('salesDepartmentPhone')}}: {{client.salesDepartmentPhone}}
-
diff --git a/print/templates/email/printer-setup/sql/client.sql b/print/templates/email/printer-setup/sql/client.sql index 31454408f..c0ac811f4 100644 --- a/print/templates/email/printer-setup/sql/client.sql +++ b/print/templates/email/printer-setup/sql/client.sql @@ -1,12 +1,11 @@ SELECT c.id, - u.name AS userName, - c.email recipient, - CONCAT(w.lastName, ' ', w.firstName) salesPersonName, - w.phone AS salesPersonPhone, - CONCAT(wu.name, '@verdnatura.es') AS salesPersonEmail + u.name userName, + c.email recipient, + CONCAT(d.name) salesDepartmentName, + d.pbxQueue salesDepartmentPhone, + d.notificationEmail salesDeparmentEmail FROM client c JOIN account.user u ON u.id = c.id - LEFT JOIN worker w ON w.id = c.salesPersonFk - LEFT JOIN account.user wu ON wu.id = w.id + LEFT JOIN department d ON d.id = c.salesDepartmentFk WHERE c.id = ? diff --git a/print/templates/reports/driver-route/driver-route.html b/print/templates/reports/driver-route/driver-route.html index 109afd2f5..cfb4af7f0 100644 --- a/print/templates/reports/driver-route/driver-route.html +++ b/print/templates/reports/driver-route/driver-route.html @@ -123,8 +123,8 @@ {{$t('warehouse')}} {{ticket.warehouseName}} - {{$t('salesPerson')}} - {{ticket.salesPersonName}} + {{$t('salesDepartment')}} + {{ticket.salesDepartmentName}} diff --git a/print/templates/reports/driver-route/locale/es.yml b/print/templates/reports/driver-route/locale/es.yml index 8f986b0d2..7ee153c52 100644 --- a/print/templates/reports/driver-route/locale/es.yml +++ b/print/templates/reports/driver-route/locale/es.yml @@ -18,7 +18,7 @@ city: Ciudad mobile: Móvil phone: Teléfono warehouse: Almacén -salesPerson: Comercial +salesDepartment: Equipo Comercial import: Importe route: Ruta routeId: Ruta {0} diff --git a/print/templates/reports/driver-route/sql/tickets.sql b/print/templates/reports/driver-route/sql/tickets.sql index 1ffb4d623..30edd5b2a 100644 --- a/print/templates/reports/driver-route/sql/tickets.sql +++ b/print/templates/reports/driver-route/sql/tickets.sql @@ -16,14 +16,13 @@ SELECT t.nickname addressName, 0 import, am.name ticketAgency, tob.description, - u.nickName salesPersonName, + d.name salesDepartmentName, ipkg.itemPackingTypes FROM route r JOIN ticket t ON t.routeFk = r.id LEFT JOIN address a ON a.id = t.addressFk LEFT JOIN client c ON c.id = t.clientFk - LEFT JOIN worker w ON w.id = client_getSalesPerson(t.clientFk, CURDATE()) - LEFT JOIN account.user u ON u.id = w.id + LEFT JOIN department d ON d.id = c.salesDepartmentFk LEFT JOIN ticketObservation tob ON tob.ticketFk = t.id AND tob.observationTypeFk = 3 LEFT JOIN province p ON a.provinceFk = p.id LEFT JOIN warehouse wh ON wh.id = t.warehouseFk