"""Logic for Worksheet Account Contract Payable Details.""" from typing import Any, Dict, Iterable, List from sqlalchemy import exc from payment.constants import error from payment.logic.exceptions import LogicError from payment.models import WorksheetAccountContractPayableDetails from payment.repository import worksheet_account_contract_payable_details as repository def bulk_create( event_id: int, statement_period_id: int, create_params: tuple, ) -> List[WorksheetAccountContractPayableDetails]: """Create one or more worksheet_account_contract_payable_details records. Args: event_id (int): id of the event statement_period_id (int): id of the statement period create_params (tuple): POST parameters per record """ base_params = {'statement_period_id': statement_period_id} instances = [ WorksheetAccountContractPayableDetails(**{**params, **base_params}) for params in create_params ] try: WorksheetAccountContractPayableDetails.bulk_create(instances) except exc.IntegrityError: raise LogicError(error.ERROR_INTEGRITY) return instances def bulk_delete(event_id: int) -> None: """Soft delete worksheet_account_contract_payable_details records.""" WorksheetAccountContractPayableDetails.soft_delete_by_event_id(event_id) def bulk_soft_delete_corrections_by_worksheet_payable_after_tax_ids( worksheet_account_contract_payable_after_tax_ids: Iterable[int], ) -> None: """Soft delete vat/wht correction payable details by after-tax ids. Only rows with `payable_detail_type_id` in (4/withholding_tax_correction, 5/vat_correction) are deleted. """ WorksheetAccountContractPayableDetails.soft_delete_corrections_by_worksheet_payable_after_tax_ids( worksheet_account_contract_payable_after_tax_ids ) def get_filtered_active_records( statement_period_id: int, worksheet_after_tax_ids: List[int] = None, detail_groups: List[str] = None, limit: int = None, offset: int = None, ) -> Dict[str, Any]: """Get worksheet_account_contract_payable_details by statement_period_id and optionally by list of worksheet_account_contract_payable_after_tax_ids. Args: statement_period_id (int): id of the statement period worksheet_after_tax_ids (int): ids of the worksheet_account_contract_payable_after_tax entities detail_groups (str): group names of the reference_payable_detail_type entities limit (int): The limit of the pagination. offset (int): The offset of the pagination. """ # noqa items, total_count = repository.get_filtered_active_records( # noqa statement_period_id=statement_period_id, worksheet_after_tax_ids=worksheet_after_tax_ids, detail_groups=detail_groups, limit=limit, offset=offset, ) return { 'items': items, 'total_count': total_count, } def get_by_payment_group_payment_account_ids( payment_group_payment_account_ids: List[int], payable_detail_type_ids: List[int] = None, limit: int = None, offset: int = None, ) -> Dict[str, Any]: """Get worksheet_account_contract_payable_details by payment_group_payment_account_ids. Args: payment_group_payment_account_ids: List of payment_group_payment_account_ids payable_detail_type_ids: Optional list of payable_detail_type_ids to filter by limit: Maximum number of records to return offset: Number of records to skip Returns: Dict with items and total_count """ items, total_count = repository.get_by_payment_group_payment_account_ids( payment_group_payment_account_ids=payment_group_payment_account_ids, payable_detail_type_ids=payable_detail_type_ids, limit=limit, offset=offset, ) return { 'items': items, 'total_count': total_count, }