Files
celtic-life-science/app/reports/schemas.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

210 lines
6.4 KiB
Python

import uuid
from datetime import date, datetime, time
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator
from app.common.enums import CallOutcome, ConversionStatus, DCRStatus, ReportType, Role
from app.config.settings import settings
#: How many promoted (or sample) products one doctor visit may name.
MAX_PRODUCTS_PER_VISIT = 10
def _product_list(value: str | list[str] | None) -> list[str]:
"""Trimmed, de-duplicated product names. A single string is still accepted."""
items = [value] if isinstance(value, str) else value or []
names: list[str] = []
for item in items:
if not isinstance(item, str):
raise ValueError("Product names must be text")
name = item.strip()
if len(name) > 200:
raise ValueError("A product name must be 200 characters or fewer")
if name and name.casefold() not in {n.casefold() for n in names}:
names.append(name)
if len(names) > MAX_PRODUCTS_PER_VISIT:
raise ValueError(f"Choose at most {MAX_PRODUCTS_PER_VISIT} products")
return names
class DoctorCallCreate(BaseModel):
doctor_id: uuid.UUID
visit_time: time
#: Required on an end-of-day report; ignored on a plan (a planned visit has no outcome).
call_outcome: CallOutcome | None = None
#: V7 — accepted only when the doctor is flagged as a core doctor.
conversion_status: ConversionStatus | None = None
product_promote: list[str] = Field(default_factory=list)
product_sample: list[str] = Field(default_factory=list)
gift: str | None = Field(default=None, max_length=200)
remarks: str | None = Field(default=None, max_length=2000)
_products = field_validator("product_promote", "product_sample", mode="before")(
_product_list
)
class ChemistCallCreate(BaseModel):
chemist_id: uuid.UUID
visit_time: time
pob: str | None = Field(default=None, max_length=100)
remarks: str | None = Field(default=None, max_length=2000)
class StockistCallCreate(BaseModel):
stockist_id: uuid.UUID
visit_time: time
pob: str | None = Field(default=None, max_length=100)
remarks: str | None = Field(default=None, max_length=2000)
class DoctorCallRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
doctor_id: uuid.UUID
doctor_name: str
doctor_speciality: str | None
is_core_doctor: bool
visit_time: time
call_outcome: CallOutcome | None
conversion_status: ConversionStatus | None
product_promote: list[str]
product_sample: list[str]
gift: str | None = None
remarks: str | None
class ChemistCallRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
chemist_id: uuid.UUID
chemist_name: str
visit_time: time
pob: str | None
remarks: str | None
class StockistCallRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
stockist_id: uuid.UUID
stockist_name: str
visit_time: time
pob: str | None
remarks: str | None
class DCRCreate(BaseModel):
report_date: date
report_type: ReportType = ReportType.ACTUAL
#: SELF reports only: which of the Area Business Manager's headquarters the visits were in.
headquarters_id: uuid.UUID | None = None
mr_remarks: str | None = Field(default=None, max_length=2000)
doctor_calls: list[DoctorCallCreate] = Field(default_factory=list)
chemist_calls: list[ChemistCallCreate] = Field(default_factory=list)
class DCRUpdate(BaseModel):
mr_remarks: str | None = Field(default=None, max_length=2000)
stockist_visit: str | None = Field(default=None, max_length=200)
order_value: str | None = Field(default=None, max_length=100)
class MissedVisitSet(BaseModel):
"""Why one planned doctor, chemist OR stockist was not visited (end-of-day report only)."""
doctor_id: uuid.UUID | None = None
chemist_id: uuid.UUID | None = None
stockist_id: uuid.UUID | None = None
reason: str = Field(min_length=3, max_length=500)
class PlannedVisitRead(BaseModel):
"""One line of the approved plan, checked against the end-of-day report.
VISITED — the MR recorded a visit; MISSED — the MR explained why not;
PENDING — neither yet (the report cannot be submitted while any remain).
"""
kind: Literal["DOCTOR", "CHEMIST", "STOCKIST"]
target_id: uuid.UUID
name: str
is_core_doctor: bool
planned_time: time
status: Literal["VISITED", "MISSED", "PENDING"]
missed_visit_id: uuid.UUID | None
missed_reason: str | None
class ReturnRequest(BaseModel):
"""V8 — a manager must say WHY. This reason is what makes the audit trail useful."""
reason: str = Field(min_length=settings.MIN_RETURN_REASON_LENGTH, max_length=2000)
class ApproveRequest(BaseModel):
comment: str | None = Field(default=None, max_length=2000)
class DCRListItem(BaseModel):
"""Row for S2/S6 — the list screens that replaced the cut dashboards."""
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
report_date: date
report_type: ReportType
status: DCRStatus
mr_user_id: uuid.UUID
mr_name: str
headquarters_id: uuid.UUID
headquarters_name: str
doctor_call_count: int
chemist_call_count: int
submitted_at: datetime | None
approved_at: datetime | None
returned_by_role: Role | None
class DCRRead(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: uuid.UUID
mr_user_id: uuid.UUID
mr_name: str
employee_code: str
headquarters_id: uuid.UUID
headquarters_name: str
report_date: date
report_type: ReportType
status: DCRStatus
is_locked: bool
#: End-of-day report → its plan; plan → its end-of-day report, once started.
plan_id: uuid.UUID | None
actual_report_id: uuid.UUID | None
mr_remarks: str | None
stockist_visit: str | None
order_value: str | None
return_reason: str | None
returned_by_role: Role | None
submitted_at: datetime | None
approved_at: datetime | None
doctor_calls: list[DoctorCallRead]
chemist_calls: list[ChemistCallRead]
stockist_calls: list[StockistCallRead]
#: End-of-day report only: the approved plan, line by line, with what became of it.
planned_visits: list[PlannedVisitRead]
class StatusCounts(BaseModel):
"""Header counts on the list screens — the cheap replacement for dashboards."""
DRAFT: int = 0
PENDING_ABM: int = 0
RETURNED: int = 0
APPROVED: int = 0