update contas_receber set vlr_quitacao = 0, situacao = 'ABERTO'
where id not in (select id_conta from recibo_receber_itens where id_conta = contas_receber.id) and situacao = 'QUITADO'
Last active
March 25, 2024 17:25
-
-
Save dxdns/f439cf17fdfcaa7f1a5bdda45c4f1ee0 to your computer and use it in GitHub Desktop.
comandos sql | firebirdsql
merge into produtos a
using(select d.id_produto, sum(d.quantidade) as quantidade from nota_compra_detalhe d
left join nota_compra c on c.id = d.id_nfe where c.nota = X
group by d.id_produto) b
on (a.id_produto = b.id_produto)
when matched then update set
a.estoque = a.estoque + b.quantidade
merge into produtos a
using(select d.id_produto, substring(d.cst from 1 for 1) as origem,
case when substring(d.cst from 2 for 2) = 20 then
substring(d.cst from 1 for 1)||'00'
else
substring(d.cst from 1 for 1)||substring(d.cst from 2 for 2) end as cst
from NOTA_COMPRA_DETALHE d
where d.id = (select max(e.id) from nota_compra_detalhe e
where e.id_produto = d.id_produto group by e.id_produto)) b
on (a.id_produto = b.id_produto)
when matched then update set
a.origem = b.origem, a.cst = b.cst
update PRODUTOS
set ST = 'F', CSOSN = 500, CST = '060', ECF_ICMS_ST = 'FF', ICMS = 0
where substring(CST from 2 for 2) = '60';
update PRODUTOS
set ST = 'T', CSOSN = 102, CST = '000', ECF_ICMS_ST = '18', ICMS = 18
where substring(CST from 2 for 2) = '00';
update PRODUTOS
set ST = 'I', CSOSN = 103, CST = '040', ECF_ICMS_ST = 'II', ICMS = 0
where substring(CST from 2 for 2) = '40';
with recursive itens as (
select id_nfce, sum(total_item) as total from nfce_itens where id_nfce in (
SELECT id FROM nfce where data_venda between '2020/09/01' and '2020/09/30' and nfce_status = 'AUTORIZADO'
) and cancelado = 'N' --and valor_total <> total_item
group by id_nfce
)
select a.*, itens.*, total - valor_final as diferenca
from (SELECT id, valor_final FROM nfce where data_venda between '2020/09/01' and '2020/09/30' and nfce_status = 'AUTORIZADO') a
left join itens on itens.id_nfce = a.id
where valor_final <> total
execute block
returns (teste int)
as
declare variable iID int;
declare variable idNUM int;
begin
iID = 0;
for select distinct id_cliente from clientes where id_cliente is not null order by id_cliente into :idNUM do
begin
iID = iID + 1;
while (iID < idNUM) do begin
TESTE = iID;
iID = iID + 1;
suspend;
end
end
end
merge into nfce a
using(select i.id_nfce, sum(i.total_item) as bc_icms,
sum((i.total_item * i.TAXA_ICMS) / 100) as icms from nfce_itens i
where i.TAXA_ICMS > 0 and i.CANCELADO = 'N' group by i.id_nfce) b
on (a.id = b.id_nfce)
when matched then update set
a.base_icms = b.bc_icms, a.icms = b.icms
update produtos set custo=custo *1.3
update produtos set margem_atac = x
update produtos set valor_atacado = x
update produtos set valor_atacado = custo*margem_atac
update produtos set valor_atacado = valor_venda*margem_atac
update produtos set margem = 40
update produtos set valor_venda = valor_venda *1.4
update PRODUTOS set ecf_icms_st = substring(icms from 1 for 2) where ST = 'T' and ecf_icms_st is null
update nfce_itens set icms = (base_icms * taxa_icms) / 100 where taxa_icms > 0 and cancelado = 'N'
update nfce set icms = (select sum(icms) from NFCE_ITENS i where i.cancelado = 'N' and i.id_nfce = nfce.id)
where id = (select id_nfce from NFCE_ITENS i where i.cancelado = 'N' and i.id_nfce = nfce.id group by id_nfce)
insert into DAV_TOTAL_TIPO_PGTO (ID_VENDA_CABECALHO, ID_TIPO_PAGAMENTO, VALOR, estorno)
select dav.id as id_venda_cabecalho, '12' as ID_TIPO_PAGAMENTO, dav.total_documento as
valor, 'N' as estorno from dav
left join DAV_TOTAL_TIPO_PGTO on DAV_TOTAL_TIPO_PGTO.ID_VENDA_CABECALHO = DAV.ID
where DAV_TOTAL_TIPO_PGTO.id is null
insert into NFCE_TOTAL_TIPO_PGTO (ID_VENDA_CABECALHO, ID_TIPO_PAGAMENTO, VALOR, estorno)
select nfce.id as id_venda_cabecalho, '12' as ID_TIPO_PAGAMENTO, nfce.total_documento as
valor, 'N' as estorno from NFCE
left join NFCE_TOTAL_TIPO_PGTO on NFCE_TOTAL_TIPO_PGTO.ID_VENDA_CABECALHO = NFCE.ID
where NFCE_TOTAL_TIPO_PGTO.id is null
update nfce set nfce_status = 'INUTILIZADO'
where nfce_numero = x
update nfce set nfce_status = 'INUTILIZADO'
where nfce_numero = x and nfce_serie = y
update nfce set nfce_status = 'INUTILIZADO'
where nfce_status = 'PROCESSADO'
(geralmente utilizado para reverter um cancelamento de uma nfce que está como autorizada, mas o cupom e itens está como cancelado
update nfce_total_tipo_pgto p set p.estorno = 'N' where p.id_venda_cabecalho in (
select n.id from NFCE n
where n.nfce_status = 'AUTORIZADO' and n.CUPOM_CANCELADO = 'S' and p.id_venda_cabecalho = n.id
)
update nfce_itens i set i.cancelado = 'N' where i.id_nfce in (
select n.id from NFCE n
where n.nfce_status = 'AUTORIZADO' and n.CUPOM_CANCELADO = 'S' and i.id_nfce = n.id
)
update nfce set CUPOM_CANCELADO = 'N', STATUS_VENDA = 'F'
where nfce_status = 'AUTORIZADO'
SELECT i.*, n.base_icms as bc_venda
FROM (
SELECT I.id_nfce, SUM(I.base_icms) AS BC_ITENS
FROM NFCE N
INNER JOIN NFCE_ITENS I ON I.id_nfce = N.ID
WHERE NFCE_NUMERO BETWEEN 416525 AND 417270 AND CUPOM_CANCELADO = 'N' AND
N.nfce_status IN ('AUTORIZADO', 'CONTIGENCIA') AND I.cancelado = 'N'
GROUP BY 1 ) i
inner join nfce n on n.id = i.id_nfce
where i.bc_itens <> n.base_icms
SELECT i.*, n.base_icms as bc_venda
FROM (
SELECT I.id_nfce, SUM(I.base_icms) AS BC_ITENS
FROM NFCE N
INNER JOIN NFCE_ITENS I ON I.id_nfce = N.ID
WHERE data_venda BETWEEN '2020-10-09' AND '2020-10-09' AND CUPOM_CANCELADO = 'N' AND
N.nfce_status IN ('AUTORIZADO', 'CONTIGENCIA') AND I.cancelado = 'N'
GROUP BY 1 ) i
inner join nfce n on n.id = i.id_nfce
where i.bc_itens <> n.base_icms
update PRODUTOS
set ST = 'F', CSOSN = 500, CST = '060', ECF_ICMS_ST = 'FF', ICMS = 0
where ICMS = 0
update PRODUTOS
set ST = 'T', CSOSN = 102, CST = '000', ECF_ICMS_ST = '18', ICMS = 18
where ICMS = 18
update PRODUTOS
set ST = 'I', CSOSN = 103, CST = '040', ECF_ICMS_ST = 'II', ICMS = 0
where ICMS = 0
update PRODUTOS
set ST = 'N', CSOSN = 400, CST = '041', ECF_ICMS_ST = 'NN', ICMS = 0
where ICMS = 0
ALTER TABLE NFCE ADD FCP_VALOR
NUMERIC ( 18, 2) DEFAULT 0, ADD FCP_VALORST
NUMERIC ( 18, 2) DEFAULT 0, ADD FCP_VALORRET
NUMERIC ( 18, 2) DEFAULT 0
ALTER TABLE NFCE_ITENS ADD FCP_TAXA_STRET NUMERIC( 18, 2)
ALTER TABLE NFCE_ITENS ADD ANP_GLP_PESO_LIQ NUMERIC( 18, 4)
merge into PRODUTOS b
using NFE_ITENS p
on p.id_produto = b.id_produto and p.id_nfe = X
when matched then
update set b.estoque = b.estoque + p.quantidade
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment