2024-12-02 11:54:36 +00:00
|
|
|
DELIMITER $$
|
2024-12-02 12:01:12 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`buy_getLastWithoutInventory`(
|
2024-12-02 11:54:36 +00:00
|
|
|
vItemFk INT,
|
|
|
|
vWarehouseFk INT
|
|
|
|
)
|
|
|
|
RETURNS int(11)
|
|
|
|
DETERMINISTIC
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Retorna la última compra que no sea inventario.
|
|
|
|
*
|
|
|
|
* @param vItemFk Id del artículo
|
|
|
|
* @param vWarehouseFk Id del almacén
|
|
|
|
* @return Id de compra
|
|
|
|
*/
|
|
|
|
DECLARE vBuyFk INT;
|
|
|
|
|
|
|
|
SELECT b.id INTO vBuyFk
|
2024-12-02 11:57:34 +00:00
|
|
|
FROM buy b
|
|
|
|
JOIN entry e ON e.id = b.entryFk
|
|
|
|
JOIN travel t ON t.id = e.travelFk
|
|
|
|
WHERE e.id <> (SELECT defaultEntry FROM entryConfig)
|
|
|
|
AND e.supplierFk <> (SELECT supplierFk FROM inventoryConfig)
|
2024-12-02 11:54:36 +00:00
|
|
|
AND e.typeFk <> 'inventory'
|
|
|
|
AND b.itemFk = vItemFk
|
|
|
|
AND (t.warehouseInFk = vWarehouseFk OR t.warehouseInFk IS NULL)
|
|
|
|
ORDER BY ABS(DATEDIFF(util.VN_CURDATE(), t.landed)), e.created DESC
|
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
RETURN vBuyFk;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|