select 
m.mes_001 as id_mesa,
v.ven_001 as id_venda, 
v.ven_029 as nro_venda, 
m.mes_003 as nro_mesa, 
m.mes_002 as descricao_mesa, 
v.ven_009 as valor_venda, 
case when :flag_tx_servico  then round((sum(i.ite_005) * 0.1), 2) else 0 end as valor_tx,
-- case when :flag_tx_servico  then round((v.ven_009 * 0.1), 2) else 0 end as valor_tx,
coalesce(v.ven_029, -1) as cod_venda,
cast(trim(concat(m.mes_002,' ', v.ven_027))  as varchar(100)) as nro_nome_mesa,
v.dat_001_1 as data_abertura,
coalesce(v.ven_007,0.00) as desconto,
coalesce(v.ven_008, 0.00) as acrescimo,
0.00 as total_pago,
0.00 as valor_restante,
0.00 as valor_troco,
0.00 as desconto_percent,
case when :flag_couvert_mesa then :valor_couvert_masc else 0.00 end as couvert_m,  
case when :flag_couvert_mesa then :valor_couvert_fem else 0.00 end as couvert_f,
v.nro_couvert_m,
v.nro_couvert_f,
v.nro_pessoas,
v.b_taxa_entrega,
v.cpf_cliente,
c.id_caixa,
v.ven_027 as obs_mesa,
coalesce(sum(i.desconto),0.00) as valor_desconto_itens,
count (1) as nro_itens,
0.00 as valor_pag1,
0.00 as valor_pag2,
0.00 as valor_pag3,
0 as cod_pag1,
0 as cod_pag2,
0 as cod_pag3,
0.00 as desconto_acrescimo,
numero_cupom,
v.ven_024 as tipo_venda,
v.cli_001,
cl.cli_002 as nome_cliente,
u.usu_002 as nome_vendedor,
sum(i.ite_005)  as total_itens
from venda v 
join vendaitem i on i.ven_001=v.ven_001 and i.emp_001=v.emp_001 and i.sit_001 in (4) 
join caixa c on c.id_empresa = v.emp_001 and id_caixa=:id_caixa
left join  mesa m on (m.emp_001 = v.emp_001) and (m.mes_001 = v.ven_025)
left join clientes cl on cl.cli_001 = v.cli_001 and cl.emp_001=v.emp_001
left join usuarios u on v.id_vendedor=u.usu_001


where v.ven_001 = :id_venda and v.emp_001=:emp
group by 
m.mes_001, v.ven_001, v.ven_029, m.mes_003, m.mes_002, c.id_caixa, v.ven_024,
v.ven_009, v.ven_027, v.dat_001_1, v.nro_couvert_m, v.nro_couvert_f, v.nro_pessoas, v.b_taxa_entrega, 
v.cpf_cliente, v.numero_cupom, v.cli_001, cl.cli_002, u.usu_002, v.ven_008, v.ven_007
