8179-testToMaster #3176
|
@ -2,7 +2,7 @@ DELIMITER $$
|
||||||
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticket_isTooLittle`(
|
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticket_isTooLittle`(
|
||||||
vSelf INT
|
vSelf INT
|
||||||
)
|
)
|
||||||
RETURNS tinyint(1)
|
RETURNS BOOL
|
||||||
READS SQL DATA
|
READS SQL DATA
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
|
@ -11,14 +11,21 @@ BEGIN
|
||||||
* @param vSelf Id ticket
|
* @param vSelf Id ticket
|
||||||
* @return BOOL
|
* @return BOOL
|
||||||
*/
|
*/
|
||||||
DECLARE vIsTooLittle TINYINT(1);
|
DECLARE vIsTooLittle BOOL;
|
||||||
|
|
||||||
|
WITH ticketData AS (
|
||||||
|
SELECT addressFk, DATE(shipped) dated
|
||||||
|
FROM vn.ticket
|
||||||
|
WHERE id = vSelf
|
||||||
|
)
|
||||||
SELECT (SUM(IFNULL(sv.litros, 0)) < vc.minTicketVolume
|
SELECT (SUM(IFNULL(sv.litros, 0)) < vc.minTicketVolume
|
||||||
AND IFNULL(t.totalWithoutVat, 0) < vc.minTicketValue) INTO vIsTooLittle
|
AND SUM(IFNULL(t.totalWithoutVat, 0)) < vc.minTicketValue) INTO vIsTooLittle
|
||||||
FROM ticket t
|
FROM ticketData td
|
||||||
LEFT JOIN saleVolume sv ON sv.ticketFk = t.id
|
JOIN vn.ticket t ON t.addressFk = td.addressFk
|
||||||
JOIN volumeConfig vc
|
LEFT JOIN vn.saleVolume sv ON sv.ticketFk = t.id
|
||||||
WHERE t.id = vSelf;
|
JOIN vn.volumeConfig vc
|
||||||
|
WHERE t.shipped BETWEEN td.dated AND util.dayEnd(td.dated)
|
||||||
|
AND ticket_isProblemCalcNeeded(t.id);
|
||||||
|
|
||||||
RETURN vIsTooLittle;
|
RETURN vIsTooLittle;
|
||||||
END$$
|
END$$
|
||||||
|
|
|
@ -19,14 +19,12 @@ BEGIN
|
||||||
DELETE FROM workerActivity WHERE created < v2Years;
|
DELETE FROM workerActivity WHERE created < v2Years;
|
||||||
DELETE FROM ticketParking WHERE created < v2Months;
|
DELETE FROM ticketParking WHERE created < v2Months;
|
||||||
DELETE FROM routesMonitor WHERE dated < v2Months;
|
DELETE FROM routesMonitor WHERE dated < v2Months;
|
||||||
DELETE FROM workerTimeControlLog WHERE created < v2Months;
|
|
||||||
DELETE FROM `message` WHERE sendDate < v2Months;
|
DELETE FROM `message` WHERE sendDate < v2Months;
|
||||||
DELETE FROM messageInbox WHERE sendDate < v2Months;
|
DELETE FROM messageInbox WHERE sendDate < v2Months;
|
||||||
DELETE FROM messageInbox WHERE sendDate < v2Months;
|
DELETE FROM messageInbox WHERE sendDate < v2Months;
|
||||||
DELETE FROM workerTimeControl WHERE timed < v4Years;
|
DELETE FROM workerTimeControl WHERE timed < v4Years;
|
||||||
DELETE FROM itemShelving WHERE created < util.VN_CURDATE() AND visible = 0;
|
DELETE FROM itemShelving WHERE created < util.VN_CURDATE() AND visible = 0;
|
||||||
DELETE FROM ticketDown WHERE created < util.yesterday();
|
DELETE FROM ticketDown WHERE created < util.yesterday();
|
||||||
DELETE FROM entryLog WHERE creationDate < v2Months;
|
|
||||||
DELETE IGNORE FROM expedition WHERE created < v26Months;
|
DELETE IGNORE FROM expedition WHERE created < v26Months;
|
||||||
DELETE cs
|
DELETE cs
|
||||||
FROM sms s
|
FROM sms s
|
||||||
|
@ -61,11 +59,8 @@ BEGIN
|
||||||
DELETE b FROM buy b
|
DELETE b FROM buy b
|
||||||
JOIN entryConfig e ON e.defaultEntry = b.entryFk
|
JOIN entryConfig e ON e.defaultEntry = b.entryFk
|
||||||
WHERE b.created < v2Months;
|
WHERE b.created < v2Months;
|
||||||
DELETE FROM itemShelvingLog WHERE created < v2Months;
|
|
||||||
DELETE FROM stockBuyed WHERE creationDate < v2Months;
|
DELETE FROM stockBuyed WHERE creationDate < v2Months;
|
||||||
DELETE FROM itemCleanLog WHERE created < util.VN_NOW() - INTERVAL 1 YEAR;
|
|
||||||
DELETE FROM printQueue WHERE statusCode = 'printed' AND created < v2Months;
|
DELETE FROM printQueue WHERE statusCode = 'printed' AND created < v2Months;
|
||||||
DELETE FROM ticketLog WHERE creationDate <= v5Years;
|
|
||||||
-- Equipos duplicados
|
-- Equipos duplicados
|
||||||
DELETE w.*
|
DELETE w.*
|
||||||
FROM workerTeam w
|
FROM workerTeam w
|
||||||
|
@ -174,7 +169,6 @@ BEGIN
|
||||||
|
|
||||||
-- Borra los registros de collection y ticketcollection
|
-- Borra los registros de collection y ticketcollection
|
||||||
DELETE FROM collection WHERE created < v2Months;
|
DELETE FROM collection WHERE created < v2Months;
|
||||||
DELETE FROM travelLog WHERE creationDate < v3Months;
|
|
||||||
|
|
||||||
CALL shelving_clean();
|
CALL shelving_clean();
|
||||||
|
|
||||||
|
|
|
@ -13,39 +13,42 @@ BEGIN
|
||||||
* @param vCollectionFk Id de colección
|
* @param vCollectionFk Id de colección
|
||||||
*/
|
*/
|
||||||
DECLARE vHasTooMuchCollections BOOL;
|
DECLARE vHasTooMuchCollections BOOL;
|
||||||
DECLARE vItemPackingTypeFk VARCHAR(1);
|
DECLARE vDone BOOL DEFAULT FALSE;
|
||||||
DECLARE vWarehouseFk INT;
|
DECLARE vCollectionWorker INT;
|
||||||
DECLARE vLockName VARCHAR(215);
|
DECLARE vMaxNotAssignedCollectionLifeTime TIME;
|
||||||
DECLARE vLockTime INT DEFAULT 30;
|
|
||||||
DECLARE vErrorNumber INT;
|
|
||||||
DECLARE vErrorMsg TEXT;
|
|
||||||
|
|
||||||
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
DECLARE vCollections CURSOR FOR
|
||||||
BEGIN
|
WITH collections AS (
|
||||||
GET DIAGNOSTICS CONDITION 1
|
SELECT tc.collectionFk,
|
||||||
vErrorNumber = MYSQL_ERRNO,
|
SUM(sv.volume) volume,
|
||||||
vErrorMsg = MESSAGE_TEXT;
|
c.saleTotalCount,
|
||||||
|
c.itemPackingTypeFk,
|
||||||
CALL util.debugAdd('collection_assign', JSON_OBJECT(
|
c.trainFk,
|
||||||
'errorNumber', vErrorNumber,
|
c.warehouseFk,
|
||||||
'errorMsg', vErrorMsg,
|
c.wagons
|
||||||
'lockName', vLockName,
|
FROM vn.ticketCollection tc
|
||||||
'userFk', vUserFk
|
JOIN vn.collection c ON c.id = tc.collectionFk
|
||||||
)); -- Tmp
|
JOIN vn.saleVolume sv ON sv.ticketFk = tc.ticketFk
|
||||||
|
WHERE c.workerFk IS NULL
|
||||||
IF vLockName IS NOT NULL THEN
|
AND sv.shipped >= util.VN_CURDATE()
|
||||||
DO RELEASE_LOCK(vLockName);
|
GROUP BY tc.collectionFk
|
||||||
END IF;
|
) SELECT c.collectionFk
|
||||||
RESIGNAL;
|
FROM collections c
|
||||||
END;
|
JOIN vn.operator o
|
||||||
|
WHERE o.workerFk = vUserFk
|
||||||
|
AND (c.saleTotalCount <= o.linesLimit OR o.linesLimit IS NULL)
|
||||||
|
AND (c.itemPackingTypeFk = o.itemPackingTypeFk OR o.itemPackingTypeFk IS NULL)
|
||||||
|
AND o.numberOfWagons = c.wagons
|
||||||
|
AND o.trainFk = c.trainFk
|
||||||
|
AND o.warehouseFk = c.warehouseFk;
|
||||||
|
|
||||||
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
||||||
-- Si hay colecciones sin terminar, sale del proceso
|
-- Si hay colecciones sin terminar, sale del proceso
|
||||||
|
|
||||||
CALL collection_get(vUserFk);
|
CALL collection_get(vUserFk);
|
||||||
|
|
||||||
SELECT (pc.maxNotReadyCollections - COUNT(*)) <= 0,
|
SELECT (pc.maxNotReadyCollections - COUNT(*)) <= 0, pc.maxNotAssignedCollectionLifeTime
|
||||||
collection_assign_lockname
|
INTO vHasTooMuchCollections, vMaxNotAssignedCollectionLifeTime
|
||||||
INTO vHasTooMuchCollections,
|
|
||||||
vLockName
|
|
||||||
FROM productionConfig pc
|
FROM productionConfig pc
|
||||||
LEFT JOIN tmp.collection ON TRUE;
|
LEFT JOIN tmp.collection ON TRUE;
|
||||||
|
|
||||||
|
@ -55,69 +58,69 @@ BEGIN
|
||||||
CALL util.throw('Hay colecciones pendientes');
|
CALL util.throw('Hay colecciones pendientes');
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SELECT warehouseFk, itemPackingTypeFk
|
|
||||||
INTO vWarehouseFk, vItemPackingTypeFk
|
|
||||||
FROM operator
|
|
||||||
WHERE workerFk = vUserFk;
|
|
||||||
|
|
||||||
SET vLockName = CONCAT_WS('/',
|
|
||||||
vLockName,
|
|
||||||
vWarehouseFk,
|
|
||||||
vItemPackingTypeFk
|
|
||||||
);
|
|
||||||
|
|
||||||
IF NOT GET_LOCK(vLockName, vLockTime) THEN
|
|
||||||
CALL util.throw(CONCAT('Cannot get lock: ', vLockName));
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
-- Se eliminan las colecciones sin asignar que estan obsoletas
|
-- Se eliminan las colecciones sin asignar que estan obsoletas
|
||||||
INSERT INTO ticketTracking(stateFk, ticketFk)
|
|
||||||
SELECT s.id, tc.ticketFk
|
|
||||||
FROM `collection` c
|
|
||||||
JOIN ticketCollection tc ON tc.collectionFk = c.id
|
|
||||||
JOIN `state` s ON s.code = 'PRINTED_AUTO'
|
|
||||||
JOIN productionConfig pc
|
|
||||||
WHERE c.workerFk IS NULL
|
|
||||||
AND TIMEDIFF(util.VN_NOW(), c.created) > pc.maxNotAssignedCollectionLifeTime;
|
|
||||||
|
|
||||||
DELETE c.*
|
INSERT INTO ticketTracking(stateFk, ticketFk)
|
||||||
FROM `collection` c
|
SELECT s.id, tc.ticketFk
|
||||||
JOIN productionConfig pc
|
FROM `collection` c
|
||||||
WHERE c.workerFk IS NULL
|
JOIN ticketCollection tc ON tc.collectionFk = c.id
|
||||||
AND TIMEDIFF(util.VN_NOW(), c.created) > pc.maxNotAssignedCollectionLifeTime;
|
JOIN `state` s ON s.code = 'PRINTED_AUTO'
|
||||||
|
WHERE c.workerFk IS NULL
|
||||||
|
AND TIMEDIFF(util.VN_NOW(), c.created) > vMaxNotAssignedCollectionLifeTime;
|
||||||
|
|
||||||
|
DELETE FROM `collection`
|
||||||
|
WHERE workerFk IS NULL
|
||||||
|
AND TIMEDIFF(util.VN_NOW(), created) > vMaxNotAssignedCollectionLifeTime;
|
||||||
|
|
||||||
-- Se añade registro al semillero
|
-- Se añade registro al semillero
|
||||||
INSERT INTO collectionHotbed(userFk)
|
|
||||||
VALUES(vUserFk);
|
INSERT INTO collectionHotbed(userFk) VALUES(vUserFk);
|
||||||
|
|
||||||
-- Comprueba si hay colecciones disponibles que se ajustan a su configuracion
|
-- Comprueba si hay colecciones disponibles que se ajustan a su configuracion
|
||||||
SELECT MIN(c.id) INTO vCollectionFk
|
|
||||||
FROM `collection` c
|
OPEN vCollections;
|
||||||
JOIN operator o
|
l: LOOP
|
||||||
ON (o.itemPackingTypeFk = c.itemPackingTypeFk OR c.itemPackingTypeFk IS NULL)
|
SET vDone = FALSE;
|
||||||
AND o.numberOfWagons = c.wagons
|
FETCH vCollections INTO vCollectionFk;
|
||||||
AND o.trainFk = c.trainFk
|
|
||||||
AND o.warehouseFk = c.warehouseFk
|
IF vDone THEN
|
||||||
AND c.workerFk IS NULL
|
LEAVE l;
|
||||||
AND (c.saleTotalCount <= o.linesLimit OR o.linesLimit IS NULL)
|
END IF;
|
||||||
JOIN (
|
|
||||||
SELECT tc.collectionFk, SUM(sv.volume) volume
|
BEGIN
|
||||||
FROM ticketCollection tc
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
||||||
JOIN saleVolume sv ON sv.ticketFk = tc.ticketFk
|
BEGIN
|
||||||
WHERE sv.shipped >= util.VN_CURDATE()
|
ROLLBACK;
|
||||||
GROUP BY tc.collectionFk
|
SET vCollectionFk = NULL;
|
||||||
) sub ON sub.collectionFk = c.id
|
RESIGNAL;
|
||||||
AND (volume <= o.volumeLimit OR o.volumeLimit IS NULL)
|
END;
|
||||||
WHERE o.workerFk = vUserFk;
|
|
||||||
|
START TRANSACTION;
|
||||||
|
|
||||||
|
SELECT workerFk INTO vCollectionWorker
|
||||||
|
FROM `collection`
|
||||||
|
WHERE id = vCollectionFk FOR UPDATE;
|
||||||
|
|
||||||
|
IF vCollectionWorker IS NULL THEN
|
||||||
|
UPDATE `collection`
|
||||||
|
SET workerFk = vUserFk
|
||||||
|
WHERE id = vCollectionFk;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
LEAVE l;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
ROLLBACK;
|
||||||
|
END;
|
||||||
|
END LOOP;
|
||||||
|
CLOSE vCollections;
|
||||||
|
|
||||||
IF vCollectionFk IS NULL THEN
|
IF vCollectionFk IS NULL THEN
|
||||||
CALL collection_new(vUserFk, vCollectionFk);
|
CALL collection_new(vUserFk, vCollectionFk);
|
||||||
|
|
||||||
|
UPDATE `collection`
|
||||||
|
SET workerFk = vUserFk
|
||||||
|
WHERE id = vCollectionFk;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
UPDATE `collection`
|
|
||||||
SET workerFk = vUserFk
|
|
||||||
WHERE id = vCollectionFk;
|
|
||||||
|
|
||||||
DO RELEASE_LOCK(vLockName);
|
|
||||||
END$$
|
END$$
|
||||||
DELIMITER ;
|
DELIMITER ;
|
|
@ -0,0 +1,29 @@
|
||||||
|
DELIMITER $$
|
||||||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_mergeSales`(vCollectionFk INT)
|
||||||
|
BEGIN
|
||||||
|
DECLARE vDone BOOL;
|
||||||
|
DECLARE vTicketFk INT;
|
||||||
|
|
||||||
|
DECLARE vTickets CURSOR FOR
|
||||||
|
SELECT ticketFk
|
||||||
|
FROM ticketCollection
|
||||||
|
WHERE collectionFk = vCollectionFk;
|
||||||
|
|
||||||
|
DECLARE CONTINUE HANDLER FOR NOT FOUND
|
||||||
|
SET vDone = TRUE;
|
||||||
|
|
||||||
|
OPEN vTickets;
|
||||||
|
l: LOOP
|
||||||
|
SET vDone = FALSE;
|
||||||
|
|
||||||
|
FETCH vTickets INTO vTicketFk;
|
||||||
|
|
||||||
|
IF vDone THEN
|
||||||
|
LEAVE l;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
CALL ticket_mergeSales(vTicketFk);
|
||||||
|
END LOOP;
|
||||||
|
CLOSE vTickets;
|
||||||
|
END$$
|
||||||
|
DELIMITER ;
|
|
@ -1,5 +1,8 @@
|
||||||
DELIMITER $$
|
DELIMITER $$
|
||||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_new`(vUserFk INT, OUT vCollectionFk INT)
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_new`(
|
||||||
|
vUserFk INT,
|
||||||
|
OUT vCollectionFk INT
|
||||||
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
* Genera colecciones de tickets sin asignar trabajador.
|
* Genera colecciones de tickets sin asignar trabajador.
|
||||||
|
@ -12,30 +15,29 @@ BEGIN
|
||||||
DECLARE vLinesLimit INT;
|
DECLARE vLinesLimit INT;
|
||||||
DECLARE vTicketLines INT;
|
DECLARE vTicketLines INT;
|
||||||
DECLARE vVolumeLimit DECIMAL;
|
DECLARE vVolumeLimit DECIMAL;
|
||||||
DECLARE vTicketVolume DECIMAL;
|
|
||||||
DECLARE vSizeLimit INT;
|
DECLARE vSizeLimit INT;
|
||||||
|
DECLARE vTicketVolume DECIMAL;
|
||||||
DECLARE vMaxTickets INT;
|
DECLARE vMaxTickets INT;
|
||||||
DECLARE vStateFk VARCHAR(45);
|
DECLARE vStateCode VARCHAR(45);
|
||||||
DECLARE vFirstTicketFk INT;
|
DECLARE vFirstTicketFk INT;
|
||||||
DECLARE vHour INT;
|
|
||||||
DECLARE vMinute INT;
|
|
||||||
DECLARE vWorkerCode VARCHAR(3);
|
DECLARE vWorkerCode VARCHAR(3);
|
||||||
DECLARE vWagonCounter INT DEFAULT 0;
|
DECLARE vWagonCounter INT DEFAULT 1;
|
||||||
DECLARE vTicketFk INT;
|
DECLARE vTicketFk INT;
|
||||||
DECLARE vItemPackingTypeFk VARCHAR(1);
|
DECLARE vItemPackingTypeFk VARCHAR(1);
|
||||||
DECLARE vHasAssignedTickets BOOLEAN;
|
DECLARE vHasAssignedTickets BOOL;
|
||||||
DECLARE vHasUniqueCollectionTime BOOL;
|
DECLARE vHasUniqueCollectionTime BOOL;
|
||||||
DECLARE vDone INT DEFAULT FALSE;
|
DECLARE vHeight INT;
|
||||||
DECLARE vLockName VARCHAR(215);
|
DECLARE vVolume INT;
|
||||||
DECLARE vLockTime INT DEFAULT 30;
|
DECLARE vLiters INT;
|
||||||
|
DECLARE vLines INT;
|
||||||
|
DECLARE vTotalLines INT DEFAULT 0;
|
||||||
|
DECLARE vTotalVolume INT DEFAULT 0;
|
||||||
DECLARE vFreeWagonFk INT;
|
DECLARE vFreeWagonFk INT;
|
||||||
DECLARE vErrorNumber INT;
|
DECLARE vDone INT DEFAULT FALSE;
|
||||||
DECLARE vErrorMsg TEXT;
|
|
||||||
|
|
||||||
DECLARE c1 CURSOR FOR
|
DECLARE vTickets CURSOR FOR
|
||||||
SELECT ticketFk, `lines`, m3
|
SELECT ticketFk, `lines`, m3
|
||||||
FROM tmp.productionBuffer
|
FROM tmp.productionBuffer
|
||||||
WHERE ticketFk <> vFirstTicketFk
|
|
||||||
ORDER BY HH,
|
ORDER BY HH,
|
||||||
mm,
|
mm,
|
||||||
productionOrder DESC,
|
productionOrder DESC,
|
||||||
|
@ -48,26 +50,6 @@ BEGIN
|
||||||
|
|
||||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
||||||
|
|
||||||
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
||||||
BEGIN
|
|
||||||
GET DIAGNOSTICS CONDITION 1
|
|
||||||
vErrorNumber = MYSQL_ERRNO,
|
|
||||||
vErrorMsg = MESSAGE_TEXT;
|
|
||||||
|
|
||||||
CALL util.debugAdd('collection_new', JSON_OBJECT(
|
|
||||||
'errorNumber', vErrorNumber,
|
|
||||||
'errorMsg', vErrorMsg,
|
|
||||||
'lockName', vLockName,
|
|
||||||
'userFk', vUserFk,
|
|
||||||
'ticketFk', vTicketFk
|
|
||||||
)); -- Tmp
|
|
||||||
|
|
||||||
IF vLockName IS NOT NULL THEN
|
|
||||||
DO RELEASE_LOCK(vLockName);
|
|
||||||
END IF;
|
|
||||||
RESIGNAL;
|
|
||||||
END;
|
|
||||||
|
|
||||||
SELECT pc.ticketTrolleyMax * o.numberOfWagons,
|
SELECT pc.ticketTrolleyMax * o.numberOfWagons,
|
||||||
pc.hasUniqueCollectionTime,
|
pc.hasUniqueCollectionTime,
|
||||||
w.code,
|
w.code,
|
||||||
|
@ -78,36 +60,26 @@ BEGIN
|
||||||
o.trainFk,
|
o.trainFk,
|
||||||
o.linesLimit,
|
o.linesLimit,
|
||||||
o.volumeLimit,
|
o.volumeLimit,
|
||||||
o.sizeLimit,
|
o.sizeLimit
|
||||||
pc.collection_new_lockname
|
|
||||||
INTO vMaxTickets,
|
INTO vMaxTickets,
|
||||||
vHasUniqueCollectionTime,
|
vHasUniqueCollectionTime,
|
||||||
vWorkerCode,
|
vWorkerCode,
|
||||||
vWarehouseFk,
|
vWarehouseFk,
|
||||||
vItemPackingTypeFk,
|
vItemPackingTypeFk,
|
||||||
vStateFk,
|
vStateCode,
|
||||||
vWagons,
|
vWagons,
|
||||||
vTrainFk,
|
vTrainFk,
|
||||||
vLinesLimit,
|
vLinesLimit,
|
||||||
vVolumeLimit,
|
vVolumeLimit,
|
||||||
vSizeLimit,
|
vSizeLimit
|
||||||
vLockName
|
FROM worker w
|
||||||
FROM productionConfig pc
|
JOIN operator o ON o.workerFk = w.id
|
||||||
JOIN worker w ON w.id = vUserFk
|
|
||||||
JOIN state st ON st.`code` = 'ON_PREPARATION'
|
JOIN state st ON st.`code` = 'ON_PREPARATION'
|
||||||
JOIN operator o ON o.workerFk = vUserFk;
|
JOIN productionConfig pc
|
||||||
|
WHERE w.id = vUserFk;
|
||||||
SET vLockName = CONCAT_WS('/',
|
|
||||||
vLockName,
|
|
||||||
vWarehouseFk,
|
|
||||||
vItemPackingTypeFk
|
|
||||||
);
|
|
||||||
|
|
||||||
IF NOT GET_LOCK(vLockName, vLockTime) THEN
|
|
||||||
CALL util.throw(CONCAT('Cannot get lock: ', vLockName));
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
-- Se prepara el tren, con tantos vagones como sea necesario.
|
-- Se prepara el tren, con tantos vagones como sea necesario.
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tTrain
|
CREATE OR REPLACE TEMPORARY TABLE tTrain
|
||||||
(wagon INT,
|
(wagon INT,
|
||||||
shelve INT,
|
shelve INT,
|
||||||
|
@ -118,59 +90,58 @@ BEGIN
|
||||||
PRIMARY KEY(wagon, shelve))
|
PRIMARY KEY(wagon, shelve))
|
||||||
ENGINE = MEMORY;
|
ENGINE = MEMORY;
|
||||||
|
|
||||||
WHILE vWagons > vWagonCounter DO
|
INSERT INTO tTrain (wagon, shelve, liters, `lines`, height)
|
||||||
SET vWagonCounter = vWagonCounter + 1;
|
WITH RECURSIVE wagonSequence AS (
|
||||||
|
SELECT vWagonCounter wagon
|
||||||
INSERT INTO tTrain(wagon, shelve, liters, `lines`, height)
|
UNION ALL
|
||||||
SELECT vWagonCounter, cv.`level` , cv.liters , cv.`lines` , cv.height
|
SELECT wagon + 1 wagon
|
||||||
FROM collectionVolumetry cv
|
FROM wagonSequence
|
||||||
WHERE cv.trainFk = vTrainFk
|
WHERE wagon < vWagonCounter + vWagons -1
|
||||||
|
)
|
||||||
|
SELECT ws.wagon, cv.`level`, cv.liters, cv.`lines`, cv.height
|
||||||
|
FROM wagonSequence ws
|
||||||
|
JOIN vn.collectionVolumetry cv ON cv.trainFk = vTrainFk
|
||||||
AND cv.itemPackingTypeFk = vItemPackingTypeFk;
|
AND cv.itemPackingTypeFk = vItemPackingTypeFk;
|
||||||
END WHILE;
|
|
||||||
|
|
||||||
-- Esto desaparecerá cuando tengamos la table cache.ticket
|
-- Esto desaparecerá cuando tengamos la table cache.ticket
|
||||||
|
|
||||||
CALL productionControl(vWarehouseFk, 0);
|
CALL productionControl(vWarehouseFk, 0);
|
||||||
|
|
||||||
ALTER TABLE tmp.productionBuffer
|
ALTER TABLE tmp.productionBuffer
|
||||||
ADD COLUMN liters INT,
|
ADD COLUMN liters INT,
|
||||||
ADD COLUMN height INT;
|
ADD COLUMN height INT;
|
||||||
|
|
||||||
-- Se obtiene nº de colección.
|
|
||||||
INSERT INTO collection
|
|
||||||
SET itemPackingTypeFk = vItemPackingTypeFk,
|
|
||||||
trainFk = vTrainFk,
|
|
||||||
wagons = vWagons,
|
|
||||||
warehouseFk = vWarehouseFk;
|
|
||||||
|
|
||||||
SELECT LAST_INSERT_ID() INTO vCollectionFk;
|
|
||||||
|
|
||||||
-- Los tickets de recogida en Algemesí sólo se sacan si están asignados.
|
-- Los tickets de recogida en Algemesí sólo se sacan si están asignados.
|
||||||
-- Los pedidos con riesgo no se sacan aunque se asignen.
|
-- Los pedidos con riesgo no se sacan aunque se asignen.
|
||||||
DELETE pb.*
|
|
||||||
|
DELETE pb
|
||||||
FROM tmp.productionBuffer pb
|
FROM tmp.productionBuffer pb
|
||||||
JOIN state s ON s.id = pb.state
|
JOIN state s ON s.id = pb.state
|
||||||
WHERE (pb.agency = 'REC_ALGEMESI'
|
WHERE (pb.agency = 'REC_ALGEMESI'
|
||||||
AND s.code <> 'PICKER_DESIGNED')
|
AND s.code <> 'PICKER_DESIGNED')
|
||||||
OR pb.problem LIKE '%RIESGO%';
|
OR pb.problem LIKE '%RIESGO%';
|
||||||
|
|
||||||
-- Comprobamos si hay tickets asignados. En ese caso, nos centramos
|
-- Si hay tickets asignados, nos centramos exclusivamente en esos tickets
|
||||||
-- exclusivamente en esos tickets y los sacamos independientemente
|
-- y los sacamos independientemente de problemas o tamaños
|
||||||
-- de problemas o tamaños
|
|
||||||
SELECT COUNT(*) INTO vHasAssignedTickets
|
SELECT EXISTS (
|
||||||
FROM tmp.productionBuffer pb
|
SELECT TRUE
|
||||||
JOIN state s ON s.id = pb.state
|
FROM tmp.productionBuffer pb
|
||||||
WHERE s.code = 'PICKER_DESIGNED'
|
JOIN state s ON s.id = pb.state
|
||||||
AND pb.workerCode = vWorkerCode;
|
WHERE s.code = 'PICKER_DESIGNED'
|
||||||
|
AND pb.workerCode = vWorkerCode
|
||||||
|
) INTO vHasAssignedTickets;
|
||||||
|
|
||||||
-- Se dejan en la tabla tmp.productionBuffer sólo aquellos tickets adecuados
|
-- Se dejan en la tabla tmp.productionBuffer sólo aquellos tickets adecuados
|
||||||
|
|
||||||
IF vHasAssignedTickets THEN
|
IF vHasAssignedTickets THEN
|
||||||
DELETE pb.*
|
DELETE pb
|
||||||
FROM tmp.productionBuffer pb
|
FROM tmp.productionBuffer pb
|
||||||
JOIN state s ON s.id = pb.state
|
JOIN state s ON s.id = pb.state
|
||||||
WHERE s.code <> 'PICKER_DESIGNED'
|
WHERE s.code <> 'PICKER_DESIGNED'
|
||||||
OR pb.workerCode <> vWorkerCode;
|
OR pb.workerCode <> vWorkerCode;
|
||||||
ELSE
|
ELSE
|
||||||
DELETE pb.*
|
DELETE pb
|
||||||
FROM tmp.productionBuffer pb
|
FROM tmp.productionBuffer pb
|
||||||
JOIN state s ON s.id = pb.state
|
JOIN state s ON s.id = pb.state
|
||||||
JOIN agencyMode am ON am.id = pb.agencyModeFk
|
JOIN agencyMode am ON am.id = pb.agencyModeFk
|
||||||
|
@ -193,72 +164,65 @@ BEGIN
|
||||||
OR (NOT pb.H AND pb.V > 0 AND vItemPackingTypeFk = 'H')
|
OR (NOT pb.H AND pb.V > 0 AND vItemPackingTypeFk = 'H')
|
||||||
OR (NOT pb.V AND vItemPackingTypeFk = 'V')
|
OR (NOT pb.V AND vItemPackingTypeFk = 'V')
|
||||||
OR (pc.isPreviousPreparationRequired AND pb.previousWithoutParking)
|
OR (pc.isPreviousPreparationRequired AND pb.previousWithoutParking)
|
||||||
OR LENGTH(pb.problem) > 0
|
OR LENGTH(pb.problem)
|
||||||
OR (pb.lines > vLinesLimit AND vLinesLimit IS NOT NULL)
|
OR pb.lines > vLinesLimit
|
||||||
OR (pb.m3 > vVolumeLimit AND vVolumeLimit IS NOT NULL)
|
OR pb.m3 > vVolumeLimit
|
||||||
OR ((sub.maxSize > vSizeLimit OR sub.maxSize IS NOT NULL) AND vSizeLimit IS NOT NULL);
|
OR sub.maxSize > vSizeLimit;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
-- Es importante que el primer ticket se coja en todos los casos
|
|
||||||
SELECT ticketFk,
|
|
||||||
HH,
|
|
||||||
mm,
|
|
||||||
`lines`,
|
|
||||||
m3
|
|
||||||
INTO vFirstTicketFk,
|
|
||||||
vHour,
|
|
||||||
vMinute,
|
|
||||||
vTicketLines,
|
|
||||||
vTicketVolume
|
|
||||||
FROM tmp.productionBuffer
|
|
||||||
ORDER BY HH,
|
|
||||||
mm,
|
|
||||||
productionOrder DESC,
|
|
||||||
m3 DESC,
|
|
||||||
agency,
|
|
||||||
zona,
|
|
||||||
routeFk,
|
|
||||||
ticketFk
|
|
||||||
LIMIT 1;
|
|
||||||
|
|
||||||
-- Hay que excluir aquellos que no tengan la misma hora de preparacion, si procede
|
-- Hay que excluir aquellos que no tengan la misma hora de preparacion, si procede
|
||||||
IF vHasUniqueCollectionTime THEN
|
IF vHasUniqueCollectionTime THEN
|
||||||
DELETE FROM tmp.productionBuffer
|
|
||||||
WHERE HH <> vHour
|
SELECT ticketFk INTO vFirstTicketFk
|
||||||
OR mm <> vMinute;
|
FROM tmp.productionBuffer
|
||||||
|
ORDER BY HH,
|
||||||
|
mm,
|
||||||
|
productionOrder DESC,
|
||||||
|
m3 DESC,
|
||||||
|
agency,
|
||||||
|
zona,
|
||||||
|
routeFk,
|
||||||
|
ticketFk
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
DELETE pb
|
||||||
|
FROM tmp.productionBuffer pb
|
||||||
|
JOIN tmp.productionBuffer pb2 ON pb2.ticketFk = vFirstTicketFk
|
||||||
|
AND (pb.HH <> pb2.HH OR pb.mm <> pb2.mm);
|
||||||
|
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SET vTicketFk = vFirstTicketFk;
|
OPEN vTickets;
|
||||||
SET @lines = 0;
|
l: LOOP
|
||||||
SET @volume = 0;
|
|
||||||
|
|
||||||
OPEN c1;
|
|
||||||
read_loop: LOOP
|
|
||||||
SET vDone = FALSE;
|
SET vDone = FALSE;
|
||||||
|
FETCH vTickets INTO vTicketFk, vTicketLines, vTicketVolume;
|
||||||
|
|
||||||
|
IF vDone THEN
|
||||||
|
LEAVE l;
|
||||||
|
END IF;
|
||||||
|
|
||||||
-- Buscamos un ticket que cumpla con los requisitos en el listado
|
-- Buscamos un ticket que cumpla con los requisitos en el listado
|
||||||
IF ((vTicketLines + @lines) <= vLinesLimit OR vLinesLimit IS NULL)
|
|
||||||
AND ((vTicketVolume + @volume) <= vVolumeLimit OR vVolumeLimit IS NULL) THEN
|
IF (vLinesLimit IS NULL OR (vTotalLines + vTicketLines) <= vLinesLimit)
|
||||||
|
AND (vVolumeLimit IS NULL OR (vTotalVolume + vTicketVolume) <= vVolumeLimit) THEN
|
||||||
|
|
||||||
CALL ticket_splitItemPackingType(vTicketFk, vItemPackingTypeFk);
|
CALL ticket_splitItemPackingType(vTicketFk, vItemPackingTypeFk);
|
||||||
DROP TEMPORARY TABLE tmp.ticketIPT;
|
DROP TEMPORARY TABLE tmp.ticketIPT;
|
||||||
|
|
||||||
|
SELECT COUNT(*), SUM(litros), MAX(i.`size`), SUM(sv.volume)
|
||||||
|
INTO vLines, vLiters, vHeight, vVolume
|
||||||
|
FROM saleVolume sv
|
||||||
|
JOIN sale s ON s.id = sv.saleFk
|
||||||
|
JOIN item i ON i.id = s.itemFk
|
||||||
|
WHERE sv.ticketFk = vTicketFk;
|
||||||
|
|
||||||
|
SET vTotalVolume = vTotalVolume + vVolume,
|
||||||
|
vTotalLines = vTotalLines + vLines;
|
||||||
|
|
||||||
UPDATE tmp.productionBuffer pb
|
UPDATE tmp.productionBuffer pb
|
||||||
JOIN (
|
SET pb.liters = vLiters,
|
||||||
SELECT SUM(litros) liters,
|
pb.`lines` = vLines,
|
||||||
@lines:= COUNT(*) + @lines,
|
pb.height = vHeight
|
||||||
COUNT(*) `lines`,
|
|
||||||
MAX(i.`size`) height,
|
|
||||||
@volume := SUM(sv.volume) + @volume,
|
|
||||||
SUM(sv.volume) volume
|
|
||||||
FROM saleVolume sv
|
|
||||||
JOIN sale s ON s.id = sv.saleFk
|
|
||||||
JOIN item i ON i.id = s.itemFk
|
|
||||||
WHERE sv.ticketFk = vTicketFk
|
|
||||||
) sub
|
|
||||||
SET pb.liters = sub.liters,
|
|
||||||
pb.`lines` = sub.`lines`,
|
|
||||||
pb.height = sub.height
|
|
||||||
WHERE pb.ticketFk = vTicketFk;
|
WHERE pb.ticketFk = vTicketFk;
|
||||||
|
|
||||||
UPDATE tTrain tt
|
UPDATE tTrain tt
|
||||||
|
@ -275,17 +239,13 @@ BEGIN
|
||||||
tt.height
|
tt.height
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
-- Si no le encuentra una balda adecuada, intentamos darle un carro entero si queda alguno libre
|
-- Si no le encuentra una balda, intentamos darle un carro entero libre
|
||||||
|
|
||||||
IF NOT (SELECT COUNT(*) FROM tTrain WHERE ticketFk) THEN
|
IF NOT (SELECT COUNT(*) FROM tTrain WHERE ticketFk) THEN
|
||||||
SELECT tt.wagon
|
SELECT wagon INTO vFreeWagonFk
|
||||||
INTO vFreeWagonFk
|
FROM tTrain
|
||||||
FROM tTrain tt
|
GROUP BY wagon
|
||||||
LEFT JOIN (
|
HAVING SUM(IFNULL(ticketFk, 0)) = 0
|
||||||
SELECT DISTINCT wagon
|
|
||||||
FROM tTrain
|
|
||||||
WHERE ticketFk IS NOT NULL
|
|
||||||
) nn ON nn.wagon = tt.wagon
|
|
||||||
WHERE nn.wagon IS NULL
|
|
||||||
ORDER BY wagon
|
ORDER BY wagon
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
|
|
||||||
|
@ -294,38 +254,35 @@ BEGIN
|
||||||
SET ticketFk = vFirstTicketFk
|
SET ticketFk = vFirstTicketFk
|
||||||
WHERE wagon = vFreeWagonFk;
|
WHERE wagon = vFreeWagonFk;
|
||||||
|
|
||||||
-- Se anulan el resto de carros libres para que sólo uno lleve un pedido excesivo
|
-- Se anulan el resto de carros libres,
|
||||||
DELETE tt.*
|
-- máximo un carro con pedido excesivo
|
||||||
FROM tTrain tt
|
|
||||||
LEFT JOIN (
|
|
||||||
SELECT DISTINCT wagon
|
|
||||||
FROM tTrain
|
|
||||||
WHERE ticketFk IS NOT NULL
|
|
||||||
) nn ON nn.wagon = tt.wagon
|
|
||||||
WHERE nn.wagon IS NULL;
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
FETCH c1 INTO vTicketFk, vTicketLines, vTicketVolume;
|
DELETE tt
|
||||||
IF vDone OR NOT (SELECT COUNT(*) FROM tTrain WHERE ticketFk IS NULL) THEN
|
FROM tTrain tt
|
||||||
LEAVE read_loop;
|
JOIN (SELECT wagon
|
||||||
END IF;
|
FROM tTrain
|
||||||
ELSE
|
GROUP BY wagon
|
||||||
FETCH c1 INTO vTicketFk, vTicketLines, vTicketVolume;
|
HAVING SUM(IFNULL(ticketFk, 0)) = 0
|
||||||
IF vDone THEN
|
) sub ON sub.wagon = tt.wagon;
|
||||||
LEAVE read_loop;
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
CLOSE c1;
|
CLOSE vTickets;
|
||||||
|
|
||||||
IF (SELECT COUNT(*) FROM tTrain WHERE ticketFk) THEN
|
IF (SELECT COUNT(*) FROM tTrain WHERE ticketFk) THEN
|
||||||
UPDATE collection c
|
-- Se obtiene nº de colección
|
||||||
JOIN state st ON st.code = 'ON_PREPARATION'
|
|
||||||
SET c.stateFk = st.id
|
INSERT INTO collection
|
||||||
WHERE c.id = vCollectionFk;
|
SET itemPackingTypeFk = vItemPackingTypeFk,
|
||||||
|
trainFk = vTrainFk,
|
||||||
|
wagons = vWagons,
|
||||||
|
warehouseFk = vWarehouseFk;
|
||||||
|
|
||||||
|
SELECT LAST_INSERT_ID() INTO vCollectionFk;
|
||||||
|
|
||||||
-- Asigna las bandejas
|
-- Asigna las bandejas
|
||||||
|
|
||||||
INSERT IGNORE INTO ticketCollection(ticketFk, collectionFk, `level`, wagon, liters)
|
INSERT IGNORE INTO ticketCollection(ticketFk, collectionFk, `level`, wagon, liters)
|
||||||
SELECT tt.ticketFk, vCollectionFk, tt.shelve, tt.wagon, tt.liters
|
SELECT tt.ticketFk, vCollectionFk, tt.shelve, tt.wagon, tt.liters
|
||||||
FROM tTrain tt
|
FROM tTrain tt
|
||||||
|
@ -333,37 +290,34 @@ BEGIN
|
||||||
ORDER BY tt.wagon, tt.shelve;
|
ORDER BY tt.wagon, tt.shelve;
|
||||||
|
|
||||||
-- Actualiza el estado de los tickets
|
-- Actualiza el estado de los tickets
|
||||||
CALL collection_setState(vCollectionFk, vStateFk);
|
|
||||||
|
CALL collection_setState(vCollectionFk, vStateCode);
|
||||||
|
|
||||||
-- Aviso para la preparacion previa
|
-- Aviso para la preparacion previa
|
||||||
|
|
||||||
INSERT INTO ticketDown(ticketFk, collectionFk)
|
INSERT INTO ticketDown(ticketFk, collectionFk)
|
||||||
SELECT tc.ticketFk, tc.collectionFk
|
SELECT tc.ticketFk, tc.collectionFk
|
||||||
FROM ticketCollection tc
|
FROM ticketCollection tc
|
||||||
WHERE tc.collectionFk = vCollectionFk;
|
WHERE tc.collectionFk = vCollectionFk;
|
||||||
|
|
||||||
CALL sales_mergeByCollection(vCollectionFk);
|
CALL collection_mergeSales(vCollectionFk);
|
||||||
|
|
||||||
UPDATE `collection` c
|
UPDATE `collection` c
|
||||||
JOIN (
|
JOIN(
|
||||||
SELECT COUNT(*) saleTotalCount,
|
SELECT COUNT(*) saleTotalCount,
|
||||||
SUM(s.isPicked <> 0) salePickedCount
|
SUM(s.isPicked <> 0) salePickedCount
|
||||||
FROM ticketCollection tc
|
FROM ticketCollection tc
|
||||||
JOIN sale s ON s.ticketFk = tc.ticketFk
|
JOIN sale s ON s.ticketFk = tc.ticketFk
|
||||||
WHERE tc.collectionFk = vCollectionFk
|
WHERE tc.collectionFk = vCollectionFk
|
||||||
AND s.quantity > 0
|
AND s.quantity > 0
|
||||||
) sub
|
)sub
|
||||||
SET c.saleTotalCount = sub.saleTotalCount,
|
SET c.saleTotalCount = sub.saleTotalCount,
|
||||||
c.salePickedCount = sub.salePickedCount
|
c.salePickedCount = sub.salePickedCount
|
||||||
WHERE c.id = vCollectionFk;
|
WHERE c.id = vCollectionFk;
|
||||||
|
|
||||||
ELSE
|
ELSE
|
||||||
DELETE FROM `collection`
|
SET vCollectionFk = NULL;
|
||||||
WHERE id = vCollectionFk;
|
|
||||||
SET vCollectionFk = NULL;
|
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
DO RELEASE_LOCK(vLockName);
|
|
||||||
|
|
||||||
DROP TEMPORARY TABLE
|
DROP TEMPORARY TABLE
|
||||||
tTrain,
|
tTrain,
|
||||||
tmp.productionBuffer;
|
tmp.productionBuffer;
|
||||||
|
|
|
@ -1,41 +0,0 @@
|
||||||
DELIMITER $$
|
|
||||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sales_merge`(vTicketFk INT)
|
|
||||||
BEGIN
|
|
||||||
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
||||||
BEGIN
|
|
||||||
ROLLBACK;
|
|
||||||
RESIGNAL;
|
|
||||||
END;
|
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tSalesToPreserve
|
|
||||||
(PRIMARY KEY (id))
|
|
||||||
ENGINE = MEMORY
|
|
||||||
SELECT s.id, s.itemFk, SUM(s.quantity) newQuantity
|
|
||||||
FROM sale s
|
|
||||||
JOIN item i ON i.id = s.itemFk
|
|
||||||
JOIN itemType it ON it.id = i.typeFk
|
|
||||||
WHERE s.ticketFk = vTicketFk
|
|
||||||
AND it.isMergeable
|
|
||||||
GROUP BY s.itemFk, s.price, s.discount;
|
|
||||||
|
|
||||||
START TRANSACTION;
|
|
||||||
|
|
||||||
UPDATE sale s
|
|
||||||
JOIN tSalesToPreserve stp ON stp.id = s.id
|
|
||||||
SET s.quantity = newQuantity
|
|
||||||
WHERE s.ticketFk = vTicketFk;
|
|
||||||
|
|
||||||
DELETE s.*
|
|
||||||
FROM sale s
|
|
||||||
LEFT JOIN tSalesToPreserve stp ON stp.id = s.id
|
|
||||||
JOIN item i ON i.id = s.itemFk
|
|
||||||
JOIN itemType it ON it.id = i.typeFk
|
|
||||||
WHERE s.ticketFk = vTicketFk
|
|
||||||
AND stp.id IS NULL
|
|
||||||
AND it.isMergeable;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
DROP TEMPORARY TABLE tSalesToPreserve;
|
|
||||||
END$$
|
|
||||||
DELIMITER ;
|
|
|
@ -26,7 +26,7 @@ BEGIN
|
||||||
LEAVE myLoop;
|
LEAVE myLoop;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
CALL vn.sales_merge(vTicketFk);
|
CALL ticket_mergeSales(vTicketFk);
|
||||||
|
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
DELIMITER $$
|
||||||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_mergeSales`(
|
||||||
|
vSelf INT
|
||||||
|
)
|
||||||
|
BEGIN
|
||||||
|
DECLARE vHasSalesToMerge BOOL;
|
||||||
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
||||||
|
BEGIN
|
||||||
|
ROLLBACK;
|
||||||
|
RESIGNAL;
|
||||||
|
END;
|
||||||
|
|
||||||
|
START TRANSACTION;
|
||||||
|
|
||||||
|
SELECT id INTO vSelf
|
||||||
|
FROM ticket
|
||||||
|
WHERE id = vSelf FOR UPDATE;
|
||||||
|
|
||||||
|
CREATE OR REPLACE TEMPORARY TABLE tSalesToPreserve
|
||||||
|
(PRIMARY KEY (id))
|
||||||
|
ENGINE = MEMORY
|
||||||
|
SELECT s.id, s.itemFk, SUM(s.quantity) newQuantity
|
||||||
|
FROM sale s
|
||||||
|
JOIN item i ON i.id = s.itemFk
|
||||||
|
JOIN itemType it ON it.id = i.typeFk
|
||||||
|
WHERE s.ticketFk = vSelf
|
||||||
|
AND it.isMergeable
|
||||||
|
GROUP BY s.itemFk, s.price, s.discount
|
||||||
|
HAVING COUNT(*) > 1;
|
||||||
|
|
||||||
|
SELECT COUNT(*) INTO vHasSalesToMerge
|
||||||
|
FROM tSalesToPreserve;
|
||||||
|
|
||||||
|
IF vHasSalesToMerge THEN
|
||||||
|
UPDATE sale s
|
||||||
|
JOIN tSalesToPreserve stp ON stp.id = s.id
|
||||||
|
SET s.quantity = newQuantity;
|
||||||
|
|
||||||
|
DELETE s
|
||||||
|
FROM sale s
|
||||||
|
JOIN tSalesToPreserve stp ON stp.itemFk = s.itemFk
|
||||||
|
WHERE s.ticketFk = vSelf
|
||||||
|
AND s.id <> stp.id;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
DROP TEMPORARY TABLE tSalesToPreserve;
|
||||||
|
END$$
|
||||||
|
DELIMITER ;
|
|
@ -8,17 +8,27 @@ BEGIN
|
||||||
*
|
*
|
||||||
* @param vSelf Id del ticket
|
* @param vSelf Id del ticket
|
||||||
*/
|
*/
|
||||||
|
DECLARE vTicketIsTooLittle BOOL;
|
||||||
|
|
||||||
|
SELECT ticket_isTooLittle(vSelf) INTO vTicketIsTooLittle;
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
|
||||||
(INDEX(ticketFk, isProblemCalcNeeded))
|
(INDEX(ticketFk, isProblemCalcNeeded))
|
||||||
ENGINE = MEMORY
|
ENGINE = MEMORY
|
||||||
SELECT vSelf ticketFk,
|
WITH ticketData AS (
|
||||||
ticket_isTooLittle(vSelf) hasProblem,
|
SELECT addressFk, DATE(shipped) dated
|
||||||
ticket_isProblemCalcNeeded(vSelf) isProblemCalcNeeded;
|
FROM vn.ticket
|
||||||
|
WHERE id = vSelf
|
||||||
|
)
|
||||||
|
SELECT t.id ticketFk,
|
||||||
|
vTicketIsTooLittle hasProblem,
|
||||||
|
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
|
||||||
|
FROM vn.ticket t
|
||||||
|
JOIN ticketData td ON td.addressFk = t.addressFk
|
||||||
|
WHERE t.shipped BETWEEN td.dated AND util.dayEnd(td.dated);
|
||||||
|
|
||||||
CALL ticket_setProblem('isTooLittle');
|
CALL ticket_setProblem('isTooLittle');
|
||||||
|
|
||||||
DROP TEMPORARY TABLE tmp.ticket;
|
DROP TEMPORARY TABLE tmp.ticket;
|
||||||
|
|
||||||
END$$
|
END$$
|
||||||
DELIMITER ;
|
DELIMITER ;
|
|
@ -5,139 +5,73 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_splitItemPac
|
||||||
)
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
* Clona y reparte las ventas de un ticket en funcion del tipo de empaquetado.
|
* Clona y reparte las líneas de ventas de un ticket en funcion del tipo de empaquetado.
|
||||||
* Respeta el id inicial para el tipo propuesto.
|
* Respeta el id de ticket inicial para el tipo de empaquetado propuesto.
|
||||||
*
|
*
|
||||||
* @param vSelf Id ticket
|
* @param vSelf Id ticket
|
||||||
* @param vOriginalItemPackingTypeFk Tipo para el que se reserva el número de ticket original
|
* @param vOriginalItemPackingTypeFk Tipo empaquetado al que se mantiene el ticket original
|
||||||
* @return table tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
|
||||||
*/
|
*/
|
||||||
DECLARE vItemPackingTypeFk VARCHAR(1) DEFAULT 'H';
|
|
||||||
DECLARE vNewTicketFk INT;
|
|
||||||
DECLARE vPackingTypesToSplit INT;
|
|
||||||
DECLARE vDone INT DEFAULT FALSE;
|
DECLARE vDone INT DEFAULT FALSE;
|
||||||
DECLARE vErrorNumber INT;
|
DECLARE vHasItemPackingType BOOL;
|
||||||
DECLARE vErrorMsg TEXT;
|
DECLARE vItemPackingTypeFk INT;
|
||||||
|
DECLARE vNewTicketFk INT;
|
||||||
|
|
||||||
DECLARE vSaleGroup CURSOR FOR
|
DECLARE vItemPackingTypes CURSOR FOR
|
||||||
SELECT itemPackingTypeFk
|
SELECT DISTINCT itemPackingTypeFk
|
||||||
FROM tSaleGroup
|
FROM tSalesToMove;
|
||||||
WHERE itemPackingTypeFk IS NOT NULL
|
|
||||||
ORDER BY (itemPackingTypeFk = vOriginalItemPackingTypeFk) DESC;
|
|
||||||
|
|
||||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
||||||
|
|
||||||
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
SELECT COUNT(*) INTO vHasItemPackingType
|
||||||
BEGIN
|
FROM ticket t
|
||||||
GET DIAGNOSTICS CONDITION 1
|
JOIN sale s ON s.ticketFk = t.id
|
||||||
vErrorNumber = MYSQL_ERRNO,
|
JOIN item i ON i.id = s.itemFk
|
||||||
vErrorMsg = MESSAGE_TEXT;
|
WHERE t.id = vSelf
|
||||||
|
AND i.itemPackingTypeFk = vOriginalItemPackingTypeFk;
|
||||||
|
|
||||||
CALL util.debugAdd('ticket_splitItemPackingType', JSON_OBJECT(
|
IF NOT vHasItemPackingType THEN
|
||||||
'errorNumber', vErrorNumber,
|
CALL util.throw('The ticket has not sales with the itemPackingType');
|
||||||
'errorMsg', vErrorMsg,
|
END IF;
|
||||||
'ticketFk', vSelf
|
|
||||||
)); -- Tmp
|
|
||||||
ROLLBACK;
|
|
||||||
RESIGNAL;
|
|
||||||
END;
|
|
||||||
|
|
||||||
START TRANSACTION;
|
CREATE OR REPLACE TEMPORARY TABLE tSalesToMove (
|
||||||
|
|
||||||
SELECT id
|
|
||||||
FROM sale
|
|
||||||
WHERE ticketFk = vSelf
|
|
||||||
AND NOT quantity
|
|
||||||
FOR UPDATE;
|
|
||||||
|
|
||||||
DELETE FROM sale
|
|
||||||
WHERE NOT quantity
|
|
||||||
AND ticketFk = vSelf;
|
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tSale
|
|
||||||
(PRIMARY KEY (id))
|
|
||||||
ENGINE = MEMORY
|
|
||||||
SELECT s.id, i.itemPackingTypeFk, IFNULL(sv.litros, 0) litros
|
|
||||||
FROM sale s
|
|
||||||
JOIN item i ON i.id = s.itemFk
|
|
||||||
LEFT JOIN saleVolume sv ON sv.saleFk = s.id
|
|
||||||
WHERE s.ticketFk = vSelf;
|
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tSaleGroup
|
|
||||||
ENGINE = MEMORY
|
|
||||||
SELECT itemPackingTypeFk, SUM(litros) totalLitros
|
|
||||||
FROM tSale
|
|
||||||
GROUP BY itemPackingTypeFk;
|
|
||||||
|
|
||||||
SELECT COUNT(*) INTO vPackingTypesToSplit
|
|
||||||
FROM tSaleGroup
|
|
||||||
WHERE itemPackingTypeFk IS NOT NULL;
|
|
||||||
|
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketIPT(
|
|
||||||
ticketFk INT,
|
ticketFk INT,
|
||||||
itemPackingTypeFk VARCHAR(1)
|
saleFk INT,
|
||||||
) ENGINE = MEMORY;
|
itemPackingTypeFk INT
|
||||||
|
) ENGINE=MEMORY;
|
||||||
|
|
||||||
CASE vPackingTypesToSplit
|
INSERT INTO tSalesToMove (saleFk, itemPackingTypeFk)
|
||||||
WHEN 0 THEN
|
SELECT s.id, i.itemPackingTypeFk
|
||||||
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
FROM ticket t
|
||||||
VALUES(vSelf, vItemPackingTypeFk);
|
JOIN sale s ON s.ticketFk = t.id
|
||||||
WHEN 1 THEN
|
JOIN item i ON i.id = s.itemFk
|
||||||
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
WHERE t.id = vSelf
|
||||||
SELECT vSelf, itemPackingTypeFk
|
AND i.itemPackingTypeFk <> vOriginalItemPackingTypeFk;
|
||||||
FROM tSaleGroup
|
|
||||||
WHERE itemPackingTypeFk IS NOT NULL;
|
|
||||||
ELSE
|
|
||||||
OPEN vSaleGroup;
|
|
||||||
FETCH vSaleGroup INTO vItemPackingTypeFk;
|
|
||||||
|
|
||||||
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
OPEN vItemPackingTypes;
|
||||||
VALUES(vSelf, vItemPackingTypeFk);
|
|
||||||
|
|
||||||
l: LOOP
|
l: LOOP
|
||||||
SET vDone = FALSE;
|
SET vDone = FALSE;
|
||||||
FETCH vSaleGroup INTO vItemPackingTypeFk;
|
FETCH vItemPackingTypes INTO vItemPackingTypeFk;
|
||||||
|
|
||||||
IF vDone THEN
|
IF vDone THEN
|
||||||
LEAVE l;
|
LEAVE l;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
CALL ticket_Clone(vSelf, vNewTicketFk);
|
CALL ticket_Clone(vSelf, vNewTicketFk);
|
||||||
|
|
||||||
INSERT INTO tmp.ticketIPT(ticketFk, itemPackingTypeFk)
|
UPDATE tSalesToMove
|
||||||
VALUES(vNewTicketFk, vItemPackingTypeFk);
|
SET ticketFk = vNewTicketFk
|
||||||
END LOOP;
|
WHERE itemPackingTypeFk = vItemPackingTypeFk;
|
||||||
|
|
||||||
CLOSE vSaleGroup;
|
END LOOP;
|
||||||
|
|
||||||
SELECT s.id
|
CLOSE vItemPackingTypes;
|
||||||
FROM sale s
|
|
||||||
JOIN tSale ts ON ts.id = s.id
|
|
||||||
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk
|
|
||||||
FOR UPDATE;
|
|
||||||
|
|
||||||
UPDATE sale s
|
UPDATE sale s
|
||||||
JOIN tSale ts ON ts.id = s.id
|
JOIN tSalesToMove stm ON stm.saleFk = s.id
|
||||||
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = ts.itemPackingTypeFk
|
SET s.ticketFk = stm.ticketFk
|
||||||
SET s.ticketFk = t.ticketFk;
|
WHERE stm.ticketFk;
|
||||||
|
|
||||||
SELECT itemPackingTypeFk INTO vItemPackingTypeFk
|
DROP TEMPORARY TABLE tSalesToMove;
|
||||||
FROM tSaleGroup sg
|
|
||||||
WHERE sg.itemPackingTypeFk IS NOT NULL
|
|
||||||
ORDER BY sg.itemPackingTypeFk
|
|
||||||
LIMIT 1;
|
|
||||||
|
|
||||||
UPDATE sale s
|
|
||||||
JOIN tSale ts ON ts.id = s.id
|
|
||||||
JOIN tmp.ticketIPT t ON t.itemPackingTypeFk = vItemPackingTypeFk
|
|
||||||
SET s.ticketFk = t.ticketFk
|
|
||||||
WHERE ts.itemPackingTypeFk IS NULL;
|
|
||||||
END CASE;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
DROP TEMPORARY TABLE
|
|
||||||
tSale,
|
|
||||||
tSaleGroup;
|
|
||||||
END$$
|
END$$
|
||||||
DELIMITER ;
|
DELIMITER ;
|
|
@ -0,0 +1,5 @@
|
||||||
|
ALTER TABLE vn.ormConfig
|
||||||
|
MODIFY COLUMN id INT NOT NULL,
|
||||||
|
DROP PRIMARY KEY,
|
||||||
|
ADD CONSTRAINT ormConfig_check CHECK (id = 1),
|
||||||
|
ADD PRIMARY KEY (id);
|
|
@ -56,6 +56,8 @@ export default class App {
|
||||||
}
|
}
|
||||||
|
|
||||||
getUrl(route, appName = 'lilium') {
|
getUrl(route, appName = 'lilium') {
|
||||||
|
const index = window.location.hash.indexOf(route.toLowerCase());
|
||||||
|
const newRoute = index < 0 ? route : window.location.hash.substring(index);
|
||||||
const env = process.env.NODE_ENV;
|
const env = process.env.NODE_ENV;
|
||||||
const filter = {
|
const filter = {
|
||||||
where: {and: [
|
where: {and: [
|
||||||
|
@ -67,7 +69,7 @@ export default class App {
|
||||||
return this.logger.$http.get('Urls/findOne', {filter})
|
return this.logger.$http.get('Urls/findOne', {filter})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res && res.data)
|
if (res && res.data)
|
||||||
return res.data.url + route;
|
return res.data.url + newRoute;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
this.showError('Direction not found');
|
this.showError('Direction not found');
|
||||||
|
|
|
@ -108,7 +108,7 @@ module.exports = Self => {
|
||||||
|
|
||||||
async function notifyStateChange(ctx, workerId, claim, newState) {
|
async function notifyStateChange(ctx, workerId, claim, newState) {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const url = await models.Url.getUrl('lilium');
|
const url = await models.Url.getUrl();
|
||||||
const $t = ctx.req.__;
|
const $t = ctx.req.__;
|
||||||
|
|
||||||
const message = $t(`Claim state has changed to`, {
|
const message = $t(`Claim state has changed to`, {
|
||||||
|
@ -122,7 +122,7 @@ module.exports = Self => {
|
||||||
|
|
||||||
async function notifyPickUp(ctx, workerId, claim) {
|
async function notifyPickUp(ctx, workerId, claim) {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const url = await models.Url.getUrl('lilium');
|
const url = await models.Url.getUrl();
|
||||||
const $t = ctx.req.__; // $translate
|
const $t = ctx.req.__; // $translate
|
||||||
|
|
||||||
const message = $t('Claim will be picked', {
|
const message = $t('Claim will be picked', {
|
||||||
|
|
|
@ -7,7 +7,7 @@ export default class Claim extends ModuleMain {
|
||||||
}
|
}
|
||||||
async $onInit() {
|
async $onInit() {
|
||||||
this.$state.go('home');
|
this.$state.go('home');
|
||||||
window.location.href = await this.vnApp.getUrl(`Claim/`);
|
window.location.href = await this.vnApp.getUrl(`claim/`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ describe('Ticket transferClient()', () => {
|
||||||
const ctx = beforeAll.getCtx();
|
const ctx = beforeAll.getCtx();
|
||||||
let options;
|
let options;
|
||||||
let tx;
|
let tx;
|
||||||
|
|
||||||
beforeEach(async() => {
|
beforeEach(async() => {
|
||||||
options = {transaction: tx};
|
options = {transaction: tx};
|
||||||
tx = await models.Ticket.beginTransaction({});
|
tx = await models.Ticket.beginTransaction({});
|
||||||
|
@ -21,21 +22,23 @@ describe('Ticket transferClient()', () => {
|
||||||
it('should throw an error as the ticket is not editable', async() => {
|
it('should throw an error as the ticket is not editable', async() => {
|
||||||
try {
|
try {
|
||||||
const ticketId = 4;
|
const ticketId = 4;
|
||||||
const clientId = 1;
|
|
||||||
await models.Ticket.transferClient(ctx, ticketId, clientId, options);
|
await models.Ticket.transferClient(ctx, ticketId, clientId, options);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e.message).toEqual(`This ticket is locked`);
|
expect(e.message).toEqual('This ticket is locked');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be assigned a different clientFk in the original ticket', async() => {
|
it('should be assigned a different clientFk and nickname in the original ticket', async() => {
|
||||||
await models.Ticket.transferClient(ctx, 2, clientId, options);
|
await models.Ticket.transferClient(ctx, 2, clientId, options);
|
||||||
const afterTransfer = await models.Ticket.findById(2, null, options);
|
const afterTransfer = await models.Ticket.findById(2, null, options);
|
||||||
|
const client = await models.Client.findById(clientId, {fields: ['defaultAddressFk']}, options);
|
||||||
|
const address = await models.Address.findById(client.defaultAddressFk, {fields: ['nickname']}, options);
|
||||||
|
|
||||||
expect(afterTransfer.clientFk).toEqual(clientId);
|
expect(afterTransfer.clientFk).toEqual(clientId);
|
||||||
|
expect(afterTransfer.nickname).toEqual(address.nickname);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be assigned a different clientFk in the original and refund ticket and claim', async() => {
|
it('should be assigned a different clientFk and nickname in the original and refund ticket and claim', async() => {
|
||||||
await models.Ticket.transferClient(ctx, originalTicketId, clientId, options);
|
await models.Ticket.transferClient(ctx, originalTicketId, clientId, options);
|
||||||
|
|
||||||
const [originalTicket, refundTicket] = await models.Ticket.find({
|
const [originalTicket, refundTicket] = await models.Ticket.find({
|
||||||
|
@ -46,8 +49,14 @@ describe('Ticket transferClient()', () => {
|
||||||
where: {ticketFk: originalTicketId}
|
where: {ticketFk: originalTicketId}
|
||||||
}, options);
|
}, options);
|
||||||
|
|
||||||
|
const client = await models.Client.findById(clientId, {fields: ['defaultAddressFk']}, options);
|
||||||
|
const address = await models.Address.findById(client.defaultAddressFk, {fields: ['nickname']}, options);
|
||||||
|
|
||||||
expect(originalTicket.clientFk).toEqual(clientId);
|
expect(originalTicket.clientFk).toEqual(clientId);
|
||||||
expect(refundTicket.clientFk).toEqual(clientId);
|
expect(refundTicket.clientFk).toEqual(clientId);
|
||||||
expect(claim.clientFk).toEqual(clientId);
|
expect(claim.clientFk).toEqual(clientId);
|
||||||
|
|
||||||
|
expect(originalTicket.nickname).toEqual(address.nickname);
|
||||||
|
expect(refundTicket.nickname).toEqual(address.nickname);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('transferClient', {
|
Self.remoteMethodCtx('transferClient', {
|
||||||
description: 'Transfering ticket to another client',
|
description: 'Transferring ticket to another client',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
|
@ -40,10 +40,13 @@ module.exports = Self => {
|
||||||
include: [{relation: 'refundTicket'}, {relation: 'originalTicket'}]
|
include: [{relation: 'refundTicket'}, {relation: 'originalTicket'}]
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
const {defaultAddressFk: addressFk} = await models.Client.findById(clientFk,
|
const client = await models.Client.findById(clientFk,
|
||||||
{fields: ['id', 'defaultAddressFk']}, myOptions);
|
{fields: ['id', 'defaultAddressFk']}, myOptions);
|
||||||
|
|
||||||
const attributes = {clientFk, addressFk};
|
const address = await models.Address.findById(client.defaultAddressFk,
|
||||||
|
{fields: ['id', 'nickname']}, myOptions);
|
||||||
|
|
||||||
|
const attributes = {clientFk, addressFk: client.defaultAddressFk, nickname: address.nickname};
|
||||||
|
|
||||||
const tickets = [];
|
const tickets = [];
|
||||||
const ticketIds = [];
|
const ticketIds = [];
|
||||||
|
|
Loading…
Reference in New Issue