create or replace
PROCEDURE CONTCTBL_CLIENTE(
         P_CDEMPRESA IN EMPRESA.CDEMPRESA%TYPE,
         P_TIPOOPER IN TIPOOPERAC.CDTIPOOPER%TYPE,
	 P_IDENTRSAID IN VARCHAR2,
	 P_FILIAL IN FILIAL.CDFILIAL%TYPE,
	 P_CLIENTE IN CLIENTE.CDCLIENTE%TYPE,
         P_CDCONTCTBL OUT VARCHAR2,
         P_CDCONTCTBLP OUT VARCHAR2,
         P_STCONTDIFE OUT VARCHAR2,
         P_CDHISTPADR OUT VARCHAR2)
IS
    V_CDCONTTOPER VARCHAR2(20);
    V_CDCONTFORCLI VARCHAR2(20);
    V_CDHISTPADR VARCHAR2(4);
    V_CDCONTPCTBL VARCHAR2(20);
    V_CDCONTPFILI VARCHAR2(20);
    V_CDCONTPEMPR VARCHAR2(20);
    V_CDCONTPROD VARCHAR2(20);
    V_CDCONTDIFE VARCHAR2(20);
    V_STCONTCTBL VARCHAR2(20);
    V_STCONTCTBLP VARCHAR2(20);
    V_STCDHISTPADR VARCHAR2(4);
    V_IDORIGDESTOP VARCHAR2(1);
BEGIN
   BEGIN
      SELECT CDCONTTOPER,  CDCONTFORCLI, CDHISTPADR,  IDORIGDESTOP,
             CDCONTPCTBL,  CDCONTPFILI,  CDCONTPEMPR,  CDCONTPROD  ,
             CDCONTDIFE
      INTO V_CDCONTTOPER, V_CDCONTFORCLI, V_CDHISTPADR, V_IDORIGDESTOP,
           V_CDCONTPCTBL, V_CDCONTPFILI, V_CDCONTPEMPR, V_CDCONTPROD,
           V_CDCONTDIFE
      FROM
        (SELECT MAX(CDCONTFORCLI) AS CDCONTTOPER, MAX(T.CDHISTPADR) AS CDHISTPADR, MAX(IDORIGDESTOP) AS IDORIGDESTOP,
                MAX(CDCONTPROD) AS CDCONTPROD
         FROM TIPOOPERAC T
         WHERE CDTIPOOPER   = P_TIPOOPER
         AND IDENTRSAIDOP = P_IDENTRSAID) A,
  
        (SELECT C.CDCONTCTBL AS CDCONTFORCLI, CDCONTPCTBL,
               CDCONTPFILI, CDCONTPEMPR, CDCONTDIFEFI AS CDCONTDIFE
        FROM CLIENTE C,
            (SELECT MAX(CDCONTCLIE)  AS CDCONTPCTBL FROM PARAMCTBL)  PC,
            (SELECT MAX(CDCONCLIEEM) AS CDCONTPEMPR FROM PARAMEMPRESA
             WHERE CDEMPRESA = P_CDEMPRESA)PE,
            (SELECT MAX(CDCONCLIEFI) AS CDCONTPFILI, MAX(CDCONTDIFEFI) AS CDCONTDIFEFI
               FROM PARAMFILIAL
             WHERE CDFILIAL  = P_FILIAL) PF
        WHERE CDCLIENTE     = P_CLIENTE) B;
EXCEPTION
   WHEN NO_DATA_FOUND THEN
   V_CDCONTTOPER   := NULL;
    V_CDCONTFORCLI := NULL;
    V_CDHISTPADR  := NULL;
    V_CDCONTPCTBL := NULL;
    V_CDCONTPFILI := NULL;
    V_CDCONTPEMPR := NULL;
    V_CDCONTPROD  := NULL;
    V_CDCONTDIFE  := NULL;
   
END;


      IF V_CDCONTTOPER IS NOT NULL THEN
         P_CDCONTCTBL := V_CDCONTTOPER;
      ELSIF V_CDCONTFORCLI IS NOT NULL THEN
         P_CDCONTCTBL := V_CDCONTFORCLI;
      ELSIF V_CDCONTPFILI IS NOT NULL THEN
         P_CDCONTCTBL := V_CDCONTPFILI;
      ELSIF V_CDCONTPEMPR IS NOT NULL THEN
         P_CDCONTCTBL := V_CDCONTPEMPR;
      ELSIF V_CDCONTPCTBL IS NOT NULL THEN
         P_CDCONTCTBL := V_CDCONTPCTBL;
      END IF;
      IF P_CDCONTCTBL IS NULL THEN
         BEGIN
           SELECT MAX(CDCONTFORCLI) AS CDCONTTOPER, MAX(T.CDHISTPADR) AS CDHISTPADR,
                  MAX(CDCONTPROD) AS CDCONTPROD
             INTO V_CDCONTTOPER, V_CDHISTPADR, V_CDCONTPROD
             FROM TIPOOPERAC T
            WHERE CDTIPOOPER   = P_TIPOOPER
              AND IDENTRSAIDOP = P_IDENTRSAID;
         EXCEPTION WHEN NO_DATA_FOUND THEN
         V_CDCONTTOPER := NULL;
         V_CDHISTPADR  := NULL;
         V_CDCONTPROD  := NULL;
         END;
      END IF;
      P_STCONTDIFE := V_CDCONTDIFE;

      P_CDCONTCTBLP := V_CDCONTPROD;

      P_CDHISTPADR := V_CDHISTPADR;

END;
/

-- VERSAO --
BEGIN ATUVEROBJ('CONTCTBL_CLIENTE','6.05.xxx'); END;
/

