Skip to content

Instantly share code, notes, and snippets.

@aimedey19
Created November 25, 2022 13:35
Show Gist options
  • Save aimedey19/780ea1907bfab7f077e73560fba29792 to your computer and use it in GitHub Desktop.
Save aimedey19/780ea1907bfab7f077e73560fba29792 to your computer and use it in GitHub Desktop.
Reports views
from operator import itemgetter
from django.db.models import Q
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from render_block import render_block_to_string
from django.conf import settings
from smarthealth.laboratory.models import MedicalExamItem, Laboratory
from smarthealth.prescriptions.models import (
Prescription,
Drugstore,
DrugSale,
DrugSaleTransaction,
)
from smarthealth.utils import render_block
from smarthealth.waiting_list.models import Hospital
def reports(request: HttpRequest):
data = []
for hospital in Hospital.objects.all():
total_amount = float(
sum(
[
item.analysis.price
for item in MedicalExamItem.objects.filter(
Q(exam__consultation__hospital=hospital)
)
]
)
) + float(
sum(
[
prescription.total
for prescription in Prescription.objects.filter(
Q(ordonance__hospital=hospital)
)
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
data.append(
{
"hospital": hospital,
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"clients_amount": round(clients_amount, 2),
}
)
data.sort(key=itemgetter("total_amount"), reverse=True)
context = {"hospitals_prestations": data[:10]}
# Drugstores
data1 = []
for drugstore in Drugstore.objects.all():
total_amount = float(
sum(
[
drugsale.total
for drugsale in DrugSale.objects.filter(Q(drugstore=drugstore))
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
refunded_amount = float(
sum(
[
transaction.refunded
for transaction in drugstore.drugsaletransaction_set.all()
]
)
)
data1.append(
{
"drugstore": drugstore,
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"refunded_amount": round(refunded_amount, 2),
"clients_amount": round(clients_amount, 2),
}
)
data1.sort(key=itemgetter("total_amount"), reverse=True)
context["drugstores_prestations"] = data1[:10]
# print(data1, " drugstores prestations")
# Laboratories
data2 = []
for laboratory in Laboratory.objects.all():
total_amount = float(
sum(
[
item.analysis.price
for item in MedicalExamItem.objects.filter(Q(laboratory=laboratory))
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
data2.append(
{
"laboratory": laboratory,
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"clients_amount": round(clients_amount, 2),
}
)
data2.sort(key=itemgetter("total_amount"), reverse=True)
context["laboratories_prestations"] = data2[:10]
total_amount = float(
sum(
[
item.analysis.price
for item in MedicalExamItem.objects.filter(is_active=False)
]
)
) + float(
sum(
[
prescription.total
for prescription in Prescription.objects.filter(is_active=False)
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
context.update({
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"clients_amount": round(clients_amount, 2),
"refunded": float(
sum(
[
transaction.refunded
for transaction in DrugSaleTransaction.objects.all()
]
)
),
"rest": round(insurance_covered, 2)
- float(
sum(
[
transaction.refunded
for transaction in DrugSaleTransaction.objects.all()
]
)
),
})
return render(request, "reports/reports.html", context)
# Create your views here.
def hospital_prestations(request: HttpRequest):
# Hospitals
data = []
print("*" * 500)
for hospital in Hospital.objects.all():
total_amount = float(
sum(
[
item.analysis.price
for item in MedicalExamItem.objects.filter(
Q(exam__consultation__hospital=hospital)
)
]
)
) + float(
sum(
[
prescription.total
for prescription in Prescription.objects.filter(
Q(ordonance__hospital=hospital)
)
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
data.append(
{
"hospital": hospital,
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"clients_amount": round(clients_amount, 2),
}
)
data.sort(key=itemgetter("total_amount"), reverse=True)
context = {"hospitals_prestations": data[:10]}
return render_block(
"reports/reports.html",
"hospitals_report",
# {"analysis_count": MedicalExamItem.objects.count()},
context,
)
def drugstore_prestations(request: HttpRequest):
data = []
for drugstore in Drugstore.objects.all():
total_amount = float(
sum(
[
drugsale.total
for drugsale in DrugSale.objects.filter(Q(drugstore=drugstore))
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
refunded_amount = float(
sum(
[
transaction.refunded
for transaction in drugstore.drugsaletransaction_set.all()
]
)
)
clients_amount = total_amount - insurance_covered
data.append(
{
"drugstore": drugstore,
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"refunded_amount": round(refunded_amount, 2),
"clients_amount": round(clients_amount, 2),
}
)
block_name = ""
template_name = ""
data.sort(key=itemgetter("total_amount"), reverse=True)
context = {"drugstores_prestations": data[:10]}
return render_block(
"reports/reports.html",
"drugstores_sales",
# {"analysis_count": MedicalExamItem.objects.count()},
context,
)
# return HttpResponse(render_block_to_string(template_name, block_name, context))
def laboratory_prestations(request: HttpRequest):
data = []
for laboratory in Laboratory.objects.all():
total_amount = float(
sum(
[
item.analysis.price
for item in MedicalExamItem.objects.filter(Q(laboratory=laboratory))
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
data.append(
{
"laboratory": laboratory,
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"clients_amount": round(clients_amount, 2),
}
)
block_name = ""
template_name = ""
context = {"laboratories_prestations": data}
return render_block(
"reports/reports.html",
"laboratories_report",
# {"analysis_count": MedicalExamItem.objects.count()},
context,
)
def global_stats(request: HttpRequest):
total_amount = float(
sum(
[
item.analysis.price
for item in MedicalExamItem.objects.filter(is_active=False)
]
)
) + float(
sum(
[
prescription.total
for prescription in Prescription.objects.filter(is_active=False)
]
)
)
insurance_covered = total_amount * settings.ICASH_INSURANCE_COVERED_PERCENT
clients_amount = total_amount - insurance_covered
block_name = ""
template_name = ""
context = {
"total_amount": total_amount,
"insurance_amount": round(insurance_covered, 2),
"clients_amount": round(clients_amount, 2),
"refunded": float(
sum(
[
transaction.refunded
for transaction in DrugSaleTransaction.objects.all()
]
)
),
"rest": round(insurance_covered, 2)
- float(
sum(
[
transaction.refunded
for transaction in DrugSaleTransaction.objects.all()
]
)
),
}
return render_block(
"reports/reports.html",
"top_stats",
# {"analysis_count": MedicalExamItem.objects.count()},
context,
)
def drugstores_sales_transactions(request: HttpRequest):
transactions = DrugSaleTransaction.objects.all()
if request.method == "POST":
drugstore_keyword = request.POST.get("drugstore") or ""
ordonance_keyword = request.POST.get("ordinance") or ""
patient_keyword = request.POST.get("patient") or ""
drugstore_filter_ = Q(drugstore__name__icontains=drugstore_keyword.strip())
ordonance_filter = Q(
ordonance__reference_number__icontains=ordonance_keyword.strip()
)
patient_filter = Q(patient__name__icontains=patient_keyword.strip())
transactions = DrugSaleTransaction.objects.filter(
drugstore_filter_, ordonance_filter, patient_filter
)
return render_block(
"reports/pharmacy-transactions.html",
"transactions_table",
context={"transactions": transactions},
)
return render(
request,
"reports/pharmacy-transactions.html",
context={"transactions": transactions},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment