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

93 lines
2.2 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-03-04 12:14:41 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getSimilar`(
vSelf INT,
vWarehouseFk INT,
vDated DATE,
2024-03-04 12:14:41 +00:00
vShowType BOOL
)
BEGIN
/**
2024-03-04 12:14:41 +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
2024-03-04 12:14:41 +00:00
* @param vShowType Mostrar tipos
*/
DECLARE vCalcFk INT;
DECLARE vTypeFk INT;
DECLARE vPriority INT DEFAULT 1;
CALL cache.available_refresh(vCalcFk, FALSE, vWarehouseFk, vDated);
2024-03-13 06:42:51 +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
LEFT JOIN vn.itemTag it ON it.itemFk = i.id
AND it.priority = vPriority
LEFT JOIN vn.tag t ON t.id = it.tagFk
WHERE i.id = vSelf
)
2024-03-04 12:14:41 +00:00
SELECT i.id itemFk,
i.longName,
i.subName,
i.tag5,
i.value5,
2024-03-13 06:42:51 +00:00
(i.value5 <=> its.value5) match5,
2024-03-04 12:14:41 +00:00
i.tag6,
i.value6,
2024-03-13 06:42:51 +00:00
(i.value6 <=> its.value6) match6,
2024-03-04 12:14:41 +00:00
i.tag7,
i.value7,
2024-03-13 06:42:51 +00:00
(i.value7 <=> its.value7) match7,
2024-03-04 12:14:41 +00:00
i.tag8,
i.value8,
2024-03-13 06:42:51 +00:00
(i.value8 <=> its.value8) match8,
2024-03-04 12:14:41 +00:00
a.available,
IFNULL(ip.counter, 0) `counter`,
IF(b.groupingMode = 1, b.grouping, b.packing) minQuantity,
iss.visible located
2024-03-13 06:42:51 +00:00
FROM vn.item i
JOIN cache.available a ON a.item_id = i.id
2024-03-11 12:27:18 +00:00
AND a.calc_id = vCalcFk
2024-03-13 06:42:51 +00:00
LEFT JOIN vn.itemProposal ip ON ip.mateFk = i.id
2024-03-04 12:14:41 +00:00
AND ip.itemFk = vSelf
2024-03-13 06:42:51 +00:00
LEFT JOIN vn.itemTag it ON it.itemFk = i.id
2024-03-04 12:14:41 +00:00
AND it.priority = vPriority
2024-03-13 06:42:51 +00:00
LEFT JOIN vn.tag t ON t.id = it.tagFk
2024-03-04 12:14:41 +00:00
LEFT JOIN cache.last_buy lb ON lb.item_id = i.id
AND lb.warehouse_id = vWarehouseFk
2024-03-13 06:42:51 +00:00
LEFT JOIN vn.buy b ON b.id = lb.buy_id
LEFT JOIN vn.itemShelvingStock iss ON iss.itemFk = i.id
2024-03-04 12:14:41 +00:00
AND iss.warehouseFk = vWarehouseFk
2024-03-13 06:42:51 +00:00
JOIN itemTags its
2024-03-11 12:27:18 +00:00
WHERE a.available > 0
2024-03-13 06:42:51 +00:00
AND IF(vShowType, i.typeFk = its.typeFk, TRUE)
2024-03-04 12:14:41 +00:00
AND i.id <> vSelf
ORDER BY `counter` DESC,
2024-03-13 06:42:51 +00:00
(t.name = its.name) DESC,
(it.value = its.value) DESC,
(i.tag5 = its.tag5) DESC,
2024-03-04 12:14:41 +00:00
match5 DESC,
2024-03-13 06:42:51 +00:00
(i.tag6 = its.tag6) DESC,
2024-03-04 12:14:41 +00:00
match6 DESC,
2024-03-13 06:42:51 +00:00
(i.tag7 = its.tag7) DESC,
2024-03-04 12:14:41 +00:00
match7 DESC,
2024-03-13 06:42:51 +00:00
(i.tag8 = its.tag8) DESC,
2024-03-04 12:14:41 +00:00
match8 DESC;
END$$
DELIMITER ;