diff --git a/db/routines/vn/procedures/productionControl.sql b/db/routines/vn/procedures/productionControl.sql index af6d929d7..84717a19a 100644 --- a/db/routines/vn/procedures/productionControl.sql +++ b/db/routines/vn/procedures/productionControl.sql @@ -15,11 +15,6 @@ proc: BEGIN DECLARE vEndingDate DATETIME; DECLARE vIsTodayRelative BOOLEAN; - DECLARE EXIT HANDLER FOR SQLEXCEPTION - BEGIN - RESIGNAL; - END; - SELECT util.dayEnd(util.VN_CURDATE()) + INTERVAL LEAST(vScopeDays, maxProductionScopeDays) DAY INTO vEndingDate FROM productionConfig; @@ -273,7 +268,6 @@ proc: BEGIN DROP TEMPORARY TABLE tmp.productionTicket, tmp.ticket, - tmp.risk, tmp.ticket_problems, tmp.ticketWithPrevia, tItemShelvingStock, diff --git a/db/routines/vn/procedures/sale_getProblems.sql b/db/routines/vn/procedures/sale_getProblems.sql index c881bcf92..20e9f4c3a 100644 --- a/db/routines/vn/procedures/sale_getProblems.sql +++ b/db/routines/vn/procedures/sale_getProblems.sql @@ -1,152 +1,87 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_getProblems`(IN vIsTodayRelative tinyint(1)) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_getProblems`( + vIsTodayRelative tinyint(1) +) BEGIN /** * Calcula los problemas de cada venta para un conjunto de tickets. * * @param vIsTodayRelative Indica si se calcula el disponible como si todo saliera hoy - * @table tmp.sale_getProblems(ticketFk, clientFk, warehouseFk, shipped) Identificadores de los tickets a calcular + * @table tmp.sale_getProblems(ticketFk, clientFk, warehouseFk, shipped) Tickets a calcular * @return tmp.sale_problems */ - DECLARE vWarehouseFk INT; + DECLARE vWarehouseFk INT; DECLARE vDate DATE; - DECLARE vAvailableCache INT; + DECLARE vAvailableCache INT; DECLARE vVisibleCache INT; DECLARE vDone BOOL; - DECLARE vRequiredComponent INT; - - DECLARE vCursor CURSOR FOR - SELECT DISTINCT tt.warehouseFk, IF(vIsTodayRelative, util.VN_CURDATE(), DATE(tt.shipped)) - FROM tmp.sale_getProblems tt - WHERE DATE(tt.shipped) BETWEEN util.VN_CURDATE() - AND util.VN_CURDATE() + INTERVAL IF(vIsTodayRelative, 9.9, 1.9) DAY; + DECLARE vCursor CURSOR FOR + SELECT DISTINCT warehouseFk, IF(vIsTodayRelative, util.VN_CURDATE(), DATE(shipped)) + FROM tmp.sale_getProblems + WHERE shipped BETWEEN util.VN_CURDATE() + AND util.dayEnd(util.VN_CURDATE() + INTERVAL IF(vIsTodayRelative, 9.9, 1.9) DAY); DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; DELETE tt.* FROM tmp.sale_getProblems tt JOIN ticketObservation tto ON tto.ticketFk = tt.ticketFk - JOIN observationType ot ON ot.id = tto.observationTypeFk - WHERE ot.code = 'administrative' + JOIN observationType ot ON ot.id = tto.observationTypeFk + WHERE ot.code = 'administrative' AND tto.description = 'Miriam'; - CREATE OR REPLACE TEMPORARY TABLE tmp.sale_problems ( - ticketFk INT(11), - saleFk INT(11), - isFreezed INTEGER(1) DEFAULT 0, - risk DECIMAL(10,1) DEFAULT 0, - hasHighRisk TINYINT(1) DEFAULT 0, - hasTicketRequest INTEGER(1) DEFAULT 0, - itemShortage VARCHAR(255), - isTaxDataChecked INTEGER(1) DEFAULT 1, - itemDelay VARCHAR(255), - itemLost VARCHAR(255), - hasComponentLack INTEGER(1), - hasRounding VARCHAR(255), - isTooLittle BOOL DEFAULT FALSE, - isVip BOOL DEFAULT FALSE, - PRIMARY KEY (ticketFk, saleFk) - ) ENGINE = MEMORY; + CREATE OR REPLACE TEMPORARY TABLE tmp.sale_problems ( + ticketFk INT(11), + saleFk INT(11), + isFreezed INTEGER(1) DEFAULT 0, + risk DECIMAL(10,1) DEFAULT 0, + hasHighRisk TINYINT(1) DEFAULT 0, + hasTicketRequest INTEGER(1) DEFAULT 0, + itemShortage VARCHAR(255), + isTaxDataChecked INTEGER(1) DEFAULT 1, + itemDelay VARCHAR(255), + itemLost VARCHAR(255), + hasComponentLack INTEGER(1), + hasRounding VARCHAR(255), + isTooLittle BOOL DEFAULT FALSE, + isVip BOOL DEFAULT FALSE, + PRIMARY KEY (ticketFk, saleFk) + ) ENGINE = MEMORY; - CREATE OR REPLACE TEMPORARY TABLE tmp.ticket_list - (PRIMARY KEY (ticketFk)) - ENGINE = MEMORY - SELECT ticketFk, clientFk - FROM tmp.sale_getProblems; - - SELECT COUNT(*) INTO vRequiredComponent - FROM component - WHERE isRequired; - - -- Too Little - INSERT INTO tmp.sale_problems(ticketFk, isTooLittle) - SELECT tp.ticketFk, TRUE - FROM tmp.sale_getProblems tp - JOIN ticket t ON t.id = tp.ticketFk - JOIN ( - SELECT t.addressFk, - SUM(ROUND(`ic`.`cm3delivery` * `s`.`quantity` / 1000, 0)) litros, - t.totalWithoutVat - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk - JOIN sale s ON s.ticketFk = t.id - AND s.quantity > 0 - JOIN itemCost ic ON ic.itemFk = s.itemFk - AND ic.warehouseFk = t.warehouseFk - JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk - AND zc.dated = util.VN_CURDATE() - JOIN agencyMode am ON am.id = t.agencyModeFk - JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk - WHERE util.VN_NOW() < (util.VN_CURDATE() + INTERVAL HOUR(zc.`hour`) HOUR) + INTERVAL MINUTE(zc.`hour`) MINUTE - AND dm.code IN ('AGENCY','DELIVERY','PICKUP') - AND t.shipped BETWEEN util.VN_CURDATE() AND util.midnight() - GROUP BY t.addressFk - ) sub ON sub.addressFk = t.addressFk - JOIN volumeConfig vc - WHERE sub.litros < vc.minTicketVolume - AND sub.totalWithoutVat < vc.minTicketValue; - - -- VIP - INSERT INTO tmp.sale_problems(ticketFk, isVip) - SELECT DISTINCT tl.ticketFk, TRUE - FROM tmp.ticket_list tl - JOIN client c ON c.id = tl.clientFk - WHERE c.businessTypeFk = 'VIP' - ON DUPLICATE KEY UPDATE isVip = TRUE; - - -- Faltan componentes - INSERT INTO tmp.sale_problems(ticketFk, hasComponentLack, saleFk) - SELECT t.id, COUNT(c.id) < vRequiredComponent hasComponentLack, s.id - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk - JOIN sale s ON s.ticketFk = t.id - JOIN agencyMode am ON am.id = t.agencyModeFk - JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk - LEFT JOIN saleComponent sc ON sc.saleFk = s.id - LEFT JOIN component c ON c.id = sc.componentFk - AND c.isRequired - WHERE dm.code IN ('AGENCY','DELIVERY','PICKUP') - AND s.quantity > 0 - GROUP BY s.id - HAVING hasComponentLack; - - -- Cliente congelado - INSERT INTO tmp.sale_problems(ticketFk, isFreezed) - SELECT DISTINCT tl.ticketFk, TRUE - FROM tmp.ticket_list tl - JOIN client c ON c.id = tl.clientFk - WHERE c.isFreezed - ON DUPLICATE KEY UPDATE isFreezed = c.isFreezed; - - -- Credit exceeded - CREATE OR REPLACE TEMPORARY TABLE tmp.clientGetDebt - (PRIMARY KEY (clientFk)) - ENGINE = MEMORY - SELECT DISTINCT clientFk - FROM tmp.ticket_list; - - CALL client_getDebt(util.VN_CURDATE()); - - INSERT INTO tmp.sale_problems(ticketFk, risk, hasHighRisk) - SELECT DISTINCT tl.ticketFk, r.risk, ((r.risk - cc.riskTolerance) > c.credit + 10) - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk - JOIN agencyMode a ON t.agencyModeFk = a.id - JOIN tmp.risk r ON r.clientFk = t.clientFk - JOIN client c ON c.id = t.clientFk - JOIN clientConfig cc - WHERE r.risk > c.credit + 10 - AND NOT a.isRiskFree - ON DUPLICATE KEY UPDATE - risk = r.risk, hasHighRisk = ((r.risk - cc.riskTolerance) > c.credit + 10); - - -- Antiguo COD 100, son peticiones de compra sin terminar - INSERT INTO tmp.sale_problems(ticketFk, hasTicketRequest) - SELECT DISTINCT tl.ticketFk, TRUE - FROM tmp.ticket_list tl - JOIN ticketRequest tr ON tr.ticketFk = tl.ticketFk - WHERE tr.isOK IS NULL - ON DUPLICATE KEY UPDATE hasTicketRequest = TRUE; + INSERT INTO tmp.sale_problems (ticketFk, + saleFk, + isFreezed, + hasHighRisk, + hasTicketRequest, + isTaxDataChecked, + hasComponentLack, + hasRounding, + isTooLittle, + isVip) + SELECT sgp.ticketFk, + s.id, + IF(FIND_IN_SET('isFreezed', s.problem), TRUE, FALSE) isFreezed, + IF(FIND_IN_SET('hasHighRisk', s.problem), TRUE, FALSE) hasHighRisk, + IF(FIND_IN_SET('hasTicketRequest', s.problem), TRUE, FALSE) hasTicketRequest, + IF(FIND_IN_SET('isTaxDataChecked', s.problem), FALSE, TRUE) isTaxDataChecked, + IF(FIND_IN_SET('hasComponentLack', s.problem), TRUE, FALSE) hasComponentLack, + IF(FIND_IN_SET('hasRounding', s.problem), + LEFT(GROUP_CONCAT('RE: ', i.id, ' ', IFNULL(i.longName,'') SEPARATOR ', '), 250), + NULL + ) hasRounding, + IF(FIND_IN_SET('isTooLittle', s.problem), TRUE, FALSE) isTooLittle, + IF(FIND_IN_SET('isVip', s.problem), TRUE, FALSE) isVip + FROM tmp.sale_getProblems sgp + JOIN ticket t ON t.id = sgp.ticketFk + JOIN sale s ON s.ticketFk = t.id + JOIN item i ON i.id = s.itemFk + WHERE s.problem <> '' + GROUP BY s.id; + + INSERT INTO tmp.sale_problems (ticketFk, risk) + SELECT t.id, t.risk + FROM tmp.sale_getProblems sgp + JOIN ticket t ON t.id = sgp.ticketFk; CREATE OR REPLACE TEMPORARY TABLE tItemShelvingStock_byWarehouse (INDEX (itemFk, warehouseFk)) @@ -158,10 +93,9 @@ BEGIN JOIN shelving sh ON sh.code = ish.shelvingFk JOIN parking p ON p.id = sh.parkingFk JOIN sector s ON s.id = p.sectorFk - GROUP BY ish.itemFk, - s.warehouseFk; + GROUP BY ish.itemFk, s.warehouseFk; - -- Disponible, Faltas, Inventario y Retrasos + -- Disponible, faltas, inventario y retrasos OPEN vCursor; l: LOOP SET vDone = FALSE; @@ -180,14 +114,14 @@ BEGIN INSERT INTO tmp.sale_problems(ticketFk, itemShortage, saleFk) SELECT ticketFk, problem, saleFk FROM ( - SELECT tl.ticketFk, - LEFT(CONCAT('F: ',GROUP_CONCAT(i.id, ' ', i.longName, ' ')),250) problem, - s.id AS saleFk - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk + SELECT sgp.ticketFk, + LEFT(CONCAT('F: ', GROUP_CONCAT(i.id, ' ', i.longName, ' ')), 250) problem, + s.id saleFk + FROM tmp.sale_getProblems sgp + JOIN ticket t ON t.id = sgp.ticketFk JOIN sale s ON s.ticketFk = t.id JOIN item i ON i.id = s.itemFk - JOIN itemType it on it.id = i.typeFk + JOIN itemType it ON it.id = i.typeFk JOIN itemCategory ic ON ic.id = it.categoryFk LEFT JOIN cache.visible v ON v.item_id = i.id AND v.calc_id = vVisibleCache @@ -195,8 +129,8 @@ BEGIN AND av.calc_id = vAvailableCache LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND issw.warehouseFk = t.warehouseFk - WHERE IFNULL(v.visible,0) < s.quantity - AND IFNULL(av.available ,0) < s.quantity + WHERE IFNULL(v.visible, 0) < s.quantity + AND IFNULL(av.available, 0) < s.quantity AND IFNULL(issw.visible, 0) < s.quantity AND NOT s.isPicked AND NOT s.reserved @@ -205,27 +139,27 @@ BEGIN AND NOT i.generic AND util.VN_CURDATE() = vDate AND t.warehouseFk = vWarehouseFk - GROUP BY tl.ticketFk) sub + GROUP BY sgp.ticketFk) sub ON DUPLICATE KEY UPDATE itemShortage = sub.problem, saleFk = sub.saleFk; -- Inventario: Visible suficiente, pero ubicado menor a la cantidad vendida INSERT INTO tmp.sale_problems(ticketFk, itemLost, saleFk) SELECT ticketFk, problem, saleFk FROM ( - SELECT tl.ticketFk, + SELECT sgp.ticketFk, LEFT(GROUP_CONCAT('I: ', i.id, ' ', i.longName, ' '), 250) problem, s.id saleFk - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk + FROM tmp.sale_getProblems sgp + JOIN ticket t ON t.id = sgp.ticketFk JOIN sale s ON s.ticketFk = t.id JOIN item i ON i.id = s.itemFk - JOIN itemType it on it.id = i.typeFk + JOIN itemType it ON it.id = i.typeFk JOIN itemCategory ic ON ic.id = it.categoryFk LEFT JOIN cache.visible v ON v.item_id = s.itemFk AND v.calc_id = vVisibleCache LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND issw.warehouseFk = t.warehouseFk - WHERE IFNULL(v.visible,0) >= s.quantity + WHERE IFNULL(v.visible, 0) >= s.quantity AND IFNULL(issw.visible, 0) < s.quantity AND s.quantity > 0 AND NOT s.isPicked @@ -235,22 +169,22 @@ BEGIN AND NOT i.generic AND util.VN_CURDATE() = vDate AND t.warehouseFk = vWarehouseFk - GROUP BY tl.ticketFk + GROUP BY sgp.ticketFk ) sub - ON DUPLICATE KEY UPDATE itemDelay = sub.problem, saleFk = sub.saleFk; + ON DUPLICATE KEY UPDATE itemLost = sub.problem, saleFk = sub.saleFk; -- Retraso: Disponible suficiente, pero no visible ni ubicado INSERT INTO tmp.sale_problems(ticketFk, itemDelay, saleFk) SELECT ticketFk, problem, saleFk FROM ( - SELECT tl.ticketFk, + SELECT sgp.ticketFk, LEFT(GROUP_CONCAT('R: ', i.id, ' ', i.longName, ' '), 250) problem, s.id saleFk - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk + FROM tmp.sale_getProblems sgp + JOIN ticket t ON t.id = sgp.ticketFk JOIN sale s ON s.ticketFk = t.id JOIN item i ON i.id = s.itemFk - JOIN itemType it on it.id = i.typeFk + JOIN itemType it ON it.id = i.typeFk JOIN itemCategory ic ON ic.id = it.categoryFk LEFT JOIN cache.visible v ON v.item_id = s.itemFk AND v.calc_id = vVisibleCache @@ -269,42 +203,29 @@ BEGIN AND NOT i.generic AND util.VN_CURDATE() = vDate AND t.warehouseFk = vWarehouseFk - GROUP BY tl.ticketFk + GROUP BY sgp.ticketFk ) sub ON DUPLICATE KEY UPDATE itemDelay = sub.problem, saleFk = sub.saleFk; - - -- Redondeo: Cantidad pedida incorrecta en al grouping de la Ășltima compra - CALL buy_getUltimate(NULL, vWarehouseFk, vDate); - INSERT INTO tmp.sale_problems(ticketFk, hasRounding, saleFk) - SELECT ticketFk, problem ,saleFk - FROM ( - SELECT tl.ticketFk, - s.id saleFk, - LEFT(GROUP_CONCAT('RE: ',i.id, ' ', IFNULL(i.longName,'') SEPARATOR ', '), 250) problem - FROM tmp.ticket_list tl - JOIN ticket t ON t.id = tl.ticketFk - AND t.warehouseFk = vWarehouseFk - JOIN sale s ON s.ticketFk = tl.ticketFk - JOIN item i ON i.id = s.itemFk - JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk - JOIN buy b ON b.id = bu.buyFk - WHERE MOD(s.quantity, b.`grouping`) - GROUP BY tl.ticketFk - )sub - ON DUPLICATE KEY UPDATE hasRounding = sub.problem, saleFk = sub.saleFk; END LOOP; CLOSE vCursor; - - INSERT INTO tmp.sale_problems(ticketFk, isTaxDataChecked) - SELECT DISTINCT tl.ticketFk, FALSE - FROM tmp.ticket_list tl - JOIN client c ON c.id = tl.clientFk - WHERE NOT c.isTaxDataChecked - ON DUPLICATE KEY UPDATE isTaxDataChecked = FALSE; + + INSERT INTO tmp.sale_problems(ticketFk, hasRounding, saleFk) + SELECT ticketFk, problem, saleFk + FROM ( + SELECT sgp.ticketFk, + s.id saleFk, + LEFT(GROUP_CONCAT('RE: ', i.id, ' ', IFNULL(i.longName,'') SEPARATOR ', '), 250) problem + FROM tmp.sale_getProblems sgp + JOIN ticket t ON t.id = sgp.ticketFk + JOIN sale s ON s.ticketFk = sgp.ticketFk + JOIN item i ON i.id = s.itemFk + WHERE FIND_IN_SET('hasRounding', s.problem) + GROUP BY sgp.ticketFk + ) sub + ON DUPLICATE KEY UPDATE hasRounding = sub.problem, saleFk = sub.saleFk; DROP TEMPORARY TABLE - tmp.clientGetDebt, - tmp.ticket_list, + tmp.sale_getProblems, tItemShelvingStock_byWarehouse; END$$ DELIMITER ; diff --git a/db/routines/vn/procedures/sale_getProblemsByTicket.sql b/db/routines/vn/procedures/sale_getProblemsByTicket.sql index b4aaad7de..f7a066705 100644 --- a/db/routines/vn/procedures/sale_getProblemsByTicket.sql +++ b/db/routines/vn/procedures/sale_getProblemsByTicket.sql @@ -7,8 +7,7 @@ BEGIN * * @return Problems result */ - DROP TEMPORARY TABLE IF EXISTS tmp.sale_getProblems; - CREATE TEMPORARY TABLE tmp.sale_getProblems + CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems (INDEX (ticketFk)) ENGINE = MEMORY SELECT t.id ticketFk, t.clientFk, t.warehouseFk, t.shipped diff --git a/db/routines/vn/procedures/ticket_getProblems.sql b/db/routines/vn/procedures/ticket_getProblems.sql index 521e4cf2f..e9becab2a 100644 --- a/db/routines/vn/procedures/ticket_getProblems.sql +++ b/db/routines/vn/procedures/ticket_getProblems.sql @@ -1,5 +1,7 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_getProblems`(IN vIsTodayRelative tinyint(1)) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_getProblems`( + vIsTodayRelative tinyint(1) +) BEGIN /** * Calcula los problemas para un conjunto de tickets.