|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import csv |
| 8 | +from datetime import timedelta |
8 | 9 |
|
9 | 10 | from django.contrib import admin |
10 | 11 | from django.http import HttpRequest, HttpResponse |
|
34 | 35 | UnregisteredStudent, |
35 | 36 | Update, |
36 | 37 | ) |
| 38 | +from home.utils.rebate_checker import max_days_rebate |
37 | 39 |
|
38 | 40 | from .resources import ( |
39 | 41 | AllocationResource, |
|
46 | 48 | ) |
47 | 49 | from .utils.django_email_server import long_rebate_query_mail |
48 | 50 | from .utils.month import fill_periods, map_periods_to_long_rebate |
49 | | -from .utils.rebate_bills_saver import ( |
50 | | - fix_all_bills, |
51 | | - save_long_bill, |
52 | | - save_short_bill, |
53 | | - update_bills, |
54 | | -) |
| 51 | +from .utils.rebate_bills_saver import fix_all_bills, save_long_bill, update_bills |
55 | 52 |
|
56 | 53 | # Customising the heading and title of the admin page |
57 | 54 | admin.site.site_header = "Dining Website Admin Page" |
@@ -1028,43 +1025,43 @@ class about_Admin(admin.ModelAdmin): |
1028 | 1025 | actions = ["Add"] |
1029 | 1026 |
|
1030 | 1027 | @admin.action(description="Add left short rebate to Bills") |
1031 | | - def Add(self, request, queryset): |
| 1028 | + def Add(self, request, queryset: list[LeftShortRebate]): |
1032 | 1029 | """ |
1033 | 1030 | Export action available in the admin page |
1034 | 1031 | """ |
1035 | 1032 | for obj in queryset: |
1036 | 1033 | email = obj.email |
1037 | 1034 | student_obj = Student.objects.filter(email=email).last() |
1038 | 1035 | for period in Period.objects.all(): |
1039 | | - if ( |
1040 | | - period.start_date <= obj.start_date |
1041 | | - and period.end_date >= obj.end_date |
1042 | | - ): |
1043 | | - days = (obj.end_date - obj.start_date).days + 1 |
| 1036 | + if period.start_date <= obj.start_date <= period.end_date: |
| 1037 | + start_date = obj.start_date |
| 1038 | + end_date = obj.end_date |
| 1039 | + date_applied = obj.date_applied |
| 1040 | + if period.end_date < obj.end_date: |
| 1041 | + obj.start_date = period.end_date + timedelta(days=1) |
| 1042 | + obj.save() |
| 1043 | + end_date = period.end_date |
| 1044 | + else: |
| 1045 | + obj.delete() |
1044 | 1046 | allocation = Allocation.objects.filter( |
1045 | 1047 | email=student_obj, period=period |
1046 | 1048 | ).last() |
1047 | 1049 | if allocation: |
1048 | | - save_short_bill( |
1049 | | - student_obj, |
1050 | | - period, |
1051 | | - days, |
1052 | | - allocation.high_tea, |
1053 | | - allocation.caterer, |
| 1050 | + upper_cap_check = max_days_rebate( |
| 1051 | + student_obj, start_date, end_date, period |
1054 | 1052 | ) |
1055 | | - print("Saved") |
| 1053 | + if upper_cap_check == 0: |
| 1054 | + continue |
| 1055 | + end_date = start_date + timedelta(days=upper_cap_check - 1) |
1056 | 1056 | short_rebate = Rebate( |
1057 | 1057 | email=student_obj, |
1058 | 1058 | allocation_id=allocation, |
1059 | | - start_date=obj.start_date, |
1060 | | - end_date=obj.end_date, |
1061 | | - date_applied=obj.date_applied, |
| 1059 | + start_date=start_date, |
| 1060 | + end_date=end_date, |
| 1061 | + date_applied=date_applied, |
1062 | 1062 | approved=True, |
1063 | 1063 | ) |
1064 | 1064 | short_rebate.save() |
1065 | | - LeftShortRebate.objects.filter( |
1066 | | - email=email, date_applied=obj.date_applied |
1067 | | - ).delete() |
1068 | 1065 |
|
1069 | 1066 |
|
1070 | 1067 | @admin.register(AllocationForm) |
|
0 commit comments