diff --git a/services/db/install/changes/1.0.8/manaSpellersRequery.sql b/services/db/install/changes/1.0.8/manaSpellersRequery.sql new file mode 100644 index 000000000..fa24d46b6 --- /dev/null +++ b/services/db/install/changes/1.0.8/manaSpellersRequery.sql @@ -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 ; +