"""A Task to generate a vat applied report.""" import pandas as pd from lib.config import VAT_APPLIED_COUNTRY_CODES from lib.constants import VAT_APPLIED_HEADERS_MAP from lib.utils import ows from lib.utils.helpers import get_country_by_code from tasks.accounting_period_calculate_vat import helpers def create_vat_applied_report_task(dag_run, **kwargs): """Request for ledger accounting run vat applied entries and save on s3.""" event = helpers.get_event_from_params(dag_run, **kwargs) accounting_period = ows.get_accounting_period_details(event.target_id) accounting_period_id = accounting_period.get('accounting_period_id') for country_code in VAT_APPLIED_COUNTRY_CODES.split(','): report_data_json = ows.get_accounting_run_vat_by_category( accounting_period_id, 'vat_applied', country_code )['items'] if not report_data_json: print(f'No applied vat found for {country_code}.') continue report_s3_path = helpers.build_vat_report_path( accounting_period_id, accounting_period.get('accounting_period_name'), report_type=f'vat_applied_{country_code.lower()}' ) vat_applied_data = pd.DataFrame( report_data_json, columns=VAT_APPLIED_HEADERS_MAP.keys() ) vat_applied_data['country_of_tax_residence'] = \ vat_applied_data['country_of_tax_residence'].map( lambda a: get_country_by_code(a).name) vat_applied_data.rename(columns=VAT_APPLIED_HEADERS_MAP, inplace=True) vat_applied_data.to_csv(report_s3_path, index=False, sep='\t')