Skip to content

Commit

Permalink
fix issues with refill_end_datetime calc
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Feb 15, 2024
1 parent 96285ee commit 691989b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 50 deletions.
1 change: 0 additions & 1 deletion edc_pharmacy/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .crf import StudyMedicationModelFormMixin
from .dispensing_history_form import (
DispensingHistoryForm,
DispensingHistoryReadonlyForm,
Expand Down
1 change: 0 additions & 1 deletion edc_pharmacy/forms/crf/__init__.py

This file was deleted.

19 changes: 0 additions & 19 deletions edc_pharmacy/forms/crf/study_medication_modelform_mixin.py

This file was deleted.

1 change: 0 additions & 1 deletion edc_pharmacy/model_mixins/previous_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def previous(self):

@property
def next(self):
# self.update_or_raise_on_null_refill_end_datetimes()
opts = {"refill_start_datetime__gt": self.refill_start_datetime}
if getattr(self, "related_visit_model_attr", None):
opts.update(
Expand Down
21 changes: 13 additions & 8 deletions edc_pharmacy/model_mixins/study_medication_crf_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from edc_appointment.utils import get_next_appointment
from edc_constants.constants import YES
from edc_utils import formatted_datetime
from edc_visit_tracking.utils import get_next_related_visit

from ..exceptions import NextStudyMedicationError, StudyMedicationError
from ..refill import create_refills_from_crf
Expand Down Expand Up @@ -38,15 +39,19 @@ def save(self, *args, **kwargs):
f"Perhaps catch this in the form. See {self.related_visit}."
)

if self.refill_to_next_visit == YES:
# overwrite the value of refill_end_datetime coming from
# the form with the date of next visit or appointment.
self.refill_end_datetime = getattr(
get_next_related_visit(self.related_visit), "report_datetime", None
) or getattr(
get_next_appointment(self.related_visit.appointment, include_interim=True),
"appt_datetime",
None,
)
if not self.refill_end_datetime:
if self.refill_to_next_visit == YES:
self.refill_end_datetime = get_next_appointment(
self.related_visit.appointment, include_interim=True
).appt_datetime
else:
self.refill_end_datetime = self.refill_start_datetime

self.adjust_end_datetimes()
# if None, means there is not a next appointment
self.refill_end_datetime = self.refill_start_datetime

self.number_of_days = (self.refill_end_datetime - self.refill_start_datetime).days

Expand Down
32 changes: 12 additions & 20 deletions edc_pharmacy/models/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from django.dispatch import receiver

from ..dispense import Dispensing
from ..model_mixins import StudyMedicationCrfModelMixin
from ..utils import update_previous_refill_end_datetime
from .dispensing_history import DispensingHistory

# from .order import Order
# from .rx_refill import RxRefill
from .stock_create_labels import Labels, StockCreateLabels


Expand Down Expand Up @@ -51,23 +50,16 @@ def create_or_update_refills_on_post_save(
pass


# @receiver(
# post_save,
# sender=RxRefill,
# dispatch_uid="create_or_update_refills_on_post_save",
# )
# def get_or_create_subject_orders_on_refill(
# sender, instance, raw, created, update_fields, **kwargs
# ):
# """Confirm or create an order for the items requested thru the refill"""
# if not raw:
# pass
# instance.total
# Order.objects.filter(product=product)
# # check orders
# # is the product requested on order
# # is the product requested on order and delivered to
# # check patient_stock
@receiver(
post_save,
dispatch_uid="update_refill_end_datetime",
)
def update_previous_refill_end_datetime_on_post_save(
sender, instance, raw, created, update_fields, **kwargs
):
if not raw and not update_fields:
if isinstance(instance, (StudyMedicationCrfModelMixin,)):
update_previous_refill_end_datetime(instance)


@receiver(
Expand Down
18 changes: 18 additions & 0 deletions edc_pharmacy/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from dateutil.relativedelta import relativedelta
from django.apps import apps as django_apps
from django.core.exceptions import ObjectDoesNotExist
from edc_visit_tracking.utils import get_previous_related_visit


def get_rxrefill_model_cls():
Expand All @@ -7,3 +10,18 @@ def get_rxrefill_model_cls():

def get_rx_model_cls():
return django_apps.get_model("edc_pharmacy.rx")


def update_previous_refill_end_datetime(instance):
"""Update refill_end_datetime from previous visit relative to the
refill_start_datetime of this visit.
"""
if previous_visit := get_previous_related_visit(instance.related_visit):
opts = {instance.__class__.related_visit_model_attr(): previous_visit}
try:
obj = instance.__class__.objects.get(**opts)
except ObjectDoesNotExist:
pass
else:
obj.refill_end_datetime = instance.refill_start_datetime - relativedelta(seconds=1)
obj.save_base(update_fields=["refill_end_datetime"])

0 comments on commit 691989b

Please sign in to comment.