"""Logic for Ledger Account.""" from owsresponse import response from ledger.constants.constants import ( DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET, ERROR_INVALID_LIMIT_OFFSET, ) from ledger.models.ledger_account import LedgerAccount from ledger.schemas.ledger_account import ( LedgerAccountCurrentBalanceSchema, LedgerAccountListSchema, LedgerAccountSchema, ) from ledger.utils.format_error import validation_error def get_ledger_account_info(account_id): """GET ledger account information.""" ledger_info, _ = LedgerAccount.get_ledger_account_info(account_id) return response.Response( message=LedgerAccountListSchema().dump(ledger_info, many=True), status=200 ) # TODO: TO BE DEPRECATED # replaced by ledger_account payable_balance logic # (dczinsky) def get_current_balance(account_id): """GET current balance of a account.""" latest_entry = LedgerAccount.get_ledger_account_balance(account_id).first() return response.Response( message=LedgerAccountCurrentBalanceSchema(exclude=['account_id']).dump( latest_entry ), status=200, ) # TODO: TO BE DEPRECATED # backs endpoint used in lambda-abacus-accounting-period-close # (dczinsky) def get_ledger_accounts_by_acc_period_id(accounting_period_id, params): """GET ledger payees by accounting period id.""" params_or_error = _validate_request_params(accounting_period_id, params) if not isinstance(params_or_error, dict): return params_or_error ledger_accounts = LedgerAccount.get_accounts_by_acc_period_id(**params_or_error) accounts_count = LedgerAccount.get_total_accounts_count_by_acc_period_id( accounting_period_id ) return response.Response( message={ 'items': LedgerAccountSchema().dump(ledger_accounts, many=True), 'total_count': accounts_count, }, status=200, ) def _validate_request_params(accounting_period_id, request_params): """Format and validate request parameters.""" limit = DEFAULT_PAGE_LIMIT offset = DEFAULT_PAGE_OFFSET try: limit = int(request_params.get('limit', limit)) offset = int(request_params.get('offset', offset)) except ValueError: return validation_error(ERROR_INVALID_LIMIT_OFFSET) return { 'limit': max(limit, 1), 'offset': max(offset, 0), 'accounting_period_id': accounting_period_id, }