From ad187c02dae6df1e0ea8a714a9a72621b25dea75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 15 Apr 2020 12:16:55 +0200 Subject: [PATCH] Import VAT rates "importer" from dynamicweb --- .../management/commands/import-vat-rates.py | 44 +++++++++++++++++++ .../migrations/0005_auto_20200415_1003.py | 31 +++++++++++++ .../uncloud/uncloud_pay/models.py | 9 ++++ 3 files changed, 84 insertions(+) create mode 100644 uncloud_django_based/uncloud/uncloud_pay/management/commands/import-vat-rates.py create mode 100644 uncloud_django_based/uncloud/uncloud_pay/migrations/0005_auto_20200415_1003.py diff --git a/uncloud_django_based/uncloud/uncloud_pay/management/commands/import-vat-rates.py b/uncloud_django_based/uncloud/uncloud_pay/management/commands/import-vat-rates.py new file mode 100644 index 0000000..32938e4 --- /dev/null +++ b/uncloud_django_based/uncloud/uncloud_pay/management/commands/import-vat-rates.py @@ -0,0 +1,44 @@ +from django.core.management.base import BaseCommand +from uncloud_pay.models import VATRate +import csv + + +class Command(BaseCommand): + help = '''Imports VAT Rates. Assume vat rates of format https://github.com/kdeldycke/vat-rates/blob/master/vat_rates.csv''' + + def add_arguments(self, parser): + parser.add_argument('csv_file', nargs='+', type=str) + + def handle(self, *args, **options): + try: + for c_file in options['csv_file']: + print("c_file = %s" % c_file) + with open(c_file, mode='r') as csv_file: + csv_reader = csv.DictReader(csv_file) + line_count = 0 + for row in csv_reader: + if line_count == 0: + line_count += 1 + obj, created = VATRate.objects.get_or_create( + start_date=row["start_date"], + stop_date=row["stop_date"] if row["stop_date"] is not "" else None, + territory_codes=row["territory_codes"], + currency_code=row["currency_code"], + rate=row["rate"], + rate_type=row["rate_type"], + description=row["description"] + ) + if created: + self.stdout.write(self.style.SUCCESS( + '%s. %s - %s - %s - %s' % ( + line_count, + obj.start_date, + obj.stop_date, + obj.territory_codes, + obj.rate + ) + )) + line_count+=1 + + except Exception as e: + print(" *** Error occurred. Details {}".format(str(e))) diff --git a/uncloud_django_based/uncloud/uncloud_pay/migrations/0005_auto_20200415_1003.py b/uncloud_django_based/uncloud/uncloud_pay/migrations/0005_auto_20200415_1003.py new file mode 100644 index 0000000..c30f527 --- /dev/null +++ b/uncloud_django_based/uncloud/uncloud_pay/migrations/0005_auto_20200415_1003.py @@ -0,0 +1,31 @@ +# Generated by Django 3.0.5 on 2020-04-15 10:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0004_auto_20200409_1225'), + ] + + operations = [ + migrations.CreateModel( + name='VATRate', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('start_date', models.DateField(blank=True, null=True)), + ('stop_date', models.DateField(blank=True, null=True)), + ('territory_codes', models.TextField(blank=True, default='')), + ('currency_code', models.CharField(max_length=10)), + ('rate', models.FloatField()), + ('rate_type', models.TextField(blank=True, default='')), + ('description', models.TextField(blank=True, default='')), + ], + ), + migrations.AlterField( + model_name='order', + name='recurring_period', + field=models.CharField(choices=[('ONCE', 'Onetime'), ('YEAR', 'Per Year'), ('MONTH', 'Per Month'), ('WEEK', 'Per Week'), ('DAY', 'Per Day'), ('HOUR', 'Per Hour'), ('MINUTE', 'Per Minute'), ('SECOND', 'Per Second')], default='MONTH', max_length=32), + ), + ] diff --git a/uncloud_django_based/uncloud/uncloud_pay/models.py b/uncloud_django_based/uncloud/uncloud_pay/models.py index 4cb1952..d6d0f2a 100644 --- a/uncloud_django_based/uncloud/uncloud_pay/models.py +++ b/uncloud_django_based/uncloud/uncloud_pay/models.py @@ -453,6 +453,15 @@ class BillRecord(): def amount(self): return Decimal(float(self.recurring_price) * self.recurring_count) + self.one_time_price +class VATRate(models.Model): + start_date = models.DateField(blank=True, null=True) + stop_date = models.DateField(blank=True, null=True) + territory_codes = models.TextField(blank=True, default='') + currency_code = models.CharField(max_length=10) + rate = models.FloatField() + rate_type = models.TextField(blank=True, default='') + description = models.TextField(blank=True, default='') + ### # Orders.