"""Адаптация готовых позиций VGO в агрегат таблицы корректировок.""" import uuid from decimal import Decimal from types import SimpleNamespace from src.domain.consolidated_osv import CounterpartyType from src.schemas.consolidations import ( EliminationMatchedPairOut, VgoAssignmentGroupOut, VgoAssignmentRowOut, ) from src.services.correction_source import VgoCorrectionSource, _RuleSpec def _row( # noqa: PLR0913 - компактная фабрика строки ОСВ для одного теста entry_id: uuid.UUID, owner_id: uuid.UUID, counterparty_id: uuid.UUID, account: str, *, debit: Decimal = Decimal("0"), credit: Decimal = Decimal("0"), ): return VgoAssignmentRowOut( entry_ids=[entry_id], legal_entity_id=owner_id, legal_entity_name="Владелец", legal_entity_inn="1234567890", account_code=account, account_root=account.split(".", 1)[0], period="2026 Q1", is_cumulative=True, row_number=1, counterparty_name="Контрагент", counterparty_inn="0987654321", counterparty_id=counterparty_id, counterparty_entity_name="Контрагент", counterparty_entity_inn="0987654321", counterparty_type=CounterpartyType.GSK, match_kind=None, match_stored=True, opening_debit=Decimal("0"), opening_credit=Decimal("0"), turnover_debit=Decimal("0"), turnover_credit=Decimal("0"), closing_debit=debit, closing_credit=credit, warnings=[], ) def test_vgo_positions_form_oriented_company_aggregate() -> None: first_id, second_id, rule_id = (uuid.uuid4() for _ in range(3)) rule = SimpleNamespace( id=rule_id, rule_no=6, article_1_line="1230", article_2_line="1520", side_1="D", measure="closing", elimination_mode="min", ) group = VgoAssignmentGroupOut( rule_id=rule_id, rule_no=6, article_1_line="1230", article_2_line="1520", measure="closing", company_a_id=first_id, company_a_name="Компания А", company_b_id=second_id, company_b_name="Компания Б", amount_a=Decimal("100"), amount_b=Decimal("95"), deviation_amount=Decimal("5"), deviation_percent=Decimal("5.26"), with_discrepancy=[ EliminationMatchedPairOut( rule_id=rule_id, company_a_id=first_id, company_b_id=second_id, amount_a=Decimal("100"), amount_b=Decimal("95"), deviation_amount=Decimal("5"), deviation_percent=Decimal("5.26"), osv_entry_ids_a=[uuid.UUID(int=1)], osv_entry_ids_b=[uuid.UUID(int=2)], ) ], ) rows = [ _row(uuid.UUID(int=1), first_id, second_id, "62.01", debit=Decimal("100")), _row(uuid.UUID(int=2), second_id, first_id, "60.01", credit=Decimal("95")), ] aggregate = VgoCorrectionSource._aggregate(group, _RuleSpec(rule), rows) # type: ignore[arg-type] assert (aggregate.company_1.id, aggregate.company_2.id) == (first_id, second_id) assert aggregate.side_1.amount == Decimal("100.00") assert aggregate.side_2.amount == Decimal("95.00") assert aggregate.deviation_percent == Decimal("5") / Decimal("95") * 100 assert aggregate.exceeds_threshold assert aggregate.side_1.entries[0].entry_ids == [uuid.UUID(int=1)] assert aggregate.pairs[0].side_1.entries[0].entry_ids == [uuid.UUID(int=1)] assert aggregate.pairs[0].side_2.entries[0].entry_ids == [uuid.UUID(int=2)]