""" Period Model ============= Model function for getting period information. """ from ows_accounting import config from ows_accounting import response from ows_accounting.constants import error from ows_accounting.models.sql import period from ows_accounting.utils import dynamodb from ows_accounting.utils import mysql def get_available_periods(user_id_type): """Get all available accounting periods that have sales for this particular account. Args: user_id_type (str): concatenation of user account and account type. L for vendor and S for subaccount. Returns: Response: response object with a list of integer values of accounting period ids. Response error if key is not found. """ table = dynamodb.get_table( config.ACCOUNTING_STATEMENT_EXPORT_TABLE) records = dynamodb.query_items( table, 'user_id_type', user_id_type, file_type='AVRO', status=config.STATUS_GENERATED) if records: periods = ','.join([r.get('period_ids') for r in records]).split(',') return response.Response([int(v) for v in periods]) return response.create_error_response( code=error.ERROR_CODE_INVALID_PERIOD, message=error.ERROR_MESSAGE_INVALID_PERIOD) def get_available_accounting_periods(account_id, account_type): """Get all available accounting periods that have sales for this particular account. Args: account_id (int): vendor_id or subaccount_id. account_type (str): vendor or subaccount. Returns: Response: response object with a list of integer values of accounting period ids. """ if account_type not in ['vendor', 'subaccount']: return response.create_not_found_response() user_type = 'L' if account_type == 'subaccount': user_type = 'S' user_id_type = '{}{}'.format(account_id, user_type) table = dynamodb.get_table( config.ACCOUNTING_STATEMENT_EXPORT_TABLE) records = dynamodb.query_items( table, 'user_id_type', user_id_type, file_type='AVRO', status=config.STATUS_GENERATED) if records: periods = ','.join([r.get('period_ids') for r in records]).split(',') return response.Response([int(v) for v in periods]) return response.create_not_found_response() def get_first_statement_period(account_id, account_type): """Get accounting first statement period by vendor id. Args: account_id (int): Account Id. account_type(str): Account type (vendor|subaccount) Returns: response.Response: Accounting period data or error in Response object. """ sql = period.SQL_GET_FIRST_STATEMENT_PERIOD_VENDOR if account_type == 'subaccount': sql = period.SQL_GET_FIRST_STATEMENT_PERIOD_SUBACCOUNT with mysql.db_session() as session: result = session.execute(sql, {'account_id': account_id}) return response.Response(result.fetchone()[0])