Mudanças entre as edições de "Tiny Types"
De Grupo Acert
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 | |
− | + | private | |
− | + | FUnformatted: string; | |
− | + | function GetFormatted: string; | |
− | + | public | |
− | + | class operator Implicit(const CNPJ: string): TCNPJ; | |
− | + | 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; | |
− | function Clear(const Doc: string): string; | + | function Clear(const Doc: string): string; |
− | var | + | var |
− | + | Letter: Char; | |
− | begin | + | begin |
− | + | Result := ''; | |
− | + | for Letter in Doc do | |
− | + | begin | |
− | + | if Letter in ['0'..'9'] then | |
− | + | Result := Result + Letter; | |
+ | |||
+ | end; | ||
end; | end; | ||
− | + | { TCNPJ } | |
− | + | class operator TCNPJ.Implicit(const CNPJ: string): TCNPJ; | |
+ | begin | ||
− | + | Result.FUnformatted := Clear(CNPJ); | |
− | + | ||
− | + | end; | |
− | + | function TCNPJ.GetFormatted: string; | |
+ | begin | ||
− | + | Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted); | |
− | + | ||
− | + | end; | |
− | + | class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string; | |
+ | begin | ||
− | + | Result := CNPJ.FUnformatted; | |
− | + | ||
− | + | end; | |
− | + | ||
− | end; | + | |
− | function TCNPJ.IsValid: Boolean; | + | function TCNPJ.IsValid: Boolean; |
− | const | + | const |
− | + | BLACKLIST: array[0..9] of string = ('00000000000000', | |
− | + | '11111111111111', | |
− | + | '22222222222222', | |
− | + | '33333333333333', | |
− | + | '44444444444444', | |
− | + | '55555555555555', | |
− | + | '66666666666666', | |
− | + | '77777777777777', | |
− | + | '88888888888888', | |
− | + | '99999999999999'); | |
− | var | + | var |
− | + | IsInvalidSize: Boolean; | |
− | + | IsBlacklisted: Boolean; | |
− | + | I: Integer; | |
− | + | D1, D2: Integer; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
begin | begin | ||
+ | |||
+ | IsInvalidSize := Length(FUnformatted) <> 14; | ||
+ | IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0; | ||
+ | if IsInvalidSize or IsBlacklisted then | ||
+ | begin | ||
+ | Result := False; | ||
+ | end | ||
+ | else | ||
+ | begin | ||
− | + | D1 := 0; | |
− | + | for I := 1 to 12 do | |
− | + | begin | |
− | + | if I < 5 then | |
− | + | D1 := D1 + (StrToInt(FUnformatted[I]) * (6 - I)) | |
− | + | else | |
− | + | D1 := D1 + (StrToInt(FUnformatted[I]) * (14 - I)); | |
− | + | end; | |
− | + | D1 := 11 - (D1 mod 11); | |
− | + | if D1 >= 10 then | |
− | + | D1 := 0; | |
− | + | D2:= D1 * 2; | |
− | + | ||
− | + | for I := 1 to 12 do | |
+ | begin | ||
− | + | if I < 6 then | |
− | + | D2 := D2 + (StrToInt(FUnformatted[I]) * (7 - I)) | |
− | + | else | |
− | + | D2 := D2 + (StrToInt(FUnformatted[I]) * (15 - I)); | |
− | + | end; | |
− | + | D2 := 11 - (D2 mod 11); | |
− | + | ||
− | + | ||
− | + | if D2 >= 10 then D2 :=0; | |
+ | |||
+ | Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2); | ||
+ | |||
+ | end; | ||
end; | end; | ||
− | |||
− | |||
{ TCPF } | { TCPF } |