salix/db/routines/vn/procedures/ticketComponentUpdateSale.sql

110 lines
3.8 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-11-27 17:19:57 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticketComponentUpdateSale`(
vCode VARCHAR(25)
)
BEGIN
/**
* A partir de la tabla tmp.sale, crea los Movimientos_componentes
2024-11-27 17:19:57 +00:00
* y modifica el campo vn.sale.price
*
2024-11-27 17:19:57 +00:00
* @param vCode integer tipo de actualizacion
* @param table tmp.sale tabla memory con el campo saleFk, warehouseFk
**/
DECLARE vComponentFk INT;
2024-11-27 17:19:57 +00:00
IF vCode <> 'renewPrices' THEN
2024-01-25 16:33:54 +00:00
SELECT id INTO vComponentFk FROM component WHERE `code` = vCode;
END IF;
2024-11-27 17:19:57 +00:00
DELETE sc
FROM tmp.sale tmps
2024-01-25 16:33:54 +00:00
JOIN saleComponent sc ON sc.saleFk = tmps.saleFk
JOIN `component` c ON c.id = sc.componentFk
WHERE c.isRenewable;
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, tc.componentFk, tc.cost
2024-11-27 17:19:57 +00:00
FROM sale s
2024-01-25 16:33:54 +00:00
JOIN tmp.sale tmps ON tmps.saleFk = s.id
JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk AND tc.warehouseFk = tmps.warehouseFk
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
AND sc.componentFk = tc.componentFk
LEFT JOIN `component` c ON c.id = tc.componentFk
WHERE IF(sc.componentFk IS NULL AND NOT c.isRenewable, FALSE, TRUE);
-- Añadir componente venta por paquete
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT t.id, t.componentFk, t.cost
FROM (
SELECT s.id, tc.componentFk, tc.cost, MOD(s.quantity, b.packing) as resto
FROM vn.sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
JOIN cache.last_buy lb ON lb.item_id = s.itemFk AND tmps.warehouseFk = lb.warehouse_id
JOIN vn.buy b ON b.id = buy_id
JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk AND tc.warehouseFk = tmps.warehouseFk
2024-11-27 17:19:57 +00:00
JOIN `component` c ON c.id = tc.componentFk AND c.code = 'salePerPackage'
2024-01-25 16:33:54 +00:00
LEFT JOIN (
SELECT s.id
FROM vn.sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk AND tc.warehouseFk = tmps.warehouseFk
JOIN saleComponent sc ON sc.saleFk = s.id AND sc.componentFk = tc.componentFk
JOIN `component` c ON c.id = sc.componentFk AND c.code = 'lastUnitsDiscount'
) tp ON tp.id = s.id
WHERE tp.id IS NULL
HAVING resto <> 0) t;
2025-01-22 14:05:45 +00:00
IF vCode = 'renewPrices' THEN
SET vComponentFk = 21;
2024-01-25 16:33:54 +00:00
END IF;
2025-01-22 14:05:45 +00:00
UPDATE sale s
JOIN ticket t ON t.id = s.ticketFk
JOIN item i on i.id = s.itemFk
JOIN itemType it on it.id = i.typeFk
JOIN (SELECT SUM(sc.value) sumValue, sc.saleFk
FROM saleComponent sc
JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
GROUP BY sc.saleFk) sc ON sc.saleFk = s.id
SET s.price = IF(vCode <> 'renewPrices', sumValue / ((100 - s.discount) / 100), s.price),
s.foreignPrice = currency_getRate(t.currencyFk, NULL) * (sumValue / ((100 - s.discount) / 100))
WHERE it.code <> 'PRT' ;
REPLACE INTO saleComponent(saleFk, componentFk, value)
SELECT s.id, vComponentFk, ROUND((s.price * (100 - s.discount) / 100) - SUM(sc.value), 3) dif
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
LEFT JOIN saleComponent sc ON sc.saleFk = s.id
WHERE sc.saleFk <> vComponentFk
GROUP BY s.id
HAVING dif <> 0;
2024-01-25 16:33:54 +00:00
UPDATE sale s
JOIN (
2024-11-27 17:19:57 +00:00
SELECT SUM(sc.value) sumValue, sc.saleFk
FROM saleComponent sc
JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
JOIN `component` c ON c.id = sc.componentFk
JOIN componentType ct on ct.id = c.typeFk AND ct.isBase
GROUP BY sc.saleFk) sc ON sc.saleFk = s.id
2024-11-27 17:19:57 +00:00
SET s.priceFixed = sumValue,
s.isPriceFixed = TRUE;
2024-11-27 17:19:57 +00:00
DELETE sc
FROM saleComponent sc
JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
JOIN sale s on s.id = sc.saleFk
JOIN item i ON i.id = s.itemFk
JOIN itemType it ON it.id = i.typeFk
2024-01-25 16:33:54 +00:00
WHERE it.code = 'PRT';
2024-11-27 17:19:57 +00:00
INSERT INTO saleComponent(saleFk, componentFk, value)
2024-01-25 16:33:54 +00:00
SELECT s.id, 15, s.price
FROM sale s
JOIN tmp.sale tmps ON tmps.saleFk = s.id
JOIN item i ON i.id = s.itemFK
JOIN itemType it ON it.id = i.typeFk
WHERE it.code = 'PRT' AND s.price > 0;
END$$
DELIMITER ;