89 lines
2.6 KiB
MySQL
89 lines
2.6 KiB
MySQL
|
DELIMITER $$
|
||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`historico_joined`(IN idART INT, IN wh_joined INT, IN v_virtual INT)
|
||
|
BEGIN
|
||
|
DECLARE datFEC DATETIME;
|
||
|
|
||
|
SELECT Fechainventario INTO datFEC FROM tblContadores;
|
||
|
SET @a = 0;
|
||
|
SELECT DATE(Fecha) Fecha,
|
||
|
Entrada,
|
||
|
Salida,
|
||
|
OK,
|
||
|
Alias,
|
||
|
Referencia,
|
||
|
id,
|
||
|
@a := @a + IFNULL(Entrada,0) - IFNULL(Salida,0) acumulado,
|
||
|
F5,
|
||
|
v_virtual,
|
||
|
CodigoTrabajador
|
||
|
FROM
|
||
|
( SELECT TR.landing Fecha,
|
||
|
C.Cantidad Entrada,
|
||
|
NULL Salida,
|
||
|
(TR.received != FALSE) OK,
|
||
|
P.Proveedor Alias,
|
||
|
E.Referencia Referencia,
|
||
|
E.Id_Entrada id,
|
||
|
TR.delivered F5,
|
||
|
tra.CodigoTrabajador
|
||
|
FROM Compres C
|
||
|
JOIN Entradas E USING (Id_Entrada)
|
||
|
JOIN travel TR ON TR.id = E.travel_id
|
||
|
JOIN Proveedores P USING (Id_Proveedor)
|
||
|
LEFT JOIN Articles a ON a.Id_Article = C.Id_Article
|
||
|
LEFT JOIN Tipos ti ON ti.Id_Tipo = a.tipo_id
|
||
|
LEFT JOIN Trabajadores tra ON tra.Id_Trabajador = ti.Id_Trabajador
|
||
|
LEFT JOIN warehouse_joined wj ON wj.warehouse_id = TR.warehouse_id
|
||
|
WHERE TR.landing >= datFEC
|
||
|
AND wh_joined IN (wj.warehouse_alias_id , 0)
|
||
|
AND C.Id_Article = idART
|
||
|
AND E.Inventario = 0
|
||
|
AND E.Redada = 0
|
||
|
UNION ALL
|
||
|
SELECT TR.shipment Fecha,
|
||
|
NULL Entrada,
|
||
|
C.Cantidad Salida,
|
||
|
TR.delivered OK,
|
||
|
P.Proveedor Alias,
|
||
|
E.Referencia Referencia,
|
||
|
E.Id_Entrada id,
|
||
|
TR.delivered F5,
|
||
|
tra.CodigoTrabajador
|
||
|
FROM Compres C
|
||
|
JOIN Entradas E USING (Id_Entrada)
|
||
|
JOIN travel TR ON TR.id = E.travel_id
|
||
|
JOIN Proveedores P USING (Id_Proveedor)
|
||
|
LEFT JOIN Articles a ON a.Id_Article = C.Id_Article
|
||
|
LEFT JOIN Tipos ti ON ti.Id_Tipo = a.tipo_id
|
||
|
LEFT JOIN Trabajadores tra ON tra.Id_Trabajador = ti.Id_Trabajador
|
||
|
LEFT JOIN warehouse_joined wj ON wj.warehouse_id = TR.warehouse_id_out
|
||
|
WHERE TR.shipment >= datFEC
|
||
|
AND wh_joined IN (wj.warehouse_alias_id,0)
|
||
|
AND Id_Proveedor <> 4
|
||
|
AND C.Id_Article = idART
|
||
|
AND E.Inventario = 0
|
||
|
AND E.Redada = 0
|
||
|
UNION ALL
|
||
|
SELECT T.Fecha Fecha,
|
||
|
NULL Entrada,
|
||
|
M.Cantidad Salida,
|
||
|
(M.OK <> 0 OR T.Etiquetasemitidas <> 0 OR T.Factura IS NOT NULL) OK,
|
||
|
T.Alias Alias,
|
||
|
T.Localizacion Referencia,
|
||
|
T.Id_Ticket id,
|
||
|
T.PedidoImpreso EtiquetasEmitidas,
|
||
|
tr.CodigoTrabajador
|
||
|
FROM Movimientos M
|
||
|
JOIN Tickets T USING (Id_Ticket)
|
||
|
JOIN Clientes C ON C.Id_Cliente = T.Id_Cliente
|
||
|
LEFT JOIN Trabajadores tr ON tr.Id_Trabajador = C.Id_Trabajador
|
||
|
LEFT JOIN warehouse_joined wj ON wj.warehouse_id = T.warehouse_id
|
||
|
WHERE T.Fecha >= datFEC
|
||
|
AND M.Id_Article = idART
|
||
|
AND wh_joined IN (wj.warehouse_alias_id , 0)
|
||
|
) AS Historico
|
||
|
ORDER BY Fecha, Entrada DESC, OK DESC;
|
||
|
|
||
|
END$$
|
||
|
DELIMITER ;
|