/*
   @C:\web_407\sql\end_conv\oracle\02CV_GERTRG_LOG_CTB_LANCCCUSTO.SQL;
*/
CREATE OR REPLACE TRIGGER LOG_EST_LANCCCUSTO
AFTER INSERT
ON LANCCCUSTO FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(300);
  V_DTOPER       DATE;
  V_COUNT        INTEGER;
BEGIN
   IF INSERTING THEN
      SELECT COUNT(*)
      INTO V_COUNT
      FROM LANCLOTE
      WHERE CDEMPRESA  = :NEW.CDEMPRESA
        AND NRLANCDEFI = :NEW.NRLANCCONT
        AND NRSEQUDEFI = :NEW.NRSEQLANC;

      IF V_COUNT <> 0 THEN
         V_DESC         := '';
         V_TIPO         := '1';
         V_ITPESQ       := :NEW.CDEMPRESA||:NEW.NRLANCCONT||:NEW.NRSEQLANC;

         IF :NEW.CDFILIAL IS NOT NULL THEN
            V_DESC := V_DESC || ' Filial = ' || :NEW.CDFILIAL;
         END IF;

         IF :NEW.CDCENTCUST IS NOT NULL THEN
            V_DESC := V_DESC || ' Centro de Custo = ' || :NEW.CDCENTCUST;
         END IF;

         IF :NEW.VRLANCCUST IS NOT NULL THEN
            V_DESC := V_DESC || ' Valor = ' || :NEW.VRLANCCUST;
         END IF;

         IF V_TIPO <> '0' THEN
            IF NOT V_DESC IS NULL THEN
               NMUSUARIO(V_USUARIO);
               IF NOT V_USUARIO IS NULL THEN
                  V_DTOPER := SYSDATE;
                  INSERT INTO LOGOPERA
                     (NMTABELA,   DTOPERACAO, NMRESPOPER,
                      IDTIPOOPER, CDPESQITEM, DSOPERACAO)
                  VALUES
                     ('LANCCCUSTO',  V_DTOPER, V_USUARIO,
                      V_TIPO,         V_ITPESQ, V_DESC);
               END IF;
            END IF;
         END IF;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_CTB_LANCCCUSTO','4.06.042'); END;
/

