prepare linking of persons and companies

This commit is contained in:
2026-07-22 10:20:10 +02:00
parent 9a73f27518
commit 1cab78b2b4
3 changed files with 60 additions and 15 deletions
+41 -8
View File
@@ -6,8 +6,9 @@ from pprint import pformat
from typing import TYPE_CHECKING, Any, cast
import polars as pl
import sqlalchemy as sql
import sqlalchemy as sa
from dopt_basics.result_pattern import wrap_result
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
from wce_crm import db
from wce_crm.constants import TIMEZONE_CEST
@@ -479,7 +480,7 @@ def page_consulting_to_db(
"[Consulting Page] Call update for sessions:\n%s", pformat(rows_for_db_update)
)
stmt = db.beratung_einzelberatung.update().where(
db.beratung_einzelberatung.c.beratung_id == sql.bindparam("b_beratung_id")
db.beratung_einzelberatung.c.beratung_id == sa.bindparam("b_beratung_id")
)
conn.execute(stmt, rows_for_db_update)
@@ -599,7 +600,7 @@ def page_consulting_linking_companies(
un_id: RecId | None,
) -> list[LinkingConsultationsEntryCompany]:
with db.ENGINE.connect() as conn:
q_relevant_companies = sql.select(
q_relevant_companies = sa.select(
db.grunderfassung_unternehmen.c.un_id,
db.grunderfassung_unternehmen.c.Partnersuche__un_suche,
).where(
@@ -613,7 +614,7 @@ def page_consulting_linking_companies(
q_relevant_companies = q_relevant_companies.subquery("relevant_companies")
stmt = sql.select(
stmt = sa.select(
q_relevant_companies.c.un_id,
db.ext_crm_master_attach.c.ma_unternehmensname,
).select_from(
@@ -650,7 +651,7 @@ def page_consulting_linking_persons(
# if un_id None: list all persons
# else: list just the persons associated with the company
person_select = sql.select(
person_select = sa.select(
db.grunderfassung_personen.c.pers_id,
db.grunderfassung_personen.c.Stammdaten__name,
db.grunderfassung_personen.c.Stammdaten__vorname,
@@ -698,7 +699,7 @@ def companyprofile_page_get_consultations(
un_id: RecId,
) -> CompanyProfileConsultations:
logger.debug("[Call backend] _companyprofile_page_get_consultations")
stmt = sql.select(
stmt = sa.select(
db.beratung_vorgang.c.vorgang_id,
db.beratung_vorgang.c.aktualisiert,
db.beratung_vorgang.c.titel,
@@ -746,10 +747,42 @@ def companyprofile_page_get_consultations(
)
# // initial recording interaction
@wrap_result(10)
def initrec_link_person_company(
un_id: RecId,
pers_id: RecId,
unlink: bool,
) -> None:
if unlink:
deleted_ts = datetime.datetime.now(tz=datetime.UTC)
stmt = (
sa.update(db.zuordnung_personen_unternehmen)
.where(
db.zuordnung_personen_unternehmen.c.un_id == un_id,
db.zuordnung_personen_unternehmen.c.pers_id == pers_id,
db.zuordnung_personen_unternehmen.c.gueltig_bis.is_(None),
)
.values(gueltig_bis=deleted_ts)
)
else:
stmt = (
sqlite_insert(db.zuordnung_personen_unternehmen)
.values(un_id=un_id, pers_id=pers_id)
.on_conflict_do_nothing(
index_elements=["un_id", "pers_id"],
index_where=sa.text("gueltig_bis IS NULL"),
)
)
with db.ENGINE.begin() as conn:
conn.execute(stmt)
# // main page interaction
def _main_page_get_company_list() -> list[MainPageEntry]:
logger.debug("[Call backend] get_company_list")
stmt = sql.select(
stmt = sa.select(
db.grunderfassung_unternehmen.c.un_id,
db.grunderfassung_unternehmen.c.Partnersuche__un_suche,
db.grunderfassung_unternehmen.c.Metadaten_aktualisierung,
@@ -785,7 +818,7 @@ def _main_page_get_company_list() -> list[MainPageEntry]:
def _main_page_get_person_list() -> list[MainPageEntry]:
logger.debug("[Call backend] get_company_list")
stmt = sql.select(
stmt = sa.select(
db.grunderfassung_personen.c.pers_id,
db.grunderfassung_personen.c.Stammdaten__vorname,
db.grunderfassung_personen.c.Stammdaten__name,