update structure

This commit is contained in:
Bernat 2018-07-17 08:34:27 +02:00
parent ee1ee77240
commit 7e1a2025a5
1 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,60 @@
USE `vn`;
DROP procedure IF EXISTS `manaSpellersRequery`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `manaSpellersRequery`(worker INTEGER)
BEGIN
DECLARE vWorkerIsExcluded BOOLEAN;
DECLARE vFromDated DATE;
DECLARE vToDated DATE DEFAULT TIMESTAMPADD(DAY,1,CURDATE());
SELECT COUNT(*) INTO vWorkerIsExcluded
FROM bs.manaSpellersExcluded
WHERE workerFk = worker;
IF NOT vWorkerIsExcluded THEN
SELECT max(dated) INTO vFromDated
FROM clientManaCache;
SELECT worker, sum(mana) FROM
(
SELECT s.quantity * sc.value as mana
FROM ticket t
JOIN address a ON a.id = t.addressFk
JOIN client c ON c.id = a.clientFk
JOIN sale s ON s.ticketFk = t.id
JOIN saleComponent sc ON sc.saleFk = s.id
WHERE c.salesPersonFk = worker AND componentFk IN (39, 37) -- maná auto y maná
AND t.shipped > vFromDated AND t.shipped < vToDated
UNION ALL
SELECT - r.amountPaid
FROM receipt r
JOIN client c ON c.id = r.clientFk
WHERE c.salesPersonFk = worker AND bankFk = 66
AND payed > vFromDated
UNION ALL
SELECT g.amount
FROM greuge g
JOIN client c ON c.id = g.clientFk
WHERE c.salesPersonFk = worker AND g.greugeTypeFk = 3 -- Maná
AND g.shipped > vFromDated and g.shipped < CURDATE()
UNION ALL
SELECT mana
FROM clientManaCache cc
JOIN client c ON c.id = cc.clientFk
WHERE c.salesPersonFk = worker AND cc.dated = vFromDated
) sub;
END IF;
END$$
DELIMITER ;