"""Adjustment.""" from os import path import pathlib from connectors.abacus_db import AbacusMysqlConnection from adjustment.helpers import get_adjustments from adjustment.sql_templates import GET_STATEMENT_PERIODS_BY_ID from adjustment.adjustment_file import AdjustmentFile class Adjustment: def __init__(self, statement_period_id, file): self.__statement_period_id = statement_period_id self.__file = file def process(self): """validate statement period and adjustment file.""" try: self._validate_statement_period() self._validate_adjustment_file() except Exception as e: print(f'Failed to upload adjustments: {str(e)}') def _validate_statement_period(self): """validate statement period is closed or not.""" statement_period = AbacusMysqlConnection.execute_sql_query( GET_STATEMENT_PERIODS_BY_ID, tuple([self.__statement_period_id]) ) if not statement_period: raise Exception(f'Statement period {self.__statement_period_id} is closed. New adjustments cannot be created for it') return True def _is_file_exists(self): """check if specified file path exist or not.""" if not path.exists(self.__file): raise Exception('File doesn\'t exist') return True def _is_excel_file(self): """check if specified file is excel or not.""" path = pathlib.Path(self.__file) if path.suffix != '.xlsx': raise Exception('Not a valid excel file') return True def _validate_adjustment_file(self): """validate adjustment file""" self._is_file_exists() self._is_excel_file() # Get adjustments get_adjustments() # Read file and validate adjustment_file = AdjustmentFile(self.__file) adjustment_file.read_and_validate_file()