quinta-feira, 29 de março de 2012

Cálculo do Módulo10 para o Nosso Número


Neste exemplo, criei em plpgsql, uma função para o cálculo do módulo10, para usar na geração do "Nosso Número" para a remessa e bloquetos do banco do brasil. 


CREATE OR REPLACE FUNCTION "TS_CalculaModulo10BancoBrasili"(xdocumento character varying)
  RETURNS character varying AS
$BODY$
declare 
  XNumero     Varchar;
  XNumeroOrg  Varchar; 
  XCont       Integer;
  XParcial    Integer;
  XParcialPosterior Integer;
  XDivisorM10 Integer;
  XResto      Integer;
  XDigVerM10  Varchar(1);
  XMultiplo   Integer;
  XTotalM10   Integer; 
begin
  /*
   Função responsável por calcular o módulo 10 
   para os campos da linha digitável cobrança banco do brasil
  */
  --Pegando somente os números do documentos
  XNumeroOrg = regexp_replace(XDocumento, '[^[:digit:]]', '', 'g');


  /* 
  *** Calculando Módulo "10" *** 
  */
  XDivisorM10 = 2;
  XParcial    = 0;
  XNumero     = trim(XNumeroOrg);
  XCont       = length(XNumero);
  loop
    XTotalM10 = Cast(SubStr(trim(XNumero),XCont,1) as Integer) * XDivisorM10;
    if XTotalM10 < 10 then
       XParcial    = XParcial    + XTotalM10;
    else
       XParcial    = XParcial    + (Cast(SubStr(XTotalM10,1,1) as Integer) + Cast(SubStr(XTotalM10,2,1) as Integer));
    end if;
    XCont       = XCont - 1;
    if XCont = 0 then
       Exit; 
    end if;
    --Pegando número a número e multiplicando por 2 e 1 e acumulando o resultado linha a linha
    if XDivisorM10 = 1 then
       XDivisorM10 = 2;
    else
       XDivisorM10 = 1;
    end if;


  end loop;
  /*
  Após achar o resto da divisão, encontrar o maior 
  valor da soma e fazer a subtração
  */
  if Length(XParcial) <= 2 then
     XParcialPosterior = (SubStr(Cast(XParcial+10 as Varchar),1,1)||'0');
  elsif Length(XParcial) = 3 then  
     XParcialPosterior = (SubStr(Cast(XParcial+10 as Varchar),1,2)||'0');
  elsif Length(XParcial) = 4 then  
     XParcialPosterior = (SubStr(Cast(XParcial+10 as Varchar),1,2)||'0');
  end if;   
  
  if (XParcialPosterior-XParcial) = 10  then -- Se maior que 9 desmembrar o número e soma-los Resto = 18 fazer 1 + 8 = 9
     Return (0);
  else   
     Return (XParcialPosterior-XParcial);
  end if; 
end;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;



*** Para executar esta função digite: ***
Select "TS_CalculaModulo10BancoBrasili"('12345678')


No próximo post, mostrarei a função que cálcula o DV, usando o módulo11. 

Um comentário:

  1. Olá.
    Quando executado o codigo está aparecendo o erro seguinte.:

    > ERROR: function substr(integer, integer, integer) does not exist
    LINE 1: SELECT XParcial + (Cast(SubStr(XTotalM10,1,1) as Integer)...
    ^
    HINT: No function matches the given name and argument types. You might need to add explicit type casts.
    QUERY: SELECT XParcial + (Cast(SubStr(XTotalM10,1,1) as Integer) + Cast(SubStr(XTotalM10,2,1) as Integer))
    CONTEXT: PL/pgSQL function "Modulo10"(character varying) line 34 at assignment

    > Time: 0,004s

    ResponderExcluir

Comente sempre por favor.