748 lines
35 KiB
Python
748 lines
35 KiB
Python
import re
|
|
import uuid
|
|
from collections.abc import Iterable
|
|
from zoneinfo import ZoneInfo
|
|
|
|
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.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, FORM_ORDER, TABLES
|
|
from app.database.session import get_db
|
|
from app.users.models import User
|
|
from app.organization.models import Headquarters
|
|
|
|
router = APIRouter(prefix="/csv-data", tags=["CSV data"])
|
|
SERIAL_COLUMNS = {"serial_no", "doctor_sr_no", "chemist_sr_no"}
|
|
IDENTITY_COLUMNS = {"name", "employee_name", "headquarter"}
|
|
APPROVAL_TABLES = approvals.APPROVAL_TABLES
|
|
HIDDEN_TABLES = {"daily_call_report_csv"}
|
|
#: Forms where every row belongs to one MR (and is approved by that MR's Area Manager).
|
|
OWNED_TABLES = APPROVAL_TABLES
|
|
|
|
|
|
def _table(table_name: str) -> tuple[str, ...]:
|
|
if table_name not in TABLES:
|
|
raise NotFoundError("CSV table not found", {"table": table_name})
|
|
return TABLES[table_name]
|
|
|
|
|
|
def _clean_payload(table_name: str, payload: dict[str, object]) -> dict[str, str | None]:
|
|
columns = _table(table_name)
|
|
unknown = set(payload) - set(columns)
|
|
if unknown:
|
|
raise ValidationError("Unknown CSV column", {"columns": sorted(unknown)})
|
|
return {column: None if payload.get(column) is None else str(payload.get(column, "")) for column in columns}
|
|
|
|
|
|
def _has_user_data(table_name: str, values: dict[str, str | None]) -> bool:
|
|
return any(value and column not in SERIAL_COLUMNS for column, value in values.items())
|
|
|
|
|
|
async def _reject_repeat(
|
|
table_name: str, values: dict[str, str | None], db: AsyncSession, owner_id: uuid.UUID | None = None
|
|
) -> None:
|
|
"""A row identical to one already in the form is a repeat (a double click), not new data."""
|
|
compared = [c for c in _table(table_name) if c not in SERIAL_COLUMNS]
|
|
conditions = " AND ".join(f"COALESCE(\"{c}\", '') = :c_{i}" for i, c in enumerate(compared))
|
|
params: dict[str, object] = {f"c_{i}": (values.get(c) or "") for i, c in enumerate(compared)}
|
|
if owner_id is not None:
|
|
conditions += " AND owner_user_id = :owner"
|
|
params["owner"] = owner_id
|
|
if (await db.execute(text(f'SELECT 1 FROM "{table_name}" WHERE {conditions} LIMIT 1'), params)).first():
|
|
raise ConflictError("An identical row already exists in this form")
|
|
|
|
|
|
#: 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", "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:
|
|
columns = _table(table_name)
|
|
# An MR's own form is numbered 1, 2, 3… whoever else has rows in the same table.
|
|
if table_name in OWNED_TABLES:
|
|
partition = "PARTITION BY owner_user_id "
|
|
elif table_name in MONTH_NUMBERED_TABLES:
|
|
partition = "PARTITION BY lower(btrim(COALESCE(\"month\", ''))) "
|
|
else:
|
|
partition = ""
|
|
serial_updates = ""
|
|
for column in columns:
|
|
if column in SERIAL_COLUMNS:
|
|
serial_updates += f', "{column}" = numbered.serial::text'
|
|
await db.execute(
|
|
text(
|
|
f'''WITH numbered AS (
|
|
SELECT id,
|
|
ROW_NUMBER() OVER (ORDER BY source_row_number, id) AS row_number,
|
|
ROW_NUMBER() OVER ({partition}ORDER BY source_row_number, id) AS serial
|
|
FROM "{table_name}"
|
|
)
|
|
UPDATE "{table_name}" AS target
|
|
SET source_row_number = numbered.row_number{serial_updates}
|
|
FROM numbered
|
|
WHERE target.id = numbered.id'''
|
|
)
|
|
)
|
|
|
|
|
|
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 not in FIELD_ROLES:
|
|
return values
|
|
headquarters_name = None
|
|
if subject.headquarters_id:
|
|
headquarters = await db.get(Headquarters, subject.headquarters_id)
|
|
headquarters_name = headquarters.name if headquarters else None
|
|
for column in values:
|
|
if column in {"name", "employee_name"}:
|
|
values[column] = subject.full_name
|
|
elif column == "headquarter":
|
|
values[column] = headquarters_name
|
|
return values
|
|
|
|
|
|
async def _present_rows_for_user(
|
|
rows: list[dict[str, object]], current_user: User, db: AsyncSession
|
|
) -> list[dict[str, object]]:
|
|
"""Show account identity from the session without changing imported data.
|
|
|
|
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 not in FIELD_ROLES:
|
|
return rows
|
|
|
|
headquarters_name = None
|
|
if current_user.headquarters_id:
|
|
headquarters = await db.get(Headquarters, current_user.headquarters_id)
|
|
headquarters_name = headquarters.name if headquarters else None
|
|
|
|
presented: list[dict[str, object]] = []
|
|
for row in rows:
|
|
item = dict(row)
|
|
if "name" in item:
|
|
item["name"] = current_user.full_name
|
|
if "employee_name" in item:
|
|
item["employee_name"] = current_user.full_name
|
|
if "headquarter" in item:
|
|
item["headquarter"] = headquarters_name or ""
|
|
presented.append(item)
|
|
return presented
|
|
|
|
|
|
async def _apply_sales_projection_product(
|
|
values: dict[str, str | None], db: AsyncSession, current_user: User | None = None
|
|
) -> dict[str, str | None]:
|
|
if values.get("product_name") is None:
|
|
return values
|
|
if current_user is not None and current_user.role == Role.ADMIN:
|
|
# Admin maintains the product list itself: any product name and PTS may be entered.
|
|
if not (values.get("product_name") or "").strip():
|
|
raise ValidationError("Enter a product name")
|
|
return values
|
|
result = await db.execute(
|
|
text(
|
|
'SELECT pts FROM "sales_projection" '
|
|
'WHERE product_name = :product_name LIMIT 1'
|
|
),
|
|
{"product_name": values["product_name"]},
|
|
)
|
|
pts = result.scalar_one_or_none()
|
|
if pts is None:
|
|
raise ValidationError(
|
|
"Choose a product from the Sales Projection product list",
|
|
{"product_name": values["product_name"]},
|
|
)
|
|
values["pts"] = str(pts)
|
|
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}"'
|
|
|
|
|
|
async def _owner_condition(
|
|
table_name: str, current_user: User, db: AsyncSession, owner_id: uuid.UUID | None
|
|
) -> tuple[list[str], dict[str, object]]:
|
|
"""Limit an approval form's rows to the MRs this user may see: an MR sees only their own,
|
|
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 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}
|
|
if owner_id is not None:
|
|
await approvals.assert_owner_in_scope(db, current_user, owner_id)
|
|
return ["owner_user_id = :owner"], {"owner": owner_id}
|
|
if current_user.role == Role.AM:
|
|
return ["owner_user_id = ANY(:owners)"], {"owners": [o.id for o in await approvals.owners_in_scope(db, current_user)]}
|
|
return [], {}
|
|
|
|
|
|
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 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 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:
|
|
raise ForbiddenError("This row is outside your headquarters")
|
|
await approvals.assert_owner_in_scope(db, current_user, owner_id)
|
|
return owner_id
|
|
|
|
|
|
def _month_filter(table_name: str, month: str | None) -> tuple[list[str], dict[str, object]]:
|
|
if not month or not month.strip():
|
|
return [], {}
|
|
if "month" not in _table(table_name):
|
|
raise ValidationError("This form has no month to filter by", {"table": table_name})
|
|
return ["lower(btrim(COALESCE(\"month\", ''))) = :month"], {"month": month.strip().lower()}
|
|
|
|
|
|
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 ""
|
|
result = await db.execute(text(f"{_select(table_name, columns)}{where} ORDER BY source_row_number"), {**params, **extra})
|
|
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 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
|
|
|
|
|
|
def _ensure_table_access(table_name: str, current_user: User) -> None:
|
|
if table_name in HIDDEN_TABLES:
|
|
raise ForbiddenError("This CSV form is not available for export.", {"table": table_name})
|
|
if current_user.role == Role.AM and table_name not in APPROVAL_TABLES:
|
|
raise ForbiddenError("Area Manager can only access approval forms.", {"table": table_name})
|
|
|
|
|
|
@router.get("/tables")
|
|
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)]
|
|
|
|
|
|
async def _signature(
|
|
table_name: str, owner_id: uuid.UUID | None, db: AsyncSession
|
|
) -> tuple[str | None, str | None, str | None]:
|
|
"""(status, approver name, approval date/time) for the PDF's signature — decided here, never by the browser.
|
|
|
|
It is the approval of the one MR whose rows the PDF holds; a PDF mixing several MRs is never signed."""
|
|
if table_name not in APPROVAL_TABLES:
|
|
return None, None, None
|
|
row = await approvals.get_approval(db, table_name, owner_id) if owner_id else None
|
|
if row is None or row.status != "approved":
|
|
return "pending", None, None
|
|
when = row.updated_at.astimezone(ZoneInfo(settings.APP_TIMEZONE)).strftime("%d/%m/%Y %H:%M") if row.updated_at else None
|
|
return "approved", row.approved_by_name, when
|
|
|
|
|
|
def _approval_out(row, owner: User, headquarters_name: str | None = None) -> dict[str, object]:
|
|
return {
|
|
"table_name": row.table_name,
|
|
"owner_user_id": str(owner.id),
|
|
"owner_name": owner.full_name,
|
|
"headquarters_name": headquarters_name,
|
|
"status": row.status,
|
|
"approved_by_name": row.approved_by_name,
|
|
"updated_at": row.updated_at.isoformat() if row.updated_at else None,
|
|
}
|
|
|
|
|
|
async def _hq_name(db: AsyncSession, user: User) -> str | None:
|
|
headquarters = await db.get(Headquarters, user.headquarters_id) if user.headquarters_id else None
|
|
return headquarters.name if headquarters else None
|
|
|
|
|
|
def _approval_table(table_name: str) -> str:
|
|
if table_name not in APPROVAL_TABLES:
|
|
raise NotFoundError("This form has no approval step", {"table": table_name})
|
|
return table_name
|
|
|
|
|
|
@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, 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)]
|
|
|
|
|
|
@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, 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()}
|
|
return [
|
|
{"id": str(o.id), "name": o.full_name, "employee_code": o.employee_code, "headquarters_name": hq.get(o.headquarters_id)}
|
|
for o in await approvals.owners_in_scope(db, current_user)
|
|
]
|
|
|
|
|
|
@router.post("/{table_name}/approval/request")
|
|
async def request_form_approval(
|
|
table_name: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(require_role(Role.MR)),
|
|
) -> dict[str, object]:
|
|
"""The MR sends a form to the Area Manager."""
|
|
row = await approvals.request_approval(db, _approval_table(table_name), current_user)
|
|
return _approval_out(row, current_user, await _hq_name(db, current_user))
|
|
|
|
|
|
@router.post("/{table_name}/approval/approve")
|
|
async def approve_form(
|
|
table_name: str,
|
|
owner_id: uuid.UUID = Query(description="The MR whose form is being approved"),
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(require_role(Role.AM)),
|
|
) -> dict[str, object]:
|
|
"""The Area Manager signs an MR's form — only the one who manages that MR's headquarters."""
|
|
row = await approvals.approve(db, _approval_table(table_name), owner_id, current_user)
|
|
owner = await db.get(User, owner_id)
|
|
return _approval_out(row, owner, await _hq_name(db, owner))
|
|
|
|
|
|
@router.get("/{table_name}/rows")
|
|
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, Role.GM)),
|
|
) -> list[dict[str, object]]:
|
|
_ensure_table_access(table_name, current_user)
|
|
return await _load_rows(table_name, db, current_user, owner_id)
|
|
|
|
|
|
@router.post("/{table_name}/rows", status_code=status.HTTP_201_CREATED)
|
|
async def create_csv_row(
|
|
table_name: str,
|
|
payload: dict[str, object] = Body(...),
|
|
owner_id: uuid.UUID | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(require_role(Role.MR, Role.ADMIN)),
|
|
) -> dict[str, object]:
|
|
columns = _table(table_name)
|
|
values = _clean_payload(table_name, payload)
|
|
owner: User | None = None
|
|
if table_name in OWNED_TABLES:
|
|
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)
|
|
next_row = (await db.execute(text(f'SELECT COALESCE(MAX(source_row_number), 0) + 1 FROM "{table_name}"'))).scalar_one()
|
|
for column in columns:
|
|
if column in SERIAL_COLUMNS:
|
|
values[column] = str(next_row)
|
|
extra_ids, extra_params = (["owner_user_id"], [":owner_user_id"]) if owner else ([], [])
|
|
identifiers = ", ".join(["id", "source_row_number", *extra_ids, *[f'"{column}"' for column in columns]])
|
|
parameters = ", ".join([":id", ":source_row_number", *extra_params, *[f":{column}" for column in columns]])
|
|
values.update(id=uuid.uuid4(), source_row_number=next_row)
|
|
if owner:
|
|
values["owner_user_id"] = owner.id
|
|
result = await db.execute(text(f'INSERT INTO "{table_name}" ({identifiers}) VALUES ({parameters}) RETURNING id'), values)
|
|
row_id = result.scalar_one()
|
|
await _renumber_rows(table_name, db)
|
|
await approvals.invalidate(db, table_name, owner.id if owner else None)
|
|
await db.commit()
|
|
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")
|
|
# 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:
|
|
raise ValidationError("Serial numbers are generated automatically")
|
|
assignments = ", ".join(f'"{column}" = :{column}' for column in changes)
|
|
result = await db.execute(text(f'UPDATE "{table_name}" SET {assignments} WHERE id = :id RETURNING id'), {**changes, "id": row_id})
|
|
if result.scalar_one_or_none() is None:
|
|
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
|
await approvals.invalidate(db, table_name, row_owner)
|
|
await db.commit()
|
|
return await _get_row(table_name, row_id, db)
|
|
|
|
|
|
@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,
|
|
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()
|
|
|
|
|
|
async def _get_row(table_name: str, row_id: uuid.UUID, db: AsyncSession) -> dict[str, object]:
|
|
columns = _table(table_name)
|
|
result = await db.execute(text(f'{_select(table_name, columns)} WHERE id = :id'), {"id": row_id})
|
|
row = result.mappings().one_or_none()
|
|
if row is None:
|
|
raise NotFoundError("CSV row not found", {"id": str(row_id)})
|
|
return dict(row)
|
|
|
|
|
|
def _pdf_bytes(
|
|
table_name: str,
|
|
table_label: str,
|
|
row: dict[str, object],
|
|
approval_status: str | None = None,
|
|
am_name: str | None = None,
|
|
approved_at: str | None = None,
|
|
) -> bytes:
|
|
from app.pdf.service import csv_table_pdf_bytes
|
|
return csv_table_pdf_bytes(
|
|
table_name,
|
|
table_label,
|
|
_table(table_name),
|
|
[row],
|
|
approval_status,
|
|
am_name,
|
|
approved_at=approved_at,
|
|
)
|
|
|
|
|
|
@router.get("/{table_name}/rows/{row_id}/pdf")
|
|
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, Role.GM)),
|
|
) -> Response:
|
|
_ensure_table_access(table_name, current_user)
|
|
row_owner = await _row_owner(table_name, row_id, current_user, db)
|
|
row = await _get_row(table_name, row_id, db)
|
|
row = (await _present_rows_for_user([row], current_user, db))[0]
|
|
approval_status, am_name, approved_at = await _signature(table_name, row_owner, db)
|
|
return Response(
|
|
_pdf_bytes(table_name, CSV_LABELS[table_name], row, approval_status, am_name, approved_at),
|
|
media_type="application/pdf",
|
|
headers={"Content-Disposition": f'attachment; filename="{table_name}-{row_id}.pdf"'},
|
|
)
|
|
|
|
|
|
@router.get("/{table_name}/pdf")
|
|
async def download_csv_table_pdfs(
|
|
table_name: str,
|
|
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, 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)
|
|
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 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()) or whole_range)
|
|
pdf = csv_table_pdf_bytes(
|
|
table_name,
|
|
CSV_LABELS[table_name],
|
|
columns,
|
|
rows,
|
|
approval_status,
|
|
am_name,
|
|
approved_at=approved_at,
|
|
renumber=renumber,
|
|
)
|
|
return Response(
|
|
pdf,
|
|
media_type="application/pdf",
|
|
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
|
|
) |