Advertisement
EliasJRodriguez

TR SAFACT - N/E to CxC como Factura v2

Jun 22nd, 2021
1,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.80 KB | None | 0 0
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE TRIGGER [dbo].[USD_SAFACT]
  6.    ON  [dbo].[SAFACT]
  7.    AFTER INSERT AS
  8.  BEGIN
  9.  SET NOCOUNT ON
  10.  DECLARE @Factor        DECIMAL(28, 4)
  11.  DECLARE @FACTORM    DECIMAL(28,4),
  12.  @MONTO         DECIMAL(28,4),
  13.  @CONTADO       DECIMAL(28,4),
  14.  @CREDITO       DECIMAL(28,4),
  15.  @CAMBIO        DECIMAL(28,4),
  16.  @CANCELA       DECIMAL(28,4),
  17.  @CANCELE       DECIMAL(28,4),
  18.  @CANCELI       DECIMAL(28,4),
  19.  @CANCELC       DECIMAL(28,4),
  20.  @CANCELT       DECIMAL(28,4),
  21.  @CANCELG       DECIMAL(28,4),
  22.  @DESCTO1       DECIMAL(28,4),
  23.  @MTOTOTAL        DECIMAL(28,4),
  24.  @PRECIO        DECIMAL(28,4),
  25.  @COSTO        DECIMAL(28,4),
  26.  @TOTALITEM        DECIMAL(28,4),
  27.  @TOTALPRD        DECIMAL(28,4),
  28.  @COSTOPRD        DECIMAL(28,4),
  29.  @TOTALSRV        DECIMAL(28,4),
  30.  @COSTOSRV       DECIMAL(28,4),
  31.  @MONTOTAX     DECIMAL(28,4),
  32.  @MTOTAX     DECIMAL(28,4),
  33.  @MTOTAXI     DECIMAL(28,4),
  34.  @TGRAVABLE     DECIMAL(28,4),
  35.  @NUMEROFAC    VARCHAR(20),
  36.  @NUMERODOC    VARCHAR(20),
  37.  @SALDO      DECIMAL(28,4),
  38.  @SaldoAnt   DECIMAL(28,4),
  39.  @SaldoOrg   DECIMAL(28,4),
  40.  @TipoCxC    VARCHAR(2),
  41.  @TipoF    VARCHAR(1),
  42.  @CODCLIE    VARCHAR(15),
  43.  @CODITEM    VARCHAR(15),
  44.  @NROLINEA  INT,
  45.  @NUMERRORS    INT;
  46. SET @NUMERRORS=0;
  47. SET @Factor = (SELECT FACTOR FROM dbo.SACONF);
  48. SET @FACTORM = (SELECT FACTOR FROM dbo.SACONF);
  49.  
  50. SELECT  @NUMEROFAC=Inserted.NumeroD, @TipoF=Inserted.TipoFac  FROM INSERTED
  51. SELECT @TipoCxC=@TipoCxC FROM dbo.SAACXC WHERE CodClie=@CODCLIE AND NumeroD=@NUMEROFAC
  52.  
  53. DECLARE Fact_UPDATE_Mex cursor FOR
  54.      
  55.  
  56.  SELECT F.NumeroD, F.TipoFac, F.Monto, F.MtoTax, F.TGravable, F.CostoPrd, F.CostoSrv, F.MtoTotal, F.Contado, F.Credito, F.CancelI, F.CancelA, F.CancelE, F.CancelC, F.CancelT,
  57.         F.CancelG, F.Cambio, F.Descto1, F.TotalPrd, F.TotalSrv, I. NroLinea, I.CodItem, I.TotalItem, I.Costo, I.Precio, I.MtoTax, C.Saldo, C.SaldoOrg, C.SaldoAct  
  58.         FROM dbo.SAITEMFAC AS I
  59.         JOIN dbo.SAFACT AS F ON F.NumeroD=I.NumeroD  
  60.         JOIN dbo.SAACXC AS C ON C.TipoCxc=10 AND C.NumeroD=F.NumeroD
  61.         WHERE F.NumeroD=@NUMEROFAC AND F.TipoFac=@TipoF AND C.TipoCxc IN ('10','20')  AND I.TipoFac = @TipoF
  62.  
  63. OPEN Fact_UPDATE_Mex
  64.  
  65.   fetch NEXT FROM Fact_UPDATE_Mex INTO @NUMEROFAC, @TipoF, @MONTO, @MTOTAX, @TGRAVABLE, @COSTOPRD, @COSTOSRV, @MTOTOTAL, @CONTADO, @CREDITO, @CANCELI, @CANCELA, @CANCELE, @CANCELC, @CANCELT,
  66.                                     @CANCELG, @CAMBIO, @DESCTO1, @TOTALPRD, @TOTALSRV, @NROLINEA, @CODITEM, @TOTALITEM, @COSTO, @PRECIO, @MTOTAXI, @SALDO, @SALDOORG, @SaldoAnt
  67.  
  68.   WHILE @@fetch_status = 0  
  69. BEGIN
  70.  
  71.    IF EXISTS (SELECT * FROM dbo.SAFACT WHERE NumeroD = @NUMEROFAC AND FactorMex=0)
  72.     BEGIN
  73.      
  74.         UPDATE dbo.SAFACT SET FactorMex=@FACTORM, MontoMEx=@MONTO*@FACTORM, MtoTotalMex=@MTOTOTAL*@FACTORM, ContadoMex=@CONTADO*@FACTORM, CreditoMex=@CREDITO*@FACTORM, TotalPrdMex=@TOTALPRD*@FACTORM,
  75.                         TGravableMex=@TGRAVABLE*@FACTORM, MtoTaxMEx=@MTOTAX*@FACTORM, CancelIMex=@CANCELI*@FACTORM, CancelAMex=@CANCELA*@FACTORM, CancelEMex=@CANCELE*@FACTORM,
  76.                         CancelCMex=@CANCELC*@FACTORM, CancelTMex=@CANCELT*@FACTORM, CancelGMex=@CANCELG*@FACTORM
  77.                         WHERE (CODSUCU='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC);
  78.  
  79.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  80.  
  81.         UPDATE dbo.SATAXVTA SET TGravableMex=@TGRAVABLE*@FACTORM,MontoMex=@MONTO*@FACTORM  WHERE (CODSUCU='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC);
  82.        
  83.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  84.        
  85.         UPDATE dbo.SAITEMFAC SET  FactorMex=@FACTORM,TotalItemMex=@TOTALITEM*@FACTORM,PrecioMex=@PRECIO*@FACTORM, CostoMex=@COSTO*@FACTORM, MtoTaxMex=@MTOTAXI*@FACTORM
  86.                              WHERE (CODSUCU='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC) AND (NroLinea=@NROLINEA);
  87.  
  88.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  89.  
  90.         UPDATE dbo.SATAXITF SET TGravable=@TOTALITEM*@FACTORM,Monto=@MTOTAXI*@FACTORM WHERE (CodSucu='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC) AND (NroLinea=@NROLINEA);
  91.  
  92.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  93.  
  94.         UPDATE dbo.SAACXC SET SaldoMEx=@SALDO*@FACTORM, SaldoOrgMex=@SaldoOrg*@FACTORM, SaldoActMex=@SaldoAnt*@FACTORM, MtoTaxMex=@MTOTAX*@FACTORM, MontoMEx=@MONTO*@FACTORM, BaseImpoMex=@TGRAVABLE*@FACTORM,
  95.                             CreditosMex=@CREDITO*@FACTORM, CancelIMex=@CANCELI*@FACTORM, CancelAMex=@CANCELA*@FACTORM, CancelEMex=@CANCELE*@FACTORM, CancelCMex=@CANCELC*@FACTORM,
  96.                             CancelTMex=@CANCELT*@FACTORM, CancelGMex=@CANCELG*@FACTORM  
  97.                             WHERE (CODSUCU='00000') AND (TipoCxc=@TipoCxC) AND (NUMEROD=@NUMEROFAC);
  98.  
  99.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  100.  
  101.     END
  102.    
  103. FETCH NEXT FROM Fact_UPDATE_Mex INTO @NUMEROFAC, @TipoF, @MONTO, @MTOTAX, @TGRAVABLE, @COSTOPRD, @COSTOSRV, @MTOTOTAL, @CONTADO, @CREDITO, @CANCELI, @CANCELA, @CANCELE, @CANCELC, @CANCELT,
  104.                                     @CANCELG, @CAMBIO, @DESCTO1, @TOTALPRD, @TOTALSRV, @NROLINEA, @CODITEM, @TOTALITEM, @COSTO, @PRECIO, @MTOTAXI, @SALDO, @SALDOORG, @SaldoAnt
  105.  
  106. END
  107.   close Fact_UPDATE_Mex
  108. deallocate Fact_UPDATE_Mex
  109.  
  110.  
  111. END
  112. GO
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement