import os import requests import argparse import csv from dotenv import load_dotenv # Load environment variables from a .env file if present load_dotenv('.env') ENV = os.environ.get('ENV', 'qa') IDENTITY_ID = os.environ.get('ORCHARD_IDENTITY_ID') PROFILE_ID = os.environ.get('ORCHARD_PROFILE_ID') PROFILE_UUID = os.environ.get('ORCHARD_PROFILE_UUID') parser = argparse.ArgumentParser( description='Pull vat/tax info for given accounts and payees') parser.add_argument('input', type=argparse.FileType('r'), help='path to csv file (see test_data.csv for example)') args = parser.parse_args() tax_detail_url = 'https://{env}-ows-payee.theorchard.io/account-payee/{payee_id}/tax-details' output_fieldnames = [ 'account_id', 'account_payee_id', 'Company TAX/VAT ID', 'Company Address', 'Zip Code', 'City', 'Country', 'Business Name' ] def fetch_payee_data(payee): response = requests.get( tax_detail_url.format(env=ENV, payee_id=payee['account_payee_id']), headers={ 'orchard-identity-id': IDENTITY_ID, 'orchard-profile-id': PROFILE_ID, 'orchard-profile-uuid': PROFILE_UUID, 'orchard-roles': 'administrator', 'orchard-profile-type': 'AbacusProfile' } ) if response.status_code == 200: tax_data_payee = response.json() return tax_data_payee return {} def _format_payee_data(payee, tax_data): address = tax_data.get('address', {}) address_1 = address.get('address_1', '') address_2 = address.get('address_2', '') return { 'account_id': payee['account_id'], 'account_payee_id': payee['account_id'], 'Company TAX/VAT ID': tax_data.get('vat_number'), 'Company Address': f'{address_1} {address_2}', 'Zip Code': address.get('zip'), 'City': address.get('city'), 'Country': address.get('country_code'), 'Business Name': tax_data.get('business_name') } def main(): account_payees = [] csv_reader = csv.DictReader(args.input, delimiter=',') for row in csv_reader: account_payees.append(row) tax_data_list = [] for payee in account_payees: tax_data = fetch_payee_data(payee) print(tax_data) tax_data_list.append(_format_payee_data(payee, tax_data)) with open('output.csv', 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=output_fieldnames) writer.writeheader() for data in tax_data_list: writer.writerow(data) main()