diff --git a/db/changes/10300-newFacility/00-weekWaste.sql b/db/changes/10300-newFacility/00-weekWaste.sql index efa988aea..4883a49fc 100644 --- a/db/changes/10300-newFacility/00-weekWaste.sql +++ b/db/changes/10300-newFacility/00-weekWaste.sql @@ -1,24 +1,28 @@ -drop procedure weekWaste; +DROP PROCEDURE IF EXISTS `bs`.`weekWaste`; -create definer = root@`%` procedure weekWaste__() +DELIMITER $$ +$$ +CREATE DEFINER = `root`@`%` PROCEDURE `bs`.`weekWaste__`() BEGIN - DECLARE vWeek INT; + DECLARE vWeek INT; DECLARE vYear INT; SELECT week, year - INTO vWeek, vYear - FROM vn.time - WHERE dated = DATE_ADD(CURDATE(), INTERVAL -1 WEEK); + INTO vWeek, vYear + FROM vn.time + WHERE dated = DATE_ADD(CURDATE(), INTERVAL -1 WEEK); - SELECT *, 100 * dwindle / total AS percentage - FROM ( - SELECT buyer, - sum(saleTotal) as total, - sum(saleWaste) as dwindle - FROM bs.waste - WHERE year = vYear and week = vWeek - GROUP BY buyer - ) sub - ORDER BY percentage DESC; -END; + SELECT *, 100 * dwindle / total AS percentage + FROM ( + SELECT buyer, + SUM(saleTotal) AS total, + SUM(saleWaste) AS dwindle + FROM bs.waste + WHERE year = vYear + AND week = vWeek + GROUP BY buyer + ) sub + ORDER BY percentage DESC; +END;$$ +DELIMITER ; diff --git a/db/changes/10300-newFacility/00-weekWaste_byWorker.sql b/db/changes/10300-newFacility/00-weekWaste_byWorker.sql index 2da098ae6..ff660494f 100644 --- a/db/changes/10300-newFacility/00-weekWaste_byWorker.sql +++ b/db/changes/10300-newFacility/00-weekWaste_byWorker.sql @@ -1,28 +1,32 @@ -drop procedure weekWaste_byWorker; +DROP PROCEDURE IF EXISTS `bs`.`weekWaste_byWorker`; -create definer = root@`%` procedure weekWaste_byWorker__(IN vWorkerFk int) +DELIMITER $$ +$$ +CREATE + DEFINER = root@`%` PROCEDURE `bs`.`weekWaste_byWorker__`(IN vWorkerFk INT) BEGIN - DECLARE vWeek INT; + DECLARE vWeek INT; DECLARE vYear INT; - + SELECT week, year - INTO vWeek, vYear - FROM vn.time - WHERE dated = TIMESTAMPADD(WEEK,-1,CURDATE()); + INTO vWeek, vYear + FROM vn.time + WHERE dated = TIMESTAMPADD(WEEK, -1, CURDATE()); - SELECT *, 100 * mermas / total as porcentaje - FROM ( - SELECT ws.family, - sum(ws.saleTotal) as total, - sum(ws.saleWaste) as mermas - FROM bs.waste ws + SELECT *, 100 * mermas / total AS porcentaje + FROM ( + SELECT ws.family, + SUM(ws.saleTotal) AS total, + SUM(ws.saleWaste) AS mermas + FROM bs.waste ws JOIN vn.worker w ON w.user = ws.buyer - WHERE year = vYear AND week = vWeek - AND w.id = vWorkerFk - GROUP BY family - - ) sub - ORDER BY porcentaje DESC; -END; + WHERE year = vYear + AND week = vWeek + AND w.id = vWorkerFk + GROUP BY family + ) sub + ORDER BY porcentaje DESC; +END;;$$ +DELIMITER ; diff --git a/db/changes/10300-newFacility/00-weekWaste_getDetail.sql b/db/changes/10300-newFacility/00-weekWaste_getDetail.sql index 4dd0cab6c..5cf107d24 100644 --- a/db/changes/10300-newFacility/00-weekWaste_getDetail.sql +++ b/db/changes/10300-newFacility/00-weekWaste_getDetail.sql @@ -1,25 +1,30 @@ -drop procedure weekWaste_getDetail; +DROP PROCEDURE IF EXISTS `bs`.`weekWaste_getDetail`; -create definer = root@`%` procedure weekWaste_getDetail__() +DELIMITER $$ +$$ +CREATE + DEFINER = root@`%` PROCEDURE `bs`.`weekWaste_getDetail__`() BEGIN - DECLARE vLastWeek DATE; - DECLARE vWeek INT; + DECLARE vLastWeek DATE; + DECLARE vWeek INT; DECLARE vYear INT; - - SET vLastWeek = TIMESTAMPADD(WEEK,-1,CURDATE()); + + SET vLastWeek = TIMESTAMPADD(WEEK, -1, CURDATE()); SET vYear = YEAR(vLastWeek); SET vWeek = WEEK(vLastWeek, 1); - - SELECT *, 100 * dwindle / total AS percentage - FROM ( - SELECT buyer, - ws.family, - sum(ws.saleTotal) AS total, - sum(ws.saleWaste) AS dwindle - FROM bs.waste ws - WHERE year = vYear AND week = vWeek - GROUP BY buyer, family - ) sub - ORDER BY percentage DESC; -END; + + SELECT *, 100 * dwindle / total AS percentage + FROM ( + SELECT buyer, + ws.family, + SUM(ws.saleTotal) AS total, + SUM(ws.saleWaste) AS dwindle + FROM bs.waste ws + WHERE year = vYear + AND week = vWeek + GROUP BY buyer, family + ) sub + ORDER BY percentage DESC; +END;$$ +DELIMITER ; diff --git a/db/changes/10300-newFacility/01-waste.sql b/db/changes/10300-newFacility/01-waste.sql index 1f7f5fdeb..b01f3b6df 100644 --- a/db/changes/10300-newFacility/01-waste.sql +++ b/db/changes/10300-newFacility/01-waste.sql @@ -17,6 +17,5 @@ ALTER TABLE `bs`.`waste` ALTER TABLE `bs`.`waste` DROP PRIMARY KEY; ALTER TABLE `bs`.`waste` - AD PRIMARY KEY (buyer, year, week, family, itemFk); - + ADD PRIMARY KEY (buyer, `year`, week, family, itemFk); diff --git a/db/changes/10300-newFacility/02-waste_addSales.sql b/db/changes/10300-newFacility/02-waste_addSales.sql index df6db2d2c..ae9efd95b 100644 --- a/db/changes/10300-newFacility/02-waste_addSales.sql +++ b/db/changes/10300-newFacility/02-waste_addSales.sql @@ -2,27 +2,28 @@ UPDATE `bs`.nightTask t SET t.`procedure` = 'waste_addSales' WHERE t.id = 54; DROP PROCEDURE IF EXISTS `bs`.`waste_Add`; +DELIMITER $$ +$$ CREATE - DEFINER = root@`%` PROCEDURE `bs`.`waste_addSales`() + DEFINER = root@`%` PROCEDURE `bs`.`waste_addSales`() BEGIN - DECLARE vWeek INT; - DECLARE vYear INT; - - SELECT week, year + DECLARE vYear INT; + + SELECT week, year INTO vWeek, vYear - FROM vn.time - WHERE dated = CURDATE(); + FROM vn.time + WHERE dated = CURDATE(); - REPLACE bs.waste + REPLACE bs.waste SELECT *, 100 * mermas / total as porcentaje FROM ( SELECT buyer, year, - week, + week, family, - itemFk, - itemTypeFk, + itemFk, + itemTypeFk, floor(sum(value)) as total, floor(sum(IF(clientTypeFk = 'loses', value, 0))) as mermas FROM vn.saleValue @@ -32,7 +33,5 @@ BEGIN ) sub ORDER BY mermas DESC; - -END; - - +END;$$ +DELIMITER ;