"""Worksheet Account Contract Taxable Revenue logic.""" from typing import Iterable, List, Optional from abacus_common_logic.connectors.database import db from sqlalchemy import exc from payment.constants import error from payment.logic.exceptions import LogicError from payment.models import WorksheetAccountContractTaxableRevenue from payment.repository import worksheet_account_contract_taxable_revenue as repository def bulk_create( event_id: int, create_params: Iterable[dict], ) -> List[WorksheetAccountContractTaxableRevenue]: """Create one or more worksheet_account_contract_taxable_revenue records. Args: event_id (int): id of the event create_params (List[dict]) """ base_params = {'abacus_event_id': event_id} instances = [] for params in create_params: params.update(base_params) record = WorksheetAccountContractTaxableRevenue(**params) instances.append(record) if instances: try: WorksheetAccountContractTaxableRevenue.bulk_create(instances) except exc.IntegrityError: db.session.rollback() raise LogicError(error.ERROR_INTEGRITY) return instances def bulk_delete(event_id: int) -> None: """Soft delete worksheet_account_contract_taxable_revenue records.""" WorksheetAccountContractTaxableRevenue.soft_delete_by_event_id(event_id) def get_list( statement_period_id: int, limit: int, offset: int, contract_ids: Optional[List[int]] = None, closing_balance_ids: Optional[List[int]] = None, ) -> dict: """Get list of worksheet_account_contract_taxable_revenue records.""" items, total_count = WorksheetAccountContractTaxableRevenue.get_filtered_items( statement_period_id=statement_period_id, limit=limit, offset=offset, contract_ids=contract_ids, closing_balance_ids=closing_balance_ids, ) return {'items': items, 'total_count': total_count} def catchup_taxable_revenue( limit: int, offset: int, account_ids: Optional[List[int]] = None, ) -> dict: """Catchup taxable revenue items starting from last payment statement period.""" items, total_count = repository.catchup_taxable_revenue( limit=limit, offset=offset, account_ids=account_ids, ) return {'items': items, 'total_count': total_count}