"""Backfill VAT summary entries.""" import logging import sys sys.path.append('') logging.basicConfig(level=logging.INFO) from scripts.utils import get_env # noqa: E402 from scripts.utils import load_csv_file # noqa: E402 from moneyhub.connectors.ows_moneyhub import OwsMoneyhub # noqa: E402 from moneyhub.constants.constants import StatementPeriodStatus # noqa: E402 from moneyhub.models.ledger_account_contract import LedgerAccountContract # noqa: E402 from moneyhub.models.statement_period import StatementPeriod # noqa: E402 def _verify_data(row: dict, env_statement_period) -> bool: row_statement_period_id = int(row['statement_period_id']) contract_id = int(row['contract_id']) account_id = int(row['account_id']) row_account_payee_currency = row['payee_currency_code'] db_statement_period_object = StatementPeriod.get_by_id(row_statement_period_id) db_account_contract = LedgerAccountContract.get_events_for_account_and_contract( account_id, contract_id, [row_statement_period_id], True) if not row_statement_period_id == env_statement_period: logging.error(f'Check that statement period: {env_statement_period} is the only statement period found in the CSV file. Found: {row_statement_period_id}.') # noqa: E501 exit(1) if not bool(db_account_contract): logging.error(f'Check that contract: {contract_id} belongs to account: {account_id}.') exit(1) if not db_statement_period_object.statement_period_status == StatementPeriodStatus.CLOSED: logging.error(f'Check that statement period: {db_statement_period_object.statement_period_name} is closed') # noqa: E501 exit(1) if not row_account_payee_currency == db_account_contract.currency_code: logging.error( f'Check that payee currency: {row_account_payee_currency} matches ' f'{db_account_contract.currency_code} for account: {account_id}, contract: ' f'{contract_id}, and statement period {db_statement_period_object.statement_period_name}' # noqa: E501 f' as found in Abacus Ledger Account Contract.') exit(1) return True if __name__ == '__main__': statement_period_id = int(get_env('STATEMENT_PERIOD_ID', True)) if len(sys.argv) == 1: logging.error('Missing filename CLI parameter') exit(1) data = load_csv_file(sys.argv[1]) filtered_data = [ { 'account_id': int(row['account_id']), 'contract_id': int(row['contract_id']), 'vat_category': row['vat_category'], 'payee_currency_code': row['payee_currency_code'], 'vat_currency_code': row['vat_currency_code'], 'base_amount_payee_currency': row['base_amount_payee_currency'], 'vat_rate': row['vat_rate'], 'vat_amount_payee_currency': row['vat_amount_payee_currency'], 'vat_amount_vat_currency': row['vat_amount_vat_currency'], 'wht_amount_payee_currency': row['wht_amount_payee_currency'], 'wht_amount_vat_currency': row['wht_amount_vat_currency'], 'wht_rate': row['wht_rate'], 'net_amount_payee_currency': row['net_amount_payee_currency'], } for row in data if _verify_data(row, statement_period_id) ] if len(filtered_data) == 0: logging.error(f'No entries for statement period {statement_period_id} in provided file') exit(1) logging.info(f'Attempting to backfill {len(filtered_data)} entries') response = OwsMoneyhub.backfill_vat_summary(statement_period_id, filtered_data) logging.info(response)