CREATE OR REPLACE PROCEDURE ATUALIZA_ITEMORCA(
	P_CDEMPRESA  IN ITEMORCA.CDEMPRESA%TYPE,
      P_CDORCAMENT IN ITEMORCA.CDORCAMENT%TYPE,
      P_CDFILIAL   IN ITEMORCA.CDFILIAL%TYPE,
      P_CDCENTCUST IN ITEMORCA.CDCENTCUST%TYPE,
      P_CDCONTCTBL IN ITEMORCA.CDCONTCTBL%TYPE)
IS

V_PRIMNIVEL   ITEMORCA.CDCONTCTBL%TYPE;

BEGIN

       SELECT CDCONTCTBL INTO V_PRIMNIVEL
             FROM CONTCTBL
             WHERE (INSTR(P_CDCONTCTBL,RTRIM(CONTCTBL.CDCONTCTBL)) = 1) AND
                   (CONTCTBL.CDNVCONTCTBL = '1');

      UPDATE ITEMORCA SET  VRITEMORCA = 0
             WHERE  (ITEMORCA.CDEMPRESA  = P_CDEMPRESA) AND
                    (ITEMORCA.CDORCAMENT = P_CDORCAMENT) AND
                    (ITEMORCA.CDFILIAL   = P_CDFILIAL) AND
                    (ITEMORCA.CDCENTCUST = P_CDCENTCUST) AND
                    (INSTR(ITEMORCA.CDCONTCTBL,RTRIM(V_PRIMNIVEL)) = 1) AND
                    (ITEMORCA.CDCONTCTBL IN (SELECT CDCONTCTBL
                                                 FROM CONTCTBL
                                                 WHERE IDTIPOCONT = 'S'));


	FOR ATUALIZA IN (SELECT CDCONTCTBL, DTITEMORCA, VRITEMORCA  
                    FROM ITEMORCA
			  WHERE  (CDEMPRESA  = P_CDEMPRESA) AND
                           (CDORCAMENT = P_CDORCAMENT) AND
                           (CDFILIAL   = P_CDFILIAL) AND
                           (CDCENTCUST = P_CDCENTCUST) AND
                           (INSTR(ITEMORCA.CDCONTCTBL,RTRIM(V_PRIMNIVEL)) = 1) AND
                           (ITEMORCA.CDCONTCTBL IN (SELECT CDCONTCTBL
                                                 FROM CONTCTBL
                                                 WHERE IDTIPOCONT = 'A'))
                    ORDER BY CDCONTCTBL, DTITEMORCA, VRITEMORCA) LOOP

          INSERT INTO ITEMORCA
             (CDEMPRESA,CDORCAMENT,CDFILIAL, CDCENTCUST,CDCONTCTBL,DTITEMORCA,VRITEMORCA)
	       (SELECT P_CDEMPRESA, P_CDORCAMENT,P_CDFILIAL, P_CDCENTCUST, CDCONTCTBL,ATUALIZA.DTITEMORCA, 0
              FROM CONTCTBL
              WHERE (CDCONTCTBL NOT IN (SELECT DISTINCT CdContCtbl
                                 FROM ITEMORCA
                                 WHERE (ITEMORCA.CDEMPRESA  = P_CDEMPRESA) AND
                                       (ITEMORCA.CDORCAMENT = P_CDORCAMENT) AND
                                       (ITEMORCA.CDFILIAL   = P_CDFILIAL) AND
                                       (ITEMORCA.CDCENTCUST = P_CDCENTCUST) AND
                                       (ITEMORCA.DTITEMORCA = ATUALIZA.DTITEMORCA))) AND
                    (INSTR(ATUALIZA.CDCONTCTBL,RTRIM(CONTCTBL.CDCONTCTBL)) = 1));

          UPDATE ITEMORCA
                 SET  VRITEMORCA = VRITEMORCA + ATUALIZA.VRITEMORCA
                 WHERE  (ITEMORCA.CDEMPRESA  = P_CDEMPRESA) AND
                        (ITEMORCA.CDORCAMENT = P_CDORCAMENT) AND
                        (ITEMORCA.CDFILIAL   = P_CDFILIAL) AND
                        (ITEMORCA.CDCENTCUST = P_CDCENTCUST) AND
                        (ITEMORCA.DTITEMORCA = ATUALIZA.DTITEMORCA) AND
                        (INSTR(ATUALIZA.CDCONTCTBL,RTRIM(ITEMORCA.CDCONTCTBL)) = 1) AND
                        (ITEMORCA.CDCONTCTBL IN (SELECT CDCONTCTBL
                                                 FROM CONTCTBL
                                                 WHERE IDTIPOCONT = 'S'));                    


	END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ATUALIZA_ITEMORCA','4.09.090'); END;
/

