INSERT INTO [dbo].[TB02030](
				[TB02030_DTCAD], 
				[TB02030_OPCAD], 
				[TB02030_CONTRATO],
				[TB02030_BASEICMS], 
				[TB02030_BASEICMSSUB], 
				[TB02030_CODEMP], 
				[TB02030_CODFOR], 
				[TB02030_CODIGO], 
				[TB02030_CONDPAG], 
				[TB02030_DATA], 
				[TB02030_DTENT], 
				[TB02030_DESPADD], 
				[TB02030_OBS], 
				[TB02030_OBSADD], 
				[TB02030_PERCDESCONTO], 
				[TB02030_QTDE], 
				[TB02030_QTDER], 
				[TB02030_QTVOLUMES], 
				[TB02030_SITUACAO], 
				[TB02030_TRANSP], 
				[TB02030_VLRBRUTO], 
				[TB02030_VLRDESCONTO], 
				[TB02030_VLRFRETE], 
				[TB02030_VLRICMS], 
				[TB02030_VLRICMSSUB], 
				[TB02030_VLRIPI], 
				[TB02030_VLRNOTA], 
				[TB02030_VLRNOTAR], 
				[TB02030_VLROUTDESP], 
				[TB02030_NATUREZA], 
				[TB02030_TIPOTOTAL], 
				[TB02030_VLRICMSSUB2], 
				[TB02030_VLRBRUTO2], 
				[TB02030_VLRDESCONTO2], 
				[TB02030_VLRFRETE2], 
				[TB02030_VLRFRETE3], 
				[TB02030_VLRIPI2], 
				[TB02030_VLRNOTA2], 
				[TB02030_SOMASUB], 
				[TB02030_NOME], 
				[TB02030_CODCEN], 
				[TB02030_CODSUB], 
				[TB02030_PLANCON], 
				[TB02030_CONTABIL], 
				[TB02030_TIPOFRETE], 
				[TB02030_PESOBRUTO], 
				[TB02030_PESOLIQUIDO], 
				[TB02030_DATANOTA], 
				[TB02030_VLROUTDESP2], 
				[TB02030_BASEICMS2], 
				[TB02030_VLRICMS2], 
				[TB02030_STATUS], 
				[TB02030_SOMAFRETE], 
				[TB02030_OPCOM], 
				[TB02030_QTDEVOL], 
				[TB02030_QTDEVOLR], 
				[TB02030_DEVLOCACAO], 
				[TB02030_ORDEM], 
				[TB02030_KMINICIAL], 
				[TB02030_KMFINAL], 
				[TB02030_TIPOSUP], 
				[TB02030_USADO], 
				[TB02030_QTSERV], 
				[TB02030_VLRSERV], 
				[TB02030_VLRIMPOSTOS], 
				[TB02030_BASEPIS], 
				[TB02030_VLRPIS], 
				[TB02030_BASECOFINS],
				[TB02030_VLRCOFINS], 
				[TB02030_VLRCSLL], 
				[TB02030_VLRIR], 
				[TB02030_VLRISS], 
				[TB02030_VLRINSS], 
				[TB02030_VLRRETPIS],
				[TB02030_VLRRETCOFINS], 
				[TB02030_VLRRETCSLL], 
				[TB02030_VLRRETIR], 
				[TB02030_VLRRETISS], 
				[TB02030_VLRRETINSS], 
				[TB02030_DATACOTACAO], 
				[TB02030_NROCOTACAO], 
				[TB02030_TIPOPEDIDO],
				[TB02030_CODSITE],
				[TB02030_NUMERO])

			SELECT TOP 1
			    ----todos impostos 0
				GETDATE(),  --[TB02030_DTCAD], 
				'$user',    --[TB02030_OPCAD], 
				a.TB02112_CODIGO,   --[TB02030_CONTRATO],
				0,	--[TB02030_BASEICMS], 
				0,	--[TB02030_BASEICMSSUB], 
				TB02111_CODEMP,	--[TB02030_CODEMP], 
				NULL,	--[TB02030_CODFOR], Pegar do cliente pelo codCli
				Pedido.UltCont,	--[TB02030_CODIGO], 
				NULL,	--[TB02030_CONDPAG], Vamos pegar 
				GETDATE(),	--[TB02030_DATA], 
				GETDATE(),	--[TB02030_DTENT], 
				0,	--[TB02030_DESPADD], 
				'APP_CONTRATOS',	--[TB02030_OBS], 
				'APP_CONTRATOS',	--[TB02030_OBSADD], 
				0,	--[TB02030_PERCDESCONTO], 
				0,	--[TB02030_QTDE], 
				0,	--[TB02030_QTDER], 
				0,	--[TB02030_QTVOLUMES], 
				NULL, --[TB02030_SITUACAO], Vai pegar
				'0000',	--[TB02030_TRANSP], Apnet
				SUM(a.TB02112_CUSTO),	--[TB02030_VLRBRUTO], Soma custo 2112 ou 2054
				0,	--[TB02030_VLRDESCONTO], 
				0,	--[TB02030_VLRFRETE], 
				0,	--[TB02030_VLRICMS], 
				0,	--[TB02030_VLRICMSSUB], 
				0,	--[TB02030_VLRIPI], 
				0,	--[TB02030_VLRNOTA], Soma custo 2112 ou 2054
				0,--[TB02030_VLRNOTAR], 
				0,--[TB02030_VLROUTDESP], 
				NULL,	--[TB02030_NATUREZA], Vai pegar
				NULL,	--[TB02030_TIPOTOTAL], Fabricio gay
				0,	--[TB02030_VLRICMSSUB2], 
				0,	--[TB02030_VLRBRUTO2], 
				0,	--[TB02030_VLRDESCONTO2], 
				0,	--[TB02030_VLRFRETE2], 
				0,	--[TB02030_VLRFRETE3], 
				0,	--[TB02030_VLRIPI2], 
				0,	--[TB02030_VLRNOTA2], 
				0,	--[TB02030_SOMASUB], 
				'APNET',	--[TB02030_NOME], Nome do Fornecedor
				NULL,	--[TB02030_CODCEN], Fabricio gay
				NULL,	--[TB02030_CODSUB], Fabricio gay
				NULL,	--[TB02030_PLANCON], 
				'S',	--[TB02030_CONTABIL], 
				NULL,	--[TB02030_TIPOFRETE], 
				0,	--[TB02030_PESOBRUTO], 
				0,	--[TB02030_PESOLIQUIDO], 
				NULL,	--[TB02030_DATANOTA], 
				0,	--[TB02030_VLROUTDESP2], 
				0,	--[TB02030_BASEICMS2], 
				0,	--[TB02030_VLRICMS2], 
				NULL,	--[TB02030_STATUS], Fabricio gay
				0,--[TB02030_SOMAFRETE], 
				NULL, --[TB02030_OPCOM], Fabricio gay
				COUNT(a.TB02112_NUMSERIE),--[TB02030_QTDEVOL], Soma dos itens
				0,--[TB02030_QTDEVOLR], 
				0,--[TB02030_DEVLOCACAO], 
				0,--[TB02030_ORDEM], 
				0,--[TB02030_KMINICIAL], 
				0,--[TB02030_KMFINAL], 
				9,--[TB02030_TIPOSUP], Equip
				'S',	--[TB02030_USADO], ?????
				NUll,	--[TB02030_QTSERV], 
				0,	--[TB02030_VLRSERV], 
				0,	--[TB02030_VLRIMPOSTOS], 
				0,	--[TB02030_BASEPIS], 
				0,	--[TB02030_VLRPIS], 
				0,	--[TB02030_BASECOFINS],
				0,	--[TB02030_VLRCOFINS], 
				0,	--[TB02030_VLRCSLL], 
				0,	--[TB02030_VLRIR], 
				0,	--[TB02030_VLRISS], 
				0,	--[TB02030_VLRINSS], 
				0,	--[TB02030_VLRRETPIS],
				0,	--[TB02030_VLRRETCOFINS], 
				0,	--[TB02030_VLRRETCSLL], 
				0,	--[TB02030_VLRRETIR], 
				0,	--[TB02030_VLRRETISS], 
				0,	--[TB02030_VLRRETINSS], 
				GETDATE(),--[TB02030_DATACOTACAO], 
				NUll,	--[TB02030_NROCOTACAO], 
				NULL,	--[TB02030_TIPOPEDIDO],
				'000002',	--[TB02030_CODSITE], 
				0	--[TB02030_NUMERO]

				FROM TB02112 a
				LEFT JOIN TB02111 ON TB02111_CODIGO = a.TB02112_CODIGO
				
				OUTER APPLY(
					SELECT TOP 1 
						FORMAT(TB00002_COD+1,'000000') UltCont
					FROM TB00002 
					WHERE TB00002_TABELA = 'TB02030' 
					ORDER BY 1 DESC
				) AS Pedido
				WHERE TB02112_SITUACAO = 'A'
					AND TB02112_PAT IN ('027306', '027742', '028564')
					
				GROUP BY a.TB02112_CODIGO, TB02111_CODEMP, Pedido.UltCont

				UPDATE TB00002 
				SET TB00002_COD = (SELECT TOP 1 FORMAT(TB00002_COD+1,'000000') FROM TB00002 WHERE TB00002_TABELA = 'TB02030' ORDER BY 1 DESC)
				WHERE TB00002_TABELA = 'TB02030'

