From 1551ce0cbe43889bbd8f3167de0ea672fe77d48b Mon Sep 17 00:00:00 2001 From: Gael Le Mignot Date: Thu, 1 Apr 2021 09:12:14 +0200 Subject: [PATCH] Added commands for db cleanup --- .../teleforma-cleanup-dup-discounts.py | 17 +++++++ .../commands/teleforma-fixate-oral-fees.py | 38 +++++++++++++++ .../teleforma-prefill-payment-schedules.py | 48 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 teleforma/management/commands/teleforma-cleanup-dup-discounts.py create mode 100644 teleforma/management/commands/teleforma-fixate-oral-fees.py create mode 100644 teleforma/management/commands/teleforma-prefill-payment-schedules.py diff --git a/teleforma/management/commands/teleforma-cleanup-dup-discounts.py b/teleforma/management/commands/teleforma-cleanup-dup-discounts.py new file mode 100644 index 00000000..5f4ff27f --- /dev/null +++ b/teleforma/management/commands/teleforma-cleanup-dup-discounts.py @@ -0,0 +1,17 @@ +from django.core.management.base import BaseCommand +from teleforma.models import * + +class Command(BaseCommand): + help = "Cleanup duplicate discount objects" + + def handle(self, *args, **options): + for student in Student.objects.all(): + known = set() + for discount in student.discounts.all(): + key = (discount.value, discount.description) + if key in known: + print("Deleting %d (%s: %s)" % (discount.id, student, key)) + discount.delete() + else: + known.add(key) + diff --git a/teleforma/management/commands/teleforma-fixate-oral-fees.py b/teleforma/management/commands/teleforma-fixate-oral-fees.py new file mode 100644 index 00000000..72540452 --- /dev/null +++ b/teleforma/management/commands/teleforma-fixate-oral-fees.py @@ -0,0 +1,38 @@ +from django.core.management.base import BaseCommand +from teleforma.models import * + +import datetime + +class Command(BaseCommand): + help = "Create missing schedule objects" + + def handle(self, *args, **options): + date = datetime.date(2021, 8, 31) + amount = 250 + + for student in Student.objects.all(): + if not student.date_subscribed or not student.period or not student.oral_1: + continue + if student.period.name != 'Estivale': + continue + if student.payment_type != 'online': + continue + if student.oral_1.title != 'Anglais': + continue + + found = False + for fee in student.optional_fees.all(): + if fee.value == amount: + found = True + if found: + continue + p = Payment(student = student, + value = amount, + month = date.month, + scheduled = date) + p.save() + f = OptionalFee(student = student, + value = amount, + description = "Option langue") + f.save() + print("Student %d: created %d / %d" % (student.id, p.id, f.id)) diff --git a/teleforma/management/commands/teleforma-prefill-payment-schedules.py b/teleforma/management/commands/teleforma-prefill-payment-schedules.py new file mode 100644 index 00000000..c9af91ef --- /dev/null +++ b/teleforma/management/commands/teleforma-prefill-payment-schedules.py @@ -0,0 +1,48 @@ +from django.core.management.base import BaseCommand +from teleforma.models import * + +import datetime + +class Command(BaseCommand): + help = "Create missing schedule objects" + + def handle(self, *args, **options): + threshold = datetime.date(2021, 2, 2) + tomorrow = datetime.date.today() + datetime.timedelta(days=1) + + date1 = datetime.date(2021, 6, 30) + date2 = datetime.date(2021, 7, 31) + + full = ((0.30, tomorrow), + (0.35, date1), + (0.35, date2)) + partial = ((0.5, date1), + (0.5, date2)) + + for student in Student.objects.all(): + if not student.date_subscribed or not student.period: + continue + if student.period.name != 'Estivale': + continue + if student.date_subscribed.date() < threshold: + continue + if student.payment_type != 'online': + continue + student.update_balance() + if student.balance_intermediary >= 0: + continue + + if len(student.payments.all()) > 0: + schedule = partial + else: + schedule = full + + balance = student.balance_intermediary + for ratio, date in schedule: + amount = -balance * ratio + p = Payment(student = student, + value = amount, + month = date.month, + scheduled = date) + p.save() + print("Student %d: created %d" % (student.id, p.id)) -- 2.39.5