Source code for aeat.application.modelo._m349_ledger_guard
"""Modelo 349 raw-ledger safety guard.Modelo 349 operator rows require counterparty identity fields that raw ledgertransactions do not carry. The supported source paths are business invoices orexplicit operador detail rows, so raw intra-community ledger classifications mustfail closed instead of producing a zero-row declaration. The guard reads thebucket transaction catalogue through:class:`~domain.transactions.TransactionCatalogueRepository` only to detectthat refusal condition; it does not resolve registry binding values itself."""from__future__importannotationsfrom...adapters.persistence.profile.transactionsimportTransactionCatalogueRepositoryfrom...coreimportModelofrom...domain.ivaimportIvaCategoryfrom...domain.modelosimportModelo349OperadorRow,ModeloDetailRow,WorkUnitfrom...domain.periodimportperiod_end_date,period_start_datefrom...domain.transactionsimportTransactionCatalogueRepositoryProtocol,TransactionLifecycleStatefrom._action_errorsimportModeloAggregationBindingError_M349_INTRACOM_LEDGER_CATEGORIES=frozenset({IvaCategory.INTRA_COMMUNITY_SUPPLY,IvaCategory.INTRA_COMMUNITY_ACQUISITION_REVERSE_CHARGE,IvaCategory.INTRA_COMMUNITY_TRIANGULATION,},)
[docs]defraise_if_m349_intracom_ledger_rows_need_operator_rows(*,work_unit:WorkUnit,transaction_repository:TransactionCatalogueRepositoryProtocol|None,detail_rows:tuple[ModeloDetailRow,...],)->None:"""Refuse M349 calculation when raw ledger rows lack declarable operator rows."""ifstr(work_unit.modelo)!=Modelo.M349.value:returnifany(isinstance(row,Modelo349OperadorRow)forrowindetail_rows):returnrepository=transaction_repositoryorTransactionCatalogueRepository(bucket_id=work_unit.bucket_id)period_start=period_start_date(work_unit.filing_year,work_unit.period.registry_token)period_end=period_end_date(work_unit.filing_year,work_unit.period.registry_token)transaction_ids=tuple(sorted(transaction.transaction_idfortransactioninrepository.load().transactions.values()iftransaction.lifecycle_stateisTransactionLifecycleState.ACTIVEandtransaction.iva_categoryin_M349_INTRACOM_LEDGER_CATEGORIESandperiod_start<=(transaction.raw.value_dateortransaction.raw.booked_date)<=period_end),)ifnottransaction_ids:returnraiseModeloAggregationBindingError("Modelo 349 calculation found intra-community ledger transactions, but no declarable operator rows ""were available. Raw ledger classifications cannot safely produce Modelo 349 operator records; add ""business invoices or pass operador detail rows with country, VAT number, legal name, clave, and amount.",context={"modelo":Modelo.M349.value,"filing_year":work_unit.filing_year,"period":work_unit.period.registry_token,"transaction_count":len(transaction_ids),"sample_transaction_ids":transaction_ids[:3],},suggestion="aeat app ledger invoice add --help",)