CREATE OR REPLACE TRIGGER LOG_DFE_PARAMFORNE
AFTER DELETE OR INSERT OR UPDATE
ON PARAMFORNE FOR EACH ROW
DECLARE
  V_USUARIO	 VARCHAR2(40);
  V_TIPO	 VARCHAR2(1);
  V_ITPESQ	 VARCHAR2(40);
  V_DESC	 VARCHAR2(500);
  V_CDFILLOGOPER VARCHAR2(4);
  V_DTOPER	 DATE;
BEGIN
   V_TIPO := '0';
   V_DESC := '';
   V_CDFILLOGOPER := '';

   IF DELETING THEN
      IF :OLD.CDFORNECED IS NOT NULL THEN
         V_DESC := V_DESC || ' Forne=' || :OLD.CDFORNECED;
      END IF;
      IF :OLD.NRDIASBLOQNFFOR IS NOT NULL THEN
         V_DESC := V_DESC || ' NrBloqNFForne=' || :OLD.NRDIASBLOQNFFOR;
      END IF;
      IF :OLD.IDBLQNFRELAFOAF IS NOT NULL THEN
         V_DESC := V_DESC || ' BloqFechNFeRelac=' || :OLD.IDBLQNFRELAFOAF;
      END IF;
      IF :OLD.IDOBRIMPXMLNF IS NOT NULL THEN
         V_DESC := V_DESC || ' ImportXMLNFe=' || :OLD.IDOBRIMPXMLNF;
      END IF;
      IF :OLD.IDOBRCHAVACESFO IS NOT NULL THEN
         V_DESC := V_DESC || ' ObrigChaveNFe=' || :OLD.IDOBRCHAVACESFO;
      END IF;
      IF :OLD.IDVERINSFORCHNFE IS NOT NULL THEN
         V_DESC := V_DESC || ' VerifCNPJChave=' || :OLD.IDVERINSFORCHNFE;
      END IF;
      IF :OLD.IDSIMPLESNACIONAL IS NOT NULL THEN
         V_DESC := V_DESC || ' FornOptSimples=' || :OLD.IDSIMPLESNACIONAL;
      END IF;
      IF :OLD.IMPVENCXML IS NOT NULL THEN
         V_DESC := V_DESC || ' ImpVencXMLNFe=' || :OLD.IMPVENCXML;
      END IF;
      IF :OLD.IDREACONSIMPXML IS NOT NULL THEN
         V_DESC := V_DESC || ' ReaConsImpXML=' || :OLD.IDREACONSIMPXML;
      END IF;
	     IF :OLD.IDBLOQALTPRODREL IS NOT NULL THEN
         V_DESC := V_DESC || ' BloqAltProdRel=' || :OLD.IDBLOQALTPRODREL;
      END IF;
	     IF :OLD.IDPROCNOTADEBCRE IS NOT NULL THEN
         V_DESC := V_DESC || ' ProcNFCredSAP=' || :OLD.IDPROCNOTADEBCRE;
      END IF;
	     IF :OLD.IDUTPRIGXML IS NOT NULL THEN
         V_DESC := V_DESC || ' UtiCodProdArqXML=' || :OLD.IDUTPRIGXML;
      END IF;
	     IF :OLD.VRMAXPERMFORN IS NOT NULL THEN
         V_DESC := V_DESC || ' VRMaxPermForn=' || :OLD.VRMAXPERMFORN;
      END IF;
	     IF :OLD.IDREACONFVRNF IS NOT NULL THEN
         V_DESC := V_DESC || ' ReaConfValNF=' || :OLD.IDREACONFVRNF;
      END IF;
      IF :OLD.IDUTDESCITBCST IS NOT NULL THEN
         V_DESC := V_DESC || ' ConsDescBCST=' || :OLD.IDUTDESCITBCST;
      END IF;
      IF :OLD.IDIMPVENCXMLCTE IS NOT NULL THEN
         V_DESC := V_DESC || ' ImpVencXMLCTe=' || :OLD.IDIMPVENCXMLCTE;
      END IF;
      IF :OLD.IDCFOPCTEXML IS NOT NULL THEN
         V_DESC := V_DESC || ' UtDig1CFOPCTeXML=' || :OLD.IDCFOPCTEXML;
      END IF;
      IF :OLD.IDNGRAVARELAC IS NOT NULL THEN
         V_DESC := V_DESC || ' NGravaRelacProdXML=' || :OLD.IDNGRAVARELAC;
      END IF;
      IF :OLD.DTREACONSIMPXML IS NOT NULL THEN
         V_DESC := V_DESC || ' DtReaConsImpXML=' || TO_CHAR(:OLD.DTREACONSIMPXML, 'DD/MM/YYYY');
      END IF;
      IF :OLD.QTREPETEPRODNF IS NOT NULL THEN
         V_DESC := V_DESC || ' QtRepeteProdNF=' || :OLD.QTREPETEPRODNF;
      ELSE
         V_DESC := V_DESC || ' QtRepeteProdNF=Null';
      END IF;
      IF :OLD.CDPRODCONSREPE IS NOT NULL THEN
         V_DESC := V_DESC || ' CdProdConsRepe=' || :OLD.CDPRODCONSREPE;
      ELSE
         V_DESC := V_DESC || ' CdProdConsRepe=Null';
      END IF;


      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDFORNECED;
   ELSE
      IF UPDATING THEN
         IF (:OLD.CDFORNECED                 <> :NEW.CDFORNECED)        OR
            (:OLD.NRDIASBLOQNFFOR            <> :NEW.NRDIASBLOQNFFOR)   OR
            (:OLD.IDBLQNFRELAFOAF            <> :NEW.IDBLQNFRELAFOAF)   OR
            (:OLD.IDOBRIMPXMLNF              <> :NEW.IDOBRIMPXMLNF)     OR
            (:OLD.IDOBRCHAVACESFO            <> :NEW.IDOBRCHAVACESFO)   OR
            (:OLD.IDVERINSFORCHNFE           <> :NEW.IDVERINSFORCHNFE)  OR
            (:OLD.IDSIMPLESNACIONAL          <> :NEW.IDSIMPLESNACIONAL) OR
            (:OLD.IMPVENCXML                 <> :NEW.IMPVENCXML)        OR
            (:OLD.IDREACONSIMPXML            <> :NEW.IDREACONSIMPXML)   OR
            (:OLD.IDBLOQALTPRODREL           <> :NEW.IDBLOQALTPRODREL)  OR
            (:OLD.IDPROCNOTADEBCRE           <> :NEW.IDPROCNOTADEBCRE)  OR
            (:OLD.IDUTPRIGXML                <> :NEW.IDUTPRIGXML)       OR
            (:OLD.VRMAXPERMFORN              <> :NEW.VRMAXPERMFORN)     OR
            (:OLD.IDREACONFVRNF              <> :NEW.IDREACONFVRNF)     OR
            (:OLD.IDUTDESCITBCST             <> :NEW.IDUTDESCITBCST)    OR
            (:OLD.IDIMPVENCXMLCTE            <> :NEW.IDIMPVENCXMLCTE)   OR
            (:OLD.IDCFOPCTEXML               <> :NEW.IDCFOPCTEXML)      OR
            (:OLD.IDNGRAVARELAC              <> :NEW.IDNGRAVARELAC)     OR
            (:OLD.DTREACONSIMPXML            <> :NEW.DTREACONSIMPXML)   OR
            (NVL(:OLD.QTREPETEPRODNF, 0)     <> NVL(:NEW.QTREPETEPRODNF, 0))     OR
            (NVL(:OLD.CDPRODCONSREPE, '!')   <> NVL(:NEW.CDPRODCONSREPE, '!'))
         THEN

            V_TIPO     := '2';
            V_ITPESQ   := :OLD.CDFORNECED;
         END IF;
      ELSE
         V_TIPO    := '1';
         V_ITPESQ  := :NEW.CDFORNECED;
      END IF;

      IF :NEW.CDFORNECED IS NOT NULL THEN
         V_DESC := V_DESC || ' Forne=' || :NEW.CDFORNECED;
      END IF;
      IF :NEW.NRDIASBLOQNFFOR IS NOT NULL THEN
         V_DESC := V_DESC || ' NrBloqNFForne=' || :NEW.NRDIASBLOQNFFOR;
      END IF;
      IF :NEW.IDBLQNFRELAFOAF IS NOT NULL THEN
         V_DESC := V_DESC || ' BloqFechNFeRelac=' || :NEW.IDBLQNFRELAFOAF;
      END IF;
      IF :NEW.IDOBRIMPXMLNF IS NOT NULL THEN
         V_DESC := V_DESC || ' ImportXMLNFe=' || :NEW.IDOBRIMPXMLNF;
      END IF;
      IF :NEW.IDOBRCHAVACESFO IS NOT NULL THEN
         V_DESC := V_DESC || ' ObrigChaveNFe=' || :NEW.IDOBRCHAVACESFO;
      END IF;
      IF :NEW.IDVERINSFORCHNFE IS NOT NULL THEN
         V_DESC := V_DESC || ' VerifCNPJChave=' || :NEW.IDVERINSFORCHNFE;
      END IF;
      IF :NEW.IDSIMPLESNACIONAL IS NOT NULL THEN
         V_DESC := V_DESC || ' FornOptSimples=' || :NEW.IDSIMPLESNACIONAL;
      END IF;
      IF :NEW.IMPVENCXML IS NOT NULL THEN
         V_DESC := V_DESC || ' ImpVencXMLNFe=' || :NEW.IMPVENCXML;
      END IF;
      IF :NEW.IDREACONSIMPXML IS NOT NULL THEN
         V_DESC := V_DESC || ' ReaConsImpXML=' || :NEW.IDREACONSIMPXML;
      END IF;
	     IF :NEW.IDBLOQALTPRODREL IS NOT NULL THEN
         V_DESC := V_DESC || ' BloqAltProdRel=' || :NEW.IDBLOQALTPRODREL;
      END IF;
	     IF :NEW.IDPROCNOTADEBCRE IS NOT NULL THEN
         V_DESC := V_DESC || ' ProcNFCredSAP=' || :NEW.IDPROCNOTADEBCRE;
      END IF;
	     IF :NEW.IDUTPRIGXML IS NOT NULL THEN
         V_DESC := V_DESC || ' UtiCodProdArqXML=' || :NEW.IDUTPRIGXML;
      END IF;
	     IF :NEW.VRMAXPERMFORN IS NOT NULL THEN
         V_DESC := V_DESC || ' VRMaxPermForn=' || :NEW.VRMAXPERMFORN;
      END IF;
	     IF :NEW.IDREACONFVRNF IS NOT NULL THEN
         V_DESC := V_DESC || ' ReaConfValNF=' || :NEW.IDREACONFVRNF;
      END IF;
      IF :NEW.IDUTDESCITBCST IS NOT NULL THEN
         V_DESC := V_DESC || ' ConsDescBCST=' || :NEW.IDUTDESCITBCST;
      END IF;
      IF :NEW.IDIMPVENCXMLCTE IS NOT NULL THEN
         V_DESC := V_DESC || ' ImpVencXMLCTe=' || :NEW.IDIMPVENCXMLCTE;
      END IF;
      IF :NEW.IDCFOPCTEXML IS NOT NULL THEN
         V_DESC := V_DESC || ' UtDig1CFOPCTeXML=' || :NEW.IDCFOPCTEXML;
      END IF;
      IF :NEW.IDNGRAVARELAC IS NOT NULL THEN
         V_DESC := V_DESC || ' NGravaRelacProdXML=' || :NEW.IDNGRAVARELAC;
      END IF;
      IF :NEW.DTREACONSIMPXML IS NOT NULL THEN
         V_DESC := V_DESC || ' DtReaConsImpXML=' || TO_CHAR(:NEW.DTREACONSIMPXML, 'DD/MM/YYYY');
      END IF;
      IF :NEW.QTREPETEPRODNF IS NOT NULL THEN
         V_DESC := V_DESC || ' QtRepeteProdNF=' || :NEW.QTREPETEPRODNF;
      ELSE
         V_DESC := V_DESC || ' QtRepeteProdNF=Null';
      END IF;
      IF :NEW.CDPRODCONSREPE IS NOT NULL THEN
         V_DESC := V_DESC || ' CdProdConsRepe=' || :NEW.CDPRODCONSREPE;
      ELSE
         V_DESC := V_DESC || ' CdProdConsRepe=Null';
      END IF;
   END IF;

   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      IF NOT V_DESC IS NULL THEN
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;

            INSERT INTO LOGOPERA
               (NMTABELA,   DTOPERACAO, NMRESPOPER,
                IDTIPOOPER, CDPESQITEM, DSOPERACAO, CDFILLOGOPER)
            VALUES
               ('PARAMFORNE',  V_DTOPER, V_USUARIO,
                V_TIPO,      V_ITPESQ, V_DESC, V_CDFILLOGOPER);
         END IF;
      END IF;
   END IF;
END;
/