IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
                    WHERE
	        O.NAME = 'CTB_HIST_PAG'  AND
	        TYPE = 'P' 			            AND
	        U.UID = O.UID             		AND
	        U.NAME = USER)

 DROP PROCEDURE CTB_HIST_PAG
 GO

 CREATE PROCEDURE CTB_HIST_PAG(
 @P_CDHISTPADR VARCHAR(4),
 @P_CDFILIAL VARCHAR(4),
 @P_NRLANCTONF VARCHAR(10),
 @P_NRSEQVENPAG VARCHAR(2),
 @P_NRPARCELAPAG VARCHAR(2),
 @P_CDAJUCTPAG VARCHAR(4),
 @P_DSHISTPADR VARCHAR(200) OUTPUT )

AS
BEGIN
   DECLARE @V_DSHISTPADR VARCHAR(200);
   DECLARE @V_DSAJUSTE VARCHAR(40);
   DECLARE @V_STCHEQUE VARCHAR(6);

   DECLARE @C_HIST_NMRAZSOCFORN VARCHAR(50);
   DECLARE @C_HIST_NMFANTFORN VARCHAR(50);
   DECLARE @C_HIST_NRINSJURFORN VARCHAR(20);
   DECLARE @C_HIST_CDFILIEMIT VARCHAR(4);
   DECLARE @C_HIST_NRNOTAFISC VARCHAR(20);
   DECLARE @C_HIST_NRLANCTONF VARCHAR(10);
   DECLARE @C_HIST_DSTPCTPAGAR VARCHAR(40);
   DECLARE @C_HIST_DSCONTAPAG VARCHAR(200);
   DECLARE @C_HIST_NRCHEQUE VARCHAR(6);

   SELECT @V_DSHISTPADR = DSHISTPADR
     FROM HISTPADR
    WHERE CDHISTPADR = @P_CDHISTPADR;

   DECLARE C_HIST CURSOR FOR(
      SELECT F.NMRAZSOCFORN, F.NMFANTFORN, F.NRINSJURFORN, NF.CDFILIEMIT, NF.NRNOTAFISC,
              NF.NRLANCTONF, TP.DSTPCTPAGAR, CT.DSCONTAPAG, BX.NRCHEQUE
         FROM  TIPOCTPAGAR TP, NOTAFISCAL NF, FORNECEDOR F,
               CONTAPAGAR CT LEFT JOIN BOLETPAGAR BP LEFT JOIN BAIXAPAGA BX
                                                            ON (BP.CDFILIBAIPAG     = BX.CDFILIAL
                                                                AND BP.NRBAIXAPAG   = BX.NRBAIXAPAG)

                                    ON (CT.CDFILIBPAG       = BP.CDFILIAL
                                        AND CT.NRLANBOLPAG  = BP.NRLANBOLPAG)
          WHERE CT.CDFILIAL   = @P_CDFILIAL
          AND CT.NRLANCTONF   = @P_NRLANCTONF
          AND CT.NRSEQVENPAG  = @P_NRSEQVENPAG
          AND CT.NRPARCELAPAG = @P_NRPARCELAPAG
          AND CT.CDTPCTPAGAR  = TP.CDTPCTPAGAR
          AND CT.CDFILIAL     = NF.CDFILIAL
          AND CT.NRLANCTONF   = NF.NRLANCTONF
          AND NF.CDFORNECED   = F.CDFORNECED
      )
      OPEN C_HIST;
      FETCH NEXT FROM C_HIST INTO @C_HIST_NMRAZSOCFORN, @C_HIST_NMFANTFORN, @C_HIST_NRINSJURFORN, @C_HIST_CDFILIEMIT,
                                  @C_HIST_NRNOTAFISC, @C_HIST_NRLANCTONF, @C_HIST_DSTPCTPAGAR, @C_HIST_DSCONTAPAG, @C_HIST_NRCHEQUE;

      WHILE (@@FETCH_STATUS = 0) BEGIN

         IF (@C_HIST_NRCHEQUE IS NULL) BEGIN
            SET @V_STCHEQUE = '---';
         END
         ELSE BEGIN
            SET @V_STCHEQUE = @C_HIST_NRCHEQUE;
         END;

         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[#CHQ]', ISNULL(@V_STCHEQUE,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[FIL]', ISNULL(@C_HIST_CDFILIEMIT,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[DOC]', ISNULL(@C_HIST_NRNOTAFISC,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[SEQ]', ISNULL(@C_HIST_NRLANCTONF,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[TCP]', ISNULL(@C_HIST_DSTPCTPAGAR,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[DCP]', ISNULL(@C_HIST_DSCONTAPAG,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[FOR]', ISNULL(@C_HIST_NRINSJURFORN,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[NFF]', ISNULL(@C_HIST_NMFANTFORN,''));
         SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[RSF]', ISNULL(@C_HIST_NMRAZSOCFORN,''));

         FETCH NEXT FROM C_HIST INTO @C_HIST_NMRAZSOCFORN, @C_HIST_NMFANTFORN, @C_HIST_NRINSJURFORN, @C_HIST_CDFILIEMIT,
                                     @C_HIST_NRNOTAFISC, @C_HIST_NRLANCTONF, @C_HIST_DSTPCTPAGAR, @C_HIST_DSCONTAPAG, @C_HIST_NRCHEQUE;
      END;
      CLOSE C_HIST;
      DEALLOCATE C_HIST;

      IF @P_CDAJUCTPAG IS NOT NULL BEGIN

         SELECT @V_DSAJUSTE = DSAJUCTPAG
           FROM TIPOAJPAGAR
          WHERE CDAJUCTPAG = @P_CDAJUCTPAG;
      END;

      SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[TAP]', ISNULL(@P_CDAJUCTPAG,''));
      SET @V_DSHISTPADR = REPLACE(@V_DSHISTPADR, '[DAP]', ISNULL(@V_DSAJUSTE,''));

      SET @P_DSHISTPADR = @V_DSHISTPADR;
     
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'CTB_HIST_PAG','5.50.000';
GO

