set @acumuladoVenda = 0;
set @totalVendas = (select 	
        sum(`itensnfs`.`Vlr_LiqItem`) as vlrTotalVenda
from itensnfs
where 
Dat_Emissao >= '2018-01-01 00:00:00'
and Dat_Emissao <= '2018-12-31 23:59:59'
and `Cod_Cfop` IN (5102 , 
					6102,
					5403,
					6403,
					6108,
					6922,
					5922,
					6120,5120));
                    
select 	Codigo, 
		grprod, 
        Descricao, 
        Fabricante, 
        Tipo, 
        Cod_Estado, 
        '' as giroPeriodo,
        sum(Qtd_Fisico) as Qtd_Fisico, 
        sum(Qtd_Solicitado) as Qtd_Solicitado, 
        sum(Qtd_Quarentena) as Qtd_Quarentena, 
        sum(Qtd_Disponivel) as Qtd_Disponivel, 
        sum(TotalprcUltimaEntrada) as TotalprcUltimaEntrada, 
        sum(qtdNF) as qtdNF, 
        sum(vlrUnit) as vlrUnit, 
        sum(ticketMedioVenda) as ticketMedioVenda, 
        sum(Qtd_UnidVenda) as Qtd_UnidVenda, 
        sum(vlrTotalVenda) as vlrTotalVenda, 
        sum(part) as part 
from (
select 	
		Codigo, 
		grprod,
		Descricao, 
		Fabricante, 
		Tipo, 
		Cod_Estado,
		Qtd_Fisico, 
		Qtd_Solicitado, 
		Qtd_Quarentena, 
		Qtd_Disponivel,
		-- day(Dat_Emissao),
		-- month(Dat_Emissao) as mes, 
		-- year(Dat_Emissao) as ano,
        sum(ifnull(`itensnfs`.`PrcUltimaEntrada`,0)*ifnull(`itensnfs`.`Qtd_Produto`,0)) as TotalprcUltimaEntrada,
        count(num_nota) as qtdNF,
        (sum(ifnull(`itensnfs`.`Vlr_LiqItem`,0))/sum(ifnull(`itensnfs`.`Qtd_Produto`,0))) as vlrUnit,
        (sum(ifnull(`itensnfs`.`Vlr_LiqItem`,0))/count(num_nota)) as ticketMedioVenda,
        sum(ifnull(`itensnfs`.`Qtd_Produto`,0)) as Qtd_UnidVenda,        
        sum(ifnull(`itensnfs`.`Vlr_LiqItem`,0)) as vlrTotalVenda,
        round((sum(ifnull(`itensnfs`.`Vlr_LiqItem`,0))/@totalVendas)*100,2) as part
        
from produ
left join itensnfs on itensnfs.cod_produto = produ.codigo
where 
Dat_Emissao >= '2018-01-01 00:00:00'
and Dat_Emissao <= '2018-12-31 23:59:59'
and `Cod_Cfop` IN (5102 , 
					6102,
					5403,
					6403,
					6108,
					6922,
					5922,
					6120,
					5120)
group by 
	Codigo, 
	Descricao, 
	Fabricante, 
	Tipo, 
	Cod_Estado,
	Qtd_Fisico, 
	Qtd_Solicitado, 
	Qtd_Quarentena, 
	Qtd_Disponivel,
    grprod

UNION ALL
select 	Codigo, 
		grprod,
		Descricao, 
		Fabricante, 
		Tipo, 
		Cod_Estado,
		Qtd_Fisico, 
		Qtd_Solicitado, 
		Qtd_Quarentena, 
		Qtd_Disponivel,        
        0 as TotalprcUltimaEntrada,
        0 as qtdNF,
        0 as vlrUnit,
        0 as ticketMedioVenda,
        0 as Qtd_UnidVenda,
        0 as vlrTotalVenda,
        0 as part
from produ
	where
		Qtd_Disponivel > 0
) produtos
where
	((Qtd_Disponivel > 0) or (Qtd_UnidVenda > 0))
group by 
	Codigo, 
	Descricao, 
	Fabricante, 
	Tipo, 
	Cod_Estado,
	Qtd_Fisico, 
	Qtd_Solicitado, 
	Qtd_Quarentena, 
	Qtd_Disponivel,
    grprod
order by part desc;


/*


select 	
        sum(`itensnfs`.`Vlr_LiqItem`) as vlrTotalVenda
from itensnfs
where 
Dat_Emissao >= '2018-01-01'
and Dat_Emissao <= '2018-12-31'
and `Cod_Cfop` IN (5102 , 
					6102,
					5403,
					6403,
					6108,
					6922,
					5922,
					6120,
					5120)
                    
                  */  