-- script de geração de recálculo de estoque após migração para uso de almoxarifado -- atualização: 20/01/2026 declare @dt_inicial date = '01.01.2015' declare @dt_final date = '31.12.2015' -- faturamento origem 41 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 41 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_lancamento , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_notas_faturamento_itens a join tbl_notas_faturamento b on a.cd_lancamento = b.cd_lancamento join (select 41 cd_origem) o on o.cd_origem = 41 left join tbl_materiais_kardex_estoque est on a.cd_lancamento = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_lancamento = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_emissao between @dt_inicial and @dt_final and b.cd_empresa = 1 and (est.cd_id is null or alm.cd_item is null) union all -- emitidas saídas origem 43 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 43 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_nota , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_notas_emitidas_itens a join tbl_notas_emitidas b on a.cd_nota = b.cd_nota join (select 43 cd_origem) o on o.cd_origem = 43 join tbl_tipo_documentos c on b.cd_tipodocumento = c.cd_tipodocumento and c.cd_operacao = 2 left join tbl_materiais_kardex_estoque est on a.cd_nota = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_nota = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_emissao between @dt_inicial and @dt_final and b.cd_empresa = 1 and c.cd_operacao = 2 and (est.cd_id is null or alm.cd_item is null) union all -- emitidas entradas origem 2 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 2 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_nota , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_notas_emitidas_itens a join tbl_notas_emitidas b on a.cd_nota = b.cd_nota join (select 2 cd_origem) o on o.cd_origem = 2 join tbl_tipo_documentos c on b.cd_tipodocumento = c.cd_tipodocumento and c.cd_operacao = 1 left join tbl_materiais_kardex_estoque est on a.cd_nota = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_nota = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_emissao between @dt_inicial and @dt_final and b.cd_empresa = 1 and c.cd_operacao = 1 and (est.cd_id is null or alm.cd_item is null) union all -- compras origem 1 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 1 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_entrada , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_compras_notafiscal_entrada_itens a join tbl_compras_notafiscal_entrada b on a.cd_entrada = b.cd_entrada join (select 1 cd_origem) o on o.cd_origem = 1 left join tbl_materiais_kardex_estoque est on a.cd_entrada = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_entrada = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_entrada between @dt_inicial and @dt_final and b.cd_empresa = 1 and (est.cd_id is null or alm.cd_item is null) union all -- requisição saídas origem 42 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 42 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_requisicao , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_requisicao_itens a join tbl_requisicao b on a.cd_requisicao = b.cd_requisicao join (select 42 cd_origem) o on o.cd_origem = 42 left join tbl_materiais_kardex_estoque est on a.cd_requisicao = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_requisicao = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_emissao between @dt_inicial and @dt_final and b.cd_empresa = 1 and b.cd_operacao = 1 and (est.cd_id is null or alm.cd_item is null) union all -- requisição entradas origem 50 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 50 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_requisicao , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(dt_emissao, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_requisicao_itens a join tbl_requisicao b on a.cd_requisicao = b.cd_requisicao join (select 50 cd_origem) o on o.cd_origem = 50 left join tbl_materiais_kardex_estoque est on a.cd_requisicao = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_requisicao = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_emissao between @dt_inicial and @dt_final and b.cd_empresa = 1 and b.cd_operacao = 2 and (est.cd_id is null or alm.cd_item is null) union all -- contagens origem 100 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 100 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_lancamento , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(b.dt_lancamento, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_materiais_contagem_estoque_itens a join tbl_materiais_contagem_estoque b on a.cd_lancamento = b.cd_lancamento join (select 100 cd_origem) o on o.cd_origem = 100 left join tbl_materiais_kardex_estoque est on a.cd_lancamento = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_lancamento = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_lancamento between @dt_inicial and @dt_final and b.cd_empresa = 1 and (est.cd_id is null or alm.cd_item is null) union all -- produção consumo origem 46 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 46 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_entrada , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(b.dt_entrada, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_ordem_producao_consumo a join tbl_ordem_producao b on a.cd_entrada = b.cd_entrada join (select 46 cd_origem) o on o.cd_origem = 46 left join tbl_materiais_kardex_estoque est on a.cd_entrada = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_entrada = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_entrada between @dt_inicial and @dt_final and b.cd_empresa = 1 and (est.cd_id is null or alm.cd_item is null) union all -- produção produto produzido origem 4 -- declare @dt_inicial date = '01.01.2015', @dt_final date = '31.12.2015', @cd_origem int = 4 select concat('exec procedure_calcula_estoque @cd_empresa = ', b.cd_empresa , ', @cd_filial = ', b.cd_filial , ', @cd_origem = ', o.cd_origem , ', @cd_lancamento = ', a.cd_entrada , ', @cd_id = ', a.cd_id , ', @cd_material = ', a.cd_material , ', @dt_kardex = ', '''', format(b.dt_entrada, 'yyyyMMdd'), '''' , ', @x_estoque_almoxarifado = 1' , '; -- ', b.cd_filial ) script from tbl_ordem_producao_produto_produzido a join tbl_ordem_producao b on a.cd_entrada = b.cd_entrada join (select 4 cd_origem) o on o.cd_origem = 4 left join tbl_materiais_kardex_estoque est on a.cd_entrada = est.cd_lancamento and a.cd_id = est.cd_id_item_lancamento and est.cd_origem = o.cd_origem left join tbl_materiais_kardex_estoque_almoxarifado alm on a.cd_entrada = alm.cd_lancamento and a.cd_id = alm.cd_item and alm.cd_origem = o.cd_origem where b.dt_entrada between @dt_inicial and @dt_final and b.cd_empresa = 1 and (est.cd_id is null or alm.cd_item is null)