Coverage for apps/products/signals.py: 26%
39 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
1from django.db.models import signals
2from django.dispatch import receiver
4from apps.appointments.models import AppointmentType
5from apps.invoices.models import ProductInvoiceConfiguration
7from .models import (
8 ConsultationProduct,
9 HospitalizationProduct,
10 ProcedureProduct,
11 Product,
12 SellableProduct,
13 SurgicalProduct,
14)
17@receiver(signals.post_save, sender=ConsultationProduct)
18def create_appointment_type_for_product(sender, instance, created, *args, **kwargs):
19 if created:
20 appointment_type = AppointmentType.objects.create(
21 practitioner=instance.practitioner, name=instance.name, duration=instance.duration
22 )
23 appointment_type.products.add(instance)
24 appointment_type.save()
25 else:
26 for item in instance.appointmenttypeproduct_set.filter(is_binding=True):
27 item.appointment_type.name = instance.name
28 item.appointment_type.is_active = instance.is_active
29 item.appointment_type.duration = instance.duration
31 item.appointment_type.save()
34@receiver(signals.pre_delete, sender=ConsultationProduct)
35def delete_appointment_type_for_product(sender, instance, *args, **kwargs):
36 for item in instance.appointmenttypeproduct_set.filter(is_binding=True):
37 item.appointment_type.is_active = False
38 item.appointment_type.save()
41@receiver(signals.post_save, sender=ConsultationProduct)
42@receiver(signals.post_save, sender=HospitalizationProduct)
43@receiver(signals.post_save, sender=ProcedureProduct)
44@receiver(signals.post_save, sender=SellableProduct)
45@receiver(signals.post_save, sender=SurgicalProduct)
46def set_product_invoice_configuration(sender, instance: Product, created, *args, **kwargs):
47 if created:
48 if isinstance(instance, ConsultationProduct):
49 product_key = "85121600"
50 elif isinstance(instance, HospitalizationProduct):
51 product_key = "85101502"
52 elif isinstance(instance, ProcedureProduct):
53 product_key = "85101503"
54 elif isinstance(instance, SellableProduct):
55 product_key = "85121800"
56 elif isinstance(instance, SurgicalProduct):
57 product_key = "85121609"
59 ProductInvoiceConfiguration.objects.create(
60 product=instance, description=instance.name, product_key=product_key, unit_key="C62"
61 )