SET TERM ^ ;

CREATE OR ALTER trigger ttbnfs_b8u for tbnfs
active before update position 8
AS
DECLARE VARIABLE VENDEDOR_COMISSAO NUMERIC(15,2);
DECLARE VARIABLE CORRETOR_COMISSAO NUMERIC(15,2);
BEGIN

   IF(COALESCE(NEW.REPRESENTANTE_CODIGO,0)      <> COALESCE(OLD.REPRESENTANTE_CODIGO,0)      OR
      COALESCE(NEW.VENDEDOR_ID ,0)              <> COALESCE(OLD.VENDEDOR_ID ,0)              OR
      COALESCE(NEW.REPRESENTANTE_COMISSAO,0.00) <> COALESCE(OLD.REPRESENTANTE_COMISSAO,0.00) OR
      COALESCE(NEW.VENDEDOR_COMISSAO,0.00)      <> COALESCE(OLD.VENDEDOR_COMISSAO,0.00) OR
      COALESCE(NEW.CORRETOR_ID,0.00)            <> COALESCE(OLD.CORRETOR_ID,0.00)            OR
      COALESCE(NEW.FORMA_COMISSAO,'')           <> COALESCE(OLD.forma_comissao,'')) THEN
      BEGIN

        /* ALTERACAO DE VENDEDOR, APLICAR NOS ITENS */
        IF (COALESCE(NEW.VENDEDOR_ID ,0)        <> COALESCE(OLD.VENDEDOR_ID ,0)) THEN
        BEGIN

             UPDATE  TBNFS_ITEM C SET
             C.VENDEDOR_ID = NEW.VENDEDOR_ID
             WHERE C.NFS_CONTROLE = NEW.CONTROLE;
    
        END

        UPDATE TBNFS_PARCELAMENTO P SET
            P.REPRESENTANTE_ID       = NEW.REPRESENTANTE_CODIGO,
            P.REPRESENTANTE_COMISSAO = NEW.REPRESENTANTE_COMISSAO,
            P.forma_comissao         = NEW.FORMA_COMISSAO,
            P.CORRETOR_ID            = NEW.CORRETOR_ID,
            P.VENDEDOR_ID            = NEW.VENDEDOR_ID,
            P.VENDEDOR_COMISSAO      = NEW.VENDEDOR_COMISSAO
        WHERE P.NFS_CONTROLE = NEW.CONTROLE;

        IF(COALESCE(NEW.REPRESENTANTE_CODIGO,0)      <> COALESCE(OLD.REPRESENTANTE_CODIGO,0)
        OR COALESCE(NEW.REPRESENTANTE_COMISSAO,0.00) <> COALESCE(OLD.REPRESENTANTE_COMISSAO,0.00))THEN
        BEGIN
          IF(COALESCE(NEW.REPRESENTANTE_COMISSAO,0.00) > 0 AND COALESCE(NEW.REPRESENTANTE_CODIGO,0) > 0)THEN
          BEGIN
            EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.CONTROLE,'NFS',1);
          END
          ELSE
          BEGIN
               DELETE FROM TBCOMISSAO A
                 WHERE A.TABELA_ID        = NEW.CONTROLE
                   AND A.ORIGEM           = 'NFS'
                   AND A.REPRESENTANTE_ID > 0;
          END
        END

        IF(COALESCE(NEW.VENDEDOR_ID,0) <> COALESCE(OLD.VENDEDOR_ID,0))THEN
        BEGIN
           :VENDEDOR_COMISSAO = COALESCE((SELECT FIRST 1 B.COMISSAO FROM TBREPRESENTANTE_VENDEDOR B WHERE B.CODIGO = NEW.VENDEDOR_ID AND B.GERAR_COMISSAO = 1),0);
           IF(:VENDEDOR_COMISSAO > 0)THEN
           BEGIN
               EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.CONTROLE,'NFS',2);
           END
           ELSE
           BEGIN
               DELETE FROM TBCOMISSAO A
                 WHERE A.TABELA_ID     = NEW.CONTROLE
                   AND A.ORIGEM        = 'NFS'
                   AND A.VENDEDOR_ID   > 0;
           END
        END

        IF(COALESCE(NEW.CORRETOR_ID,0) <> COALESCE(OLD.CORRETOR_ID,0))THEN
        BEGIN
           :CORRETOR_COMISSAO = COALESCE((SELECT FIRST 1 B.COMISSAO FROM TBCORRETOR B WHERE B.ID = NEW.CORRETOR_ID AND B.GERAR_COMISSAO = 1),0);
           IF(:CORRETOR_COMISSAO > 0)THEN
           BEGIN
               EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.CONTROLE,'NFS',3);
           END
           ELSE
           BEGIN
               DELETE FROM TBCOMISSAO A
                 WHERE A.TABELA_ID     = NEW.CONTROLE
                   AND A.ORIGEM        = 'NFS'
                   AND A.CORRETOR_ID   > 0;
           END
        END

      END

END
^

SET TERM ; ^

