aeat.application.ledger._actions_classification module

Bulk and rule-based ledger classification services.

CSV bulk classification parses BulkClassifyRow records, converts populated fields into ManualLedgerTransactionPatch, and applies them through update_manual_transaction_fields(). Rule application evaluates LedgerClassificationRule instances over active transactions and returns ApplyRulesResult.

Batch persistence uses the concrete TransactionCatalogueRepository so the end-of-batch save can compose the catalogue write with bucket events in one secure-object unit of work.

bulk_classify_from_csv(*, bucket_id, csv_text, actor, source_command='aeat app ledger classify --from-csv', transaction_repository=None, bucket_event_repository=None, work_unit_repository=None, calculation_repository=None)[source]

Apply batch classifications from a CSV string.

The CSV must contain transaction_id and classification columns; category_id, business_pct, usage_ratio_id, taxable_base, iva_rate, iva_amount, iva_category, and irpf_category are optional. Blank optional cells are treated as omitted, so a partial CSV classification row preserves existing tax facts instead of clearing them accidentally. Populated tax facts ride the same ManualLedgerTransactionPatch and the update_manual_transaction_fields() write path the single-classify surface uses, so a bulk row persists the same typed taxable_base/iva_rate/iva_amount/iva_category/ irpf_category values as --id-mode classify with identical validation. Unknown columns are rejected before any writes. Rows that fail validation (unknown transaction id, invalid classification value, malformed tax fact, pydantic error) are collected in failures and the remaining valid rows are applied (partial-success semantics matching the ledger import pattern).

Returns a BulkClassifyResult.

Return type:

BulkClassifyResult

Parameters:
add_classification_rule(*, bucket_id, description_pattern, classification, category_id=None, priority=100, actor, rule_repository=None)[source]

Persist a new ledger classification rule and return it.

rule_id is content-addressed: adding the same description_pattern + classification + category_id combination twice produces the same id and the repository save overwrites the prior entry (idempotent creation).

Returns a LedgerClassificationRule.

Raises ValueError when description_pattern is not a valid regex, as validated by LedgerClassificationRule.

Return type:

LedgerClassificationRule

Parameters:
apply_classification_rules(*, bucket_id, reaffirm=False, actor, source_command='aeat app ledger rule apply', transaction_repository=None, bucket_event_repository=None, rule_repository=None)[source]

Apply stored classification rules to unclassified ACTIVE transactions.

Scope: ACTIVE transactions in NOT_YET_PROCESSED state. When reaffirm=True, also includes ACTIVE transactions where classified_by == "manual" so the operator can explicitly re-run the rule engine over manually classified rows.

Rules are evaluated in priority order (lower number = higher priority); the first matching rule wins. Match is re.search(pattern, description, re.IGNORECASE).

Returns an ApplyRulesResult.

Return type:

ApplyRulesResult

Parameters: