new models and cau 10507
This commit is contained in:
parent
2f38f6e88f
commit
6d9fee06f6
|
@ -0,0 +1,76 @@
|
||||||
|
|
||||||
|
DROP function IF EXISTS `vn`.`clientGetSalesPerson`;
|
||||||
|
DELIMITER $$
|
||||||
|
CREATE DEFINER=`root`@`%` FUNCTION `vn`.`clientGetSalesPerson`(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 vSalesperson INT DEFAULT NULL;
|
||||||
|
DECLARE vSubstitute INT DEFAULT NULL;
|
||||||
|
DECLARE vLoop BOOLEAN;
|
||||||
|
|
||||||
|
-- Obtiene el comercial original y el de sharingClient
|
||||||
|
|
||||||
|
SELECT c.salesPersonFk, s.workerFk
|
||||||
|
INTO vSalesperson, vSubstitute
|
||||||
|
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 vSubstitute IS NOT NULL
|
||||||
|
THEN
|
||||||
|
SET vSalesperson = vSubstitute;
|
||||||
|
ELSEIF vSalesperson IS NOT NULL
|
||||||
|
THEN
|
||||||
|
DROP TEMPORARY TABLE IF EXISTS tmp.stack;
|
||||||
|
CREATE TEMPORARY TABLE tmp.stack
|
||||||
|
(INDEX (substitute))
|
||||||
|
ENGINE = MEMORY
|
||||||
|
SELECT vSalesperson substitute;
|
||||||
|
|
||||||
|
l: LOOP
|
||||||
|
SELECT workerSubstitute INTO vSubstitute
|
||||||
|
FROM sharingCart
|
||||||
|
WHERE vDated BETWEEN started AND ended
|
||||||
|
AND workerFk = vSalesperson
|
||||||
|
ORDER BY id
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
IF vSubstitute IS NULL THEN
|
||||||
|
LEAVE l;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
SELECT COUNT(*) > 0 INTO vLoop
|
||||||
|
FROM tmp.stack WHERE substitute = vSubstitute;
|
||||||
|
|
||||||
|
IF vLoop THEN
|
||||||
|
LEAVE l;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
INSERT INTO tmp.stack SET
|
||||||
|
substitute = vSubstitute;
|
||||||
|
|
||||||
|
SET vSalesperson = vSubstitute;
|
||||||
|
END LOOP;
|
||||||
|
|
||||||
|
DROP TEMPORARY TABLE tmp.stack;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
RETURN vSalesperson;
|
||||||
|
END$$
|
||||||
|
|
||||||
|
DELIMITER ;
|
|
@ -0,0 +1,14 @@
|
||||||
|
CREATE
|
||||||
|
ALGORITHM = UNDEFINED
|
||||||
|
DEFINER = `root`@`%`
|
||||||
|
SQL SECURITY DEFINER
|
||||||
|
VIEW `vn`.`sharingCart` AS
|
||||||
|
SELECT
|
||||||
|
`s`.`id` AS `id`,
|
||||||
|
`s`.`Id_Trabajador` AS `workerFk`,
|
||||||
|
`s`.`datSTART` AS `started`,
|
||||||
|
`s`.`datEND` AS `ended`,
|
||||||
|
`s`.`Id_Suplente` AS `workerSubstitute`,
|
||||||
|
`s`.`odbc_date` AS `created`
|
||||||
|
FROM
|
||||||
|
`vn2008`.`sharingcart` `s`
|
|
@ -0,0 +1,13 @@
|
||||||
|
CREATE
|
||||||
|
OR REPLACE ALGORITHM = UNDEFINED
|
||||||
|
DEFINER = `root`@`%`
|
||||||
|
SQL SECURITY DEFINER
|
||||||
|
VIEW `vn`.`sharingClient` AS
|
||||||
|
SELECT
|
||||||
|
`s`.`id` AS `id`,
|
||||||
|
`s`.`Id_Trabajador` AS `workerFk`,
|
||||||
|
`s`.`datSTART` AS `started`,
|
||||||
|
`s`.`datEND` AS `ended`,
|
||||||
|
`s`.`Id_Cliente` AS `clientFk`
|
||||||
|
FROM
|
||||||
|
`vn2008`.`sharingclient` `s`;
|
|
@ -1317,3 +1317,11 @@ INSERT INTO `vn`.`smsConfig` (`id`, `uri`, `user`, `password`, `title`)
|
||||||
VALUES
|
VALUES
|
||||||
('1', 'https://websms.xtratelecom.es/api_php/server.wsdl', 'VERDINATURA', '182wbOKu', 'Verdnatura');
|
('1', 'https://websms.xtratelecom.es/api_php/server.wsdl', 'VERDINATURA', '182wbOKu', 'Verdnatura');
|
||||||
|
|
||||||
|
INSERT INTO `vn`.`sharingClient`(`id`, `workerFk`, `started`, `ended`, `clientFk`)
|
||||||
|
VALUES
|
||||||
|
(1, 19, DATE_ADD(CURDATE(), INTERVAL -5 DAY), DATE_ADD(CURDATE(), INTERVAL +15 DAY), 101),
|
||||||
|
(2, 18, DATE_ADD(CURDATE(), INTERVAL -5 DAY), DATE_ADD(CURDATE(), INTERVAL +15 DAY), 106);
|
||||||
|
|
||||||
|
INSERT INTO `vn`.`sharingCart`(`id`, `workerFk`, `started`, `ended`, `workerSubstitute`, `created`)
|
||||||
|
VALUES
|
||||||
|
(1, 18, DATE_ADD(CURDATE(), INTERVAL -5 DAY), DATE_ADD(CURDATE(), INTERVAL +15 DAY), 19, DATE_ADD(CURDATE(), INTERVAL -5 DAY));
|
Loading…
Reference in New Issue