
CREATE OR REPLACE TRIGGER ATUALIZA_DADOS_EFETCLIEPME
BEFORE UPDATE OR INSERT OR DELETE
ON EFETCLIEPME FOR EACH ROW
DECLARE
   V_DTINICIAL       DATE;
   V_DTFINAL         DATE;
   V_IDFECHDIAROP    CHAR;
   V_IDUTPADRROPFIL  CHAR;
   V_IDUTPADRROP     CHAR;
   v_CDFILICENTRAL   VARCHAR2(4);

BEGIN

   IF INSERTING THEN

     SELECT MAX(CDFILCENTPME)
     INTO v_CDFILICENTRAL
     FROM FILIAL
     WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);

     SELECT MAX(DTINIAPURACROP), MAX(DTFINAPURACROP)
     INTO V_DTINICIAL, V_DTFINAL
     FROM APURACAOROP
     WHERE CDFILIAL =v_CDFILICENTRAL;

     IF (NVL(V_IDFECHDIAROP,'N') <> 'S') THEN
        IF (:NEW.DTEFETCLPME < V_DTFINAL)  AND (:NEW.DTEFETCLPME < V_DTINICIAL) THEN
           UPDATE APURACAOROP
           SET DTINIAPURACROP  = :NEW.DTEFETCLPME
           WHERE CDFILIAL = v_CDFILICENTRAL;
        END IF;
     END IF;
   END IF;

   IF INSERTING THEN

     SELECT MAX(CDFILCENTPME)
     INTO v_CDFILICENTRAL
     FROM FILIAL
     WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);

     SELECT MAX(DTINIAPURACROP), MAX(DTFINAPURACROP)
     INTO V_DTINICIAL, V_DTFINAL
     FROM APURACAOROP
     WHERE CDFILIAL =v_CDFILICENTRAL;

     IF (NVL(V_IDFECHDIAROP,'N') <> 'S') THEN
        IF (:OLD.DTEFETCLPME < V_DTFINAL)  AND (:OLD.DTEFETCLPME < V_DTINICIAL) THEN
           UPDATE APURACAOROP
           SET DTINIAPURACROP  = :OLD.DTEFETCLPME
           WHERE CDFILIAL = v_CDFILICENTRAL;
        END IF;
     END IF;
   END IF;

   IF UPDATING THEN
      SELECT MAX(IDUTPADRROP)
      INTO V_IDUTPADRROP
      FROM PARAMGERAL G;
      
      SELECT MAX(CDFILCENTPME)
         INTO v_CDFILICENTRAL
         FROM FILIAL
         WHERE CDFILIAL = NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);

      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;
      --RAISE_APPLICATION_ERROR(-20151,TO_CHAR(:NEW.QTFUNEFCLPME)||'TESTE'||TO_CHAR(:OLD.QTFUNEFCLPME), TRUE);
      IF (:NEW.QTCOMEFCLPME <> :OLD.QTCOMEFCLPME)
         OR (:NEW.QTFUNEFCLPME <> :OLD.QTFUNEFCLPME)
         OR (:NEW.QTDOAEFCLPME <> :OLD.QTDOAEFCLPME) THEN
         SELECT MAX(DTINIAPURACROP), MAX(DTFINAPURACROP)
         INTO V_DTINICIAL, V_DTFINAL
         FROM APURACAOROP
         WHERE CDFILIAL =v_CDFILICENTRAL;
         --RAISE_APPLICATION_ERROR(-20151,V_DTINICIAL, TRUE);

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