SET TRANSACTION ISOLATION LEVEL SERIALIZABLE 
DECLARE @DEAD INT SET @DEAD=1 WHILE @DEAD=1 BEGIN BEGIN TRY BEGIN TRANSACTION COMPRA 
SET DATEFORMAT YMD 
DECLARE
 @MONTO        DECIMAL(28,2),
 @MONTOTAX     DECIMAL(28,2),
 @EXISTANT     DECIMAL(28,3),
 @EXISTANTUND  DECIMAL(28,3),
 @NUMEROCOM VARCHAR(20),
 @NUMERODEB VARCHAR(20),
 @NUMERORET VARCHAR(20),
 @NUMERORETIVA VARCHAR(20),
 @NROUNICO INT,
 @NROUNICOCXP INT,
 @NROUNICOLOT INT,
 @NROUNICORET INT,
 @NROUNICORETREV INT,
 @NROUNICONDB INT,
 @NROUNICORETIVA INT,
 @UCOSTOACT DECIMAL(28,3),
 @UCOSTOPRO DECIMAL(28,3),
 @UCOSTOANT DECIMAL(28,3),
 @NCOSTOACT DECIMAL(28,3),
 @NCOSTOPRO DECIMAL(28,3),
 @NCOSTOANT DECIMAL(28,3),
 @NROREGISERI INT,
 @FINISHPO  INT,
 @NUMERRORS INT;
SET @NUMERRORS=0;
SET @NUMEROCOM='554433'
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SASERV WITH (ROWLOCK) SET Costo=100000000,FechaUC='2021-03-31 13:30:27.001' WHERE (CodServ='S01')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
IF NOT EXISTS(SELECT * FROM SAESRV WITH (NOLOCK) WHERE (CodServ='S01') And (Periodo='202103')) 
INSERT INTO SAESRV ([CodServ], [Periodo]) VALUES ('S01', '202103')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SAESRV WITH (ROWLOCK) SET [MtoCompra]=[MtoCompra]+100000000, [CntCompra]=[CntCompra]+1 WHERE (CodServ='S01') And (Periodo='202103')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SAITEMCOM ([Signo], [CodProv], [TipoCom], [NumeroD], [NroLinea], [FechaE], [CodItem], [CodUbic], [Descrip1], [Cantidad], [Costo], [Precio1], [Precio2], [Precio3], [TotalItem], [EsServ], [EsFreeP], [NroUnicoL], [FechaL], [ExistAntU], [ExistAnt]) VALUES (1, 'J309849697', 'H', @NUMEROCOM, 1, '2021-03-31 13:30:27.001', 'S01', '01', '?SERVICIO', 1, 100000000, 100000000, 100000000, 100000000, 100000000, 1, 1, ISNULL(@NROUNICOLOT,0), '2021-03-31 13:30:27.037', ISNULL(@EXISTANTUND,0), ISNULL(@EXISTANT,0))
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SATAXITC ([TipoCom], [NumeroD], [CodTaxs], [CodProv], [CodItem], [TGravable], [MtoTax], [Monto], [NroLinea]) VALUES ('H', @NUMEROCOM, 'IVA', 'J309849697', 'S01', 100000000, 16, 16000000, 1)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SACOMP ([Signo], [TipoCom], [CodSucu], [CodUsua], [CodEsta], [FechaT], [NumeroD], [CodProv], [CodUbic], [Descrip], [Factor], [Direc1], [ID3], [FechaI], [FechaE], [FechaV], [Monto], [MtoTax], [RetenIVA], [TGravable], [TotalSrv], [MtoTotal], [CancelI], [Credito]) VALUES (1, 'H', '00000', '001', 'DESKTOP-6J', GETDATE(), @NUMEROCOM, 'J309849697', '01', 'MOL SYSTEM, C.A.', 1, 'MICHELENA', 'J309849697', '2021-03-31 13:30:27.077', '2021-03-31 13:30:27.116', '2021-03-31 13:30:27.147', 100000000, 16000000, 12000000, 100000000, 100000000, 104000000, 3000000, 104000000)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SATAXCOM ([TipoCom], [NumeroD], [CodProv], [CodTaxs], [MtoTax], [Monto], [TGravable]) VALUES ('H', @NUMEROCOM, 'J309849697', 'IVA', 16, 16000000, 100000000)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SACOMP WITH (ROWLOCK)
   SET SaldoAct=C.SALDO,
       MtoPagos=T.MTOPAGOS,
       MtoNCredito=T.MTOCREDITOS,
       MtoNDebito=T.MTODEBITOS
  FROM SACOMP F
 INNER JOIN SAPROV C ON
  C.CODPROV=F.CODPROV
 INNER JOIN (SELECT CODPROV,
                SUM(CASE SUBSTRING(TIPOCXP,1,1) WHEN '4' THEN SALDO ELSE 0 END) AS MTOPAGOS,
                SUM(CASE SUBSTRING(TIPOCXP,1,1) WHEN '2' THEN SALDO ELSE 0 END) AS MTOCREDITOS,
                SUM(CASE SUBSTRING(TIPOCXP,1,1) WHEN '3' THEN SALDO ELSE 0 END) AS MTODEBITOS
               FROM SAACXP WITH (NOLOCK)
              WHERE CODPROV='J309849697' AND '2021-03-01'<=FECHAE AND 
                    FECHAE<='2021-03-31' 
           GROUP BY CODPROV) T ON 
  T.CODPROV=F.CODPROV 
