CREATE OR REPLACE TRIGGER GRAVA_LOGSOLICITA
BEFORE INSERT OR UPDATE OR DELETE
ON SOLICITA FOR EACH ROW

DECLARE

   V_IDGRAVLOGSOL VARCHAR2(03);
   V_DSRDMUSULSOL VARCHAR2(50);
   V_DSENDUSULSOL VARCHAR2(50);
   V_DSNMUSULSOL  VARCHAR2(50);
   V_DSMETUSULSOL VARCHAR2(250);
   V_CDOPERLSOL   VARCHAR2(12);
   V_IDUTILGERLOG VARCHAR2(1);

BEGIN

   SELECT MAX(IDUTILGERLOG)
   INTO V_IDUTILGERLOG
   FROM PARAMGERAL;

   SELECT MAX(NVL(IDGRAVLOGSOL, 'SSS'))
   INTO V_IDGRAVLOGSOL
   FROM PARASUPRI
   WHERE (CDFILIAL = (SELECT MAX(NVL(CDFILCENTCMP, CDFILIAL))
                      FROM FILIAL
                      WHERE (CDFILIAL = NVL(:NEW.CDFILSOLI, :OLD.CDFILSOLI))));

   WEBTUNNING(V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSRDMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL);

    IF V_IDUTILGERLOG = 'S' THEN
       IF INSERTING THEN
          INSERT INTO LOGSOLICITA
          (CDFILSOLLSOL, CDFILLANLSOL, NRSOLCMPLSOL, DTSOLCMPLSOL, IDTIPLANLSOL, IDOPERALSOL,
           IDSOLEXTRANT, IDSOLEXTRDEP, IDBAIXSOLANT, IDBAIXSOLDEP, DSRDMUSULSOL, DSENDUSULSOL,
           DSNMUSULSOL,  DSMETUSULSOL, CDOPERLSOL, DTOPERASOL, QTITEMSOLCMP, NRNECECMPSOL)
          VALUES
          (:NEW.CDFILSOLI, :NEW.CDFILLANCA, :NEW.NRSOLICMP, :NEW.DTSOLCMP, :NEW.IDTIPLAN, 'I',
           '', :NEW.IDSOLEXTRA, '', :NEW.IDBAIXASOL, V_DSRDMUSULSOL, V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL, SYSDATE, :NEW.QTITEMSOLIC, :NEW.NRNECECMPSOL);
       END IF;

       IF UPDATING THEN
          INSERT INTO LOGSOLICITA
          (CDFILSOLLSOL, CDFILLANLSOL, NRSOLCMPLSOL, DTSOLCMPLSOL, IDTIPLANLSOL, IDOPERALSOL,
           IDSOLEXTRANT, IDSOLEXTRDEP, IDBAIXSOLANT, IDBAIXSOLDEP, DSRDMUSULSOL, DSENDUSULSOL,
           DSNMUSULSOL,  DSMETUSULSOL, CDOPERLSOL, DTOPERASOL, QTITEMSOLCMP, NRNECECMPSOL)
          VALUES
          (:NEW.CDFILSOLI, :NEW.CDFILLANCA, :NEW.NRSOLICMP, :NEW.DTSOLCMP, :NEW.IDTIPLAN, 'A',
           :OLD.IDSOLEXTRA, :NEW.IDSOLEXTRA, :OLD.IDBAIXASOL, :NEW.IDBAIXASOL, V_DSRDMUSULSOL, V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL, SYSDATE, :NEW.QTITEMSOLIC, :NEW.NRNECECMPSOL);
       END IF;

       IF DELETING THEN
          INSERT INTO LOGSOLICITA
          (CDFILSOLLSOL, CDFILLANLSOL, NRSOLCMPLSOL, DTSOLCMPLSOL, IDTIPLANLSOL, IDOPERALSOL,
           IDSOLEXTRANT, IDSOLEXTRDEP, IDBAIXSOLANT, IDBAIXSOLDEP, DSRDMUSULSOL, DSENDUSULSOL,
           DSNMUSULSOL,  DSMETUSULSOL, CDOPERLSOL, DTOPERASOL, QTITEMSOLCMP, NRNECECMPSOL)
          VALUES
          (:OLD.CDFILSOLI, :OLD.CDFILLANCA, :OLD.NRSOLICMP, :OLD.DTSOLCMP, :OLD.IDTIPLAN, 'E',
           :OLD.IDSOLEXTRA, :OLD.IDSOLEXTRA, :OLD.IDBAIXASOL, :OLD.IDBAIXASOL, V_DSRDMUSULSOL, V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL, SYSDATE, :OLD.QTITEMSOLIC, :OLD.NRNECECMPSOL);
       END IF;
    ELSE
       IF INSERTING THEN
          IF SUBSTR(V_IDGRAVLOGSOL, 1, 1) = 'S' THEN
             INSERT INTO LOGSOLICITA
             (CDFILSOLLSOL, CDFILLANLSOL, NRSOLCMPLSOL, DTSOLCMPLSOL, IDTIPLANLSOL, IDOPERALSOL,
              IDSOLEXTRANT, IDSOLEXTRDEP, IDBAIXSOLANT, IDBAIXSOLDEP, DSRDMUSULSOL, DSENDUSULSOL,
              DSNMUSULSOL,  DSMETUSULSOL, CDOPERLSOL, DTOPERASOL, NRNECECMPSOL)
             VALUES
             (:NEW.CDFILSOLI, :NEW.CDFILLANCA, :NEW.NRSOLICMP, :NEW.DTSOLCMP, :NEW.IDTIPLAN, 'I',
              '', :NEW.IDSOLEXTRA, '', :NEW.IDBAIXASOL, V_DSRDMUSULSOL, V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL, SYSDATE, :NEW.NRNECECMPSOL);
          END IF;
       END IF;

       IF UPDATING THEN
          IF SUBSTR(V_IDGRAVLOGSOL, 2, 1) = 'S' THEN
             INSERT INTO LOGSOLICITA
             (CDFILSOLLSOL, CDFILLANLSOL, NRSOLCMPLSOL, DTSOLCMPLSOL, IDTIPLANLSOL, IDOPERALSOL,
              IDSOLEXTRANT, IDSOLEXTRDEP, IDBAIXSOLANT, IDBAIXSOLDEP, DSRDMUSULSOL, DSENDUSULSOL,
              DSNMUSULSOL,  DSMETUSULSOL, CDOPERLSOL, DTOPERASOL, NRNECECMPSOL)
             VALUES
             (:NEW.CDFILSOLI, :NEW.CDFILLANCA, :NEW.NRSOLICMP, :NEW.DTSOLCMP, :NEW.IDTIPLAN, 'A',
              :OLD.IDSOLEXTRA, :NEW.IDSOLEXTRA, :OLD.IDBAIXASOL, :NEW.IDBAIXASOL, V_DSRDMUSULSOL, V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL, SYSDATE, :NEW.NRNECECMPSOL);
          END IF;
       END IF;

       IF DELETING THEN
          IF SUBSTR(V_IDGRAVLOGSOL, 3, 1) = 'S' THEN
             INSERT INTO LOGSOLICITA
             (CDFILSOLLSOL, CDFILLANLSOL, NRSOLCMPLSOL, DTSOLCMPLSOL, IDTIPLANLSOL, IDOPERALSOL,
              IDSOLEXTRANT, IDSOLEXTRDEP, IDBAIXSOLANT, IDBAIXSOLDEP, DSRDMUSULSOL, DSENDUSULSOL,
              DSNMUSULSOL,  DSMETUSULSOL, CDOPERLSOL, DTOPERASOL, NRNECECMPSOL)
             VALUES
             (:OLD.CDFILSOLI, :OLD.CDFILLANCA, :OLD.NRSOLICMP, :OLD.DTSOLCMP, :OLD.IDTIPLAN, 'E',
              :OLD.IDSOLEXTRA, :OLD.IDSOLEXTRA, :OLD.IDBAIXASOL, :OLD.IDBAIXASOL, V_DSRDMUSULSOL, V_DSENDUSULSOL, V_DSNMUSULSOL, V_DSMETUSULSOL, V_CDOPERLSOL, SYSDATE, :OLD.NRNECECMPSOL);
          END IF;
       END IF;
    END IF;
END;
/