Advertisement
EliasJRodriguez

CSO TRIGGER

Jun 19th, 2023
1,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.34 KB | Fixit | 0 0
  1. USE [EnterpriseAdminDB]
  2. GO
  3. /****** Object:  Trigger [dbo].[USD_SAFACT]    Script Date: 19/06/2023 14:38:02 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. --DROP TRIGGER [dbo].[USD_SAFACT]
  9. --GO
  10. ALTER TRIGGER [dbo].[USD_SAFACT]
  11.    ON  [dbo].[SAITEMFAC]
  12.    AFTER INSERT AS
  13.  BEGIN
  14.  SET NOCOUNT ON
  15.  DECLARE
  16.  @Factor        DECIMAL(28, 4),
  17.  @FACTORM    DECIMAL(28,4),
  18.  @MONTO         DECIMAL(28,4),
  19.  @CONTADO       DECIMAL(28,4),
  20.  @CREDITO       DECIMAL(28,4),
  21.  @CAMBIO        DECIMAL(28,4),
  22.  @CANCELA       DECIMAL(28,4),
  23.  @CANCELE       DECIMAL(28,4),
  24.  @CANCELI       DECIMAL(28,4),
  25.  @CANCELC       DECIMAL(28,4),
  26.  @CANCELT       DECIMAL(28,4),
  27.  @CANCELG       DECIMAL(28,4),
  28.  @DESCTO1       DECIMAL(28,4),
  29.  @MTOTOTAL        DECIMAL(28,4),
  30.  @PRECIO        DECIMAL(28,4),
  31.  @COSTO        DECIMAL(28,4),
  32.  @TOTALITEM        DECIMAL(28,4),
  33.  @TOTALPRD        DECIMAL(28,4),
  34.  @COSTOPRD        DECIMAL(28,4),
  35.  @TOTALSRV        DECIMAL(28,4),
  36.  @COSTOSRV       DECIMAL(28,4),
  37.  @MONTOTAX     DECIMAL(28,4),
  38.  @MTOTAX     DECIMAL(28,4),
  39.  @MTOTAXI     DECIMAL(28,4),
  40.  @NOTAS      VARCHAR(MAX),
  41.  @TGRAVABLE     DECIMAL(28,4),
  42.  @NUMEROFAC    VARCHAR(20),
  43.  @NUMERODOC    VARCHAR(20),
  44.  @SALDO      decimal(28,4),
  45.  @SaldoAnt   DECIMAL(28,4),
  46.  @SaldoOrg   DECIMAL(28,4),
  47.  @TipoCxC    VARCHAR(2),
  48.  @TipoF    VARCHAR(1),
  49.  @CODCLIE    VARCHAR(15),
  50.  @CODITEM    VARCHAR(15),
  51.  @NROLINEA  INT,
  52.  @NUMERRORS    INT;
  53. SET @NUMERRORS=0;
  54. SET @Factor = (SELECT FACTOR FROM dbo.SACONF);
  55. SET @FACTORM = (SELECT FactorM FROM dbo.SACONF);
  56.  
  57. SELECT  @NUMEROFAC=Inserted.NumeroD, @TipoF=Inserted.TipoFac, @NROLINEA=NroLinea  FROM INSERTED
  58.  
  59.  
  60. declare Fact_UPDATE_Mex cursor FOR
  61.      
  62.  
  63.  SELECT F.NumeroD, F.TipoFac, F.Monto, F.MtoTax, F.TGravable, F.CostoPrd, F.CostoSrv, F.MtoTotal, F.Contado, F.Credito, F.CancelI,
  64.         F.CancelA, F.CancelE, F.CancelC, F.CancelT, F.CancelG, F.Cambio, F.Descto1, F.TotalPrd, F.TotalSrv,
  65.         I. NroLinea, I.CodItem, I.TotalItem, I.Costo, I.Precio, I.MtoTax, F.Notas6
  66.         FROM dbo.SAITEMFAC AS I
  67.         JOIN dbo.SAFACT AS F ON F.NumeroD=I.NumeroD        
  68.         WHERE F.NumeroD=@NUMEROFAC AND F.TipoFac=@TipoF AND I.NroLinea=@NROLINEA
  69.  
  70. open Fact_UPDATE_Mex
  71.  
  72.   fetch next from Fact_UPDATE_Mex into @NUMEROFAC, @TipoF, @MONTO, @MTOTAX, @TGRAVABLE, @COSTOPRD, @COSTOSRV, @MTOTOTAL, @CONTADO, @CREDITO, @CANCELI, @CANCELA, @CANCELE, @CANCELC, @CANCELT,
  73.                                     @CANCELG, @CAMBIO, @DESCTO1, @TOTALPRD, @TOTALSRV, @NROLINEA, @CODITEM, @TOTALITEM, @COSTO, @PRECIO, @MTOTAXI, @NOTAS
  74.  
  75.   WHILE @@fetch_status = 0  
  76. BEGIN
  77.  
  78.    IF EXISTS (SELECT * FROM dbo.SAFACT WHERE NumeroD = @NUMEROFAC AND TipoFac=@TipoF AND @TipoF='G')
  79.     BEGIN
  80.         IF EXISTS (SELECT * FROM dbo.SAFACT WHERE NumeroD = @NUMEROFAC AND TipoFac=@TipoF AND Notas10 IS NULL)
  81.     BEGIN
  82.         UPDATE dbo.SAFACT SET Monto=@MONTO*@FACTORM, MtoTotal=@MTOTOTAL*@FACTORM, MontoMEx=@MONTO,
  83.                         Contado=@CONTADO*@FACTORM, Credito=@CREDITO*@FACTORM,
  84.                         TGravable=@TGRAVABLE*@FACTORM, MtoTax=@MTOTAX*@FACTORM,
  85.                         CancelI=@CANCELI*@FACTORM, CancelA=@CANCELA*@FACTORM, CancelE=@CANCELE*@FACTORM,
  86.                         CancelC=@CANCELC*@FACTORM, CancelT=@CANCELT*@FACTORM, CancelG=@CANCELG*@FACTORM,
  87.                         Factor=@FACTORM, Notas10='UPDATED'
  88.                         WHERE (CODSUCU='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC) AND @NOTAS LIKE '%$%';
  89.  
  90.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  91.    
  92.         UPDATE dbo.SATAXVTA SET TGravable=@TGRAVABLE*@FACTORM, Monto=@MONTO*@FACTORM  WHERE (CODSUCU='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC);
  93.    
  94.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  95.     END
  96.         UPDATE dbo.SAITEMFAC SET    TotalItem=@TOTALITEM*@FACTORM, MtoTax=@MTOTAXI*@FACTORM,
  97.                                     Precio=@PRECIO*@FACTORM, Costo=@COSTO*@FACTORM
  98.                              WHERE (CODSUCU='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC) AND (NroLinea=@NROLINEA);
  99.  
  100.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  101.  
  102.         UPDATE dbo.SATAXITF SET TGravable=@TOTALITEM*@FACTORM,Monto=@MTOTAXI*@FACTORM
  103.                             WHERE (CodSucu='00000') AND (TIPOFAC=@TipoF) AND (NUMEROD=@NUMEROFAC) AND (NroLinea=@NROLINEA);
  104.  
  105.  
  106.         SET @NUMERRORS=@NUMERRORS+@@ERROR;
  107.  
  108.     END
  109.    
  110. FETCH NEXT FROM Fact_UPDATE_Mex INTO @NUMEROFAC, @TipoF, @MONTO, @MTOTAX, @TGRAVABLE, @COSTOPRD, @COSTOSRV, @MTOTOTAL, @CONTADO, @CREDITO, @CANCELI, @CANCELA, @CANCELE, @CANCELC, @CANCELT,
  111.                                     @CANCELG, @CAMBIO, @DESCTO1, @TOTALPRD, @TOTALSRV, @NROLINEA, @CODITEM, @TOTALITEM, @COSTO, @PRECIO, @MTOTAXI, @NOTAS
  112.  
  113. END
  114.   close Fact_UPDATE_Mex
  115. deallocate Fact_UPDATE_Mex
  116.  
  117.  
  118. END
  119.  
Tags: sql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement