diff --git a/db/routines/vn/procedures/item_getSimilar.sql b/db/routines/vn/procedures/item_getSimilar.sql index eeb71a693..537f53848 100644 --- a/db/routines/vn/procedures/item_getSimilar.sql +++ b/db/routines/vn/procedures/item_getSimilar.sql @@ -8,19 +8,22 @@ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_getSimilar`( ) BEGIN /** -* Propone articulos 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 -* @param vDaysInForward Días de alcance para las ventas -*/ + * Propone articulos 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 + * @param vDaysInForward Días de alcance para las ventas (https://redmine.verdnatura.es/issues/7956#note-4) + */ DECLARE vAvailableCalcFk INT; + DECLARE vVisibleCalcFk INT; + DECLARE vTypeFk INT; DECLARE vPriority INT DEFAULT 1; CALL cache.available_refresh(vAvailableCalcFk, FALSE, vWarehouseFk, vDated); + CALL cache.visible_refresh(vVisibleCalcFk, FALSE, vWarehouseFk); WITH itemTags AS ( SELECT i.id, @@ -40,29 +43,9 @@ BEGIN AND it.priority = vPriority LEFT JOIN vn.tag t ON t.id = it.tagFk WHERE i.id = vSelf - ), - stock AS ( - SELECT ish.itemFk, - SUM(ish.visible) stock - FROM vn.itemShelving ish - JOIN vn.shelving sh ON sh.`code` = ish.shelvingFk - JOIN vn.parking p ON p.id = sh.parkingFk - JOIN vn.sector s ON s.id = p.sectorFk - WHERE s.warehouseFk = vWarehouseFk - GROUP BY ish.itemFk - ), - sold AS ( - SELECT SUM(s.quantity) quantity, s.itemFk - FROM vn.sale s - JOIN vn.ticket t ON t.id = s.ticketFk - LEFT JOIN vn.itemShelvingSale iss ON iss.saleFk = s.id - WHERE t.shipped >= CURDATE() + INTERVAL vDaysInForward DAY - AND iss.saleFk IS NULL - AND t.warehouseFk = vWarehouseFk - GROUP BY s.itemFk ) SELECT i.id itemFk, - LEAST(CAST(sd.quantity AS INT), sk.stock) advanceable, + NULL advanceable, -- https://redmine.verdnatura.es/issues/7956#note-4 i.longName, i.subName, i.tag5, @@ -84,13 +67,13 @@ BEGIN WHEN b.groupingMode = 'packing' THEN b.packing ELSE 1 END minQuantity, - sk.stock located, + v.visible located, b.price2 FROM vn.item i - LEFT JOIN sold sd ON sd.itemFk = i.id JOIN cache.available a ON a.item_id = i.id AND a.calc_id = vAvailableCalcFk - LEFT JOIN stock sk ON sk.itemFk = i.id + 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 LEFT JOIN vn.itemProposal ip ON ip.mateFk = i.id @@ -100,21 +83,20 @@ BEGIN 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 OR sd.quantity < sk.stock) + WHERE a.available > 0 AND (i.typeFk = its.typeFk OR NOT vShowType) AND i.id <> vSelf - ORDER BY (a.available > 0) DESC, - `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 + 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 ;