SELECT
Estq_auxCOdigoGrade.IdProduto,
Estq_cadProdutos.IdSetorProduto,
(Select Descricao from Estq_cadSetores where Estq_cadSetores.IdSetorProduto = Estq_cadProdutos.IdSetorProduto) as Setor,
(Select Descricao from estq_cadClassProduto where estq_cadClassProduto.IdClassProduto = Estq_cadProdutos.IdClassProduto) as Classificacao,
Estq_cadProdutos.IdClassProduto,
Estq_cadProdutos.Codigo as CodigoPai,
Estq_auxCodigoGrade.Codigo as CodigoFilho,
Estq_cadClassProd5.Descricao as Tamanho,
Estq_cadClassProd5.IdClassProd5,
Estq_cadClassProd6.IdClassProd6,
Estq_cadClassProd6.Descricao as Cor,
Estq_cadProdutos.Descricao,
(select top 1 Data from Estq_auxMovimentoEstoque
WHERE ISNULL(Observacoes,'') NOT LIKE ('%Inventário%')
and Estq_auxMovimentoEstoque.IdProduto = Estq_auxCOdigoGrade.IdProduto
and Estq_auxMovimentoEstoque.IdClassProd5 = Estq_auxCOdigoGrade.IdClassProd5
and Estq_auxMovimentoEstoque.IdClassProd6 = Estq_auxCOdigoGrade.IdClassProd6
ORDER BY DATA DESC) as UltimaEntrada,
( datediff(day, (select top 1 Data from Estq_auxMovimentoEstoque
WHERE ISNULL(Observacoes,'') NOT LIKE ('%Inventário%')
and Estq_auxMovimentoEstoque.IdProduto = Estq_auxCOdigoGrade.IdProduto
and Estq_auxMovimentoEstoque.IdClassProd5 = Estq_auxCOdigoGrade.IdClassProd5
and Estq_auxMovimentoEstoque.IdClassProd6 = Estq_auxCOdigoGrade.IdClassProd6
ORDER BY DATA DESC), getdaTE())
) as QtdeDiasEmEstoque,
(SELECT TOP 1 (custoTotal/Qtde) as Custo FROM Estq_auxMovimentoEstoque
WHERE
Estq_auxMovimentoEstoque.IdProduto = Estq_auxCOdigoGrade.IdProduto
and custoTotal > 0 and sentido = 1
ORDER BY Data DESC, IdMovimentoEstoque Desc) as Custo,
((SELECT TOP 1 (custoTotal/Qtde) as Custo FROM Estq_auxMovimentoEstoque WHERE Estq_auxMovimentoEstoque.IdProduto = Estq_auxCOdigoGrade.IdProduto and custoTotal > 0 and sentido = 1 ORDER BY Data DESC, IdMovimentoEstoque Desc)*isnull((SELECT SUM((CASE when Estq_auxMovimentoEstoque.Sentido=1 then Convert(decimal(18,4),Estq_auxMovimentoEstoque.Qtde) else (-1)*Convert(decimal(18,4),Estq_auxMovimentoEstoque.Qtde) end)) FROM Estq_auxMovimentoEstoque WHERE Estq_auxMovimentoEstoque.IdProduto = Estq_auxCOdigoGrade.IdProduto and Estq_auxMovimentoEstoque.IdClassProd5 = Estq_auxCOdigoGrade.IdClassProd5 and Estq_auxMovimentoEstoque.IdClassProd6 = Estq_auxCOdigoGrade.IdClassProd6 ),0)) as CustoTotal,
CONVERT(integer,isnull((SELECT SUM((CASE when Estq_auxMovimentoEstoque.Sentido=1 then Convert(decimal(18,4),Estq_auxMovimentoEstoque.Qtde) else (-1)*Convert(decimal(18,4),Estq_auxMovimentoEstoque.Qtde) end)) FROM Estq_auxMovimentoEstoque
WHERE
Estq_auxMovimentoEstoque.IdProduto = Estq_auxCOdigoGrade.IdProduto
and Estq_auxMovimentoEstoque.IdClassProd5 = Estq_auxCOdigoGrade.IdClassProd5
and Estq_auxMovimentoEstoque.IdClassProd6 = Estq_auxCOdigoGrade.IdClassProd6
),0)) as Atual,
PrecoVenda, PrecoDePara,
CodigoForn as CodReferencia FROM Estq_auxCOdigoGrade
LEFT JOIN Estq_cadProdutos ON Estq_auxCOdigoGrade.IdProduto =Estq_cadProdutos.IdProduto
LEFT JOIN Estq_cadClassProd5 ON Estq_auxCOdigoGrade.IdClassProd5 =Estq_cadClassProd5.IdClassProd5
LEFT JOIN Estq_cadClassProd6 ON Estq_auxCOdigoGrade.IdClassProd6 =Estq_cadClassProd6.IdClassProd6
WHERE Estq_cadProdutos.Ativado=1 ORDER BY IdProduto DESC