diff --git a/database.py b/database.py index e6e73a1..12e872a 100644 --- a/database.py +++ b/database.py @@ -693,6 +693,13 @@ class Company(Base): ceidg_raw_data = Column(PG_JSONB) ceidg_fetched_at = Column(DateTime) + # === KRS DATA (API prs.ms.gov.pl) === + krs_raw_data = Column(PG_JSONB) # Pełna odpowiedź z KRS API + krs_fetched_at = Column(DateTime) + krs_registration_date = Column(Date) # Data rejestracji w KRS + krs_representation = Column(Text) # Sposób reprezentacji spółki + krs_activities = Column(PG_JSONB, default=[]) # Przedmiot działalności + # Data source tracking data_source = Column(String(100)) data_quality_score = Column(Integer) diff --git a/database/migrations/037_krs_extended_data.sql b/database/migrations/037_krs_extended_data.sql new file mode 100644 index 0000000..bcc208b --- /dev/null +++ b/database/migrations/037_krs_extended_data.sql @@ -0,0 +1,29 @@ +-- ============================================================ +-- 037_krs_extended_data.sql +-- Rozszerzone dane z KRS API +-- ============================================================ + +-- Surowe dane z KRS API (JSONB) +ALTER TABLE companies ADD COLUMN IF NOT EXISTS krs_raw_data JSONB; + +-- Timestamp ostatniego pobrania z KRS +ALTER TABLE companies ADD COLUMN IF NOT EXISTS krs_fetched_at TIMESTAMP; + +-- Data rejestracji w KRS +ALTER TABLE companies ADD COLUMN IF NOT EXISTS krs_registration_date DATE; + +-- Sposób reprezentacji +ALTER TABLE companies ADD COLUMN IF NOT EXISTS krs_representation TEXT; + +-- Przedmiot działalności z KRS (JSONB array) +ALTER TABLE companies ADD COLUMN IF NOT EXISTS krs_activities JSONB DEFAULT '[]'; + +-- Komentarze +COMMENT ON COLUMN companies.krs_raw_data IS 'Pełna odpowiedź z API KRS (JSON)'; +COMMENT ON COLUMN companies.krs_fetched_at IS 'Data ostatniego pobrania danych z KRS'; +COMMENT ON COLUMN companies.krs_registration_date IS 'Data rejestracji w KRS'; +COMMENT ON COLUMN companies.krs_representation IS 'Sposób reprezentacji spółki'; +COMMENT ON COLUMN companies.krs_activities IS 'Przedmiot działalności z KRS jako JSON array'; + +-- Grant permissions +GRANT ALL ON TABLE companies TO nordabiz_app; diff --git a/scripts/fetch_ceidg_api.py b/scripts/fetch_ceidg_api.py index c9503c6..c6b64f4 100644 --- a/scripts/fetch_ceidg_api.py +++ b/scripts/fetch_ceidg_api.py @@ -495,7 +495,7 @@ def update_company_from_ceidg(company_id: int, ceidg_data: dict, db) -> bool: def update_company_from_krs(company_id: int, krs_data: dict, db) -> bool: """ - Aktualizuje firmę w bazie danymi z KRS API. + Aktualizuje firmę w bazie WSZYSTKIMI danymi z KRS API. Args: company_id: ID firmy w naszej bazie @@ -518,7 +518,7 @@ def update_company_from_krs(company_id: int, krs_data: dict, db) -> bool: if krs_data.get("nip") and not company.nip: company.nip = krs_data.get("nip") if krs_data.get("regon") and not company.regon: - company.regon = krs_data.get("regon") + company.regon = krs_data.get("regon")[:14] if krs_data.get("regon") else None # Forma prawna if krs_data.get("forma_prawna"): @@ -549,16 +549,32 @@ def update_company_from_krs(company_id: int, krs_data: dict, db) -> bool: if kapital.get("zakladowy"): company.capital_amount = kapital.get("zakladowy") - # Data rejestracji + # Data rejestracji w KRS daty = krs_data.get("daty", {}) if daty.get("rejestracji"): from datetime import datetime as dt try: - company.business_start_date = dt.strptime( - daty.get("rejestracji"), "%Y-%m-%d" + # Format: "10.02.2021" + company.krs_registration_date = dt.strptime( + daty.get("rejestracji"), "%d.%m.%Y" ).date() - except: - pass + # Też ustaw business_start_date jeśli puste + if not company.business_start_date: + company.business_start_date = company.krs_registration_date + except Exception as e: + print(f" [WARN] Nie można sparsować daty: {daty.get('rejestracji')} - {e}") + + # Sposób reprezentacji + if krs_data.get("sposob_reprezentacji"): + company.krs_representation = krs_data.get("sposob_reprezentacji") + + # Przedmiot działalności (PKD z KRS) + if krs_data.get("przedmiot_dzialalnosci"): + company.krs_activities = krs_data.get("przedmiot_dzialalnosci") + + # SUROWE DANE - zapisz całą odpowiedź z API + company.krs_raw_data = krs_data + company.krs_fetched_at = datetime.now() # Data source company.data_source = "KRS API" @@ -568,6 +584,8 @@ def update_company_from_krs(company_id: int, krs_data: dict, db) -> bool: except Exception as e: print(f" [ERROR] Błąd aktualizacji: {e}") + import traceback + traceback.print_exc() return False diff --git a/templates/company_detail.html b/templates/company_detail.html index 0b19bff..70cf827 100755 --- a/templates/company_detail.html +++ b/templates/company_detail.html @@ -1185,6 +1185,8 @@
{% if company.ceidg_fetched_at %} Pobrano: {{ company.ceidg_fetched_at.strftime('%d.%m.%Y %H:%M') }} • + {% elif company.krs_fetched_at %} + Pobrano: {{ company.krs_fetched_at.strftime('%d.%m.%Y %H:%M') }} • {% elif company.last_verified_at %} Zweryfikowano: {{ company.last_verified_at.strftime('%d.%m.%Y %H:%M') }} • {% endif %} @@ -1336,6 +1338,7 @@ {% if company.krs and company.data_source == 'KRS API' and not company.ceidg_id %} + {% set krs = company.krs_raw_data or {} %} {% if company.legal_form %} @@ -1354,6 +1357,9 @@
{{ '{:,.2f}'.format(company.capital_amount).replace(',', ' ') }} PLN
+ {% if krs.kapital and krs.kapital.waluta %} +
Waluta: {{ krs.kapital.waluta }}
+ {% endif %}
{% endif %} @@ -1365,6 +1371,170 @@ + + {% if company.krs_registration_date or (krs.daty and krs.daty.rejestracji) %} +
+
Data rejestracji w KRS
+
+ {% if company.krs_registration_date %} + {{ company.krs_registration_date.strftime('%d.%m.%Y') }} + {% else %} + {{ krs.daty.rejestracji }} + {% endif %} +
+ {% if company.krs_registration_date %} + {% set years_active = ((now.date() - company.krs_registration_date).days / 365.25)|int %} + {% if years_active > 0 %} +
{{ years_active }} lat działalności
+ {% endif %} + {% endif %} +
+ {% endif %} + + + {% if company.krs_representation or krs.sposob_reprezentacji %} +
+
Sposób reprezentacji
+
+ {{ company.krs_representation or krs.sposob_reprezentacji }} +
+
+ {% endif %} + + + {% if krs.zarzad and krs.zarzad|length > 0 %} +
+
+ Zarząd ({{ krs.zarzad|length }} {{ 'osoba' if krs.zarzad|length == 1 else 'osoby' if krs.zarzad|length < 5 else 'osób' }}) +
+
+ {% for osoba in krs.zarzad %} +
+
+ {{ osoba.imie[0] if osoba.imie else '?' }}{{ osoba.nazwisko[0] if osoba.nazwisko else '' }} +
+
+
+ {{ osoba.imie }} {% if osoba.imie_drugie %}{{ osoba.imie_drugie }} {% endif %}{{ osoba.nazwisko }} +
+ {% if osoba.funkcja %} +
{{ osoba.funkcja }}
+ {% endif %} +
+
+ {% endfor %} +
+
+ {% endif %} + + + {% if krs.wspolnicy and krs.wspolnicy|length > 0 %} +
+
+ Wspólnicy ({{ krs.wspolnicy|length }}) +
+
+ {% for wspolnik in krs.wspolnicy %} +
+
+ {{ wspolnik.imie[0] if wspolnik.imie else '?' }}{{ wspolnik.nazwisko[0] if wspolnik.nazwisko else '' }} +
+
+
+ {{ wspolnik.imie }} {{ wspolnik.nazwisko }} +
+ {% if wspolnik.udzialy %} +
+ Udziały: {{ wspolnik.udzialy }} + {% if wspolnik.calosc_udzialow %}(100%){% endif %} +
+ {% endif %} +
+
+ {% endfor %} +
+
+ {% endif %} + + + {% if company.krs_activities and company.krs_activities|length > 0 %} +
+
+ Przedmiot działalności z KRS ({{ company.krs_activities|length }} PKD) +
+
+ {% for pkd in company.krs_activities %} + + {{ pkd }} + + {% endfor %} +
+
+ {% elif krs.przedmiot_dzialalnosci and krs.przedmiot_dzialalnosci|length > 0 %} +
+
+ Przedmiot działalności z KRS ({{ krs.przedmiot_dzialalnosci|length }} PKD) +
+
+ {% for pkd in krs.przedmiot_dzialalnosci %} + + {{ pkd }} + + {% endfor %} +
+
+ {% endif %} + + + {% if krs.adres and (krs.adres.ulica or krs.adres.miejscowosc) %} +
+
Adres siedziby z KRS
+
+ {% if krs.adres.ulica %}{{ krs.adres.ulica }}{% endif %} + {% if krs.adres.nr_domu %} {{ krs.adres.nr_domu }}{% endif %} + {% if krs.adres.nr_lokalu %}/{{ krs.adres.nr_lokalu }}{% endif %}
+ {% if krs.adres.kod_pocztowy %}{{ krs.adres.kod_pocztowy }} {% endif %}{{ krs.adres.miejscowosc or '' }} +
+ {% if krs.adres.powiat or krs.adres.wojewodztwo %} +
+ {% if krs.adres.powiat %}pow. {{ krs.adres.powiat }}, {% endif %}woj. {{ krs.adres.wojewodztwo or '' }} +
+ {% endif %} +
+ {% endif %} + + + {% if krs.czy_opp %} +
+
Status OPP
+
+ Organizacja Pożytku Publicznego +
+
Można przekazać 1,5% podatku
+
+ {% endif %} + + + {% if krs.metadata or krs.daty %} +
+
Informacje o odpisie KRS
+
+ {% if krs.metadata %} + {% if krs.metadata.stan_z_dnia %}
Stan na dzień: {{ krs.metadata.stan_z_dnia }}
{% endif %} + {% if krs.metadata.data_odpisu %}
Data odpisu: {{ krs.metadata.data_odpisu }}
{% endif %} + {% endif %} + {% if krs.daty %} + {% if krs.daty.ostatniego_wpisu %}
Ostatni wpis: {{ krs.daty.ostatniego_wpisu }}{% if krs.daty.numer_ostatniego_wpisu %} (nr {{ krs.daty.numer_ostatniego_wpisu }}){% endif %}
{% endif %} + {% endif %} + {% if company.krs_fetched_at %} +
+ Pobrano z API: {{ company.krs_fetched_at.strftime('%d.%m.%Y %H:%M') }} +
+ {% endif %} +
+
+ {% endif %} + {% endif %}