Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ONI-257: New deduction calculation and validity checks #6

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed claim_sampling/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file removed claim_sampling/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file removed claim_sampling/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file removed claim_sampling/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file removed claim_sampling/__pycache__/services.cpython-38.pyc
Binary file not shown.
Binary file removed claim_sampling/__pycache__/tests.cpython-38.pyc
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.10 on 2024-06-11 12:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('claim_sampling', '0004_rename_claim_id_claimsamplingbatchassignment_claim_and_more'),
]

operations = [
migrations.AlterField(
model_name='claimsamplingbatchassignment',
name='status',
field=models.CharField(choices=[('S', 'Skipped'), ('I', 'Idle')], default='I', max_length=2),
),
migrations.AlterField(
model_name='historicalclaimsamplingbatchassignment',
name='status',
field=models.CharField(choices=[('S', 'Skipped'), ('I', 'Idle')], default='I', max_length=2),
),
]
Binary file not shown.
48 changes: 31 additions & 17 deletions claim_sampling/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def create(self, obj_data, task_group: TaskGroup = None):

if len(claim_batch_ids) == 0:
raise ValueError(_("Claim List cannot be empty"))

claim_batch_ids = self.__filter_already_assigned(claim_batch_ids=claim_batch_ids)

if len(claim_batch_ids) == 0:
raise ValueError(_("All claims already assigned"))

if percentage < 1 or percentage > 100:
raise ValueError(_("Percentage not in range (0, 100)"))

Expand All @@ -79,7 +85,7 @@ def create(self, obj_data, task_group: TaskGroup = None):
user_created=self.user,
user_updated=self.user
))
if claim.review_status in [Claim.REVIEW_IDLE, Claim.REVIEW_NOT_SELECTED, Claim.REVIEW_BYPASSED] \
if claim.review_status in [Claim.REVIEW_IDLE, Claim.REVIEW_NOT_SELECTED] \
and should_be_reviewed == ClaimSamplingBatchAssignment.Status.IDLE:
claim.review_status = Claim.REVIEW_SELECTED
claim.save_history()
Expand All @@ -97,6 +103,10 @@ def update(self, obj_data):
def delete(self, obj_data):
return super().delete(obj_data)

def __filter_already_assigned(self, claim_batch_ids):
filtered_claim_batch_ids = claim_batch_ids.exclude(id__in=ClaimSamplingBatchAssignment.objects.filter(claim__uuid__in=claim_batch_ids).values("claim"))
return filtered_claim_batch_ids

@transaction.atomic
def extrapolate_results(self, claim_sampling_id):
claim_sampling = ClaimSamplingBatch.objects.get(id=claim_sampling_id)
Expand All @@ -120,22 +130,21 @@ def extrapolate_results(self, claim_sampling_id):

deductible = round(rejected_from_review.count() / reviewed_delivered.count(), 2) * 100
# SHOULD WE RUN ENGINE BEFORE THIS?
split = self.__choose_random_claims_for_deductible(
claims_awaiting_validation.count(), deductible
)

result = {
'approved': [],
'rejected': []
}

for approved, assignment in zip(split, claims_awaiting_validation):
for approved, assignment in claims_awaiting_validation:
if approved == Claim.STATUS_VALUATED:
result['approved'].append(assignment.claim)
else:
result['rejected'].append(assignment.claim.uuid)

set_claims_status(result['rejected'], 'status', Claim.STATUS_REJECTED)
for claim in approved:
self.apply_claim_item_service_deduction(claim=claim, deduction_rate=deductible)
errors = []
for claim in result['approved']:
errors += validate_and_process_dedrem_claim(claim, self.user, True)
Expand All @@ -154,6 +163,23 @@ def prepare_sampling_summary(self, claim_sampling_id):
rejected_from_review = reviewed_delivered.filter(status=Claim.STATUS_REJECTED)
return rejected_from_review, reviewed_delivered, total

def apply_claim_item_service_deduction(self, claim, deduction_rate):
claim_items = claim.items.all()

for item in claim_items:
new_claim_item = item
if new_claim_item.price_approved:
new_claim_item.price_approved *= (100-deduction_rate)/100
new_claim_item.save()

claim_services = claim.services.all()

for service in claim_services:
new_claim_service = service
if new_claim_service.price_approved:
new_claim_service.price_approved *= (100-deduction_rate)/100
new_claim_service.save()

def _get_sampling_claims(self, claim_sampling_id, include_skip=False):
assigned_claims = ClaimSamplingBatchAssignment.objects.filter(claim_batch_id=claim_sampling_id)
filters = [
Expand Down Expand Up @@ -188,18 +214,6 @@ def __choose_random_claims_for_review(self, total_elements: int, percentage: int
random.shuffle(result_list)
return result_list

def __choose_random_claims_for_deductible(self, total_elements: int, percentage: int):
rejected = int((percentage/100.0) * total_elements)
valid = total_elements - rejected

# Create the matching number of claims
result_list = [Claim.STATUS_VALUATED] * valid + \
[Claim.STATUS_REJECTED] * rejected

# Shuffle the list to randomize the order
random.shuffle(result_list)
return result_list

def _create_sampling_task(self, sampling_batch_data, sampling_batch, task_group):
return TaskService(self.user).create({
'source': 'claim_sampling',
Expand Down
Loading