|
1 | 1 | import logging |
2 | 2 |
|
3 | 3 | from django.db import transaction |
| 4 | +from django.db.models import Q |
4 | 5 | from django.contrib.contenttypes.models import ContentType |
5 | 6 |
|
6 | 7 | from core.custom_filters import CustomFilterWizardStorage |
@@ -49,8 +50,9 @@ def __init__(self, user, validation_class=PayrollValidation): |
49 | 50 | def create(self, obj_data): |
50 | 51 | try: |
51 | 52 | with transaction.atomic(): |
| 53 | + included_unpaid = obj_data.pop("included_unpaid", False) |
52 | 54 | obj_data = self._adjust_create_payload(obj_data) |
53 | | - bills_queryset = self._get_bills_queryset(obj_data) |
| 55 | + bills_queryset = self._get_bills_queryset(obj_data, included_unpaid) |
54 | 56 | obj_data_and_bills = {**obj_data, "bills": bills_queryset} |
55 | 57 | self.validation_class.validate_create(self.user, **obj_data_and_bills) |
56 | 58 | obj_ = self.OBJECT_TYPE(**obj_data) |
@@ -96,7 +98,7 @@ def _create_payroll_bills(self, bills_queryset, payroll_id): |
96 | 98 | payroll_bill = PayrollBill(bill_id=bill.id, payroll_id=payroll_id) |
97 | 99 | payroll_bill.save(username=self.user.username) |
98 | 100 |
|
99 | | - def _get_bills_queryset(self, obj_data): |
| 101 | + def _get_bills_queryset(self, obj_data, included_unpaid): |
100 | 102 | benefit_plan_id = obj_data.get("benefit_plan_id") |
101 | 103 | date_from = obj_data.get("date_valid_from") |
102 | 104 | date_to = obj_data.get("date_valid_to") |
@@ -125,7 +127,20 @@ def _get_bills_queryset(self, obj_data): |
125 | 127 | is_deleted=False, |
126 | 128 | date_bill__range=(date_from, date_to), |
127 | 129 | subject_type=ContentType.objects.get_for_model(Beneficiary), |
128 | | - subject_id__in=beneficiary_ids |
| 130 | + subject_id__in=beneficiary_ids, |
| 131 | + status__in=[Bill.Status.VALIDATED], |
129 | 132 | ) |
130 | 133 |
|
| 134 | + bills_queryset = bills_queryset.filter( |
| 135 | + Q(payrollbill__isnull=True) | Q(payrollbill__is_deleted=True) |
| 136 | + ) |
| 137 | + |
| 138 | + if included_unpaid: |
| 139 | + bills_queryset = bills_queryset.filter(json_ext__unpaid=True) |
| 140 | + else: |
| 141 | + bills_queryset = bills_queryset.filter( |
| 142 | + Q(json_ext__unpaid=False) | |
| 143 | + Q(json_ext__unpaid__isnull=True) |
| 144 | + ) |
| 145 | + |
131 | 146 | return bills_queryset |
0 commit comments