#!/usr/bin/env python import requests import fulcrmpy.apiv2 import settings import datetime reduced_map = { "accommodation": "accommodation", "admission to cultural events": "cultural_event", "admission to entertainment events": "entertainment_event", "admission to sporting events": "sporting_event", "advertising": "advertising", "agricultural supplies": "argi_supplies", "baby foodstuffs": "baby_food", "bikes": "bike", "books": "book", "childrens clothing": "clothing_child", "domestic fuel": "fuel_domestic", "domestic services": "services_domestic", "e-books": "ebook", "foodstuffs": "food", "hotels": "hotel", "medical": "medical", "newspapers": "news", "passenger transport": "transport", "pharmaceuticals": "pharma", "property renovations": "renovation", "restaurants": "restaurant", "social housing": "social_housing", "water": "water", "wine": "wine", } def check_or_update( fulcrm, country_code, rate, taxtype_name, slug, archived, archived_suffix, fulcrm_rate ): if fulcrm_rate is not None: if ( "%0.2f" % ( fulcrm_rate[ "rate" ] * 100.0 ) ) != ( "%0.2f" % rate ): print( "RATE CHANGE: %s standard from %0.2f to %0.2f" % ( country_code, fulcrm_data[ 'rate' ] * 100, rate ) ) fulcrm.patch( fulcrm_rate[ 'url' ], { "archived": archived, "s": "%s_%s" % ( slug, archived_suffix ), }, no_d = True ) fulcrm.post( 'taxtype/', { "s": slug, "rate": rate / 100.0, "name": taxtype_name, }, no_d = True ) elif ( fulcrm_rate[ 'name' ] != taxtype_name ): print( 'NAME CHANGE: "%s" to "%s"' % ( fulcrm_rate[ 'name' ], taxtype_name ) ) fulcrm.patch( fulcrm_rate[ 'url' ], { 'name': taxtype_name }, no_d = True ) else: print( 'MISSING RATE: "%s"' % ( taxtype_name, ) ) fulcrm.post( 'taxtype', { "s": slug, "rate": rate / 100.0, "name": taxtype_name, }, no_d = True ) def __main__(): now = datetime.datetime.now() now_s = now.strftime( "%Y%m%d_%H%M%S" ) now_fulcrm = now.strftime( "%Y-%m-%dT%H:%M:%S.%f" ) fulcrm = fulcrmpy.apiv2.APIv2( settings.FULCRM_USER, settings.FULCRM_API_KEY ) fulcrm_data = {} for taxtype in fulcrm.get_many( "taxtype/", no_d = True ): fulcrm_data[ taxtype[ 's' ] ] = taxtype api_data = requests.get( settings.VATLAYER_API_URL ).json() if not api_data[ 'success' ]: return for ( country_code, country_data ) in api_data[ 'rates' ].items(): slug = "vatmoss_%s_standard" % ( country_code, ) taxtype_name = "%s VAT @ %.1f%%" % ( country_code, country_data[ 'standard_rate' ] ) check_or_update( fulcrm, country_code, country_data[ 'standard_rate' ], taxtype_name, slug, now_fulcrm, now_s, fulcrm_data.get( slug, None ) ) if country_data[ 'reduced_rates' ]: for ( reduced_name, reduced_rate ) in country_data[ 'reduced_rates' ].items(): reduced_slug = reduced_map[ reduced_name ] slug = "vatmoss_%s_reduced_%s" % ( country_code, reduced_slug ) taxtype_name = "%s (%s) VAT @ %.1f%%" % ( country_code, reduced_name, country_data[ 'standard_rate' ] ) check_or_update( fulcrm, country_code, reduced_rate, taxtype_name, slug, now_fulcrm, now_s, fulcrm_data.get( slug, None ) ) print( "vat2ful: COMPLETED" ) if __name__ == '__main__': __main__()