Update wehago matching logic and exclude reports
This commit is contained in:
@@ -2599,6 +2599,87 @@ def supplement_group_with_missing_wehago_rows(
|
||||
return supplemented
|
||||
|
||||
|
||||
def raw_erp_context_row(entry: dict[str, Any], group: dict[str, Any]) -> dict[str, Any]:
|
||||
summary = group.get("summary") or {}
|
||||
category = _classify_account_category(entry.get("account_code"), entry.get("account_name"))
|
||||
amount, side = raw_erp_entry_signed_amount_side(entry, category)
|
||||
description = " ".join(
|
||||
part for part in (clean(entry.get("desc1")), clean(entry.get("desc2"))) if part
|
||||
)
|
||||
row = {
|
||||
"fiscal_year": int(summary.get("fiscal_year") or YEAR),
|
||||
"status_label": clean(summary.get("status_label")),
|
||||
"ledger_date": clean(summary.get("ledger_date")),
|
||||
"proof_date": clean(entry.get("proof_date")),
|
||||
"voucher_no": clean(summary.get("voucher_no")),
|
||||
"draft_no": clean(entry.get("draft_no")) or clean(entry.get("confirmed_no")),
|
||||
"ledger_account_name": "",
|
||||
"voucher_account_name": clean(entry.get("account_name")),
|
||||
"ledger_vendor": "",
|
||||
"voucher_vendor": clean(entry.get("vendor_name")),
|
||||
"ledger_debit": 0.0,
|
||||
"ledger_credit": 0.0,
|
||||
"voucher_debit": amount if side == "debit" else 0.0,
|
||||
"voucher_credit": amount if side == "credit" else 0.0,
|
||||
"ledger_desc": "",
|
||||
"voucher_desc": description,
|
||||
"review_reason": "ERP_FULL_DRAFT_VOUCHER_CONTEXT",
|
||||
"matched_case": "ERP_CONTEXT_ROW",
|
||||
"ledger_row_key": "",
|
||||
"voucher_row_key": "",
|
||||
"match_identity_key": "",
|
||||
}
|
||||
row["voucher_row_key"] = build_voucher_row_key(row)
|
||||
row["match_identity_key"] = row["voucher_row_key"]
|
||||
return row
|
||||
|
||||
|
||||
def supplement_group_with_full_erp_vouchers(
|
||||
group: dict[str, Any],
|
||||
status_key: str,
|
||||
) -> dict[str, Any]:
|
||||
if status_key not in {"voucher_matched", "voucher_recheck"}:
|
||||
return group
|
||||
rows = [dict(row) for row in group.get("rows") or []]
|
||||
requested_bases = row_draft_bases(rows, group.get("summary") or {})
|
||||
if not requested_bases:
|
||||
return group
|
||||
seen_voucher_rows = {
|
||||
voucher_row_identity(row)
|
||||
for row in rows
|
||||
if has_erp_value(row)
|
||||
}
|
||||
seen_draft_rows = {
|
||||
clean(row.get("draft_no"))
|
||||
for row in rows
|
||||
if has_erp_value(row) and clean(row.get("draft_no"))
|
||||
}
|
||||
added = False
|
||||
for draft_base in sorted(requested_bases):
|
||||
for entry in RAW_ERP_ROWS_BY_DRAFT_BASE.get(draft_base, []):
|
||||
context_row = raw_erp_context_row(entry, group)
|
||||
draft_no = clean(context_row.get("draft_no"))
|
||||
if draft_no and draft_no in seen_draft_rows:
|
||||
continue
|
||||
identity = voucher_row_identity(context_row)
|
||||
if identity in seen_voucher_rows:
|
||||
continue
|
||||
rows.append(context_row)
|
||||
seen_voucher_rows.add(identity)
|
||||
if draft_no:
|
||||
seen_draft_rows.add(draft_no)
|
||||
added = True
|
||||
if not added:
|
||||
return group
|
||||
supplemented = {
|
||||
"summary": dict(group.get("summary") or {}),
|
||||
"rows": rows,
|
||||
"source_group_index": group.get("source_group_index"),
|
||||
}
|
||||
supplemented["summary"] = rebuild_summary(supplemented, status_key)
|
||||
return supplemented
|
||||
|
||||
|
||||
def insert_group(conn: sqlite3.Connection, signature: str, status_key: str, group_index: int, group: dict[str, Any]) -> None:
|
||||
summary = dict(group["summary"])
|
||||
summary.update(
|
||||
@@ -2963,6 +3044,12 @@ def main() -> None:
|
||||
apply_split_draft_row_matches(status_groups)
|
||||
apply_exact_reversal_pairs_to_status_groups(status_groups)
|
||||
invariant_diagnostics = enforce_wehago_status_invariants(status_groups)
|
||||
log_step("expanding matched ERP vouchers for display")
|
||||
for display_status_key in ("voucher_matched", "voucher_recheck"):
|
||||
status_groups[display_status_key] = [
|
||||
supplement_group_with_full_erp_vouchers(group, display_status_key)
|
||||
for group in status_groups.get(display_status_key) or []
|
||||
]
|
||||
excepted_wehago_keys = {
|
||||
group_identity(group)
|
||||
for group in status_groups.get("voucher_excepted") or []
|
||||
|
||||
Reference in New Issue
Block a user