Files
celtic-life-science/app/reports/models.py
T
Shubhankar45 8fdd20b29f Production
2026-09-22 06:23:46 +00:00

145 lines
5.9 KiB
Python

"""The Daily Call Report and its call rows — the core artefact of the MVP."""
import uuid
from datetime import date, datetime, time
from sqlalchemy import (
CheckConstraint,
Date,
DateTime,
Enum,
ForeignKey,
String,
Text,
Time,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import UUID as PGUUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.common.enums import CallOutcome, ConversionStatus, DCRStatus, Role
from app.database.base import Base, TimestampMixin, UUIDMixin
class DailyCallReport(Base, UUIDMixin, TimestampMixin):
__tablename__ = "daily_call_report"
__table_args__ = (
# V1 / A9 — ONE report per MR per calendar date. Enforced in the database and
# not only in the service, so a double-submit race cannot create two reports.
UniqueConstraint("mr_user_id", "report_date", name="uq_dcr_mr_date"),
# An approved report must carry its approval timestamp, and nothing else may.
CheckConstraint(
"(status = 'APPROVED' AND approved_at IS NOT NULL) "
"OR (status <> 'APPROVED' AND approved_at IS NULL)",
name="ck_dcr_approved_at",
),
)
mr_user_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="RESTRICT"), nullable=False
)
#: SNAPSHOT of the MR's headquarters at creation time. If the MR is later
#: transferred, historical reports keep the HQ they were filed under (A1).
headquarters_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("headquarters.id", ondelete="RESTRICT"), nullable=False
)
report_date: Mapped[date] = mapped_column(Date, nullable=False)
status: Mapped[DCRStatus] = mapped_column(
Enum(DCRStatus, name="dcr_status_enum", native_enum=False, length=20),
default=DCRStatus.DRAFT,
nullable=False,
)
#: SNAPSHOT of the reviewing AM / approving GM, so an HQ handover never rewrites
#: history (report §5.5).
current_am_user_id: Mapped[uuid.UUID | None] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="RESTRICT")
)
current_gm_user_id: Mapped[uuid.UUID | None] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="RESTRICT")
)
mr_remarks: Mapped[str | None] = mapped_column(Text)
#: The printed form's "STOCKIST VISIT" and "ORDER" lines.
stockist_visit: Mapped[str | None] = mapped_column(String(200))
order_value: Mapped[str | None] = mapped_column(String(100))
return_reason: Mapped[str | None] = mapped_column(Text)
#: Which role sent it back — data, not a separate status (report §7.2).
returned_by_role: Mapped[Role | None] = mapped_column(
Enum(Role, name="role_enum", native_enum=False, length=10)
)
submitted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
approved_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
pdf_path: Mapped[str | None] = mapped_column(String(500))
mr: Mapped["User"] = relationship(foreign_keys=[mr_user_id]) # noqa: F821
headquarters: Mapped["Headquarters"] = relationship() # noqa: F821
doctor_calls: Mapped[list["DoctorCall"]] = relationship(
back_populates="dcr", cascade="all, delete-orphan", lazy="selectin"
)
chemist_calls: Mapped[list["ChemistCall"]] = relationship(
back_populates="dcr", cascade="all, delete-orphan", lazy="selectin"
)
@property
def is_locked(self) -> bool:
"""APPROVED is the lock. There is no separate LOCKED status (report §7.2)."""
return self.status == DCRStatus.APPROVED
class DoctorCall(Base, UUIDMixin, TimestampMixin):
__tablename__ = "doctor_call"
__table_args__ = (
# V5 / A11 — a doctor may appear only once per report.
UniqueConstraint("dcr_id", "doctor_id", name="uq_call_dcr_doctor"),
)
dcr_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("daily_call_report.id", ondelete="CASCADE"),
nullable=False,
)
doctor_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("doctor.id", ondelete="RESTRICT"), nullable=False
)
visit_time: Mapped[time] = mapped_column(Time, nullable=False)
call_outcome: Mapped[CallOutcome] = mapped_column(
Enum(CallOutcome, name="call_outcome_enum", native_enum=False, length=20),
nullable=False,
)
#: V7 — only permitted when the doctor is a core doctor.
conversion_status: Mapped[ConversionStatus | None] = mapped_column(
Enum(ConversionStatus, name="conversion_status_enum", native_enum=False, length=20)
)
#: The printed form's "PRODUCT PROMOTE" and "PRODUCT SAMPLE" columns.
product_promote: Mapped[str | None] = mapped_column(String(200))
product_sample: Mapped[str | None] = mapped_column(String(200))
remarks: Mapped[str | None] = mapped_column(Text)
dcr: Mapped["DailyCallReport"] = relationship(back_populates="doctor_calls")
doctor: Mapped["Doctor"] = relationship(lazy="selectin") # noqa: F821
class ChemistCall(Base, UUIDMixin, TimestampMixin):
__tablename__ = "chemist_call"
__table_args__ = (
UniqueConstraint("dcr_id", "chemist_id", name="uq_call_dcr_chemist"),
)
dcr_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True),
ForeignKey("daily_call_report.id", ondelete="CASCADE"),
nullable=False,
)
chemist_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("chemist.id", ondelete="RESTRICT"), nullable=False
)
visit_time: Mapped[time] = mapped_column(Time, nullable=False)
#: The printed form's "POB" column.
pob: Mapped[str | None] = mapped_column(String(100))
remarks: Mapped[str | None] = mapped_column(Text)
dcr: Mapped["DailyCallReport"] = relationship(back_populates="chemist_calls")
chemist: Mapped["Chemist"] = relationship(lazy="selectin") # noqa: F821