98 lines
2.3 KiB
SQL
98 lines
2.3 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getSimilar`(
|
|
vSelf INT,
|
|
vWarehouseFk INT,
|
|
vDated DATE,
|
|
vShowType BOOL
|
|
)
|
|
BEGIN
|
|
/**
|
|
* 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
|
|
*/
|
|
DECLARE vCalcFk INT;
|
|
DECLARE vTypeFk INT;
|
|
DECLARE vPriority INT DEFAULT 1;
|
|
|
|
CALL cache.available_refresh(vCalcFk, FALSE, vWarehouseFk, vDated);
|
|
|
|
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
|
|
)
|
|
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`,
|
|
CASE
|
|
WHEN b.groupingMode = 'grouping' THEN b.grouping
|
|
WHEN b.groupingMode = 'packing' THEN b.packing
|
|
ELSE 1
|
|
END AS minQuantity,
|
|
iss.visible located,
|
|
b.price2
|
|
FROM vn.item i
|
|
JOIN cache.available a ON a.item_id = i.id
|
|
AND a.calc_id = vCalcFk
|
|
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 cache.last_buy lb ON lb.item_id = i.id
|
|
AND lb.warehouse_id = vWarehouseFk
|
|
LEFT JOIN vn.buy b ON b.id = lb.buy_id
|
|
LEFT JOIN vn.itemShelvingStock iss ON iss.itemFk = i.id
|
|
AND iss.warehouseFk = vWarehouseFk
|
|
JOIN itemTags its
|
|
WHERE a.available > 0
|
|
AND (i.typeFk = its.typeFk OR NOT vShowType)
|
|
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,
|
|
match8 DESC
|
|
LIMIT 100;
|
|
END$$
|
|
DELIMITER ;
|