8032-devToTest_2440 #3009
|
@ -1,27 +1,26 @@
|
||||||
DELIMITER $$
|
DELIMITER $$
|
||||||
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`item_getSimilar`(
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getSimilar`(
|
||||||
vSelf INT,
|
vSelf INT,
|
||||||
vWarehouseFk INT,
|
vWarehouseFk INT,
|
||||||
vDated DATE,
|
vDated DATE,
|
||||||
vShowType BOOL
|
vShowType BOOL,
|
||||||
|
vDaysInForward INT
|
||||||
)
|
)
|
||||||
BEGIN
|
BEGIN
|
||||||
/**
|
/**
|
||||||
* Propone articulos disponibles ordenados, con la cantidad
|
* Propone articulos ordenados, con la cantidad
|
||||||
* de veces usado y segun sus caracteristicas.
|
* de veces usado y segun sus caracteristicas.
|
||||||
*
|
*
|
||||||
* @param vSelf Id de artículo
|
* @param vSelf Id de artículo
|
||||||
* @param vWarehouseFk Id de almacen
|
* @param vWarehouseFk Id de almacen
|
||||||
* @param vDated Fecha
|
* @param vDated Fecha
|
||||||
* @param vShowType Mostrar tipos
|
* @param vShowType Mostrar tipos
|
||||||
|
* @param vDaysInForward Días de alcance para las ventas
|
||||||
*/
|
*/
|
||||||
DECLARE vAvailableCalcFk INT;
|
DECLARE vAvailableCalcFk INT;
|
||||||
DECLARE vVisibleCalcFk INT;
|
|
||||||
DECLARE vTypeFk INT;
|
|
||||||
DECLARE vPriority INT DEFAULT 1;
|
DECLARE vPriority INT DEFAULT 1;
|
||||||
|
|
||||||
CALL cache.available_refresh(vAvailableCalcFk, FALSE, vWarehouseFk, vDated);
|
CALL cache.available_refresh(vAvailableCalcFk, FALSE, vWarehouseFk, vDated);
|
||||||
CALL cache.visible_refresh(vVisibleCalcFk, FALSE, vWarehouseFk);
|
|
||||||
|
|
||||||
WITH itemTags AS (
|
WITH itemTags AS (
|
||||||
SELECT i.id,
|
SELECT i.id,
|
||||||
|
@ -41,8 +40,26 @@ BEGIN
|
||||||
AND it.priority = vPriority
|
AND it.priority = vPriority
|
||||||
LEFT JOIN vn.tag t ON t.id = it.tagFk
|
LEFT JOIN vn.tag t ON t.id = it.tagFk
|
||||||
WHERE i.id = vSelf
|
WHERE i.id = vSelf
|
||||||
|
),
|
||||||
|
stock AS (
|
||||||
|
SELECT itemFk, SUM(visible) stock
|
||||||
|
FROM vn.itemShelvingStock
|
||||||
|
WHERE warehouseFk = vWarehouseFk
|
||||||
|
GROUP BY itemFk
|
||||||
|
),
|
||||||
|
sold AS (
|
||||||
|
SELECT SUM(s.quantity) AS 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 BETWEEN CURDATE() AND CURDATE() + INTERVAL vDaysInForward DAY
|
||||||
|
AND iss.saleFk IS NULL
|
||||||
|
AND t.warehouseFk = vWarehouseFk
|
||||||
|
GROUP BY s.itemFk
|
||||||
)
|
)
|
||||||
SELECT i.id itemFk,
|
SELECT i.id itemFk,
|
||||||
|
CAST(sd.quantity AS INT) advanceable,
|
||||||
i.longName,
|
i.longName,
|
||||||
i.subName,
|
i.subName,
|
||||||
i.tag5,
|
i.tag5,
|
||||||
|
@ -64,13 +81,13 @@ BEGIN
|
||||||
WHEN b.groupingMode = 'packing' THEN b.packing
|
WHEN b.groupingMode = 'packing' THEN b.packing
|
||||||
ELSE 1
|
ELSE 1
|
||||||
END minQuantity,
|
END minQuantity,
|
||||||
v.visible located,
|
sk.stock located,
|
||||||
b.price2
|
b.price2
|
||||||
FROM vn.item i
|
FROM vn.item i
|
||||||
|
LEFT JOIN sold sd ON sd.itemFk = i.id
|
||||||
JOIN cache.available a ON a.item_id = i.id
|
JOIN cache.available a ON a.item_id = i.id
|
||||||
AND a.calc_id = vAvailableCalcFk
|
AND a.calc_id = vAvailableCalcFk
|
||||||
LEFT JOIN cache.visible v ON v.item_id = i.id
|
LEFT JOIN stock sk ON sk.itemFk = i.id
|
||||||
AND v.calc_id = vVisibleCalcFk
|
|
||||||
LEFT JOIN cache.last_buy lb ON lb.item_id = i.id
|
LEFT JOIN cache.last_buy lb ON lb.item_id = i.id
|
||||||
AND lb.warehouse_id = vWarehouseFk
|
AND lb.warehouse_id = vWarehouseFk
|
||||||
LEFT JOIN vn.itemProposal ip ON ip.mateFk = i.id
|
LEFT JOIN vn.itemProposal ip ON ip.mateFk = i.id
|
||||||
|
@ -80,10 +97,11 @@ BEGIN
|
||||||
LEFT JOIN vn.tag t ON t.id = it.tagFk
|
LEFT JOIN vn.tag t ON t.id = it.tagFk
|
||||||
LEFT JOIN vn.buy b ON b.id = lb.buy_id
|
LEFT JOIN vn.buy b ON b.id = lb.buy_id
|
||||||
JOIN itemTags its
|
JOIN itemTags its
|
||||||
WHERE a.available > 0
|
WHERE (a.available > 0 OR sd.quantity < sk.stock)
|
||||||
AND (i.typeFk = its.typeFk OR NOT vShowType)
|
AND (i.typeFk = its.typeFk OR NOT vShowType)
|
||||||
AND i.id <> vSelf
|
AND i.id <> vSelf
|
||||||
ORDER BY `counter` DESC,
|
ORDER BY (a.available > 0) DESC,
|
||||||
|
`counter` DESC,
|
||||||
(t.name = its.name) DESC,
|
(t.name = its.name) DESC,
|
||||||
(it.value = its.value) DESC,
|
(it.value = its.value) DESC,
|
||||||
(i.tag5 = its.tag5) DESC,
|
(i.tag5 = its.tag5) DESC,
|
||||||
|
|
Loading…
Reference in New Issue