2024-01-25 16:33:54 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_replaceItem`(vSaleFk INT, vNewItemFk INT, vQuantity INT)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Añade un nuevo articulo para sustituir a otro, y actualiza la memoria de sustituciones.
|
|
|
|
*
|
|
|
|
* @param vSaleFk id de la tabla sale
|
|
|
|
* @param vNewItemFk articulo sustituto
|
|
|
|
* @param vQuantity cantidad que se va a sustituir
|
|
|
|
*/
|
|
|
|
DECLARE vTicketFk INT;
|
|
|
|
DECLARE vItemFk INT;
|
|
|
|
DECLARE vWarehouseFk SMALLINT;
|
|
|
|
DECLARE vDate DATE;
|
|
|
|
DECLARE vGrouping INT;
|
2024-04-17 05:15:24 +00:00
|
|
|
DECLARE vGroupingMode VARCHAR(255);
|
2024-01-25 16:33:54 +00:00
|
|
|
DECLARE vPacking INT;
|
|
|
|
DECLARE vRoundQuantity INT DEFAULT 1;
|
|
|
|
DECLARE vLanded DATE;
|
|
|
|
DECLARE vAddressFk INT;
|
|
|
|
DECLARE vAgencyModeFk INT;
|
|
|
|
DECLARE vNewPrice DECIMAL(10,2);
|
|
|
|
DECLARE vOldPrice DECIMAL(10,2);
|
2024-01-30 09:52:33 +00:00
|
|
|
DECLARE vOption VARCHAR(255);
|
2024-01-25 16:33:54 +00:00
|
|
|
DECLARE vNewSaleFk INT;
|
|
|
|
DECLARE vFinalPrice DECIMAL(10,2);
|
|
|
|
|
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
|
|
|
|
|
|
|
SELECT s.ticketFk,
|
|
|
|
LEAST(s.quantity, vQuantity),
|
|
|
|
s.itemFk,
|
|
|
|
t.shipped,
|
|
|
|
t.warehouseFk,
|
|
|
|
t.landed,
|
|
|
|
t.addressFk,
|
|
|
|
t.agencyModeFk,
|
|
|
|
s.price
|
|
|
|
INTO vTicketFk,
|
|
|
|
vQuantity,
|
|
|
|
vItemFk,
|
|
|
|
vDate,
|
|
|
|
vWarehouseFk,
|
|
|
|
vLanded,
|
|
|
|
vAddressFk,
|
|
|
|
vAgencyModeFk,
|
|
|
|
vOldPrice
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
|
|
WHERE s.id = vSaleFk;
|
|
|
|
|
|
|
|
CALL buyUltimate(vWarehouseFk, vDate);
|
|
|
|
|
|
|
|
SELECT `grouping`, groupingMode, packing
|
2024-04-17 05:15:24 +00:00
|
|
|
INTO vGrouping,vGroupingMode,vPacking
|
2024-01-25 16:33:54 +00:00
|
|
|
FROM buy b
|
|
|
|
JOIN tmp.buyUltimate tmp ON b.id = tmp.buyFk
|
|
|
|
WHERE tmp.itemFk = vNewItemFk AND tmp.WarehouseFk = vWarehouseFk;
|
|
|
|
|
2024-04-17 05:15:24 +00:00
|
|
|
IF vGroupingMode = 'packing' AND vPacking > 0 THEN
|
2024-01-25 16:33:54 +00:00
|
|
|
SET vRoundQuantity = vPacking;
|
|
|
|
END IF;
|
2024-04-17 05:15:24 +00:00
|
|
|
IF vGroupingMode = 'grouping' AND vGrouping > 0 THEN
|
2024-01-25 16:33:54 +00:00
|
|
|
SET vRoundQuantity = vGrouping;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
CALL catalog_calcFromItem(
|
|
|
|
vLanded,
|
|
|
|
vAddressFk,
|
|
|
|
vAgencyModeFk,
|
|
|
|
vNewItemFk);
|
|
|
|
|
|
|
|
SELECT price INTO vNewPrice
|
2024-03-25 13:56:12 +00:00
|
|
|
FROM tmp.ticketComponentPrice
|
2024-03-26 15:27:49 +00:00
|
|
|
ORDER BY (vQuantity % `grouping`) ASC
|
|
|
|
LIMIT 1;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
|
|
|
IF vNewPrice > vOldPrice THEN
|
|
|
|
SET vFinalPrice = vOldPrice;
|
2024-01-30 09:03:21 +00:00
|
|
|
SET vOption = 'substitution';
|
2024-01-25 16:33:54 +00:00
|
|
|
ELSE
|
|
|
|
SET vFinalPrice = vNewPrice;
|
2024-01-30 09:03:21 +00:00
|
|
|
SET vOption = 'renewPrices';
|
2024-01-25 16:33:54 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
START TRANSACTION;
|
|
|
|
|
|
|
|
UPDATE sale
|
|
|
|
SET quantity = quantity - vQuantity
|
|
|
|
WHERE id = vSaleFk;
|
|
|
|
|
|
|
|
INSERT INTO vn.sale(ticketFk,
|
|
|
|
itemFk,
|
|
|
|
quantity,
|
|
|
|
concept,
|
|
|
|
price)
|
|
|
|
SELECT vTicketFk,
|
|
|
|
vNewItemFk,
|
2024-03-22 14:06:57 +00:00
|
|
|
CEIL(vQuantity / vRoundQuantity) * vRoundQuantity, CONCAT('+ ', i.name),
|
2024-01-25 16:33:54 +00:00
|
|
|
vFinalPrice
|
|
|
|
FROM vn.item i
|
|
|
|
WHERE id = vNewItemFk;
|
|
|
|
|
|
|
|
SELECT LAST_INSERT_ID() INTO vNewSaleFk;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
|
|
|
|
CREATE TEMPORARY TABLE tmp.sale
|
|
|
|
(PRIMARY KEY (saleFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT id saleFk, vWarehouseFk warehouseFk
|
|
|
|
FROM sale s WHERE id = vNewSaleFk;
|
|
|
|
|
|
|
|
CALL ticketComponentUpdateSale(vOption);
|
|
|
|
CALL catalog_componentPurge();
|
|
|
|
|
|
|
|
INSERT INTO itemProposal(itemFk, mateFk, counter)
|
|
|
|
VALUES(vItemFk, vNewItemFk, 1)
|
|
|
|
ON DUPLICATE KEY UPDATE counter = counter + 1;
|
|
|
|
|
|
|
|
COMMIT;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|