"""Logic for Statement Period Payment Entity.""" from abacus_common_logic.utils.features import is_feature_enabled from flask import g from owsresponse import response from royalties import models from royalties.constants import error, features from royalties.schemas.statement_period_payment_entity import ( StatementPeriodPaymentEntityAbacusStateSchema, StatementPeriodPaymentEntitySchema, ) statement_period_payment_entity_schema = StatementPeriodPaymentEntitySchema() def get_by_statement_period_id(statement_period_id): """Get statement_period_payment_entity by statement_period_id.""" statement_period = models.StatementPeriod.get_by_id_or_error(statement_period_id) items = statement_period.statement_period_payment_entities statement_period_payment_entity_list = statement_period_payment_entity_schema.dump( items, many=True ) return response.Response(message=statement_period_payment_entity_list, status=200) def get_states_by_statement_period(statement_period_id: int) -> response.Response: """Get statement_period_payment_entity states for the specified statement_period. Args: statement_period_id (int): ID of parent statement_period """ statement_period_payment_entity_states = ( models.StatementPeriodPaymentEntity.get_states_by_statement_period( statement_period_id ) ) message = StatementPeriodPaymentEntityAbacusStateSchema(many=True).dump( statement_period_payment_entity_states ) return response.Response(message=message, status=200) def set_is_visible_to_customer(statement_period_id, payment_entity_id): """Update statement_period_payment_entity.is_visible_to_customer to True.""" record = ( models.StatementPeriodPaymentEntity.get_by_statement_period_and_payment_entity( statement_period_id, payment_entity_id ) ) if not record: return response.create_error_response( code='Error', message=error.ERROR_STATEMENT_PERIOD_PAYMENT_ENTITY_NOT_FOUND.format( payment_entity_id=payment_entity_id, statement_period_id=statement_period_id, ), status=404, ) record.update_attributes(is_visible_to_customer=True) models.StatementPeriodPaymentEntity.commit_changes() message = statement_period_payment_entity_schema.dump(record) return response.Response(message=message, status=200)