--
This commit is contained in:
@@ -10,7 +10,7 @@ The Area Manager is the final approver; there is no General Manager stage.
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from app.common.enums import EDITABLE_STATUSES, DCRStatus, Role, WorkflowAction
|
||||
from app.common.enums import EDITABLE_STATUSES, FIELD_ROLES, DCRStatus, Role, WorkflowAction
|
||||
from app.common.exceptions import ForbiddenError, InvalidStateTransitionError
|
||||
|
||||
|
||||
@@ -62,7 +62,8 @@ def assert_transition(status: DCRStatus, action: WorkflowAction, role: Role) ->
|
||||
),
|
||||
},
|
||||
)
|
||||
if transition.allowed_role != role:
|
||||
# An SFE files and submits reports exactly as an MR does.
|
||||
if transition.allowed_role != (Role.MR if role in FIELD_ROLES else role):
|
||||
raise ForbiddenError(
|
||||
f"Role {role.value} may not {action.value.lower()} this report",
|
||||
{"required_role": transition.allowed_role.value, "your_role": role.value},
|
||||
|
||||
@@ -16,7 +16,7 @@ from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.security import decode_access_token
|
||||
from app.common.enums import Role
|
||||
from app.common.enums import Role, FIELD_ROLES
|
||||
from app.common.exceptions import ForbiddenError, UnauthorizedError
|
||||
from app.database.session import get_db
|
||||
from app.organization.models import AMHeadquarters
|
||||
@@ -51,7 +51,9 @@ def require_role(*allowed: Role):
|
||||
"""Layer 1. Usage: `user: User = Depends(require_role(Role.MR))`."""
|
||||
|
||||
async def _check(user: User = Depends(get_current_user)) -> User:
|
||||
if user.role not in allowed:
|
||||
# Wherever an MR is allowed, an SFE is too.
|
||||
permitted = set(allowed) | ({Role.SFE} if Role.MR in allowed else set())
|
||||
if user.role not in permitted:
|
||||
raise ForbiddenError(
|
||||
"Role not permitted for this action",
|
||||
{"your_role": user.role.value, "required": [r.value for r in allowed]},
|
||||
@@ -76,8 +78,8 @@ async def visible_hq_ids(db: AsyncSession, user: User) -> set[uuid.UUID] | None:
|
||||
return None
|
||||
if user.role == Role.AM:
|
||||
return await active_hq_ids_for_am(db, user.id)
|
||||
if user.role == Role.MR:
|
||||
return {user.headquarters_id} if user.headquarters_id else set()
|
||||
if user.role in FIELD_ROLES:
|
||||
return user.covered_headquarter_ids
|
||||
return set()
|
||||
|
||||
|
||||
@@ -96,7 +98,7 @@ async def assert_can_view_dcr(db: AsyncSession, user: User, dcr) -> None:
|
||||
"""
|
||||
if user.role in (Role.GM, Role.ADMIN):
|
||||
return
|
||||
if user.role == Role.MR and dcr.mr_user_id == user.id:
|
||||
if user.role in FIELD_ROLES and dcr.mr_user_id == user.id:
|
||||
return
|
||||
if user.role == Role.AM:
|
||||
if dcr.headquarters_id in await active_hq_ids_for_am(db, user.id):
|
||||
|
||||
@@ -31,3 +31,5 @@ class CurrentUser(BaseModel):
|
||||
role: Role
|
||||
headquarters_id: uuid.UUID | None
|
||||
is_active: bool
|
||||
is_superadmin: bool = False
|
||||
extra_headquarter_ids: list[uuid.UUID] = []
|
||||
|
||||
@@ -7,6 +7,7 @@ from sqlalchemy import Boolean, Date, ForeignKey, Index, String, UniqueConstrain
|
||||
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.common.enums import MasterApproval
|
||||
from app.database.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
@@ -25,6 +26,22 @@ class Chemist(Base, UUIDMixin, TimestampMixin):
|
||||
shop_address: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
contact_number: Mapped[str | None] = mapped_column(String(20))
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
#: PENDING until the Area Manager approves one an MR added; only APPROVED ones are listed or visited.
|
||||
approval_status: Mapped[str] = mapped_column(
|
||||
String(10), default=MasterApproval.APPROVED.value, server_default=MasterApproval.APPROVED.value, nullable=False
|
||||
)
|
||||
#: The MR who asked for it (None for one the administrator created).
|
||||
requested_by_user_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="SET NULL")
|
||||
)
|
||||
rejection_reason: Mapped[str | None] = mapped_column(String(500))
|
||||
requested_by: Mapped["User | None"] = relationship( # noqa: F821
|
||||
lazy="selectin", foreign_keys=[requested_by_user_id]
|
||||
)
|
||||
|
||||
@property
|
||||
def requested_by_name(self) -> str | None:
|
||||
return self.requested_by.full_name if self.requested_by else None
|
||||
|
||||
assignments: Mapped[list["ChemistAssignment"]] = relationship(back_populates="chemist")
|
||||
|
||||
|
||||
+34
-2
@@ -9,7 +9,7 @@ from app.chemists.schemas import ChemistCreate, ChemistRead, ChemistUpdate
|
||||
from app.chemists.service import ChemistService
|
||||
from app.common.enums import Role
|
||||
from app.database.session import get_db
|
||||
from app.doctors.schemas import AssignToMR
|
||||
from app.doctors.schemas import AssignToMR, RejectMaster
|
||||
from app.users.models import User
|
||||
|
||||
router = APIRouter(prefix="/chemists", tags=["chemists"])
|
||||
@@ -26,9 +26,10 @@ async def list_chemists(
|
||||
async def create_chemist(
|
||||
payload: ChemistCreate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN)),
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.MR)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Chemist:
|
||||
"""An Administrator, or an MR within their own headquarters (AM and GM cannot)."""
|
||||
return await ChemistService(db).create(payload, current_user, client_ip(request))
|
||||
|
||||
|
||||
@@ -60,3 +61,34 @@ async def assign_chemist(
|
||||
"mr_user_id": assignment.mr_user_id,
|
||||
"effective_from": assignment.effective_from,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/requests", response_model=list[ChemistRead])
|
||||
async def list_chemist_requests(
|
||||
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
||||
) -> list[Chemist]:
|
||||
"""Chemists an MR added that wait for the Area Manager: an MR sees their own (with any
|
||||
rejected and why), an Area Manager what waits for them, the administrator and GM all."""
|
||||
return await ChemistService(db).requests_for(current_user)
|
||||
|
||||
|
||||
@router.post("/{chemist_id}/approve", response_model=ChemistRead)
|
||||
async def approve_chemist(
|
||||
chemist_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.AM, Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Chemist:
|
||||
"""The Area Manager of its headquarters adds it to the list, assigned to the MR who asked."""
|
||||
return await ChemistService(db).review(chemist_id, True, None, current_user, client_ip(request))
|
||||
|
||||
|
||||
@router.post("/{chemist_id}/reject", response_model=ChemistRead)
|
||||
async def reject_chemist(
|
||||
chemist_id: uuid.UUID,
|
||||
payload: RejectMaster,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.AM, Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Chemist:
|
||||
return await ChemistService(db).review(chemist_id, False, payload.reason, current_user, client_ip(request))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
@@ -26,3 +27,9 @@ class ChemistRead(BaseModel):
|
||||
shop_address: str
|
||||
contact_number: str | None
|
||||
is_active: bool
|
||||
#: PENDING / APPROVED / REJECTED — see app/common/master_approval.py.
|
||||
approval_status: str
|
||||
rejection_reason: str | None
|
||||
requested_by_name: str | None
|
||||
created_at: datetime
|
||||
|
||||
|
||||
+60
-6
@@ -9,7 +9,8 @@ from app.audit.service import AuditService
|
||||
from app.auth.dependencies import assert_can_view_hq, visible_hq_ids
|
||||
from app.chemists.models import Chemist, ChemistAssignment
|
||||
from app.chemists.schemas import ChemistCreate, ChemistUpdate
|
||||
from app.common.enums import AuditAction, EntityType, Role
|
||||
from app.common import master_approval
|
||||
from app.common.enums import AuditAction, EntityType, MasterApproval, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, NotFoundError, ValidationError
|
||||
from app.common.utils import snapshot
|
||||
from app.users.models import User
|
||||
@@ -25,7 +26,32 @@ class ChemistService:
|
||||
async def create(self, data: ChemistCreate, actor: User, ip: str | None = None) -> Chemist:
|
||||
await assert_can_view_hq(self.db, actor, data.headquarters_id)
|
||||
|
||||
chemist = Chemist(**data.model_dump())
|
||||
values = data.model_dump()
|
||||
# Added by an MR: it waits for the Area Manager. One they were refused is sent again.
|
||||
twin = None
|
||||
if actor.role in FIELD_ROLES:
|
||||
twin = await master_approval.rejected_twin(
|
||||
self.db, Chemist, {k: values[k] for k in ('headquarters_id', 'name', 'shop_address')}
|
||||
)
|
||||
if twin is not None:
|
||||
await master_approval.resubmit(self.db, twin, values, actor, ChemistAssignment, "chemist_id")
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.CHEMIST,
|
||||
entity_id=twin.id,
|
||||
action=AuditAction.SUBMIT,
|
||||
after=snapshot(twin, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(twin)
|
||||
return twin
|
||||
|
||||
chemist = Chemist(
|
||||
**values,
|
||||
approval_status=master_approval.initial_status(actor),
|
||||
requested_by_user_id=actor.id if actor.role in FIELD_ROLES else None,
|
||||
)
|
||||
self.db.add(chemist)
|
||||
try:
|
||||
await self.db.flush()
|
||||
@@ -44,7 +70,22 @@ class ChemistService:
|
||||
after=snapshot(chemist, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
# An MR only sees assigned chemists, so the one they add is assigned to them.
|
||||
if actor.role in FIELD_ROLES:
|
||||
start = date.today()
|
||||
self.db.add(
|
||||
ChemistAssignment(chemist_id=chemist.id, mr_user_id=actor.id, effective_from=start)
|
||||
)
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.CHEMIST,
|
||||
entity_id=chemist.id,
|
||||
action=AuditAction.ASSIGN,
|
||||
after={"mr_user_id": str(actor.id), "effective_from": start.isoformat()},
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(chemist)
|
||||
return chemist
|
||||
|
||||
async def update(
|
||||
@@ -72,7 +113,7 @@ class ChemistService:
|
||||
return chemist
|
||||
|
||||
async def list_for(self, actor: User) -> list[Chemist]:
|
||||
if actor.role == Role.MR:
|
||||
if actor.role in FIELD_ROLES:
|
||||
stmt = (
|
||||
select(Chemist)
|
||||
.join(ChemistAssignment, ChemistAssignment.chemist_id == Chemist.id)
|
||||
@@ -80,13 +121,14 @@ class ChemistService:
|
||||
ChemistAssignment.mr_user_id == actor.id,
|
||||
ChemistAssignment.effective_to.is_(None),
|
||||
Chemist.is_active.is_(True),
|
||||
Chemist.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
.order_by(Chemist.name)
|
||||
)
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
allowed = await visible_hq_ids(self.db, actor)
|
||||
stmt = select(Chemist).order_by(Chemist.name)
|
||||
stmt = select(Chemist).where(Chemist.approval_status == MasterApproval.APPROVED).order_by(Chemist.name)
|
||||
if allowed is not None:
|
||||
stmt = stmt.where(Chemist.headquarters_id.in_(allowed or [None]))
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
@@ -103,13 +145,15 @@ class ChemistService:
|
||||
if chemist is None:
|
||||
raise NotFoundError("Chemist not found")
|
||||
await assert_can_view_hq(self.db, actor, chemist.headquarters_id)
|
||||
if chemist.approval_status != MasterApproval.APPROVED:
|
||||
raise ValidationError("This chemist is still waiting for the Area Manager's approval")
|
||||
|
||||
mr = await self.db.get(User, mr_user_id)
|
||||
if mr is None:
|
||||
raise NotFoundError("MR not found")
|
||||
if mr.role != Role.MR:
|
||||
if mr.role not in FIELD_ROLES:
|
||||
raise ValidationError("Chemists can only be assigned to an MR")
|
||||
if mr.headquarters_id != chemist.headquarters_id:
|
||||
if chemist.headquarters_id not in mr.covered_headquarter_ids:
|
||||
raise ValidationError("MR and chemist belong to different headquarters")
|
||||
|
||||
start = effective_from or date.today()
|
||||
@@ -141,3 +185,13 @@ class ChemistService:
|
||||
)
|
||||
await self.db.commit()
|
||||
return assignment
|
||||
|
||||
async def requests_for(self, actor: User) -> list[Chemist]:
|
||||
return await master_approval.requests_for(self.db, Chemist, actor)
|
||||
|
||||
async def review(
|
||||
self, chemist_id: uuid.UUID, approve: bool, reason: str | None, actor: User, ip: str | None = None
|
||||
) -> Chemist:
|
||||
return await master_approval.review(
|
||||
self.db, Chemist, EntityType.CHEMIST, chemist_id, approve, reason, actor, ip
|
||||
)
|
||||
|
||||
@@ -19,6 +19,13 @@ class Role(StrEnum):
|
||||
GM = "GM"
|
||||
AM = "AM"
|
||||
MR = "MR"
|
||||
#: Senior Field Executive: does an MR's work (daily reports, forms, doctors, chemists) and
|
||||
#: reports to the same Area Manager, but may cover several headquarters.
|
||||
SFE = "SFE"
|
||||
|
||||
|
||||
#: The roles that do field work and file reports; everywhere "an MR" is checked, an SFE counts too.
|
||||
FIELD_ROLES: frozenset[Role] = frozenset({Role.MR, Role.SFE})
|
||||
|
||||
|
||||
class DCRStatus(StrEnum):
|
||||
@@ -34,6 +41,21 @@ class DCRStatus(StrEnum):
|
||||
APPROVED = "APPROVED"
|
||||
|
||||
|
||||
class ReportType(StrEnum):
|
||||
"""An MR files two reports a day, each approved by the Area Manager on its own.
|
||||
|
||||
PLAN — in the morning: which doctors and chemists the MR intends to visit, and when.
|
||||
ACTUAL — at the end of the day: the visits that happened, plus a short note for every
|
||||
planned visit that did not. It can be started only once the plan is approved.
|
||||
SELF — an Area Manager's own end-of-day report of the visits they made. It needs no
|
||||
plan and no approval: submitting it makes it final.
|
||||
"""
|
||||
|
||||
PLAN = "PLAN"
|
||||
ACTUAL = "ACTUAL"
|
||||
SELF = "SELF"
|
||||
|
||||
|
||||
#: Statuses in which the owning MR may still change report data (V9).
|
||||
EDITABLE_STATUSES: frozenset[DCRStatus] = frozenset(
|
||||
{DCRStatus.DRAFT, DCRStatus.RETURNED}
|
||||
@@ -61,6 +83,14 @@ class ConversionStatus(StrEnum):
|
||||
LOST = "LOST"
|
||||
|
||||
|
||||
class MasterApproval(StrEnum):
|
||||
"""A doctor or chemist an MR adds waits for their Area Manager before anyone can use it."""
|
||||
|
||||
PENDING = "PENDING"
|
||||
APPROVED = "APPROVED"
|
||||
REJECTED = "REJECTED"
|
||||
|
||||
|
||||
class AuditAction(StrEnum):
|
||||
CREATE = "CREATE"
|
||||
UPDATE = "UPDATE"
|
||||
@@ -81,3 +111,5 @@ class EntityType(StrEnum):
|
||||
CHEMIST = "chemist"
|
||||
DCR = "dcr"
|
||||
PRODUCT = "product"
|
||||
#: A row of a company form (the table is named in the entry's data).
|
||||
FORM_ROW = "form_row"
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Approval of the doctors and chemists an MR adds.
|
||||
|
||||
MR adds one --> PENDING --approve(AM)--> APPROVED (listed, and visitable by that MR)
|
||||
--reject(AM)---> REJECTED (the MR sees why; adding it again re-sends it)
|
||||
|
||||
Only APPROVED masters are listed or may be visited. The administrator's own additions are
|
||||
APPROVED at once. The reviewer is the Area Manager of the master's headquarters (or the
|
||||
administrator); the General Manager may look but not act.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
from datetime import date
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.dependencies import assert_can_view_hq, visible_hq_ids
|
||||
from app.common.enums import AuditAction, EntityType, MasterApproval, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, ForbiddenError, NotFoundError
|
||||
from app.users.models import User
|
||||
|
||||
|
||||
def initial_status(actor: User) -> str:
|
||||
return MasterApproval.PENDING if actor.role in FIELD_ROLES else MasterApproval.APPROVED
|
||||
|
||||
|
||||
async def rejected_twin(db: AsyncSession, model: Any, match: dict[str, Any]) -> Any | None:
|
||||
"""A rejected record with the same identity: adding it again re-sends that one for approval."""
|
||||
stmt = select(model).where(*(getattr(model, k) == v for k, v in match.items()))
|
||||
found = (await db.execute(stmt)).scalars().first()
|
||||
return found if found is not None and found.approval_status == MasterApproval.REJECTED else None
|
||||
|
||||
|
||||
async def resubmit(
|
||||
db: AsyncSession, record: Any, values: dict[str, Any], actor: User, assignment_model: Any, fk: str
|
||||
) -> None:
|
||||
"""Re-send a rejected record with the MR's new details, assigned to that MR."""
|
||||
for field, value in values.items():
|
||||
setattr(record, field, value)
|
||||
record.approval_status = MasterApproval.PENDING
|
||||
record.requested_by_user_id = actor.id
|
||||
record.rejection_reason = None
|
||||
today = date.today()
|
||||
current = (
|
||||
await db.execute(
|
||||
select(assignment_model).where(
|
||||
getattr(assignment_model, fk) == record.id, assignment_model.effective_to.is_(None)
|
||||
)
|
||||
)
|
||||
).scalars().all()
|
||||
if not any(a.mr_user_id == actor.id for a in current):
|
||||
for row in current:
|
||||
row.effective_to = today
|
||||
await db.flush()
|
||||
db.add(assignment_model(**{fk: record.id, "mr_user_id": actor.id, "effective_from": today}))
|
||||
|
||||
|
||||
async def requests_for(db: AsyncSession, model: Any, actor: User) -> list[Any]:
|
||||
"""An MR: what they asked for that is still waiting or was rejected.
|
||||
An Area Manager: what waits for them. Administrator and GM: everything waiting."""
|
||||
stmt = select(model).order_by(model.created_at.desc())
|
||||
if actor.role in FIELD_ROLES:
|
||||
return list(
|
||||
(
|
||||
await db.execute(
|
||||
stmt.where(
|
||||
model.requested_by_user_id == actor.id,
|
||||
model.approval_status.in_([MasterApproval.PENDING, MasterApproval.REJECTED]),
|
||||
)
|
||||
)
|
||||
).scalars()
|
||||
)
|
||||
stmt = stmt.where(model.approval_status == MasterApproval.PENDING)
|
||||
allowed = await visible_hq_ids(db, actor)
|
||||
if allowed is not None:
|
||||
stmt = stmt.where(model.headquarters_id.in_(allowed or [None]))
|
||||
return list((await db.execute(stmt)).scalars())
|
||||
|
||||
|
||||
async def review(
|
||||
db: AsyncSession,
|
||||
model: Any,
|
||||
entity_type: EntityType,
|
||||
record_id: uuid.UUID,
|
||||
approve: bool,
|
||||
reason: str | None,
|
||||
actor: User,
|
||||
ip: str | None,
|
||||
) -> Any:
|
||||
record = await db.get(model, record_id)
|
||||
if record is None:
|
||||
raise NotFoundError(f"{entity_type.value.capitalize()} not found")
|
||||
if actor.role not in (Role.AM, Role.ADMIN):
|
||||
raise ForbiddenError("Only the Area Manager approves a new doctor or chemist")
|
||||
await assert_can_view_hq(db, actor, record.headquarters_id)
|
||||
target = MasterApproval.APPROVED if approve else MasterApproval.REJECTED
|
||||
if record.approval_status == target:
|
||||
return record # a double click
|
||||
if record.approval_status != MasterApproval.PENDING:
|
||||
raise ConflictError("This request has already been decided", {"status": record.approval_status})
|
||||
record.approval_status = target
|
||||
record.rejection_reason = None if approve else reason
|
||||
await AuditService(db).log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=entity_type,
|
||||
entity_id=record.id,
|
||||
action=AuditAction.APPROVE if approve else AuditAction.RETURN,
|
||||
before={"approval_status": MasterApproval.PENDING.value},
|
||||
after={"approval_status": target.value, **({"reason": reason} if reason else {})},
|
||||
ip_address=ip,
|
||||
)
|
||||
await db.commit()
|
||||
await db.refresh(record)
|
||||
return record
|
||||
@@ -40,6 +40,8 @@ class Settings(BaseSettings):
|
||||
# --- Business rules (see report §21 — confirm with client) ----------
|
||||
# A10 / Q8: how many days back an MR may file a report.
|
||||
BACKDATING_WINDOW_DAYS: int = Field(default=7, ge=0, le=90)
|
||||
# A Daily Call Report (Plan) may be made this many days ahead; Done and Self never.
|
||||
PLAN_AHEAD_DAYS: int = Field(default=2, ge=0, le=30)
|
||||
# A16 / Q9: report for date D must be submitted by end of D + this many days.
|
||||
SUBMISSION_DEADLINE_DAYS: int = Field(default=1, ge=0, le=30)
|
||||
# V8: minimum length of a manager's return reason.
|
||||
|
||||
@@ -14,23 +14,23 @@ from sqlalchemy import select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import active_hq_ids_for_am
|
||||
from app.common.enums import Role
|
||||
from app.common.enums import Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, ForbiddenError, NotFoundError, ValidationError
|
||||
from app.csv_data.models import FormApproval
|
||||
from app.organization.models import AMHeadquarters, Headquarters
|
||||
from app.users.models import User
|
||||
|
||||
APPROVAL_TABLES = {"standard_tour_plan", "tour_plan", "fare_chart"}
|
||||
APPROVAL_TABLES = {"standard_tour_plan", "tour_plan", "fare_chart", "expense_statement"}
|
||||
|
||||
|
||||
async def owners_in_scope(db: AsyncSession, user: User) -> list[User]:
|
||||
"""The MRs whose forms this user may see: themselves, the MRs of their headquarters, or (admin) all."""
|
||||
stmt = select(User).where(User.role == Role.MR, User.is_active.is_(True)).order_by(User.full_name)
|
||||
if user.role == Role.MR:
|
||||
"""The MRs whose forms this user may see: themselves, their headquarters' MRs, or (admin, GM) all."""
|
||||
stmt = select(User).where(User.role.in_(FIELD_ROLES), User.is_active.is_(True)).order_by(User.full_name)
|
||||
if user.role in FIELD_ROLES:
|
||||
stmt = stmt.where(User.id == user.id)
|
||||
elif user.role == Role.AM:
|
||||
stmt = stmt.where(User.headquarters_id.in_(await active_hq_ids_for_am(db, user.id)))
|
||||
elif user.role != Role.ADMIN:
|
||||
elif user.role not in (Role.ADMIN, Role.GM):
|
||||
return []
|
||||
return list((await db.execute(stmt)).scalars())
|
||||
|
||||
@@ -49,7 +49,7 @@ async def assert_owner_in_scope(db: AsyncSession, user: User, owner_id: uuid.UUI
|
||||
for owner in await owners_in_scope(db, user):
|
||||
if owner.id == owner_id:
|
||||
return owner
|
||||
if user.role == Role.ADMIN or (await db.get(User, owner_id)) is None:
|
||||
if user.role in (Role.ADMIN, Role.GM) or (await db.get(User, owner_id)) is None:
|
||||
raise NotFoundError("Medical representative not found", {"id": str(owner_id)})
|
||||
raise ForbiddenError("This form belongs to a medical representative outside your headquarters")
|
||||
|
||||
@@ -80,7 +80,7 @@ async def list_approvals(db: AsyncSession, user: User) -> list[tuple[FormApprova
|
||||
for r in (await db.execute(select(FormApproval).where(FormApproval.owner_user_id.in_(owners)))).scalars()
|
||||
}
|
||||
out: list[tuple[FormApproval, User, str | None]] = []
|
||||
if user.role == Role.MR:
|
||||
if user.role in FIELD_ROLES:
|
||||
for name in sorted(APPROVAL_TABLES):
|
||||
out.append((existing.get((name, user.id)) or FormApproval(table_name=name, owner_user_id=user.id, status="draft"), user, hq_names.get(user.headquarters_id)))
|
||||
return out
|
||||
|
||||
+19
-2
@@ -1,7 +1,7 @@
|
||||
"""Allow-listed imported CSV tables and their database columns."""
|
||||
|
||||
TABLES: dict[str, tuple[str, ...]] = {
|
||||
"sales_projection": ("name", "headquarter", "report_date", "month", "serial_no", "product_name", "pts", "secondary_qty", "value", "closing", "secondary_value", "aug_sales", "value_2"),
|
||||
"sales_projection": ("name", "headquarter", "report_date", "month", "serial_no", "product_name", "pts", "closing_qty", "closing_value", "projection_qty", "projection_value"),
|
||||
"weekly_report": ("employee_name", "report_date", "headquarter", "month", "no_of_days_worked", "no_of_doctor_called", "no_of_chemist_called", "pob_booked", "secondary_sales", "pob_objective", "secondary_objective", "product_offer_scheme_booked", "product_offer_scheme_objective"),
|
||||
"field_activity_report": ("name", "headquarter", "month", "report_date", "station_worked", "no_of_doctors_called", "no_of_chemists_pds_called", "doctor_call_avg", "chemist_call_avg"),
|
||||
"fare_chart": ("name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "kilometers_km"),
|
||||
@@ -11,17 +11,34 @@ TABLES: dict[str, tuple[str, ...]] = {
|
||||
"sales_and_stock_statement": ("name", "headquarter", "report_date", "month", "serial_no", "product_name", "opening", "receipt", "sale", "closing", "value_in_pts"),
|
||||
"tour_plan": ("name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "pob", "stockiest_visit"),
|
||||
"standard_tour_plan": ("name", "headquarter", "serial_no", "place"),
|
||||
"expense_statement": ("name", "designation", "headquarter", "month", "serial_no", "report_date", "from_location", "to_location", "fare", "da_hq", "da_ex", "da_out", "postage", "other_expense", "total", "business_achieved"),
|
||||
}
|
||||
|
||||
#: The order the forms are listed in, on screen and in the API.
|
||||
FORM_ORDER = (
|
||||
"standard_tour_plan",
|
||||
"tour_plan",
|
||||
"fare_chart",
|
||||
"sales_and_stock_statement",
|
||||
"sales_projection",
|
||||
"field_activity_report",
|
||||
"expense_statement",
|
||||
"area_covered",
|
||||
"weekly_report",
|
||||
"core_dr_call_conversion",
|
||||
"daily_call_report_csv",
|
||||
)
|
||||
|
||||
CSV_LABELS = {
|
||||
"weekly_report": "Weekly Report",
|
||||
"field_activity_report": "Field Activity Report",
|
||||
"fare_chart": "Fare Chart",
|
||||
"daily_call_report_csv": "Daily Call Report CSV",
|
||||
"core_dr_call_conversion": "Core Doctor Call Conversion",
|
||||
"area_covered": "Area Covered",
|
||||
"area_covered": "Area Coverage",
|
||||
"sales_and_stock_statement": "Sales and Stock Statement",
|
||||
"tour_plan": "Tour Plan",
|
||||
"standard_tour_plan": "Standard Tour Plan",
|
||||
"sales_projection": "Sales Projection",
|
||||
"expense_statement": "Expense Statement",
|
||||
}
|
||||
+263
-23
@@ -3,17 +3,18 @@ import uuid
|
||||
from collections.abc import Iterable
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Query, status
|
||||
from fastapi import APIRouter, Body, Depends, Query, Request, status
|
||||
from fastapi.responses import Response
|
||||
from sqlalchemy import select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import require_role
|
||||
from app.common.enums import Role
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.dependencies import client_ip, require_role
|
||||
from app.common.enums import AuditAction, EntityType, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, ForbiddenError, NotFoundError, ValidationError
|
||||
from app.config.settings import settings
|
||||
from app.csv_data import approval as approvals
|
||||
from app.csv_data.config import CSV_LABELS, TABLES
|
||||
from app.csv_data.config import CSV_LABELS, FORM_ORDER, TABLES
|
||||
from app.database.session import get_db
|
||||
from app.users.models import User
|
||||
from app.organization.models import Headquarters
|
||||
@@ -62,7 +63,11 @@ async def _reject_repeat(
|
||||
#: Tables whose printed Sl. No. starts over at 1 for each month rather than running on
|
||||
#: forever — a shared master list (e.g. Sales Projection's product list) refilled every
|
||||
#: month, not one MR's own running form.
|
||||
MONTH_NUMBERED_TABLES = {"sales_projection"}
|
||||
MONTH_NUMBERED_TABLES = {"sales_projection", "sales_and_stock_statement"}
|
||||
#: Forms whose rows are the company's product list, shared by every MR. An MR may delete an extra
|
||||
#: copy of a product (a row added by mistake), but never a product's only row — that would take
|
||||
#: the product off everyone's list; they clear its figures instead (a PATCH). The admin may delete any.
|
||||
PRODUCT_LIST_TABLES = {"sales_projection", "sales_and_stock_statement"}
|
||||
|
||||
|
||||
async def _renumber_rows(table_name: str, db: AsyncSession) -> None:
|
||||
@@ -98,7 +103,7 @@ async def _apply_mr_identity(
|
||||
values: dict[str, str | None], subject: User | None, db: AsyncSession
|
||||
) -> dict[str, str | None]:
|
||||
"""Fill NAME / HEADQUARTER from the MR the row is for (the signed-in MR, or the one an admin picked)."""
|
||||
if subject is None or subject.role != Role.MR:
|
||||
if subject is None or subject.role not in FIELD_ROLES:
|
||||
return values
|
||||
headquarters_name = None
|
||||
if subject.headquarters_id:
|
||||
@@ -120,7 +125,7 @@ async def _present_rows_for_user(
|
||||
Admins must see the source values and every row. MRs use the imported rows as
|
||||
their working data, but identity columns must always reflect the logged-in MR.
|
||||
"""
|
||||
if current_user.role != Role.MR:
|
||||
if current_user.role not in FIELD_ROLES:
|
||||
return rows
|
||||
|
||||
headquarters_name = None
|
||||
@@ -168,6 +173,25 @@ async def _apply_sales_projection_product(
|
||||
return values
|
||||
|
||||
|
||||
#: Expense Statement: the TOTAL column is always fare + daily allowance + postage + other expenses.
|
||||
EXPENSE_AMOUNTS = ("fare", "da_hq", "da_ex", "da_out", "postage", "other_expense")
|
||||
|
||||
|
||||
def _apply_expense_total(values: dict[str, str | None]) -> dict[str, str | None]:
|
||||
total = 0.0
|
||||
for column in EXPENSE_AMOUNTS:
|
||||
raw = (values.get(column) or "").replace(",", "").strip()
|
||||
if not raw:
|
||||
continue
|
||||
try:
|
||||
total += float(raw)
|
||||
except ValueError:
|
||||
raise ValidationError("Expense amounts must be numbers", {"column": column}) from None
|
||||
has_amount = any((values.get(column) or "").strip() for column in EXPENSE_AMOUNTS)
|
||||
values["total"] = f"{total:.2f}" if has_amount else None
|
||||
return values
|
||||
|
||||
|
||||
def _select(table_name: str, columns: Iterable[str]) -> str:
|
||||
selected = ", ".join(f'"{column}"' for column in columns)
|
||||
return f'SELECT id, source_row_number, {selected} FROM "{table_name}"'
|
||||
@@ -180,7 +204,7 @@ async def _owner_condition(
|
||||
an Area Manager only the MRs of their headquarters, an admin anyone (or one chosen MR)."""
|
||||
if table_name not in OWNED_TABLES:
|
||||
return [], {}
|
||||
if current_user.role == Role.MR:
|
||||
if current_user.role in FIELD_ROLES:
|
||||
if owner_id not in (None, current_user.id):
|
||||
raise ForbiddenError("You can only see your own forms")
|
||||
return ["owner_user_id = :owner"], {"owner": current_user.id}
|
||||
@@ -194,13 +218,18 @@ async def _owner_condition(
|
||||
|
||||
async def _row_owner(table_name: str, row_id: uuid.UUID, current_user: User, db: AsyncSession) -> uuid.UUID | None:
|
||||
"""Who owns this row, after checking `current_user` is allowed to touch it."""
|
||||
if table_name not in OWNED_TABLES:
|
||||
if table_name not in OWNED_TABLES and table_name not in PER_MR_PRODUCT_TABLES:
|
||||
return None
|
||||
found = (await db.execute(text(f'SELECT owner_user_id FROM "{table_name}" WHERE id = :id'), {"id": row_id})).first()
|
||||
if found is None:
|
||||
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
||||
owner_id = found[0]
|
||||
if current_user.role == Role.MR and owner_id != current_user.id:
|
||||
if table_name in PER_MR_PRODUCT_TABLES:
|
||||
# A shared product line has no owner; only an MR's own row is theirs alone.
|
||||
if current_user.role in FIELD_ROLES and owner_id not in (None, current_user.id):
|
||||
raise ForbiddenError("This row belongs to another medical representative")
|
||||
return owner_id
|
||||
if current_user.role in FIELD_ROLES and owner_id != current_user.id:
|
||||
raise ForbiddenError("This row belongs to another medical representative")
|
||||
if current_user.role == Role.AM:
|
||||
if owner_id is None:
|
||||
@@ -221,6 +250,8 @@ async def _load_rows(
|
||||
table_name: str, db: AsyncSession, current_user: User, owner_id: uuid.UUID | None = None, month: str | None = None
|
||||
) -> list[dict[str, object]]:
|
||||
columns = _table(table_name)
|
||||
if table_name in PER_MR_PRODUCT_TABLES:
|
||||
return await _load_per_mr_rows(table_name, db, current_user, owner_id, month)
|
||||
conditions, params = await _owner_condition(table_name, current_user, db, owner_id)
|
||||
more, extra = _month_filter(table_name, month)
|
||||
where = f" WHERE {' AND '.join(conditions + more)}" if conditions or more else ""
|
||||
@@ -228,8 +259,116 @@ async def _load_rows(
|
||||
return await _present_rows_for_user([dict(row) for row in result.mappings().all()], current_user, db)
|
||||
|
||||
|
||||
#: Product-list forms where every MR has their own figures. Rows with no owner are the shared
|
||||
#: product list: an MR sees each product they have not started yet as a blank line, and their
|
||||
#: first entry on it becomes their own row. A manager sees the MRs' rows, with the MR's name
|
||||
#: and headquarters taken from the owner.
|
||||
PER_MR_PRODUCT_TABLES = {"sales_projection", "sales_and_stock_statement"}
|
||||
#: What an MR types in (and the month it belongs to): a product line with none of these is blank.
|
||||
_FIGURES = {
|
||||
"sales_projection": ("report_date", "month", "closing_qty", "closing_value", "projection_qty", "projection_value"),
|
||||
"sales_and_stock_statement": ("report_date", "month", "opening", "receipt", "sale", "closing", "value_in_pts"),
|
||||
}
|
||||
|
||||
|
||||
def _product_key(row: dict[str, object]) -> str:
|
||||
return str(row.get("product_name") or "").strip().upper()
|
||||
|
||||
|
||||
def _has_figures(table_name: str, row: dict[str, object]) -> bool:
|
||||
return any(str(row.get(c) or "").strip() for c in _FIGURES[table_name])
|
||||
|
||||
|
||||
async def _view_owner(
|
||||
table_name: str, db: AsyncSession, current_user: User, owner_id: uuid.UUID | None
|
||||
) -> uuid.UUID | None:
|
||||
"""The one MR whose copy is shown: the signed-in MR, or the one a manager picked."""
|
||||
if current_user.role in FIELD_ROLES:
|
||||
return current_user.id
|
||||
if owner_id is None:
|
||||
return None
|
||||
await approvals.assert_owner_in_scope(db, current_user, owner_id)
|
||||
return owner_id
|
||||
|
||||
|
||||
async def _load_per_mr_rows(
|
||||
table_name: str, db: AsyncSession, current_user: User, owner_id: uuid.UUID | None, month: str | None
|
||||
) -> list[dict[str, object]]:
|
||||
columns = _table(table_name)
|
||||
selected = ", ".join(f'"{c}"' for c in columns)
|
||||
result = await db.execute(
|
||||
text(f'SELECT id, source_row_number, owner_user_id, {selected} FROM "{table_name}" ORDER BY source_row_number')
|
||||
)
|
||||
everything = [dict(r) for r in result.mappings().all()]
|
||||
mine = await _view_owner(table_name, db, current_user, owner_id)
|
||||
owner_ids = {r["owner_user_id"] for r in everything if r["owner_user_id"]} | ({mine} if mine else set())
|
||||
people: dict[uuid.UUID, tuple[str, str | None]] = {}
|
||||
if owner_ids:
|
||||
found = await db.execute(
|
||||
select(User.id, User.full_name, Headquarters.name)
|
||||
.outerjoin(Headquarters, Headquarters.id == User.headquarters_id)
|
||||
.where(User.id.in_(owner_ids))
|
||||
)
|
||||
people = {uid: (name, hq) for uid, name, hq in found.all()}
|
||||
|
||||
shown: list[dict[str, object]] = []
|
||||
if mine is not None:
|
||||
own = [r for r in everything if r["owner_user_id"] == mine]
|
||||
started = {_product_key(r) for r in own}
|
||||
for r in everything:
|
||||
if r["owner_user_id"] is None and _product_key(r) not in started:
|
||||
shown.append({**r, **{c: None for c in _FIGURES[table_name]}}) # a blank line of the product list
|
||||
shown += own
|
||||
else:
|
||||
# A manager gets every MR's rows plus the shared product list (the page lays the list out).
|
||||
shown = everything
|
||||
# A row an MR starts on a product keeps that product's place in the list, whenever it was
|
||||
# created; a manager reading several MRs sees them one MR after another.
|
||||
place: dict[str, int] = {}
|
||||
for r in everything:
|
||||
if r["owner_user_id"] is None:
|
||||
place[_product_key(r)] = min(place.get(_product_key(r), r["source_row_number"]), r["source_row_number"])
|
||||
def order(r: dict[str, object]) -> tuple:
|
||||
who = people.get(r["owner_user_id"], ("", None))[0] if mine is None else ""
|
||||
return (who, place.get(_product_key(r), r["source_row_number"]), r["source_row_number"])
|
||||
shown.sort(key=order)
|
||||
|
||||
wanted = (month or "").strip().lower()
|
||||
out: list[dict[str, object]] = []
|
||||
for r in shown:
|
||||
if wanted and str(r.get("month") or "").strip().lower() != wanted:
|
||||
continue
|
||||
item = {k: v for k, v in r.items() if k != "owner_user_id"}
|
||||
who = r["owner_user_id"] or mine # a blank product line is shown as that MR's own
|
||||
if who in people:
|
||||
item["name"], item["headquarter"] = people[who][0], people[who][1] or ""
|
||||
out.append(item)
|
||||
return await _present_rows_for_user(out, current_user, db)
|
||||
|
||||
|
||||
def _month_of_every_product(rows: list[dict[str, object]], month: str, columns: Iterable[str]) -> list[dict[str, object]]:
|
||||
"""A product-list form for one month: every product once — its row for that month, else its
|
||||
still-unused blank row, else an empty line carrying only the product — so the printed form
|
||||
always lists the whole range, filled in where this month has figures."""
|
||||
wanted = month.strip().lower()
|
||||
key = lambda r: str(r.get("product_name") or "").strip().upper() # noqa: E731
|
||||
copies: dict[str, list[dict[str, object]]] = {}
|
||||
for row in rows:
|
||||
if key(row):
|
||||
copies.setdefault(key(row), []).append(row)
|
||||
out: list[dict[str, object]] = []
|
||||
for product, group in copies.items():
|
||||
mine = next((r for r in group if str(r.get("month") or "").strip().lower() == wanted), None) or next(
|
||||
(r for r in group if not str(r.get("month") or "").strip()), None
|
||||
)
|
||||
if mine is None:
|
||||
mine = {c: group[0].get(c) for c in ("product_name", "pts", "value_in_pts", "name", "headquarter") if c in set(columns)}
|
||||
out.append({**mine, "month": month.strip()})
|
||||
return out
|
||||
|
||||
|
||||
def _available_csv_tables(current_user: User) -> list[str]:
|
||||
visible = [name for name in TABLES if name not in HIDDEN_TABLES]
|
||||
visible = [name for name in FORM_ORDER if name not in HIDDEN_TABLES]
|
||||
if current_user.role == Role.AM:
|
||||
return [name for name in visible if name in APPROVAL_TABLES]
|
||||
return visible
|
||||
@@ -243,7 +382,7 @@ def _ensure_table_access(table_name: str, current_user: User) -> None:
|
||||
|
||||
|
||||
@router.get("/tables")
|
||||
async def list_csv_tables(current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM))) -> list[dict[str, object]]:
|
||||
async def list_csv_tables(current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM))) -> list[dict[str, object]]:
|
||||
return [{"name": name, "label": CSV_LABELS[name], "columns": list(TABLES[name])} for name in _available_csv_tables(current_user)]
|
||||
|
||||
|
||||
@@ -288,7 +427,7 @@ def _approval_table(table_name: str) -> str:
|
||||
@router.get("/approvals")
|
||||
async def list_form_approvals(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> list[dict[str, object]]:
|
||||
"""Approval state of the forms that carry an Area Manager signature, limited to the MRs this user may see."""
|
||||
return [_approval_out(r, owner, hq) for r, owner, hq in await approvals.list_approvals(db, current_user)]
|
||||
@@ -297,7 +436,7 @@ async def list_form_approvals(
|
||||
@router.get("/owners")
|
||||
async def list_form_owners(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> list[dict[str, object]]:
|
||||
"""The medical representatives whose forms this user may open."""
|
||||
hq = {h.id: h.name for h in (await db.execute(select(Headquarters))).scalars()}
|
||||
@@ -336,7 +475,7 @@ async def list_csv_rows(
|
||||
table_name: str,
|
||||
owner_id: uuid.UUID | None = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> list[dict[str, object]]:
|
||||
_ensure_table_access(table_name, current_user)
|
||||
return await _load_rows(table_name, db, current_user, owner_id)
|
||||
@@ -354,15 +493,19 @@ async def create_csv_row(
|
||||
values = _clean_payload(table_name, payload)
|
||||
owner: User | None = None
|
||||
if table_name in OWNED_TABLES:
|
||||
if current_user.role == Role.MR:
|
||||
if current_user.role in FIELD_ROLES:
|
||||
owner = current_user
|
||||
elif owner_id is None:
|
||||
raise ValidationError("Choose which medical representative this row is for")
|
||||
else:
|
||||
owner = await approvals.assert_owner_in_scope(db, current_user, owner_id)
|
||||
if owner is None and table_name in PER_MR_PRODUCT_TABLES and current_user.role in FIELD_ROLES:
|
||||
owner = current_user
|
||||
values = await _apply_mr_identity(values, owner or current_user, db)
|
||||
if table_name == "sales_projection":
|
||||
values = await _apply_sales_projection_product(values, db, current_user)
|
||||
if table_name == "expense_statement":
|
||||
values = _apply_expense_total(values)
|
||||
if not _has_user_data(table_name, values):
|
||||
raise ValidationError("Add at least one value before creating a row")
|
||||
await _reject_repeat(table_name, values, db, owner.id if owner else None)
|
||||
@@ -384,17 +527,65 @@ async def create_csv_row(
|
||||
return await _get_row(table_name, row_id, db)
|
||||
|
||||
|
||||
async def _stored_owner(table_name: str, row_id: uuid.UUID, db: AsyncSession) -> uuid.UUID | None:
|
||||
found = (await db.execute(text(f'SELECT owner_user_id FROM "{table_name}" WHERE id = :id'), {"id": row_id})).first()
|
||||
if found is None:
|
||||
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
||||
return found[0]
|
||||
|
||||
|
||||
async def _start_own_row(
|
||||
table_name: str, shared: dict[str, object], values: dict[str, str | None],
|
||||
payload: dict[str, object], db: AsyncSession, current_user: User,
|
||||
) -> dict[str, object]:
|
||||
"""An MR's first entry on a product of the shared list: it becomes their own row, so the
|
||||
shared line stays blank for everyone else."""
|
||||
columns = _table(table_name)
|
||||
new = {c: None for c in columns}
|
||||
for c in ("product_name", "pts"):
|
||||
if c in new:
|
||||
new[c] = shared.get(c)
|
||||
new.update({c: v for c, v in values.items() if c in payload and c not in SERIAL_COLUMNS})
|
||||
new = await _apply_mr_identity(new, current_user, db)
|
||||
if not _has_user_data(table_name, new):
|
||||
raise ValidationError("A row must contain at least one value")
|
||||
next_row = (await db.execute(text(f'SELECT COALESCE(MAX(source_row_number), 0) + 1 FROM "{table_name}"'))).scalar_one()
|
||||
for c in columns:
|
||||
if c in SERIAL_COLUMNS:
|
||||
new[c] = str(next_row)
|
||||
names = ", ".join(["id", "source_row_number", "owner_user_id", *[f'"{c}"' for c in columns]])
|
||||
params = ", ".join([":id", ":source_row_number", ":owner_user_id", *[f":{c}" for c in columns]])
|
||||
row_id = uuid.uuid4()
|
||||
await db.execute(
|
||||
text(f'INSERT INTO "{table_name}" ({names}) VALUES ({params})'),
|
||||
{**new, "id": row_id, "source_row_number": next_row, "owner_user_id": current_user.id},
|
||||
)
|
||||
await _renumber_rows(table_name, db)
|
||||
await db.commit()
|
||||
return await _get_row(table_name, row_id, db)
|
||||
|
||||
|
||||
@router.patch("/{table_name}/rows/{row_id}")
|
||||
async def update_csv_row(table_name: str, row_id: uuid.UUID, payload: dict[str, object] = Body(...), db: AsyncSession = Depends(get_db), current_user: User = Depends(require_role(Role.MR, Role.ADMIN))) -> dict[str, object]:
|
||||
values = _clean_payload(table_name, payload)
|
||||
if table_name in PER_MR_PRODUCT_TABLES and current_user.role in FIELD_ROLES:
|
||||
shared = await _get_row(table_name, row_id, db)
|
||||
if await _stored_owner(table_name, row_id, db) is None:
|
||||
return await _start_own_row(table_name, shared, values, payload, db, current_user)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
values = await _apply_mr_identity(values, current_user, db)
|
||||
if table_name == "sales_projection":
|
||||
values = await _apply_sales_projection_product(values, db, current_user)
|
||||
changes = {column: value for column, value in values.items() if column in payload}
|
||||
stored = await _get_row(table_name, row_id, db)
|
||||
if table_name == "expense_statement" and any(column in payload for column in (*EXPENSE_AMOUNTS, "total")):
|
||||
merged = {c: (changes[c] if c in changes else stored.get(c)) for c in EXPENSE_AMOUNTS}
|
||||
changes["total"] = _apply_expense_total(merged)["total"]
|
||||
if not changes:
|
||||
raise ValidationError("At least one CSV column is required")
|
||||
if not _has_user_data(table_name, {**values, **changes}):
|
||||
# Judged on the whole row as it will be stored, not only the columns sent: clearing
|
||||
# one cell of a row that still has other values is a normal edit.
|
||||
if not _has_user_data(table_name, {**{c: stored.get(c) for c in _table(table_name)}, **changes}):
|
||||
raise ValidationError("A row must contain at least one value")
|
||||
changes = {column: value for column, value in changes.items() if column not in SERIAL_COLUMNS}
|
||||
if not changes:
|
||||
@@ -409,12 +600,56 @@ async def update_csv_row(table_name: str, row_id: uuid.UUID, payload: dict[str,
|
||||
|
||||
|
||||
@router.delete("/{table_name}/rows/{row_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_csv_row(table_name: str, row_id: uuid.UUID, db: AsyncSession = Depends(get_db), current_user: User = Depends(require_role(Role.ADMIN))) -> None:
|
||||
async def delete_csv_row(
|
||||
table_name: str,
|
||||
row_id: uuid.UUID,
|
||||
request: Request,
|
||||
confirm: str | None = Query(default=None, description="The product's name, to remove a product's last row"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN)),
|
||||
) -> None:
|
||||
"""Remove a row entered by mistake. An MR may remove only their own rows (checked by _row_owner).
|
||||
|
||||
On a shared product list, a product's last row takes the product off everyone's list: an MR
|
||||
may not remove it at all, and the administrator must name the product in `confirm`. Every
|
||||
removal is written to the audit log with the whole row, so it can be put back by hand."""
|
||||
_table(table_name)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
row = await _get_row(table_name, row_id, db)
|
||||
if table_name in PRODUCT_LIST_TABLES:
|
||||
product = str(row.get("product_name") or "").strip()
|
||||
copies = (
|
||||
await db.execute(
|
||||
text(
|
||||
f'SELECT COUNT(*) FROM "{table_name}" '
|
||||
"WHERE upper(btrim(COALESCE(product_name, ''))) = upper(:product)"
|
||||
+ (" AND owner_user_id IS NOT DISTINCT FROM :owner" if table_name in PER_MR_PRODUCT_TABLES else "")
|
||||
),
|
||||
{"product": product, **({"owner": row_owner} if table_name in PER_MR_PRODUCT_TABLES else {})},
|
||||
)
|
||||
).scalar_one()
|
||||
if copies < 2:
|
||||
if current_user.role in FIELD_ROLES:
|
||||
raise ForbiddenError(
|
||||
"This is the product's only row, so it stays in the list. Clear its figures instead.",
|
||||
{"table": table_name},
|
||||
)
|
||||
if (confirm or "").strip().upper() != product.upper():
|
||||
raise ValidationError(
|
||||
"Type the product's name to confirm removing it from everyone's list",
|
||||
{"product_name": product},
|
||||
)
|
||||
result = await db.execute(text(f'DELETE FROM "{table_name}" WHERE id = :id'), {"id": row_id})
|
||||
if result.rowcount == 0:
|
||||
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
||||
await AuditService(db).log(
|
||||
actor_user_id=current_user.id,
|
||||
entity_type=EntityType.FORM_ROW,
|
||||
entity_id=row_id,
|
||||
action=AuditAction.DELETE,
|
||||
before={"table": table_name, **{k: (str(v) if isinstance(v, uuid.UUID) else v) for k, v in row.items()}},
|
||||
ip_address=client_ip(request),
|
||||
)
|
||||
await _renumber_rows(table_name, db)
|
||||
await approvals.invalidate(db, table_name, row_owner)
|
||||
await db.commit()
|
||||
@@ -454,7 +689,7 @@ async def download_csv_row_pdf(
|
||||
table_name: str,
|
||||
row_id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> Response:
|
||||
_ensure_table_access(table_name, current_user)
|
||||
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
||||
@@ -474,23 +709,28 @@ async def download_csv_table_pdfs(
|
||||
owner_id: uuid.UUID | None = None,
|
||||
month: str | None = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.ADMIN, Role.AM, Role.GM)),
|
||||
) -> Response:
|
||||
"""The whole form, or only one month of it (for the forms that have a month)."""
|
||||
_ensure_table_access(table_name, current_user)
|
||||
columns = _table(table_name)
|
||||
rows = await _load_rows(table_name, db, current_user, owner_id, month)
|
||||
whole_range = table_name in PRODUCT_LIST_TABLES and bool(month and month.strip())
|
||||
if table_name in PER_MR_PRODUCT_TABLES and await _view_owner(table_name, db, current_user, owner_id) is None:
|
||||
whole_range = False # several MRs together: print the rows that exist, not one line per product
|
||||
rows = await _load_rows(table_name, db, current_user, owner_id, None if whole_range else month)
|
||||
if whole_range and month:
|
||||
rows = _month_of_every_product(rows, month, columns)
|
||||
if month and month.strip() and not rows:
|
||||
raise NotFoundError(f"There are no rows for {month.strip()} in this form", {"month": month})
|
||||
# The signature is that of the single MR whose rows these are; a PDF of several MRs is unsigned.
|
||||
subject = current_user.id if current_user.role == Role.MR else owner_id
|
||||
subject = current_user.id if current_user.role in FIELD_ROLES else owner_id
|
||||
approval_status, am_name, approved_at = await _signature(table_name, subject, db)
|
||||
from app.pdf.service import csv_table_pdf_bytes
|
||||
suffix = re.sub(r"[^a-z]", "", month.lower()) if month else ""
|
||||
filename = f"{table_name}-{suffix}.pdf" if suffix else f"{table_name}.pdf"
|
||||
# Printing every month together: a month-numbered table's stored Sl. No. (1, 2… 1, 2…)
|
||||
# would repeat, so it is recomputed as one running count over what is actually printed.
|
||||
renumber = table_name in MONTH_NUMBERED_TABLES and not (month and month.strip())
|
||||
renumber = table_name in MONTH_NUMBERED_TABLES and (not (month and month.strip()) or whole_range)
|
||||
pdf = csv_table_pdf_bytes(
|
||||
table_name,
|
||||
CSV_LABELS[table_name],
|
||||
|
||||
@@ -13,8 +13,8 @@ from app.database.base import Base
|
||||
from app.doctors.models import Doctor, DoctorAssignment
|
||||
from app.organization.models import AMHeadquarters, Company, Headquarters
|
||||
from app.products.models import Product
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall
|
||||
from app.users.models import User
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall, MissedVisit
|
||||
from app.users.models import User, UserExtraHeadquarters # noqa: F401
|
||||
|
||||
__all__ = [
|
||||
"Base",
|
||||
@@ -31,6 +31,7 @@ __all__ = [
|
||||
"ChemistCall",
|
||||
"DailyCallReport",
|
||||
"DoctorCall",
|
||||
"MissedVisit",
|
||||
"User",
|
||||
"Product",
|
||||
]
|
||||
|
||||
@@ -15,6 +15,7 @@ from sqlalchemy import Boolean, Date, ForeignKey, Index, String, UniqueConstrain
|
||||
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.common.enums import MasterApproval
|
||||
from app.database.base import Base, TimestampMixin, UUIDMixin
|
||||
|
||||
|
||||
@@ -35,9 +36,26 @@ class Doctor(Base, UUIDMixin, TimestampMixin):
|
||||
qualification: Mapped[str | None] = mapped_column(String(100))
|
||||
clinic_address: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
contact_number: Mapped[str | None] = mapped_column(String(20))
|
||||
date_of_birth: Mapped[date | None] = mapped_column(Date)
|
||||
#: Core doctors are the high-value targets tracked for conversion.
|
||||
is_core_doctor: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
#: PENDING until the Area Manager approves one an MR added; only APPROVED ones are listed or visited.
|
||||
approval_status: Mapped[str] = mapped_column(
|
||||
String(10), default=MasterApproval.APPROVED.value, server_default=MasterApproval.APPROVED.value, nullable=False
|
||||
)
|
||||
#: The MR who asked for it (None for one the administrator created).
|
||||
requested_by_user_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="SET NULL")
|
||||
)
|
||||
rejection_reason: Mapped[str | None] = mapped_column(String(500))
|
||||
requested_by: Mapped["User | None"] = relationship( # noqa: F821
|
||||
lazy="selectin", foreign_keys=[requested_by_user_id]
|
||||
)
|
||||
|
||||
@property
|
||||
def requested_by_name(self) -> str | None:
|
||||
return self.requested_by.full_name if self.requested_by else None
|
||||
|
||||
assignments: Mapped[list["DoctorAssignment"]] = relationship(back_populates="doctor")
|
||||
|
||||
|
||||
+34
-3
@@ -7,7 +7,7 @@ from app.auth.dependencies import client_ip, get_current_user, require_role
|
||||
from app.common.enums import Role
|
||||
from app.database.session import get_db
|
||||
from app.doctors.models import Doctor
|
||||
from app.doctors.schemas import AssignToMR, DoctorCreate, DoctorRead, DoctorUpdate
|
||||
from app.doctors.schemas import AssignToMR, DoctorCreate, DoctorRead, DoctorUpdate, RejectMaster
|
||||
from app.doctors.service import DoctorService
|
||||
from app.users.models import User
|
||||
|
||||
@@ -26,10 +26,10 @@ async def list_doctors(
|
||||
async def create_doctor(
|
||||
payload: DoctorCreate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN)),
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.MR)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Doctor:
|
||||
"""Only an Administrator can create a doctor (MR, AM and GM cannot)."""
|
||||
"""An Administrator, or an MR within their own headquarters (AM and GM cannot)."""
|
||||
return await DoctorService(db).create(payload, current_user, client_ip(request))
|
||||
|
||||
|
||||
@@ -62,3 +62,34 @@ async def assign_doctor(
|
||||
"mr_user_id": assignment.mr_user_id,
|
||||
"effective_from": assignment.effective_from,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/requests", response_model=list[DoctorRead])
|
||||
async def list_doctor_requests(
|
||||
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
||||
) -> list[Doctor]:
|
||||
"""Doctors an MR added that wait for the Area Manager: an MR sees their own (with any
|
||||
rejected and why), an Area Manager what waits for them, the administrator and GM all."""
|
||||
return await DoctorService(db).requests_for(current_user)
|
||||
|
||||
|
||||
@router.post("/{doctor_id}/approve", response_model=DoctorRead)
|
||||
async def approve_doctor(
|
||||
doctor_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.AM, Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Doctor:
|
||||
"""The Area Manager of its headquarters adds it to the list, assigned to the MR who asked."""
|
||||
return await DoctorService(db).review(doctor_id, True, None, current_user, client_ip(request))
|
||||
|
||||
|
||||
@router.post("/{doctor_id}/reject", response_model=DoctorRead)
|
||||
async def reject_doctor(
|
||||
doctor_id: uuid.UUID,
|
||||
payload: RejectMaster,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.AM, Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Doctor:
|
||||
return await DoctorService(db).review(doctor_id, False, payload.reason, current_user, client_ip(request))
|
||||
|
||||
+26
-2
@@ -1,7 +1,13 @@
|
||||
import uuid
|
||||
from datetime import date
|
||||
from datetime import date, datetime
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
|
||||
def _not_in_future(value: date | None) -> date | None:
|
||||
if value is not None and value > date.today():
|
||||
raise ValueError("date_of_birth cannot be in the future")
|
||||
return value
|
||||
|
||||
|
||||
class DoctorCreate(BaseModel):
|
||||
@@ -11,8 +17,11 @@ class DoctorCreate(BaseModel):
|
||||
speciality: str | None = Field(default=None, max_length=100)
|
||||
qualification: str | None = Field(default=None, max_length=100)
|
||||
contact_number: str | None = Field(default=None, max_length=20)
|
||||
date_of_birth: date | None = None
|
||||
is_core_doctor: bool = False
|
||||
|
||||
_check_dob = field_validator("date_of_birth")(_not_in_future)
|
||||
|
||||
|
||||
class DoctorUpdate(BaseModel):
|
||||
name: str | None = Field(default=None, min_length=2, max_length=200)
|
||||
@@ -20,9 +29,12 @@ class DoctorUpdate(BaseModel):
|
||||
speciality: str | None = Field(default=None, max_length=100)
|
||||
qualification: str | None = Field(default=None, max_length=100)
|
||||
contact_number: str | None = Field(default=None, max_length=20)
|
||||
date_of_birth: date | None = None
|
||||
is_core_doctor: bool | None = None
|
||||
is_active: bool | None = None
|
||||
|
||||
_check_dob = field_validator("date_of_birth")(_not_in_future)
|
||||
|
||||
|
||||
class DoctorRead(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
@@ -34,10 +46,22 @@ class DoctorRead(BaseModel):
|
||||
speciality: str | None
|
||||
qualification: str | None
|
||||
contact_number: str | None
|
||||
date_of_birth: date | None
|
||||
is_core_doctor: bool
|
||||
is_active: bool
|
||||
#: PENDING / APPROVED / REJECTED — see app/common/master_approval.py.
|
||||
approval_status: str
|
||||
rejection_reason: str | None
|
||||
requested_by_name: str | None
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class AssignToMR(BaseModel):
|
||||
mr_user_id: uuid.UUID
|
||||
effective_from: date | None = None
|
||||
|
||||
|
||||
class RejectMaster(BaseModel):
|
||||
"""Why the Area Manager refused a doctor or chemist an MR added — shown to that MR."""
|
||||
|
||||
reason: str = Field(min_length=3, max_length=500)
|
||||
|
||||
+69
-8
@@ -7,14 +7,22 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.dependencies import assert_can_view_hq, visible_hq_ids
|
||||
from app.common.enums import AuditAction, EntityType, Role
|
||||
from app.common import master_approval
|
||||
from app.common.enums import AuditAction, EntityType, MasterApproval, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, NotFoundError, ValidationError
|
||||
from app.common.utils import snapshot
|
||||
from app.doctors.models import Doctor, DoctorAssignment
|
||||
from app.doctors.schemas import DoctorCreate, DoctorUpdate
|
||||
from app.users.models import User
|
||||
|
||||
_AUDIT_FIELDS = ["name", "clinic_address", "speciality", "is_core_doctor", "is_active"]
|
||||
_AUDIT_FIELDS = [
|
||||
"name",
|
||||
"clinic_address",
|
||||
"speciality",
|
||||
"date_of_birth",
|
||||
"is_core_doctor",
|
||||
"is_active",
|
||||
]
|
||||
|
||||
|
||||
class DoctorService:
|
||||
@@ -23,10 +31,35 @@ class DoctorService:
|
||||
self.audit = AuditService(db)
|
||||
|
||||
async def create(self, data: DoctorCreate, actor: User, ip: str | None = None) -> Doctor:
|
||||
# A2/A3 — masters are owned by ADMIN/GM. An MR cannot create a doctor.
|
||||
# A2/A3 — masters belong to a headquarters. An MR may add one only to their own.
|
||||
await assert_can_view_hq(self.db, actor, data.headquarters_id)
|
||||
|
||||
doctor = Doctor(**data.model_dump())
|
||||
values = data.model_dump()
|
||||
# Added by an MR: it waits for the Area Manager. One they were refused is sent again.
|
||||
twin = None
|
||||
if actor.role in FIELD_ROLES:
|
||||
twin = await master_approval.rejected_twin(
|
||||
self.db, Doctor, {k: values[k] for k in ('headquarters_id', 'name', 'clinic_address')}
|
||||
)
|
||||
if twin is not None:
|
||||
await master_approval.resubmit(self.db, twin, values, actor, DoctorAssignment, "doctor_id")
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DOCTOR,
|
||||
entity_id=twin.id,
|
||||
action=AuditAction.SUBMIT,
|
||||
after=snapshot(twin, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(twin)
|
||||
return twin
|
||||
|
||||
doctor = Doctor(
|
||||
**values,
|
||||
approval_status=master_approval.initial_status(actor),
|
||||
requested_by_user_id=actor.id if actor.role in FIELD_ROLES else None,
|
||||
)
|
||||
self.db.add(doctor)
|
||||
try:
|
||||
await self.db.flush()
|
||||
@@ -45,7 +78,22 @@ class DoctorService:
|
||||
after=snapshot(doctor, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
# An MR only sees assigned doctors, so the one they add is assigned to them.
|
||||
if actor.role in FIELD_ROLES:
|
||||
start = date.today()
|
||||
self.db.add(
|
||||
DoctorAssignment(doctor_id=doctor.id, mr_user_id=actor.id, effective_from=start)
|
||||
)
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DOCTOR,
|
||||
entity_id=doctor.id,
|
||||
action=AuditAction.ASSIGN,
|
||||
after={"mr_user_id": str(actor.id), "effective_from": start.isoformat()},
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(doctor)
|
||||
return doctor
|
||||
|
||||
async def update(
|
||||
@@ -74,7 +122,7 @@ class DoctorService:
|
||||
|
||||
async def list_for(self, actor: User) -> list[Doctor]:
|
||||
"""An MR sees ONLY doctors currently assigned to them (R5)."""
|
||||
if actor.role == Role.MR:
|
||||
if actor.role in FIELD_ROLES:
|
||||
stmt = (
|
||||
select(Doctor)
|
||||
.join(DoctorAssignment, DoctorAssignment.doctor_id == Doctor.id)
|
||||
@@ -82,13 +130,14 @@ class DoctorService:
|
||||
DoctorAssignment.mr_user_id == actor.id,
|
||||
DoctorAssignment.effective_to.is_(None),
|
||||
Doctor.is_active.is_(True),
|
||||
Doctor.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
.order_by(Doctor.name)
|
||||
)
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
allowed = await visible_hq_ids(self.db, actor)
|
||||
stmt = select(Doctor).order_by(Doctor.name)
|
||||
stmt = select(Doctor).where(Doctor.approval_status == MasterApproval.APPROVED).order_by(Doctor.name)
|
||||
if allowed is not None:
|
||||
stmt = stmt.where(Doctor.headquarters_id.in_(allowed or [None]))
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
@@ -105,14 +154,16 @@ class DoctorService:
|
||||
if doctor is None:
|
||||
raise NotFoundError("Doctor not found")
|
||||
await assert_can_view_hq(self.db, actor, doctor.headquarters_id)
|
||||
if doctor.approval_status != MasterApproval.APPROVED:
|
||||
raise ValidationError("This doctor is still waiting for the Area Manager's approval")
|
||||
|
||||
mr = await self.db.get(User, mr_user_id)
|
||||
if mr is None:
|
||||
raise NotFoundError("MR not found")
|
||||
if mr.role != Role.MR:
|
||||
if mr.role not in FIELD_ROLES:
|
||||
raise ValidationError("Doctors can only be assigned to an MR")
|
||||
# An MR may only be given doctors from their own headquarters.
|
||||
if mr.headquarters_id != doctor.headquarters_id:
|
||||
if doctor.headquarters_id not in mr.covered_headquarter_ids:
|
||||
raise ValidationError(
|
||||
"MR and doctor belong to different headquarters",
|
||||
{
|
||||
@@ -150,3 +201,13 @@ class DoctorService:
|
||||
)
|
||||
await self.db.commit()
|
||||
return assignment
|
||||
|
||||
async def requests_for(self, actor: User) -> list[Doctor]:
|
||||
return await master_approval.requests_for(self.db, Doctor, actor)
|
||||
|
||||
async def review(
|
||||
self, doctor_id: uuid.UUID, approve: bool, reason: str | None, actor: User, ip: str | None = None
|
||||
) -> Doctor:
|
||||
return await master_approval.review(
|
||||
self.db, Doctor, EntityType.DOCTOR, doctor_id, approve, reason, actor, ip
|
||||
)
|
||||
|
||||
@@ -56,15 +56,17 @@ class Headquarters(Base, UUIDMixin, TimestampMixin):
|
||||
class AMHeadquarters(Base, UUIDMixin, TimestampMixin):
|
||||
"""Which Area Manager currently manages which headquarters.
|
||||
|
||||
`effective_to IS NULL` means "current". A partial unique index guarantees at most
|
||||
one active Area Manager per headquarters at any time.
|
||||
`effective_to IS NULL` means "current". A headquarters may have several Area Managers
|
||||
and an Area Manager several headquarters; the partial unique index only stops the same
|
||||
pair being active twice.
|
||||
"""
|
||||
|
||||
__tablename__ = "am_headquarters"
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"uq_active_am_per_hq",
|
||||
"uq_active_am_hq_pair",
|
||||
"headquarters_id",
|
||||
"area_manager_user_id",
|
||||
unique=True,
|
||||
postgresql_where=text("effective_to IS NULL"),
|
||||
),
|
||||
|
||||
@@ -81,9 +81,8 @@ class OrganizationService:
|
||||
) -> list[AMHeadquarters]:
|
||||
"""Make `headquarters_ids` exactly the set of headquarters this AM manages.
|
||||
|
||||
Headquarters missing from the list are released, new ones are added. If a new
|
||||
headquarters already has another AM, that mapping is closed first (one active AM
|
||||
per headquarters stays true). An AM may manage any number of headquarters.
|
||||
Headquarters missing from the list are released, new ones are added. Other Area
|
||||
Managers of the same headquarters keep it: a headquarters may have several.
|
||||
"""
|
||||
manager = await self.db.get(User, am_user_id)
|
||||
if manager is None or manager.company_id != actor.company_id:
|
||||
@@ -127,16 +126,6 @@ class OrganizationService:
|
||||
for hq_id in released:
|
||||
mine[hq_id].effective_to = start
|
||||
|
||||
if added:
|
||||
for row in (
|
||||
await self.db.execute(
|
||||
select(AMHeadquarters).where(
|
||||
AMHeadquarters.headquarters_id.in_(added),
|
||||
AMHeadquarters.effective_to.is_(None),
|
||||
)
|
||||
)
|
||||
).scalars():
|
||||
row.effective_to = start
|
||||
await self.db.flush()
|
||||
|
||||
for hq_id in added:
|
||||
@@ -176,10 +165,9 @@ class OrganizationService:
|
||||
actor: User,
|
||||
ip: str | None = None,
|
||||
) -> AMHeadquarters:
|
||||
"""Assign an AM to an HQ, closing any existing active mapping first.
|
||||
"""Add an Area Manager to a headquarters, beside any it already has.
|
||||
|
||||
The partial unique index `uq_active_am_per_hq` is the real guarantee that only
|
||||
one Area Manager is active per headquarters at a time.
|
||||
Assigning the same pair twice changes nothing (`uq_active_am_hq_pair`).
|
||||
"""
|
||||
hq = await self.db.get(Headquarters, headquarters_id)
|
||||
if hq is None:
|
||||
@@ -207,8 +195,6 @@ class OrganizationService:
|
||||
for row in existing:
|
||||
if row.area_manager_user_id == am_user_id:
|
||||
return row
|
||||
row.effective_to = start
|
||||
await self.db.flush()
|
||||
|
||||
mapping = AMHeadquarters(
|
||||
area_manager_user_id=am_user_id,
|
||||
|
||||
+248
-48
@@ -17,7 +17,7 @@ from dataclasses import dataclass
|
||||
from datetime import date
|
||||
|
||||
DATE_FIELDS = {"report_date", "date_of_visit"}
|
||||
APPROVAL_FORMS = {"standard_tour_plan", "tour_plan", "fare_chart"}
|
||||
APPROVAL_FORMS = {"standard_tour_plan", "tour_plan", "fare_chart", "expense_statement"}
|
||||
SERIAL = "#" # pseudo-field: the running "Sl.No." column
|
||||
_ALIGN = {"left": "l", "center": "c", "right": "r"}
|
||||
|
||||
@@ -27,7 +27,7 @@ Meta = tuple[str, str] | None # (printed label, column) — None leaves the slo
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Col:
|
||||
header: str
|
||||
header: str # a "\n" starts a second line in the printed heading
|
||||
field: str
|
||||
width: float # percent of the table
|
||||
align: str = "left"
|
||||
@@ -94,20 +94,19 @@ FORMS: dict[str, FormSpec] = {
|
||||
Col("Date", "report_date", 15.3, "center"),
|
||||
Col("From", "from_location", 30.2),
|
||||
Col("To", "to_location", 27.1),
|
||||
Col("Kilometers (KM)", "kilometers_km", 17.6, "center"),
|
||||
Col("Kilometers (KM)\n(One Way)", "kilometers_km", 17.6, "center"),
|
||||
),
|
||||
serial_field="serial_no",
|
||||
serial_dot=True,
|
||||
),
|
||||
"area_covered": FormSpec(
|
||||
"area_covered",
|
||||
"AREA COVERED",
|
||||
"AREA COVERAGE",
|
||||
_NAME_HQ_MONTH,
|
||||
(
|
||||
Col("SALE STATUS", "sale_status", 34.0),
|
||||
Col("AREA TO COVER", "area_to_cover", 24.3),
|
||||
Col("SALE OBJECTIVE", "sale_objective", 20.8),
|
||||
Col("STATUS", "status", 20.9, "center"),
|
||||
Col("AREA TO COVER", "area_to_cover", 40.0),
|
||||
Col("SALE OBJECTIVE", "sale_objective", 30.0),
|
||||
Col("STATUS", "status", 30.0, "center"),
|
||||
),
|
||||
min_rows=28,
|
||||
full_letterhead=False,
|
||||
@@ -130,8 +129,8 @@ FORMS: dict[str, FormSpec] = {
|
||||
"FIELD ACTIVITY REPORT",
|
||||
_NAME_HQ_MONTH,
|
||||
(
|
||||
Col("DATE", "report_date", 14.5, "center"),
|
||||
Col("STATION WORKED", "station_worked", 36.6),
|
||||
Col("DATE", "report_date", 17.5, "center"),
|
||||
Col("STATION WORKED", "station_worked", 33.6),
|
||||
Col("NO. OF DOCTORS CALLED", "no_of_doctors_called", 19.8, "center"),
|
||||
Col("NO. OF CHEMISTS/PDS CALLED", "no_of_chemists_pds_called", 29.1, "center"),
|
||||
),
|
||||
@@ -142,15 +141,19 @@ FORMS: dict[str, FormSpec] = {
|
||||
"sales_and_stock_statement": FormSpec(
|
||||
"sales_and_stock_statement",
|
||||
"SALES AND STOCK STATEMENT",
|
||||
_NAME_HQ_DATE_MONTH,
|
||||
# No date: the statement is for a month, not a day.
|
||||
(
|
||||
(("NAME:", "name"), None),
|
||||
(("HEADQUARTER:", "headquarter"), ("Month:", "month")),
|
||||
),
|
||||
(
|
||||
_serial("Sl.No.", 7.0),
|
||||
Col("Name of the Product", "product_name", 22.6),
|
||||
Col("Opening", "opening", 15.0, "center"),
|
||||
Col("Receipt", "receipt", 14.7, "center"),
|
||||
Col("Sale", "sale", 15.0, "center"),
|
||||
Col("Closing", "closing", 12.2, "center"),
|
||||
Col("Value in PTS", "value_in_pts", 13.5, "center"),
|
||||
Col("Name of the Product", "product_name", 27.0),
|
||||
Col("Value in PTS", "value_in_pts", 15.0, "center"),
|
||||
Col("Opening", "opening", 13.0, "center"),
|
||||
Col("Receipt", "receipt", 13.0, "center"),
|
||||
Col("Sale", "sale", 12.5, "center"),
|
||||
Col("Closing", "closing", 12.5, "center"),
|
||||
),
|
||||
min_rows=26,
|
||||
serial_field="serial_no",
|
||||
@@ -160,17 +163,14 @@ FORMS: dict[str, FormSpec] = {
|
||||
"SALES PROJECTION",
|
||||
_NAME_HQ_MONTH,
|
||||
(
|
||||
_serial("SL. No.", 7.1),
|
||||
Col("NAME OF THE PRODUCT", "product_name", 22.7),
|
||||
Col("PTS", "pts", 10.7, "center"),
|
||||
Col("SEC. QTY", "secondary_qty", 9.1, "center"),
|
||||
Col("VALUE", "value", 9.6, "center"),
|
||||
Col("CLOSING", "closing", 11.6, "center"),
|
||||
Col("SEC. VALUE", "secondary_value", 10.1, "center"),
|
||||
# Header text is filled in per report from the "month" column — see
|
||||
# _sales_projection_columns — never a hardcoded month.
|
||||
Col("SALES", "aug_sales", 9.6, "center"),
|
||||
Col("VALUE", "value_2", 9.5, "center"),
|
||||
# Printed under two group headings: CLOSING and PROJECTION (see the template).
|
||||
_serial("SL. No.", 7.0),
|
||||
Col("NAME OF THE PRODUCT", "product_name", 29.0),
|
||||
Col("PTS", "pts", 12.0, "center"),
|
||||
Col("QUANTITY", "closing_qty", 13.0, "center"),
|
||||
Col("VALUE", "closing_value", 13.0, "center"),
|
||||
Col("QUANTITY", "projection_qty", 13.0, "center"),
|
||||
Col("VALUE", "projection_value", 13.0, "center"),
|
||||
),
|
||||
min_rows=26,
|
||||
serial_field="serial_no",
|
||||
@@ -181,6 +181,16 @@ FORMS: dict[str, FormSpec] = {
|
||||
"DAILY CALL REPORT",
|
||||
_NAME_HQ_DATE_MONTH,
|
||||
),
|
||||
"expense_statement": FormSpec(
|
||||
"expense_statement",
|
||||
"Expenses Statement",
|
||||
(
|
||||
(("Name", "name"), ("Designation", "designation")),
|
||||
(("H.Q.", "headquarter"), None),
|
||||
),
|
||||
min_rows=27,
|
||||
serial_field="serial_no",
|
||||
),
|
||||
"weekly_report": FormSpec(
|
||||
"weekly_report",
|
||||
"Weekly Report",
|
||||
@@ -208,6 +218,49 @@ _CHEMIST_COLS = (
|
||||
Col("POB", "pob", 57.0),
|
||||
)
|
||||
_CHEMIST_FIELDS = tuple(c.field for c in _CHEMIST_COLS if c.field != SERIAL)
|
||||
# The morning day plan: who the MR intends to visit, and when. No outcomes yet.
|
||||
_PLAN_DOCTOR_COLS = (
|
||||
_serial("SR. NO.", 8.6),
|
||||
Col("DR. NAME", "doctor_name", 32.0),
|
||||
Col("QUALIFICATION", "qualification", 17.0),
|
||||
Col("PLANNED TIME", "planned_time", 14.0, "center"),
|
||||
Col("REMARKS", "remarks", 28.4),
|
||||
)
|
||||
_PLAN_CHEMIST_COLS = (
|
||||
_serial("SR. NO.", 10.2),
|
||||
Col("NAME OF THE CHEMIST", "chemist_name", 52.8),
|
||||
Col("PLANNED TIME", "planned_time", 37.0, "center"),
|
||||
)
|
||||
# End-of-day report: the planned visits that were not made, and why.
|
||||
_MISSED_COLS = (
|
||||
_serial("SR. NO.", 8.6),
|
||||
Col("NAME", "name", 33.0),
|
||||
Col("TYPE", "kind", 14.0, "center"),
|
||||
Col("REASON NOT VISITED", "reason", 44.4),
|
||||
)
|
||||
|
||||
# Expense Statement: money columns print as Rs | P pairs, like the printed form.
|
||||
_EXPENSE_COLS = (
|
||||
_serial("", 3.0),
|
||||
Col("DATE", "report_date", 8.6, "center"),
|
||||
Col("From", "from_location", 11.6),
|
||||
Col("To", "to_location", 11.6),
|
||||
Col("Rs", "fare_rs", 6.0, "right"),
|
||||
Col("P.", "fare_p", 3.2, "center"),
|
||||
Col("H.Q", "da_hq", 5.6, "right"),
|
||||
Col("EX", "da_ex", 5.6, "right"),
|
||||
Col("OUT", "da_out", 5.6, "right"),
|
||||
Col("Rs", "postage_rs", 5.4, "right"),
|
||||
Col("P.", "postage_p", 3.2, "center"),
|
||||
Col("Rs", "other_expense_rs", 6.0, "right"),
|
||||
Col("P.", "other_expense_p", 3.2, "center"),
|
||||
Col("Rs", "total_rs", 7.0, "right"),
|
||||
Col("P.", "total_p", 3.2, "center"),
|
||||
Col("Business Achived", "business_achieved", 11.2, "center"),
|
||||
)
|
||||
_EXPENSE_SPLIT = ("fare", "postage", "other_expense", "total")
|
||||
_EXPENSE_WHOLE = ("da_hq", "da_ex", "da_out")
|
||||
_EXPENSE_AMOUNTS = ("fare", "da_hq", "da_ex", "da_out", "postage", "other_expense")
|
||||
|
||||
_WEEKLY_SUMMARY = (
|
||||
("No. of Days Worked", "no_of_days_worked"),
|
||||
@@ -234,7 +287,9 @@ def _display(field: str, value: object) -> str:
|
||||
text = _text(value)
|
||||
if field in DATE_FIELDS and text:
|
||||
try:
|
||||
return date.fromisoformat(text[:10]).strftime("%d/%m/%Y")
|
||||
day = date.fromisoformat(text[:10])
|
||||
# Core Doctor Call Conversion prints just the day of the month (13, 25…).
|
||||
return str(day.day) if field == "date_of_visit" else day.strftime("%d/%m/%Y")
|
||||
except ValueError:
|
||||
return text
|
||||
return text
|
||||
@@ -254,12 +309,79 @@ def _has(row: Row, fields: tuple[str, ...]) -> bool:
|
||||
return any(_text(row.get(field)) for field in fields)
|
||||
|
||||
|
||||
def _sales_projection_columns(cols: tuple[Col, ...], rows: list[Row]) -> tuple[Col, ...]:
|
||||
"""The printed form's "<Month>. Sales" column: the month it prints is whichever
|
||||
month the rows were actually filled in for, never a fixed month."""
|
||||
month = _distinct(rows, "month")
|
||||
header = f"{month[:3].upper()}. SALES" if month else "SALES"
|
||||
return tuple(Col(header, c.field, c.width, c.align) if c.field == "aug_sales" else c for c in cols)
|
||||
def _amount(value: object) -> float | None:
|
||||
text = _text(value).replace(",", "")
|
||||
try:
|
||||
return float(text) if text else None
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def _rupees(amount: float | None) -> tuple[str, str]:
|
||||
"""(Rs, P) as the printed form splits money: 1234.5 -> ("1234", "50")."""
|
||||
if amount is None:
|
||||
return "", ""
|
||||
paise = round(amount * 100)
|
||||
return str(paise // 100), f"{paise % 100:02d}"
|
||||
|
||||
|
||||
def _whole(amount: float | None) -> str:
|
||||
if amount is None:
|
||||
return ""
|
||||
return f"{amount:.0f}" if amount == int(amount) else f"{amount:.2f}"
|
||||
|
||||
|
||||
def call_average(rows: list[Row], field: str) -> str:
|
||||
"""Field Activity Report: calls in `field` summed over the days worked, divided by those days.
|
||||
|
||||
A working day is a distinct date with a row (rows without a date count one day each); the MR
|
||||
never types an average — it is always worked out from the figures."""
|
||||
days = {_text(r.get("report_date"))[:10] or f"row-{i}" for i, r in enumerate(rows) if _has(r, ("report_date", field))}
|
||||
if not days:
|
||||
return ""
|
||||
total = sum(v for v in (_amount(r.get(field)) for r in rows) if v is not None)
|
||||
average = round(total / len(days), 2)
|
||||
return f"{average:g}" if average == int(average) else f"{average:.2f}"
|
||||
|
||||
|
||||
def _sales_projection_totals(rows: list[Row]) -> list[str]:
|
||||
"""The TOTAL line under the Sales Projection: one sum per figure column (blank when nothing is filled in)."""
|
||||
totals: list[str] = []
|
||||
for field in ("closing_qty", "closing_value", "projection_qty", "projection_value"):
|
||||
values = [v for v in (_amount(r.get(field)) for r in rows) if v is not None]
|
||||
totals.append(_whole(round(sum(values), 2)) if values else "")
|
||||
return totals
|
||||
|
||||
|
||||
def _expense_row(row: Row) -> Row:
|
||||
out: Row = dict(row)
|
||||
amounts = {f: _amount(row.get(f)) for f in _EXPENSE_AMOUNTS}
|
||||
# The total is always the sum of the day's amounts, whatever was stored.
|
||||
present = [a for a in amounts.values() if a is not None]
|
||||
amounts["total"] = sum(present) if present else None
|
||||
for field in _EXPENSE_SPLIT:
|
||||
out[f"{field}_rs"], out[f"{field}_p"] = _rupees(amounts[field])
|
||||
for field in _EXPENSE_WHOLE:
|
||||
if amounts[field] is not None:
|
||||
out[field] = _whole(amounts[field])
|
||||
return out
|
||||
|
||||
|
||||
def _expense_totals(rows: list[Row]) -> dict[str, str]:
|
||||
"""The TOTAL line under the table, one figure per money column."""
|
||||
sums: dict[str, float | None] = {}
|
||||
for field in _EXPENSE_AMOUNTS:
|
||||
values = [v for v in (_amount(r.get(field)) for r in rows) if v is not None]
|
||||
sums[field] = sum(values) if values else None
|
||||
present = [v for v in sums.values() if v is not None]
|
||||
sums["total"] = sum(present) if present else None
|
||||
totals: dict[str, str] = {}
|
||||
for field in _EXPENSE_SPLIT:
|
||||
totals[f"{field}_rs"], totals[f"{field}_p"] = _rupees(sums[field])
|
||||
for field in _EXPENSE_WHOLE:
|
||||
totals[field] = _whole(sums[field])
|
||||
totals["claimed"] = f"{sums['total']:,.2f}" if sums["total"] is not None else ""
|
||||
return totals
|
||||
|
||||
|
||||
def _grid(
|
||||
@@ -367,6 +489,20 @@ def build_form_context(
|
||||
)
|
||||
extra["stockist_visit"] = _distinct(rows, "stockist_visit")
|
||||
extra["order_value"] = _distinct(rows, "order_value")
|
||||
elif spec.template == "expense_statement":
|
||||
printed = [_expense_row(r) for r in rows]
|
||||
grids["main"] = _grid(
|
||||
_EXPENSE_COLS,
|
||||
printed,
|
||||
min_rows=spec.min_rows,
|
||||
serial_field=None if renumber else spec.serial_field,
|
||||
)
|
||||
extra["sums"] = _expense_totals(rows)
|
||||
dates = sorted(
|
||||
{_text(r.get("report_date"))[:10] for r in rows if _text(r.get("report_date"))}
|
||||
)
|
||||
extra["period_from"] = _display("report_date", dates[0]) if dates else ""
|
||||
extra["period_to"] = _display("report_date", dates[-1]) if dates else ""
|
||||
elif spec.template == "weekly_report":
|
||||
extra["reports"] = [
|
||||
{
|
||||
@@ -377,20 +513,19 @@ def build_form_context(
|
||||
for r in rows or [{}]
|
||||
]
|
||||
else:
|
||||
columns = spec.columns
|
||||
if table_name == "sales_projection":
|
||||
columns = _sales_projection_columns(columns, rows)
|
||||
grids["main"] = _grid(
|
||||
columns,
|
||||
spec.columns,
|
||||
rows,
|
||||
min_rows=spec.min_rows,
|
||||
serial_field=None if renumber else spec.serial_field,
|
||||
serial_dot=spec.serial_dot,
|
||||
table_width=spec.table_width,
|
||||
)
|
||||
if table_name == "sales_projection":
|
||||
extra["totals"] = _sales_projection_totals(rows)
|
||||
if table_name == "field_activity_report":
|
||||
extra["doctor_call_avg"] = _distinct(rows, "doctor_call_avg")
|
||||
extra["chemist_call_avg"] = _distinct(rows, "chemist_call_avg")
|
||||
extra["doctor_call_avg"] = call_average(rows, "no_of_doctors_called")
|
||||
extra["chemist_call_avg"] = call_average(rows, "no_of_chemists_pds_called")
|
||||
|
||||
approval = None
|
||||
if table_name in APPROVAL_FORMS:
|
||||
@@ -426,12 +561,22 @@ _OUTCOME_NOTE = {"NOT_AVAILABLE": "Not available", "RESCHEDULED": "Rescheduled"}
|
||||
|
||||
|
||||
def _doctor_remarks(call) -> str:
|
||||
"""The remarks column; a visit that did not happen is flagged, since the form has no outcome column."""
|
||||
"""The remarks column; a visit that did not happen is flagged, since the form has no outcome column,
|
||||
and so is any gift given."""
|
||||
note = _OUTCOME_NOTE.get(getattr(call.call_outcome, "value", str(call.call_outcome)), "")
|
||||
return ". ".join(part for part in (note, (call.remarks or "").strip()) if part)
|
||||
# The printed form has no gift column either, so a gift is noted with the remarks.
|
||||
gift = (getattr(call, "gift", None) or "").strip()
|
||||
gift = f"Gift: {gift}" if gift else ""
|
||||
return ". ".join(part for part in (note, gift, (call.remarks or "").strip()) if part)
|
||||
|
||||
|
||||
def build_dcr_context(dcr, doctor_calls, chemist_calls, approval: dict[str, str] | None = None) -> dict[str, object]:
|
||||
def build_dcr_context(
|
||||
dcr,
|
||||
doctor_calls,
|
||||
chemist_calls,
|
||||
approval: dict[str, str] | None = None,
|
||||
missed: list[Row] | None = None,
|
||||
) -> dict[str, object]:
|
||||
"""The approved Daily Call Report, laid out exactly like the printed company form.
|
||||
|
||||
Same template family, headings, column widths and number of ruled lines as the
|
||||
@@ -439,13 +584,22 @@ def build_dcr_context(dcr, doctor_calls, chemist_calls, approval: dict[str, str]
|
||||
from the top in visit order; more visits simply continue on the next page. The only
|
||||
additions are the signatures: the MR's name, and (once approved) the Area Manager's
|
||||
approval signature with its date and time.
|
||||
|
||||
A morning day plan uses the same form with a PLANNED TIME column in place of the
|
||||
visit details. An end-of-day report adds the planned visits that were not made
|
||||
(``missed``: name / kind / reason rows).
|
||||
"""
|
||||
report_type = getattr(dcr, "report_type", None)
|
||||
report_type = getattr(report_type, "value", report_type)
|
||||
if report_type == "PLAN":
|
||||
return _build_plan_context(dcr, doctor_calls, chemist_calls, approval)
|
||||
is_self = report_type == "SELF" # an Area Manager's own report: no approval signature
|
||||
doctors = [
|
||||
{
|
||||
"doctor_name": c.doctor.name,
|
||||
"qualification": c.doctor.qualification or "",
|
||||
"product_promote": c.product_promote or "",
|
||||
"product_sample": c.product_sample or "",
|
||||
"product_promote": _products(c.product_promote),
|
||||
"product_sample": _products(c.product_sample),
|
||||
"remarks": _doctor_remarks(c),
|
||||
}
|
||||
for c in doctor_calls
|
||||
@@ -457,17 +611,63 @@ def build_dcr_context(dcr, doctor_calls, chemist_calls, approval: dict[str, str]
|
||||
]
|
||||
return {
|
||||
"template": "dcr",
|
||||
"label": "Daily Call Report",
|
||||
"title": "DAILY CALL REPORT",
|
||||
"label": "Daily Call Report (Self)" if is_self else "Daily Call Report",
|
||||
"title": "DAILY CALL REPORT (SELF)" if is_self else "DAILY CALL REPORT",
|
||||
"title_suffix": f"{dcr.mr.full_name} — {dcr.report_date.strftime('%d/%m/%Y')}",
|
||||
"full_letterhead": True,
|
||||
"meta_rows": meta,
|
||||
"grids": {
|
||||
"doctors": _grid(_DOCTOR_COLS, doctors, min_rows=10),
|
||||
"chemists": _grid(_CHEMIST_COLS, chemists, min_rows=5, table_width=84),
|
||||
**({"missed": _grid(_MISSED_COLS, missed, min_rows=0)} if missed else {}),
|
||||
},
|
||||
"is_plan": False,
|
||||
"stockist_visit": dcr.stockist_visit or "",
|
||||
"order_value": dcr.order_value or "",
|
||||
"signer_label": "Area Manager Signature" if is_self else "MR Signature",
|
||||
"mr_signed": f"{dcr.mr.full_name} ({dcr.mr.employee_code})",
|
||||
"approval": approval, # {"name": ..., "at": "dd/mm/yyyy HH:MM"} or None
|
||||
}
|
||||
|
||||
|
||||
def _products(names: list[str] | str | None) -> str:
|
||||
"""Several products share one cell of the printed form."""
|
||||
return names if isinstance(names, str) else ", ".join(names or [])
|
||||
|
||||
|
||||
def _build_plan_context(dcr, doctor_calls, chemist_calls, approval) -> dict[str, object]:
|
||||
doctors = [
|
||||
{
|
||||
"doctor_name": c.doctor.name,
|
||||
"qualification": c.doctor.qualification or "",
|
||||
"planned_time": c.visit_time.strftime("%H:%M"),
|
||||
"remarks": (c.remarks or "").strip(),
|
||||
}
|
||||
for c in doctor_calls
|
||||
]
|
||||
chemists = [
|
||||
{"chemist_name": c.chemist.name, "planned_time": c.visit_time.strftime("%H:%M")}
|
||||
for c in chemist_calls
|
||||
]
|
||||
meta = [
|
||||
[("NAME:", dcr.mr.full_name), ("Date:", dcr.report_date.strftime("%d/%m/%Y"))],
|
||||
[("HEADQUARTER:", dcr.headquarters.name), ("Month:", dcr.report_date.strftime("%B %Y"))],
|
||||
]
|
||||
return {
|
||||
"template": "dcr",
|
||||
"label": "Daily Call Report (Plan)",
|
||||
"title": "DAILY CALL REPORT (PLAN)",
|
||||
"title_suffix": f"{dcr.mr.full_name} — {dcr.report_date.strftime('%d/%m/%Y')}",
|
||||
"full_letterhead": True,
|
||||
"meta_rows": meta,
|
||||
"grids": {
|
||||
"doctors": _grid(_PLAN_DOCTOR_COLS, doctors, min_rows=10),
|
||||
"chemists": _grid(_PLAN_CHEMIST_COLS, chemists, min_rows=5, table_width=84),
|
||||
},
|
||||
"is_plan": True,
|
||||
"stockist_visit": "",
|
||||
"order_value": "",
|
||||
"mr_remarks": dcr.mr_remarks or "",
|
||||
"mr_signed": f"{dcr.mr.full_name} ({dcr.mr.employee_code})",
|
||||
"approval": approval,
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ from app.common.enums import AuditAction, EntityType
|
||||
from app.config.settings import settings
|
||||
from app.pdf.forms import approval_signature_line, build_dcr_context, build_form_context
|
||||
from app.reports.models import DailyCallReport
|
||||
from app.reports.serializers import planned_visits
|
||||
from app.users.models import User
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -198,11 +199,17 @@ class PDFService:
|
||||
return {"name": approver.full_name, "at": when}
|
||||
|
||||
async def _context(self, dcr: DailyCallReport) -> dict:
|
||||
missed = [
|
||||
{"name": p.name, "kind": p.kind.title(), "reason": p.missed_reason}
|
||||
for p in planned_visits(dcr)
|
||||
if p.status == "MISSED"
|
||||
]
|
||||
return build_dcr_context(
|
||||
dcr,
|
||||
sorted(dcr.doctor_calls, key=lambda c: c.visit_time),
|
||||
sorted(dcr.chemist_calls, key=lambda c: c.visit_time),
|
||||
await self._approval(dcr),
|
||||
missed,
|
||||
)
|
||||
|
||||
async def render_bytes(self, dcr: DailyCallReport) -> bytes:
|
||||
|
||||
@@ -19,19 +19,27 @@
|
||||
|
||||
{% block form %}
|
||||
{{ grid(grids.doctors, left=True) }}
|
||||
<h2 class="section">CHEMIST VISIT</h2>
|
||||
<h2 class="section">{{ "CHEMISTS TO VISIT" if is_plan else "CHEMIST VISIT" }}</h2>
|
||||
{{ grid(grids.chemists, left=True) }}
|
||||
{% if is_plan %}
|
||||
{% if mr_remarks %}<div class="visit"><div><b>REMARKS :</b> {{ mr_remarks }}</div></div>{% endif %}
|
||||
{% else %}
|
||||
{% if grids.missed %}
|
||||
<h2 class="section">PLANNED VISITS NOT MADE</h2>
|
||||
{{ grid(grids.missed, left=True) }}
|
||||
{% endif %}
|
||||
<div class="visit">
|
||||
<div><b>STOCKIST VISIT :</b> {{ stockist_visit }}</div>
|
||||
<div><b>ORDER :</b> {{ order_value }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block signature %}
|
||||
<table class="dcr-sign">
|
||||
<tr>
|
||||
<td class="mr">
|
||||
<div class="sign-label">MR Signature</div>
|
||||
<div class="sign-label">{{ signer_label | default("MR Signature") }}</div>
|
||||
<div class="signed">{{ mr_signed }}</div>
|
||||
</td>
|
||||
<td class="am">
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
{{ company_name }}
|
||||
{{ company_address }}
|
||||
Website: {{ company_website }} E-Mail: {{ company_email }} PHONE: {{ company_phone }}
|
||||
DAILY CALL REPORT
|
||||
{{ title }}
|
||||
{% for pair in meta_rows %}{% for slot in pair %}{% if slot %}{{ slot[0] }} {{ slot[1] }} {% endif %}{% endfor %}
|
||||
{% endfor %}
|
||||
SR.NO | DR. NAME | QUALIFICATION | PRODUCT PROMOTE | PRODUCT SAMPLE | REMARKS
|
||||
{{ grids.doctors.headers | join(' | ') }}
|
||||
{% for r in grids.doctors.rows %}{{ r.cells | join(' | ') }}
|
||||
{% endfor %}
|
||||
CHEMIST VISIT
|
||||
SR.NO | NAME OF THE CHEMIST | POB
|
||||
{{ "CHEMISTS TO VISIT" if is_plan else "CHEMIST VISIT" }}
|
||||
{{ grids.chemists.headers | join(' | ') }}
|
||||
{% for r in grids.chemists.rows %}{{ r.cells | join(' | ') }}
|
||||
{% endfor %}
|
||||
STOCKIST VISIT : {{ stockist_visit }}
|
||||
{% if grids.missed %}PLANNED VISITS NOT MADE
|
||||
{{ grids.missed.headers | join(' | ') }}
|
||||
{% for r in grids.missed.rows %}{{ r.cells | join(' | ') }}
|
||||
{% endfor %}
|
||||
{% endif %}{% if not is_plan %}STOCKIST VISIT : {{ stockist_visit }}
|
||||
ORDER : {{ order_value }}
|
||||
|
||||
{% endif %}
|
||||
MR Signature: {{ mr_signed }}
|
||||
{% if approval %}APPROVAL SIGNATURE: {{ approval.name }} ({{ approval.at }})
|
||||
{% endif %}
|
||||
@@ -30,7 +30,7 @@
|
||||
<table class="grid{% if left %} left{% endif %}" style="width:{{ g.width }}">
|
||||
<colgroup>{% for w in g.widths %}<col style="width:{{ w }}">{% endfor %}</colgroup>
|
||||
<thead>
|
||||
<tr>{% for h in g.headers %}<th>{{ h }}</th>{% endfor %}</tr>
|
||||
<tr>{% for h in g.headers %}<th>{% for part in h.split("\n") %}{{ part }}{% if not loop.last %}<br>{% endif %}{% endfor %}</th>{% endfor %}</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for r in g.rows %}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
{% extends "forms/_form_base.html" %}
|
||||
{% from "forms/_macros.html" import letterhead %}
|
||||
|
||||
{% block page_box %}size: A4 landscape; margin: 7mm 11mm;{% endblock %}
|
||||
|
||||
{% block form_styles %}
|
||||
body { font-size: 10pt; }
|
||||
.company .name { font-size: 24pt; }
|
||||
.company .name { line-height: 1.05; }
|
||||
.company .line { font-size: 9pt; line-height: 1.2; font-style: italic; }
|
||||
h1.title { margin: 2mm 0 2mm; font-size: 15pt; letter-spacing: 1.5pt; font-style: italic; text-decoration: none; text-align: left; }
|
||||
h1.title .ln { min-width: 34mm; font-size: 10.5pt; letter-spacing: 0; font-style: normal; }
|
||||
table.who { width: 100%; border-collapse: collapse; margin: 1.5mm 0 0; }
|
||||
table.who td { padding: 0; font-size: 10pt; font-style: italic; font-weight: bold; white-space: nowrap; }
|
||||
table.who .ln { min-width: 52mm; font-style: normal; font-weight: normal; }
|
||||
table.who .ln.short { min-width: 30mm; }
|
||||
table.grid th, table.grid td { padding: 0 .9mm; }
|
||||
table.grid th { height: 5mm; font-size: 9pt; font-weight: normal; }
|
||||
table.grid thead tr:first-child th { font-weight: bold; }
|
||||
table.grid td { height: 4.2mm; font-size: 9pt; overflow-wrap: normal; white-space: nowrap; overflow: hidden; }
|
||||
table.grid tr.total td { font-weight: bold; }
|
||||
table.grid td.no { font-size: 8pt; }
|
||||
table.foot { width: 100%; margin-top: 2mm; border-collapse: collapse; page-break-inside: avoid; }
|
||||
table.foot td { vertical-align: top; padding: 0 2mm 0 0; font-size: 9pt; }
|
||||
.box { border: .7pt solid #000; padding: 1mm 2mm; }
|
||||
.box .office { text-align: center; font-style: italic; font-weight: bold; letter-spacing: 1.5pt; margin-bottom: 1mm; }
|
||||
.box .ln { min-width: 34mm; text-align: left; padding-left: 1mm; }
|
||||
.line-row { margin: 1.2mm 0; white-space: nowrap; }
|
||||
.status div { margin: .3mm 0; white-space: nowrap; }
|
||||
.status .ln { min-width: 18mm; height: 3.8mm; }
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
{{ letterhead(full_letterhead) }}
|
||||
<table class="who">
|
||||
<tr>
|
||||
<td>Name <span class="ln">{{ meta_rows[0][0][1] }}</span></td>
|
||||
<td>Designation <span class="ln">{{ meta_rows[0][1][1] }}</span></td>
|
||||
<td>H.Q. <span class="ln short">{{ meta_rows[1][0][1] }}</span></td>
|
||||
<td>Date <span class="ln short"></span></td>
|
||||
</tr>
|
||||
</table>
|
||||
<h1 class="title">Expenses Statement from Dt. <span class="ln">{{ period_from }}</span> to <span class="ln">{{ period_to }}</span></h1>
|
||||
|
||||
{% set g = grids.main %}
|
||||
<table class="grid" style="width:{{ g.width }}">
|
||||
<colgroup>{% for w in g.widths %}<col style="width:{{ w }}">{% endfor %}</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th rowspan="2"></th>
|
||||
<th rowspan="2">DATE</th>
|
||||
<th colspan="2">Place Of Visit</th>
|
||||
<th colspan="2">Fare</th>
|
||||
<th colspan="3">Daily Allowance</th>
|
||||
<th colspan="2">Postage</th>
|
||||
<th colspan="2">Other Expence</th>
|
||||
<th colspan="2">Total</th>
|
||||
<th rowspan="2">Business Achived</th>
|
||||
</tr>
|
||||
<tr>
|
||||
{% for h in g.headers[2:-1] %}<th>{{ h }}</th>{% endfor %}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for r in g.rows %}
|
||||
<tr>{% for c in r.cells %}<td class="{{ g.aligns[loop.index0] }}{% if loop.first %} no{% endif %}">{{ c }}</td>{% endfor %}</tr>
|
||||
{% endfor %}
|
||||
<tr class="total">
|
||||
<td colspan="4" class="c">TOTAL</td>
|
||||
<td class="r">{{ sums.fare_rs }}</td><td class="c">{{ sums.fare_p }}</td>
|
||||
<td class="r">{{ sums.da_hq }}</td><td class="r">{{ sums.da_ex }}</td><td class="r">{{ sums.da_out }}</td>
|
||||
<td class="r">{{ sums.postage_rs }}</td><td class="c">{{ sums.postage_p }}</td>
|
||||
<td class="r">{{ sums.other_expense_rs }}</td><td class="c">{{ sums.other_expense_p }}</td>
|
||||
<td class="r">{{ sums.total_rs }}</td><td class="c">{{ sums.total_p }}</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<table class="foot">
|
||||
<tr>
|
||||
<td style="width:46%">
|
||||
<div class="box">
|
||||
<div class="office">For Office Use Only</div>
|
||||
<div class="line-row">Amount Claimed <span class="ln">{{ sums.claimed }}</span> Amt. Passed <span class="ln"></span></div>
|
||||
<div class="line-row">Deduction / Addition with reason <span class="ln" style="min-width:60mm"></span></div>
|
||||
</div>
|
||||
</td>
|
||||
<td style="width:26%">
|
||||
<div class="box status">
|
||||
<div class="office">Report Status</div>
|
||||
<div>Daily Report :- <span class="ln"></span></div>
|
||||
<div>Stock and Sale Statement :- <span class="ln"></span></div>
|
||||
<div>Weekly Report :- <span class="ln"></span></div>
|
||||
<div>Monthly Apprasal :- <span class="ln"></span></div>
|
||||
</div>
|
||||
</td>
|
||||
<td style="width:28%">
|
||||
<div class="line-row">Signature :- <span class="ln">{{ approval.mr_name }}</span></div>
|
||||
<div class="line-row">Checked by :- <span class="ln">{% if approval.value != "PENDING" %}{{ approval.value }}{% endif %}</span></div>
|
||||
<div class="line-row">DATE :- <span class="ln">{{ approval.when }}</span></div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% endblock %}
|
||||
@@ -1,5 +1,4 @@
|
||||
{% extends "forms/_form_base.html" %}
|
||||
{% from "forms/_macros.html" import grid %}
|
||||
|
||||
{# Arial throughout, title tucked directly under the company name — as printed. #}
|
||||
{% block form_styles %}
|
||||
@@ -10,11 +9,35 @@
|
||||
font-size: 14pt;
|
||||
}
|
||||
table.grid th, table.grid td { font-family: "Liberation Sans", Arial, "DejaVu Sans", sans-serif; }
|
||||
table.grid th { height: 10mm; font-size: 9.5pt; }
|
||||
table.grid th { height: 7mm; font-size: 9.5pt; }
|
||||
table.grid th.sub { height: 6mm; font-size: 8.5pt; }
|
||||
table.grid td { font-size: 9.5pt; }
|
||||
table.grid td:nth-child(2) { font-weight: bold; }
|
||||
table.grid tr.total td { font-weight: bold; }
|
||||
table.grid tr.total td:nth-child(2) { text-align: right; }
|
||||
{% endblock %}
|
||||
|
||||
{# Two heading rows: CLOSING and PROJECTION each span their QUANTITY and VALUE. #}
|
||||
{% block form %}
|
||||
{{ grid(grids.main) }}
|
||||
{% set g = grids.main %}
|
||||
<table class="grid" style="width:{{ g.width }}">
|
||||
<colgroup>{% for w in g.widths %}<col style="width:{{ w }}">{% endfor %}</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
{% for h in g.headers[:3] %}<th rowspan="2">{{ h }}</th>{% endfor %}
|
||||
<th colspan="2">CLOSING</th>
|
||||
<th colspan="2">PROJECTION</th>
|
||||
</tr>
|
||||
<tr>{% for h in g.headers[3:] %}<th class="sub">{{ h }}</th>{% endfor %}</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for r in g.rows %}
|
||||
<tr>{% for c in r.cells %}<td class="{{ g.aligns[loop.index0] }}">{{ c }}</td>{% endfor %}</tr>
|
||||
{% endfor %}
|
||||
<tr class="total">
|
||||
<td></td><td>TOTAL</td><td></td>
|
||||
{% for t in totals %}<td class="{{ g.aligns[loop.index0 + 3] }}">{{ t }}</td>{% endfor %}
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock %}
|
||||
|
||||
@@ -5,7 +5,7 @@ from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.audit.service import AuditService
|
||||
from app.common.enums import AuditAction, EntityType, Role
|
||||
from app.common.enums import AuditAction, EntityType, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, NotFoundError
|
||||
from app.common.utils import snapshot
|
||||
from app.products.models import Product
|
||||
@@ -27,7 +27,7 @@ class ProductService:
|
||||
|
||||
async def list_for(self, actor: User) -> list[Product]:
|
||||
stmt = select(Product).where(Product.company_id == actor.company_id).order_by(Product.name)
|
||||
if actor.role == Role.MR:
|
||||
if actor.role in FIELD_ROLES:
|
||||
stmt = stmt.where(Product.is_active.is_(True))
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
|
||||
+82
-11
@@ -1,4 +1,12 @@
|
||||
"""The Daily Call Report and its call rows — the core artefact of the MVP."""
|
||||
"""The Daily Call Report and its call rows — the core artefact of the MVP.
|
||||
|
||||
Each working day an MR files two reports (see ``ReportType``): a morning PLAN of the
|
||||
doctors and chemists they intend to visit, and an end-of-day ACTUAL report of the visits
|
||||
that happened. Both share this table and its call rows; the ACTUAL report points at its
|
||||
plan, and records why any planned visit was missed in ``missed_visit``.
|
||||
|
||||
An Area Manager files a SELF report of their own visits in the same table; ``mr_user_id``
|
||||
is then the Area Manager (the report's owner), and submitting it makes it final."""
|
||||
|
||||
import uuid
|
||||
from datetime import date, datetime, time
|
||||
@@ -14,19 +22,24 @@ from sqlalchemy import (
|
||||
Time,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import ARRAY
|
||||
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.common.enums import CallOutcome, ConversionStatus, DCRStatus, ReportType, 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"),
|
||||
# V1 / A9 — ONE report of each type per MR per calendar date. Enforced in the
|
||||
# database and not only in the service, so a double-submit race cannot create two.
|
||||
UniqueConstraint("mr_user_id", "report_date", "report_type", name="uq_dcr_mr_date_type"),
|
||||
# A plan is followed by at most one end-of-day report.
|
||||
UniqueConstraint("plan_id", name="uq_dcr_plan_id"),
|
||||
# Only an end-of-day report points at a plan.
|
||||
CheckConstraint("report_type = 'ACTUAL' OR plan_id IS NULL", name="ck_dcr_plan_link"),
|
||||
# An approved report must carry its approval timestamp, and nothing else may.
|
||||
CheckConstraint(
|
||||
"(status = 'APPROVED' AND approved_at IS NOT NULL) "
|
||||
@@ -44,6 +57,16 @@ class DailyCallReport(Base, UUIDMixin, TimestampMixin):
|
||||
PGUUID(as_uuid=True), ForeignKey("headquarters.id", ondelete="RESTRICT"), nullable=False
|
||||
)
|
||||
report_date: Mapped[date] = mapped_column(Date, nullable=False)
|
||||
report_type: Mapped[ReportType] = mapped_column(
|
||||
Enum(ReportType, name="report_type_enum", native_enum=False, length=10),
|
||||
default=ReportType.ACTUAL,
|
||||
nullable=False,
|
||||
)
|
||||
#: The approved morning plan an end-of-day report is filed against. NULL for plans,
|
||||
#: and for end-of-day reports filed before plans existed.
|
||||
plan_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("daily_call_report.id", ondelete="RESTRICT")
|
||||
)
|
||||
status: Mapped[DCRStatus] = mapped_column(
|
||||
Enum(DCRStatus, name="dcr_status_enum", native_enum=False, length=20),
|
||||
default=DCRStatus.DRAFT,
|
||||
@@ -81,6 +104,16 @@ class DailyCallReport(Base, UUIDMixin, TimestampMixin):
|
||||
chemist_calls: Mapped[list["ChemistCall"]] = relationship(
|
||||
back_populates="dcr", cascade="all, delete-orphan", lazy="selectin"
|
||||
)
|
||||
missed_visits: Mapped[list["MissedVisit"]] = relationship(
|
||||
back_populates="dcr", cascade="all, delete-orphan", lazy="selectin"
|
||||
)
|
||||
#: Loaded explicitly by the repository (self-referential, so never eagerly by default).
|
||||
plan: Mapped["DailyCallReport | None"] = relationship(
|
||||
remote_side="DailyCallReport.id", foreign_keys=[plan_id], back_populates="actual"
|
||||
)
|
||||
actual: Mapped["DailyCallReport | None"] = relationship(
|
||||
foreign_keys=[plan_id], back_populates="plan", uselist=False
|
||||
)
|
||||
|
||||
@property
|
||||
def is_locked(self) -> bool:
|
||||
@@ -103,18 +136,25 @@ class DoctorCall(Base, UUIDMixin, TimestampMixin):
|
||||
doctor_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("doctor.id", ondelete="RESTRICT"), nullable=False
|
||||
)
|
||||
#: On a plan this is the planned time; on an end-of-day report, when the visit happened.
|
||||
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,
|
||||
#: Required on an end-of-day report; a planned visit has no outcome yet (NULL).
|
||||
call_outcome: Mapped[CallOutcome | None] = mapped_column(
|
||||
Enum(CallOutcome, name="call_outcome_enum", native_enum=False, length=20)
|
||||
)
|
||||
#: 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))
|
||||
#: The printed form's "PRODUCT PROMOTE" and "PRODUCT SAMPLE" columns; several per visit.
|
||||
product_promote: Mapped[list[str]] = mapped_column(
|
||||
ARRAY(String(200)), default=list, server_default="{}", nullable=False
|
||||
)
|
||||
product_sample: Mapped[list[str]] = mapped_column(
|
||||
ARRAY(String(200)), default=list, server_default="{}", nullable=False
|
||||
)
|
||||
#: A gift given to the doctor on this visit, typed in by the MR.
|
||||
gift: Mapped[str | None] = mapped_column(String(200))
|
||||
remarks: Mapped[str | None] = mapped_column(Text)
|
||||
|
||||
dcr: Mapped["DailyCallReport"] = relationship(back_populates="doctor_calls")
|
||||
@@ -142,3 +182,34 @@ class ChemistCall(Base, UUIDMixin, TimestampMixin):
|
||||
|
||||
dcr: Mapped["DailyCallReport"] = relationship(back_populates="chemist_calls")
|
||||
chemist: Mapped["Chemist"] = relationship(lazy="selectin") # noqa: F821
|
||||
|
||||
|
||||
class MissedVisit(Base, UUIDMixin, TimestampMixin):
|
||||
"""Why a doctor or chemist on the approved plan was not visited that day.
|
||||
|
||||
Belongs to the end-of-day report; exactly one of doctor/chemist is set.
|
||||
"""
|
||||
|
||||
__tablename__ = "missed_visit"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("dcr_id", "doctor_id", name="uq_missed_dcr_doctor"),
|
||||
UniqueConstraint("dcr_id", "chemist_id", name="uq_missed_dcr_chemist"),
|
||||
CheckConstraint(
|
||||
"(doctor_id IS NULL) <> (chemist_id IS NULL)", name="ck_missed_one_target"
|
||||
),
|
||||
)
|
||||
|
||||
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 | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("doctor.id", ondelete="RESTRICT")
|
||||
)
|
||||
chemist_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("chemist.id", ondelete="RESTRICT")
|
||||
)
|
||||
reason: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
|
||||
dcr: Mapped["DailyCallReport"] = relationship(back_populates="missed_visits")
|
||||
|
||||
@@ -4,7 +4,7 @@ from datetime import date
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.common.enums import DCRStatus
|
||||
from app.common.enums import DCRStatus, ReportType
|
||||
from app.common.repository import BaseRepository
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall
|
||||
|
||||
@@ -19,8 +19,17 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
.options(
|
||||
selectinload(DailyCallReport.doctor_calls).selectinload(DoctorCall.doctor),
|
||||
selectinload(DailyCallReport.chemist_calls).selectinload(ChemistCall.chemist),
|
||||
selectinload(DailyCallReport.missed_visits),
|
||||
selectinload(DailyCallReport.mr),
|
||||
selectinload(DailyCallReport.headquarters),
|
||||
# The plan's visits are the checklist an end-of-day report is measured against.
|
||||
selectinload(DailyCallReport.plan)
|
||||
.selectinload(DailyCallReport.doctor_calls)
|
||||
.selectinload(DoctorCall.doctor),
|
||||
selectinload(DailyCallReport.plan)
|
||||
.selectinload(DailyCallReport.chemist_calls)
|
||||
.selectinload(ChemistCall.chemist),
|
||||
selectinload(DailyCallReport.actual),
|
||||
)
|
||||
# The session keeps objects after commit (expire_on_commit=False), so without this the
|
||||
# report comes back with the call lists it had *before* an add or remove.
|
||||
@@ -29,12 +38,13 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
return (await self.db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
async def get_for_date(
|
||||
self, mr_user_id: uuid.UUID, report_date: date
|
||||
self, mr_user_id: uuid.UUID, report_date: date, report_type: ReportType
|
||||
) -> DailyCallReport | None:
|
||||
"""V1 pre-check. The database unique constraint is the real guarantee."""
|
||||
stmt = select(DailyCallReport).where(
|
||||
DailyCallReport.mr_user_id == mr_user_id,
|
||||
DailyCallReport.report_date == report_date,
|
||||
DailyCallReport.report_type == report_type,
|
||||
)
|
||||
return (await self.db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
@@ -46,6 +56,7 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
status: DCRStatus | None,
|
||||
date_from: date | None,
|
||||
date_to: date | None,
|
||||
report_type: ReportType | None = None,
|
||||
):
|
||||
stmt = select(DailyCallReport)
|
||||
if mr_user_id is not None:
|
||||
@@ -55,6 +66,12 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
stmt = stmt.where(DailyCallReport.headquarters_id.in_(hq_ids or [None]))
|
||||
if status is not None:
|
||||
stmt = stmt.where(DailyCallReport.status == status)
|
||||
if report_type is not None:
|
||||
stmt = stmt.where(DailyCallReport.report_type == report_type)
|
||||
else:
|
||||
# Without a type, lists and counts cover the MR approval workflow; an Area
|
||||
# Manager's own SELF reports are asked for by type.
|
||||
stmt = stmt.where(DailyCallReport.report_type != ReportType.SELF)
|
||||
if date_from is not None:
|
||||
stmt = stmt.where(DailyCallReport.report_date >= date_from)
|
||||
if date_to is not None:
|
||||
@@ -69,13 +86,14 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
status: DCRStatus | None = None,
|
||||
date_from: date | None = None,
|
||||
date_to: date | None = None,
|
||||
report_type: ReportType | None = None,
|
||||
limit: int = 50,
|
||||
offset: int = 0,
|
||||
) -> list[DailyCallReport]:
|
||||
stmt = (
|
||||
self._scoped(
|
||||
mr_user_id=mr_user_id, hq_ids=hq_ids, status=status,
|
||||
date_from=date_from, date_to=date_to,
|
||||
date_from=date_from, date_to=date_to, report_type=report_type,
|
||||
)
|
||||
.options(
|
||||
selectinload(DailyCallReport.doctor_calls),
|
||||
|
||||
+49
-11
@@ -5,7 +5,7 @@ from fastapi import APIRouter, Depends, Query, Request, Response, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import client_ip, get_current_user, require_role
|
||||
from app.common.enums import DCRStatus, Role
|
||||
from app.common.enums import DCRStatus, ReportType, Role
|
||||
from app.common.exceptions import ValidationError
|
||||
from app.database.session import get_db
|
||||
from app.pdf.service import PDFService
|
||||
@@ -17,6 +17,7 @@ from app.reports.schemas import (
|
||||
DCRRead,
|
||||
DCRUpdate,
|
||||
DoctorCallCreate,
|
||||
MissedVisitSet,
|
||||
ReturnRequest,
|
||||
StatusCounts,
|
||||
)
|
||||
@@ -33,17 +34,22 @@ router = APIRouter(prefix="/dcr", tags=["reports"])
|
||||
@router.get("", response_model=list[DCRListItem])
|
||||
async def list_reports(
|
||||
status_filter: DCRStatus | None = Query(default=None, alias="status"),
|
||||
report_type: ReportType | None = Query(default=None, alias="type"),
|
||||
date_from: date | None = None,
|
||||
date_to: date | None = None,
|
||||
limit: int = Query(default=50, ge=1, le=200),
|
||||
offset: int = Query(default=0, ge=0),
|
||||
headquarters_id: uuid.UUID | None = None,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> list[DCRListItem]:
|
||||
"""S2 (MR) and S6 (manager queue). Scope is applied in the service, never here."""
|
||||
"""S2 (MR) and S6 (manager queue). Scope is applied in the service, never here;
|
||||
`headquarters_id` only narrows a manager's list to one of their headquarters."""
|
||||
reports = await DCRService(db).list_for(
|
||||
current_user,
|
||||
headquarters_id=headquarters_id,
|
||||
status=status_filter,
|
||||
report_type=report_type,
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
limit=limit,
|
||||
@@ -54,10 +60,12 @@ async def list_reports(
|
||||
|
||||
@router.get("/counts", response_model=StatusCounts)
|
||||
async def status_counts(
|
||||
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
||||
headquarters_id: uuid.UUID | None = None,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> StatusCounts:
|
||||
"""Header counts — the deliberate, cheap replacement for the cut dashboards."""
|
||||
return StatusCounts(**await DCRService(db).counts_for(current_user))
|
||||
return StatusCounts(**await DCRService(db).counts_for(current_user, headquarters_id))
|
||||
|
||||
|
||||
@router.get("/{dcr_id}", response_model=DCRRead)
|
||||
@@ -87,7 +95,8 @@ async def download_pdf(
|
||||
# uses the current form layout, even for reports approved before the layout changed.
|
||||
content = await PDFService(db).render_bytes(dcr)
|
||||
|
||||
filename = f"DCR-{dcr.mr.employee_code}-{dcr.report_date.isoformat()}.pdf"
|
||||
prefix = {ReportType.PLAN: "PLAN", ReportType.SELF: "DCR-SELF"}.get(dcr.report_type, "DCR")
|
||||
filename = f"{prefix}-{dcr.mr.employee_code}-{dcr.report_date.isoformat()}.pdf"
|
||||
return Response(
|
||||
content=content,
|
||||
media_type="application/pdf",
|
||||
@@ -102,7 +111,7 @@ async def download_pdf(
|
||||
async def create_report(
|
||||
payload: DCRCreate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(await DCRService(db).create(payload, current_user, client_ip(request)))
|
||||
@@ -113,7 +122,7 @@ async def update_report(
|
||||
dcr_id: uuid.UUID,
|
||||
payload: DCRUpdate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(
|
||||
@@ -128,7 +137,7 @@ async def add_doctor_call(
|
||||
dcr_id: uuid.UUID,
|
||||
payload: DoctorCallCreate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(
|
||||
@@ -141,7 +150,7 @@ async def add_chemist_call(
|
||||
dcr_id: uuid.UUID,
|
||||
payload: ChemistCallCreate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(
|
||||
@@ -154,7 +163,7 @@ async def delete_call(
|
||||
dcr_id: uuid.UUID,
|
||||
call_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(
|
||||
@@ -162,6 +171,35 @@ async def delete_call(
|
||||
)
|
||||
|
||||
|
||||
@router.put("/{dcr_id}/missed", response_model=DCRRead)
|
||||
async def set_missed_reason(
|
||||
dcr_id: uuid.UUID,
|
||||
payload: MissedVisitSet,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
"""End-of-day report: why a doctor or chemist on the day plan was not visited."""
|
||||
return to_read(
|
||||
await DCRService(db).set_missed_reason(dcr_id, payload, current_user, client_ip(request))
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{dcr_id}/missed/{missed_id}", response_model=DCRRead)
|
||||
async def clear_missed_reason(
|
||||
dcr_id: uuid.UUID,
|
||||
missed_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(
|
||||
await DCRService(db).clear_missed_reason(
|
||||
dcr_id, missed_id, current_user, client_ip(request)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# ------------------------------------------------------------- transitions
|
||||
|
||||
|
||||
@@ -169,7 +207,7 @@ async def delete_call(
|
||||
async def submit(
|
||||
dcr_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.MR)),
|
||||
current_user: User = Depends(require_role(Role.MR, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> DCRRead:
|
||||
return to_read(await DCRService(db).submit(dcr_id, current_user, client_ip(request)))
|
||||
|
||||
+71
-8
@@ -1,22 +1,49 @@
|
||||
import uuid
|
||||
from datetime import date, datetime, time
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from app.common.enums import CallOutcome, ConversionStatus, DCRStatus, Role
|
||||
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
|
||||
call_outcome: CallOutcome
|
||||
#: 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: str | None = Field(default=None, max_length=200)
|
||||
product_sample: str | None = Field(default=None, max_length=200)
|
||||
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
|
||||
@@ -34,10 +61,11 @@ class DoctorCallRead(BaseModel):
|
||||
doctor_speciality: str | None
|
||||
is_core_doctor: bool
|
||||
visit_time: time
|
||||
call_outcome: CallOutcome
|
||||
call_outcome: CallOutcome | None
|
||||
conversion_status: ConversionStatus | None
|
||||
product_promote: str | None
|
||||
product_sample: str | None
|
||||
product_promote: list[str]
|
||||
product_sample: list[str]
|
||||
gift: str | None = None
|
||||
remarks: str | None
|
||||
|
||||
|
||||
@@ -54,6 +82,9 @@ class ChemistCallRead(BaseModel):
|
||||
|
||||
class DCRCreate(BaseModel):
|
||||
report_date: date
|
||||
report_type: ReportType = ReportType.ACTUAL
|
||||
#: SELF reports only: which of the Area 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)
|
||||
@@ -65,6 +96,31 @@ class DCRUpdate(BaseModel):
|
||||
order_value: str | None = Field(default=None, max_length=100)
|
||||
|
||||
|
||||
class MissedVisitSet(BaseModel):
|
||||
"""Why one planned doctor OR chemist was not visited (end-of-day report only)."""
|
||||
|
||||
doctor_id: uuid.UUID | None = None
|
||||
chemist_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"]
|
||||
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."""
|
||||
|
||||
@@ -82,6 +138,7 @@ class DCRListItem(BaseModel):
|
||||
|
||||
id: uuid.UUID
|
||||
report_date: date
|
||||
report_type: ReportType
|
||||
status: DCRStatus
|
||||
mr_user_id: uuid.UUID
|
||||
mr_name: str
|
||||
@@ -104,8 +161,12 @@ class DCRRead(BaseModel):
|
||||
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
|
||||
@@ -115,6 +176,8 @@ class DCRRead(BaseModel):
|
||||
approved_at: datetime | None
|
||||
doctor_calls: list[DoctorCallRead]
|
||||
chemist_calls: list[ChemistCallRead]
|
||||
#: End-of-day report only: the approved plan, line by line, with what became of it.
|
||||
planned_visits: list[PlannedVisitRead]
|
||||
|
||||
|
||||
class StatusCounts(BaseModel):
|
||||
|
||||
@@ -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,
|
||||
|
||||
+330
-52
@@ -2,6 +2,22 @@
|
||||
|
||||
Routers never contain rules; repositories never contain rules. This module is the
|
||||
single place a reviewer needs to read to understand what the system enforces.
|
||||
|
||||
Plan / end-of-day rules (P1-P5):
|
||||
P1 A PLAN lists the visits the MR intends to make; it carries no outcomes, and its
|
||||
times are planned times, so they may be later than "now".
|
||||
P2 An ACTUAL (end-of-day) report can be created only once the PLAN for that date is
|
||||
APPROVED by the Area Manager; it is linked to that plan.
|
||||
P3 Every planned doctor/chemist must, before submit, either have a visit recorded on
|
||||
the end-of-day report or a short note saying why it was missed.
|
||||
P4 Recording a visit for a doctor/chemist drops any "missed" note for them.
|
||||
P5 Visits not on the plan may still be added to the end-of-day report.
|
||||
|
||||
Area Manager's own report (S1-S3):
|
||||
S1 An Area Manager files one SELF report per date, for one headquarters they manage;
|
||||
it needs no plan.
|
||||
S2 Its visits may be to any active doctor or chemist of that headquarters.
|
||||
S3 There is no approval: submitting it makes it final (APPROVED, locked, PDF stored).
|
||||
"""
|
||||
|
||||
import uuid
|
||||
@@ -13,30 +29,37 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.approvals.state_machine import assert_editable, assert_transition
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.dependencies import assert_can_view_dcr, visible_hq_ids
|
||||
from app.auth.dependencies import active_hq_ids_for_am, assert_can_view_dcr, visible_hq_ids
|
||||
from app.chemists.models import Chemist, ChemistAssignment
|
||||
from app.common.enums import (
|
||||
FIELD_ROLES,
|
||||
AuditAction,
|
||||
DCRStatus,
|
||||
EntityType,
|
||||
MasterApproval,
|
||||
ReportType,
|
||||
Role,
|
||||
WorkflowAction,
|
||||
FIELD_ROLES,
|
||||
)
|
||||
from app.common.exceptions import (
|
||||
ConflictError,
|
||||
ForbiddenError,
|
||||
NotFoundError,
|
||||
ValidationError,
|
||||
)
|
||||
from app.common.utils import local_now, local_today, snapshot, utcnow
|
||||
from app.config.settings import settings
|
||||
from app.doctors.models import Doctor, DoctorAssignment
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall, MissedVisit
|
||||
from app.reports.repository import DCRRepository
|
||||
from app.reports.schemas import ChemistCallCreate, DCRCreate, DoctorCallCreate
|
||||
from app.reports.schemas import ChemistCallCreate, DCRCreate, DoctorCallCreate, MissedVisitSet
|
||||
from app.reports.serializers import planned_visits
|
||||
from app.users.models import User
|
||||
|
||||
#: Audit fields — the columns worth recording on a state change.
|
||||
_AUDIT_FIELDS = [
|
||||
"report_type",
|
||||
"status",
|
||||
"report_date",
|
||||
"mr_remarks",
|
||||
@@ -49,6 +72,15 @@ _AUDIT_FIELDS = [
|
||||
]
|
||||
|
||||
|
||||
#: Who files (and alone may edit or submit) each type of report.
|
||||
_OWNER_ROLE = {ReportType.PLAN: FIELD_ROLES, ReportType.ACTUAL: FIELD_ROLES, ReportType.SELF: frozenset({Role.AM})}
|
||||
_LABEL = {
|
||||
ReportType.PLAN: "Daily Call Report (Plan)",
|
||||
ReportType.ACTUAL: "Daily Call Report (Done)",
|
||||
ReportType.SELF: "Daily Call Report (Self)",
|
||||
}
|
||||
|
||||
|
||||
def _clean(value: str | None) -> str | None:
|
||||
"""Trimmed text, with blank input stored as NULL."""
|
||||
value = (value or "").strip()
|
||||
@@ -65,13 +97,15 @@ class DCRService:
|
||||
# Validation helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _validate_report_date(self, report_date: date) -> None:
|
||||
def _validate_report_date(self, report_date: date, report_type: ReportType) -> None:
|
||||
today = local_today()
|
||||
# V2 — no future reports.
|
||||
if report_date > today:
|
||||
# V2 — no future reports, except a plan, which may be made PLAN_AHEAD_DAYS ahead.
|
||||
ahead = settings.PLAN_AHEAD_DAYS if report_type == ReportType.PLAN else 0
|
||||
latest = today + timedelta(days=ahead)
|
||||
if report_date > latest:
|
||||
raise ValidationError(
|
||||
"Report date cannot be in the future",
|
||||
{"report_date": report_date.isoformat(), "today": today.isoformat()},
|
||||
f"A plan can be made at most {ahead} days ahead" if ahead else "Report date cannot be in the future",
|
||||
{"report_date": report_date.isoformat(), "latest_allowed": latest.isoformat()},
|
||||
)
|
||||
# V3 / A10 / Q8 — back-dating window.
|
||||
earliest = today - timedelta(days=settings.BACKDATING_WINDOW_DAYS)
|
||||
@@ -91,17 +125,49 @@ class DCRService:
|
||||
{"report_date": report_date.isoformat(), "deadline": deadline.isoformat()},
|
||||
)
|
||||
|
||||
async def _allowed_doctor_ids(self, dcr: DailyCallReport) -> set[uuid.UUID]:
|
||||
"""V6 — an MR's assigned doctors; for a SELF report, those of its headquarters (S2)."""
|
||||
if dcr.report_type != ReportType.SELF:
|
||||
return await self._assigned_doctor_ids(dcr.mr_user_id)
|
||||
stmt = select(Doctor.id).where(
|
||||
Doctor.headquarters_id == dcr.headquarters_id,
|
||||
Doctor.is_active.is_(True),
|
||||
Doctor.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
return set((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
async def _allowed_chemist_ids(self, dcr: DailyCallReport) -> set[uuid.UUID]:
|
||||
if dcr.report_type != ReportType.SELF:
|
||||
return await self._assigned_chemist_ids(dcr.mr_user_id)
|
||||
stmt = select(Chemist.id).where(
|
||||
Chemist.headquarters_id == dcr.headquarters_id,
|
||||
Chemist.is_active.is_(True),
|
||||
Chemist.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
return set((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
async def _assigned_doctor_ids(self, mr_user_id: uuid.UUID) -> set[uuid.UUID]:
|
||||
stmt = select(DoctorAssignment.doctor_id).where(
|
||||
DoctorAssignment.mr_user_id == mr_user_id,
|
||||
DoctorAssignment.effective_to.is_(None),
|
||||
# A doctor the MR added is visitable only once the Area Manager has approved it.
|
||||
stmt = (
|
||||
select(DoctorAssignment.doctor_id)
|
||||
.join(Doctor, Doctor.id == DoctorAssignment.doctor_id)
|
||||
.where(
|
||||
DoctorAssignment.mr_user_id == mr_user_id,
|
||||
DoctorAssignment.effective_to.is_(None),
|
||||
Doctor.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
)
|
||||
return set((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
async def _assigned_chemist_ids(self, mr_user_id: uuid.UUID) -> set[uuid.UUID]:
|
||||
stmt = select(ChemistAssignment.chemist_id).where(
|
||||
ChemistAssignment.mr_user_id == mr_user_id,
|
||||
ChemistAssignment.effective_to.is_(None),
|
||||
stmt = (
|
||||
select(ChemistAssignment.chemist_id)
|
||||
.join(Chemist, Chemist.id == ChemistAssignment.chemist_id)
|
||||
.where(
|
||||
ChemistAssignment.mr_user_id == mr_user_id,
|
||||
ChemistAssignment.effective_to.is_(None),
|
||||
Chemist.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
)
|
||||
return set((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
@@ -111,12 +177,21 @@ class DCRService:
|
||||
# V6 — only doctors currently assigned to this MR may be called.
|
||||
if payload.doctor_id not in assigned:
|
||||
raise ValidationError(
|
||||
"Doctor is not assigned to this MR",
|
||||
"Doctor is not in this report's headquarters"
|
||||
if dcr.report_type == ReportType.SELF
|
||||
else "Doctor is not assigned to this MR",
|
||||
{"doctor_id": str(payload.doctor_id)},
|
||||
)
|
||||
doctor = await self.db.get(Doctor, payload.doctor_id)
|
||||
if doctor is None:
|
||||
raise NotFoundError("Doctor not found", {"doctor_id": str(payload.doctor_id)})
|
||||
# P1 — a planned visit has not happened yet, so it has no outcome.
|
||||
is_plan = dcr.report_type == ReportType.PLAN
|
||||
if not is_plan and payload.call_outcome is None:
|
||||
raise ValidationError(
|
||||
"call_outcome is required for a visit on a Daily Call Report (Done)",
|
||||
{"doctor_id": str(doctor.id)},
|
||||
)
|
||||
# V7 — conversion status is meaningful only for core doctors.
|
||||
if payload.conversion_status is not None and not doctor.is_core_doctor:
|
||||
raise ValidationError(
|
||||
@@ -127,10 +202,11 @@ class DCRService:
|
||||
dcr_id=dcr.id,
|
||||
doctor_id=payload.doctor_id,
|
||||
visit_time=payload.visit_time,
|
||||
call_outcome=payload.call_outcome,
|
||||
conversion_status=payload.conversion_status,
|
||||
product_promote=_clean(payload.product_promote),
|
||||
product_sample=_clean(payload.product_sample),
|
||||
call_outcome=None if is_plan else payload.call_outcome,
|
||||
conversion_status=None if is_plan else payload.conversion_status,
|
||||
product_promote=payload.product_promote,
|
||||
product_sample=payload.product_sample,
|
||||
gift=None if is_plan else payload.gift,
|
||||
remarks=payload.remarks,
|
||||
)
|
||||
|
||||
@@ -139,7 +215,10 @@ class DCRService:
|
||||
) -> ChemistCall:
|
||||
if payload.chemist_id not in assigned:
|
||||
raise ValidationError(
|
||||
"Chemist is not assigned to this MR", {"chemist_id": str(payload.chemist_id)}
|
||||
"Chemist is not in this report's headquarters"
|
||||
if dcr.report_type == ReportType.SELF
|
||||
else "Chemist is not assigned to this MR",
|
||||
{"chemist_id": str(payload.chemist_id)},
|
||||
)
|
||||
if await self.db.get(Chemist, payload.chemist_id) is None:
|
||||
raise NotFoundError("Chemist not found", {"chemist_id": str(payload.chemist_id)})
|
||||
@@ -158,30 +237,47 @@ class DCRService:
|
||||
async def create(
|
||||
self, data: DCRCreate, actor: User, ip: str | None = None
|
||||
) -> DailyCallReport:
|
||||
if actor.role != Role.MR or actor.headquarters_id is None:
|
||||
if data.report_type == ReportType.SELF:
|
||||
if actor.role != Role.AM:
|
||||
raise ForbiddenError("Only an Area Manager files a Daily Call Report (Self)")
|
||||
headquarters_id = await self._self_report_hq(actor, data.headquarters_id)
|
||||
elif actor.role not in FIELD_ROLES:
|
||||
raise ForbiddenError("Only an MR files a Daily Call Report (Plan) or (Done)")
|
||||
elif actor.headquarters_id is None:
|
||||
raise ValidationError("Only an MR with a headquarters can create a report")
|
||||
else:
|
||||
headquarters_id = actor.headquarters_id # snapshot (A1)
|
||||
|
||||
self._validate_report_date(data.report_date)
|
||||
self._validate_report_date(data.report_date, data.report_type)
|
||||
|
||||
# V1 pre-check for a friendly 409; the unique constraint below is the guarantee.
|
||||
if await self.repo.get_for_date(actor.id, data.report_date):
|
||||
if await self.repo.get_for_date(actor.id, data.report_date, data.report_type):
|
||||
raise ConflictError(
|
||||
"A report already exists for this date",
|
||||
{"report_date": data.report_date.isoformat()},
|
||||
f"A {_LABEL[data.report_type]} already exists for this date",
|
||||
{
|
||||
"report_date": data.report_date.isoformat(),
|
||||
"report_type": data.report_type.value,
|
||||
},
|
||||
)
|
||||
|
||||
plan_id = None
|
||||
if data.report_type == ReportType.ACTUAL:
|
||||
plan_id = (await self._approved_plan(actor.id, data.report_date)).id
|
||||
|
||||
dcr = DailyCallReport(
|
||||
id=uuid.uuid4(),
|
||||
mr_user_id=actor.id,
|
||||
headquarters_id=actor.headquarters_id, # snapshot (A1)
|
||||
headquarters_id=headquarters_id,
|
||||
report_date=data.report_date,
|
||||
report_type=data.report_type,
|
||||
plan_id=plan_id,
|
||||
status=DCRStatus.DRAFT,
|
||||
mr_remarks=data.mr_remarks,
|
||||
)
|
||||
self.db.add(dcr)
|
||||
|
||||
doctor_ids = await self._assigned_doctor_ids(actor.id)
|
||||
chemist_ids = await self._assigned_chemist_ids(actor.id)
|
||||
doctor_ids = await self._allowed_doctor_ids(dcr)
|
||||
chemist_ids = await self._allowed_chemist_ids(dcr)
|
||||
|
||||
seen_doctors: set[uuid.UUID] = set()
|
||||
for payload in data.doctor_calls:
|
||||
@@ -217,7 +313,8 @@ class DCRService:
|
||||
except IntegrityError as exc:
|
||||
await self.db.rollback()
|
||||
raise ConflictError(
|
||||
"A report already exists for this MR and date", {"cause": "uq_dcr_mr_date"}
|
||||
"A report of this type already exists for this MR and date",
|
||||
{"cause": "uq_dcr_mr_date_type"},
|
||||
) from exc
|
||||
|
||||
return await self.repo.get_full(dcr.id)
|
||||
@@ -247,7 +344,7 @@ class DCRService:
|
||||
self, dcr_id: uuid.UUID, payload: DoctorCallCreate, actor: User, ip: str | None = None
|
||||
) -> DailyCallReport:
|
||||
dcr = await self._get_owned_editable(dcr_id, actor)
|
||||
assigned = await self._assigned_doctor_ids(actor.id)
|
||||
assigned = await self._allowed_doctor_ids(dcr)
|
||||
call = await self._build_doctor_call(dcr, payload, assigned)
|
||||
self._validate_visit_time(dcr, payload.visit_time)
|
||||
self.db.add(call)
|
||||
@@ -264,6 +361,7 @@ class DCRService:
|
||||
{"doctor_id": str(payload.doctor_id), "cause": "uq_call_dcr_doctor"},
|
||||
) from exc
|
||||
|
||||
await self._drop_missed_note(dcr, doctor_id=payload.doctor_id)
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DCR,
|
||||
@@ -279,7 +377,7 @@ class DCRService:
|
||||
self, dcr_id: uuid.UUID, payload: ChemistCallCreate, actor: User, ip: str | None = None
|
||||
) -> DailyCallReport:
|
||||
dcr = await self._get_owned_editable(dcr_id, actor)
|
||||
assigned = await self._assigned_chemist_ids(actor.id)
|
||||
assigned = await self._allowed_chemist_ids(dcr)
|
||||
call = await self._build_chemist_call(dcr, payload, assigned)
|
||||
self._validate_visit_time(dcr, payload.visit_time)
|
||||
self.db.add(call)
|
||||
@@ -293,6 +391,7 @@ class DCRService:
|
||||
{"chemist_id": str(payload.chemist_id), "cause": "uq_call_dcr_chemist"},
|
||||
) from exc
|
||||
|
||||
await self._drop_missed_note(dcr, chemist_id=payload.chemist_id)
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DCR,
|
||||
@@ -305,8 +404,8 @@ class DCRService:
|
||||
return await self.repo.get_full(dcr.id)
|
||||
|
||||
def _validate_visit_time(self, dcr: DailyCallReport, visit_time) -> None:
|
||||
"""V11 — a visit time on today's report cannot be in the future."""
|
||||
if dcr.report_date == local_today():
|
||||
"""V11 — a visit time on today's report cannot be in the future (P1: plans excepted)."""
|
||||
if dcr.report_type != ReportType.PLAN and dcr.report_date == local_today():
|
||||
# Visit times are entered in the company's local time (APP_TIMEZONE), so they
|
||||
# must be compared with local "now" — not the server's UTC clock.
|
||||
if visit_time > local_now().time() and settings.ENVIRONMENT == "production":
|
||||
@@ -333,6 +432,79 @@ class DCRService:
|
||||
await self.db.commit()
|
||||
return await self.repo.get_full(dcr.id)
|
||||
|
||||
async def set_missed_reason(
|
||||
self, dcr_id: uuid.UUID, payload: MissedVisitSet, actor: User, ip: str | None = None
|
||||
) -> DailyCallReport:
|
||||
"""P3 — record (or reword) why a planned doctor/chemist was not visited."""
|
||||
dcr = await self._get_owned_editable(dcr_id, actor)
|
||||
if dcr.report_type != ReportType.ACTUAL or dcr.plan is None:
|
||||
raise ValidationError("Missed-visit notes belong on a Daily Call Report (Done)")
|
||||
if (payload.doctor_id is None) == (payload.chemist_id is None):
|
||||
raise ValidationError("Give exactly one of doctor_id or chemist_id")
|
||||
reason = _clean(payload.reason)
|
||||
if reason is None:
|
||||
raise ValidationError("Say briefly why the visit was missed")
|
||||
|
||||
target_id = payload.doctor_id or payload.chemist_id
|
||||
line = next((p for p in planned_visits(dcr) if p.target_id == target_id), None)
|
||||
if line is None:
|
||||
raise ValidationError(
|
||||
"This doctor or chemist is not on the plan", {"target_id": str(target_id)}
|
||||
)
|
||||
if line.status == "VISITED":
|
||||
raise ConflictError(
|
||||
"A visit is already recorded for this doctor or chemist; remove it first",
|
||||
{"target_id": str(target_id)},
|
||||
)
|
||||
|
||||
note = next(
|
||||
(m for m in dcr.missed_visits if (m.doctor_id or m.chemist_id) == target_id), None
|
||||
)
|
||||
before = {"reason": note.reason} if note else None
|
||||
if note is None:
|
||||
self.db.add(
|
||||
MissedVisit(
|
||||
dcr_id=dcr.id,
|
||||
doctor_id=payload.doctor_id,
|
||||
chemist_id=payload.chemist_id,
|
||||
reason=reason,
|
||||
)
|
||||
)
|
||||
else:
|
||||
note.reason = reason
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DCR,
|
||||
entity_id=dcr.id,
|
||||
action=AuditAction.UPDATE,
|
||||
before=before,
|
||||
after={"missed_visit": str(target_id), "reason": reason},
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
return await self.repo.get_full(dcr.id)
|
||||
|
||||
async def clear_missed_reason(
|
||||
self, dcr_id: uuid.UUID, missed_id: uuid.UUID, actor: User, ip: str | None = None
|
||||
) -> DailyCallReport:
|
||||
dcr = await self._get_owned_editable(dcr_id, actor)
|
||||
note = next((m for m in dcr.missed_visits if m.id == missed_id), None)
|
||||
if note is None:
|
||||
raise NotFoundError(
|
||||
"Missed-visit note not found on this report", {"id": str(missed_id)}
|
||||
)
|
||||
await self.db.delete(note)
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DCR,
|
||||
entity_id=dcr.id,
|
||||
action=AuditAction.DELETE,
|
||||
before={"missed_visit": str(note.doctor_id or note.chemist_id), "reason": note.reason},
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
return await self.repo.get_full(dcr.id)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Workflow transitions
|
||||
# ------------------------------------------------------------------
|
||||
@@ -344,19 +516,18 @@ class DCRService:
|
||||
if dcr is None:
|
||||
raise NotFoundError("Report not found", {"id": str(dcr_id)})
|
||||
await assert_can_view_dcr(self.db, actor, dcr)
|
||||
if dcr.mr_user_id != actor.id:
|
||||
from app.common.exceptions import ForbiddenError
|
||||
if dcr.mr_user_id != actor.id or actor.role not in _OWNER_ROLE[dcr.report_type]:
|
||||
raise ForbiddenError("Only the report's owner can submit it")
|
||||
|
||||
raise ForbiddenError("Only the owning MR can submit this report")
|
||||
if dcr.report_type == ReportType.SELF:
|
||||
return await self._finalise_self_report(dcr, actor, ip)
|
||||
|
||||
if dcr.status == DCRStatus.PENDING_AM:
|
||||
return dcr # already submitted (a double click): nothing more to do
|
||||
|
||||
transition = assert_transition(dcr.status, WorkflowAction.SUBMIT, actor.role)
|
||||
|
||||
# V4 — at least one doctor call is required to submit.
|
||||
if not dcr.doctor_calls:
|
||||
raise ValidationError("A report must contain at least one doctor call to submit")
|
||||
self._check_submit_contents(dcr)
|
||||
self._check_submission_deadline(dcr.report_date)
|
||||
|
||||
before = snapshot(dcr, _AUDIT_FIELDS)
|
||||
@@ -378,6 +549,36 @@ class DCRService:
|
||||
await self.db.commit()
|
||||
return await self.repo.get_full(dcr.id)
|
||||
|
||||
async def _finalise_self_report(
|
||||
self, dcr: DailyCallReport, actor: User, ip: str | None
|
||||
) -> DailyCallReport:
|
||||
"""S3 — an Area Manager's own report is final once submitted; nobody approves it."""
|
||||
if dcr.status == DCRStatus.APPROVED:
|
||||
return dcr # already submitted (a double click)
|
||||
assert_editable(dcr.status)
|
||||
self._check_submit_contents(dcr)
|
||||
self._check_submission_deadline(dcr.report_date)
|
||||
|
||||
before = snapshot(dcr, _AUDIT_FIELDS)
|
||||
dcr.status = DCRStatus.APPROVED
|
||||
dcr.submitted_at = dcr.approved_at = utcnow()
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DCR,
|
||||
entity_id=dcr.id,
|
||||
action=AuditAction.SUBMIT,
|
||||
before=before,
|
||||
after=snapshot(dcr, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
|
||||
from app.pdf.service import PDFService
|
||||
|
||||
full = await self.repo.get_full(dcr.id)
|
||||
dcr.pdf_path = await PDFService(self.db).render_and_store(full, approver=actor)
|
||||
await self.db.commit()
|
||||
return await self.repo.get_full(dcr.id)
|
||||
|
||||
async def return_report(
|
||||
self, dcr_id: uuid.UUID, reason: str, actor: User, ip: str | None = None
|
||||
) -> DailyCallReport:
|
||||
@@ -447,16 +648,33 @@ class DCRService:
|
||||
await assert_can_view_dcr(self.db, actor, dcr)
|
||||
return dcr
|
||||
|
||||
async def list_for(self, actor: User, **filters) -> list[DailyCallReport]:
|
||||
if actor.role == Role.MR:
|
||||
async def _hq_scope(
|
||||
self, actor: User, headquarters_id: uuid.UUID | None
|
||||
) -> set[uuid.UUID] | None:
|
||||
"""The headquarters a manager's lists cover: all they may see, or the one they chose —
|
||||
narrowed, never widened (one outside their scope gives nothing)."""
|
||||
allowed = await visible_hq_ids(self.db, actor)
|
||||
if headquarters_id is None:
|
||||
return allowed
|
||||
return {headquarters_id} if allowed is None or headquarters_id in allowed else set()
|
||||
|
||||
async def list_for(
|
||||
self, actor: User, headquarters_id: uuid.UUID | None = None, **filters
|
||||
) -> list[DailyCallReport]:
|
||||
# An Area Manager's SELF list is their own reports, not their headquarters'.
|
||||
if actor.role in FIELD_ROLES or (
|
||||
actor.role == Role.AM and filters.get("report_type") == ReportType.SELF
|
||||
):
|
||||
return await self.repo.list_scoped(mr_user_id=actor.id, **filters)
|
||||
hq_ids = await visible_hq_ids(self.db, actor)
|
||||
hq_ids = await self._hq_scope(actor, headquarters_id)
|
||||
return await self.repo.list_scoped(hq_ids=hq_ids, **filters)
|
||||
|
||||
async def counts_for(self, actor: User) -> dict[str, int]:
|
||||
if actor.role == Role.MR:
|
||||
async def counts_for(
|
||||
self, actor: User, headquarters_id: uuid.UUID | None = None
|
||||
) -> dict[str, int]:
|
||||
if actor.role in FIELD_ROLES:
|
||||
return await self.repo.status_counts(mr_user_id=actor.id)
|
||||
return await self.repo.status_counts(hq_ids=await visible_hq_ids(self.db, actor))
|
||||
return await self.repo.status_counts(hq_ids=await self._hq_scope(actor, headquarters_id))
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Internals
|
||||
@@ -468,13 +686,22 @@ class DCRService:
|
||||
if dcr is None:
|
||||
raise NotFoundError("Report not found", {"id": str(dcr_id)})
|
||||
await assert_can_view_dcr(self.db, actor, dcr)
|
||||
if actor.role != Role.MR or dcr.mr_user_id != actor.id:
|
||||
from app.common.exceptions import ForbiddenError
|
||||
|
||||
raise ForbiddenError("Only the owning MR can modify report data")
|
||||
if dcr.mr_user_id != actor.id or actor.role not in _OWNER_ROLE[dcr.report_type]:
|
||||
raise ForbiddenError("Only the report's owner can modify report data")
|
||||
assert_editable(dcr.status)
|
||||
return dcr
|
||||
|
||||
async def _self_report_hq(self, actor: User, headquarters_id: uuid.UUID | None) -> uuid.UUID:
|
||||
"""S1 — one of the headquarters this Area Manager manages; implied when there is one."""
|
||||
managed = await active_hq_ids_for_am(self.db, actor.id)
|
||||
if headquarters_id is None and len(managed) == 1:
|
||||
return next(iter(managed))
|
||||
if headquarters_id is None:
|
||||
raise ValidationError("Choose the headquarters this report is for")
|
||||
if headquarters_id not in managed:
|
||||
raise ForbiddenError("You do not manage this headquarters")
|
||||
return headquarters_id
|
||||
|
||||
async def _get_in_scope(self, dcr_id: uuid.UUID, actor: User) -> DailyCallReport:
|
||||
dcr = await self.repo.get_full(dcr_id)
|
||||
if dcr is None:
|
||||
@@ -482,11 +709,62 @@ class DCRService:
|
||||
await assert_can_view_dcr(self.db, actor, dcr)
|
||||
return dcr
|
||||
|
||||
def _check_submit_contents(self, dcr: DailyCallReport) -> None:
|
||||
if dcr.report_type == ReportType.PLAN:
|
||||
if not dcr.doctor_calls and not dcr.chemist_calls:
|
||||
raise ValidationError("Plan at least one doctor or chemist visit to submit")
|
||||
return
|
||||
# P3 — every planned visit is either recorded or explained.
|
||||
pending = [p for p in planned_visits(dcr) if p.status == "PENDING"]
|
||||
if pending:
|
||||
raise ValidationError(
|
||||
"Record a visit or add a missed-visit note for every planned doctor and chemist",
|
||||
{"pending": [p.name for p in pending]},
|
||||
)
|
||||
# V4 — at least one doctor call, unless the planned visits were all explained.
|
||||
if not dcr.doctor_calls and not dcr.missed_visits:
|
||||
raise ValidationError("A report must contain at least one doctor call to submit")
|
||||
|
||||
async def _approved_plan(self, mr_user_id: uuid.UUID, report_date: date) -> DailyCallReport:
|
||||
"""P2 — the end-of-day report follows an Area-Manager-approved plan."""
|
||||
plan = await self.repo.get_for_date(mr_user_id, report_date, ReportType.PLAN)
|
||||
if plan is None:
|
||||
raise ValidationError(
|
||||
"Create your Daily Call Report (Plan) for this date first; "
|
||||
"the Done report follows it",
|
||||
{"report_date": report_date.isoformat()},
|
||||
)
|
||||
if plan.status != DCRStatus.APPROVED:
|
||||
raise ValidationError(
|
||||
"Your Daily Call Report (Plan) for this date must be approved "
|
||||
"by the Area Manager first",
|
||||
{"report_date": report_date.isoformat(), "plan_status": plan.status.value},
|
||||
)
|
||||
return plan
|
||||
|
||||
async def _drop_missed_note(
|
||||
self,
|
||||
dcr: DailyCallReport,
|
||||
doctor_id: uuid.UUID | None = None,
|
||||
chemist_id: uuid.UUID | None = None,
|
||||
) -> None:
|
||||
"""P4 — a recorded visit replaces any note saying it was missed."""
|
||||
for note in dcr.missed_visits:
|
||||
if (doctor_id and note.doctor_id == doctor_id) or (
|
||||
chemist_id and note.chemist_id == chemist_id
|
||||
):
|
||||
await self.db.delete(note)
|
||||
|
||||
async def _current_am_for_hq(self, headquarters_id: uuid.UUID) -> uuid.UUID | None:
|
||||
from app.organization.models import AMHeadquarters
|
||||
|
||||
stmt = select(AMHeadquarters.area_manager_user_id).where(
|
||||
AMHeadquarters.headquarters_id == headquarters_id,
|
||||
AMHeadquarters.effective_to.is_(None),
|
||||
# Recorded only; any Area Manager of the headquarters may act on the report.
|
||||
stmt = (
|
||||
select(AMHeadquarters.area_manager_user_id)
|
||||
.where(
|
||||
AMHeadquarters.headquarters_id == headquarters_id,
|
||||
AMHeadquarters.effective_to.is_(None),
|
||||
)
|
||||
.order_by(AMHeadquarters.effective_from, AMHeadquarters.created_at)
|
||||
)
|
||||
return (await self.db.execute(stmt)).scalars().first()
|
||||
|
||||
+37
-2
@@ -22,8 +22,8 @@ class User(Base, UUIDMixin, TimestampMixin):
|
||||
UniqueConstraint("company_id", "employee_code", name="uq_user_company_empcode"),
|
||||
# An MR must have a headquarters; nobody else may have one (R4, R8).
|
||||
CheckConstraint(
|
||||
"(role = 'MR' AND headquarters_id IS NOT NULL) "
|
||||
"OR (role <> 'MR' AND headquarters_id IS NULL)",
|
||||
"(role IN ('MR', 'SFE') AND headquarters_id IS NOT NULL) "
|
||||
"OR (role NOT IN ('MR', 'SFE') AND headquarters_id IS NULL)",
|
||||
name="ck_user_mr_requires_hq",
|
||||
),
|
||||
)
|
||||
@@ -45,6 +45,41 @@ class User(Base, UUIDMixin, TimestampMixin):
|
||||
)
|
||||
#: Checked on EVERY request, not only at login (SEC-5).
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
#: The one protected owner account. Nobody — not even itself — can edit, deactivate,
|
||||
#: re-role or delete it through the API; a DB trigger backs that up. It acts as an
|
||||
#: ADMIN everywhere (role is ADMIN), and is created only by `scripts/create_superadmin.py`.
|
||||
is_superadmin: Mapped[bool] = mapped_column(
|
||||
Boolean, default=False, server_default="false", nullable=False
|
||||
)
|
||||
failed_login_attempts: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
|
||||
headquarters: Mapped["Headquarters | None"] = relationship() # noqa: F821
|
||||
#: SFE only — the other headquarters (districts) covered besides the primary one.
|
||||
extra_headquarters: Mapped[list["UserExtraHeadquarters"]] = relationship(
|
||||
lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_headquarter_ids(self) -> list[uuid.UUID]:
|
||||
return [e.headquarters_id for e in self.extra_headquarters]
|
||||
|
||||
@property
|
||||
def covered_headquarter_ids(self) -> set[uuid.UUID]:
|
||||
"""Every headquarters this field user works in: the primary one plus the extra ones."""
|
||||
ids = set(self.extra_headquarter_ids)
|
||||
if self.headquarters_id:
|
||||
ids.add(self.headquarters_id)
|
||||
return ids
|
||||
|
||||
|
||||
class UserExtraHeadquarters(Base):
|
||||
"""An SFE covers more than one district: each extra headquarters is one row here."""
|
||||
|
||||
__tablename__ = "user_extra_headquarters"
|
||||
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="CASCADE"), primary_key=True
|
||||
)
|
||||
headquarters_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("headquarters.id", ondelete="RESTRICT"), primary_key=True
|
||||
)
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
"""One person's whole picture for the General Manager and Admin: who they are, where they
|
||||
work, who they report to, their doctors and chemists, their reports and their company forms."""
|
||||
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.chemists.models import Chemist, ChemistAssignment
|
||||
from app.common.enums import FIELD_ROLES, ReportType, Role
|
||||
from app.common.exceptions import NotFoundError
|
||||
from app.csv_data.config import CSV_LABELS, FORM_ORDER, TABLES
|
||||
from app.csv_data.models import FormApproval
|
||||
from app.doctors.models import Doctor, DoctorAssignment
|
||||
from app.organization.models import AMHeadquarters, Headquarters
|
||||
from app.reports.models import DailyCallReport
|
||||
from app.reports.repository import DCRRepository
|
||||
from app.reports.serializers import to_list_item
|
||||
from app.users.models import User
|
||||
|
||||
|
||||
def _hq(h: Headquarters, main: bool = False) -> dict[str, Any]:
|
||||
return {"id": str(h.id), "code": h.code, "name": h.name, "district": h.district, "state": h.state, "main": main}
|
||||
|
||||
|
||||
def _person(u: User) -> dict[str, Any]:
|
||||
return {"id": str(u.id), "full_name": u.full_name, "email": u.email, "role": u.role.value, "is_active": u.is_active}
|
||||
|
||||
|
||||
async def _report_counts(db: AsyncSession, **where: Any) -> dict[str, dict[str, int]]:
|
||||
stmt = select(DailyCallReport.report_type, DailyCallReport.status, func.count()).group_by(
|
||||
DailyCallReport.report_type, DailyCallReport.status
|
||||
)
|
||||
if "mr_user_id" in where:
|
||||
stmt = stmt.where(DailyCallReport.mr_user_id == where["mr_user_id"])
|
||||
if "hq_ids" in where:
|
||||
stmt = stmt.where(DailyCallReport.headquarters_id.in_(where["hq_ids"] or [None]))
|
||||
out: dict[str, dict[str, int]] = {}
|
||||
for kind, status, n in (await db.execute(stmt)).all():
|
||||
out.setdefault(str(getattr(kind, "value", kind)), {})[str(getattr(status, "value", status))] = n
|
||||
return out
|
||||
|
||||
|
||||
async def _forms(db: AsyncSession, user: User) -> list[dict[str, Any]]:
|
||||
from app.csv_data.approval import APPROVAL_TABLES
|
||||
from app.csv_data.router import HIDDEN_TABLES, PER_MR_PRODUCT_TABLES
|
||||
|
||||
owned = APPROVAL_TABLES | PER_MR_PRODUCT_TABLES
|
||||
approvals = {
|
||||
a.table_name: a.status
|
||||
for a in (await db.execute(select(FormApproval).where(FormApproval.owner_user_id == user.id))).scalars()
|
||||
}
|
||||
out = []
|
||||
for name in FORM_ORDER:
|
||||
if name in HIDDEN_TABLES:
|
||||
continue
|
||||
columns = TABLES[name]
|
||||
if name in owned:
|
||||
where, params = "owner_user_id = :v", {"v": user.id}
|
||||
else:
|
||||
who = "name" if "name" in columns else "employee_name"
|
||||
where, params = f"lower(btrim(COALESCE({who}, ''))) = :v", {"v": user.full_name.strip().lower()}
|
||||
rows = (await db.execute(text(f'SELECT COUNT(*) FROM "{name}" WHERE {where}'), params)).scalar_one()
|
||||
months: list[str] = []
|
||||
if "month" in columns:
|
||||
found = await db.execute(
|
||||
text(f'SELECT DISTINCT btrim(month) FROM "{name}" WHERE {where} AND btrim(COALESCE(month, \'\')) <> \'\''),
|
||||
params,
|
||||
)
|
||||
months = sorted(m for (m,) in found.all())
|
||||
out.append(
|
||||
{
|
||||
"table": name,
|
||||
"label": CSV_LABELS[name],
|
||||
"rows": rows,
|
||||
"months": months,
|
||||
"approval_status": approvals.get(name, "draft") if name in APPROVAL_TABLES else None,
|
||||
}
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
async def user_overview(db: AsyncSession, actor: User, user_id: uuid.UUID) -> dict[str, Any]:
|
||||
user = await db.get(User, user_id)
|
||||
if user is None or user.company_id != actor.company_id:
|
||||
raise NotFoundError("User not found")
|
||||
repo = DCRRepository(db)
|
||||
data: dict[str, Any] = {
|
||||
"user": {
|
||||
"id": str(user.id), "full_name": user.full_name, "email": user.email,
|
||||
"employee_code": user.employee_code, "phone": user.phone, "role": user.role.value,
|
||||
"is_active": user.is_active, "is_superadmin": user.is_superadmin,
|
||||
},
|
||||
"headquarters": [], "reporting_managers": [], "doctors": [], "chemists": [],
|
||||
"reports": {"counts": {}, "recent": []}, "forms": [], "team": [], "own_reports": None,
|
||||
}
|
||||
|
||||
if user.role in FIELD_ROLES:
|
||||
covered = (
|
||||
await db.execute(select(Headquarters).where(Headquarters.id.in_(user.covered_headquarter_ids or [None])))
|
||||
).scalars().all()
|
||||
data["headquarters"] = sorted((_hq(h, h.id == user.headquarters_id) for h in covered), key=lambda h: not h["main"])
|
||||
if user.headquarters_id:
|
||||
managers = (
|
||||
await db.execute(
|
||||
select(User)
|
||||
.join(AMHeadquarters, AMHeadquarters.area_manager_user_id == User.id)
|
||||
.where(AMHeadquarters.headquarters_id == user.headquarters_id, AMHeadquarters.effective_to.is_(None))
|
||||
.order_by(User.full_name)
|
||||
)
|
||||
).scalars().all()
|
||||
data["reporting_managers"] = [_person(m) for m in managers]
|
||||
doctors = await db.execute(
|
||||
select(Doctor, Headquarters.name)
|
||||
.join(DoctorAssignment, DoctorAssignment.doctor_id == Doctor.id)
|
||||
.join(Headquarters, Headquarters.id == Doctor.headquarters_id)
|
||||
.where(DoctorAssignment.mr_user_id == user.id, DoctorAssignment.effective_to.is_(None))
|
||||
.order_by(Doctor.name)
|
||||
)
|
||||
data["doctors"] = [
|
||||
{"id": str(d.id), "name": d.name, "speciality": d.speciality, "clinic_address": d.clinic_address,
|
||||
"headquarters_name": hq, "is_core_doctor": d.is_core_doctor, "is_active": d.is_active}
|
||||
for d, hq in doctors.all()
|
||||
]
|
||||
chemists = await db.execute(
|
||||
select(Chemist, Headquarters.name)
|
||||
.join(ChemistAssignment, ChemistAssignment.chemist_id == Chemist.id)
|
||||
.join(Headquarters, Headquarters.id == Chemist.headquarters_id)
|
||||
.where(ChemistAssignment.mr_user_id == user.id, ChemistAssignment.effective_to.is_(None))
|
||||
.order_by(Chemist.name)
|
||||
)
|
||||
data["chemists"] = [
|
||||
{"id": str(c.id), "name": c.name, "shop_address": c.shop_address, "headquarters_name": hq, "is_active": c.is_active}
|
||||
for c, hq in chemists.all()
|
||||
]
|
||||
data["reports"]["counts"] = await _report_counts(db, mr_user_id=user.id)
|
||||
recent = await repo.list_scoped(mr_user_id=user.id, limit=15)
|
||||
data["reports"]["recent"] = [to_list_item(r).model_dump(mode="json") for r in recent]
|
||||
data["forms"] = await _forms(db, user)
|
||||
|
||||
elif user.role == Role.AM:
|
||||
managed = (
|
||||
await db.execute(
|
||||
select(Headquarters)
|
||||
.join(AMHeadquarters, AMHeadquarters.headquarters_id == Headquarters.id)
|
||||
.where(AMHeadquarters.area_manager_user_id == user.id, AMHeadquarters.effective_to.is_(None))
|
||||
.order_by(Headquarters.name)
|
||||
)
|
||||
).scalars().all()
|
||||
ids = {h.id for h in managed}
|
||||
team = (
|
||||
await db.execute(
|
||||
select(User).where(User.role.in_(FIELD_ROLES), User.headquarters_id.in_(ids or [None])).order_by(User.full_name)
|
||||
)
|
||||
).scalars().all()
|
||||
counts_by_hq = {
|
||||
hq_id: n for hq_id, n in (
|
||||
await db.execute(select(User.headquarters_id, func.count()).where(User.role.in_(FIELD_ROLES), User.is_active.is_(True)).group_by(User.headquarters_id))
|
||||
).all()
|
||||
}
|
||||
data["headquarters"] = [{**_hq(h), "field_staff": counts_by_hq.get(h.id, 0)} for h in managed]
|
||||
data["team"] = [{**_person(t), "headquarters": next((h.code for h in managed if h.id == t.headquarters_id), None)} for t in team]
|
||||
data["reports"]["counts"] = await _report_counts(db, hq_ids=ids)
|
||||
data["reports"]["recent"] = [to_list_item(r).model_dump(mode="json") for r in await repo.list_scoped(hq_ids=ids, limit=15)]
|
||||
own = await repo.list_scoped(mr_user_id=user.id, report_type=ReportType.SELF, limit=10)
|
||||
data["own_reports"] = [to_list_item(r).model_dump(mode="json") for r in own]
|
||||
return data
|
||||
@@ -6,6 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.auth.dependencies import client_ip, get_current_user, require_role
|
||||
from app.common.enums import Role
|
||||
from app.database.session import get_db
|
||||
from app.users import overview
|
||||
from app.users.models import User
|
||||
from app.users.schemas import MyProfile, UserCreate, UserRead, UserUpdate
|
||||
from app.users.service import UserService
|
||||
@@ -20,6 +21,16 @@ async def my_profile(
|
||||
return await UserService(db).my_profile(current_user)
|
||||
|
||||
|
||||
@router.get("/{user_id}/overview")
|
||||
async def user_overview(
|
||||
user_id: uuid.UUID,
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.GM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> dict:
|
||||
"""The whole picture of one person, for the General Manager and Admin."""
|
||||
return await overview.user_overview(db, current_user, user_id)
|
||||
|
||||
|
||||
@router.get("", response_model=list[UserRead])
|
||||
async def list_users(
|
||||
role: Role | None = None,
|
||||
|
||||
+14
-3
@@ -2,7 +2,7 @@ import uuid
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field, model_validator
|
||||
|
||||
from app.common.enums import Role
|
||||
from app.common.enums import Role, FIELD_ROLES
|
||||
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
@@ -14,12 +14,16 @@ class UserCreate(BaseModel):
|
||||
phone: str | None = Field(default=None, max_length=20)
|
||||
#: Required for MR, forbidden for every other role (R4/R8).
|
||||
headquarters_id: uuid.UUID | None = None
|
||||
#: SFE only: the other districts they cover besides `headquarters_id`.
|
||||
extra_headquarter_ids: list[uuid.UUID] = []
|
||||
|
||||
@model_validator(mode="after")
|
||||
def check_hq_matches_role(self):
|
||||
if self.role == Role.MR and self.headquarters_id is None:
|
||||
if self.extra_headquarter_ids and self.role != Role.SFE:
|
||||
raise ValueError("extra_headquarter_ids may only be set for an SFE")
|
||||
if self.role in FIELD_ROLES and self.headquarters_id is None:
|
||||
raise ValueError("headquarters_id is required for an MR")
|
||||
if self.role != Role.MR and self.headquarters_id is not None:
|
||||
if self.role not in FIELD_ROLES and self.headquarters_id is not None:
|
||||
raise ValueError("headquarters_id may only be set for an MR")
|
||||
return self
|
||||
|
||||
@@ -32,7 +36,12 @@ class UserUpdate(BaseModel):
|
||||
#: A new password. Signs the user out everywhere and clears a lockout.
|
||||
password: str | None = Field(default=None, min_length=8, max_length=128)
|
||||
phone: str | None = Field(default=None, max_length=20)
|
||||
#: Admin-only. Moving to MR needs `headquarters_id`; leaving MR or AM ends that role's
|
||||
#: assignments, and the user is signed out so they sign in again with the new role.
|
||||
role: Role | None = None
|
||||
headquarters_id: uuid.UUID | None = None
|
||||
#: SFE only. The full list of extra districts; omit to leave it unchanged.
|
||||
extra_headquarter_ids: list[uuid.UUID] | None = None
|
||||
is_active: bool | None = None
|
||||
|
||||
|
||||
@@ -47,6 +56,8 @@ class UserRead(BaseModel):
|
||||
phone: str | None
|
||||
headquarters_id: uuid.UUID | None
|
||||
is_active: bool
|
||||
is_superadmin: bool = False
|
||||
extra_headquarter_ids: list[uuid.UUID] = []
|
||||
|
||||
|
||||
class MyProfile(BaseModel):
|
||||
|
||||
+95
-11
@@ -1,17 +1,25 @@
|
||||
import uuid
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.security import hash_password
|
||||
from app.auth.service import AuthService
|
||||
from app.common.enums import AuditAction, EntityType, Role
|
||||
from app.common.exceptions import ConflictError, NotFoundError, ValidationError
|
||||
from app.chemists.models import ChemistAssignment
|
||||
from app.common.enums import AuditAction, EntityType, Role, FIELD_ROLES
|
||||
from app.common.exceptions import (
|
||||
ConflictError,
|
||||
ForbiddenError,
|
||||
NotFoundError,
|
||||
ValidationError,
|
||||
)
|
||||
from app.common.utils import snapshot
|
||||
from app.doctors.models import DoctorAssignment
|
||||
from app.organization.models import AMHeadquarters, Headquarters
|
||||
from app.users.models import User
|
||||
from app.users.models import User, UserExtraHeadquarters
|
||||
from app.users.schemas import MyProfile, UserCreate, UserUpdate
|
||||
|
||||
_AUDIT_FIELDS = ["email", "full_name", "employee_code", "role", "headquarters_id", "is_active"]
|
||||
@@ -30,7 +38,9 @@ class UserService:
|
||||
if hq.company_id != actor.company_id:
|
||||
raise ValidationError("Headquarters belongs to another company")
|
||||
|
||||
extras = await self._extra_headquarters(data.extra_headquarter_ids, data.headquarters_id, actor)
|
||||
user = User(
|
||||
extra_headquarters=[UserExtraHeadquarters(headquarters_id=h) for h in extras],
|
||||
company_id=actor.company_id,
|
||||
email=str(data.email).lower(),
|
||||
password_hash=hash_password(data.password),
|
||||
@@ -58,6 +68,17 @@ class UserService:
|
||||
await self.db.commit()
|
||||
return user
|
||||
|
||||
async def _extra_headquarters(
|
||||
self, ids: list[uuid.UUID], primary: uuid.UUID | None, actor: User
|
||||
) -> list[uuid.UUID]:
|
||||
"""The extra districts an SFE covers: each must exist in this company; the primary one and repeats are dropped."""
|
||||
wanted = list(dict.fromkeys(h for h in ids if h != primary))
|
||||
for hq_id in wanted:
|
||||
hq = await self.db.get(Headquarters, hq_id)
|
||||
if hq is None or hq.company_id != actor.company_id:
|
||||
raise NotFoundError("Headquarters not found")
|
||||
return wanted
|
||||
|
||||
async def update(
|
||||
self, user_id: uuid.UUID, data: UserUpdate, actor: User, ip: str | None = None
|
||||
) -> User:
|
||||
@@ -67,6 +88,8 @@ class UserService:
|
||||
|
||||
if user.company_id != actor.company_id:
|
||||
raise NotFoundError("User not found")
|
||||
if user.is_superadmin:
|
||||
raise ForbiddenError("The super admin account cannot be edited or deactivated")
|
||||
|
||||
before = snapshot(user, _AUDIT_FIELDS)
|
||||
deactivating = data.is_active is False and user.is_active
|
||||
@@ -77,8 +100,33 @@ class UserService:
|
||||
if changes["email"] is None:
|
||||
raise ValidationError("Email cannot be empty")
|
||||
changes["email"] = str(changes["email"]).lower()
|
||||
if changes.get("role") is None:
|
||||
changes.pop("role", None)
|
||||
old_role = user.role
|
||||
old_hq = user.headquarters_id
|
||||
role_changed = "role" in changes and changes["role"] != old_role
|
||||
if role_changed:
|
||||
if user.id == actor.id:
|
||||
raise ValidationError("You cannot change your own role")
|
||||
# Only an MR has a headquarters; other roles drop it unless told otherwise.
|
||||
if changes["role"] not in FIELD_ROLES:
|
||||
changes.setdefault("headquarters_id", None)
|
||||
if changes.get("headquarters_id") is not None:
|
||||
hq = await self.db.get(Headquarters, changes["headquarters_id"])
|
||||
if hq is None or hq.company_id != actor.company_id:
|
||||
raise NotFoundError("Headquarters not found")
|
||||
new_extras = changes.pop("extra_headquarter_ids", None)
|
||||
for field, value in changes.items():
|
||||
setattr(user, field, value)
|
||||
# Only an SFE has extra districts; leaving the SFE role drops them.
|
||||
if user.role != Role.SFE:
|
||||
new_extras = []
|
||||
if new_extras is not None:
|
||||
wanted = await self._extra_headquarters(new_extras, user.headquarters_id, actor)
|
||||
user.extra_headquarters = [UserExtraHeadquarters(headquarters_id=h) for h in wanted]
|
||||
elif user.role == Role.SFE and "headquarters_id" in changes:
|
||||
# Moving the primary headquarters: it can no longer also be an extra one.
|
||||
user.extra_headquarters = [e for e in user.extra_headquarters if e.headquarters_id != user.headquarters_id]
|
||||
|
||||
password_reset = new_password is not None
|
||||
if password_reset:
|
||||
@@ -86,14 +134,25 @@ class UserService:
|
||||
user.failed_login_attempts = 0 # a reset also unlocks the account
|
||||
|
||||
# An MR must keep a headquarters; nobody else may gain one (DB check mirrors this).
|
||||
if user.role == Role.MR and user.headquarters_id is None:
|
||||
if user.role in FIELD_ROLES and user.headquarters_id is None:
|
||||
raise ValidationError("An MR must have a headquarters")
|
||||
if user.role != Role.MR and user.headquarters_id is not None:
|
||||
if user.role not in FIELD_ROLES and user.headquarters_id is not None:
|
||||
raise ValidationError("Only an MR can have a headquarters")
|
||||
|
||||
hq_changed = not role_changed and user.role in FIELD_ROLES and user.headquarters_id != old_hq
|
||||
# Changing role, or moving an MR to another headquarters, ends what the old post held.
|
||||
switched_field_role = role_changed and old_role in FIELD_ROLES and user.role in FIELD_ROLES # MR <-> SFE keeps its work
|
||||
released = (
|
||||
await self._end_role_assignments(user.id, old_role)
|
||||
if (role_changed and not switched_field_role) or hq_changed
|
||||
else {}
|
||||
)
|
||||
|
||||
after = snapshot(user, _AUDIT_FIELDS)
|
||||
if password_reset:
|
||||
after["password_reset"] = True # the fact is logged, never the password or its hash
|
||||
if released:
|
||||
after["released"] = released
|
||||
try:
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
@@ -110,11 +169,34 @@ class UserService:
|
||||
raise ConflictError("A user with this email or employee code already exists") from exc
|
||||
|
||||
# SEC-5 — revoke refresh tokens the moment a user is deactivated, or their
|
||||
# credentials are changed, so an old session cannot outlive the old password/email.
|
||||
if deactivating or password_reset or "email" in changes:
|
||||
# credentials or role are changed, so an old session cannot outlive them.
|
||||
if deactivating or password_reset or "email" in changes or role_changed:
|
||||
await AuthService(self.db).logout(user.id)
|
||||
return user
|
||||
|
||||
async def _end_role_assignments(self, user_id: uuid.UUID, old_role: Role) -> dict[str, int]:
|
||||
"""Close what the old role held: an MR's doctors and chemists, an AM's headquarters."""
|
||||
today = date.today()
|
||||
if old_role in FIELD_ROLES:
|
||||
targets = [
|
||||
("doctors", DoctorAssignment, DoctorAssignment.mr_user_id),
|
||||
("chemists", ChemistAssignment, ChemistAssignment.mr_user_id),
|
||||
]
|
||||
elif old_role == Role.AM:
|
||||
targets = [("headquarters", AMHeadquarters, AMHeadquarters.area_manager_user_id)]
|
||||
else:
|
||||
return {}
|
||||
released = {}
|
||||
for label, model, owner in targets:
|
||||
result = await self.db.execute(
|
||||
update(model)
|
||||
.where(owner == user_id, model.effective_to.is_(None))
|
||||
.values(effective_to=today)
|
||||
)
|
||||
if result.rowcount:
|
||||
released[label] = result.rowcount
|
||||
return released
|
||||
|
||||
async def list_users(self, actor: User, role: Role | None = None) -> list[User]:
|
||||
stmt = select(User).where(User.company_id == actor.company_id)
|
||||
if role is not None:
|
||||
@@ -139,9 +221,11 @@ class UserService:
|
||||
AMHeadquarters.effective_to.is_(None),
|
||||
)
|
||||
)
|
||||
manager = (await self.db.execute(stmt)).scalars().first()
|
||||
if manager:
|
||||
manager_id, manager_name = manager.id, manager.full_name
|
||||
# A headquarters may have several Area Managers: name every one.
|
||||
managers = list((await self.db.execute(stmt.order_by(User.full_name))).scalars())
|
||||
if managers:
|
||||
manager_id = managers[0].id
|
||||
manager_name = ", ".join(m.full_name for m in managers)
|
||||
|
||||
return MyProfile(
|
||||
id=user.id,
|
||||
|
||||
Reference in New Issue
Block a user