Coverage for apps/appointments/signals.py: 26%
81 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-09 16:59 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-09 16:59 -0600
1from django.core.files.base import ContentFile
2from django.db.models import signals
3from django.dispatch import receiver
5from apps.products.models import ProductOrganizationPrice
6from services.pusher import PusherClient
8from .models import (
9 Appointment,
10 AppointmentCharge,
11 AppointmentChargeExport,
12 AppointmentExport,
13 AppointmentPractitioner,
14 GoogleCalendarEvent,
15)
16from .reports import create_appointment_charge_report, create_appointment_report
17from .resources import AppointmentChargeResource, AppointmentResource
19# Appointments
20#####################
23@receiver(signals.pre_save, sender=Appointment)
24def calculate_totals_for_appointment(sender, instance: Appointment, *args, **kwargs):
25 if instance.status == instance.StatusChoices.CLOSED and instance.pending_amount.amount > 0:
26 instance.overdue_appointment()
27 if instance.status == instance.StatusChoices.OVERDUE and instance.pending_amount.amount == 0:
28 instance.close_appointment()
31@receiver(signals.post_save, sender=AppointmentPractitioner)
32def post_save_appointment_practitioner_calendarevent(
33 sender, instance: AppointmentPractitioner, created, *args, **kwargs
34):
35 calendar = getattr(instance.practitioner, "google_calendar", None)
36 if calendar:
37 from .services import GoogleCalendarEventHandler
39 gce, new = GoogleCalendarEvent.objects.get_or_create(calendar=calendar, appointment=instance.appointment)
40 gceh = GoogleCalendarEventHandler(gce=gce)
41 if new:
42 try:
43 gceh.create_event()
44 except Exception as e:
45 print("### Error al CREAR evento en google ####")
46 print(e)
47 else:
48 try:
49 gceh.update_event(event_id=gce.event_id)
50 except Exception as e:
51 print("### Error al ACTUALIZAR evento en google ####")
52 print(e)
55@receiver(signals.post_delete, sender=GoogleCalendarEvent)
56def post_delete_calendarevent(sender, instance: GoogleCalendarEvent, *args, **kwargs):
57 try:
58 from .services import GoogleCalendarEventHandler
60 gceh = GoogleCalendarEventHandler(gce=instance).delete_event(event_id=instance.event_id)
61 except Exception as e:
62 print(e)
65@receiver(signals.post_save, sender=Appointment)
66def update_apptmtpractitioners_for_appointment(sender, instance: Appointment, created, *args, **kwargs):
67 if getattr(instance, "googleevent", None):
68 instance.googleevent.save()
69 for item in instance.practitioners.all():
70 item.save()
73@receiver(signals.post_save, sender=Appointment)
74def set_default_appointment_charges(sender, instance: Appointment, created, *args, **kwargs):
75 if created:
76 if instance.type_of:
77 default_products = instance.type_of.products.filter(prices__organization=instance.organization)
79 for product in default_products:
80 prices = product.prices.filter(organization=instance.organization)
81 for price in prices:
82 instance.charges.create(
83 product=product,
84 hard_cost=price.hard_cost,
85 shared_cost=price.shared_cost,
86 price=price.price,
87 quantity=1,
88 )
91# Appointment Charges
92#####################
95@receiver(signals.pre_save, sender=AppointmentCharge)
96def set_pre_save_atributes_for_appointmentcharge(sender, instance: AppointmentCharge, **kwargs):
97 instance.side = "L"
98 pat_account = instance.appointment.patient.patientaccount
99 instance.account = pat_account
100 org = instance.appointment.organization
101 try:
102 prod_org = ProductOrganizationPrice.objects.get(
103 organization=org,
104 product=instance.product,
105 )
106 instance.hard_cost = instance.hard_cost or prod_org.hard_cost or 0
107 instance.shared_cost = instance.shared_cost or prod_org.shared_cost or 0
108 instance.price = instance.price or prod_org.price or 0
109 instance.quantity = instance.quantity or 1
110 instance.amount = instance.amount or instance.price * instance.quantity
111 except:
112 pass
115@receiver(signals.post_save, sender=AppointmentCharge)
116@receiver(signals.post_delete, sender=AppointmentCharge)
117def set_amounts_on_appointment_after_appointment_charge(sender, instance: AppointmentCharge, *args, **kwargs):
118 instance.appointment.save()
121# Appointment Exports
122#####################
125@receiver(signals.post_save, sender=AppointmentExport)
126def save_appointment_export_file(sender, instance: AppointmentExport, created, *args, **kwargs):
127 if created:
128 create_appointment_report(instance)
131@receiver(signals.post_save, sender=AppointmentChargeExport)
132def save_appointment_charge_export_file(sender, instance: AppointmentChargeExport, created, *args, **kwargs):
133 if created:
134 create_appointment_charge_report(instance)