2020-04-15 10:16:55 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from uncloud_pay.models import VATRate
|
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
import logging
|
2020-10-08 17:54:04 +00:00
|
|
|
import urllib
|
|
|
|
import csv
|
|
|
|
import sys
|
|
|
|
import io
|
2020-04-15 10:16:55 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-04-15 10:16:55 +00:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = '''Imports VAT Rates. Assume vat rates of format https://github.com/kdeldycke/vat-rates/blob/master/vat_rates.csv'''
|
2020-10-11 20:32:08 +00:00
|
|
|
vat_url = "https://raw.githubusercontent.com/ungleich/vat-rates/main/vat_rates.csv"
|
2020-10-08 17:54:04 +00:00
|
|
|
|
2020-04-15 10:16:55 +00:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2020-10-08 17:54:04 +00:00
|
|
|
parser.add_argument('--vat-url', default=self.vat_url)
|
2020-04-15 10:16:55 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2020-10-08 17:54:04 +00:00
|
|
|
vat_url = options['vat_url']
|
|
|
|
url_open = urllib.request.urlopen(vat_url)
|
|
|
|
|
|
|
|
# map to fileio using stringIO
|
|
|
|
csv_file = io.StringIO(url_open.read().decode('utf-8'))
|
|
|
|
reader = csv.DictReader(csv_file)
|
2020-04-15 10:16:55 +00:00
|
|
|
|
2020-10-08 17:54:04 +00:00
|
|
|
for row in reader:
|
2021-07-19 14:36:10 +00:00
|
|
|
if row["territory_codes"] and len(row["territory_codes"].splitlines()) > 1:
|
|
|
|
for code in row["territory_codes"].splitlines():
|
|
|
|
VATRate.objects.get_or_create(
|
|
|
|
starting_date=row["start_date"],
|
|
|
|
ending_date=row["stop_date"] if row["stop_date"] != "" else None,
|
|
|
|
territory_codes=code,
|
|
|
|
currency_code=row["currency_code"],
|
|
|
|
rate=row["rate"],
|
|
|
|
rate_type=row["rate_type"],
|
|
|
|
description=row["description"]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
VATRate.objects.get_or_create(
|
|
|
|
starting_date=row["start_date"],
|
|
|
|
ending_date=row["stop_date"] if row["stop_date"] != "" else None,
|
|
|
|
territory_codes=row["territory_codes"],
|
|
|
|
currency_code=row["currency_code"],
|
|
|
|
rate=row["rate"],
|
|
|
|
rate_type=row["rate_type"],
|
|
|
|
description=row["description"]
|
|
|
|
)
|
|
|
|
logger.info('All VAT Rates have been added!')
|