CREATE OR REPLACE TRIGGER TRG_UPD_FOTOPRATOPAD
AFTER INSERT OR DELETE OR UPDATE
ON FOTOPRAPAD FOR EACH ROW
DECLARE
V_EXIST_FOTOBLOB NUMBER;
V_SEQ VARCHAR2(20);
BEGIN
   IF INSERTING THEN
      IF (:NEW.IDFOTOPRAPAD = '1') THEN
         
         NOVO_CODIGO_NT('FOTOPRATOBLOB', V_SEQ);

         INSERT INTO FOTOPRATOBLOB
         (NRFOTOPRATOBLOB, CDFILAUXPRAT, CDPRATO, IDRECEITA, CDFILIAL, CDSERVFILI, NRFOTO,
          NRORG, CDOPERINCLUSAO, CDOPERULTATU, DTINCLUSAO, NRORGINCLUSAO, NRORGULTATU, DTULTATU)
         SELECT TO_NUMBER(V_SEQ), :NEW.CDFILAUXPRAT, :NEW.CDPRATO, 'P', NULL, NULL, :NEW.NRFOTOPRAPAD,
                :NEW.NRORG, :NEW.CDOPERULTATU, :NEW.CDOPERULTATU, SYSDATE, :NEW.NRORG, :NEW.NRORG, SYSDATE
           FROM DUAL;
      END IF;
   END IF;
   IF UPDATING THEN
      IF ((:OLD.IDFOTOPRAPAD = '1' AND :NEW.IDFOTOPRAPAD IS NULL) OR
          (:OLD.IDFOTOPRAPAD = '2' AND :NEW.IDFOTOPRAPAD = '1')) THEN
         DELETE FROM FOTOPRATOBLOB
          WHERE CDFILAUXPRAT = :OLD.CDFILAUXPRAT
            AND CDPRATO = :OLD.CDPRATO
            AND IDRECEITA = 'P';

         NOVO_CODIGO_NT('FOTOPRATOBLOB', V_SEQ);

         INSERT INTO FOTOPRATOBLOB
         (NRFOTOPRATOBLOB, CDFILAUXPRAT, CDPRATO, IDRECEITA, CDFILIAL, CDSERVFILI, NRFOTO,
          NRORG, CDOPERINCLUSAO, CDOPERULTATU, DTINCLUSAO, NRORGINCLUSAO, NRORGULTATU, DTULTATU)
         SELECT TO_NUMBER(V_SEQ), :NEW.CDFILAUXPRAT, :NEW.CDPRATO, 'P', NULL, NULL, :OLD.NRFOTOPRAPAD,
                :NEW.NRORG, :NEW.CDOPERULTATU, :NEW.CDOPERULTATU, SYSDATE, :NEW.NRORG, :NEW.NRORG, SYSDATE 
           FROM DUAL;
      END IF;
   END IF;
   IF DELETING THEN
      DELETE FROM FOTOPRATOBLOB
          WHERE CDFILAUXPRAT = :OLD.CDFILAUXPRAT
            AND CDPRATO = :OLD.CDPRATO
            AND IDRECEITA = 'P';
   END IF;
END;
/


-- VERSAO --
BEGIN ATUVEROBJ('TRG_UPD_FOTOPRATOPAD','6.25.000'); END;
/