USE [APNET]
GO
/****** Object:  StoredProcedure [dbo].[CRIA_ORC_SUB_CLI]    Script Date: 16/12/2025 15:14:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CRIA_ORC_SUB_CLI]
	 @ContratoAnt VARCHAR(12),
	 @ContratoNov VARCHAR(12),
	 @CodCli VARCHAR(8),
	 @Pats VARCHAR(MAX),
	 @User VARCHAR(30),
	 @Operacao CHAR(2),
	 @Status CHAR(2)
	
AS 
BEGIN
	SET NOCOUNT ON;
DECLARE @NovOrc VARCHAR(7)
SET @NovOrc = (SELECT TOP 1
					FORMAT(CAST(SUBSTRING(TB00002_COD, 2, 6) AS NUMERIC) + 1, '000000') NovOrc
				FROM TB00002
				WHERE TB00002_TABELA = 'TB02018')

DECLARE @PatsTable TABLE (PAT VARCHAR(20));

	INSERT INTO @PatsTable (PAT)
	SELECT LTRIM(RTRIM(value))
	FROM STRING_SPLIT(@Pats, ',');

INSERT INTO [dbo].[TB02018]
           ([TB02018_DTCAD]
           ,[TB02018_OPCAD]
           ,[TB02018_DTALT]
           ,[TB02018_OPALT]
           ,[TB02018_BASEICMS]
           ,[TB02018_BASEICMSSUB]
           ,[TB02018_CODEMP]
           ,[TB02018_CODCLI]
           ,[TB02018_CODIGO]
           ,[TB02018_CONDPAG]
           ,[TB02018_CODTEC]
           ,[TB02018_DATA]
           ,[TB02018_DTENT]
           ,[TB02018_DEFEITO]
           ,[TB02018_DESPADD]
           ,[TB02018_EQUIP]
           ,[TB02018_ESPERCIE]
           ,[TB02018_HORASTRAB]
           ,[TB02018_HORASCOM]
           ,[TB02018_HORASFIM]
           ,[TB02018_LAUDO]
           ,[TB02018_MARCA]
           ,[TB02018_NTFISC]
           ,[TB02018_NUMERO]
           ,[TB02018_OBS]
           ,[TB02018_OBSADD]
           ,[TB02018_OBSINT]
           ,[TB02018_OPERACAO]
           ,[TB02018_PERCDESCONTO]
           ,[TB02018_PREST]
           ,[TB02018_QTDE]
           ,[TB02018_QTVOLUMES]
           ,[TB02018_RESPENT]
           ,[TB02018_SITUACAO]
           ,[TB02018_STATUS]
           ,[TB02018_TIPODESC]
           ,[TB02018_TRANSP]
           ,[TB02018_VEND]
           ,[TB02018_VLRBRUTO]
           ,[TB02018_VLRDESCONTO]
           ,[TB02018_VLRFRETE]
           ,[TB02018_VLRICMS]
           ,[TB02018_VLRICMSSUB]
           ,[TB02018_VLRIPI]
           ,[TB02018_VLRPREST]
           ,[TB02018_VLRNOTA]
           ,[TB02018_VLROUTDESP]
           ,[TB02018_VLRSERV]
           ,[TB02018_NATUREZA]
           ,[TB02018_NUMSERIE]
           ,[TB02018_POSCLI]
           ,[TB02018_CONTABIL]
           ,[TB02018_TIPOFRETE]
           ,[TB02018_PLACA]
           ,[TB02018_UFPLACA]
           ,[TB02018_PESOBRUTO]
           ,[TB02018_PESOLIQUIDO]
           ,[TB02018_DATANOTA]
           ,[TB02018_HORANOTA]
           ,[TB02018_NUMDAV]
           ,[TB02018_EMPDEST]
           ,[TB02018_VLRBACKUP]
           ,[TB02018_NOME]
           ,[TB02018_FONE]
           ,[TB02018_CODREV]
           ,[TB02018_COMPUPD]
           ,[TB02018_GERAUPD]
           ,[TB02018_VENDACONS]
           ,[TB02018_VLRDESCACU]
           ,[TB02018_PERCCOND]
           ,[TB02018_VLROVER]
           ,[TB02018_VLRIMPOSTOS]
           ,[TB02018_DATAEXEC]
           ,[TB02018_EMAIL]
           ,[TB02018_VLRFRETE2]
           ,[TB02018_VLROUTDESP2]
           ,[TB02018_CONTPB]
           ,[TB02018_CONTCOLOR]
           ,[TB02018_SPED]
           ,[TB02018_BASEPIS]
           ,[TB02018_BASECOFINS]
           ,[TB02018_VLRPIS]
           ,[TB02018_VLRCOFINS]
           ,[TB02018_VLRISS]
           ,[TB02018_NFSE]
           ,[TB02018_VLRCSLL]
           ,[TB02018_VLRIR]
           ,[TB02018_VLRRETPIS]
           ,[TB02018_VLRRETCOFINS]
           ,[TB02018_VLRRETCSLL]
           ,[TB02018_VLRRETIR]
           ,[TB02018_VLRRETISS]
           ,[TB02018_MODULO]
           ,[TB02018_CODDEF]
           ,[TB02018_COO]
           ,[TB02018_CODECF]
           ,[TB02018_CNPJ]
           ,[TB02018_NATUOPERSERV]
           ,[TB02018_CODTRIBUTACAO]
           ,[TB02018_SUBITEM]
           ,[TB02018_CLATENDE]
           ,[TB02018_MUNTRIBSERV]
           ,[TB02018_OS]
           ,[TB02018_VLRINSS]
           ,[TB02018_VLRRETINSS]
           ,[TB02018_CONTRATO]
           ,[TB02018_VLRDIFALIQ]
           ,[TB02018_VLRICMSINT]
           ,[TB02018_VLRICMSEXT]
           ,[TB02018_VLRICMSFCP]
           ,[TB02018_URGENCIA]
           ,[TB02018_CAUCAO]
           ,[TB02018_VLRCAUCAO]
           ,[TB02018_NDD]
           ,[TB02018_NDDREMESSA]
           ,[TB02018_NDDTIPOREMESSA]
           ,[TB02018_NDDPEDIDO]
           ,[TB02018_PROJETO]
           ,[TB02018_CODSITE]
           ,[TB02018_CODDEPTO]
           ,[TB02018_CONTTOTAL]
           ,[TB02018_ORDEM]
           ,[TB02018_HRDESLOC]
           ,[TB02018_KMINICIAL]
           ,[TB02018_KMFINAL]
           ,[TB02018_BASEFCPTOT]
           ,[TB02018_VLRFCPTOT]
           ,[TB02018_BASEFCPTOTST]
           ,[TB02018_VLRFCPTOTST]
           ,[TB02018_CODEMPREST]
           ,[TB02018_CODORIGINAL]
           ,[TB02018_DTVALIDADE]
           ,[TB02018_VLRICMSDIF]
           ,[TB02018_VLRICMSDESON]
           ,[TB02018_EMTRANSITO]
           ,[TB02018_VLRBCIPI]
           ,[TB02018_COTACAO]
           ,[TB02018_NUMCOTACAO]
           ,[TB02018_DTCOTACAO]
           ,[TB02018_PRECONTRATO]
           ,[TB02018_CODEMPANTERIOR]
           ,[TB02018_VLRDESCBENEF])
     SELECT TOP 1
           GETDATE(), --[TB02018_DTCAD]
           @User, --[TB02018_OPCAD]
           NULL, --[TB02018_DTALT]
           NULL, --[TB02018_OPALT]
           0, --[TB02018_BASEICMS]
           0, --[TB02018_BASEICMSSUB]
           TB02111_CODEMP, --[TB02018_CODEMP]
           @CodCli, --[TB02018_CODCLI]
           @NovOrc,	--[TB02018_CODIGO]
           '000',	--[TB02018_CONDPAG]
           NULL,	--[TB02018_CODTEC]
           GETDATE(),	--[TB02018_DATA]
           GETDATE(),	--[TB02018_DTENT]
           NULL,	--[TB02018_DEFEITO]
           NULL,	--[TB02018_DESPADD]
           NULL,	--[TB02018_EQUIP]
           NULL,	--[TB02018_ESPERCIE]
           NULL,	--[TB02018_HORASTRAB]
           NULL,	--[TB02018_HORASCOM]
           NULL,	--[TB02018_HORASFIM]
           NULL,	--[TB02018_LAUDO]
           NULL,	--[TB02018_MARCA]
           NULL,	--[TB02018_NTFISC]
           0,	--[TB02018_NUMERO]
           'APP_SUBS',	--[TB02018_OBS]
           'APP_SUBS',	--[TB02018_OBSADD]
           'APP_SUBS',	--[TB02018_OBSINT]
           '00',	--[TB02018_OPERACAO]
           0,	--[TB02018_PERCDESCONTO]
           NULL,	--[TB02018_PREST]
           Itens.Qtde,	--[TB02018_QTDE]
           Itens.Qtde,	--[TB02018_QTVOLUMES]
           NULL,	--[TB02018_RESPENT]
           'A',	--[TB02018_SITUACAO]
           @Status,	--[TB02018_STATUS]
		   @Operacao, --[TB02018_TIPODESC]
		  '0000', --[TB02018_TRANSP]
           '0000', --[TB02018_VEND]
           0, --[TB02018_VLRBRUTO]
           0, --[TB02018_VLRDESCONTO]
           0, --[TB02018_VLRFRETE]
           0, --[TB02018_VLRICMS]
           0, --[TB02018_VLRICMSSUB]
           0, --[TB02018_VLRIPI]
           0, --[TB02018_VLRPREST]
           Itens.Vlrtotal, --[TB02018_VLRNOTA]
           0, --[TB02018_VLROUTDESP]
           0, --[TB02018_VLRSERV]
           NULL, --[TB02018_NATUREZA]
           NULL, --[TB02018_NUMSERIE]
           'S', --[TB02018_POSCLI]
           'N', --[TB02018_CONTABIL]
           0, --[TB02018_TIPOFRETE]
           NULL, --[TB02018_PLACA]
           NULL, --[TB02018_UFPLACA]
           0, --[TB02018_PESOBRUTO]
           0,	--[TB02018_PESOLIQUIDO]
           NULL, --[TB02018_DATANOTA]
           NULL, --[TB02018_HORANOTA]
           NULL, --[TB02018_NUMDAV]
           TB02111_CODEMP, --[TB02018_EMPDEST]
           0, --[TB02018_VLRBACKUP]
           TB01007_NOME, --[TB02018_NOME]
           NULL, --[TB02018_FONE]
           NULL, --[TB02018_CODREV]
           NULL, --[TB02018_COMPUPD]
           NULL, --[TB02018_GERAUPD]
           'N', --[TB02018_VENDACONS]
           0, --[TB02018_VLRDESCACU]
           0, --[TB02018_PERCCOND]
           0, --[TB02018_VLROVER]
           0, --[TB02018_VLRIMPOSTOS]
           0, --[TB02018_DATAEXEC]
           TB02111_EMAIL, --[TB02018_EMAIL]
           0, --[TB02018_VLRFRETE2]
           0, --[TB02018_VLROUTDESP2]
           0, --[TB02018_CONTPB]
           0, --[TB02018_CONTCOLOR]
           0, --[TB02018_SPED]
           0, --[TB02018_BASEPIS]
           0, --[TB02018_BASECOFINS]
           0, --[TB02018_VLRPIS]
           0, --[TB02018_VLRCOFINS]
           0, --[TB02018_VLRISS]
           NULL, --[TB02018_NFSE]
           0, --[TB02018_VLRCSLL]
           0, --[TB02018_VLRIR]
           0, --[TB02018_VLRRETPIS]
           0, --[TB02018_VLRRETCOFINS]
           0, --[TB02018_VLRRETCSLL]
           0, --[TB02018_VLRRETIR]
           0, --[TB02018_VLRRETISS]
           NULL, --[TB02018_MODULO]
           NULL, --[TB02018_CODDEF]
           NULL, --[TB02018_COO]
           NULL, --[TB02018_CODECF]
           TB01007_CNPJ, --[TB02018_CNPJ]
           NULL, --[TB02018_NATUOPERSERV]
           NULL, --[TB02018_CODTRIBUTACAO]
           NULL, --[TB02018_SUBITEM]
           '0000', --[TB02018_CLATENDE]
           NULL, --[TB02018_MUNTRIBSERV]
           NULL, --[TB02018_OS]
           0, --[TB02018_VLRINSS]
           0, --[TB02018_VLRRETINSS]
		   TB02111_CODIGO,	--[TB02018_CONTRATO]
           0, --[TB02018_VLRDIFALIQ]
           0, --[TB02018_VLRICMSINT]
           0, --[TB02018_VLRICMSEXT]
           0, --[TB02018_VLRICMSFCP]
           NULL, --[TB02018_URGENCIA]
           NULL, --[TB02018_CAUCAO]
           0, --[TB02018_VLRCAUCAO]
           NULL, --[TB02018_NDD]
           NULL, --[TB02018_NDDREMESSA]
           NULL, --[TB02018_NDDTIPOREMESSA]
           NULL, --[TB02018_NDDPEDIDO]
           NULL, --[TB02018_PROJETO]
           Itens.TB02112_CODSITE,  --[TB02018_CODSITE]
           NULL, --[TB02018_CODDEPTO]
           0, --[TB02018_CONTTOTAL]
           NULL, --[TB02018_ORDEM]
           NULL, --[TB02018_HRDESLOC]
           NULL, --[TB02018_KMINICIAL]
           NULL, --[TB02018_KMFINAL]
           0, --[TB02018_BASEFCPTOT]
           0, --[TB02018_VLRFCPTOT]
           0, --[TB02018_BASEFCPTOTST]
           0, --[TB02018_VLRFCPTOTST]
           NULL, --[TB02018_CODEMPREST]
           NULL, --[TB02018_CODORIGINAL]
           TB02111_DTVALIDADE, --[TB02018_DTVALIDADE]
           0, --[TB02018_VLRICMSDIF]
           0, --[TB02018_VLRICMSDESON]
           NULL, --[TB02018_EMTRANSITO]
           0, --[TB02018_VLRBCIPI]
           NULL, --[TB02018_COTACAO]
           NULL, --[TB02018_NUMCOTACAO]
           GETDATE(), --[TB02018_DTCOTACAO]
           TB02111_PRECONTRATO, --[TB02018_PRECONTRATO]
           NULL, --[TB02018_CODEMPANTERIOR]
           0  --[TB02018_VLRDESCBENEF]
	 FROM TB02111
	 LEFT JOIN TB01007 ON TB01007_CODIGO = TB02111_CODEMP
      OUTER APPLY(
				SELECT 
					COUNT(TB02112_PAT) Qtde,
					SUM(TB02112_CUSTO) Vlrtotal,
					TB02112_CODSITE
				FROM TB02112
				WHERE TB02112_CODIGO = @ContratoAnt
				AND TB02112_PAT IN (SELECT PAT FROM @PatsTable)
				--AND TB02112_SITUACAO  = 'A'
				GROUP BY TB02112_CODSITE
		   ) AS Itens
		   WHERE TB02111_CODIGO = @ContratoAnt
		  -- AND TB02111_SITUACAO = 'A'


	 

INSERT INTO [dbo].[TB02019]
           ([TB02019_DTCAD]
           ,[TB02019_OPCAD]
           ,[TB02019_DTALT]
           ,[TB02019_OPALT]
           ,[TB02019_CODIGO]
           ,[TB02019_CUSTO]
           ,[TB02019_PERDESC]
           ,[TB02019_PRODUTO]
           ,[TB02019_PRUNIT]
           ,[TB02019_QTPROD]
           ,[TB02019_QTPRODB]
           ,[TB02019_SITUACAO]
           ,[TB02019_TOTVALOR]
           ,[TB02019_TOTVALORB]
           ,[TB02019_NUMSERIE]
           ,[TB02019_CODEMP]
           ,[TB02019_VLRDESC]
           ,[TB02019_VLRDESCB]
           ,[TB02019_ICMS]
           ,[TB02019_VLRICMS]
           ,[TB02019_VLRICMSB]
           ,[TB02019_NATUREZA]
           ,[TB02019_PERCIPI]
           ,[TB02019_VLRIPI]
           ,[TB02019_VLRIPIB]
           ,[TB02019_PERCST]
           ,[TB02019_VLRST]
           ,[TB02019_VLRSTB]
           ,[TB02019_PRBACKUP]
           ,[TB02019_TIPODESC]
           ,[TB02019_BASERED]
           ,[TB02019_BASEICMS]
           ,[TB02019_BASEICMSB]
           ,[TB02019_BASEST]
           ,[TB02019_BASESTB]
           ,[TB02019_COMISSAO]
           ,[TB02019_TABELA]
           ,[TB02019_CUSTOCOMPRA]
           ,[TB02019_DTENT]
           ,[TB02019_VLRDESCACU]
           ,[TB02019_IPIICMS]
           ,[TB02019_VLRFRETE]
           ,[TB02019_VLRFRETEB]
           ,[TB02019_VLROUTDESP]
           ,[TB02019_VLROUTDESPB]
           ,[TB02019_EQUIP]
           ,[TB02019_ITEM]
           ,[TB02019_DESCPROD]
           ,[TB02019_UNPROD]
           ,[TB02019_ACRESCIMO]
           ,[TB02019_CODALIQECF]
           ,[TB02019_ATIVO]
           ,[TB02019_ICMSDENTRO]
           ,[TB02019_VLRICMSDENTRO]
           ,[TB02019_VLRICMSDENTROB]
           ,[TB02019_ICMSFORA]
           ,[TB02019_VLRICMSFORA]
           ,[TB02019_VLRICMSFORAB]
           ,[TB02019_VLRDIFALIQ]
           ,[TB02019_VLRDIFALIQB]
           ,[TB02019_FCP]
           ,[TB02019_VLRFCP]
           ,[TB02019_VLRFCPB]
           ,[TB02019_PARTILHAINT]
           ,[TB02019_PARTILHAEXT]
           ,[TB02019_BASEFCPTOT]
           ,[TB02019_BASEFCPTOTB]
           ,[TB02019_VLRFCPTOT]
           ,[TB02019_VLRFCPTOTB]
           ,[TB02019_BASEFCPTOTST]
           ,[TB02019_BASEFCPTOTSTB]
           ,[TB02019_VLRFCPTOTST]
           ,[TB02019_VLRFCPTOTSTB]
           ,[TB02019_FCPST]
           ,[TB02019_VLRICMSULT]
           ,[TB02019_PERCDIF]
           ,[TB02019_VLRICMSDIF]
           ,[TB02019_VLRICMSDIFB]
           ,[TB02019_VLRICMSDESON]
           ,[TB02019_VLRICMSDESONB]
           ,[TB02019_NUMPED]
           ,[TB02019_ITEMPED]
           ,[TB02019_VLRBCIPI]
           ,[TB02019_VLRBCIPIB]
           ,[TB02019_ALIQCSLLRET]
           ,[TB02019_ALIQCOFINSRET]
           ,[TB02019_ALIQINSSRET]
           ,[TB02019_ALIQIRRET]
           ,[TB02019_ALIQPISRET]
           ,[TB02019_RETCSLL]
           ,[TB02019_RETCOFINS]
           ,[TB02019_RETINSS]
           ,[TB02019_RETIR]
           ,[TB02019_RETPIS]
           ,[TB02019_VLRCSLLRET]
           ,[TB02019_VLRCSLLRETB]
           ,[TB02019_VLRCOFINSRET]
           ,[TB02019_VLRCOFINSRETB]
           ,[TB02019_VLRINSSRET]
           ,[TB02019_VLRINSSRETB]
           ,[TB02019_VLRIRRET]
           ,[TB02019_VLRIRRETB]
           ,[TB02019_VLRPISRET]
           ,[TB02019_VLRPISRETB]
           ,[TB02019_ALIQIMPFAT]
           ,[TB02019_VLRDESCBENEF]
           ,[TB02019_VLRDESCBENEFB])
     SELECT DISTINCT
           GETDATE(), --[TB02019_DTCAD]
           @User, --[TB02019_OPCAD]
           NULL, --[TB02019_DTALT]
           NULL, --[TB02019_OPALT]
           @NovOrc, --[TB02019_CODIGO]
           COUNT(TB02112_PRODUTO) * TB01010_CUSTO, --[TB02019_CUSTO]
           0, --[TB02019_PERDESC]
           TB02112_PRODUTO, --[TB02019_PRODUTO]
           TB01010_CUSTO, --[TB02019_PRUNIT]
           COUNT(TB02112_PRODUTO), --[TB02019_QTPROD]
           COUNT(TB02112_PRODUTO), --[TB02019_QTPRODB]
           'A', --[TB02019_SITUACAO]
           COUNT(TB02112_PRODUTO) * TB01010_CUSTO, --[TB02019_TOTVALOR]
           COUNT(TB02112_PRODUTO) * TB01010_CUSTO, --[TB02019_TOTVALORB]
           NULL, --[TB02019_NUMSERIE]
           TB02111_CODEMP, --[TB02019_CODEMP]
           0, --[TB02019_VLRDESC]
           0, --[TB02019_VLRDESCB]
           0, --[TB02019_ICMS]
           0, --[TB02019_VLRICMS]
           0, --[TB02019_VLRICMSB]
           NULL, --[TB02019_NATUREZA]
           0, --[TB02019_PERCIPI]
           0, --[TB02019_VLRIPI]
           0, --[TB02019_VLRIPIB]
           0, --[TB02019_PERCST]
           0, --[TB02019_VLRST]
           0, --[TB02019_VLRSTB]
           0, --[TB02019_PRBACKUP]
           TB02112_TIPODESC, --[TB02019_TIPODESC]
           0, --[TB02019_BASERED]
           0, --[TB02019_BASEICMS]
           0, --[TB02019_BASEICMSB]
           0, --[TB02019_BASEST]
           0, --[TB02019_BASESTB]
           0, --[TB02019_COMISSAO]
           '00', --[TB02019_TABELA]
           COUNT(TB02112_PRODUTO) * TB01010_CUSTOCOMPRA, --[TB02019_CUSTOCOMPRA]
           GETDATE(), --[TB02019_DTENT]
           0, --[TB02019_VLRDESCACU]
           0, --[TB02019_IPIICMS]
           0, --[TB02019_VLRFRETE]
           0, --[TB02019_VLRFRETEB]
           0, --[TB02019_VLROUTDESP]
           0, --[TB02019_VLROUTDESPB]
           NULL, --[TB02019_EQUIP]
           0, --[TB02019_ITEM]
           NULL, --[TB02019_DESCPROD]
           TB01010_UNPROD, --[TB02019_UNPROD]
           0, --[TB02019_ACRESCIMO]
           NULL, --[TB02019_CODALIQECF]
           'S', --[TB02019_ATIVO]
           0, --[TB02019_ICMSDENTRO]
           0, --[TB02019_VLRICMSDENTRO]
           0, --[TB02019_VLRICMSDENTROB]
           0, --[TB02019_ICMSFORA]
           0, --[TB02019_VLRICMSFORA]
           0, --[TB02019_VLRICMSFORAB]
           0, --[TB02019_VLRDIFALIQ]
           0, --[TB02019_VLRDIFALIQB]
           0, --[TB02019_FCP]
           0, --[TB02019_VLRFCP]
           0, --[TB02019_VLRFCPB]
           0, --[TB02019_PARTILHAINT]
           0, --[TB02019_PARTILHAEXT]
           0, --[TB02019_BASEFCPTOT]
           0, --[TB02019_BASEFCPTOTB]
           0, --[TB02019_VLRFCPTOT]
           0, --[TB02019_VLRFCPTOTB]
           0, --[TB02019_BASEFCPTOTST]
           0, --[TB02019_BASEFCPTOTSTB]
           0, --[TB02019_VLRFCPTOTST]
           0, --[TB02019_VLRFCPTOTSTB]
           0, --[TB02019_FCPST]
           0, --[TB02019_VLRICMSULT]
           0, --[TB02019_PERCDIF]
           0, --[TB02019_VLRICMSDIF]
           0, --[TB02019_VLRICMSDIFB]
           0, --[TB02019_VLRICMSDESON]
           0, --[TB02019_VLRICMSDESONB]
           NULL, --[TB02019_NUMPED]
           0, --[TB02019_ITEMPED]
           0, --[TB02019_VLRBCIPI]
           0, --[TB02019_VLRBCIPIB]
           0, --[TB02019_ALIQCSLLRET]
           0, --[TB02019_ALIQCOFINSRET]
           0, --[TB02019_ALIQINSSRET]
           0, --[TB02019_ALIQIRRET]
           0, --[TB02019_ALIQPISRET]
           0, --[TB02019_RETCSLL]
           0, --[TB02019_RETCOFINS]
           0, --[TB02019_RETINSS]
           0, --[TB02019_RETIR]
           0, --[TB02019_RETPIS]
           0, --[TB02019_VLRCSLLRET]
           0, --[TB02019_VLRCSLLRETB]
           0, --[TB02019_VLRCOFINSRET]
           0, --[TB02019_VLRCOFINSRETB]
           0, --[TB02019_VLRINSSRET]
           0, --[TB02019_VLRINSSRETB]
           0, --[TB02019_VLRIRRET]
           0, --[TB02019_VLRIRRETB]
           0, --[TB02019_VLRPISRET]
           0, --[TB02019_VLRPISRETB]
           0, --[TB02019_ALIQIMPFAT]
           0, --[TB02019_VLRDESCBENEF]
           0 --[TB02019_VLRDESCBENEFB]
		FROM TB02112 A
		LEFT JOIN TB02111 ON TB02111_CODIGO = A.TB02112_CODIGO
		LEFT JOIN TB01010 ON TB01010_CODIGO = TB02112_PRODUTO
		WHERE  A.TB02112_PAT IN (SELECT PAT FROM @PatsTable)
        AND A.TB02112_CODIGO = @ContratoAnt
        --AND A.TB02112_SITUACAO = 'A'
		GROUP BY TB01010_CUSTO, TB02112_PRODUTO, TB02111_CODEMP, TB02112_TIPODESC, TB01010_CUSTOCOMPRA, TB01010_UNPROD



		
	UPDATE 
		TB00002 
	SET 
		TB00002_cod = @NovOrc 
	WHERE 
		TB00002_tabela = 'TB02018'


	PRINT @NovOrc;
	SELECT @NovOrc AS NovoOrcamento;

END
