CREATE OR REPLACE 
TRIGGER LIB_FAT_CUSTOMIZACAO
AFTER INSERT ON LOGFASETRAM FOR EACH ROW
DECLARE
 	 V_MAX	NUMBER;
BEGIN
   SELECT MAX(NRPARCSOLI)
     INTO V_MAX
     FROM PARCSOLISERV
    WHERE NRSOLISERV = :NEW.NRSOLISERV;

   FOR C_ATUALIZA IN 
      (SELECT SOL.CDFASETRAM, PAR.NRPARCSOLI
         FROM PARCSOLISERV PAR, SOLISERV SOL
        WHERE SOL.NRSOLISERV = :NEW.NRSOLISERV
          AND SOL.CDASSUNTO = '00021' 
          AND (SOL.CDFASETRAM = '063' OR SOL.CDFASETRAM = '105') 
          AND PAR.NRSOLISERV = SOL.NRSOLISERV
          AND PAR.NRAPURFATFAS IS NULL                          
          AND PAR.DTPARCSOLI IS NULL                            
        ORDER BY PAR.NRPARCSOLI) LOOP

	
      IF (C_ATUALIZA.CDFASETRAM = '063' AND V_MAX > 1) THEN
         UPDATE PARCSOLISERV
            SET DTPARCSOLI = SYSDATE
          WHERE NRSOLISERV = :NEW.NRSOLISERV               
            AND NRPARCSOLI = '1';
         EXIT;
      END IF;

      IF (C_ATUALIZA.CDFASETRAM = '105') THEN
         UPDATE PARCSOLISERV
            SET DTPARCSOLI = SYSDATE
          WHERE NRSOLISERV = :NEW.NRSOLISERV	           
            AND NRPARCSOLI = C_ATUALIZA.NRPARCSOLI;
      END IF;	
	  END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LIB_FAT_CUSTOMIZACAO','4.11.026'); END;
/

