
CREATE OR REPLACE TRIGGER ATUALIZA_DADOS_EFECLIFAT
BEFORE UPDATE OR INSERT OR DELETE
ON EFECLIFAT FOR EACH ROW
DECLARE
   V_DTINICIAL       DATE;
   V_DTFINAL         DATE;
   V_IDFECHDIAROP    CHAR;
   V_IDUTPADRROPFIL  CHAR;
   V_IDUTPADRROP     CHAR;

BEGIN
   IF INSERTING THEN
     SELECT MAX(DTINIAPURACROP), MAX(DTFINAPURACROP)
     INTO V_DTINICIAL, V_DTFINAL
     FROM APURACAOROP
     WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
     IF (NVL(V_IDFECHDIAROP,'N') <> 'S') THEN
        IF (:NEW.DTFATURAM < V_DTFINAL)  AND (:NEW.DTFATURAM < V_DTINICIAL) THEN
           UPDATE APURACAOROP
           SET DTINIAPURACROP  = :NEW.DTFATURAM
           WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
        END IF;
     END IF;
   END IF;

   IF DELETING THEN
     SELECT MAX(DTINIAPURACROP), MAX(DTFINAPURACROP)
     INTO V_DTINICIAL, V_DTFINAL
     FROM APURACAOROP
     WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
     IF (NVL(V_IDFECHDIAROP,'N') <> 'S') THEN
        IF (:OLD.DTFATURAM < V_DTFINAL)  AND (:OLD.DTFATURAM < V_DTINICIAL) THEN
           UPDATE APURACAOROP
           SET DTINIAPURACROP  = :OLD.DTFATURAM
           WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
        END IF;
     END IF;
   END IF;
   
   IF UPDATING THEN
      SELECT MAX(IDUTPADRROP)
      INTO V_IDUTPADRROP
      FROM PARAMGERAL G;

      BEGIN
         SELECT IDFECHDIAROP, IDUTPADRROPFIL
         INTO V_IDFECHDIAROP, V_IDUTPADRROPFIL
         FROM  PARFILIALROP
         WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDFECHDIAROP   := NULL;
            V_IDUTPADRROPFIL := NULL;
         END;
      END;

      IF V_IDUTPADRROP = 'S' OR V_IDUTPADRROPFIL = 'S' THEN 
         BEGIN
            SELECT IDFECHDIAROP, IDUTPADRROPFIL
            INTO V_IDFECHDIAROP, V_IDUTPADRROPFIL
            FROM  PARFILIALROP
            WHERE CDFILIAL = 'P';
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_IDFECHDIAROP := NULL;
               V_IDUTPADRROPFIL := NULL;
            END;
         END;
      END IF;

      IF (:NEW.QTCOMEFATU <> :OLD.QTCOMEFATU)
         OR (:NEW.QTCOMEPREV <> :OLD.QTCOMEPREV)
         OR (:NEW.VRSERVFATUDT <> :OLD.VRSERVFATUDT) THEN
         SELECT MAX(DTINIAPURACROP), MAX(DTFINAPURACROP)
         INTO V_DTINICIAL, V_DTFINAL
         FROM APURACAOROP
         WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);

         IF (NVL(V_IDFECHDIAROP,'N') <> 'S') THEN
            
            IF (:NEW.DTFATURAM < V_DTFINAL)  AND (:NEW.DTFATURAM < V_DTINICIAL) THEN
               UPDATE APURACAOROP
               SET DTINIAPURACROP  = :NEW.DTFATURAM
               WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
            END IF;
         ELSE
            DELETE ROPFILIAL
            WHERE CDFILIAL     = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL)  AND
                  DTFILIALROP  >= :NEW.DTFATURAM;
            IF (:NEW.DTFATURAM < V_DTINICIAL) THEN
               UPDATE APURACAOROP
               SET  DTINIAPURACROP  = :NEW.DTFATURAM
               WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
            END IF;
         END IF;
      END IF;
   END IF;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('ATUALIZA_DADOS_EFECLIFAT','5.11.000'); END;
/