"""A Task to generate a vat exempt report.""" import pandas as pd from lib.constants import VAT_EXEMPT_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_exempt_report_task(dag_run, **kwargs): """Request for ledger accounting run vat exempt 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') report_s3_path = helpers.build_vat_report_path( accounting_period_id, accounting_period.get('accounting_period_name'), report_type='vat_exempt' ) report_data_json = ows.get_accounting_run_vat_by_category( accounting_period_id, 'vat_exempt' )['items'] vat_extempt_data = pd.DataFrame( report_data_json, columns=VAT_EXEMPT_HEADERS_MAP.keys() ) vat_extempt_data['country_of_tax_residence'] = \ vat_extempt_data['country_of_tax_residence'].map( lambda a: get_country_by_code(a).name) vat_extempt_data.rename(columns=VAT_EXEMPT_HEADERS_MAP, inplace=True) vat_extempt_data.to_csv(report_s3_path, index=False, sep='\t')