WHERE (F.CodProv='J309849697') And (F.TipoCom='H') And (F.NumeroD=@NUMEROCOM)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
IF NOT EXISTS(SELECT * FROM SAECOM WITH (NOLOCK) WHERE (CodSucu='00000') And (Periodo='20210331')) 
INSERT INTO SAECOM ([CodSucu], [Periodo]) VALUES ('00000', '20210331')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SAECOM WITH (ROWLOCK) SET [MtoCompra]=[MtoCompra]+100000000, [MtoTax]=[MtoTax]+16000000, [Contado]=[Contado]+0, [Credito]=[Credito]+101000000, [Descto]=[Descto]+0, [Fletes]=[Fletes]+0, [NroComps]=[NroComps]+1, [NroDevol]=[NroDevol]+0 WHERE (CodSucu='00000') And (Periodo='20210331')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SAACXP ([CodProv], [NumeroD], [CodSucu], [CodUsua], [CodEsta], [TipoCxP], [Descrip], [ID3], [FechaT], [Document], [FechaI], [FechaE], [FechaV], [MontoNeto], [Factor], [Monto], [Saldo], [MtoTax], [RetenIVA], [BaseImpo], [OrgTax], [SaldoOrg], [EsLibroI]) VALUES ('J309849697', '554433', '00000', '001', 'DESKTOP-6J', '10', 'MOL SYSTEM, C.A.', 'J309849697', GETDATE(), '554433 ', '2021-03-31 13:30:27.179', '2021-03-31 13:30:27.211', '2021-03-31 13:30:27.243', 100000000, 1, 116000000, 101000000, 16000000, 12000000, 100000000, 16000000, 101000000, 1)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
SET @NROUNICOCXP=IDENT_CURRENT('SAACXP')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SATAXCXP ([NroPpal], [CodTaxs], [MtoTax], [Monto], [TGravable]) VALUES (@NROUNICOCXP, 'IVA', 16, 16000000, 100000000);
SET @NUMERRORS=@NUMERRORS+@@ERROR;
EXEC SP_ADM_PROXCORREL '00000',NULL,'PrxRetenIVA',@NUMERORETIVA OUTPUT;
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SAACXP ([NroRegi], [TipoCxP], [CodProv], [NumeroD], [NumeroN], [CodSucu], [CodUsua], [CodEsta], [Document], [FechaT], [FechaI], [FechaE], [FechaV], [Factor], [Monto], [Descrip], [ID3], [RetenIVA], [BaseImpo], [MtoTax], [MontoNeto], [EsReten]) VALUES (@NroUnicoCxp, '81', 'J309849697', @NUMERORETIVA, '554433', '00000', '001', 'DESKTOP-6J', 'RET. IVA DOC.: 554433', GETDATE(), '2021-03-31 13:30:27.274', '2021-03-31 13:30:27.306', '2021-03-31 13:30:27.337', 1, 12000000, 'MOL SYSTEM, C.A.', 'J309849697', 12000000, 100000000, 16000000, 100000000, 1)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
SET @NROUNICORETIVA=IDENT_CURRENT('SAACXP')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SAPAGCXP ([NroPpal], [NroRegi], [TipoCxP], [MontoDocA], [Monto], [NumeroD], [Descrip], [FechaE], [FechaO], [EsReten]) VALUES (@NroUnicoRETIVA, @NroUnicoCxp, '81', 16000000, 12000000, '554433', 'RET. IVA DOC.: 554433', '2021-03-31 13:30:27.369', '2021-03-31 13:30:27.400', 1)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SACOMP WITH (ROWLOCK) SET NumeroR=@NUMERORETIVA WHERE (CodProv='J309849697') And (TipoCom='H') And (NumeroD='554433')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
EXEC SP_ADM_PROXCORREL '00000',NULL,'PrxImpue',@NUMERORET OUTPUT;
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SAACXP ([NroRegi], [TipoCxP], [CodProv], [NumeroD], [NumeroN], [CodSucu], [CodUsua], [CodEsta], [Document], [FechaT], [FechaI], [FechaE], [FechaV], [Factor], [Monto], [Descrip], [ID3], [MtoTax], [BaseImpo], [EsLibroI], [EsReten]) VALUES (@NroUnicoCxp, '21', 'J309849697', @NUMERORET, '554433', '00000', '001', 'DESKTOP-6J', 'RETENCIONES', GETDATE(), '2021-03-31 13:30:27.431', '2021-03-31 13:30:27.463', '2021-03-31 13:30:27.495', 1, 3000000, 'MOL SYSTEM, C.A.', 'J309849697', 16000000, 100000000, 1, 1)
SET @NUMERRORS=@NUMERRORS+@@ERROR;
SET @NROUNICORET=IDENT_CURRENT('SAACXP')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
INSERT INTO SAPAGCXP ([NroPpal], [NroRegi], [TipoCxP], [MontoDocA], [Monto], [NumeroD], [Descrip], [FechaE], [FechaO], [EsReten], [BaseReten], [CodRete], [CodOper]) VALUES (@NroUnicoRET, @NroUnicoCXP, '10', 100000000, 3000000, @NUMERORET, 'RETENCION ISLR', '2021-03-31 13:30:27.526', '2021-03-31 13:30:27.558', 1, 100000000, 'R01', 'R01')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SAPROV WITH (ROWLOCK) SET [Saldo]=[Saldo]+101000000, [PagosA]=[PagosA]+0, FechaUC='2021-03-31', MontoUC=100000000, NumeroUC='554433', [RetenIVA]=[RetenIVA]+3000000 WHERE (CodProv='J309849697')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
IF NOT EXISTS(SELECT * FROM SAEPRV WITH (NOLOCK) WHERE (CodProv='J309849697') And (Periodo='202103')) 
INSERT INTO SAEPRV ([CodProv], [Periodo]) VALUES ('J309849697', '202103')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
UPDATE SAEPRV WITH (ROWLOCK) SET [NroComps]=[NroComps]+1, [NroDevol]=[NroDevol]+0, [Credito]=[Credito]+-3000000, [Contado]=[Contado]+3000000, [MtoDevol]=[MtoDevol]+0, [MtoRetenImp]=[MtoRetenImp]+3000000 WHERE (CodProv='J309849697') And (Periodo='202103')
SET @NUMERRORS=@NUMERRORS+@@ERROR;
IF @NUMERRORS>0 BEGIN ROLLBACK TRANSACTION COMPRA SET @DEAD=0 END 
ELSE BEGIN COMMIT  TRANSACTION COMPRA; SET @DEAD=0 END 
END TRY BEGIN CATCH  ROLLBACK TRANSACTION COMPRA SET @DEAD=0 IF ERROR_NUMBER()=1205 SET @DEAD=1  END CATCH END 
SELECT @NUMERRORS AS ERROR, ISNULL(@NUMEROCOM,'') AS NUMEROD, ISNULL(@NROUNICORET,0) AS NROUNICORET, ISNULL(@NROUNICORETREV,0) AS NROUNICORETREV, ISNULL(@NROUNICONDB,0) AS NROUNICONDB, ISNULL(@NROUNICORETIVA,0) AS NROUNICORETIVA, ISNULL(@FINISHPO,0) AS FINISHXML
