Mudanças entre as edições de "Tiny Types"

De Grupo Acert
Ir para: navegação, pesquisa
Linha 50: Linha 50:
 
     property CNPJ: TCNPJ read FCNPJ write FCNPJ;
 
     property CNPJ: TCNPJ read FCNPJ write FCNPJ;
 
   end;
 
   end;
−
+
 
 
   var
 
   var
 
     Pessoa: TPessoaNova;
 
     Pessoa: TPessoaNova;
Linha 77: Linha 77:
 
Segue abaixo a unit de exemplo com o código completo das funcionalidades para consulta:
 
Segue abaixo a unit de exemplo com o código completo das funcionalidades para consulta:
  
−
unit DocsBR;
+
  unit DocsBR;
  
−
interface
+
  interface
  
−
type
+
  type
  
−
  TCNPJ = record
+
    TCNPJ = record
−
  private
+
      private
−
    FUnformatted: string;
+
      FUnformatted: string;
−
    function GetFormatted: string;
+
      function GetFormatted: string;
  
−
  public
+
    public
−
    class operator Implicit(const CNPJ: string): TCNPJ;
+
      class operator Implicit(const CNPJ: string): TCNPJ;
−
    class operator Implicit(const CNPJ: TCNPJ): string;
+
      class operator Implicit(const CNPJ: TCNPJ): string;
  
 
     function IsValid(): Boolean;
 
     function IsValid(): Boolean;
Linha 115: Linha 115:
 
   end;
 
   end;
  
−
implementation
+
  implementation
  
−
uses
+
  uses
−
  MaskUtils, StrUtils, SysUtils;
+
    MaskUtils, StrUtils, SysUtils;
  
−
function Clear(const Doc: string): string;
+
  function Clear(const Doc: string): string;
−
var
+
  var
−
  Letter: Char;
+
    Letter: Char;
−
begin
+
  begin
  
−
  Result := '';
+
    Result := '';
  
−
  for Letter in Doc do
+
    for Letter in Doc do
−
  begin
+
    begin
  
−
    if Letter in ['0'..'9'] then
+
      if Letter in ['0'..'9'] then
−
      Result := Result + Letter;
+
        Result := Result + Letter;
 +
 
 +
    end;
  
 
   end;
 
   end;
  
−
end;
+
  { TCNPJ }
  
−
{ TCNPJ }
+
  class operator TCNPJ.Implicit(const CNPJ: string): TCNPJ;
 +
  begin
  
−
class operator TCNPJ.Implicit(const CNPJ: string): TCNPJ;
+
    Result.FUnformatted := Clear(CNPJ);
−
begin
+
  
−
   Result.FUnformatted := Clear(CNPJ);
+
   end;
  
−
end;
+
  function TCNPJ.GetFormatted: string;
 +
  begin
  
−
function TCNPJ.GetFormatted: string;
+
    Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted);
−
begin
+
  
−
   Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted);
+
   end;
  
−
end;
+
  class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string;
 +
  begin
  
−
class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string;
+
    Result := CNPJ.FUnformatted;
−
begin
+
  
−
   Result := CNPJ.FUnformatted;
+
   end;
−
 
+
−
end;
+
  
−
function TCNPJ.IsValid: Boolean;
+
  function TCNPJ.IsValid: Boolean;
−
const
+
  const
−
  BLACKLIST: array[0..9] of string = ('00000000000000',
+
    BLACKLIST: array[0..9] of string = ('00000000000000',
−
                                      '11111111111111',
+
                                        '11111111111111',
−
                                      '22222222222222',
+
                                        '22222222222222',
−
                                      '33333333333333',
+
                                        '33333333333333',
−
                                      '44444444444444',
+
                                        '44444444444444',
−
                                      '55555555555555',
+
                                        '55555555555555',
−
                                      '66666666666666',
+
                                        '66666666666666',
−
                                      '77777777777777',
+
                                        '77777777777777',
−
                                      '88888888888888',
+
                                        '88888888888888',
−
                                      '99999999999999');
+
                                        '99999999999999');
−
var
+
  var
−
  IsInvalidSize: Boolean;
+
    IsInvalidSize: Boolean;
−
  IsBlacklisted: Boolean;
+
    IsBlacklisted: Boolean;
−
  I: Integer;
+
    I: Integer;
−
  D1, D2: Integer;
+
    D1, D2: Integer;
−
begin
+
−
 
+
−
  IsInvalidSize := Length(FUnformatted) <> 14;
+
−
  IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0;
+
−
  if IsInvalidSize or IsBlacklisted then
+
−
  begin
+
−
    Result := False;
+
−
  end
+
−
  else
+
 
   begin
 
   begin
 +
 
 +
    IsInvalidSize := Length(FUnformatted) <> 14;
 +
    IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0;
 +
    if IsInvalidSize or IsBlacklisted then
 +
    begin
 +
      Result := False;
 +
    end
 +
    else
 +
    begin
  
−
    D1 := 0;
+
      D1 := 0;
  
−
    for I := 1 to 12 do
+
      for I := 1 to 12 do
−
    begin
+
      begin
  
−
      if I < 5 then
+
        if I < 5 then
−
        D1 := D1 + (StrToInt(FUnformatted[I]) * (6 - I))
+
          D1 := D1 + (StrToInt(FUnformatted[I]) * (6 - I))
−
      else
+
        else
−
        D1 := D1 + (StrToInt(FUnformatted[I]) * (14 - I));
+
          D1 := D1 + (StrToInt(FUnformatted[I]) * (14 - I));
  
−
    end;
+
      end;
  
−
    D1 := 11 - (D1 mod 11);
+
      D1 := 11 - (D1 mod 11);
  
−
    if D1 >= 10 then
+
      if D1 >= 10 then
−
      D1 := 0;
+
        D1 := 0;
 
      
 
      
−
    D2:= D1 * 2;
+
      D2:= D1 * 2;
−
    for I := 1 to 12 do
+
     
−
    begin
+
      for I := 1 to 12 do
 +
      begin
  
−
      if I < 6 then
+
        if I < 6 then
−
        D2 := D2 + (StrToInt(FUnformatted[I]) * (7 - I))
+
          D2 := D2 + (StrToInt(FUnformatted[I]) * (7 - I))
−
      else
+
        else
−
        D2 := D2 + (StrToInt(FUnformatted[I]) * (15 - I));
+
          D2 := D2 + (StrToInt(FUnformatted[I]) * (15 - I));
  
−
    end;
+
      end;
  
−
    D2 := 11 - (D2 mod 11);
+
      D2 := 11 - (D2 mod 11);
−
   
+
−
    if D2 >= 10 then D2 :=0;
+
  
−
    Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2);
+
      if D2 >= 10 then D2 :=0;
 +
 
 +
      Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2);
 +
 
 +
    end;
  
 
   end;
 
   end;
−
 
−
end;
 
  
 
{ TCPF }
 
{ TCPF }

Edição das 09h36min de 7 de janeiro de 2015