"""OWS Utilities.""" from lib import constants from lib.config import ows_client # OWS-ABACUS-EVENT def create_abacus_event( event_name: str, target_id: int, target_type: str ): """POST abacus_event. Args: event_name (str): snake_cased name of the event (i.e. 'commit_royalties') target_id (int): used with target_type; ID of the event's target target_type (str): used with target_id; name of the event's target Returns: a response object """ post_body = { 'event_name': event_name, 'target_id': target_id, 'target_type': target_type } response = ows_client.post( constants.SERVICE_ABACUS_EVENT, '/abacus-event', json=post_body, timeout=20.0 ) print('ABACUS_EVENT PARAMS:', post_body) print('RESPONSE', response) response.raise_for_status() return response.json() # OWS-ROYALTIES def get_accounting_period_details(accounting_period_id): """Get an accounting period object by ID.""" response = ows_client.get( constants.SERVICE_ROYALTIES, f'/accounting-period/{accounting_period_id}' ) response.raise_for_status() return response.json() def update_accounting_period(period_id, body): """Update fields on an accounting period.""" response = ows_client.put( constants.SERVICE_ROYALTIES, f'/accounting-period/{period_id}', json=body ) print('PERIOD_ID', period_id, 'RESPONSE', response) response.raise_for_status() return response.json() def get_accounting_period_accounting_runs(accounting_period_id): """Get a list of runs associated with accounting period.""" response = ows_client.get( constants.SERVICE_ROYALTIES, f'/accounting-period/{accounting_period_id}/accounting-runs' ) response.raise_for_status() return response.json() def create_accounting_period_report(accounting_period_id, report_type, report_url): """POST request to ows-royalties to create accounting period report entry.""" url = f'/accounting-period/{accounting_period_id}/accounting-period-report' body = { 'report_type': report_type, 'report_export_url': report_url } response = ows_client.post( constants.SERVICE_ROYALTIES, url, json=body ) print('PERIOD_ID', accounting_period_id, 'RESPONSE', response) response.raise_for_status() return response.json() def get_accounting_run_details(accounting_run_id): """Get an accounting run object by ID.""" response = ows_client.get( constants.SERVICE_ROYALTIES, f'/accounting-run/{accounting_run_id}' ) response.raise_for_status() return response.json() def update_accounting_run(run_id, **params): """Update fields on an accounting run.""" print('RUN_ID', run_id) print('PARAMS', params) response = ows_client.put( constants.SERVICE_ROYALTIES, f'/accounting-run/{run_id}', json=params ) print('RESPONSE', response) response.raise_for_status() return response.json() def get_accounting_period_sales_files(accounting_period_id): """Get an accounting period sales files by ID.""" response = ows_client.get( constants.SERVICE_ROYALTIES, f'/accounting-period/{accounting_period_id}/sales-files' ) response.raise_for_status() return response.json() def get_exchange_rates_by_statement_period(statement_period_id: int) -> list: """Get exchange rates belonging to the specified statement_period_id. Args: statement_period_id (int): ID of the parent statement_period """ response = ows_client.get( constants.SERVICE_ROYALTIES, f'/statement-period/{statement_period_id}/bulk-exchange-rates' ) print('STATEMENT_PERIOD_ID', statement_period_id) print('RESPONSE', response) response.raise_for_status() return response.json() def get_sales_file_details(sales_file_id): """Get a sales file object by ID.""" response = ows_client.get( constants.SERVICE_ROYALTIES, f'/sales-file/{sales_file_id}' ) response.raise_for_status() return response.json() def update_sales_file(sales_file_id, **params): """Update fields on a sales file.""" response = ows_client.put( constants.SERVICE_ROYALTIES, f'/sales-file/{sales_file_id}', json=params ) print('SALES_FILE_ID', sales_file_id) print('RESPONSE', response) response.raise_for_status() return response.json() def get_sales_file_urls(accounting_period_id: int) -> list: """Get sales file urls for accounting period. Args: accounting_period_id (int): ID of the parent accounting_period Returns: a list of sales_file urls """ sales_files = get_accounting_period_sales_files(accounting_period_id) return [f'{file.get("main_url")}' for file in sales_files] def get_statement_period_details(statement_period_id: int) -> dict: """Get statement_period. Args: statement_period (int): ID of a statement_period Returns: response json """ response = ows_client.get( constants.SERVICE_ROYALTIES, f'/statement-period/{statement_period_id}' ) print('STATEMENT_PERIOD_ID', statement_period_id) print('RESPONSE', response) response.raise_for_status() return response.json() def get_statement_period_adjustment_file_details( statement_period_adjustment_file_id: int ) -> dict: """Get statement_period_adjustment_file. Args: statement_period_adjustment_file_id (int): ID of a statement_period_adjustment_file Returns: response json """ response = ows_client.get( constants.SERVICE_ROYALTIES, f'/statement-period-adjustment-file/{statement_period_adjustment_file_id}' ) print('STATEMENT_PERIOD_ADJUSTMENT_FILE_ID', statement_period_adjustment_file_id) print('RESPONSE', response) response.raise_for_status() return response.json() def update_adjustment_file( statement_period_adjustment_file_id: int, **params ): """Put statement_period_adjustment_file updates. Args: statement_period_adjustment_file_id (int): ID of a statement_period_adjustment_file **params (any): Update parameters for an statement_period_adjustment_file Returns: response json """ request_body = params if 'body' in params: request_body = params['body'] response = ows_client.put( constants.SERVICE_ROYALTIES, f'/statement-period-adjustment-file/{statement_period_adjustment_file_id}', json=request_body ) print('STATEMENT_PERIOD_ADJUSTMENT_FILE_ID', statement_period_adjustment_file_id) print('RESPONSE', response) response.raise_for_status() return response.json() # CONTRACT endpoints def get_contract_details_by_contract_ids(contract_ids: str): """Get detail for all contracts in comma-separated list of contract_ids.""" response = ows_client.post( constants.SERVICE_ROYALTIES, '/contracts/snapshot', json=contract_ids ) response.raise_for_status() return response.text.encode('utf-8') def get_contracts_by_accounting_run(accounting_run_id: int): """Get list of contract_ids belonging to specified accounting run.""" response = ows_client.get( constants.SERVICE_ROYALTIES, f'/accounting-run/{accounting_run_id}/run-controller/contracts' ) response.raise_for_status() return response.json() def get_contract_term_details_by_contract_ids(contract_ids: str): """Get contract terms & conditions for csv list of contract_ids.""" response = ows_client.post( constants.SERVICE_ROYALTIES, '/contract-terms/snapshot', json=contract_ids ) response.raise_for_status() return response.text.encode('utf-8') def get_contract_mechanicals(): """Get contract mechanicals as csv.""" response = ows_client.get( constants.SERVICE_ROYALTIES, '/contract-mechanicals/snapshot', ) response.raise_for_status() return response.text.encode('utf-8') # OWS-PAYMENT def create_or_update_report_payment_group_payment( payment_group_payment_id: int, report_export_url: str, report_type: str ) -> dict: """Create or update a report_payment_group_payment. Args: payment_group_payment_id (int): ID of the parent payment_group_payment report_export_url (str): path to where report is saved on S3 report_type (str): one of "approval" or "summary" Returns: response """ put_params = { 'report_export_url': report_export_url, 'report_type': report_type } response = ows_client.put( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}/report', json=put_params ) print('PAYMENT_GROUP_PAYMENT_ID', payment_group_payment_id) print('REPORT_PAYMENT_GROUP_PAYMENT_PARAMS', put_params) print('RESPONSE', response) response.raise_for_status() return response.json() def get_payment_group_payment_details(payment_group_payment_id: int) -> dict: """Get payment_group_payment. Args: payment_group_payment_id (int): ID of a payment_group_payment record Returns: response json """ response = ows_client.get( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}' ) print('PAYMENT_GROUP_PAYMENT_ID', payment_group_payment_id) print('RESPONSE', response) response.raise_for_status() return response.json() def get_payment_group_details(payment_group_id: int) -> dict: """Get payment_group. Args: payment_group_id (int): ID of a payment_group record Returns: response json """ response = ows_client.get( constants.SERVICE_PAYMENT, f'/payment-group/{payment_group_id}' ) print('PAYMENT_GROUP_ID', payment_group_id) print('RESPONSE', response) response.raise_for_status() return response.json() def get_payment_group_payment_accounts( payment_group_payment_id: int, limit: int = 100, offset: int = 0, sort_by: str = 'account_name', sort_order: str = 'asc' ) -> dict: """Get payment_group_payment_accounts by payment_group_payment. Args: payment_group_payment_id (int): ID of the parent payment_group_payment limit (int): number of items to return; the size of a page offset (int): the number of items to skip before returning responses; the page num Returns: a paginated dict of items and total_count """ url = f'/payment-group-payment/{payment_group_payment_id}/accounts' query_string = f'?limit={limit}&offset={offset}&sort_by={sort_by}&sort_order={sort_order}' # noqa: E501 response = ows_client.get( constants.SERVICE_PAYMENT, f'{url}{query_string}' ) print('PAYMENT_GROUP_PAYMENT_ID', payment_group_payment_id) print('LIMIT', limit) print('OFFSET', offset) print('RESPONSE', response) response.raise_for_status() return response.json() def get_payment_group_payment_reports(payment_group_payment_id: int) -> list: """Get list of report_payment_group_payments by payment_group_payment. Args: payment_group_payment_id (int): ID of the parent payment_group_payment Returns: a list of dicts with report_payment_group_payment data """ response = ows_client.get( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}/reports' ) print('PAYMENT_GROUP_PAYMENT_ID', payment_group_payment_id) print('RESPONSE', response) response.raise_for_status() return response.json() def update_payment_group_payment(payment_group_payment_id, **params): """Update fields on a payment group payment.""" response = ows_client.put( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}', json=params ) print('PAYMENT_GROUP_PAYMENT_ID', payment_group_payment_id) print('RESPONSE', response) response.raise_for_status() return response.json() # OWS-ABACUS-STATE def get_accounting_period_state(accounting_period_id): """Get an abacus state for the specified accounting period id.""" response = ows_client.get( constants.SERVICE_ABACUS_STATE, f'/abacus-state/accounting-period/{accounting_period_id}' ) response.raise_for_status() return response.json() def get_abacus_states(parent_table_name, parent_table_id): """Get abacus states for specified parent_table.""" response = ows_client.get( constants.SERVICE_ABACUS_STATE, f'/abacus-state/{parent_table_name}/{parent_table_id}' ) response.raise_for_status() return response.json() def update_abacus_state(abacus_state_id, body, headers: dict[str, str] = {}): """Update an abacus state by specified abacus state id.""" response = ows_client.put( constants.SERVICE_ABACUS_STATE, f'/abacus-state/{abacus_state_id}', json=body, headers=headers ) print('ABACUS_STATE_ID', abacus_state_id) print('RESPONSE', response) response.raise_for_status() return response.json() def get_abacus_state_id(parent_table_name, parent_table_id, action_name): """Get abacus state_id for provided action_name.""" abacus_states = get_abacus_states(parent_table_name, parent_table_id) filtered_action = list( filter(lambda x: x['action_name'] == action_name, abacus_states) ) if filtered_action: return filtered_action[0]['abacus_state_id'] return None def create_abacus_state( action_name: str, parent_table_name: str, parent_table_id: int ): """POST abacus_state. Args: action_name (str): snake_cased name of the state (i.e. 'commit_royalties') parent_table_name (str): table name (i.e. 'accounting_run') parent_table_id (int): record ID (i.e. 123) Returns: a response object """ post_body = { 'action_name': action_name, 'parent_table_name': parent_table_name, 'parent_table_id': parent_table_id } print('ABACUS_STATE PARAMS:', post_body) response = ows_client.post( constants.SERVICE_ABACUS_STATE, '/abacus-states', json=[post_body] ) print('RESPONSE', response) response.raise_for_status() return response.json() # OWS-ABACUS-ACCOUNT def get_account_tax_info_snapshot(): """Get account tax info in csv format.""" response = ows_client.post( constants.SERVICE_ABACUS_ACCOUNT, '/account-tax-info/snapshot' ) response.raise_for_status() return response.text.encode('utf-8') def get_signing_entities(): """Get all signing entites.""" signing_entities = ows_client.get( constants.SERVICE_ABACUS_ACCOUNT, f'/signing-entities' ) signing_entities.raise_for_status() signing_entities = signing_entities.json() formatted_signing_entities = dict() if signing_entities: formatted_signing_entities = { signing_entity['signing_entity_id']: signing_entity['signing_entity_name'] for signing_entity in signing_entities } return formatted_signing_entities def get_reference_payment_entities(): """Get all reference payment entities.""" payment_entities_response = ows_client.get( constants.SERVICE_ROYALTIES, f'/reference-payment-entities' ) payment_entities_response.raise_for_status() payment_entities_response = payment_entities_response.json() formatted_payment_entities = dict() payment_entities = payment_entities_response['items'] if payment_entities: formatted_payment_entities = { payment_entity['reference_payment_entity_id']: payment_entity['payment_entity_name'] # noqa: E501 for payment_entity in payment_entities } return formatted_payment_entities def get_account_payment_term_snapshot(): """Get account payment terms in csv format.""" response = ows_client.post( constants.SERVICE_ABACUS_ACCOUNT, '/account-payment-terms/snapshot' ) response.raise_for_status() return response.text.encode('utf-8') def get_account_payment_term_by_account_id(account_id): """Get account payment term by account_id.""" response = ows_client.get( constants.SERVICE_ABACUS_ACCOUNT, f'/account/{account_id}/account-payment-term/' ) response.raise_for_status() return response.json() # OWS-LEDGER def debit_reserves_from_ledger_account_by_run(accounting_run_id): """POST request to debit reserves for the specified accounting_run. Args: accounting_run_id (int): the accounting_run_id Returns: string including total count of updated ledger_accounts. """ url = f'/ledger-reserve-taken/accounting-run/{accounting_run_id}' response = ows_client.post( constants.SERVICE_LEDGER, url, json={} ) response.raise_for_status() return response.json() def get_accounting_run_vat_by_category( accounting_period_id, vat_category, country_code=None): """Get ledger accounting run entries for vat calculations. Arguments: accounting_period_id (int): relevant accounting period id vat_category (str): vat_applied|vat_exempt country_code (str): country is required for vat_applied report """ url = f'/ledger-accounting-run-vat/accounting-period/' \ f'{accounting_period_id}/vat-category/{vat_category}/' if country_code: url = f'{url}?country_code={country_code}' response = ows_client.get( constants.SERVICE_LEDGER, url ) response.raise_for_status() return response.json()