Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 7283-itemSectionsMigration
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2024-08-02 15:38:49 +02:00
commit 09783ae576
2 changed files with 215 additions and 201 deletions

View File

@ -1,59 +1,62 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`(vSelf INT, vUserId INT) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_confirmWithUser`(
vSelf INT,
vUserFk INT
)
BEGIN BEGIN
/** /**
* Confirms an order, creating each of its tickets on the corresponding * Confirms an order, creating each of its tickets
* date, store and user. * on the corresponding date, store and user.
* *
* @param vSelf The order identifier * @param vSelf The order identifier
* @param vUser The user identifier * @param vUser The user identifier
*/ */
DECLARE vOk BOOL; DECLARE vHasRows BOOL;
DECLARE vDone BOOL DEFAULT FALSE; DECLARE vDone BOOL;
DECLARE vWarehouse INT; DECLARE vWarehouseFk INT;
DECLARE vShipment DATE; DECLARE vShipment DATE;
DECLARE vTicket INT; DECLARE vShipmentDayEnd DATETIME;
DECLARE vTicketFk INT;
DECLARE vNotes VARCHAR(255); DECLARE vNotes VARCHAR(255);
DECLARE vItem INT; DECLARE vItemFk INT;
DECLARE vConcept VARCHAR(30); DECLARE vConcept VARCHAR(30);
DECLARE vAmount INT; DECLARE vAmount INT;
DECLARE vAvailable INT;
DECLARE vPrice DECIMAL(10,2); DECLARE vPrice DECIMAL(10,2);
DECLARE vSale INT; DECLARE vSaleFk INT;
DECLARE vRate INT; DECLARE vRowFk INT;
DECLARE vRowId INT;
DECLARE vPriceFixed DECIMAL(10,2); DECLARE vPriceFixed DECIMAL(10,2);
DECLARE vDelivery DATE; DECLARE vLanded DATE;
DECLARE vAddress INT; DECLARE vAddressFk INT;
DECLARE vIsConfirmed BOOL; DECLARE vClientFk INT;
DECLARE vClientId INT; DECLARE vCompanyFk INT;
DECLARE vCompanyId INT; DECLARE vAgencyModeFk INT;
DECLARE vAgencyModeId INT; DECLARE vCalcFk INT;
DECLARE TICKET_FREE INT DEFAULT 2;
DECLARE vCalc INT;
DECLARE vIsLogifloraItem BOOL;
DECLARE vOldQuantity INT;
DECLARE vNewQuantity INT;
DECLARE vIsTaxDataChecked BOOL; DECLARE vIsTaxDataChecked BOOL;
DECLARE cDates CURSOR FOR DECLARE vDates CURSOR FOR
SELECT zgs.shipped, r.warehouse_id SELECT zgs.shipped, r.warehouseFk
FROM `order` o FROM `order` o
JOIN order_row r ON r.order_id = o.id JOIN orderRow r ON r.orderFk = o.id
LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouse_id LEFT JOIN tmp.zoneGetShipped zgs ON zgs.warehouseFk = r.warehouseFk
WHERE o.id = vSelf AND r.amount != 0 WHERE o.id = vSelf
GROUP BY r.warehouse_id; AND r.amount
GROUP BY r.warehouseFk;
DECLARE cRows CURSOR FOR DECLARE vRows CURSOR FOR
SELECT r.id, r.item_id, i.name, r.amount, r.price, r.rate, i.isFloramondo SELECT r.id,
FROM order_row r r.itemFk,
JOIN vn.item i ON i.id = r.item_id i.name,
WHERE r.amount != 0 r.amount,
AND r.warehouse_id = vWarehouse r.price
AND r.order_id = vSelf FROM orderRow r
JOIN vn.item i ON i.id = r.itemFk
WHERE r.amount
AND r.warehouseFk = vWarehouseFk
AND r.orderFk = vSelf
ORDER BY r.rate DESC; ORDER BY r.rate DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
SET vDone = TRUE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN BEGIN
@ -62,26 +65,36 @@ BEGIN
END; END;
-- Carga los datos del pedido -- Carga los datos del pedido
SELECT o.date_send, o.address_id, o.note, a.clientFk, SELECT o.date_send,
o.company_id, o.agency_id, c.isTaxDataChecked o.address_id,
INTO vDelivery, vAddress, vNotes, vClientId, o.note,
vCompanyId, vAgencyModeId, vIsTaxDataChecked a.clientFk,
FROM hedera.`order` o o.company_id,
o.agency_id,
c.isTaxDataChecked
INTO vLanded,
vAddressFk,
vNotes,
vClientFk,
vCompanyFk,
vAgencyModeFk,
vIsTaxDataChecked
FROM `order` o
JOIN vn.address a ON a.id = o.address_id JOIN vn.address a ON a.id = o.address_id
JOIN vn.client c ON c.id = a.clientFk JOIN vn.client c ON c.id = a.clientFk
WHERE o.id = vSelf; WHERE o.id = vSelf;
-- Verifica si el cliente tiene los datos comprobados -- Verifica si el cliente tiene los datos comprobados
IF NOT vIsTaxDataChecked THEN IF NOT vIsTaxDataChecked THEN
CALL util.throw ('clientNotVerified'); CALL util.throw('clientNotVerified');
END IF; END IF;
-- Carga las fechas de salida de cada almacen -- Carga las fechas de salida de cada almacen
CALL vn.zone_getShipped (vDelivery, vAddress, vAgencyModeId, FALSE); CALL vn.zone_getShipped(vLanded, vAddressFk, vAgencyModeFk, FALSE);
-- Trabajador que realiza la accion -- Trabajador que realiza la accion
IF vUserId IS NULL THEN IF vUserFk IS NULL THEN
SELECT employeeFk INTO vUserId FROM orderConfig; SELECT employeeFk INTO vUserFk FROM orderConfig;
END IF; END IF;
START TRANSACTION; START TRANSACTION;
@ -89,207 +102,188 @@ BEGIN
CALL order_checkEditable(vSelf); CALL order_checkEditable(vSelf);
-- Check order is not empty -- Check order is not empty
SELECT COUNT(*) > 0 INTO vHasRows
FROM orderRow
WHERE orderFk = vSelf
AND amount > 0;
SELECT COUNT(*) > 0 INTO vOk IF NOT vHasRows THEN
FROM order_row WHERE order_id = vSelf AND amount > 0; CALL util.throw('ORDER_EMPTY');
IF NOT vOk THEN
CALL util.throw ('ORDER_EMPTY');
END IF; END IF;
-- Crea los tickets del pedido -- Crea los tickets del pedido
OPEN vDates;
OPEN cDates; lDates: LOOP
SET vTicketFk = NULL;
lDates:
LOOP
SET vTicket = NULL;
SET vDone = FALSE; SET vDone = FALSE;
FETCH cDates INTO vShipment, vWarehouse; FETCH vDates INTO vShipment, vWarehouseFk;
IF vDone THEN IF vDone THEN
LEAVE lDates; LEAVE lDates;
END IF; END IF;
-- Busca un ticket existente que coincida con los parametros SET vShipmentDayEnd = util.dayEnd(vShipment);
WITH tPrevia AS
(SELECT DISTINCT s.ticketFk -- Busca un ticket libre disponible
WITH tPrevia AS (
SELECT DISTINCT s.ticketFk
FROM vn.sale s FROM vn.sale s
JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id JOIN vn.saleGroupDetail sgd ON sgd.saleFk = s.id
JOIN vn.ticket t ON t.id = s.ticketFk JOIN vn.ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN vShipment AND util.dayend(vShipment) WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd
) )
SELECT t.id INTO vTicket SELECT t.id INTO vTicketFk
FROM vn.ticket t FROM vn.ticket t
JOIN vn.alertLevel al ON al.code = 'FREE' JOIN vn.alertLevel al ON al.code = 'FREE'
LEFT JOIN tPrevia tp ON tp.ticketFk = t.id LEFT JOIN tPrevia tp ON tp.ticketFk = t.id
LEFT JOIN vn.ticketState tls on tls.ticketFk = t.id LEFT JOIN vn.ticketState tls ON tls.ticketFk = t.id
JOIN hedera.`order` o JOIN hedera.`order` o ON o.address_id = t.addressFk
ON o.address_id = t.addressFk AND t.shipped BETWEEN vShipment AND vShipmentDayEnd
AND vWarehouse = t.warehouseFk AND t.warehouseFk = vWarehouseFk
AND o.date_send = t.landed AND o.date_send = t.landed
AND DATE(t.shipped) = vShipment
WHERE o.id = vSelf WHERE o.id = vSelf
AND t.refFk IS NULL AND t.refFk IS NULL
AND tp.ticketFk IS NULL AND tp.ticketFk IS NULL
AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id) AND (tls.alertLevel IS NULL OR tls.alertLevel = al.id)
LIMIT 1; LIMIT 1;
-- Comprobamos si hay un ticket de previa disponible
IF vTicketFk IS NULL THEN
WITH tItemPackingTypeOrder AS (
SELECT GROUP_CONCAT(
DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
) distinctItemPackingTypes,
o.address_id
FROM vn.item i
JOIN hedera.orderRow oro ON oro.itemFk = i.id
JOIN hedera.`order` o ON o.id = oro.orderFk
WHERE oro.orderFk = vSelf
),
tItemPackingTypeTicket AS (
SELECT t.id,
GROUP_CONCAT(
DISTINCT i.itemPackingTypeFk ORDER BY i.itemPackingTypeFk
) distinctItemPackingTypes
FROM vn.ticket t
JOIN vn.ticketState tls ON tls.ticketFk = t.id
JOIN vn.alertLevel al ON al.id = tls.alertLevel
JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn.item i ON i.id = s.itemFk
JOIN tItemPackingTypeOrder ipto
WHERE t.shipped BETWEEN vShipment AND vShipmentDayEnd
AND t.refFk IS NULL
AND t.warehouseFk = vWarehouseFk
AND t.addressFk = ipto.address_id
AND al.code = 'ON_PREVIOUS'
GROUP BY t.id
)
SELECT iptt.id INTO vTicketFk
FROM tItemPackingTypeTicket iptt
JOIN tItemPackingTypeOrder ipto
WHERE INSTR(iptt.distinctItemPackingTypes, ipto.distinctItemPackingTypes)
LIMIT 1;
END IF;
-- Crea el ticket en el caso de no existir uno adecuado -- Crea el ticket en el caso de no existir uno adecuado
IF vTicket IS NULL IF vTicketFk IS NULL THEN
THEN
SET vShipment = IFNULL(vShipment, util.VN_CURDATE()); SET vShipment = IFNULL(vShipment, util.VN_CURDATE());
CALL vn.ticket_add( CALL vn.ticket_add(
vClientId, vClientFk,
vShipment, vShipment,
vWarehouse, vWarehouseFk,
vCompanyId, vCompanyFk,
vAddress, vAddressFk,
vAgencyModeId, vAgencyModeFk,
NULL, NULL,
vDelivery, vLanded,
vUserId, vUserFk,
TRUE, TRUE,
vTicket vTicketFk
); );
ELSE ELSE
INSERT INTO vn.ticketTracking INSERT INTO vn.ticketTracking
SET ticketFk = vTicket, SET ticketFk = vTicketFk,
userFk = vUserId, userFk = vUserFk,
stateFk = TICKET_FREE; stateFk = (SELECT id FROM vn.state WHERE code = 'FREE');
END IF; END IF;
INSERT IGNORE INTO vn.orderTicket INSERT IGNORE INTO vn.orderTicket
SET orderFk = vSelf, SET orderFk = vSelf,
ticketFk = vTicket; ticketFk = vTicketFk;
-- Añade las notas -- Añade las notas
IF vNotes IS NOT NULL AND vNotes <> '' THEN
IF vNotes IS NOT NULL AND vNotes != '' INSERT INTO vn.ticketObservation
THEN SET ticketFk = vTicketFk,
INSERT INTO vn.ticketObservation SET observationTypeFk = (SELECT id FROM vn.observationType WHERE code = 'salesPerson'),
ticketFk = vTicket,
observationTypeFk = 4 /* salesperson */ ,
`description` = vNotes `description` = vNotes
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
`description` = CONCAT(VALUES(`description`),'. ', `description`); `description` = CONCAT(VALUES(`description`),'. ', `description`);
END IF; END IF;
-- Añade los movimientos y sus componentes -- Añade los movimientos y sus componentes
OPEN vRows;
OPEN cRows;
lRows: LOOP lRows: LOOP
SET vSaleFk = NULL;
SET vDone = FALSE; SET vDone = FALSE;
FETCH cRows INTO vRowId, vItem, vConcept, vAmount, vPrice, vRate, vIsLogifloraItem; FETCH vRows INTO vRowFk, vItemFk, vConcept, vAmount, vPrice;
IF vDone THEN IF vDone THEN
LEAVE lRows; LEAVE lRows;
END IF; END IF;
SET vSale = NULL; SELECT s.id INTO vSaleFk
SELECT s.id, s.quantity INTO vSale, vOldQuantity
FROM vn.sale s FROM vn.sale s
WHERE ticketFk = vTicket WHERE ticketFk = vTicketFk
AND price = vPrice AND price = vPrice
AND itemFk = vItem AND itemFk = vItemFk
AND discount = 0 AND discount = 0
LIMIT 1; LIMIT 1;
IF vSale THEN IF vSaleFk THEN
UPDATE vn.sale UPDATE vn.sale
SET quantity = quantity + vAmount, SET quantity = quantity + vAmount,
originalQuantity = quantity originalQuantity = quantity
WHERE id = vSale; WHERE id = vSaleFk;
SELECT s.quantity INTO vNewQuantity
FROM vn.sale s
WHERE id = vSale;
ELSE ELSE
-- Obtiene el coste -- Obtiene el coste
SELECT SUM(rc.`price`) valueSum INTO vPriceFixed SELECT SUM(rc.`price`) valueSum INTO vPriceFixed
FROM orderRowComponent rc FROM orderRowComponent rc
JOIN vn.component c ON c.id = rc.componentFk JOIN vn.component c ON c.id = rc.componentFk
JOIN vn.componentType ct ON ct.id = c.typeFk AND ct.isBase JOIN vn.componentType ct ON ct.id = c.typeFk
WHERE rc.rowFk = vRowId; AND ct.isBase
WHERE rc.rowFk = vRowFk;
INSERT INTO vn.sale INSERT INTO vn.sale
SET itemFk = vItem, SET itemFk = vItemFk,
ticketFk = vTicket, ticketFk = vTicketFk,
concept = vConcept, concept = vConcept,
quantity = vAmount, quantity = vAmount,
price = vPrice, price = vPrice,
priceFixed = vPriceFixed, priceFixed = vPriceFixed,
isPriceFixed = TRUE; isPriceFixed = TRUE;
SET vSale = LAST_INSERT_ID(); SET vSaleFk = LAST_INSERT_ID();
INSERT INTO vn.saleComponent INSERT INTO vn.saleComponent (saleFk, componentFk, `value`)
(saleFk, componentFk, `value`) SELECT vSaleFk, rc.componentFk, rc.price
SELECT vSale, rc.componentFk, rc.price
FROM orderRowComponent rc FROM orderRowComponent rc
JOIN vn.component c ON c.id = rc.componentFk JOIN vn.component c ON c.id = rc.componentFk
WHERE rc.rowFk = vRowId WHERE rc.rowFk = vRowFk
GROUP BY vSale, rc.componentFk; GROUP BY vSaleFk, rc.componentFk;
END IF; END IF;
UPDATE order_row SET Id_Movimiento = vSale UPDATE orderRow
WHERE id = vRowId; SET saleFk = vSaleFk
WHERE id = vRowFk;
-- Inserta en putOrder si la compra es de Floramondo
IF vIsLogifloraItem THEN
CALL cache.availableNoRaids_refresh(vCalc,FALSE,vWarehouse,vShipment);
SET @available := 0;
SELECT GREATEST(0,available) INTO @available
FROM cache.availableNoRaids
WHERE calc_id = vCalc
AND item_id = vItem;
UPDATE cache.availableNoRaids
SET available = GREATEST(0,available - vAmount)
WHERE item_id = vItem
AND calc_id = vCalc;
INSERT INTO edi.putOrder (
deliveryInformationID,
supplyResponseId,
quantity ,
EndUserPartyId,
EndUserPartyGLN,
FHAdminNumber,
saleFk
)
SELECT di.ID,
i.supplyResponseFk,
CEIL((vAmount - @available)/ sr.NumberOfItemsPerCask),
o.address_id ,
vClientId,
IFNULL(ca.fhAdminNumber, fhc.defaultAdminNumber),
vSale
FROM edi.deliveryInformation di
JOIN vn.item i ON i.supplyResponseFk = di.supplyResponseID
JOIN edi.supplyResponse sr ON sr.ID = i.supplyResponseFk
LEFT JOIN edi.clientFHAdminNumber ca ON ca.clientFk = vClientId
JOIN edi.floraHollandConfig fhc
JOIN hedera.`order` o ON o.id = vSelf
WHERE i.id = vItem
AND di.LatestOrderDateTime > util.VN_NOW()
AND vAmount > @available
LIMIT 1;
END IF;
END LOOP; END LOOP;
CLOSE vRows;
CLOSE cRows;
END LOOP; END LOOP;
CLOSE vDates;
CLOSE cDates; UPDATE `order`
SET confirmed = TRUE,
UPDATE `order` SET confirmed = TRUE, confirm_date = util.VN_NOW() confirm_date = util.VN_NOW()
WHERE id = vSelf; WHERE id = vSelf;
COMMIT; COMMIT;

