7965-unifyProblems #2990

Open
carlosap wants to merge 10 commits from 7965-unifyProblems into dev
11 changed files with 330 additions and 332 deletions

View File

@ -1072,12 +1072,12 @@ INSERT INTO `vn`.`sale`(`id`, `itemFk`, `ticketFk`, `concept`, `quantity`, `pric
(5, 1, 2, 'Ranged weapon longbow 200cm', 1, 110.33, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 'hasComponentLack'), (5, 1, 2, 'Ranged weapon longbow 200cm', 1, 110.33, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 'hasComponentLack'),
(6, 1, 3, 'Ranged weapon longbow 200cm', 1, 110.33, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH), 'hasComponentLack'), (6, 1, 3, 'Ranged weapon longbow 200cm', 1, 110.33, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL -2 MONTH), 'hasComponentLack'),
(7, 2, 11, 'Melee weapon combat fist 15cm', 15, 7.74, 0, 0, 0, util.VN_CURDATE(), NULL), (7, 2, 11, 'Melee weapon combat fist 15cm', 15, 7.74, 0, 0, 0, util.VN_CURDATE(), NULL),
(8, 4, 11, 'Melee weapon heavy shield 100cm', 10, 1.79, 0, 0, 0, util.VN_CURDATE(), NULL), (8, 4, 11, 'Melee weapon heavy shield 100cm', 10, 1.79, 0, 0, 0, util.VN_CURDATE(), 'hasItemLost,hasRounding'),
(9, 1, 16, 'Ranged weapon longbow 200cm', 1, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL), (9, 1, 16, 'Ranged weapon longbow 200cm', 1, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL),
(10, 2, 16, 'Melee weapon combat fist 15cm', 10, 7.09, 0, 0, 0, util.VN_CURDATE(), NULL), (10, 2, 16, 'Melee weapon combat fist 15cm', 10, 7.09, 0, 0, 0, util.VN_CURDATE(), NULL),
(11, 1, 16, 'Ranged weapon longbow 200cm', 1, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL), (11, 1, 16, 'Ranged weapon longbow 200cm', 1, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL),
(12, 4, 16, 'Melee weapon heavy shield 100cm', 20, 1.71, 0, 0, 0, util.VN_CURDATE(), NULL), (12, 4, 16, 'Melee weapon heavy shield 100cm', 20, 1.71, 0, 0, 0, util.VN_CURDATE(), NULL),
(13, 2, 8, 'Melee weapon combat fist 15cm', 10, 7.08, 0, 0, 0, util.VN_CURDATE(), NULL), (13, 2, 8, 'Melee weapon combat fist 15cm', 10, 7.08, 0, 0, 0, util.VN_CURDATE(), 'hasItemLost'),
(14, 1, 8, 'Ranged weapon longbow 200cm', 2, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL), (14, 1, 8, 'Ranged weapon longbow 200cm', 2, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL),
(15, 1, 19, 'Ranged weapon longbow 200cm', 1, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL), (15, 1, 19, 'Ranged weapon longbow 200cm', 1, 103.49, 0, 0, 0, util.VN_CURDATE(), NULL),
(16, 2, 20, 'Melee weapon combat fist 15cm', 20, 7.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (16, 2, 20, 'Melee weapon combat fist 15cm', 20, 7.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'),
@ -1088,25 +1088,25 @@ INSERT INTO `vn`.`sale`(`id`, `itemFk`, `ticketFk`, `concept`, `quantity`, `pric
(21, 1, 6, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 'hasComponentLack'), (21, 1, 6, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 'hasComponentLack'),
(22, 1, 7, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (22, 1, 7, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'),
(23, 1, 9, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (23, 1, 9, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'),
(24, 1, 10, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (24, 1, 10, 'Ranged weapon longbow 200cm', 1, 8.07, 0, 0, 0, util.VN_CURDATE(), 'hasItemShortage,hasComponentLack'),
(25, 4, 12, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (25, 4, 12, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(26, 4, 13, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (26, 4, 13, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(27, 4, 14, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (27, 4, 14, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasItemShortage,hasComponentLack,hasItemLost'),
(28, 4, 15, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (28, 4, 15, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(29, 4, 17, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (29, 4, 17, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasItemShortage,hasComponentLack'),
(30, 4, 18, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (30, 4, 18, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasItemShortage,hasComponentLack'),
(31, 2, 23, 'Melee weapon combat fist 15cm', -5, 7.08, 0, 0, 0, util.VN_CURDATE(), NULL), (31, 2, 23, 'Melee weapon combat fist 15cm', -5, 7.08, 0, 0, 0, util.VN_CURDATE(), 'hasRounding'),
(32, 1, 24, 'Ranged weapon longbow 200cm', -1, 8.07, 0, 0, 0, util.VN_CURDATE(), NULL), (32, 1, 24, 'Ranged weapon longbow 200cm', -1, 8.07, 0, 0, 0, util.VN_CURDATE(), NULL),
(33, 5, 14, 'Ranged weapon pistol 9mm', 50, 1.79, 0, 0, 0, util.VN_CURDATE(), NULL), (33, 5, 14, 'Ranged weapon pistol 9mm', 50, 1.79, 0, 0, 0, util.VN_CURDATE(), NULL),
(34, 4, 28, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (34, 4, 28, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(35, 4, 29, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (35, 4, 29, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(37, 4, 31, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), NULL), (37, 4, 31, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), NULL),
(36, 4, 30, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (36, 4, 30, 'Melee weapon heavy shield 100cm', 20, 1.72, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(38, 2, 32, 'Melee weapon combat fist 15cm', 30, 7.07, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL +1 MONTH), 'hasComponentLack'), (38, 2, 32, 'Melee weapon combat fist 15cm', 30, 7.07, 0, 0, 0, DATE_ADD(util.VN_CURDATE(), INTERVAL +1 MONTH), 'hasComponentLack'),
(39, 1, 32, 'Ranged weapon longbow 200cm', 2, 103.49, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (39, 1, 32, 'Ranged weapon longbow 200cm', 2, 103.49, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'),
(40, 2, 34, 'Melee weapon combat fist 15cm', 10.00, 3.91, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (40, 2, 34, 'Melee weapon combat fist 15cm', 10.00, 3.91, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasItemLost'),
(41, 2, 35, 'Melee weapon combat fist 15cm', 8.00, 3.01, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'), (41, 2, 35, 'Melee weapon combat fist 15cm', 8.00, 3.01, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasRounding,hasItemLost'),
(42, 2, 36, 'Melee weapon combat fist 15cm', 6.00, 2.50, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack'); (42, 2, 36, 'Melee weapon combat fist 15cm', 6.00, 2.50, 0, 0, 0, util.VN_CURDATE(), 'hasComponentLack,hasRounding,hasItemLost');
INSERT INTO `vn`.`saleComponent`(`saleFk`, `componentFk`, `value`) INSERT INTO `vn`.`saleComponent`(`saleFk`, `componentFk`, `value`)
VALUES VALUES

View File

@ -1,16 +1,18 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`prepareTicketList`(vStartingDate DATETIME, vEndingDate DATETIME) CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`prepareTicketList`(
vStartingDate DATETIME,
vEndingDate DATETIME
)
BEGIN BEGIN
DROP TEMPORARY TABLE IF EXISTS tmp.productionTicket; DROP TEMPORARY TABLE IF EXISTS tmp.productionTicket;
CREATE TEMPORARY TABLE tmp.productionTicket CREATE TEMPORARY TABLE tmp.productionTicket
(PRIMARY KEY (ticketFk)) (PRIMARY KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT t.id ticketFk, t.clientFk SELECT t.id ticketFk
FROM ticket t FROM ticket t
JOIN alertLevel al ON al.code = 'DELIVERED' JOIN alertLevel al ON al.code = 'DELIVERED'
LEFT JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN ticketState ts ON ts.ticketFk = t.id
JOIN client c ON c.id = t.clientFk JOIN client c ON c.id = t.clientFk
WHERE c.typeFk IN ('normal','handMaking','internalUse') WHERE c.typeFk IN ('normal','handMaking','internalUse')
AND ( AND (
t.shipped BETWEEN util.VN_CURDATE() AND vEndingDate t.shipped BETWEEN util.VN_CURDATE() AND vEndingDate

View File

@ -24,24 +24,31 @@ proc: BEGIN
CALL prepareTicketList(util.yesterday(), vEndingDate); CALL prepareTicketList(util.yesterday(), vEndingDate);
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
SELECT * FROM tmp.productionTicket;
CALL prepareClientList();
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems
(INDEX (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT tt.ticketFk, tt.clientFk, t.warehouseFk, t.shipped SELECT ticketFk
FROM tmp.productionTicket tt FROM tmp.productionTicket;
JOIN ticket t ON t.id = tt.ticketFk;
CALL ticket_getProblems(vIsTodayRelative); CALL ticket_getProblems(vIsTodayRelative);
CREATE OR REPLACE TEMPORARY TABLE tmp.productionBuffer CREATE OR REPLACE TEMPORARY TABLE tmp.productionBuffer
(PRIMARY KEY(ticketFk), previaParking VARCHAR(255)) (PRIMARY KEY(ticketFk), previaParking VARCHAR(255))
ENGINE = MEMORY ENGINE = MEMORY
WITH saleProblemsDescription AS(
SELECT s.ticketFk,
LEFT(CONCAT('F: ', GROUP_CONCAT(CONCAT(i.id, ' ', i.longName) SEPARATOR ', ')), 250) itemShortage,
LEFT(CONCAT('R: ', GROUP_CONCAT(CONCAT(i2.id, ' ', i2.longName) SEPARATOR ', ')), 250) itemDelay,
LEFT(CONCAT('I: ', GROUP_CONCAT(CONCAT(i3.id, ' ', i3.longName) SEPARATOR ', ')), 250) itemLost
FROM tmp.saleProblems sp
JOIN vn.sale s ON s.id = sp.saleFk
LEFT JOIN vn.item i ON i.id = s.itemFk AND sp.hasItemShortage
LEFT JOIN vn.item i2 ON i2.id = s.itemFk AND sp.hasItemDelay
LEFT JOIN vn.item i3 ON i3.id = s.itemFk AND sp.hasItemLost
WHERE hasItemShortage OR hasItemDelay OR hasItemLost
GROUP BY s.ticketFk
)
SELECT tt.ticketFk, SELECT tt.ticketFk,
tt.clientFk, t.clientFk,
t.warehouseFk, t.warehouseFk,
t.nickname, t.nickname,
t.packages, t.packages,
@ -59,7 +66,17 @@ proc: BEGIN
0 `lines`, 0 `lines`,
CAST( 0 AS DECIMAL(5,2)) m3, CAST( 0 AS DECIMAL(5,2)) m3,
CAST( 0 AS DECIMAL(5,2)) preparationRate, CAST( 0 AS DECIMAL(5,2)) preparationRate,
"" problem, TRIM(CAST(CONCAT( IFNULL(sp.itemShortage, ''),
IFNULL(sp.itemDelay, ''),
IFNULL(sp.itemLost, ''),
IF(tpr.isFreezed, ' CONGELADO',''),
IF(tpr.hasHighRisk, ' RIESGO',''),
IF(tpr.hasTicketRequest, ' COD 100',''),
IF(tpr.isTaxDataChecked, '',' FICHA INCOMPLETA'),
IF(tpr.hasComponentLack, ' COMPONENTES', ''),
IF(HOUR(util.VN_NOW()) < IF(HOUR(t.shipped), HOUR(t.shipped), COALESCE(HOUR(zc.hour),HOUR(z.hour)))
AND tpr.isTooLittle, ' PEQUEÑO', '')
) AS char(255))) problem,
IFNULL(tls.state,2) state, IFNULL(tls.state,2) state,
w.code workerCode, w.code workerCode,
DATE(t.shipped) shipped, DATE(t.shipped) shipped,
@ -79,29 +96,31 @@ proc: BEGIN
ag.isOwn, ag.isOwn,
rm.bufferFk rm.bufferFk
FROM tmp.productionTicket tt FROM tmp.productionTicket tt
JOIN ticket t ON tt.ticketFk = t.id JOIN vn.ticket t ON tt.ticketFk = t.id
JOIN alertLevel al ON al.code = 'FREE' JOIN vn.alertLevel al ON al.code = 'FREE'
LEFT JOIN ticketStateToday tst ON tst.ticketFk = t.id LEFT JOIN vn.ticketStateToday tst ON tst.ticketFk = t.id
LEFT JOIN `state` st ON st.id = tst.state LEFT JOIN vn.`state` st ON st.id = tst.state
LEFT JOIN client c ON c.id = t.clientFk LEFT JOIN vn.client c ON c.id = t.clientFk
LEFT JOIN worker wk ON wk.id = c.salesPersonFk LEFT JOIN vn.worker wk ON wk.id = c.salesPersonFk
JOIN address a ON a.id = t.addressFk JOIN vn.address a ON a.id = t.addressFk
LEFT JOIN province p ON p.id = a.provinceFk LEFT JOIN vn.province p ON p.id = a.provinceFk
JOIN agencyMode am ON am.id = t.agencyModeFk JOIN vn.agencyMode am ON am.id = t.agencyModeFk
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk JOIN vn.deliveryMethod dm ON dm.id = am.deliveryMethodFk
JOIN agency ag ON ag.id = am.agencyFk JOIN vn.agency ag ON ag.id = am.agencyFk
LEFT JOIN ticketState tls ON tls.ticketFk = tt.ticketFk LEFT JOIN vn.ticketState tls ON tls.ticketFk = tt.ticketFk
LEFT JOIN ticketLastUpdated tlu ON tlu.ticketFk = tt.ticketFk LEFT JOIN vn.ticketLastUpdated tlu ON tlu.ticketFk = tt.ticketFk
LEFT JOIN worker w ON w.id = tls.userFk LEFT JOIN vn.worker w ON w.id = tls.userFk
LEFT JOIN routesMonitor rm ON rm.routeFk = t.routeFk LEFT JOIN vn.routesMonitor rm ON rm.routeFk = t.routeFk
LEFT JOIN `zone` z ON z.id = t.zoneFk LEFT JOIN vn.`zone` z ON z.id = t.zoneFk
LEFT JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk LEFT JOIN vn.zoneClosure zc ON zc.zoneFk = t.zoneFk
AND DATE(t.shipped) = zc.dated AND DATE(t.shipped) = zc.dated
LEFT JOIN ticketParking tp ON tp.ticketFk = t.id LEFT JOIN vn.ticketParking tp ON tp.ticketFk = t.id
LEFT JOIN parking pk ON pk.id = tp.parkingFk LEFT JOIN vn.parking pk ON pk.id = tp.parkingFk
LEFT JOIN tmp.ticketProblems tpr ON tpr.ticketFk = tt.ticketFk
LEFT JOIN saleProblemsDescription sp ON sp.ticketFk = tt.ticketFk
WHERE t.warehouseFk = vWarehouseFk WHERE t.warehouseFk = vWarehouseFk
AND dm.code IN ('AGENCY', 'DELIVERY', 'PICKUP'); AND dm.code IN ('AGENCY', 'DELIVERY', 'PICKUP');
UPDATE tmp.productionBuffer pb UPDATE tmp.productionBuffer pb
JOIN ( JOIN (
SELECT pb.ticketFk, GROUP_CONCAT(p.code) previaParking SELECT pb.ticketFk, GROUP_CONCAT(p.code) previaParking
@ -121,19 +140,6 @@ proc: BEGIN
ADD COLUMN `collectionV` INT, ADD COLUMN `collectionV` INT,
ADD COLUMN `collectionN` INT; ADD COLUMN `collectionN` INT;
UPDATE tmp.productionBuffer pb
JOIN tmp.ticket_problems tp ON tp.ticketFk = pb.ticketFk
SET pb.problem = TRIM(CAST(CONCAT( IFNULL(tp.itemShortage, ''),
IFNULL(tp.itemDelay, ''),
IFNULL(tp.itemLost, ''),
IF(tp.isFreezed, ' CONGELADO',''),
IF(tp.hasHighRisk, ' RIESGO',''),
IF(tp.hasTicketRequest, ' COD 100',''),
IF(tp.isTaxDataChecked, '',' FICHA INCOMPLETA'),
IF(tp.hasComponentLack, ' COMPONENTES', ''),
IF(HOUR(util.VN_NOW()) < pb.HH AND tp.isTooLittle, ' PEQUEÑO', '')
) AS char(255)));
-- Clientes Nuevos o Recuperados -- Clientes Nuevos o Recuperados
UPDATE tmp.productionBuffer pb UPDATE tmp.productionBuffer pb
LEFT JOIN bs.clientNewBorn cnb ON cnb.clientFk = pb.clientFk LEFT JOIN bs.clientNewBorn cnb ON cnb.clientFk = pb.clientFk
@ -278,7 +284,8 @@ proc: BEGIN
DROP TEMPORARY TABLE DROP TEMPORARY TABLE
tmp.productionTicket, tmp.productionTicket,
tmp.ticket, tmp.ticket,
tmp.ticket_problems, tmp.ticketProblems,
tmp.saleProblems,
tmp.ticketWithPrevia, tmp.ticketWithPrevia,
tItemShelvingStock, tItemShelvingStock,
tItemPackingType; tItemPackingType;

View File

@ -1,86 +1,40 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`sale_getProblems`( CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`sale_getProblems`(
vIsTodayRelative tinyint(1) vIsTodayRelative TINYINT(1)
) )
BEGIN BEGIN
/** /**
* Calcula los problemas de cada venta para un conjunto de tickets. * Calcula los problemas para un conjunto de sale
* *
* @param vIsTodayRelative Indica si se calcula el disponible como si todo saliera hoy * @table tmp.sale(saleFk) Identificadores de los sale a calcular
* @table tmp.sale_getProblems(ticketFk, clientFk, warehouseFk, shipped) Tickets a calcular * @return tmp.saleProblems
* @return tmp.sale_problems
*/ */
DECLARE vWarehouseFk INT; DECLARE vWarehouseFk INT;
DECLARE vDate DATE; DECLARE vDate DATE;
DECLARE vAvailableCache INT; DECLARE vAvailableCache INT;
DECLARE vVisibleCache INT; DECLARE vVisibleCache INT;
DECLARE vDone BOOL; DECLARE vDone BOOL;
DECLARE vCursor CURSOR FOR DECLARE vCursor CURSOR FOR
SELECT DISTINCT warehouseFk, IF(vIsTodayRelative, util.VN_CURDATE(), DATE(shipped)) SELECT t.warehouseFk, IF(vIsTodayRelative, util.VN_CURDATE(), DATE(t.shipped)) dated
FROM tmp.sale_getProblems FROM tmp.sale ts
WHERE shipped BETWEEN util.VN_CURDATE() JOIN sale s ON s.id = ts.saleFk
AND util.dayEnd(util.VN_CURDATE() + INTERVAL IF(vIsTodayRelative, 9.9, 1.9) DAY); JOIN ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN util.VN_CURDATE()
AND util.dayEnd(util.VN_CURDATE() + INTERVAL IF(vIsTodayRelative, 9.9, 1.9) DAY)
GROUP BY warehouseFk, dated;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_problems ( CREATE OR REPLACE TEMPORARY TABLE tmp.saleProblems(
ticketFk INT(11),
saleFk INT(11), saleFk INT(11),
isFreezed INTEGER(1) DEFAULT 0, hasItemShortage BOOL DEFAULT FALSE,
risk DECIMAL(10,1) DEFAULT 0, hasItemLost BOOL DEFAULT FALSE,
hasRisk TINYINT(1) DEFAULT 0, hasItemDelay BOOL DEFAULT FALSE,
hasHighRisk TINYINT(1) DEFAULT 0, hasRounding BOOL DEFAULT FALSE,
hasTicketRequest INTEGER(1) DEFAULT 0, PRIMARY KEY (saleFk)
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; ) ENGINE = MEMORY;
INSERT INTO tmp.sale_problems(ticketFk, CREATE OR REPLACE TEMPORARY TABLE tItemShelving
saleFk,
isFreezed,
risk,
hasRisk,
hasHighRisk,
hasTicketRequest,
isTaxDataChecked,
hasComponentLack,
isTooLittle)
SELECT sgp.ticketFk,
s.id,
IF(FIND_IN_SET('isFreezed', t.problem), TRUE, FALSE) isFreezed,
t.risk,
IF(FIND_IN_SET('hasRisk', t.problem), TRUE, FALSE) hasRisk,
IF(FIND_IN_SET('hasHighRisk', t.problem), TRUE, FALSE) hasHighRisk,
IF(FIND_IN_SET('hasTicketRequest', t.problem), TRUE, FALSE) hasTicketRequest,
IF(FIND_IN_SET('isTaxDataChecked', t.problem), FALSE, TRUE) isTaxDataChecked,
IF(FIND_IN_SET('hasComponentLack', s.problem), TRUE, FALSE) hasComponentLack,
IF(FIND_IN_SET('isTooLittle', t.problem)
AND util.VN_NOW() < (util.VN_CURDATE() + INTERVAL HOUR(zc.`hour`) HOUR) + INTERVAL MINUTE(zc.`hour`) MINUTE,
TRUE, FALSE) isTooLittle
FROM tmp.sale_getProblems sgp
JOIN ticket t ON t.id = sgp.ticketFk
LEFT JOIN sale s ON s.ticketFk = t.id
LEFT JOIN item i ON i.id = s.itemFk
LEFT JOIN zoneClosure zc ON zc.zoneFk = t.zoneFk
AND zc.dated = util.VN_CURDATE()
WHERE s.problem <> '' OR t.problem <> '' OR t.risk
GROUP BY t.id, s.id;
INSERT INTO tmp.sale_problems(ticketFk, isVip)
SELECT sgp.ticketFk, TRUE
FROM tmp.sale_getProblems sgp
JOIN client c ON c.id = sgp.clientFk
WHERE c.businessTypeFk = 'VIP'
ON DUPLICATE KEY UPDATE isVIP = TRUE;
CREATE OR REPLACE TEMPORARY TABLE tItemShelvingStock_byWarehouse
(INDEX (itemFk, warehouseFk)) (INDEX (itemFk, warehouseFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT ish.itemFk itemFk, SELECT ish.itemFk itemFk,
@ -91,7 +45,7 @@ BEGIN
JOIN parking p ON p.id = sh.parkingFk JOIN parking p ON p.id = sh.parkingFk
JOIN sector s ON s.id = p.sectorFk 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; OPEN vCursor;
l: LOOP l: LOOP
@ -101,133 +55,114 @@ BEGIN
IF vDone THEN IF vDone THEN
LEAVE l; LEAVE l;
END IF; END IF;
-- Disponible: no va a haber suficiente producto para preparar todos los pedidos -- Disponible: no va a haber suficiente producto para preparar todos los pedidos
CALL cache.available_refresh(vAvailableCache, FALSE, vWarehouseFk, vDate); CALL cache.available_refresh(vAvailableCache, FALSE, vWarehouseFk, vDate);
-- Faltas: visible, disponible y ubicado son menores que la cantidad vendida -- Faltas: visible, disponible y ubicado son menores que la cantidad vendida
CALL cache.visible_refresh(vVisibleCache, FALSE, vWarehouseFk); CALL cache.visible_refresh(vVisibleCache, FALSE, vWarehouseFk);
INSERT INTO tmp.sale_problems(ticketFk, itemShortage, saleFk) INSERT INTO tmp.saleProblems(saleFk, hasItemShortage)
SELECT ticketFk, problem, saleFk SELECT s.id, TRUE
FROM ( FROM tmp.sale ts
SELECT sgp.ticketFk, JOIN sale s ON s.id = ts.saleFk
LEFT(CONCAT('F: ', GROUP_CONCAT(i.id, ' ', i.longName, ' ')), 250) problem, JOIN ticket t ON t.id = s.ticketFk
s.id saleFk JOIN item i ON i.id = s.itemFk
FROM tmp.sale_getProblems sgp JOIN itemType it ON it.id = i.typeFk
JOIN ticket t ON t.id = sgp.ticketFk JOIN itemCategory ic ON ic.id = it.categoryFk
JOIN sale s ON s.ticketFk = t.id LEFT JOIN cache.visible v ON v.item_id = i.id
JOIN item i ON i.id = s.itemFk AND v.calc_id = vVisibleCache
JOIN itemType it ON it.id = i.typeFk LEFT JOIN cache.available av ON av.item_id = i.id
JOIN itemCategory ic ON ic.id = it.categoryFk AND av.calc_id = vAvailableCache
LEFT JOIN cache.visible v ON v.item_id = i.id LEFT JOIN tItemShelving tis ON tis.itemFk = i.id
AND v.calc_id = vVisibleCache AND tis.warehouseFk = t.warehouseFk
LEFT JOIN cache.available av ON av.item_id = i.id WHERE IFNULL(v.visible, 0) < s.quantity
AND av.calc_id = vAvailableCache AND IFNULL(av.available, 0) < s.quantity
LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND IFNULL(tis.visible, 0) < s.quantity
AND issw.warehouseFk = t.warehouseFk AND NOT s.isPicked
WHERE IFNULL(v.visible, 0) < s.quantity AND NOT s.reserved
AND IFNULL(av.available, 0) < s.quantity AND ic.merchandise
AND IFNULL(issw.visible, 0) < s.quantity AND IF(vIsTodayRelative, TRUE, DATE(t.shipped) = vDate)
AND NOT s.isPicked AND NOT i.generic
AND NOT s.reserved AND util.VN_CURDATE() = vDate
AND ic.merchandise AND t.warehouseFk = vWarehouseFk
AND IF(vIsTodayRelative, TRUE, DATE(t.shipped) = vDate) GROUP BY s.id;
AND NOT i.generic
AND util.VN_CURDATE() = vDate
AND t.warehouseFk = vWarehouseFk
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 -- Inventario: Visible suficiente, pero ubicado menor a la cantidad vendida
INSERT INTO tmp.sale_problems(ticketFk, itemLost, saleFk) INSERT INTO tmp.saleProblems(saleFk, hasItemLost)
SELECT ticketFk, problem, saleFk SELECT s.id, TRUE
FROM ( FROM tmp.sale ts
SELECT sgp.ticketFk, JOIN sale s ON s.id = ts.saleFk
LEFT(GROUP_CONCAT('I: ', i.id, ' ', i.longName, ' '), 250) problem, JOIN ticket t ON t.id = s.ticketFk
s.id saleFk JOIN item i ON i.id = s.itemFk
FROM tmp.sale_getProblems sgp JOIN itemType it ON it.id = i.typeFk
JOIN ticket t ON t.id = sgp.ticketFk JOIN itemCategory ic ON ic.id = it.categoryFk
JOIN sale s ON s.ticketFk = t.id LEFT JOIN cache.visible v ON v.item_id = s.itemFk
JOIN item i ON i.id = s.itemFk AND v.calc_id = vVisibleCache
JOIN itemType it ON it.id = i.typeFk LEFT JOIN tItemShelving tis ON tis.itemFk = i.id
JOIN itemCategory ic ON ic.id = it.categoryFk AND tis.warehouseFk = t.warehouseFk
LEFT JOIN cache.visible v ON v.item_id = s.itemFk WHERE IFNULL(v.visible, 0) >= s.quantity
AND v.calc_id = vVisibleCache AND IFNULL(tis.visible, 0) < s.quantity
LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND s.quantity > 0
AND issw.warehouseFk = t.warehouseFk AND NOT s.isPicked
WHERE IFNULL(v.visible, 0) >= s.quantity AND NOT s.reserved
AND IFNULL(issw.visible, 0) < s.quantity AND ic.merchandise
AND s.quantity > 0 AND IF(vIsTodayRelative, TRUE, DATE(t.shipped) = vDate)
AND NOT s.isPicked AND NOT i.generic
AND NOT s.reserved AND util.VN_CURDATE() = vDate
AND ic.merchandise AND t.warehouseFk = vWarehouseFk
AND IF(vIsTodayRelative, TRUE, DATE(t.shipped) = vDate) GROUP BY s.id
AND NOT i.generic ON DUPLICATE KEY UPDATE hasItemLost = TRUE;
AND util.VN_CURDATE() = vDate
AND t.warehouseFk = vWarehouseFk
GROUP BY sgp.ticketFk
) sub
ON DUPLICATE KEY UPDATE itemLost = sub.problem, saleFk = sub.saleFk;
-- Retraso: Disponible suficiente, pero no visible ni ubicado -- Retraso: Disponible suficiente, pero no visible ni ubicado
INSERT INTO tmp.sale_problems(ticketFk, itemDelay, saleFk) INSERT INTO tmp.saleProblems(saleFk, hasItemDelay)
SELECT ticketFk, problem, saleFk SELECT s.id, TRUE
FROM ( FROM tmp.sale ts
SELECT sgp.ticketFk, JOIN sale s ON s.id = ts.saleFk
LEFT(GROUP_CONCAT('R: ', i.id, ' ', i.longName, ' '), 250) problem, JOIN ticket t ON t.id = s.ticketFk
s.id saleFk JOIN item i ON i.id = s.itemFk
FROM tmp.sale_getProblems sgp JOIN itemType it ON it.id = i.typeFk
JOIN ticket t ON t.id = sgp.ticketFk JOIN itemCategory ic ON ic.id = it.categoryFk
JOIN sale s ON s.ticketFk = t.id LEFT JOIN cache.visible v ON v.item_id = s.itemFk
JOIN item i ON i.id = s.itemFk AND v.calc_id = vVisibleCache
JOIN itemType it ON it.id = i.typeFk LEFT JOIN cache.available av ON av.item_id = i.id
JOIN itemCategory ic ON ic.id = it.categoryFk AND av.calc_id = vAvailableCache
LEFT JOIN cache.visible v ON v.item_id = s.itemFk LEFT JOIN tItemShelving tis ON tis.itemFk = i.id
AND v.calc_id = vVisibleCache AND tis.warehouseFk = t.warehouseFk
LEFT JOIN cache.available av ON av.item_id = i.id WHERE IFNULL(v.visible, 0) < s.quantity
AND av.calc_id = vAvailableCache AND IFNULL(av.available, 0) >= s.quantity
LEFT JOIN tItemShelvingStock_byWarehouse issw ON issw.itemFk = i.id AND IFNULL(tis.visible, 0) < s.quantity
AND issw.warehouseFk = t.warehouseFk AND s.quantity > 0
WHERE IFNULL(v.visible, 0) < s.quantity AND NOT s.isPicked
AND IFNULL(av.available, 0) >= s.quantity AND NOT s.reserved
AND IFNULL(issw.visible, 0) < s.quantity AND ic.merchandise
AND s.quantity > 0 AND IF(vIsTodayRelative, TRUE, DATE(t.shipped) = vDate)
AND NOT s.isPicked AND NOT i.generic
AND NOT s.reserved AND util.VN_CURDATE() = vDate
AND ic.merchandise AND t.warehouseFk = vWarehouseFk
AND IF(vIsTodayRelative, TRUE, DATE(t.shipped) = vDate) GROUP BY s.id
AND NOT i.generic ON DUPLICATE KEY UPDATE hasItemDelay = TRUE;
AND util.VN_CURDATE() = vDate
AND t.warehouseFk = vWarehouseFk
GROUP BY sgp.ticketFk
) sub
ON DUPLICATE KEY UPDATE itemDelay = sub.problem, saleFk = sub.saleFk;
-- Redondeo: cantidad incorrecta con respecto al grouping -- Redondeo: cantidad incorrecta con respecto al grouping
CALL buy_getUltimate(NULL, vWarehouseFk, vDate); CALL buy_getUltimate(NULL, vWarehouseFk, vDate);
INSERT INTO tmp.sale_problems(ticketFk, hasRounding, saleFk)
SELECT ticketFk, problem, saleFk INSERT INTO tmp.saleProblems(saleFk, hasRounding)
FROM ( SELECT s.id, TRUE
SELECT sgp.ticketFk, FROM tmp.sale ts
s.id saleFk, JOIN sale s ON s.id = ts.saleFk
LEFT(GROUP_CONCAT('RE: ',i.id, ' ', IFNULL(i.longName,'') SEPARATOR ', '), 250) problem JOIN ticket t ON t.id = s.ticketFk
FROM tmp.sale_getProblems sgp AND t.warehouseFk = vWarehouseFk
JOIN ticket t ON t.id = sgp.ticketFk JOIN item i ON i.id = s.itemFk
AND t.warehouseFk = vWarehouseFk JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk
JOIN sale s ON s.ticketFk = sgp.ticketFk JOIN buy b ON b.id = bu.buyFk
JOIN item i ON i.id = s.itemFk WHERE MOD(s.quantity, b.`grouping`)
JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk GROUP BY s.id
JOIN buy b ON b.id = bu.buyFk ON DUPLICATE KEY UPDATE hasRounding = TRUE;
WHERE MOD(s.quantity, b.`grouping`)
GROUP BY sgp.ticketFk
)sub
ON DUPLICATE KEY UPDATE hasRounding = sub.problem, saleFk = sub.saleFk;
DROP TEMPORARY TABLE tmp.buyUltimate; DROP TEMPORARY TABLE tmp.buyUltimate;
END LOOP; END LOOP;
CLOSE vCursor; CLOSE vCursor;
DROP TEMPORARY TABLE tItemShelvingStock_byWarehouse; DROP TEMPORARY TABLE tItemShelving;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -1,25 +1,25 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`sale_getProblemsByTicket`(IN vTicketFk INT, IN vIsTodayRelative TINYINT(1)) CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`sale_getProblemsByTicket`(
IN vTicketFk INT,
IN vIsTodayRelative TINYINT(1)
)
BEGIN BEGIN
/** /**
* Calcula los problemas de cada venta * Calcula los problemas de cada venta para un tickets.
* para un conjunto de tickets.
* *
* @return Problems result * @return Problems result
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems CREATE OR REPLACE TEMPORARY TABLE tmp.sale
(INDEX (ticketFk)) (INDEX (saleFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT t.id ticketFk, t.clientFk, t.warehouseFk, t.shipped SELECT id saleFk FROM sale WHERE ticketFk = vTicketFk;
FROM ticket t
WHERE t.id = vTicketFk;
CALL sale_getProblems(vIsTodayRelative); CALL sale_getProblems(vIsTodayRelative);
SELECT * FROM tmp.sale_problems; SELECT * FROM tmp.saleProblems;
DROP TEMPORARY TABLE DROP TEMPORARY TABLE
tmp.sale_getProblems, tmp.saleProblems,
tmp.sale_problems; tmp.sale;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -1,53 +1,111 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_getProblems`( CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_getProblems`(
vIsTodayRelative tinyint(1) vIsTodayRelative TINYINT(1)
) )
BEGIN BEGIN
/** /**
* Calcula los problemas para un conjunto de tickets. * Calcula los problemas para un conjunto de tickets.
* Agrupados por ticket
* *
* @table tmp.sale_getProblems(ticketFk, clientFk, warehouseFk, shipped) Identificadores de los tickets a calcular * @table tmp.ticket(ticketFk) Identificadores de los tickets a calcular
* @return tmp.ticket_problems * @return tmp.ticketProblems, tmp.saleProblems
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.sale (
saleFk INT(11),
PRIMARY KEY (saleFk)
) ENGINE = MEMORY
SELECT DISTINCT s.id saleFk
FROM tmp.ticket tt
JOIN ticket t ON t.id = tt.ticketFk
JOIN sale s ON s.ticketFk = t.id
WHERE t.shipped BETWEEN util.VN_CURDATE()
AND util.dayEnd(util.VN_CURDATE() + INTERVAL IF(vIsTodayRelative, 9.9, 1.9) DAY)
GROUP BY s.id;
CALL sale_getProblems(vIsTodayRelative); CALL sale_getProblems(vIsTodayRelative);
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket_problems CREATE OR REPLACE TEMPORARY TABLE tmp.ticketProblems (
(PRIMARY KEY (ticketFk)) ticketFk INT(11),
ENGINE = MEMORY isFreezed BOOL DEFAULT FALSE,
SELECT ticketFk, risk DECIMAL(10,1) DEFAULT 0,
MAX(isFreezed) isFreezed, hasRisk BOOL DEFAULT FALSE,
MAX(risk) risk, hasHighRisk BOOL DEFAULT FALSE,
MAX(hasRisk) hasRisk, hasTicketRequest BOOL DEFAULT FALSE,
MAX(hasHighRisk) hasHighRisk, isTaxDataChecked BOOL DEFAULT FALSE,
MAX(hasTicketRequest) hasTicketRequest, isTooLittle BOOL DEFAULT FALSE,
MAX(itemShortage) itemShortage, isVip BOOL DEFAULT FALSE,
MIN(isTaxDataChecked) isTaxDataChecked, hasItemShortage BOOL DEFAULT FALSE,
MAX(hasComponentLack) hasComponentLack, hasItemDelay BOOL DEFAULT FALSE,
MAX(isTooLittle) isTooLittle, hasItemLost BOOL DEFAULT FALSE,
MAX(itemDelay) itemDelay, hasComponentLack BOOL DEFAULT FALSE,
MAX(hasRounding) hasRounding, hasRounding BOOL DEFAULT FALSE,
MAX(itemLost) itemLost, PRIMARY KEY (ticketFk)
MAX(isVip) isVip, ) ENGINE = MEMORY
WITH hasItemShortage AS(
SELECT s.ticketFk
FROM tmp.saleProblems sp
JOIN vn.sale s ON s.id = sp.saleFk
WHERE sp.hasItemShortage
GROUP BY s.ticketFk
),hasItemLost AS(
SELECT s.ticketFk
FROM tmp.saleProblems sp
JOIN vn.sale s ON s.id = sp.saleFk
WHERE sp.hasItemLost
GROUP BY s.ticketFk
),hasRounding AS(
SELECT s.ticketFk
FROM tmp.saleProblems sp
JOIN vn.sale s ON s.id = sp.saleFk
WHERE sp.hasRounding
GROUP BY s.ticketFk
), hasItemDelay AS(
SELECT s.ticketFk
FROM tmp.saleProblems sp
JOIN vn.sale s ON s.id = sp.saleFk
WHERE sp.hasItemDelay
GROUP BY s.ticketFk
), hasComponentLack AS(
SELECT s.ticketFk
FROM tmp.saleProblems sp
JOIN vn.sale s ON s.id = sp.saleFk
WHERE FIND_IN_SET('hasComponentLack', s.problem)
GROUP BY s.ticketFk
)SELECT tt.ticketFk,
IF(FIND_IN_SET('isFreezed', t.problem), TRUE, FALSE) isFreezed,
t.risk,
IF(FIND_IN_SET('hasRisk', t.problem), TRUE, FALSE) hasRisk,
IF(FIND_IN_SET('hasHighRisk', t.problem), TRUE, FALSE) hasHighRisk,
IF(FIND_IN_SET('hasTicketRequest', t.problem), TRUE, FALSE) hasTicketRequest,
IF(FIND_IN_SET('isTaxDataChecked', t.problem), TRUE, FALSE) isTaxDataChecked,
IF(FIND_IN_SET('isTooLittle', t.problem)
AND util.VN_NOW() < (util.VN_CURDATE() +
INTERVAL HOUR(zc.`hour`) HOUR) +
INTERVAL MINUTE(zc.`hour`) MINUTE,
TRUE, FALSE) isTooLittle,
IF(c.businessTypeFk = 'VIP', TRUE, FALSE) isVip,
IF(his.ticketFk IS NULL, FALSE, TRUE) hasItemShortage,
IF(hid.ticketFk IS NULL, FALSE, TRUE) hasItemDelay,
IF(hil.ticketFk IS NULL, FALSE, TRUE) hasItemLost,
IF(hcl.ticketFk IS NULL, FALSE, TRUE) hasComponentLack,
IF(hr.ticketFk IS NULL, FALSE, TRUE) hasRounding,
0 totalProblems 0 totalProblems
FROM tmp.sale_problems FROM tmp.ticket tt
GROUP BY ticketFk; JOIN vn.ticket t ON t.id = tt.ticketFk
JOIN vn.client c ON c.id = t.clientFk
UPDATE tmp.ticket_problems LEFT JOIN hasItemShortage his ON his.ticketFk = t.id
SET totalProblems = ( LEFT JOIN hasItemLost hil ON hil.ticketFk = t.id
(isFreezed) + LEFT JOIN hasRounding hr ON hr.ticketFk = t.id
(hasHighRisk) + LEFT JOIN hasItemDelay hid ON hid.ticketFk = t.id
(hasTicketRequest) + LEFT JOIN hasComponentLack hcl ON hcl.ticketFk = t.id
(!isTaxDataChecked) + LEFT JOIN vn.zoneClosure zc ON zc.zoneFk = t.zoneFk
(hasComponentLack) + AND zc.dated = util.VN_CURDATE()
(itemDelay IS NOT NULL) + GROUP BY t.id;
(isTooLittle) +
(itemLost IS NOT NULL) + UPDATE tmp.ticketProblems
(hasRounding IS NOT NULL) + SET totalProblems = isFreezed + hasHighRisk + hasTicketRequest +
(itemShortage IS NOT NULL) + isTaxDataChecked + hasComponentLack + hasItemDelay +
(isVip) isTooLittle + hasItemLost + hasRounding + hasItemShortage + isVip;
);
DROP TEMPORARY TABLE tmp.sale;
DROP TEMPORARY TABLE tmp.sale_problems;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -239,10 +239,10 @@ module.exports = Self => {
stmts.push(`SET SESSION optimizer_search_depth = @_optimizer_search_depth`); stmts.push(`SET SESSION optimizer_search_depth = @_optimizer_search_depth`);
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped SELECT f.id ticketFk
FROM tmp.filter f FROM tmp.filter f
LEFT JOIN alertLevel al ON al.id = f.alertLevel LEFT JOIN alertLevel al ON al.id = f.alertLevel
WHERE (al.code = 'FREE' OR f.alertLevel IS NULL) WHERE (al.code = 'FREE' OR f.alertLevel IS NULL)
@ -263,7 +263,7 @@ module.exports = Self => {
stmts.push('CALL ticket_getWarnings()'); stmts.push('CALL ticket_getWarnings()');
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
UPDATE tmp.ticket_problems UPDATE tmp.ticketProblems
SET risk = IF(hasRisk, risk, 0) SET risk = IF(hasRisk, risk, 0)
`); `);
stmts.push(stmt); stmts.push(stmt);
@ -271,7 +271,7 @@ module.exports = Self => {
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
SELECT * SELECT *
FROM tmp.filter f FROM tmp.filter f
LEFT JOIN tmp.ticket_problems tp ON tp.ticketFk = f.id LEFT JOIN tmp.ticketProblems tp ON tp.ticketFk = f.id
LEFT JOIN tmp.ticket_warnings tw ON tw.ticketFk = f.id LEFT JOIN tmp.ticket_warnings tw ON tw.ticketFk = f.id
`); `);
stmts.push(stmt); stmts.push(stmt);
@ -288,8 +288,8 @@ module.exports = Self => {
{'tp.hasRisk': true}, {'tp.hasRisk': true},
{'tp.hasTicketRequest': true}, {'tp.hasTicketRequest': true},
{'tp.hasComponentLack': true}, {'tp.hasComponentLack': true},
{'tp.isTaxDataChecked': false}, {'tp.isTaxDataChecked': true},
{'tp.itemShortage': {neq: null}}, {'tp.hasItemShortage': true},
{'tp.isTooLittle': true} {'tp.isTooLittle': true}
]}; ]};
} else if (hasProblems === false) { } else if (hasProblems === false) {
@ -298,8 +298,8 @@ module.exports = Self => {
{'tp.hasRisk': false}, {'tp.hasRisk': false},
{'tp.hasTicketRequest': false}, {'tp.hasTicketRequest': false},
{'tp.hasComponentLack': false}, {'tp.hasComponentLack': false},
{'tp.isTaxDataChecked': true}, {'tp.isTaxDataChecked': false},
{'tp.itemShortage': null}, {'tp.hasItemShortage': false},
{'tp.isTooLittle': false} {'tp.isTooLittle': false}
]}; ]};
} }
@ -375,9 +375,9 @@ module.exports = Self => {
stmts.push(` stmts.push(`
DROP TEMPORARY TABLE DROP TEMPORARY TABLE
tmp.ticket,
tmp.filter, tmp.filter,
tmp.ticket_problems, tmp.ticketProblems,
tmp.sale_getProblems,
tmp.sale_getWarnings, tmp.sale_getWarnings,
tmp.ticket_warnings tmp.ticket_warnings
`); `);

View File

@ -68,7 +68,7 @@ describe('SalesMonitor salesFilter()', () => {
const filter = {}; const filter = {};
const result = await models.SalesMonitor.salesFilter(ctx, filter, options); const result = await models.SalesMonitor.salesFilter(ctx, filter, options);
expect(result.length).toEqual(4); expect(result.length).toEqual(5);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -295,10 +295,10 @@ module.exports = Self => {
stmts.push(stmt); stmts.push(stmt);
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped SELECT f.id ticketFk
FROM tmp.filter f FROM tmp.filter f
LEFT JOIN alertLevel al ON al.id = f.alertLevel LEFT JOIN alertLevel al ON al.id = f.alertLevel
WHERE (al.code = 'FREE' OR f.alertLevel IS NULL) WHERE (al.code = 'FREE' OR f.alertLevel IS NULL)
@ -309,7 +309,7 @@ module.exports = Self => {
stmts.push('CALL ticket_getProblems(FALSE)'); stmts.push('CALL ticket_getProblems(FALSE)');
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
UPDATE tmp.ticket_problems UPDATE tmp.ticketProblems
SET risk = IF(hasRisk, risk, 0) SET risk = IF(hasRisk, risk, 0)
`); `);
stmts.push(stmt); stmts.push(stmt);
@ -317,7 +317,7 @@ module.exports = Self => {
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
SELECT f.*, tp.* SELECT f.*, tp.*
FROM tmp.filter f FROM tmp.filter f
LEFT JOIN tmp.ticket_problems tp ON tp.ticketFk = f.id LEFT JOIN tmp.ticketProblems tp ON tp.ticketFk = f.id
`); `);
if (args.problems != undefined && (!args.from && !args.to)) if (args.problems != undefined && (!args.from && !args.to))
@ -325,20 +325,17 @@ module.exports = Self => {
let condition; let condition;
let hasProblem; let hasProblem;
let range;
let hasWhere; let hasWhere;
switch (args.problems) { switch (args.problems) {
case true: case true:
condition = `or`; condition = `or`;
hasProblem = true; hasProblem = true;
range = {neq: null};
hasWhere = true; hasWhere = true;
break; break;
case false: case false:
condition = `and`; condition = `and`;
hasProblem = null; hasProblem = null;
range = null;
hasWhere = true; hasWhere = true;
break; break;
} }
@ -347,7 +344,7 @@ module.exports = Self => {
{'tp.isFreezed': hasProblem}, {'tp.isFreezed': hasProblem},
{'tp.hasRisk': hasProblem}, {'tp.hasRisk': hasProblem},
{'tp.hasTicketRequest': hasProblem}, {'tp.hasTicketRequest': hasProblem},
{'tp.itemShortage': range}, {'tp.hasItemShortage': hasProblem},
{'tp.hasRounding': hasProblem} {'tp.hasRounding': hasProblem}
]}; ]};
@ -372,8 +369,9 @@ module.exports = Self => {
stmts.push( stmts.push(
`DROP TEMPORARY TABLE `DROP TEMPORARY TABLE
tmp.ticket,
tmp.filter, tmp.filter,
tmp.ticket_problems`); tmp.ticketProblems`);
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions); const result = await conn.executeStmt(sql, myOptions);

View File

@ -146,10 +146,10 @@ module.exports = Self => {
stmts.push(stmt); stmts.push(stmt);
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
CREATE OR REPLACE TEMPORARY TABLE tmp.sale_getProblems CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped, f.lines, f.liters SELECT f.id ticketFk
FROM tmp.filter f FROM tmp.filter f
LEFT JOIN alertLevel al ON al.id = f.alertLevel LEFT JOIN alertLevel al ON al.id = f.alertLevel
WHERE (al.code = 'FREE' OR f.alertLevel IS NULL) WHERE (al.code = 'FREE' OR f.alertLevel IS NULL)
@ -159,7 +159,7 @@ module.exports = Self => {
stmts.push('CALL ticket_getProblems(FALSE)'); stmts.push('CALL ticket_getProblems(FALSE)');
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
UPDATE tmp.ticket_problems UPDATE tmp.ticketProblems
SET risk = IF(hasRisk, risk, 0) SET risk = IF(hasRisk, risk, 0)
`); `);
stmts.push(stmt); stmts.push(stmt);
@ -167,7 +167,7 @@ module.exports = Self => {
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
SELECT f.*, tp.* SELECT f.*, tp.*
FROM tmp.filter f FROM tmp.filter f
LEFT JOIN tmp.ticket_problems tp ON tp.ticketFk = f.id LEFT JOIN tmp.ticketProblems tp ON tp.ticketFk = f.id
`); `);
if (args.problems != undefined && (!args.originScopeDays && !args.futureScopeDays)) if (args.problems != undefined && (!args.originScopeDays && !args.futureScopeDays))
@ -175,20 +175,17 @@ module.exports = Self => {
let condition; let condition;
let hasProblem; let hasProblem;
let range;
let hasWhere; let hasWhere;
switch (args.problems) { switch (args.problems) {
case true: case true:
condition = `or`; condition = `or`;
hasProblem = true; hasProblem = true;
range = {neq: null};
hasWhere = true; hasWhere = true;
break; break;
case false: case false:
condition = `and`; condition = `and`;
hasProblem = null; hasProblem = null;
range = null;
hasWhere = true; hasWhere = true;
break; break;
} }
@ -198,7 +195,7 @@ module.exports = Self => {
{'tp.isFreezed': hasProblem}, {'tp.isFreezed': hasProblem},
{'tp.hasRisk': hasProblem}, {'tp.hasRisk': hasProblem},
{'tp.hasTicketRequest': hasProblem}, {'tp.hasTicketRequest': hasProblem},
{'tp.itemShortage': range}, {'tp.hasItemShortage': hasProblem},
{'tp.hasComponentLack': hasProblem}, {'tp.hasComponentLack': hasProblem},
{'tp.isTooLittle': hasProblem}, {'tp.isTooLittle': hasProblem},
{'tp.hasRounding': hasProblem} {'tp.hasRounding': hasProblem}
@ -216,8 +213,9 @@ module.exports = Self => {
stmts.push( stmts.push(
`DROP TEMPORARY TABLE `DROP TEMPORARY TABLE
tmp.ticket,
tmp.filter, tmp.filter,
tmp.ticket_problems`); tmp.ticketProblems`);
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions); const result = await conn.executeStmt(sql, myOptions);

View File

@ -42,11 +42,11 @@ describe('ticket filter()', () => {
const result = await models.Ticket.filter(ctx, filter, options); const result = await models.Ticket.filter(ctx, filter, options);
const hasProblemTicket = result.some(ticket => const hasProblemTicket = result.some(ticket =>
ticket.isFreezed === true || ticket.isFreezed == true ||
ticket.hasRisk === true || ticket.hasRisk == true ||
ticket.hasTicketRequest === true || ticket.hasTicketRequest == true ||
(typeof ticket.hasRounding === 'string' && ticket.hasRounding.trim().length > 0) || ticket.hasRounding == true ||
(typeof ticket.itemShortage === 'string' && ticket.itemShortage.trim().length > 0) ticket.hasItemShortage == true
); );
expect(hasProblemTicket).toBe(true); expect(hasProblemTicket).toBe(true);
@ -83,7 +83,7 @@ describe('ticket filter()', () => {
expect(ticket.isFreezed).toEqual(null); expect(ticket.isFreezed).toEqual(null);
expect(ticket.hasRisk).toEqual(null); expect(ticket.hasRisk).toEqual(null);
expect(ticket.hasTicketRequest).toEqual(null); expect(ticket.hasTicketRequest).toEqual(null);
expect(ticket.itemShortage).toEqual(null); expect(ticket.hasItemShortage).toEqual(null);
expect(ticket.hasRounding).toEqual(null); expect(ticket.hasRounding).toEqual(null);
}); });