"""Requests to ows-payment.""" import typing from src.constants import DEFAULT_BATCH_SIZE, TaxCorrectionStatuses, TaxCorrectionTypes from src.models import ( GetTaxCorrectionsResponse, GetTaxCorrectionsVATResponse, NewTaxCorrection, NewTaxCorrectionVAT, ) from src.requests import delete, post from .exceptions import OwsPaymentException SERVICE = 'ows-payment' def get_tax_corrections( correction_type: TaxCorrectionTypes, correction_status: TaxCorrectionStatuses, correction_statement_period_id: typing.Optional[int], contract_ids: typing.Optional[typing.List[int]], limit: int = DEFAULT_BATCH_SIZE, offset: int = 0, ) -> GetTaxCorrectionsResponse: """Get tax corrections.""" path = ( f'/tax-corrections/{correction_type}/{correction_status}' f'/?limit={limit}&offset={offset}' ) body: typing.Dict[str, typing.Any] = {'filters': {}} if correction_statement_period_id: body['filters']['correction_statement_period_id'] = ( correction_statement_period_id ) if contract_ids: body['filters']['contract_ids'] = contract_ids response = post(SERVICE, path, body) if response.status_code != 200: raise OwsPaymentException(f'ERROR in POST {path} with body: {body}') return GetTaxCorrectionsResponse.model_validate(response.json()) def get_tax_corrections_vat( correction_status: TaxCorrectionStatuses, correction_statement_period_id: typing.Optional[int], contract_ids: typing.Optional[typing.List[int]], limit: int = DEFAULT_BATCH_SIZE, offset: int = 0, ) -> GetTaxCorrectionsVATResponse: """Get tax corrections VAT.""" path = ( f'/tax-corrections/vat/{correction_status}' f'/?limit={limit}&offset={offset}' ) body: typing.Dict[str, typing.Any] = {'filters': {}} if correction_statement_period_id: body['filters']['correction_statement_period_id'] = ( correction_statement_period_id ) if contract_ids: body['filters']['contract_ids'] = contract_ids response = post(SERVICE, path, body) if response.status_code != 200: raise OwsPaymentException(f'ERROR in POST {path} with body: {body}') return GetTaxCorrectionsVATResponse.model_validate(response.json()) def delete_tax_corrections( worksheet_tax_correction_ids: typing.List[int], ) -> None: """Delete tax corrections.""" path = '/tax-corrections/bulk' body = {'worksheet_tax_correction_ids': worksheet_tax_correction_ids} response = delete(SERVICE, path, body) if response.status_code != 204: raise OwsPaymentException(f'ERROR in DELETE {path} with body: {body}') def delete_tax_corrections_vat( worksheet_tax_correction_vat_ids: typing.List[int], ) -> None: """Delete tax corrections VAT.""" path = '/tax-corrections/vat/bulk' body = {'worksheet_tax_correction_vat_ids': worksheet_tax_correction_vat_ids} response = delete(SERVICE, path, body) if response.status_code != 204: raise OwsPaymentException(f'ERROR in DELETE {path} with body: {body}') def create_tax_corrections(tax_corrections: typing.List[NewTaxCorrection]) -> None: """Create tax corrections.""" path = '/tax-corrections/bulk' body = [ correction.model_dump(mode='json', exclude={'correction_type'}) for correction in tax_corrections ] response = post(SERVICE, path, body) if response.status_code != 201: raise OwsPaymentException(f'ERROR in POST {path} with body: {body}') def create_tax_corrections_vat( tax_corrections_vat: typing.List[NewTaxCorrectionVAT], ) -> None: """Create tax corrections VAT.""" path = '/tax-corrections/vat/bulk' body = [ correction.model_dump(mode='json', by_alias=True) for correction in tax_corrections_vat ] response = post(SERVICE, path, body) if response.status_code != 201: raise OwsPaymentException(f'ERROR in POST {path} with body: {body}')