View File

@ -1,37 +1,40 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getMinacum`(IN vWarehouseFk TINYINT, IN vDatedFrom DATETIME, IN vRange INT, IN vItemFk INT) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getMinacum`(
vWarehouseFk TINYINT,
vDated DATE,
vRange INT,
vItemFk INT
)
BEGIN BEGIN
/** /**
* Cálculo del mínimo acumulado, para un item/almacén especificado, en caso de * Cálculo del mínimo acumulado, para un item/almacén
* NULL para todo. * especificado, en caso de NULL para todos.
* *
* @param vWarehouseFk -> warehouseFk * @param vWarehouseFk Id warehouse
* @param vDatedFrom -> fecha inicio * @param vDated Fecha inicio
* @param vRange -> número de días a considerar * @param vRange Número de días a considerar
* @param vItemFk -> Identificador de item * @param vItemFk Id de artículo
* @return tmp.itemMinacum * @return tmp.itemMinacum
*/ */
DECLARE vDatedTo DATETIME; DECLARE vDatedTo DATETIME DEFAULT util.dayEnd(vDated + INTERVAL vRange DAY);
SET vDatedFrom = TIMESTAMP(DATE(vDatedFrom), '00:00:00'); CREATE OR REPLACE TEMPORARY TABLE tmp.itemCalc
SET vDatedTo = TIMESTAMP(TIMESTAMPADD(DAY, vRange, vDatedFrom), '23:59:59');
DROP TEMPORARY TABLE IF EXISTS tmp.itemCalc;
CREATE TEMPORARY TABLE tmp.itemCalc
(INDEX (itemFk, warehouseFk)) (INDEX (itemFk, warehouseFk))
ENGINE = MEMORY
SELECT sub.itemFk, SELECT sub.itemFk,
sub.dated, sub.dated,
CAST(SUM(sub.quantity) AS SIGNED) quantity, CAST(SUM(sub.quantity) AS SIGNED) quantity,
sub.warehouseFk sub.warehouseFk
FROM (SELECT s.itemFk, FROM (
SELECT s.itemFk,
DATE(t.shipped) dated, DATE(t.shipped) dated,
-s.quantity quantity, -s.quantity quantity,
t.warehouseFk t.warehouseFk
FROM sale s FROM sale s
JOIN ticket t ON t.id = s.ticketFk JOIN ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN vDatedFrom AND vDatedTo WHERE t.shipped BETWEEN vDated AND vDatedTo
AND t.warehouseFk AND t.warehouseFk
AND s.quantity != 0 AND s.quantity <> 0
AND (vItemFk IS NULL OR s.itemFk = vItemFk) AND (vItemFk IS NULL OR s.itemFk = vItemFk)
AND (vWarehouseFk IS NULL OR t.warehouseFk = vWarehouseFk) AND (vWarehouseFk IS NULL OR t.warehouseFk = vWarehouseFk)
UNION ALL UNION ALL
@ -42,10 +45,10 @@ BEGIN
FROM buy b FROM buy b
JOIN entry e ON e.id = b.entryFk JOIN entry e ON e.id = b.entryFk
JOIN travel t ON t.id = e.travelFk JOIN travel t ON t.id = e.travelFk
WHERE t.landed BETWEEN vDatedFrom AND vDatedTo WHERE t.landed BETWEEN vDated AND vDatedTo
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk) AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
AND !e.isExcludedFromAvailable AND NOT e.isExcludedFromAvailable
AND b.quantity != 0 AND b.quantity <> 0
AND (vItemFk IS NULL OR b.itemFk = vItemFk) AND (vItemFk IS NULL OR b.itemFk = vItemFk)
UNION ALL UNION ALL
SELECT b.itemFk, SELECT b.itemFk,
@ -55,28 +58,45 @@ BEGIN
FROM buy b FROM buy b
JOIN entry e ON e.id = b.entryFk JOIN entry e ON e.id = b.entryFk
JOIN travel t ON t.id = e.travelFk JOIN travel t ON t.id = e.travelFk
WHERE t.shipped BETWEEN vDatedFrom AND vDatedTo WHERE t.shipped BETWEEN vDated AND vDatedTo
AND (vWarehouseFk IS NULL OR t.warehouseOutFk = vWarehouseFk) AND (vWarehouseFk IS NULL OR t.warehouseOutFk = vWarehouseFk)
AND !e.isExcludedFromAvailable AND NOT e.isExcludedFromAvailable
AND b.quantity != 0 AND b.quantity <> 0
AND (vItemFk IS NULL OR b.itemFk = vItemFk) AND (vItemFk IS NULL OR b.itemFk = vItemFk)
AND !e.isRaid AND NOT e.isRaid
UNION ALL
SELECT r.itemFk,
r.shipment,
-r.amount,
r.warehouseFk
FROM hedera.orderRow r
JOIN hedera.`order` o ON o.id = r.orderFk
JOIN client c ON c.id = o.customer_id
WHERE r.shipment BETWEEN vDated AND vDatedTo
AND (vWarehouseFk IS NULL OR r.warehouseFk = vWarehouseFk)
AND r.created >= (
SELECT util.VN_NOW() - INTERVAL TIME_TO_SEC(reserveTime) SECOND
FROM hedera.orderConfig
)
AND NOT o.confirmed
AND (vItemFk IS NULL OR r.itemFk = vItemFk)
AND r.amount <> 0
) sub ) sub
GROUP BY sub.itemFk, sub.warehouseFk, sub.dated; GROUP BY sub.itemFk, sub.warehouseFk, sub.dated;
CALL item_getAtp(vDatedFrom); CALL item_getAtp(vDated);
DROP TEMPORARY TABLE tmp.itemCalc;
DROP TEMPORARY TABLE IF EXISTS tmp.itemMinacum; CREATE OR REPLACE TEMPORARY TABLE tmp.itemMinacum
CREATE TEMPORARY TABLE tmp.itemMinacum
(INDEX(itemFk)) (INDEX(itemFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT i.itemFk, SELECT itemFk,
i.warehouseFk, warehouseFk,
i.quantity amount quantity amount
FROM tmp.itemAtp i FROM tmp.itemAtp
HAVING amount != 0; WHERE quantity <> 0;
DROP TEMPORARY TABLE tmp.itemAtp; DROP TEMPORARY TABLE
tmp.itemAtp,
tmp.itemCalc;
END$$ END$$
DELIMITER ; DELIMITER ;