CREATE OR REPLACE TRIGGER TRG_UPD_FOTOPRATOSER
AFTER INSERT OR DELETE OR UPDATE
ON FOTOPRASER FOR EACH ROW
DECLARE
V_EXIST_FOTOBLOB NUMBER;
V_SEQ VARCHAR2(20);
BEGIN
   IF INSERTING THEN
      IF (:NEW.IDFOTOPRASER = '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, 'S', :NEW.CDFILIAL, :NEW.CDSERVFILI, :NEW.NRFOTOPRASER,
                :NEW.NRORG, :NEW.CDOPERULTATU, :NEW.CDOPERULTATU, SYSDATE, :NEW.NRORG, :NEW.NRORG, SYSDATE
           FROM DUAL;
      END IF;
   END IF;
   IF UPDATING THEN
      IF ((:OLD.IDFOTOPRASER = '1' AND :NEW.IDFOTOPRASER IS NULL) OR
          (:OLD.IDFOTOPRASER = '2' AND :NEW.IDFOTOPRASER = '1')) THEN
         DELETE FROM FOTOPRATOBLOB
          WHERE CDFILAUXPRAT = :OLD.CDFILAUXPRAT
            AND CDPRATO = :OLD.CDPRATO
            AND CDFILIAL = :OLD.CDFILIAL
            AND CDSERVFILI = :OLD.CDSERVFILI
            AND IDRECEITA = 'S';
         
         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, 'S', :NEW.CDFILIAL, :NEW.CDSERVFILI, :OLD.NRFOTOPRASER,
                :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 CDFILIAL = :OLD.CDFILIAL
            AND CDSERVFILI = :OLD.CDSERVFILI
            AND IDRECEITA = 'S';
   END IF;
END;
/


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