UPDATE tbnfs_item_imp S SET
	
	S.VALOR_TOTAL = S.VALOR_UNITARIO * S.QUANTIDADE,
    OPERACAO_CODIGO = 'VEN',
    TRIBUTACAO_ORIGEM = 0,
    TRIBUTACAO_ICMS = 0,
    TRIBUTACAO_IPI = 53,
    TRIBUTACAO_PIS = '01',
    TRIBUTACAO_COFINS = '01',
	PERCENTUAL_ICMS = 18,
	PERCENTUAL_PIS = '1.65',
	PERCENTUAL_COFINS = '7.6',
	FISCAL_PERCENTUAL_ICMS = 18,
	FISCAL_PERCENTUAL_PIS = '1.65',
	FISCAL_PERCENTUAL_COFINS = '7.6',
	S.QUANTIDADE = S.T01+S.T02+S.T03+S.T04+S.T05+S.T06+S.T07+S.T08+S.T09+S.T10

WHERE S.IMP = 2;



select
    s.nfs_controle,
    s.MODELO_ID,
    s.cor_id,
    s.comanda,
    s.imp,
    row_number() over(partition by s.nfs_controle order by s.nfs_controle) sequencia
    ,gen_id(gtbnfs_item,1) as controle
from tbnfs_item_imp s
where s.imp = 2
order by s.nfs_controle;

update tbnfs_item i set
i.numero_notafiscal = (select first 1 s.numero_notafiscal from tbnfs s where s.controle = i.nfs_controle),
i.empresa_codigo = (select first 1 s.empresa_codigo from tbnfs s where s.controle = i.nfs_controle),
i.data_emissao   = (select first 1 s.data_emissao from tbnfs s where s.controle = i.nfs_controle),
i.localizacao_codigo = (select first 1 l.codigo from tblocalizacao l where l.estabelecimento_codigo = i.estabelecimento_codigo),
i.cfop_codigo = (select first 1 s.cfop_codigo from tbnfs s where s.controle = i.nfs_controle),
i.percentual_icms = (select first 1 s.tmp_al_icms from tbnfs s where s.controle = i.nfs_controle)
where i.imp = 2;

update tbnfs s set
   s.cfop_codigo = (select iif(e.uf = 'CE', '5.101', '6.101') from tbempresa e where e.codigo = s.empresa_codigo),
   s.TMP_AL_ICMS = (select iif(e.uf = 'CE', 18, 12) from tbempresa e where e.codigo = s.empresa_codigo)
where s.imp > 0;


--tbnfs_totais
select
    i.controle nfs_controle,
    (select sum(j.valor_total) from tbnfs_item j where j.nfs_controle = i.controle) valor_total_nf,
    1 gc_web
from tbnfs i
where i.imp > 0