Mudanças entre as edições de "Tiny Types"
De Grupo Acert
Linha 73: | Linha 73: | ||
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; | |
− | + | ||
− | + | property Unformatted: string read FUnformatted; | |
− | + | property Formatted: string read GetFormatted; | |
− | + | end; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | TCPF = record | ||
+ | private | ||
+ | FUnformatted: string; | ||
+ | |||
+ | function GetFormatted: string; | ||
+ | public | ||
+ | class operator Implicit(const CPF: string): TCPF; | ||
+ | class operator Implicit(const CPF: TCPF): string; | ||
+ | |||
+ | function IsValid(): Boolean; | ||
+ | |||
+ | property Unformatted: string read FUnformatted; | ||
+ | property Formatted: string read GetFormatted; | ||
+ | |||
+ | end; | ||
+ | |||
implementation | implementation | ||
− | + | ||
uses | uses | ||
− | + | 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; | class operator TCNPJ.Implicit(const CNPJ: string): TCNPJ; | ||
begin | begin | ||
− | + | ||
Result.FUnformatted := Clear(CNPJ); | Result.FUnformatted := Clear(CNPJ); | ||
− | + | ||
end; | end; | ||
− | + | ||
function TCNPJ.GetFormatted: string; | function TCNPJ.GetFormatted: string; | ||
begin | begin | ||
− | + | ||
Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted); | Result := MaskUtils.FormatMaskText('00.000.000/0000-00;0; ', FUnformatted); | ||
− | + | ||
end; | end; | ||
− | + | ||
class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string; | class operator TCNPJ.Implicit(const CNPJ: TCNPJ): string; | ||
begin | begin | ||
− | + | ||
Result := CNPJ.FUnformatted; | Result := CNPJ.FUnformatted; | ||
− | + | ||
end; | end; | ||
− | + | ||
function TCNPJ.IsValid: Boolean; | function TCNPJ.IsValid: Boolean; | ||
const | const | ||
Linha 173: | Linha 173: | ||
D1, D2: Integer; | D1, D2: Integer; | ||
begin | begin | ||
− | + | ||
IsInvalidSize := Length(FUnformatted) <> 14; | IsInvalidSize := Length(FUnformatted) <> 14; | ||
IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0; | IsBlacklisted := AnsiIndexStr(FUnformatted, BLACKLIST) >= 0; | ||
+ | |||
if IsInvalidSize or IsBlacklisted then | if IsInvalidSize or IsBlacklisted then | ||
begin | begin | ||
Linha 182: | Linha 183: | ||
else | else | ||
begin | 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 | for I := 1 to 12 do | ||
begin | 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; | if D2 >= 10 then D2 :=0; | ||
− | + | ||
Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2); | Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 13, 2); | ||
− | + | ||
end; | end; | ||
− | + | ||
end; | end; | ||
− | + | ||
{ TCPF } | { TCPF } | ||
class operator TCPF.Implicit(const CPF: string): TCPF; | class operator TCPF.Implicit(const CPF: string): TCPF; | ||
begin | begin | ||
− | + | ||
Result.FUnformatted := Clear(CPF); | Result.FUnformatted := Clear(CPF); | ||
− | + | ||
end; | end; | ||
− | + | ||
function TCPF.GetFormatted: string; | function TCPF.GetFormatted: string; | ||
begin | begin | ||
− | + | ||
Result := MaskUtils.FormatMaskText('000.000.000-00;0; ', FUnformatted); | Result := MaskUtils.FormatMaskText('000.000.000-00;0; ', FUnformatted); | ||
− | + | ||
end; | end; | ||
− | + | ||
class operator TCPF.Implicit(const CPF: TCPF): string; | class operator TCPF.Implicit(const CPF: TCPF): string; | ||
begin | begin | ||
− | + | ||
Result := CPF.FUnformatted; | Result := CPF.FUnformatted; | ||
− | + | ||
end; | end; | ||
− | + | ||
function TCPF.IsValid: Boolean; | function TCPF.IsValid: Boolean; | ||
const | const | ||
Linha 272: | Linha 273: | ||
else | else | ||
begin | begin | ||
− | + | ||
D1 := 0; | D1 := 0; | ||
− | + | ||
for I := 1 to 9 do | for I := 1 to 9 do | ||
D1 := D1 + (StrToInt(FUnformatted[I]) * (11 - I)); | D1 := D1 + (StrToInt(FUnformatted[I]) * (11 - I)); | ||
Linha 281: | Linha 282: | ||
if D1 >= 10 then | if D1 >= 10 then | ||
− | + | ||
D1 := 0; | D1 := 0; | ||
Linha 288: | Linha 289: | ||
for I := 1 to 9 do | for I := 1 to 9 do | ||
D2 := D2 + (StrToInt(FUnformatted[I]) * (12 - I)); | D2 := D2 + (StrToInt(FUnformatted[I]) * (12 - I)); | ||
− | + | ||
D2 := 11 - (D2 mod 11); | D2 := 11 - (D2 mod 11); | ||
if D2 >= 10 then | if D2 >= 10 then | ||
D2 :=0; | D2 :=0; | ||
− | + | ||
Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 10, 2); | Result := (IntToStr(D1) + IntToStr(D2)) = Copy(FUnformatted, 10, 2); | ||
− | + | ||
end; | end; | ||
− | + | ||
end; | end; | ||
− | + | ||
end. | end. |