114 lines
3.9 KiB
Python
114 lines
3.9 KiB
Python
"""ORM → response-schema mapping for reports."""
|
|
|
|
from app.reports.models import DailyCallReport
|
|
from app.reports.schemas import (
|
|
ChemistCallRead,
|
|
DCRListItem,
|
|
DCRRead,
|
|
DoctorCallRead,
|
|
PlannedVisitRead,
|
|
)
|
|
|
|
|
|
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}
|
|
missed = {m.doctor_id or m.chemist_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
|
|
]
|
|
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)
|
|
],
|
|
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,
|
|
)
|