USE SIES
GO
 ALTER PROCEDURE OP_GRABAR_ADJUST_CARDPROC  
( @SITEID        UD_IDSITE,  
  @DATEPROCE     UD_FECHA,  
  @USERID        UD_USERID )  
AS   
BEGIN  
  
  DECLARE @CARDIDCUR        char(2)       
  DECLARE @CARDIDADJUST     char(2)  
  DECLARE @CURYIDCUR        UD_CURYID  
  
  DECLARE @TOTALCARDNOSALES UD_MONTO              DECLARE @TOTALCARDSALES   UD_MONTO  
  DECLARE @TOTALCARDADJUST  UD_MONTO  
    
  DECLARE CURTARJETAS CURSOR    
     FOR  
     SELECT T.CARDID,T.CURYID    
            FROM OP_SALESCARD AS T JOIN OP_SALES AS S  
            ON T.SITEID=S.SITEID AND T.DOCTYPEID=S.DOCTYPEID AND T.NBRDOCUMENT=S.NBRDOCUMENT  
            WHERE T.SITEID=@SITEID AND T.DATEPROCESALES=@DATEPROCE   
                  AND S.DOCSTATUSID<>'AN'         
                  AND T.CARDID NOT IN (SELECT CARDID FROM OP_CARD_MONTSALES WHERE SITEID=@SITEID AND   
                                                                                          DATEPROCE=@DATEPROCE)  
            GROUP BY T.CARDID,T.CURYID   
                     
     OPEN CURTARJETAS     
     FETCH NEXT FROM CURTARJETAS INTO @CARDIDCUR,@CURYIDCUR  
       
     WHILE @@FETCH_STATUS=0  
 BEGIN  
            -- Hay que realizar los Ajustes  
  
            -- DECLARE @CARDIDCUR        char(2)       
            -- DECLARE @CURYIDCUR        UD_CURYID  
IF EXISTS(SELECT * FROM OP_CARD_MONTSALES WHERE SITEID=@SITEID AND CARDID=@CARDIDCUR AND   
                        DATEPROCE=@DATEPROCE ) GOTO EXISTE

        INSERT INTO OP_CARD_MONTSALES(SITEID,CARDID,DATEPROCE,CURYID,STATUS,USERID,TOTALPENSALES)  
                 SELECT top 1 T.SITEID,T.CARDID,@DATEPROCE,@CURYIDCUR,0,@USERID,  
                         SUM(T.TOTCARDPEN) FROM OP_SALESCARD AS T JOIN OP_SALES AS S  
                      ON T.SITEID=S.SITEID AND T.DOCTYPEID=S.DOCTYPEID AND T.NBRDOCUMENT=S.NBRDOCUMENT  
               WHERE T.SITEID=@SITEID AND T.CARDID=@CARDIDCUR AND   
                        T.DATEPROCESALES=@DATEPROCE AND S.DOCSTATUSID<>'AN'  
                  GROUP BY T.SITEID,T.CARDID  
  /*
           IF (@@error!=0)  
            BEGIN  
         RAISERROR  20000 'Error Insertando en  OP_CARD_MONTSALES '  
                CLOSE CURTARJETAS  
                DEALLOCATE CURTARJETAS  
         RETURN(1)  
            END  
*/
          
           -- Parte en que se va ha Generar el Ajuste  
          
           SELECT @TOTALCARDSALES=SUM(T.TOTCARDPEN)  
                  FROM OP_SALESCARD AS T JOIN OP_SALES AS S  
                  ON T.SITEID=S.SITEID AND T.DOCTYPEID=S.DOCTYPEID AND T.NBRDOCUMENT=S.NBRDOCUMENT  
           WHERE T.SITEID=@SITEID AND T.CARDID=@CARDIDCUR AND   
                 T.DATEPROCESALES=@DATEPROCE AND S.DOCSTATUSID<>'AN'  
  
           --    
           SELECT @TOTALCARDNOSALES=SUM(TOTALCARD) FROM OP_CARD_MONT  
                 WHERE SITEID=@SITEID AND CARDID=@CARDIDCUR AND DATEPROCE=@DATEPROCE  
   
  
           IF ISNULL(@TOTALCARDNOSALES,0) <> ISNULL(@TOTALCARDSALES,0) -- Predomina el @TOTALCARD         
              BEGIN          
                SET @TOTALCARDADJUST = ISNULL(@TOTALCARDNOSALES,0) - ISNULL(@TOTALCARDSALES,0)                                             
                         
                SELECT @CARDIDADJUST=CARDADJUST FROM OP_CARD WHERE CARDID=@CARDIDCUR   
  
                IF (LEN(RTRIM(LTRIM(@CARDIDADJUST)))=0) OR (@CARDIDADJUST IS NULL)  
                   BEGIN   
                RAISERROR  20000 'No existe Código de Tarjeta para realizar Ajuste '  
                    CLOSE CURTARJETAS  
                    DEALLOCATE CURTARJETAS  
             RETURN(1)                     
                   END                   
   
                INSERT INTO OP_CARD_MONTSALES(SITEID,CARDID,DATEPROCE,CURYID,STATUS,USERID,TOTALPENSALES)  
                      VALUES (@SITEID,@CARDIDADJUST,@DATEPROCE,@CURYIDCUR,0,@USERID,@TOTALCARDADJUST)   
                 
              IF (@@error!=0 OR @@ROWCOUNT=0)  
                BEGIN  
             RAISERROR  20000 'No se pudo Grabar Ajuste '  
                    CLOSE CURTARJETAS  
                    DEALLOCATE CURTARJETAS  
             RETURN(1)  
                END  
             END  
EXISTE:      
       -- Fin de realizar los Ajustes  
  
            FETCH NEXT FROM CURTARJETAS INTO @CARDIDCUR,@CURYIDCUR              
        END   
        
     CLOSE CURTARJETAS  
     DEALLOCATE CURTARJETAS  
     RETURN(0)    
        
END  
  
