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

100 lines
2.4 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_getSimilar`(
2024-03-14 07:36:19 +00:00
vSelf INT,
vWarehouseFk INT,
vDated DATE,
vShowType BOOL
)
BEGIN
/**
2024-03-14 07:36:19 +00:00
* Propone articulos disponibles ordenados, con la cantidad
* de veces usado y segun sus caracteristicas.
*
* @param vSelf Id de artículo
* @param vWarehouseFk Id de almacen
* @param vDated Fecha
* @param vShowType Mostrar tipos
*/
2024-07-01 08:06:34 +00:00
DECLARE vAvailableCalcFk INT;
DECLARE vVisibleCalcFk INT;
2024-03-14 07:36:19 +00:00
DECLARE vTypeFk INT;
DECLARE vPriority INT DEFAULT 1;
2024-07-01 08:06:34 +00:00
CALL cache.available_refresh(vAvailableCalcFk, FALSE, vWarehouseFk, vDated);
CALL cache.visible_refresh(vVisibleCalcFk, FALSE, vWarehouseFk);
2024-03-14 07:36:19 +00:00
WITH itemTags AS (
SELECT i.id,
typeFk,
tag5,
value5,
tag6,
value6,
tag7,
value7,
tag8,
value8,
t.name,
it.value
FROM vn.item i
2024-06-18 09:03:30 +00:00
LEFT JOIN vn.itemTag it ON it.itemFk = i.id
2024-03-14 07:36:19 +00:00
AND it.priority = vPriority
LEFT JOIN vn.tag t ON t.id = it.tagFk
WHERE i.id = vSelf
)
SELECT i.id itemFk,
i.longName,
i.subName,
i.tag5,
i.value5,
(i.value5 <=> its.value5) match5,
i.tag6,
i.value6,
(i.value6 <=> its.value6) match6,
i.tag7,
i.value7,
(i.value7 <=> its.value7) match7,
i.tag8,
i.value8,
(i.value8 <=> its.value8) match8,
a.available,
IFNULL(ip.counter, 0) `counter`,
2024-04-04 11:28:29 +00:00
CASE
WHEN b.groupingMode = 'grouping' THEN b.grouping
WHEN b.groupingMode = 'packing' THEN b.packing
2024-04-04 11:28:29 +00:00
ELSE 1
2024-07-11 06:57:45 +00:00
END minQuantity,
2024-07-01 08:06:34 +00:00
v.visible located,
2024-04-22 07:43:13 +00:00
b.price2
2024-03-14 07:36:19 +00:00
FROM vn.item i
2024-06-18 09:03:30 +00:00
JOIN cache.available a ON a.item_id = i.id
2024-07-01 08:06:34 +00:00
AND a.calc_id = vAvailableCalcFk
LEFT JOIN cache.visible v ON v.item_id = i.id
AND v.calc_id = vVisibleCalcFk
LEFT JOIN cache.last_buy lb ON lb.item_id = i.id
AND lb.warehouse_id = vWarehouseFk
2024-03-14 07:36:19 +00:00
LEFT JOIN vn.itemProposal ip ON ip.mateFk = i.id
AND ip.itemFk = vSelf
LEFT JOIN vn.itemTag it ON it.itemFk = i.id
AND it.priority = vPriority
LEFT JOIN vn.tag t ON t.id = it.tagFk
LEFT JOIN vn.buy b ON b.id = lb.buy_id
JOIN itemTags its
WHERE a.available > 0
2024-05-08 10:43:46 +00:00
AND (i.typeFk = its.typeFk OR NOT vShowType)
2024-03-14 07:36:19 +00:00
AND i.id <> vSelf
ORDER BY `counter` DESC,
(t.name = its.name) DESC,
(it.value = its.value) DESC,
(i.tag5 = its.tag5) DESC,
match5 DESC,
(i.tag6 = its.tag6) DESC,
match6 DESC,
(i.tag7 = its.tag7) DESC,
match7 DESC,
(i.tag8 = its.tag8) DESC,
2024-03-22 10:36:26 +00:00
match8 DESC
LIMIT 100;
END$$
DELIMITER ;