80 lines
2.9 KiB
SQL
80 lines
2.9 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemFuentesBalance`(vDaysInFuture INT)
|
|
BEGIN
|
|
|
|
/* Se utiliza para calcular la necesidad de mover mercancia entre el almacén de fuentes y el nuestro
|
|
*
|
|
* @param vDaysInFuture Rango de dias para calcular entradas y salidas
|
|
*
|
|
*/
|
|
|
|
DECLARE vWarehouseFk INT;
|
|
|
|
SELECT s.warehouseFk INTO vWarehouseFk
|
|
FROM vn.sector s
|
|
WHERE s.code = 'FUENTES_PICASSE';
|
|
|
|
CALL cache.stock_refresh(FALSE);
|
|
|
|
SELECT i.id itemFk,
|
|
i.longName,
|
|
i.size,
|
|
i.subName,
|
|
v.amount - IFNULL(fue.Fuentes,0) - IFNULL(alb.albenfruit,0) as visible,
|
|
fue.Fuentes,
|
|
alb.Albenfruit,
|
|
sale.venta,
|
|
IFNULL(buy.compra,0) + IFNULL(mov.traslado,0) as compra,
|
|
IFNULL(v.amount,0) + IFNULL(sale.venta,0) + IFNULL(buy.compra,0) + IFNULL(mov.traslado,0)
|
|
- IFNULL(fue.Fuentes,0) - IFNULL(alb.albenfruit,0) as saldo
|
|
FROM vn.item i
|
|
JOIN vn.itemType it ON it.id = i.typeFk
|
|
JOIN vn.itemCategory ic ON ic.id = it.categoryFk
|
|
LEFT JOIN (
|
|
SELECT ish.itemFk, CAST(SUM(ish.visible) AS DECIMAL(10,0)) AS Fuentes
|
|
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.code = 'FUENTES_PICASSE'
|
|
GROUP BY ish.itemFk
|
|
) fue ON fue.itemFk = i.id
|
|
LEFT JOIN (
|
|
SELECT ish.itemFk, CAST(SUM(ish.visible) AS DECIMAL(10,0)) AS Albenfruit
|
|
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.code = 'ALBENFRUIT'
|
|
GROUP BY ish.itemFk
|
|
) alb ON alb.itemFk = i.id
|
|
LEFT JOIN cache.stock v ON i.id = v.item_id AND v.warehouse_id = vWarehouseFk
|
|
LEFT JOIN (
|
|
SELECT itemFk item_id, CAST(sum(quantity)AS DECIMAL(10,0)) as venta
|
|
FROM itemTicketOut
|
|
WHERE shipped BETWEEN util.VN_CURDATE() AND TIMESTAMPADD(DAY,vDaysInFuture , util.dayend(util.VN_CURDATE()))
|
|
AND warehouseFk = vWarehouseFk
|
|
GROUP BY itemFk
|
|
) sale ON sale.item_id = i.id
|
|
LEFT JOIN (
|
|
SELECT itemFk item_id, CAST(sum(quantity)AS DECIMAL(10,0)) as compra
|
|
FROM itemEntryIn
|
|
WHERE landed BETWEEN util.VN_CURDATE() AND TIMESTAMPADD(DAY,vDaysInFuture , util.dayend(util.VN_CURDATE()))
|
|
AND warehouseInFk = vWarehouseFk
|
|
AND isVirtualStock = FALSE
|
|
GROUP BY itemFk
|
|
) buy ON buy.item_id = i.id
|
|
LEFT JOIN (
|
|
SELECT itemFk item_id, CAST(sum(quantity)AS DECIMAL(10,0)) as traslado
|
|
FROM itemEntryOut
|
|
WHERE shipped BETWEEN util.VN_CURDATE() AND TIMESTAMPADD(DAY,vDaysInFuture , util.dayend(util.VN_CURDATE()))
|
|
AND warehouseOutFk = vWarehouseFk
|
|
GROUP BY itemFk
|
|
) mov ON mov.item_id = i.id
|
|
WHERE (v.amount OR fue.Fuentes OR alb.Albenfruit)
|
|
AND i.itemPackingTypeFk = 'H'
|
|
AND ic.shortLife;
|
|
|
|
END$$
|
|
DELIMITER ;
|