fix: нормализация чисел (пробелы/запятые) и дат (DD.MM.YYYY→YYYY-MM-DD)

This commit is contained in:
2026-06-28 10:10:00 +04:00
parent f9a39e0292
commit 69ab09e304
2 changed files with 23 additions and 4 deletions
+5 -2
View File
@@ -152,11 +152,14 @@ def _log_unresolved(contract_id, supplement_id, seq, op, prompt_id, document_id,
def _hash(name, date_start=None): def _hash(name, date_start=None):
"""Нормализованный хеш услуги. Включает date_start чтобы различать периоды.""" """Нормализованный хеш услуги. Включает нормализованный date_start чтобы различать периоды."""
import hashlib import hashlib
key = name.strip().lower() key = name.strip().lower()
if date_start: if date_start:
key += "|" + str(date_start) from services.metrics import normalize_date
nd = normalize_date(str(date_start))
if nd:
key += "|" + nd
return hashlib.sha256(key.encode()).hexdigest()[:16] return hashlib.sha256(key.encode()).hexdigest()[:16]
+18 -2
View File
@@ -63,10 +63,26 @@ class ClassifyMetrics:
def _to_decimal(val) -> Decimal | None: def _to_decimal(val) -> Decimal | None:
"""Safe conversion to Decimal.""" """Safe conversion to Decimal with number normalization."""
if val is None or val == "": if val is None or val == "":
return None return None
try: try:
return Decimal(str(val)) s = str(val).replace("\xa0", "").replace(" ", "").replace(",", ".")
return Decimal(s)
except (InvalidOperation, ValueError): except (InvalidOperation, ValueError):
return None return None
def normalize_date(ds: str) -> str | None:
"""Normalize date to YYYY-MM-DD. Handles DD.MM.YYYY, YYYY-MM-DD, etc."""
if not ds:
return None
import re
# DD.MM.YYYY → YYYY-MM-DD
m = re.match(r"(\d{2})\.(\d{2})\.(\d{4})", ds)
if m:
return f"{m.group(3)}-{m.group(2)}-{m.group(1)}"
# YYYY-MM-DD — already canonical
if re.match(r"\d{4}-\d{2}-\d{2}", ds):
return ds
return ds # return as-is if unrecognized format