CREATE OR REPLACE TRIGGER LOG_FCT_ITEMCONTRATO
    BEFORE UPDATE OR INSERT OR DELETE ON FCT_ITEMCONTRATO
    FOR EACH ROW
    DECLARE
        PRAGMA AUTONOMOUS_TRANSACTION;
        V_NRLOGDADOSCONT      NUMBER;
        V_CAMPOALTERADO       VARCHAR2(100);
        V_NOMEITEM            VARCHAR2(250);
        V_DSVRANTIGOLISTFIX   VARCHAR2(100);
        V_DSVRNOVOLISTFIX     VARCHAR2(100);

    BEGIN

        IF UPDATING THEN
            IF :NEW.IDADICITEMCONT = 'S' THEN
                BEGIN
                    SELECT NMITEMCONTRATO
                    INTO V_NOMEITEM
                    FROM FCT_ITEMCONTRATO
                    WHERE NRITEMOCONTRATO = :NEW.NRITEMCONTRATOPAI
                        AND NRORG         = :NEW.NRORG
                        AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO;

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN BEGIN
                            V_NOMEITEM := NULL;
                    END;
                END;

                IF V_NOMEITEM IS NOT NULL THEN
                    V_CAMPOALTERADO := 'Itens - '||V_NOMEITEM||' - ';
                ELSE
                    V_CAMPOALTERADO := 'Itens - ';
                END IF;
            ELSE
                    V_CAMPOALTERADO := 'Itens - ';
            END IF;


            IF (NVL(:OLD.CDSERVORCA, 0)) <> (NVL(:NEW.CDSERVORCA, 0)) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Tipo Servico/Produto',
                    :OLD.CDSERVORCA,
                    :NEW.CDSERVORCA,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE CDSERVORCA = :OLD.CDSERVORCA AND NRORG = :NEW.NRORG AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO ),
                    (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE CDSERVORCA = :NEW.CDSERVORCA AND NRORG = :NEW.NRORG AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO )
                );
            END IF;

            IF (NVL(:OLD.CDPRODUTO,0) ) <> ( NVL(:NEW.CDPRODUTO,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Tipo Servico/Produto',
                    :OLD.CDPRODUTO,
                    :NEW.CDPRODUTO,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE CDPRODUTO = :OLD.CDPRODUTO AND NRORG = :NEW.NRORG AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO ),
                    (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE CDPRODUTO = :NEW.CDPRODUTO AND NRORG = :NEW.NRORG AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO )
                );
            END IF;

            IF (NVL(:OLD.NRITEMOCONTRATO,0) ) <> ( NVL(:NEW.NRITEMOCONTRATO,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Servico/Produto',
                    :OLD.NRITEMOCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE NRITEMOCONTRATO = :OLD.NRITEMOCONTRATO AND NRORG = :NEW.NRORG AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO ),
                    (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE NRITEMOCONTRATO = :NEW.NRITEMOCONTRATO AND NRORG = :NEW.NRORG AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO )
                );
            END IF;

            IF (NVL(:OLD.CDHABILITSERV,0) ) <> ( NVL(:NEW.CDHABILITSERV,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Habilidade',
                    :OLD.CDHABILITSERV,
                    :NEW.CDHABILITSERV,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    (SELECT NMHABILITSERV FROM FCT_HABILITSERV WHERE CDHABILITSERV = :OLD.CDHABILITSERV AND NRORG = :NEW.NRORG),
                    (SELECT NMHABILITSERV FROM FCT_HABILITSERV WHERE CDHABILITSERV = :NEW.CDHABILITSERV AND NRORG = :NEW.NRORG)
                );
            END IF;

            IF (NVL(:OLD.CDTPMEDIDASERV,0) ) <> ( NVL(:NEW.CDTPMEDIDASERV,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Tipo de Medida',
                    :OLD.CDTPMEDIDASERV,
                    :NEW.CDTPMEDIDASERV,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    (SELECT DSTPMEDIDASERV FROM FCT_TIPOMEDIDASERV WHERE CDTPMEDIDASERV = :OLD.CDTPMEDIDASERV AND NRORG = :NEW.NRORG),
                    (SELECT DSTPMEDIDASERV FROM FCT_TIPOMEDIDASERV WHERE CDTPMEDIDASERV = :NEW.CDTPMEDIDASERV AND NRORG = :NEW.NRORG)
                );
            END IF;

            IF (NVL(:OLD.QTITEMCONTRATO,0) ) <> ( NVL(:NEW.QTITEMCONTRATO,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Quantidade',
                    :OLD.QTITEMCONTRATO,
                    :NEW.QTITEMCONTRATO,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    TO_CHAR(:OLD.QTITEMCONTRATO , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = ''.,'''),
                    TO_CHAR(:NEW.QTITEMCONTRATO , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS =''.,''')
                );
            END IF;

            IF (NVL(:OLD.CDPRECBASESERV,0) ) <> ( NVL(:NEW.CDPRECBASESERV,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Preco Base',
                    :OLD.CDPRECBASESERV,
                    :NEW.CDPRECBASESERV,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    TO_CHAR((SELECT DSPRECBASESERV FROM FCT_PRECBASESERV WHERE CDPRECBASESERV = :OLD.CDPRECBASESERV AND NRORG = :OLD.NRORG) , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = ''.,'''),
                    TO_CHAR((SELECT DSPRECBASESERV FROM FCT_PRECBASESERV WHERE CDPRECBASESERV = :NEW.CDPRECBASESERV AND NRORG = :NEW.NRORG) , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS =''.,''')
                );
            END IF;

            IF (NVL(:OLD.IDMULTMEDVRTOT,0) ) <> ( NVL(:NEW.IDMULTMEDVRTOT,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);

                IF((NVL(:OLD.IDMULTMEDVRTOT,0) ) = 'S') THEN
                    V_DSVRANTIGOLISTFIX  := 'Sim';
                ELSE
                    V_DSVRANTIGOLISTFIX  := 'Nao';
                END IF;

                IF((NVL(:NEW.IDMULTMEDVRTOT,0) ) = 'S') THEN
                    V_DSVRNOVOLISTFIX := 'Sim';
                ELSE
                    V_DSVRNOVOLISTFIX := 'Nao';
                END IF;

                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Utiliza Medida?',
                    :OLD.IDMULTMEDVRTOT,
                    :NEW.IDMULTMEDVRTOT,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    V_DSVRANTIGOLISTFIX,
                    V_DSVRNOVOLISTFIX                        );
            END IF;

            IF (NVL(:OLD.QTMEDIDACONTR,0) ) <> ( NVL(:NEW.QTMEDIDACONTR,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
            INSERT INTO FCT_LOGDADOSCONT (
                NRLOGDADOSCONT,
                NRSEQCONTRATO,
                NRITEMOCONTRATO,
                DSCAMPOALTERADO,
                VALORANTIGO,
                VALORNOVO,
                NRORG,
                DTALTERACAO,
                CDOPERALTERACAO,
                DSVALORANTIGO,
                DSVALORNOVO
            ) VALUES (
                V_NRLOGDADOSCONT,
                :NEW.NRSEQCONTRATO,
                :NEW.NRITEMOCONTRATO,
                V_CAMPOALTERADO||'Quantidade de Medida',
                :OLD.QTMEDIDACONTR,
                :NEW.QTMEDIDACONTR,
                :NEW.NRORG,
                SYSDATE,
                :NEW.CDOPERULTATU,
                TO_CHAR(:OLD.QTMEDIDACONTR , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = ''.,'''),
                TO_CHAR(:NEW.QTMEDIDACONTR , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS =''.,''')
            );
            END IF;

            IF (NVL(:OLD.VRUNITITEMCONT,0) ) <> ( NVL(:NEW.VRUNITITEMCONT,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Valor Unitario',
                    :OLD.VRUNITITEMCONT,
                    :NEW.VRUNITITEMCONT,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    'R$'||TO_CHAR(:OLD.VRUNITITEMCONT , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = ''.,'''),
                    'R$'||TO_CHAR(:NEW.VRUNITITEMCONT , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = ''.,''')
                );
            END IF;

            IF (NVL(:OLD.IDTPDESCONTO,0) ) <> ( NVL(:NEW.IDTPDESCONTO,0) ) THEN
               IF NVL(:OLD.IDTPDESCONTO, 0) = 'S' THEN
                    V_DSVRANTIGOLISTFIX := 'Sem Ajuste';
                ELSE
                    IF NVL(:OLD.IDTPDESCONTO, 0) = 'V' THEN
                        V_DSVRANTIGOLISTFIX := 'Desconto (valor)';
                    ELSE
                        IF NVL(:OLD.IDTPDESCONTO, 0) = 'P' THEN
                            V_DSVRANTIGOLISTFIX := 'Desconto (porcentagem)';
                        ELSE
                            IF NVL(:OLD.IDTPDESCONTO, 0) = 'X' THEN
                                V_DSVRANTIGOLISTFIX := 'Acrescimo (valor)';
                            ELSE
                                IF NVL(:OLD.IDTPDESCONTO, 0) = 'Y' THEN
                                    V_DSVRANTIGOLISTFIX := 'Acrescimo (porcentagem)';
                                ELSE
                                    V_DSVRANTIGOLISTFIX := NULL;
                                END IF;
                            END IF;
                        END IF;
                    END IF;
                END IF;

                IF NVL(:NEW.IDTPDESCONTO, 0) = 'S' THEN
                    V_DSVRNOVOLISTFIX := 'Sem Ajuste';
                ELSE
                    IF NVL(:NEW.IDTPDESCONTO, 0) = 'V' THEN
                        V_DSVRNOVOLISTFIX := 'Desconto (valor)';
                    ELSE
                        IF NVL(:NEW.IDTPDESCONTO, 0) = 'P' THEN
                            V_DSVRNOVOLISTFIX := 'Desconto (porcentagem)';
                        ELSE
                            IF NVL(:NEW.IDTPDESCONTO, 0) = 'X' THEN
                                V_DSVRNOVOLISTFIX := 'Acrescimo (valor)';
                            ELSE
                                IF NVL(:NEW.IDTPDESCONTO, 0) = 'Y' THEN
                                    V_DSVRNOVOLISTFIX := 'Acrescimo (porcentagem)';
                                ELSE
                                    V_DSVRNOVOLISTFIX := NULL;
                                END IF;
                            END IF;
                        END IF;
                    END IF;
                END IF;

            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Ajuste',
                    :OLD.IDTPDESCONTO,
                    :NEW.IDTPDESCONTO,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    V_DSVRANTIGOLISTFIX,
                    V_DSVRNOVOLISTFIX
                );
            END IF;

            IF (NVL(:OLD.VRDESCONTO,0) ) <> ( NVL(:NEW.VRDESCONTO,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Valor do Ajuste',
                    :OLD.VRDESCONTO,
                    :NEW.VRDESCONTO,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    'R$'||TO_CHAR(:OLD.VRUNITITEMCONT , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = ''.,'''),
                    'R$'||TO_CHAR(:NEW.VRUNITITEMCONT , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS =''.,''')
                );
            END IF;

            IF (NVL(:OLD.VRTOTITEMCONT,0) ) <> ( NVL(:NEW.VRTOTITEMCONT,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Valor Total',
                    :OLD.VRTOTITEMCONT,
                    :NEW.VRTOTITEMCONT,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    TO_CHAR(:OLD.VRTOTITEMCONT , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS = '',.'''),
                    TO_CHAR(:NEW.VRTOTITEMCONT , 'FM999G999G999G999G999D90', 'NLS_NUMERIC_CHARACTERS ='',.''')
                );
            END IF;

        IF (NVL(:OLD.IDFATURAITEM,0) ) <> ( NVL(:NEW.IDFATURAITEM,0) ) THEN
            IF((NVL(:OLD.IDFATURAITEM,0) ) = 'S') THEN
                    V_DSVRANTIGOLISTFIX  := 'Sim';
                ELSE
                    V_DSVRANTIGOLISTFIX  := 'Nao';
                END IF;

                IF((NVL(:NEW.IDFATURAITEM,0) ) = 'S') THEN
                    V_DSVRNOVOLISTFIX := 'Sim';
                ELSE
                    V_DSVRNOVOLISTFIX := 'Nao';
            END IF;


            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'O item ira faturar no contrato?',
                    :OLD.IDFATURAITEM,
                    :NEW.IDFATURAITEM,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    V_DSVRANTIGOLISTFIX,
                    V_DSVRNOVOLISTFIX
                );
        END IF;

            IF (NVL(TO_CHAR(:OLD.DTINICFATCONTR), 0)) <> (NVL(TO_CHAR(:NEW.DTINICFATCONTR), 0)) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Data de Inicio do Faturamento',
                    :OLD.DTINICFATCONTR,
                    :NEW.DTINICFATCONTR,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    NULL,
                    NULL
                );
            END IF;

            IF (NVL(:OLD.IDFATURACONTR,0) ) <> ( NVL(:NEW.IDFATURACONTR,0) ) THEN
                IF NVL(:OLD.IDFATURACONTR, 0) = 'A' THEN
                    V_DSVRANTIGOLISTFIX := 'Periodico';
                ELSE
                    IF NVL(:OLD.IDFATURACONTR, 0) = 'O' THEN
                        V_DSVRANTIGOLISTFIX := 'Modalidade de Pagamento';
                    ELSE
                        IF NVL(:OLD.IDFATURACONTR, 0) = 'P' THEN
                            V_DSVRANTIGOLISTFIX := 'Parcelamento';
                        ELSE
                            IF NVL(:OLD.IDFATURACONTR, 0) = 'X' THEN
                                V_DSVRANTIGOLISTFIX := 'Apuracao';
                            ELSE
                                V_DSVRANTIGOLISTFIX := NULL;
                                END IF;
                            END IF;
                    END IF;
                END IF;

                IF NVL(:NEW.IDFATURACONTR, 0) = 'A' THEN
                    V_DSVRNOVOLISTFIX := 'Periodico';
                ELSE
                    IF NVL(:NEW.IDFATURACONTR, 0) = 'O' THEN
                        V_DSVRNOVOLISTFIX := 'Modalidade de Pagamento';
                    ELSE
                        IF NVL(:NEW.IDFATURACONTR, 0) = 'P' THEN
                            V_DSVRNOVOLISTFIX := 'Parcelamento';
                        ELSE
                            IF NVL(:NEW.IDFATURACONTR, 0) = 'X' THEN
                                V_DSVRNOVOLISTFIX := 'Apuracao';
                            ELSE
                                  V_DSVRNOVOLISTFIX := NULL;
                                END IF;
                            END IF;
                    END IF;
                END IF;

            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Tipo de Faturamento',
                    :OLD.IDFATURACONTR,
                    :NEW.IDFATURACONTR,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    V_DSVRANTIGOLISTFIX,
                    V_DSVRNOVOLISTFIX
                );
            END IF;

            IF (NVL(:OLD.CDMODPGTO,0) ) <> ( NVL(:NEW.CDMODPGTO,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Modalidade de Pagamento',
                    :OLD.CDMODPGTO,
                    :NEW.CDMODPGTO,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    (SELECT DSMODPGTO FROM MODPGTO WHERE CDMODPGTO = :OLD.CDMODPGTO AND NRORG = :NEW.NRORG),
                    (SELECT DSMODPGTO FROM MODPGTO WHERE CDMODPGTO = :NEW.CDMODPGTO AND NRORG = :NEW.NRORG)
                );
            END IF;

            IF (NVL(:OLD.QTPARCELASFATU,0) ) <> ( NVL(:NEW.QTPARCELASFATU,0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Quantidade de Parcelas',
                    :OLD.QTPARCELASFATU,
                    :NEW.QTPARCELASFATU,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    NULL,
                    NULL
                );
            END IF;

            IF (NVL(:OLD.NRDIAVENCPARCELASFATU,0) ) <> ( NVL(:NEW.NRDIAVENCPARCELASFATU,0) ) THEN
                NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    VALORANTIGO,
                    VALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    :NEW.NRITEMOCONTRATO,
                    V_CAMPOALTERADO||'Dia para Vencimento',
                    :OLD.NRDIAVENCPARCELASFATU,
                    :NEW.NRDIAVENCPARCELASFATU,
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    NULL,
                    NULL
                );
                END IF;

                IF (NVL(:OLD.NRGRUPOFATURA,0) ) <> ( NVL(:NEW.NRGRUPOFATURA,0) ) THEN
                    NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                    INSERT INTO FCT_LOGDADOSCONT (
                        NRLOGDADOSCONT,
                        NRSEQCONTRATO,
                        NRITEMOCONTRATO,
                        DSCAMPOALTERADO,
                        VALORANTIGO,
                        VALORNOVO,
                        NRORG,
                        DTALTERACAO,
                        CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                    ) VALUES (
                        V_NRLOGDADOSCONT,
                        :NEW.NRSEQCONTRATO,
                        :NEW.NRITEMOCONTRATO,
                        V_CAMPOALTERADO||'Grupo de Faturamento',
                        :OLD.NRGRUPOFATURA,
                        :NEW.NRGRUPOFATURA,
                        :NEW.NRORG,
                        SYSDATE,
                        :NEW.CDOPERULTATU,
                        (SELECT NMGRUPOFATURA FROM FCT_GRUPOFATURA WHERE NRGRUPOFATURA = :OLD.NRGRUPOFATURA AND NRORG = :NEW.NRORG),
                        (SELECT NMGRUPOFATURA FROM FCT_GRUPOFATURA WHERE NRGRUPOFATURA = :NEW.NRGRUPOFATURA AND NRORG = :NEW.NRORG)
                    );
                END IF;

                IF (NVL(:OLD.IDADICITEMCONT,0) ) <> ( NVL(:NEW.IDADICITEMCONT,0) ) THEN
                    IF((NVL(:OLD.IDMULTMEDVRTOT,0) ) = 'S') THEN
                        V_DSVRANTIGOLISTFIX  := 'Sim';
                    ELSE
                        V_DSVRANTIGOLISTFIX  := 'Nao';
                    END IF;

                    IF((NVL(:NEW.IDMULTMEDVRTOT,0) ) = 'S') THEN
                        V_DSVRNOVOLISTFIX := 'Sim';
                    ELSE
                        V_DSVRNOVOLISTFIX := 'Nao';
                    END IF;

                    NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                    INSERT INTO FCT_LOGDADOSCONT (
                        NRLOGDADOSCONT,
                        NRSEQCONTRATO,
                        NRITEMOCONTRATO,
                        DSCAMPOALTERADO,
                        VALORANTIGO,
                        VALORNOVO,
                        NRORG,
                        DTALTERACAO,
                        CDOPERALTERACAO,
                        DSVALORANTIGO,
                        DSVALORNOVO
                    ) VALUES (
                        V_NRLOGDADOSCONT,
                        :NEW.NRSEQCONTRATO,
                        :NEW.NRITEMOCONTRATO,
                        V_CAMPOALTERADO||'O item e um adicional?',
                        :OLD.IDADICITEMCONT,
                        :NEW.IDADICITEMCONT,
                        :NEW.NRORG,
                        SYSDATE,
                        :NEW.CDOPERULTATU,
                        V_DSVRANTIGOLISTFIX,
                        V_DSVRNOVOLISTFIX
                    );
                END IF;

                IF (NVL(:OLD.NRITEMCONTRATOPAI,0) ) <> ( NVL(:NEW.NRITEMCONTRATOPAI,0) ) THEN
                    NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                    INSERT INTO FCT_LOGDADOSCONT (
                        NRLOGDADOSCONT,
                        NRSEQCONTRATO,
                        NRITEMOCONTRATO,
                        DSCAMPOALTERADO,
                        VALORANTIGO,
                        VALORNOVO,
                        NRORG,
                        DTALTERACAO,
                        CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                    ) VALUES (
                        V_NRLOGDADOSCONT,
                        :NEW.NRSEQCONTRATO,
                        :NEW.NRITEMOCONTRATO,
                        V_CAMPOALTERADO||'Item Pai',
                        :OLD.NRITEMCONTRATOPAI,
                        :NEW.NRITEMCONTRATOPAI,
                        :NEW.NRORG,
                        SYSDATE,
                        :NEW.CDOPERULTATU,
                        (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE NRITEMOCONTRATO = :OLD.NRITEMCONTRATOPAI AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO AND NRORG = :NEW.NRORG),
                        (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE NRITEMOCONTRATO = :NEW.NRITEMCONTRATOPAI AND NRSEQCONTRATO = :NEW.NRSEQCONTRATO AND NRORG = :NEW.NRORG)
                    );
                END IF;

                IF (NVL(:OLD.IDADICCOBRCONT, NULL) ) <> ( NVL(:NEW.IDADICCOBRCONT, NULL) ) THEN
                    IF((NVL(:OLD.IDMULTMEDVRTOT,0) ) = 'S') THEN
                        V_DSVRANTIGOLISTFIX  := 'Sim';
                    ELSE
                        V_DSVRANTIGOLISTFIX  := 'Nao';
                    END IF;

                    IF((NVL(:NEW.IDMULTMEDVRTOT,0) ) = 'S') THEN
                        V_DSVRNOVOLISTFIX := 'Sim';
                    ELSE
                        V_DSVRNOVOLISTFIX := 'Nao';
                    END IF;
                    NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                    INSERT INTO FCT_LOGDADOSCONT (
                        NRLOGDADOSCONT,
                        NRSEQCONTRATO,
                        NRITEMOCONTRATO,
                        DSCAMPOALTERADO,
                        VALORANTIGO,
                        VALORNOVO,
                        NRORG,
                        DTALTERACAO,
                        CDOPERALTERACAO,
                    DSVALORANTIGO,
                    DSVALORNOVO
                    ) VALUES (
                        V_NRLOGDADOSCONT,
                        :NEW.NRSEQCONTRATO,
                        :NEW.NRITEMOCONTRATO,
                        V_CAMPOALTERADO||'O item e um adicional de cobranca?',
                        :OLD.IDADICCOBRCONT,
                        :NEW.IDADICCOBRCONT,
                        :NEW.NRORG,
                        SYSDATE,
                        :NEW.CDOPERULTATU,
                        V_DSVRANTIGOLISTFIX,
                        V_DSVRNOVOLISTFIX
                    );
                END IF;

        ELSE
            IF DELETING THEN
                NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                     NRLOGDADOSCONT,
                     NRSEQCONTRATO,
                     NRITEMOCONTRATO,
                     DSCAMPOALTERADO,
                     VALORANTIGO,
                     VALORNOVO,
                     NRORG,
                     DTALTERACAO,
                     CDOPERALTERACAO,
                     DSVALORANTIGO,
                     DSVALORNOVO
                  ) VALUES (
                    V_NRLOGDADOSCONT,
                     :OLD.NRSEQCONTRATO,
                     :OLD.NRITEMOCONTRATO,
                     'Item deletado',
                     NULL,
                     NULL,
                     :OLD.NRORG,
                     SYSDATE,
                     :OLD.CDOPERULTATU,
                     (SELECT NMITEMCONTRATO FROM FCT_ITEMCONTRATO WHERE NRSEQCONTRATO = :OLD.NRSEQCONTRATO AND NRITEMOCONTRATO = :OLD.NRITEMOCONTRATO AND NRORG = :OLD.NRORG),
                     NULL
                );
            ELSE
                IF INSERTING THEN

                NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                     NRLOGDADOSCONT,
                     NRSEQCONTRATO,
                     NRITEMOCONTRATO,
                     DSCAMPOALTERADO,
                     VALORANTIGO,
                     VALORNOVO,
                     NRORG,
                     DTALTERACAO,
                     CDOPERALTERACAO,
                     DSVALORANTIGO,
                     DSVALORNOVO
                  ) VALUES (
                    V_NRLOGDADOSCONT,
                     :NEW.NRSEQCONTRATO,
                     :NEW.NRITEMOCONTRATO,
                     'Item inserido',
                     NULL,
                     NULL,
                     :NEW.NRORG,
                     SYSDATE,
                     :NEW.CDOPERULTATU,
                     NULL,
                     NULL
                );
                END IF;
            END IF;
        END IF;
    COMMIT;
END;
/