
CREATE OR REPLACE TRIGGER ATUALIZA_DADOS_EFETCLIE
BEFORE UPDATE OR INSERT OR DELETE
ON EFETCLIE 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.DTEFETCLIE < V_DTFINAL)  AND (:NEW.DTEFETCLIE < V_DTINICIAL) THEN
           UPDATE APURACAOROP
           SET DTINIAPURACROP  = :NEW.DTEFETCLIE
           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.DTEFETCLIE < V_DTFINAL)  AND (:OLD.DTEFETCLIE < V_DTINICIAL) THEN
           UPDATE APURACAOROP
           SET DTINIAPURACROP  = :OLD.DTEFETCLIE
           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.QTCOMEFETCLI <> :OLD.QTCOMEFETCLI)
         OR (:NEW.QTFUNEFETCLI <> :OLD.QTFUNEFETCLI)
         OR (:NEW.QTDOAEFETCLI <> :OLD.QTDOAEFETCLI) 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.DTEFETCLIE < V_DTFINAL)  AND (:NEW.DTEFETCLIE < V_DTINICIAL) THEN
               UPDATE APURACAOROP
               SET DTINIAPURACROP  = :NEW.DTEFETCLIE
               WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
            END IF;
         ELSE
            DELETE ROPFILIAL
            WHERE CDFILIAL     = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL)  AND
                  DTFILIALROP  >= :NEW.DTEFETCLIE;
            IF (:NEW.DTEFETCLIE < V_DTINICIAL) THEN
               UPDATE APURACAOROP
               SET  DTINIAPURACROP  = :NEW.DTEFETCLIE
               WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
            END IF;
         END IF;
      END IF;
   END IF;
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('ATUALIZA_DADOS_EFETCLIE','5.11.000'); END;
/