diff --git a/home/admin.py b/home/admin.py index 8295cb0..55a1d01 100644 --- a/home/admin.py +++ b/home/admin.py @@ -48,7 +48,12 @@ ) from .utils.django_email_server import caterer_mail, long_rebate_query_mail from .utils.month import fill_periods -from .utils.rebate_bills_saver import save_long_bill, save_short_bill, update_bills +from .utils.rebate_bills_saver import ( + fix_all_bills, + save_long_bill, + save_short_bill, + update_bills, +) # Customising the heading and title of the admin page admin.site.site_header = "Dining Website Admin Page" @@ -891,7 +896,7 @@ def hostel(self, obj): def room_number(self, obj): return obj.email.room_no - actions = ["export_as_csv", "update_bill"] + actions = ["export_as_csv", "update_bill", "fix_all_bills"] @admin.action(description="Update the bills") def update_bill(self, request, queryset): @@ -908,6 +913,17 @@ def update_bill(self, request, queryset): obj.period6_bill = days * 115 obj.save() + @admin.action(description="Fix the bills") + def fix_all_bills(self, request, queryset): + for obj in queryset: + if obj.semester != Semester.objects.get(name="Autumn 2024"): + continue + semester = obj.semester + period_1 = Period.objects.get(semester=semester, Sno=1) + period_2 = Period.objects.get(semester=semester, Sno=2) + period_3 = Period.objects.get(semester=semester, Sno=3) + fix_all_bills(obj, period_1, period_2, period_3) + def export_as_csv(self, request, queryset): """ Export action available in the admin page diff --git a/home/utils/rebate_bills_saver.py b/home/utils/rebate_bills_saver.py index 83d8039..c3621d2 100644 --- a/home/utils/rebate_bills_saver.py +++ b/home/utils/rebate_bills_saver.py @@ -1,3 +1,5 @@ +from home.models.students import LongRebate, Rebate + from ..models import ( Allocation, CatererBills, @@ -164,3 +166,81 @@ def update_bills(email, allocation): rebate_bill.save() except Exception as e: print(e) + + +def fix_all_bills( + student_bill: StudentBills, period_1: Period, period_2: Period, period_3: Period +): + email = student_bill.email + print(email) + period_1_days = (period_1.end_date - period_1.start_date).days + 1 + period_2_days = (period_2.end_date - period_2.start_date).days + 1 + period_3_days = (period_3.end_date - period_3.start_date).days + 1 + rebates = Rebate.objects.filter(email=email, approved=True) + short_rebates_per_period = [0, 0, 0] + for rebate in rebates: + if rebate.end_date < period_1.start_date: + continue + if rebate.start_date > period_3.end_date: + continue + if rebate.start_date <= period_1.start_date: + rebate.start_date = period_1.start_date + if rebate.end_date <= period_1.end_date: + short_rebates_per_period[0] += ( + rebate.end_date - rebate.start_date + ).days + 1 + continue + rebate.start_date = period_1.end_date + 1 + if rebate.end_date <= period_2.end_date: + short_rebates_per_period[1] += ( + rebate.end_date - rebate.start_date + ).days + 1 + continue + rebate.start_date = period_2.end_date + 1 + if rebate.end_date <= period_3.end_date: + short_rebates_per_period[2] += ( + rebate.end_date - rebate.start_date + ).days + 1 + continue + short_rebates_per_period[2] += (period_3.end_date - rebate.start_date).days + 1 + long_rebates = LongRebate.objects.filter(email=email, approved=True) + long_rebates_per_period = [0, 0, 0] + for rebate in long_rebates: + if rebate.end_date < period_1.start_date: + continue + if rebate.start_date > period_3.end_date: + continue + if rebate.start_date <= period_1.start_date: + rebate.start_date = period_1.start_date + if rebate.end_date <= period_1.end_date: + long_rebates_per_period[0] += (rebate.end_date - rebate.start_date).days + 1 + continue + rebate.start_date = period_1.end_date + 1 + if rebate.end_date <= period_2.end_date: + long_rebates_per_period[1] += (rebate.end_date - rebate.start_date).days + 1 + continue + rebate.start_date = period_2.end_date + 1 + if rebate.end_date <= period_3.end_date: + long_rebates_per_period[2] += (rebate.end_date - rebate.start_date).days + 1 + continue + long_rebates_per_period[2] += (period_3.end_date - rebate.start_date).days + 1 + print(short_rebates_per_period, long_rebates_per_period) + student_bill.period1_short = short_rebates_per_period[0] + student_bill.period2_short = short_rebates_per_period[1] + student_bill.period3_short = short_rebates_per_period[2] + student_bill.period1_long = long_rebates_per_period[0] + student_bill.period2_long = long_rebates_per_period[1] + student_bill.period3_long = long_rebates_per_period[2] + student_bill.period1_high_tea = False + student_bill.period2_high_tea = False + student_bill.period3_high_tea = False + student_bill.period1_bill = 115 * ( + period_1_days - short_rebates_per_period[0] - long_rebates_per_period[0] + ) + student_bill.period2_bill = 115 * ( + period_2_days - short_rebates_per_period[1] - long_rebates_per_period[1] + ) + student_bill.period3_bill = 115 * ( + period_3_days - short_rebates_per_period[2] - long_rebates_per_period[2] + ) + student_bill.save()