--
This commit is contained in:
@@ -6,9 +6,42 @@ from app.reports.schemas import (
|
||||
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,
|
||||
@@ -18,8 +51,11 @@ def to_read(dcr: DailyCallReport) -> DCRRead:
|
||||
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,
|
||||
@@ -39,6 +75,7 @@ def to_read(dcr: DailyCallReport) -> DCRRead:
|
||||
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)
|
||||
@@ -54,6 +91,7 @@ def to_read(dcr: DailyCallReport) -> DCRRead:
|
||||
)
|
||||
for c in sorted(dcr.chemist_calls, key=lambda x: x.visit_time)
|
||||
],
|
||||
planned_visits=planned_visits(dcr),
|
||||
)
|
||||
|
||||
|
||||
@@ -61,6 +99,7 @@ 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,
|
||||
|
||||
Reference in New Issue
Block a user