if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OP_CARD_MONTInsProc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[OP_CARD_MONTInsProc]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE OP_CARD_MONTInsProc
(
    @SITEID        UD_IDSITE,
    @CARDID        char(2),
    @DATEPOS       UD_FECHA,
    @DATEPROCE     UD_FECHA,
    @CURYID        UD_CURYID,
    @TOTALCARD     UD_MONTO,
    @STATUS        bit                  = NULL,
    @COMMENT       text,
    @USERID        UD_USERID--,
--    @BUSINESS	   CHAR(1)
)
AS
BEGIN

    DECLARE @CARDIDADJUST      CHAR(2)

    BEGIN TRAN

    INSERT INTO OP_CARD_MONT(SITEID,CARDID,DATEPOS,DATEPROCE,CURYID,TOTALCARD,STATUS,COMMENT,USERID)
           VALUES(@SITEID,@CARDID,@DATEPOS,@DATEPROCE,@CURYID,@TOTALCARD,@STATUS,@COMMENT,@USERID)

      IF (@@error!=0)
        BEGIN
          RAISERROR  20000 'Error Insertando en  OP_CARD_MONT '
          ROLLBACK TRAN
          RETURN(1)    
        END

    DELETE FROM OP_CARD_MONTSALES 
          WHERE SITEID=@SITEID AND CARDID=@CARDID AND DATEPROCE=@DATEPROCE 
      
       IF (@@ERROR!=0)                
         BEGIN      
           RAISERROR 20000 'No se pudo Eliminar de OP_CARD_MONTSALES'
           ROLLBACK TRAN
           RETURN(1) 
         END
     
    SELECT @CARDIDADJUST=CARDADJUST FROM OP_CARD 
            WHERE CARDID=@CARDID 
       
    DELETE FROM OP_CARD_MONTSALES 
           WHERE SITEID=@SITEID AND CARDID=@CARDIDADJUST AND DATEPROCE=@DATEPROCE 

       IF (@@ERROR!=0)                
         BEGIN      
           RAISERROR 20000 'No se pudo Eliminar de Ajuste de OP_CARD_MONTSALES'
           ROLLBACK TRAN
           RETURN(1) 
         END         
                      

    IF NOT EXISTS (SELECT * FROM OP_CARD_MONTSALES WHERE SITEID=@SITEID AND CARDID=@CARDID AND DATEPROCE=@DATEPROCE )	
      BEGIN
        DECLARE @TOTALCARDNOSALES  UD_MONTO
        DECLARE @TOTALCARDSALES    UD_MONTO
        DECLARE @TOTALCARDADJUST   UD_MONTO
          
    	INSERT INTO OP_CARD_MONTSALES(SITEID,CARDID,DATEPROCE,CURYID,STATUS,USERID,TOTALPENSALES)
      	       SELECT T.SITEID,T.CARDID,@DATEPROCE,@CURYID,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
			INNER JOIN OP_SALESPOINTSETTING P ON S.SALESPOINT = P.SALESPOINT
    	       WHERE T.SITEID=@SITEID AND T.CARDID=@CARDID 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 '
        	ROLLBACK TRAN
        	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
		INNER JOIN OP_SALESPOINTSETTING P ON S.SALESPOINT = P.SALESPOINT
        WHERE T.SITEID=@SITEID AND T.CARDID=@CARDID AND T.DATEPROCESALES=@DATEPROCE AND S.DOCSTATUSID<>'AN'

       
        --   
        SELECT @TOTALCARDNOSALES=SUM(TOTALCARD) FROM OP_CARD_MONT
               WHERE SITEID=@SITEID AND CARDID=@CARDID 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=@CARDID 
              
              IF LEN(RTRIM(LTRIM(@CARDIDADJUST)))=0
                 BEGIN 
        	  RAISERROR  20000 'No existe Código de Tarjeta para realizar Ajuste '
        	  ROLLBACK TRAN
        	  RETURN(1)                   
                 END 

              -- Hay que Verificar si existe el Ajuste   
                             
              IF NOT EXISTS (SELECT CARDID FROM OP_CARD_MONTSALES WHERE SITEID=@SITEID AND 
                                                                        CARDID=@CARDIDADJUST AND
                                                                        DATEPROCE=@DATEPROCE ) 
                 BEGIN
                     INSERT INTO OP_CARD_MONTSALES(SITEID,CARDID,DATEPROCE,CURYID,STATUS,USERID,TOTALPENSALES)
                            VALUES (@SITEID,@CARDIDADJUST,@DATEPROCE,@CURYID,0,@USERID,@TOTALCARDADJUST) 
               
                     IF (@@error!=0 OR @@ROWCOUNT=0)
    	                 BEGIN
        	           RAISERROR  20000 'No se pudo Grabar Ajuste '
        	           ROLLBACK TRAN
        	           RETURN(1)
    	                 END 
                 END
              ELSE
                 BEGIN
                     UPDATE OP_CARD_MONTSALES SET TOTALPENSALES=@TOTALCARDADJUST                      
                            WHERE SITEID=@SITEID AND CARDID=@CARDIDADJUST AND DATEPROCE=@DATEPROCE

                     IF (@@error!=0 OR @@ROWCOUNT=0)
    	                 BEGIN
        	           RAISERROR  20000 'No se pudo Actualizar Ajuste '
        	           ROLLBACK TRAN
        	           RETURN(1)
    	                 END
                 END  
           END

        -- Fin de la Generación del Ajuste

     END

    COMMIT TRAN
END

/****************************************************/
/********** Para Actualizar en OP_CARD **************/
/****************************************************/
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

