Coverage for apps/appointments/resources.py: 60%
73 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
1import tablib
2from django.db.models import Sum
3from django.utils.timezone import datetime
4from djmoney.money import Money
5from import_export import resources
6from import_export.fields import Field
8from .models import Appointment, AppointmentCharge
11class AppointmentResource(resources.ModelResource):
12 """
13 Import export resource for Appoinment
14 """
16 date = Field(column_name="Fecha")
17 time = Field(column_name="Hora")
18 appointment_type = Field(column_name="Tipo")
19 patient = Field(column_name="Paciente")
20 product = Field(column_name="Producto")
21 quantity = Field(column_name="Cantidad")
22 price = Field(column_name="Precio")
23 shared_cost = Field(column_name="Costo Compartido")
24 hard_cost = Field(column_name="Costo Duro")
25 total_price = Field(column_name="Venta")
26 total_shared_cost = Field(column_name="Costo Compartido")
27 total_hard_cost = Field(column_name="Costo Duro")
28 utility = Field(column_name="Utilidad")
29 paid_amount = Field(column_name="Pagado")
30 pending_amount = Field(column_name="Saldo")
32 class Meta:
33 model = Appointment
34 fields = []
35 queryset = Appointment.objects.all()
37 def get_queryset(self):
38 return self.Meta.queryset
40 def export(self, queryset=None, *args, **kwargs):
41 """
42 Exports a resource.
43 """
45 def export_row(
46 date: str = "",
47 time: str = "",
48 appointment_type: str = "",
49 patient: str = "",
50 product: str = "",
51 quantity: int = "",
52 price: str = "",
53 shared_cost: str = "",
54 hard_cost: str = "",
55 total_price: str = "",
56 total_shared_cost: str = "",
57 total_hard_cost: str = "",
58 utility: str = "",
59 paid_amount: str = "",
60 pending_amount: str = "",
61 ):
62 return [
63 str(date),
64 str(time),
65 str(appointment_type),
66 str(patient),
67 str(product),
68 str(quantity),
69 str(price),
70 str(shared_cost),
71 str(hard_cost),
72 str(total_price),
73 str(total_shared_cost),
74 str(total_hard_cost),
75 str(utility),
76 str(paid_amount),
77 str(pending_amount),
78 ]
80 if queryset is None:
81 queryset = self.get_queryset()
82 headers = self.get_export_headers()
83 data = tablib.Dataset(headers=headers)
85 dates = queryset.values_list("date", flat=True).distinct().order_by()
87 for date in dates:
88 date_appointments = queryset.filter(date=date)
89 total_price = date_appointments.aggregate(total_price=Sum("total_price")).get("total_price") or 0
90 total_shared_cost = (
91 date_appointments.aggregate(total_shared_cost=Sum("total_shared_cost")).get("total_shared_cost") or 0
92 )
93 total_hard_cost = (
94 date_appointments.aggregate(total_hard_cost=Sum("total_hard_cost")).get("total_hard_cost") or 0
95 )
96 pending_amount = (
97 date_appointments.aggregate(pending_amount=Sum("pending_amount")).get("pending_amount") or 0
98 )
99 paid_amount = date_appointments.aggregate(paid_amount=Sum("paid_amount")).get("paid_amount") or 0
100 utility = total_price - total_shared_cost - total_hard_cost
101 data.append(
102 export_row(
103 date=date,
104 total_price=Money(total_price, currency="MXN"),
105 total_shared_cost=Money(total_shared_cost, currency="MXN"),
106 total_hard_cost=Money(total_hard_cost, currency="MXN"),
107 utility=Money(utility, currency="MXN"),
108 pending_amount=Money(pending_amount, currency="MXN"),
109 paid_amount=Money(paid_amount, currency="MXN"),
110 )
111 )
113 for appointment in date_appointments:
114 data.append(
115 export_row(
116 appointment_type=appointment.type_of,
117 time=appointment.start_time,
118 patient=appointment.patient.full_name,
119 )
120 )
121 appointment_charges = appointment.charges.all()
123 for appointment_charge in appointment_charges:
124 utility = (
125 appointment_charge.total_price
126 - appointment_charge.total_shared_cost
127 - appointment_charge.total_hard_cost
128 )
129 data.append(
130 export_row(
131 product=appointment_charge.product,
132 quantity=appointment_charge.quantity,
133 price=appointment_charge.price,
134 shared_cost=appointment_charge.shared_cost,
135 hard_cost=appointment_charge.hard_cost,
136 total_price=appointment_charge.total_price,
137 total_shared_cost=appointment_charge.total_shared_cost,
138 total_hard_cost=appointment_charge.total_hard_cost,
139 utility=utility,
140 paid_amount=appointment_charge.paid_amount,
141 pending_amount=appointment_charge.pending_amount,
142 )
143 )
144 data.append(export_row())
146 self.after_export(queryset, data, *args, **kwargs)
148 return data
151class AppointmentChargeResource(resources.ModelResource):
152 """
153 Import export resource for AppointmentCharge
154 """
156 # TODO: Impmlement link field
158 date = Field(attribute="date", column_name="Fecha")
159 patient = Field(attribute="appointment__patient__full_name", column_name="Paciente")
160 # link = Field(column_name="Link Consulta")
161 product = Field(attribute="product__name", column_name="Servicio")
162 practitioner = Field(column_name="Doctor")
163 quantity = Field(attribute="quantity", column_name="Cantidad")
164 price = Field(attribute="price", column_name="Precio")
165 total_price = Field(attribute="total_price", column_name="Total")
166 paid_amount = Field(attribute="paid_amount", column_name="Pagado")
167 pending_amount = Field(attribute="pending_amount", column_name="Pendiente")
168 days_delayed = Field(column_name="Dias de atraso")
170 class Meta:
171 model = AppointmentCharge
172 fields = [
173 "date",
174 "patient",
175 # "link",
176 "product",
177 "type_of",
178 "practitioner",
179 "quantity",
180 "price",
181 "total_price",
182 "paid_amount",
183 "pending_amount",
184 "days_delayed",
185 ]
187 def dehydrate_practitioner(self, appointment_charge: AppointmentCharge):
188 return appointment_charge.product.practitioner.full_name
190 def dehydrate_days_delayed(self, appointment_charge: AppointmentCharge):
191 return (datetime.today().date() - appointment_charge.appointment.date).days