Import parteneri: se curata doar CNP-urile invalide, codul persoanelor juridice ramane neatins

This commit is contained in:
2026-08-02 10:29:34 +03:00
parent 349130c369
commit da19090ed9
10 changed files with 754 additions and 139 deletions

91
ALTE/check_semnale_pj.ps1 Normal file
View File

@@ -0,0 +1,91 @@
# 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

49
ALTE/check_swap_tara.ps1 Normal file
View File

@@ -0,0 +1,49 @@
# Cazuri in care CODFISCAL_CNP e invalid dar REGCOMERT_CI_PASS contine un CNP valid (campuri inversate).
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]))
}
$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 } }
}
}
$swap = $rows.Values | Where-Object {
$d = $_.Cod -replace '[^\d]', ''
$r = $_.Reg -replace '[^\d]', ''
-not (Test-CNP $d) -and (Test-CNP $r)
}
"cazuri cu campuri inversate (CNP valid in REGCOMERT) = $($swap.Count)"
$swap | Format-Table -AutoSize Nume, Cod, Reg | Out-String -Width 200
# exista informatie de tara la cumparator?
$f = Get-ChildItem -Path $Dir -Filter 'bts_detalii_facturi_rep_*.xml' | Select-Object -First 1
$t = Get-Content $f.FullName -Raw
$m = [regex]::Match($t, '(?is)<ROW>.*?</ROW>')
''
'=== campurile de tara existente in XML (primul rand) ==='
[regex]::Matches($m.Value, '(?i)<(\w*TARA\w*)>([^<]*)</\1>') | ForEach-Object { " $($_.Groups[1].Value) = [$($_.Groups[2].Value)]" }
''
'=== cate randuri au COMPANIE_TARA completat ==='
$cuTara = 0; $total = 0
Get-ChildItem -Path $Dir -Filter 'bts_detalii_facturi_rep_*.xml' | ForEach-Object {
$txt = Get-Content $_.FullName -Raw
foreach ($mm in [regex]::Matches($txt, '(?i)<COMPANIE_TARA>([^<]*)</COMPANIE_TARA>')) {
$total++
if ($mm.Groups[1].Value.Trim() -ne '') { $cuTara++ }
}
}
"COMPANIE_TARA completat pe $cuTara din $total randuri"