Files
celtic-life-science/app/reports/serializers.py
T
Shubhankar45 0d30c58bf0 Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during
server cleanup. .env files, node_modules, builds, logs and media were excluded.
2026-11-07 05:43:12 +00:00

133 lines
4.5 KiB
Python

"""ORM → response-schema mapping for reports."""
from app.reports.models import DailyCallReport
from app.reports.schemas import (
ChemistCallRead,
DCRListItem,
DCRRead,
DoctorCallRead,
PlannedVisitRead,
StockistCallRead,
)
def planned_visits(dcr: DailyCallReport) -> list[PlannedVisitRead]:
"""The approved plan, line by line, checked against this end-of-day report."""
plan = dcr.plan
if plan is None:
return []
visited = (
{c.doctor_id for c in dcr.doctor_calls}
| {c.chemist_id for c in dcr.chemist_calls}
| {c.stockist_id for c in dcr.stockist_calls}
)
missed = {m.doctor_id or m.chemist_id or m.stockist_id: m for m in dcr.missed_visits}
def line(kind, target_id, name, is_core, planned_time) -> PlannedVisitRead:
note = missed.get(target_id)
status = "VISITED" if target_id in visited else "MISSED" if note else "PENDING"
return PlannedVisitRead(
kind=kind,
target_id=target_id,
name=name,
is_core_doctor=is_core,
planned_time=planned_time,
status=status,
missed_visit_id=note.id if note and status == "MISSED" else None,
missed_reason=note.reason if note and status == "MISSED" else None,
)
lines = [
line("DOCTOR", c.doctor_id, c.doctor.name, c.doctor.is_core_doctor, c.visit_time)
for c in plan.doctor_calls
] + [
line("CHEMIST", c.chemist_id, c.chemist.name, False, c.visit_time)
for c in plan.chemist_calls
] + [
line("STOCKIST", c.stockist_id, c.stockist.name, False, c.visit_time)
for c in plan.stockist_calls
]
return sorted(lines, key=lambda x: x.planned_time)
def to_read(dcr: DailyCallReport) -> DCRRead:
return DCRRead(
id=dcr.id,
mr_user_id=dcr.mr_user_id,
mr_name=dcr.mr.full_name,
employee_code=dcr.mr.employee_code,
headquarters_id=dcr.headquarters_id,
headquarters_name=dcr.headquarters.name,
report_date=dcr.report_date,
report_type=dcr.report_type,
status=dcr.status,
is_locked=dcr.is_locked,
plan_id=dcr.plan_id,
actual_report_id=dcr.actual.id if dcr.actual else None,
mr_remarks=dcr.mr_remarks,
stockist_visit=dcr.stockist_visit,
order_value=dcr.order_value,
return_reason=dcr.return_reason,
returned_by_role=dcr.returned_by_role,
submitted_at=dcr.submitted_at,
approved_at=dcr.approved_at,
doctor_calls=[
DoctorCallRead(
id=c.id,
doctor_id=c.doctor_id,
doctor_name=c.doctor.name,
doctor_speciality=c.doctor.speciality,
is_core_doctor=c.doctor.is_core_doctor,
visit_time=c.visit_time,
call_outcome=c.call_outcome,
conversion_status=c.conversion_status,
product_promote=c.product_promote,
product_sample=c.product_sample,
gift=c.gift,
remarks=c.remarks,
)
for c in sorted(dcr.doctor_calls, key=lambda x: x.visit_time)
],
chemist_calls=[
ChemistCallRead(
id=c.id,
chemist_id=c.chemist_id,
chemist_name=c.chemist.name,
visit_time=c.visit_time,
pob=c.pob,
remarks=c.remarks,
)
for c in sorted(dcr.chemist_calls, key=lambda x: x.visit_time)
],
stockist_calls=[
StockistCallRead(
id=c.id,
stockist_id=c.stockist_id,
stockist_name=c.stockist.name,
visit_time=c.visit_time,
pob=c.pob,
remarks=c.remarks,
)
for c in sorted(dcr.stockist_calls, key=lambda x: x.visit_time)
],
planned_visits=planned_visits(dcr),
)
def to_list_item(dcr: DailyCallReport) -> DCRListItem:
return DCRListItem(
id=dcr.id,
report_date=dcr.report_date,
report_type=dcr.report_type,
status=dcr.status,
mr_user_id=dcr.mr_user_id,
mr_name=dcr.mr.full_name,
headquarters_id=dcr.headquarters_id,
headquarters_name=dcr.headquarters.name,
doctor_call_count=len(dcr.doctor_calls),
chemist_call_count=len(dcr.chemist_calls),
submitted_at=dcr.submitted_at,
approved_at=dcr.approved_at,
returned_by_role=dcr.returned_by_role,
)