92 lines
4.4 KiB
PowerShell
92 lines
4.4 KiB
PowerShell
# Cat de bine putem distinge persoana fizica (PF) de persoana juridica (PJ) in Fidelio,
|
|
# FARA sa ne bazam pe validitatea codului fiscal.
|
|
param([string]$Dir = "D:\PROIECTE\FLORA\FLORA2ROA\EXEMPLE\hotel")
|
|
|
|
function Test-CNP([string]$c) {
|
|
if ($c -notmatch '^\d{13}$') { return $false }
|
|
$w = @(2,7,9,1,4,6,3,5,8,2,7,9); $s = 0
|
|
for ($i = 0; $i -lt 12; $i++) { $s += [int]::Parse($c[$i]) * $w[$i] }
|
|
$r = $s % 11; if ($r -eq 10) { $r = 1 }
|
|
return ($r -eq [int]::Parse($c[12]))
|
|
}
|
|
function Test-CUI([string]$c) {
|
|
if ($c -notmatch '^\d{2,10}$') { return $false }
|
|
$c = $c.PadLeft(10, '0'); $k = '7532175321'; $s = 0
|
|
for ($i = 0; $i -lt 9; $i++) { $s += [int]::Parse($c[$i]) * [int]::Parse($k[$i]) }
|
|
$r = ($s * 10) % 11; if ($r -eq 10) { $r = 0 }
|
|
return ($r -eq [int]::Parse($c[9]))
|
|
}
|
|
|
|
# indicii de denumire pentru persoana juridica
|
|
$cuvintePJ = @('SRL','S.R.L','SA','S.A','SC','S.C','PFA','P.F.A','SNC','SCS','SRL-D','II','I.I',
|
|
'ASOCIATIA','ASOCIATIE','FUNDATIA','SOCIETATE','COMPANY','LTD','LIMITED','GMBH','GMBH.',
|
|
'BV','NV','INC','CORP','GROUP','HOLDING','UNIVERSITE','UNIVERSITATEA','UNIVERSITY',
|
|
'SPITAL','PRIMARIA','MINISTERUL','AGENTIA','INSTITUTUL','SCOALA','LICEUL','COLEGIUL',
|
|
'CLUB','ACS','A.C.S','CAR','CFR','TRANS','TOURS','TRAVEL','IMOB','CONSTRUCT','CONSULT',
|
|
'SOLUTIONS','TECHTRONIC','WORKSHOP','STUDIO','CONCEPT','SERV','COMERCIAL','PROFESSIONAL')
|
|
|
|
function Get-TipDupaNume([string]$nume) {
|
|
$cuv = $nume -split '[^A-Z0-9\.]+' | Where-Object { $_ -ne '' }
|
|
foreach ($c in $cuv) { if ($cuvintePJ -contains $c) { return 'PJ' } }
|
|
return ''
|
|
}
|
|
function Get-TipDupaReg([string]$reg) {
|
|
if ($reg -eq '') { return '' }
|
|
if ($reg -match '^[JFC]\s?\d{1,2}\s?/\s?\d+\s?/\s?\d{4}') { return 'PJ' } # J40/1234/2018
|
|
if ($reg -match '^[JL]\d{6,}') { return 'PJ' } # L2019000159348
|
|
if ($reg -match '^\d{1,2}/\d{4}$') { return 'PJ' } # 54/1997
|
|
if ($reg -match '^[A-Z]{2}\s?\d{6}$') { return 'PF' } # serie CI: ZV 093375
|
|
return ''
|
|
}
|
|
|
|
$rows = @{}
|
|
Get-ChildItem -Path $Dir -Filter 'bts_detalii_facturi_rep_*.xml' | ForEach-Object {
|
|
$t = Get-Content $_.FullName -Raw
|
|
foreach ($m in [regex]::Matches($t, '(?is)<CUMPARATOR>([^<]*)</CUMPARATOR>.*?<CODFISCAL_CNP>([^<]*)</CODFISCAL_CNP>\s*<REGCOMERT_CI_PASS>([^<]*)</REGCOMERT_CI_PASS>')) {
|
|
$nume = $m.Groups[1].Value.Trim().ToUpper()
|
|
$cod = $m.Groups[2].Value.Trim().ToUpper()
|
|
$reg = $m.Groups[3].Value.Trim().ToUpper()
|
|
if ($cod -ne '') { $rows["$nume|$cod|$reg"] = [pscustomobject]@{ Nume=$nume; Cod=$cod; Reg=$reg } }
|
|
}
|
|
}
|
|
|
|
$rez = foreach ($r in $rows.Values) {
|
|
$d = $r.Cod -replace '[^\d]', ''
|
|
$roPrefix = ($r.Cod -replace '[^A-Z]', '') -in @('RO','CUI','CF','CIF')
|
|
$tipNume = Get-TipDupaNume $r.Nume
|
|
$tipReg = Get-TipDupaReg $r.Reg
|
|
$cnpOk = Test-CNP $d
|
|
$cuiOk = (Test-CUI $d) -and -not $cnpOk
|
|
|
|
# decizie: semnale tari intai (nume/reg comert/prefix RO), apoi forma codului
|
|
$tip = ''
|
|
if ($roPrefix) { $tip = 'PJ' }
|
|
elseif ($tipNume -eq 'PJ') { $tip = 'PJ' }
|
|
elseif ($tipReg -eq 'PJ') { $tip = 'PJ' }
|
|
elseif ($cnpOk) { $tip = 'PF' }
|
|
elseif ($tipReg -eq 'PF') { $tip = 'PF' }
|
|
elseif ($cuiOk) { $tip = 'PJ' }
|
|
else { $tip = 'PF?' } # necunoscut, presupunem PF
|
|
|
|
[pscustomobject]@{ Nume=$r.Nume; Cod=$r.Cod; Reg=$r.Reg; Tip=$tip; CnpOk=$cnpOk; CuiOk=$cuiOk }
|
|
}
|
|
|
|
"total = $($rez.Count)"
|
|
$rez | Group-Object Tip | Sort-Object Count -Descending | Format-Table Name, Count | Out-String -Width 100
|
|
|
|
''
|
|
'=== PJ cu cod care NU trece validarea de CUI (raman cu cod, ca sa atentioneze declaratia) ==='
|
|
$pjInvalid = $rez | Where-Object { $_.Tip -eq 'PJ' -and -not $_.CuiOk }
|
|
"numar = $($pjInvalid.Count)"
|
|
$pjInvalid | Select-Object -First 25 | Format-Table -AutoSize Nume, Cod, Reg | Out-String -Width 200
|
|
|
|
''
|
|
'=== PF cu CNP invalid (se vor salva CU CNP GOL) ==='
|
|
$pfInvalid = $rez | Where-Object { $_.Tip -like 'PF*' -and -not $_.CnpOk }
|
|
"numar = $($pfInvalid.Count)"
|
|
$pfInvalid | Select-Object -First 25 | Format-Table -AutoSize Nume, Cod, Reg | Out-String -Width 200
|
|
|
|
''
|
|
'=== risc: clasificate PJ dar codul e CNP valid (nu trebuie sa existe) ==='
|
|
($rez | Where-Object { $_.Tip -eq 'PJ' -and $_.CnpOk }) | Format-Table -AutoSize Nume, Cod, Reg | Out-String -Width 200
|