From 4e5e95775d5b0f31709d4ac6a538a2ec03f9105b Mon Sep 17 00:00:00 2001 From: Shubhankar45 Date: Tue, 27 Oct 2026 12:03:31 +0000 Subject: [PATCH] -- --- .gitignore | 2 + ...e5f1b8d0_day_plan_and_end_of_day_report.py | 99 +++++ .../n4d0f6b2c9e1_add_doctor_date_of_birth.py | 25 ++ ...1a7c3d0f2_self_report_and_product_lists.py | 42 ++ .../p6f2b8d4e1a3_expense_statement_form.py | 44 ++ ...sales_projection_closing_and_projection.py | 34 ++ ...d0f6a3c5_master_approval_and_visit_gift.py | 50 +++ ...c5e1a7b4d6_several_area_managers_per_hq.py | 50 +++ alembic/versions/t0d6f2b8c5e7_superadmin.py | 64 +++ .../u1e7a3c9d6f8_sales_projection_owner.py | 36 ++ ...2f8b4d0e7a9_sales_projection_submission.py | 31 ++ ...e1f8b0_drop_sales_projection_submission.py | 31 ++ .../x4b0d6f2a9c1_stock_statement_owner.py | 34 ++ alembic/versions/y5c1e7a3b0d2_sfe_role.py | 44 ++ app/approvals/state_machine.py | 5 +- app/auth/dependencies.py | 12 +- app/auth/schemas.py | 2 + app/chemists/models.py | 17 + app/chemists/router.py | 36 +- app/chemists/schemas.py | 7 + app/chemists/service.py | 66 ++- app/common/enums.py | 32 ++ app/common/master_approval.py | 116 ++++++ app/config/settings.py | 2 + app/csv_data/approval.py | 16 +- app/csv_data/config.py | 21 +- app/csv_data/router.py | 286 +++++++++++-- app/database/all_models.py | 5 +- app/doctors/models.py | 18 + app/doctors/router.py | 37 +- app/doctors/schemas.py | 28 +- app/doctors/service.py | 77 +++- app/organization/models.py | 8 +- app/organization/service.py | 22 +- app/pdf/forms.py | 296 +++++++++++--- app/pdf/service.py | 7 + app/pdf/templates/dcr.html | 12 +- app/pdf/templates/dcr.txt | 16 +- app/pdf/templates/forms/_macros.html | 2 +- .../templates/forms/expense_statement.html | 105 +++++ app/pdf/templates/forms/sales_projection.html | 29 +- app/products/service.py | 4 +- app/reports/models.py | 93 ++++- app/reports/repository.py | 24 +- app/reports/router.py | 60 ++- app/reports/schemas.py | 79 +++- app/reports/serializers.py | 39 ++ app/reports/service.py | 382 +++++++++++++++--- app/users/models.py | 39 +- app/users/overview.py | 168 ++++++++ app/users/router.py | 11 + app/users/schemas.py | 17 +- app/users/service.py | 106 ++++- docker-compose.yml | 21 + scripts/backup.sh | 27 ++ scripts/create_superadmin.py | 59 +++ scripts/import_csv_data.py | 13 +- tests/conftest.py | 29 ++ tests/e2e/test_area_manager_headquarters.py | 22 +- tests/e2e/test_dashboard_hq_filter.py | 24 ++ tests/e2e/test_day_plan.py | 244 +++++++++++ tests/e2e/test_dcr_form_fields.py | 77 ++-- tests/e2e/test_duplicate_requests.py | 19 +- tests/e2e/test_form_approval.py | 7 +- tests/e2e/test_form_gm_oversight.py | 67 +++ tests/e2e/test_form_ownership.py | 45 +++ tests/e2e/test_full_approval_flow.py | 68 +++- tests/e2e/test_master_approval.py | 102 +++++ tests/e2e/test_self_report.py | 141 +++++++ tests/e2e/test_user_role_change.py | 76 ++++ tests/unit/test_business_timezone.py | 16 +- tests/unit/test_csv_access_for_am.py | 2 +- tests/unit/test_csv_form_pdfs.py | 42 +- web/src/App.tsx | 25 +- web/src/api/index.ts | 17 +- .../admin/HeadquartersChecklist.tsx | 4 +- web/src/components/admin/MasterRequests.tsx | 114 ++++++ web/src/components/layout/Sidebar.tsx | 9 + web/src/components/reports/AddVisitForm.tsx | 86 ++-- .../components/reports/PlannedVisitList.tsx | 86 ++++ web/src/components/reports/ProductPicker.tsx | 62 +++ web/src/components/reports/ReportTable.tsx | 19 +- web/src/components/reports/StatusBadge.tsx | 8 +- web/src/components/reports/VisitList.tsx | 11 +- .../components/reports/WorkflowTracker.tsx | 13 +- web/src/components/ui/DataTable.tsx | 13 +- web/src/lib/format.ts | 31 +- web/src/lib/headquarters.ts | 19 +- web/src/lib/navigation.ts | 21 +- web/src/pages/DashboardPage.tsx | 139 ++++++- web/src/pages/DirectoryPage.tsx | 24 +- web/src/pages/admin/AdminPage.tsx | 146 +++++-- web/src/pages/admin/UserDetailPage.tsx | 168 ++++++++ web/src/pages/admin/UsersPage.tsx | 83 +++- web/src/pages/forms/FormsPage.tsx | 237 +++++++++-- web/src/pages/forms/InlineCell.tsx | 85 ++++ web/src/pages/forms/RowEditor.tsx | 31 +- web/src/pages/forms/approval.ts | 2 +- web/src/pages/forms/layout.ts | 36 ++ web/src/pages/reports/NewReportPage.tsx | 68 +++- web/src/pages/reports/ReportDetailPage.tsx | 165 ++++++-- web/src/pages/reports/ReportsPage.tsx | 50 ++- web/src/styles/components.css | 20 + web/src/types.ts | 35 +- 104 files changed, 5210 insertions(+), 606 deletions(-) create mode 100644 alembic/versions/m3c9e5f1b8d0_day_plan_and_end_of_day_report.py create mode 100644 alembic/versions/n4d0f6b2c9e1_add_doctor_date_of_birth.py create mode 100644 alembic/versions/o5e1a7c3d0f2_self_report_and_product_lists.py create mode 100644 alembic/versions/p6f2b8d4e1a3_expense_statement_form.py create mode 100644 alembic/versions/q7a3c9e5f2b4_sales_projection_closing_and_projection.py create mode 100644 alembic/versions/r8b4d0f6a3c5_master_approval_and_visit_gift.py create mode 100644 alembic/versions/s9c5e1a7b4d6_several_area_managers_per_hq.py create mode 100644 alembic/versions/t0d6f2b8c5e7_superadmin.py create mode 100644 alembic/versions/u1e7a3c9d6f8_sales_projection_owner.py create mode 100644 alembic/versions/v2f8b4d0e7a9_sales_projection_submission.py create mode 100644 alembic/versions/w3a9c5e1f8b0_drop_sales_projection_submission.py create mode 100644 alembic/versions/x4b0d6f2a9c1_stock_statement_owner.py create mode 100644 alembic/versions/y5c1e7a3b0d2_sfe_role.py create mode 100644 app/common/master_approval.py create mode 100644 app/pdf/templates/forms/expense_statement.html create mode 100644 app/users/overview.py create mode 100755 scripts/backup.sh create mode 100644 scripts/create_superadmin.py create mode 100644 tests/e2e/test_dashboard_hq_filter.py create mode 100644 tests/e2e/test_day_plan.py create mode 100644 tests/e2e/test_form_gm_oversight.py create mode 100644 tests/e2e/test_master_approval.py create mode 100644 tests/e2e/test_self_report.py create mode 100644 tests/e2e/test_user_role_change.py create mode 100644 web/src/components/admin/MasterRequests.tsx create mode 100644 web/src/components/reports/PlannedVisitList.tsx create mode 100644 web/src/components/reports/ProductPicker.tsx create mode 100644 web/src/pages/admin/UserDetailPage.tsx create mode 100644 web/src/pages/forms/InlineCell.tsx create mode 100644 web/src/pages/forms/layout.ts diff --git a/.gitignore b/.gitignore index 38c7135..5ba41a6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ storage/ .coverage htmlcov/ .DS_Store +# Database dumps (docker-compose `backup` service): never committed. +backups/ diff --git a/alembic/versions/m3c9e5f1b8d0_day_plan_and_end_of_day_report.py b/alembic/versions/m3c9e5f1b8d0_day_plan_and_end_of_day_report.py new file mode 100644 index 0000000..8146939 --- /dev/null +++ b/alembic/versions/m3c9e5f1b8d0_day_plan_and_end_of_day_report.py @@ -0,0 +1,99 @@ +"""split the Daily Call Report into a morning plan and an end-of-day report + +An MR now files a PLAN (doctors/chemists to visit, and when) and, once the Area Manager +approves it, an ACTUAL end-of-day report of the visits made, with a note for each planned +visit that was missed. Existing reports are end-of-day reports with no plan. + +Revision ID: m3c9e5f1b8d0 +Revises: l2b8d4e0a7c9 +Create Date: 2026-10-23 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "m3c9e5f1b8d0" +down_revision: str | None = "l2b8d4e0a7c9" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.add_column( + "daily_call_report", + sa.Column( + "report_type", + sa.Enum("PLAN", "ACTUAL", name="report_type_enum", native_enum=False, length=10), + nullable=False, + server_default="ACTUAL", + ), + ) + op.alter_column("daily_call_report", "report_type", server_default=None) + op.add_column( + "daily_call_report", + sa.Column( + "plan_id", + sa.UUID(), + sa.ForeignKey("daily_call_report.id", ondelete="RESTRICT"), + nullable=True, + ), + ) + op.drop_constraint("uq_dcr_mr_date", "daily_call_report", type_="unique") + op.create_unique_constraint( + "uq_dcr_mr_date_type", "daily_call_report", ["mr_user_id", "report_date", "report_type"] + ) + op.create_unique_constraint("uq_dcr_plan_id", "daily_call_report", ["plan_id"]) + op.create_check_constraint( + "ck_dcr_plan_link", "daily_call_report", "report_type = 'ACTUAL' OR plan_id IS NULL" + ) + + # A planned visit has no outcome yet. + op.alter_column("doctor_call", "call_outcome", nullable=True) + + op.create_table( + "missed_visit", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("dcr_id", sa.UUID(), nullable=False), + sa.Column("doctor_id", sa.UUID(), nullable=True), + sa.Column("chemist_id", sa.UUID(), nullable=True), + sa.Column("reason", sa.Text(), nullable=False), + sa.Column( + "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False + ), + sa.Column( + "updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False + ), + sa.CheckConstraint( + "(doctor_id IS NULL) <> (chemist_id IS NULL)", name="ck_missed_one_target" + ), + sa.ForeignKeyConstraint(["dcr_id"], ["daily_call_report.id"], ondelete="CASCADE"), + sa.ForeignKeyConstraint(["doctor_id"], ["doctor.id"], ondelete="RESTRICT"), + sa.ForeignKeyConstraint(["chemist_id"], ["chemist.id"], ondelete="RESTRICT"), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("dcr_id", "doctor_id", name="uq_missed_dcr_doctor"), + sa.UniqueConstraint("dcr_id", "chemist_id", name="uq_missed_dcr_chemist"), + ) + + +def downgrade() -> None: + op.drop_table("missed_visit") + # Planned visits cannot exist without an outcome in the old schema. + for table in ("doctor_call", "chemist_call"): + op.execute( + f"DELETE FROM {table} WHERE dcr_id IN " + "(SELECT id FROM daily_call_report WHERE report_type = 'PLAN')" + ) + op.execute("UPDATE daily_call_report SET plan_id = NULL") + op.execute("DELETE FROM daily_call_report WHERE report_type = 'PLAN'") + op.alter_column("doctor_call", "call_outcome", nullable=False) + op.drop_constraint("ck_dcr_plan_link", "daily_call_report", type_="check") + op.drop_constraint("uq_dcr_plan_id", "daily_call_report", type_="unique") + op.drop_constraint("uq_dcr_mr_date_type", "daily_call_report", type_="unique") + op.create_unique_constraint( + "uq_dcr_mr_date", "daily_call_report", ["mr_user_id", "report_date"] + ) + op.drop_column("daily_call_report", "plan_id") + op.drop_column("daily_call_report", "report_type") diff --git a/alembic/versions/n4d0f6b2c9e1_add_doctor_date_of_birth.py b/alembic/versions/n4d0f6b2c9e1_add_doctor_date_of_birth.py new file mode 100644 index 0000000..572b625 --- /dev/null +++ b/alembic/versions/n4d0f6b2c9e1_add_doctor_date_of_birth.py @@ -0,0 +1,25 @@ +"""add an optional date of birth to the doctor master + +Revision ID: n4d0f6b2c9e1 +Revises: m3c9e5f1b8d0 +Create Date: 2026-10-24 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "n4d0f6b2c9e1" +down_revision: str | None = "m3c9e5f1b8d0" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.add_column("doctor", sa.Column("date_of_birth", sa.Date(), nullable=True)) + + +def downgrade() -> None: + op.drop_column("doctor", "date_of_birth") diff --git a/alembic/versions/o5e1a7c3d0f2_self_report_and_product_lists.py b/alembic/versions/o5e1a7c3d0f2_self_report_and_product_lists.py new file mode 100644 index 0000000..951889c --- /dev/null +++ b/alembic/versions/o5e1a7c3d0f2_self_report_and_product_lists.py @@ -0,0 +1,42 @@ +"""Area Manager's own report (SELF), and several products per doctor visit + +``report_type`` is a plain string column, so SELF needs no schema change. The promoted +and sample product columns become text arrays; an existing value becomes a one-item list. + +Revision ID: o5e1a7c3d0f2 +Revises: n4d0f6b2c9e1 +Create Date: 2026-10-24 +""" + +from collections.abc import Sequence + +from alembic import op + +revision: str = "o5e1a7c3d0f2" +down_revision: str | None = "n4d0f6b2c9e1" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + +_COLUMNS = ("product_promote", "product_sample") + + +def upgrade() -> None: + for column in _COLUMNS: + op.execute( + f"ALTER TABLE doctor_call ALTER COLUMN {column} TYPE varchar(200)[] USING " + f"CASE WHEN btrim(coalesce({column}, '')) = '' THEN '{{}}'::varchar(200)[] " + f"ELSE ARRAY[btrim({column})]::varchar(200)[] END" + ) + op.execute(f"ALTER TABLE doctor_call ALTER COLUMN {column} SET DEFAULT '{{}}'") + op.execute(f"ALTER TABLE doctor_call ALTER COLUMN {column} SET NOT NULL") + + +def downgrade() -> None: + op.execute("DELETE FROM daily_call_report WHERE report_type = 'SELF'") + for column in _COLUMNS: + op.execute(f"ALTER TABLE doctor_call ALTER COLUMN {column} DROP NOT NULL") + op.execute(f"ALTER TABLE doctor_call ALTER COLUMN {column} DROP DEFAULT") + op.execute( + f"ALTER TABLE doctor_call ALTER COLUMN {column} TYPE varchar(200) USING " + f"nullif(left(array_to_string({column}, ', '), 200), '')" + ) diff --git a/alembic/versions/p6f2b8d4e1a3_expense_statement_form.py b/alembic/versions/p6f2b8d4e1a3_expense_statement_form.py new file mode 100644 index 0000000..d6ff854 --- /dev/null +++ b/alembic/versions/p6f2b8d4e1a3_expense_statement_form.py @@ -0,0 +1,44 @@ +"""the Expense Statement company form + +One row per day of travel: place of visit, fare, daily allowance, postage, other expenses +and business achieved. Like the fare chart, each MR has their own copy, and the Area +Manager checks (approves) it. + +Revision ID: p6f2b8d4e1a3 +Revises: o5e1a7c3d0f2 +Create Date: 2026-10-25 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "p6f2b8d4e1a3" +down_revision: str | None = "o5e1a7c3d0f2" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + +COLUMNS = ( + "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", +) + + +def upgrade() -> None: + op.create_table( + "expense_statement", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("source_row_number", sa.Integer(), nullable=False), + sa.Column("owner_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="SET NULL"), nullable=True), + *(sa.Column(column, sa.Text(), nullable=True) for column in COLUMNS), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("source_row_number", name="uq_expense_statement_source_row"), + ) + op.create_index("ix_expense_statement_owner", "expense_statement", ["owner_user_id"]) + + +def downgrade() -> None: + op.drop_index("ix_expense_statement_owner", table_name="expense_statement") + op.drop_table("expense_statement") diff --git a/alembic/versions/q7a3c9e5f2b4_sales_projection_closing_and_projection.py b/alembic/versions/q7a3c9e5f2b4_sales_projection_closing_and_projection.py new file mode 100644 index 0000000..80faf1a --- /dev/null +++ b/alembic/versions/q7a3c9e5f2b4_sales_projection_closing_and_projection.py @@ -0,0 +1,34 @@ +"""Sales Projection: Closing (quantity, value) and Projection (quantity, value) + +The form is now Sl. No. | Product | PTS | Closing (Qty, Value) | Projection (Qty, Value). +The old Closing figure becomes the closing quantity. The other old columns (Sec. Qty, Value, +Sec. Value, Sales, Value) are no longer shown, but are kept so no entered figure is lost. + +Revision ID: q7a3c9e5f2b4 +Revises: p6f2b8d4e1a3 +Create Date: 2026-10-26 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "q7a3c9e5f2b4" +down_revision: str | None = "p6f2b8d4e1a3" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + +COLUMNS = ("closing_qty", "closing_value", "projection_qty", "projection_value") + + +def upgrade() -> None: + for column in COLUMNS: + op.add_column("sales_projection", sa.Column(column, sa.Text(), nullable=True)) + op.execute('UPDATE "sales_projection" SET closing_qty = closing') + + +def downgrade() -> None: + for column in reversed(COLUMNS): + op.drop_column("sales_projection", column) diff --git a/alembic/versions/r8b4d0f6a3c5_master_approval_and_visit_gift.py b/alembic/versions/r8b4d0f6a3c5_master_approval_and_visit_gift.py new file mode 100644 index 0000000..4cb3cbc --- /dev/null +++ b/alembic/versions/r8b4d0f6a3c5_master_approval_and_visit_gift.py @@ -0,0 +1,50 @@ +"""a doctor or chemist added by an MR waits for the Area Manager; a doctor visit records a gift + +Every existing doctor and chemist is already in use, so all of them start APPROVED. + +Revision ID: r8b4d0f6a3c5 +Revises: q7a3c9e5f2b4 +Create Date: 2026-10-26 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "r8b4d0f6a3c5" +down_revision: str | None = "q7a3c9e5f2b4" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + +MASTERS = ("doctor", "chemist") + + +def upgrade() -> None: + for table in MASTERS: + op.add_column( + table, + sa.Column("approval_status", sa.String(10), nullable=False, server_default="APPROVED"), + ) + op.add_column( + table, + sa.Column( + "requested_by_user_id", + sa.UUID(), + sa.ForeignKey("user.id", ondelete="SET NULL"), + nullable=True, + ), + ) + op.add_column(table, sa.Column("rejection_reason", sa.String(500), nullable=True)) + op.create_index(f"ix_{table}_approval_status", table, ["approval_status"]) + op.add_column("doctor_call", sa.Column("gift", sa.String(200), nullable=True)) + + +def downgrade() -> None: + op.drop_column("doctor_call", "gift") + for table in reversed(MASTERS): + op.drop_index(f"ix_{table}_approval_status", table_name=table) + op.drop_column(table, "rejection_reason") + op.drop_column(table, "requested_by_user_id") + op.drop_column(table, "approval_status") diff --git a/alembic/versions/s9c5e1a7b4d6_several_area_managers_per_hq.py b/alembic/versions/s9c5e1a7b4d6_several_area_managers_per_hq.py new file mode 100644 index 0000000..b9b784e --- /dev/null +++ b/alembic/versions/s9c5e1a7b4d6_several_area_managers_per_hq.py @@ -0,0 +1,50 @@ +"""a headquarters may have several Area Managers + +The rule was one active Area Manager per headquarters. Now any number may manage one; +the same Area Manager still holds a headquarters only once at a time. + +Revision ID: s9c5e1a7b4d6 +Revises: r8b4d0f6a3c5 +Create Date: 2026-10-26 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "s9c5e1a7b4d6" +down_revision: str | None = "r8b4d0f6a3c5" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.drop_index("uq_active_am_per_hq", table_name="am_headquarters") + op.create_index( + "uq_active_am_hq_pair", + "am_headquarters", + ["headquarters_id", "area_manager_user_id"], + unique=True, + postgresql_where=sa.text("effective_to IS NULL"), + ) + + +def downgrade() -> None: + op.drop_index("uq_active_am_hq_pair", table_name="am_headquarters") + # Back to one per headquarters: keep the longest-standing Area Manager of each. + op.execute( + """UPDATE am_headquarters a SET effective_to = CURRENT_DATE + WHERE a.effective_to IS NULL AND EXISTS ( + SELECT 1 FROM am_headquarters b + WHERE b.headquarters_id = a.headquarters_id AND b.effective_to IS NULL + AND (b.effective_from, b.id::text) < (a.effective_from, a.id::text))""" + ) + op.create_index( + "uq_active_am_per_hq", + "am_headquarters", + ["headquarters_id"], + unique=True, + postgresql_where=sa.text("effective_to IS NULL"), + ) diff --git a/alembic/versions/t0d6f2b8c5e7_superadmin.py b/alembic/versions/t0d6f2b8c5e7_superadmin.py new file mode 100644 index 0000000..15297e8 --- /dev/null +++ b/alembic/versions/t0d6f2b8c5e7_superadmin.py @@ -0,0 +1,64 @@ +"""protected super admin account + +Adds `user.is_superadmin`. A trigger refuses to delete such a row, to deactivate it, or +to change its role, email, employee code or the flag itself, so the protection holds even +for a direct SQL session. (The password hash stays changeable so the owner can rotate it +with `scripts/create_superadmin.py`.) + +Revision ID: t0d6f2b8c5e7 +Revises: s9c5e1a7b4d6 +Create Date: 2026-10-27 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "t0d6f2b8c5e7" +down_revision: str | None = "s9c5e1a7b4d6" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.add_column( + "user", + sa.Column("is_superadmin", sa.Boolean(), server_default=sa.text("false"), nullable=False), + ) + op.execute( + """ + CREATE OR REPLACE FUNCTION protect_superadmin() RETURNS trigger AS $$ + BEGIN + IF TG_OP = 'DELETE' THEN + IF OLD.is_superadmin THEN + RAISE EXCEPTION 'The super admin account cannot be deleted'; + END IF; + RETURN OLD; + END IF; + IF OLD.is_superadmin AND ( + NEW.is_superadmin IS DISTINCT FROM OLD.is_superadmin + OR NEW.is_active IS DISTINCT FROM OLD.is_active + OR NEW.role IS DISTINCT FROM OLD.role + OR NEW.email IS DISTINCT FROM OLD.email + OR NEW.employee_code IS DISTINCT FROM OLD.employee_code + OR NEW.full_name IS DISTINCT FROM OLD.full_name + ) THEN + RAISE EXCEPTION 'The super admin account cannot be modified'; + END IF; + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + """ + ) + op.execute( + 'CREATE TRIGGER trg_protect_superadmin BEFORE UPDATE OR DELETE ON "user" ' + "FOR EACH ROW EXECUTE FUNCTION protect_superadmin()" + ) + + +def downgrade() -> None: + op.execute('DROP TRIGGER IF EXISTS trg_protect_superadmin ON "user"') + op.execute("DROP FUNCTION IF EXISTS protect_superadmin()") + op.drop_column("user", "is_superadmin") diff --git a/alembic/versions/u1e7a3c9d6f8_sales_projection_owner.py b/alembic/versions/u1e7a3c9d6f8_sales_projection_owner.py new file mode 100644 index 0000000..44353d7 --- /dev/null +++ b/alembic/versions/u1e7a3c9d6f8_sales_projection_owner.py @@ -0,0 +1,36 @@ +"""sales projection rows belong to the MR who entered them + +Until now the form was one shared sheet, so two MRs overwrote each other and the NAME / +HEADQUARTER columns had no reliable value. Rows with no owner stay as the shared product +list every MR starts from; an MR's first entry on a product becomes their own row. + +Revision ID: u1e7a3c9d6f8 +Revises: t0d6f2b8c5e7 +Create Date: 2026-10-27 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "u1e7a3c9d6f8" +down_revision: str | None = "t0d6f2b8c5e7" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.add_column("sales_projection", sa.Column("owner_user_id", sa.UUID(), nullable=True)) + op.create_foreign_key( + "fk_sales_projection_owner", "sales_projection", "user", ["owner_user_id"], ["id"], + ondelete="RESTRICT", + ) + op.create_index("ix_sales_projection_owner", "sales_projection", ["owner_user_id"]) + + +def downgrade() -> None: + op.drop_index("ix_sales_projection_owner", table_name="sales_projection") + op.drop_constraint("fk_sales_projection_owner", "sales_projection", type_="foreignkey") + op.drop_column("sales_projection", "owner_user_id") diff --git a/alembic/versions/v2f8b4d0e7a9_sales_projection_submission.py b/alembic/versions/v2f8b4d0e7a9_sales_projection_submission.py new file mode 100644 index 0000000..97dd1db --- /dev/null +++ b/alembic/versions/v2f8b4d0e7a9_sales_projection_submission.py @@ -0,0 +1,31 @@ +"""an MR submits a month of their Sales Projection + +Revision ID: v2f8b4d0e7a9 +Revises: u1e7a3c9d6f8 +Create Date: 2026-10-27 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "v2f8b4d0e7a9" +down_revision: str | None = "u1e7a3c9d6f8" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.create_table( + "sales_projection_submission", + sa.Column("owner_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="CASCADE"), nullable=False), + sa.Column("month", sa.String(20), nullable=False), + sa.Column("submitted_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), + sa.PrimaryKeyConstraint("owner_user_id", "month"), + ) + + +def downgrade() -> None: + op.drop_table("sales_projection_submission") diff --git a/alembic/versions/w3a9c5e1f8b0_drop_sales_projection_submission.py b/alembic/versions/w3a9c5e1f8b0_drop_sales_projection_submission.py new file mode 100644 index 0000000..fb98816 --- /dev/null +++ b/alembic/versions/w3a9c5e1f8b0_drop_sales_projection_submission.py @@ -0,0 +1,31 @@ +"""drop the Sales Projection month submission + +Revision ID: w3a9c5e1f8b0 +Revises: v2f8b4d0e7a9 +Create Date: 2026-10-27 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "w3a9c5e1f8b0" +down_revision: str | None = "v2f8b4d0e7a9" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.drop_table("sales_projection_submission") + + +def downgrade() -> None: + op.create_table( + "sales_projection_submission", + sa.Column("owner_user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="CASCADE"), nullable=False), + sa.Column("month", sa.String(20), nullable=False), + sa.Column("submitted_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), + sa.PrimaryKeyConstraint("owner_user_id", "month"), + ) diff --git a/alembic/versions/x4b0d6f2a9c1_stock_statement_owner.py b/alembic/versions/x4b0d6f2a9c1_stock_statement_owner.py new file mode 100644 index 0000000..2ce8378 --- /dev/null +++ b/alembic/versions/x4b0d6f2a9c1_stock_statement_owner.py @@ -0,0 +1,34 @@ +"""sales and stock statement rows belong to the MR who entered them + +Same change as Sales Projection: rows with no owner stay as the shared product list. + +Revision ID: x4b0d6f2a9c1 +Revises: w3a9c5e1f8b0 +Create Date: 2026-10-27 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "x4b0d6f2a9c1" +down_revision: str | None = "w3a9c5e1f8b0" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.add_column("sales_and_stock_statement", sa.Column("owner_user_id", sa.UUID(), nullable=True)) + op.create_foreign_key( + "fk_sales_stock_owner", "sales_and_stock_statement", "user", ["owner_user_id"], ["id"], + ondelete="RESTRICT", + ) + op.create_index("ix_sales_stock_owner", "sales_and_stock_statement", ["owner_user_id"]) + + +def downgrade() -> None: + op.drop_index("ix_sales_stock_owner", table_name="sales_and_stock_statement") + op.drop_constraint("fk_sales_stock_owner", "sales_and_stock_statement", type_="foreignkey") + op.drop_column("sales_and_stock_statement", "owner_user_id") diff --git a/alembic/versions/y5c1e7a3b0d2_sfe_role.py b/alembic/versions/y5c1e7a3b0d2_sfe_role.py new file mode 100644 index 0000000..c8dd669 --- /dev/null +++ b/alembic/versions/y5c1e7a3b0d2_sfe_role.py @@ -0,0 +1,44 @@ +"""Senior Field Executive: a field role like MR that may cover several headquarters + +Revision ID: y5c1e7a3b0d2 +Revises: x4b0d6f2a9c1 +Create Date: 2026-10-27 +""" + +from collections.abc import Sequence + +import sqlalchemy as sa + +from alembic import op + +revision: str = "y5c1e7a3b0d2" +down_revision: str | None = "x4b0d6f2a9c1" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.drop_constraint("ck_user_mr_requires_hq", "user", type_="check") + op.create_check_constraint( + "ck_user_mr_requires_hq", + "user", + "(role IN ('MR', 'SFE') AND headquarters_id IS NOT NULL) " + "OR (role NOT IN ('MR', 'SFE') AND headquarters_id IS NULL)", + ) + op.create_table( + "user_extra_headquarters", + sa.Column("user_id", sa.UUID(), sa.ForeignKey("user.id", ondelete="CASCADE"), primary_key=True), + sa.Column( + "headquarters_id", sa.UUID(), sa.ForeignKey("headquarters.id", ondelete="RESTRICT"), primary_key=True + ), + ) + + +def downgrade() -> None: + op.drop_table("user_extra_headquarters") + op.drop_constraint("ck_user_mr_requires_hq", "user", type_="check") + op.create_check_constraint( + "ck_user_mr_requires_hq", + "user", + "(role = 'MR' AND headquarters_id IS NOT NULL) OR (role <> 'MR' AND headquarters_id IS NULL)", + ) diff --git a/app/approvals/state_machine.py b/app/approvals/state_machine.py index ad7836d..a563a7b 100644 --- a/app/approvals/state_machine.py +++ b/app/approvals/state_machine.py @@ -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}, diff --git a/app/auth/dependencies.py b/app/auth/dependencies.py index 053dad3..9af4705 100644 --- a/app/auth/dependencies.py +++ b/app/auth/dependencies.py @@ -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): diff --git a/app/auth/schemas.py b/app/auth/schemas.py index 66b2be0..0e7f062 100644 --- a/app/auth/schemas.py +++ b/app/auth/schemas.py @@ -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] = [] diff --git a/app/chemists/models.py b/app/chemists/models.py index 912fcd9..2fc2272 100644 --- a/app/chemists/models.py +++ b/app/chemists/models.py @@ -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") diff --git a/app/chemists/router.py b/app/chemists/router.py index 05abe2d..e87a324 100644 --- a/app/chemists/router.py +++ b/app/chemists/router.py @@ -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)) diff --git a/app/chemists/schemas.py b/app/chemists/schemas.py index 92e7feb..981cf13 100644 --- a/app/chemists/schemas.py +++ b/app/chemists/schemas.py @@ -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 + diff --git a/app/chemists/service.py b/app/chemists/service.py index fcfa1b6..0355bfb 100644 --- a/app/chemists/service.py +++ b/app/chemists/service.py @@ -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 + ) diff --git a/app/common/enums.py b/app/common/enums.py index 02631aa..beea06c 100644 --- a/app/common/enums.py +++ b/app/common/enums.py @@ -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" diff --git a/app/common/master_approval.py b/app/common/master_approval.py new file mode 100644 index 0000000..2dce427 --- /dev/null +++ b/app/common/master_approval.py @@ -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 diff --git a/app/config/settings.py b/app/config/settings.py index 6d4b378..8437b9b 100644 --- a/app/config/settings.py +++ b/app/config/settings.py @@ -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. diff --git a/app/csv_data/approval.py b/app/csv_data/approval.py index 1829bbf..013edd4 100644 --- a/app/csv_data/approval.py +++ b/app/csv_data/approval.py @@ -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 diff --git a/app/csv_data/config.py b/app/csv_data/config.py index 3cbef86..545e658 100644 --- a/app/csv_data/config.py +++ b/app/csv_data/config.py @@ -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", } \ No newline at end of file diff --git a/app/csv_data/router.py b/app/csv_data/router.py index 96277c4..33d81f4 100644 --- a/app/csv_data/router.py +++ b/app/csv_data/router.py @@ -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], diff --git a/app/database/all_models.py b/app/database/all_models.py index 0677386..5aa03a7 100644 --- a/app/database/all_models.py +++ b/app/database/all_models.py @@ -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", ] diff --git a/app/doctors/models.py b/app/doctors/models.py index 858202c..9b35915 100644 --- a/app/doctors/models.py +++ b/app/doctors/models.py @@ -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") diff --git a/app/doctors/router.py b/app/doctors/router.py index f7533dd..82da814 100644 --- a/app/doctors/router.py +++ b/app/doctors/router.py @@ -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)) diff --git a/app/doctors/schemas.py b/app/doctors/schemas.py index 9d95795..123d720 100644 --- a/app/doctors/schemas.py +++ b/app/doctors/schemas.py @@ -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) diff --git a/app/doctors/service.py b/app/doctors/service.py index fcd552b..c3a4f16 100644 --- a/app/doctors/service.py +++ b/app/doctors/service.py @@ -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 + ) diff --git a/app/organization/models.py b/app/organization/models.py index 917ec9f..626f203 100644 --- a/app/organization/models.py +++ b/app/organization/models.py @@ -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"), ), diff --git a/app/organization/service.py b/app/organization/service.py index 49947ab..f6d5a63 100644 --- a/app/organization/service.py +++ b/app/organization/service.py @@ -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, diff --git a/app/pdf/forms.py b/app/pdf/forms.py index 4f91dbe..3e56f0a 100644 --- a/app/pdf/forms.py +++ b/app/pdf/forms.py @@ -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 ". 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, + } diff --git a/app/pdf/service.py b/app/pdf/service.py index 9630e70..4595b8e 100644 --- a/app/pdf/service.py +++ b/app/pdf/service.py @@ -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: diff --git a/app/pdf/templates/dcr.html b/app/pdf/templates/dcr.html index 64dd633..97a2327 100644 --- a/app/pdf/templates/dcr.html +++ b/app/pdf/templates/dcr.html @@ -19,19 +19,27 @@ {% block form %} {{ grid(grids.doctors, left=True) }} -

CHEMIST VISIT

+

{{ "CHEMISTS TO VISIT" if is_plan else "CHEMIST VISIT" }}

{{ grid(grids.chemists, left=True) }} + {% if is_plan %} + {% if mr_remarks %}
REMARKS : {{ mr_remarks }}
{% endif %} + {% else %} + {% if grids.missed %} +

PLANNED VISITS NOT MADE

+ {{ grid(grids.missed, left=True) }} + {% endif %}
STOCKIST VISIT : {{ stockist_visit }}
ORDER : {{ order_value }}
+ {% endif %} {% endblock %} {% block signature %} ' in html and '' in html def test_no_form_prints_dashboard_extras(): @@ -224,3 +222,19 @@ def test_plain_text_fallback_keeps_the_form_content_readable(): text = _html_to_text(_env.get_template("forms/fare_chart.html").render(**ctx)) assert "Puri" in text and "APPROVAL SIGNATURE: Meera" in text and "<" not in text assert _minimal_pdf(text).startswith(b"%PDF") + + +def test_expense_statement_splits_rupees_and_paise_and_totals_every_column(): + rows = _rows("expense_statement", 2, from_location="BBSR") + rows[0].update(report_date="2026-10-03", fare="120.5", da_ex="150", other_expense="20") + rows[1].update(report_date="2026-10-01", da_hq="100", postage="15.75", total="999") # a stale total is ignored + ctx = _ctx("expense_statement", rows, approval_status="approved", am_name="Anita Das") + body = ctx["grids"]["main"]["rows"] + assert len(body) == 27 + assert body[0]["cells"][4:9] == ["120", "50", "", "150", ""] + assert body[0]["cells"][13:15] == ["290", "50"] + assert body[1]["cells"][13:15] == ["115", "75"] + assert ctx["sums"]["total_rs"] == "406" and ctx["sums"]["total_p"] == "25" + assert ctx["sums"]["claimed"] == "406.25" + assert (ctx["period_from"], ctx["period_to"]) == ("01/10/2026", "03/10/2026") + assert ctx["approval"]["value"] == "Anita Das" diff --git a/web/src/App.tsx b/web/src/App.tsx index 3454ae3..ead9a2d 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -10,6 +10,7 @@ import { NotFoundPage } from "./pages/NotFoundPage"; import { ProductsPage } from "./pages/ProductsPage"; import { ProfilePage } from "./pages/ProfilePage"; import { AdminPage } from "./pages/admin/AdminPage"; +import { UserDetailPage } from "./pages/admin/UserDetailPage"; import { UsersPage } from "./pages/admin/UsersPage"; import { FormsPage } from "./pages/forms/FormsPage"; import { NewReportPage } from "./pages/reports/NewReportPage"; @@ -20,16 +21,28 @@ import type { Role } from "./types"; /** Which roles may open which screens; anything else renders "not found". */ const ROUTES: { pattern: string; roles?: Role[]; render: (params: Record) => JSX.Element }[] = [ { pattern: "/", render: () => }, - { pattern: "/reports", roles: ["MR", "AM", "GM"], render: () => }, - { pattern: "/reports/new", roles: ["MR"], render: () => }, - { pattern: "/reports/:id", roles: ["MR", "AM", "GM"], render: (p) => }, + // Daily Call Report (Plan) and (Done) each have their own list, new-report form and report pages. + { pattern: "/reports", roles: ["MR", "SFE", "AM", "GM"], render: () => }, + { pattern: "/reports/plan", roles: ["MR", "SFE", "AM", "GM"], render: () => }, + { pattern: "/reports/done", roles: ["MR", "SFE", "AM", "GM"], render: () => }, + { pattern: "/reports/plan/new", roles: ["MR", "SFE"], render: () => }, + { pattern: "/reports/done/new", roles: ["MR", "SFE"], render: () => }, + { pattern: "/reports/plan/:id", roles: ["MR", "SFE", "AM", "GM"], render: (p) => }, + { pattern: "/reports/done/:id", roles: ["MR", "SFE", "AM", "GM"], render: (p) => }, + { pattern: "/reports/self", roles: ["AM", "GM"], render: () => }, + { pattern: "/reports/self/new", roles: ["AM"], render: () => }, + { pattern: "/reports/self/:id", roles: ["AM", "GM"], render: (p) => }, + // Links from before the split. + { pattern: "/reports/new", roles: ["MR", "SFE"], render: () => }, + { pattern: "/reports/:id", roles: ["MR", "SFE", "AM", "GM"], render: (p) => }, { pattern: "/directory", render: () => }, { pattern: "/products", render: () => }, - { pattern: "/forms", roles: ["MR", "AM", "ADMIN"], render: () => }, - { pattern: "/forms/:table", roles: ["MR", "AM", "ADMIN"], render: (p) => }, + { pattern: "/forms", roles: ["MR", "SFE", "AM", "GM", "ADMIN"], render: () => }, + { pattern: "/forms/:table", roles: ["MR", "SFE", "AM", "GM", "ADMIN"], render: (p) => }, { pattern: "/profile", render: () => }, - { pattern: "/admin", roles: ["ADMIN"], render: () => }, + { pattern: "/admin", roles: ["ADMIN", "MR", "SFE"], render: () => }, { pattern: "/users", roles: ["ADMIN", "GM"], render: () => }, + { pattern: "/users/:id", roles: ["ADMIN", "GM"], render: (p) => }, ]; function Routes({ role }: { role: Role }) { diff --git a/web/src/api/index.ts b/web/src/api/index.ts index 0adc9a5..3acc9b8 100644 --- a/web/src/api/index.ts +++ b/web/src/api/index.ts @@ -1,6 +1,6 @@ import type { Approval, AreaManagerMapping, Chemist, CsvRow, CsvTable, Dcr, DcrListItem, Doctor, FormOwner, Headquarters, - FormApproval, Product, Profile, StatusCounts, StockInventory, User, + FormApproval, Product, Profile, UserOverview, ReportType, StatusCounts, StockInventory, User, } from "../types"; import { API_BASE, auth, download, json, request } from "./client"; @@ -24,14 +24,19 @@ export const api = { profile: () => request("/users/me/profile"), // daily call reports - reports: () => request("/dcr?limit=200"), - counts: () => request("/dcr/counts"), + /** `type` limits the list to Daily Call Report (Plan) or (Done); omitted, both. */ + /** `headquartersId` narrows a manager's list to one of their headquarters. */ + reports: (type?: ReportType, headquartersId?: string) => request(`/dcr${query({ limit: "200", type, headquarters_id: headquartersId })}`), + counts: (headquartersId?: string) => request(`/dcr/counts${query({ headquarters_id: headquartersId })}`), report: (id: string) => request(`/dcr/${id}`), createReport: (payload: Payload) => request("/dcr", json("POST", payload)), updateReport: (id: string, payload: Payload) => request(`/dcr/${id}`, json("PATCH", payload)), addDoctorCall: (id: string, payload: Payload) => request(`/dcr/${id}/calls/doctor`, json("POST", payload)), addChemistCall: (id: string, payload: Payload) => request(`/dcr/${id}/calls/chemist`, json("POST", payload)), deleteCall: (id: string, callId: string) => request(`/dcr/${id}/calls/${callId}`, json("DELETE")), + /** End-of-day report: why a planned doctor/chemist (`target` holds doctor_id or chemist_id) was not visited. */ + setMissedReason: (id: string, target: Payload, reason: string) => request(`/dcr/${id}/missed`, json("PUT", { ...target, reason })), + clearMissedReason: (id: string, missedId: string) => request(`/dcr/${id}/missed/${missedId}`, json("DELETE")), submit: (id: string) => request(`/dcr/${id}/submit`, json("POST")), returnReport: (id: string, reason: string) => request(`/dcr/${id}/return`, json("POST", { reason })), approve: (id: string, comment: string) => request(`/dcr/${id}/approve`, json("POST", { comment: comment || null })), @@ -41,8 +46,14 @@ export const api = { // masters and organisation doctors: () => request("/doctors"), chemists: () => request("/chemists"), + /** Doctors / chemists added by an MR that wait for (or were refused by) the Area Manager. */ + doctorRequests: () => request("/doctors/requests"), + chemistRequests: () => request("/chemists/requests"), + reviewMaster: (kind: "doctor" | "chemist", id: string, approve: boolean, reason?: string) => + request(`/${kind}s/${id}/${approve ? "approve" : "reject"}`, json("POST", approve ? undefined : { reason })), headquarters: () => request("/headquarters"), users: () => request("/users"), + userOverview: (id: string) => request(`/users/${id}/overview`), areaManagerMappings: () => request("/headquarters/area-managers"), setAreaManagerHeadquarters: (areaManagerId: string, headquarters_ids: string[]) => request(`/headquarters/area-managers/${areaManagerId}`, json("PUT", { headquarters_ids })), diff --git a/web/src/components/admin/HeadquartersChecklist.tsx b/web/src/components/admin/HeadquartersChecklist.tsx index 150b65c..e24b2ac 100644 --- a/web/src/components/admin/HeadquartersChecklist.tsx +++ b/web/src/components/admin/HeadquartersChecklist.tsx @@ -1,6 +1,6 @@ import type { Headquarters } from "../../types"; -/** Pick any number of headquarters. `owners` shows who manages each one today. */ +/** Pick any number of headquarters. `owners` shows who else manages each one today (a headquarters may have several). */ export function HeadquartersChecklist({ headquarters, selected, onChange, owners = {}, disabled = false }: { headquarters: Headquarters[]; selected: ReadonlySet; @@ -21,7 +21,7 @@ export function HeadquartersChecklist({ headquarters, selected, onChange, owners toggle(h.id)} /> {h.code} · {h.name} - {owners[h.id] && Managed by {owners[h.id]}} + {owners[h.id] && Area Manager: {owners[h.id]}} ))} diff --git a/web/src/components/admin/MasterRequests.tsx b/web/src/components/admin/MasterRequests.tsx new file mode 100644 index 0000000..68cd763 --- /dev/null +++ b/web/src/components/admin/MasterRequests.tsx @@ -0,0 +1,114 @@ +import { useState } from "react"; +import { Badge } from "../ui/Badge"; +import { Button } from "../ui/Button"; +import { Card, CardBody, CardHeader } from "../ui/Card"; +import { DataTable, type Column } from "../ui/DataTable"; +import { Field, Textarea } from "../ui/Field"; +import { Modal } from "../ui/Modal"; +import { ErrorState } from "../ui/States"; +import { useToast } from "../ui/Toast"; +import { LIVE, useAsync } from "../../hooks/useAsync"; +import { api } from "../../api"; +import { errorMessage, formatDate } from "../../lib/format"; +import { useUser } from "../../lib/session"; +import type { Chemist, Doctor } from "../../types"; + +type Request = { kind: "doctor" | "chemist"; item: Doctor | Chemist }; + +const address = (r: Request) => (r.kind === "doctor" ? (r.item as Doctor).clinic_address : (r.item as Chemist).shop_address); + +/** + * Doctors and chemists an MR added that wait for the Area Manager. + * `mine`: the MR's own requests, with any rejection and its reason. + * `review`: what waits for this Area Manager (or the admin) to approve or reject; the GM only looks. + * Shows nothing when there is nothing to show. + */ +export function MasterRequests({ mode, onReviewed }: { mode: "mine" | "review"; onReviewed?: () => void }) { + const user = useUser(); + const toast = useToast(); + const canAct = mode === "review" && (user.role === "AM" || user.role === "ADMIN"); + const data = useAsync(async () => { + const [doctors, chemists] = await Promise.all([api.doctorRequests(), api.chemistRequests()]); + return [...doctors.map((item) => ({ kind: "doctor" as const, item })), ...chemists.map((item) => ({ kind: "chemist" as const, item }))] + .sort((a, b) => b.item.created_at.localeCompare(a.item.created_at)); + }, [], { poll: LIVE }); + const [acting, setActing] = useState(null); + const [rejecting, setRejecting] = useState(null); + const [reason, setReason] = useState(""); + const [reasonError, setReasonError] = useState(null); + const rows = data.data ?? []; + + async function decide(r: Request, approve: boolean, why?: string) { + setActing(r.item.id); + try { + await api.reviewMaster(r.kind, r.item.id, approve, why); + toast.success(approve ? `${r.item.name} approved and added to the list` : `${r.item.name} rejected`); + setRejecting(null); + await data.reload(); + onReviewed?.(); + } catch (e) { + toast.error(errorMessage(e, approve ? "Could not approve" : "Could not reject")); + } finally { + setActing(null); + } + } + + if (data.error) return void data.reload()} />; + if (rows.length === 0) return null; + + const columns: Column[] = [ + { key: "kind", header: "Type", render: (r) => {r.kind === "doctor" ? "Doctor" : "Chemist"}, nowrap: true }, + { key: "name", header: "Name", render: (r) => {r.item.name} }, + { key: "address", header: "Address", render: address }, + ...(mode === "review" ? [{ key: "by", header: "Added by", render: (r: Request) => r.item.requested_by_name ?? "—", nowrap: true }] : []), + { key: "on", header: "Added on", render: (r) => formatDate(r.item.created_at), nowrap: true }, + mode === "mine" + ? { + key: "status", header: "Status", + render: (r) => r.item.approval_status === "REJECTED" + ? Rejected{r.item.rejection_reason && — {r.item.rejection_reason}} + : Waiting for Area Manager, + } + : { + key: "_actions", header: "", align: "right", nowrap: true, + render: (r) => canAct ? ( + + + + + ) : Waiting for Area Manager, + }, + ]; + + const pending = rows.filter((r) => r.item.approval_status === "PENDING").length; + return ( + <> + + + + `${r.kind}-${r.item.id}`} loading={data.loading} minWidth={720} /> + + + setRejecting(null)} + footer={<>} + > + +
-
MR Signature
+
{{ signer_label | default("MR Signature") }}
{{ mr_signed }}
diff --git a/app/pdf/templates/dcr.txt b/app/pdf/templates/dcr.txt index d91f275..01a5998 100644 --- a/app/pdf/templates/dcr.txt +++ b/app/pdf/templates/dcr.txt @@ -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 %} \ No newline at end of file diff --git a/app/pdf/templates/forms/_macros.html b/app/pdf/templates/forms/_macros.html index a4c9b6b..cd49c85 100644 --- a/app/pdf/templates/forms/_macros.html +++ b/app/pdf/templates/forms/_macros.html @@ -30,7 +30,7 @@ {% for w in g.widths %}{% endfor %} - {% for h in g.headers %}{% endfor %} + {% for h in g.headers %}{% endfor %} {% for r in g.rows %} diff --git a/app/pdf/templates/forms/expense_statement.html b/app/pdf/templates/forms/expense_statement.html new file mode 100644 index 0000000..b7fe965 --- /dev/null +++ b/app/pdf/templates/forms/expense_statement.html @@ -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) }} +
{{ h }}
{% for part in h.split("\n") %}{{ part }}{% if not loop.last %}
{% endif %}{% endfor %}
+ + + + + + +
Name {{ meta_rows[0][0][1] }}Designation {{ meta_rows[0][1][1] }}H.Q. {{ meta_rows[1][0][1] }}Date
+

Expenses Statement from Dt. {{ period_from }} to {{ period_to }}

+ + {% set g = grids.main %} + + {% for w in g.widths %}{% endfor %} + + + + + + + + + + + + + + {% for h in g.headers[2:-1] %}{% endfor %} + + + + {% for r in g.rows %} + {% for c in r.cells %}{% endfor %} + {% endfor %} + + + + + + + + + + +
DATEPlace Of VisitFareDaily AllowancePostageOther ExpenceTotalBusiness Achived
{{ h }}
{{ c }}
TOTAL{{ sums.fare_rs }}{{ sums.fare_p }}{{ sums.da_hq }}{{ sums.da_ex }}{{ sums.da_out }}{{ sums.postage_rs }}{{ sums.postage_p }}{{ sums.other_expense_rs }}{{ sums.other_expense_p }}{{ sums.total_rs }}{{ sums.total_p }}
+ + + + + + + +
+
+
For Office Use Only
+
Amount Claimed {{ sums.claimed }}   Amt. Passed
+
Deduction / Addition with reason
+
+
+
+
Report Status
+
Daily Report :-
+
Stock and Sale Statement :-
+
Weekly Report :-
+
Monthly Apprasal :-
+
+
+
Signature :- {{ approval.mr_name }}
+
Checked by :- {% if approval.value != "PENDING" %}{{ approval.value }}{% endif %}
+
DATE :- {{ approval.when }}
+
+{% endblock %} diff --git a/app/pdf/templates/forms/sales_projection.html b/app/pdf/templates/forms/sales_projection.html index 4f902be..fc11a87 100644 --- a/app/pdf/templates/forms/sales_projection.html +++ b/app/pdf/templates/forms/sales_projection.html @@ -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 %} + + {% for w in g.widths %}{% endfor %} + + + {% for h in g.headers[:3] %}{% endfor %} + + + + {% for h in g.headers[3:] %}{% endfor %} + + + {% for r in g.rows %} + {% for c in r.cells %}{% endfor %} + {% endfor %} + + + {% for t in totals %}{% endfor %} + + +
{{ h }}CLOSINGPROJECTION
{{ h }}
{{ c }}
TOTAL{{ t }}
{% endblock %} diff --git a/app/products/service.py b/app/products/service.py index e48a081..c1b9ba3 100644 --- a/app/products/service.py +++ b/app/products/service.py @@ -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()) diff --git a/app/reports/models.py b/app/reports/models.py index de8a889..67c2cfc 100644 --- a/app/reports/models.py +++ b/app/reports/models.py @@ -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") diff --git a/app/reports/repository.py b/app/reports/repository.py index abc1165..3d37513 100644 --- a/app/reports/repository.py +++ b/app/reports/repository.py @@ -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), diff --git a/app/reports/router.py b/app/reports/router.py index 65b559a..4cf5857 100644 --- a/app/reports/router.py +++ b/app/reports/router.py @@ -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))) diff --git a/app/reports/schemas.py b/app/reports/schemas.py index 0e9419e..1ee7bb6 100644 --- a/app/reports/schemas.py +++ b/app/reports/schemas.py @@ -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): diff --git a/app/reports/serializers.py b/app/reports/serializers.py index 30bf8a1..9712be2 100644 --- a/app/reports/serializers.py +++ b/app/reports/serializers.py @@ -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, diff --git a/app/reports/service.py b/app/reports/service.py index 5b4afc0..31032f5 100644 --- a/app/reports/service.py +++ b/app/reports/service.py @@ -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() diff --git a/app/users/models.py b/app/users/models.py index d8c0738..edb236c 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -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 + ) diff --git a/app/users/overview.py b/app/users/overview.py new file mode 100644 index 0000000..6ce8c4a --- /dev/null +++ b/app/users/overview.py @@ -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 diff --git a/app/users/router.py b/app/users/router.py index 23eb901..2dbcf9e 100644 --- a/app/users/router.py +++ b/app/users/router.py @@ -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, diff --git a/app/users/schemas.py b/app/users/schemas.py index 8b27a06..a8cfbd2 100644 --- a/app/users/schemas.py +++ b/app/users/schemas.py @@ -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): diff --git a/app/users/service.py b/app/users/service.py index 916b5b5..364684a 100644 --- a/app/users/service.py +++ b/app/users/service.py @@ -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, diff --git a/docker-compose.yml b/docker-compose.yml index ecc66d6..ba7f635 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,6 +29,26 @@ services: restart: unless-stopped logging: *default-logging + # Daily pg_dump of the whole database into ./backups on the host, the last 14 days kept. + # See scripts/backup.sh for how to restore one. + backup: + image: postgres:16-alpine + depends_on: + db: + condition: service_healthy + environment: + PGHOST: db + PGUSER: ${POSTGRES_USER:-pharma} + PGPASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env} + PGDATABASE: ${POSTGRES_DB:-pharma} + BACKUP_KEEP_DAYS: ${BACKUP_KEEP_DAYS:-14} + volumes: + - ./backups:/backups + - ./scripts/backup.sh:/backup.sh:ro + entrypoint: ["/bin/sh", "/backup.sh"] + restart: unless-stopped + logging: *default-logging + api: build: . depends_on: @@ -42,6 +62,7 @@ services: PDF_STORAGE_PATH: /srv/storage/pdf BACKDATING_WINDOW_DAYS: ${BACKDATING_WINDOW_DAYS:-7} SUBMISSION_DEADLINE_DAYS: ${SUBMISSION_DEADLINE_DAYS:-1} + PLAN_AHEAD_DAYS: ${PLAN_AHEAD_DAYS:-2} WEB_CONCURRENCY: ${WEB_CONCURRENCY:-2} volumes: - pdfstore:/srv/storage/pdf diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..32a1c09 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# Daily database backup, run by the `backup` service in docker-compose.yml. +# +# Takes a compressed pg_dump of the whole database once a day (and once straight away on +# start), keeps the last BACKUP_KEEP_DAYS days, and writes them to ./backups on the host so +# they can be copied off the server. Restore one with: +# gunzip -c backups/pharma-YYYYMMDD-HHMMSS.sql.gz | docker compose exec -T db psql -U pharma -d pharma +set -eu + +KEEP_DAYS="${BACKUP_KEEP_DAYS:-14}" +INTERVAL="${BACKUP_INTERVAL_SECONDS:-86400}" +mkdir -p /backups + +while true; do + stamp=$(date -u +%Y%m%d-%H%M%S) + target="/backups/${PGDATABASE}-${stamp}.sql.gz" + # Written under a temporary name, so a half-written file is never taken for a backup. + if pg_dump --clean --if-exists --no-owner | gzip > "${target}.part"; then + mv "${target}.part" "${target}" + echo "backup: wrote ${target} ($(du -h "${target}" | cut -f1))" + else + rm -f "${target}.part" + echo "backup: pg_dump FAILED at ${stamp}" >&2 + fi + find /backups -name "${PGDATABASE}-*.sql.gz" -mtime "+${KEEP_DAYS}" -delete + sleep "${INTERVAL}" +done diff --git a/scripts/create_superadmin.py b/scripts/create_superadmin.py new file mode 100644 index 0000000..5e5f538 --- /dev/null +++ b/scripts/create_superadmin.py @@ -0,0 +1,59 @@ +"""Create (or rotate the password of) the protected super admin. + + python -m scripts.create_superadmin --email owner@example.com --password 'S3cret-...' + +The super admin has the ADMIN role, so it can do everything an administrator can, but no +one can edit, deactivate, re-role or delete it afterwards (API and DB trigger both refuse). +Re-running with the same email only resets its password and clears any lockout. +""" + +import argparse +import asyncio +import sys + +from sqlalchemy import select + +from app.auth.security import hash_password +from app.common.enums import Role +from app.database.session import AsyncSessionLocal +from app.organization.models import Company +from app.users.models import User + + +async def run(email: str, password: str, name: str, code: str) -> None: + async with AsyncSessionLocal() as db: + existing = ( + await db.execute(select(User).where(User.is_superadmin.is_(True))) + ).scalar_one_or_none() + if existing is not None: + if existing.email != email.lower(): + sys.exit(f"A super admin already exists ({existing.email}); only one is allowed.") + existing.password_hash = hash_password(password) + existing.failed_login_attempts = 0 + await db.commit() + print(f"Super admin {existing.email}: password reset.") + return + + company = (await db.execute(select(Company).order_by(Company.created_at))).scalars().first() + if company is None: + sys.exit("No company exists yet — seed or create one first.") + db.add( + User( + company_id=company.id, email=email.lower(), password_hash=hash_password(password), + full_name=name, employee_code=code, role=Role.ADMIN, is_superadmin=True, + ) + ) + await db.commit() + print(f"Super admin created: {email.lower()}") + + +if __name__ == "__main__": + p = argparse.ArgumentParser() + p.add_argument("--email", required=True) + p.add_argument("--password", required=True) + p.add_argument("--name", default="Super Admin") + p.add_argument("--code", default="SUPERADMIN") + a = p.parse_args() + if len(a.password) < 12: + sys.exit("Use a password of at least 12 characters.") + asyncio.run(run(a.email, a.password, a.name, a.code)) diff --git a/scripts/import_csv_data.py b/scripts/import_csv_data.py index 812c286..fcbbf55 100644 --- a/scripts/import_csv_data.py +++ b/scripts/import_csv_data.py @@ -29,7 +29,7 @@ CSV_TABLES = { "Sales_and_Stock_Statement.csv": ("sales_and_stock_statement", ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "opening", "receipt", "sale", "closing", "value_in_pts"]), "TOUR_PLAN.csv": ("tour_plan", ["name", "headquarter", "serial_no", "report_date", "from_location", "to_location", "pob", "stockiest_visit"]), "STANDARD_TOUR_PLAN.csv": ("standard_tour_plan", ["name", "headquarter", "serial_no", "place"]), - "sales_projection.csv": ("sales_projection", ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "pts", "secondary_qty", "value", "closing", "secondary_value", "aug_sales", "value_2"]), + "sales_projection.csv": ("sales_projection", ["name", "headquarter", "report_date", "month", "serial_no", "product_name", "pts", "closing_qty", "closing_value", "projection_qty", "projection_value"]), } SERIAL_COLUMNS = {"serial_no", "doctor_sr_no", "chemist_sr_no"} @@ -57,7 +57,7 @@ async def apply_sales_projection_account(db) -> None: headquarter = headquarters.name FROM "user" AS account LEFT JOIN headquarters ON headquarters.id = account.headquarters_id - WHERE account.role = 'MR' ''' + WHERE account.id = projection.owner_user_id ''' ) ) @@ -76,13 +76,12 @@ async def import_csv_data(only_empty: bool = False) -> None: continue else: await db.execute(text(f'DELETE FROM "{table_name}"')) - source_columns = columns[4:] if table_name == "sales_projection" else columns + # The Sales Projection CSV is the product list: only Sl. No., product and PTS come + # from it; every figure is filled in later, per month, on the form. + source_columns = ["serial_no", "product_name", "pts"] if table_name == "sales_projection" else columns rows = read_rows(path, source_columns) if table_name == "sales_projection": - rows = [ - (row_number, {"name": "", "headquarter": "", "report_date": "", "month": "", **data}) - for row_number, data in rows - ] + rows = [(row_number, {**{c: "" for c in columns}, **data}) for row_number, data in rows] if rows: column_names = ", ".join(f'"{column}"' for column in columns) parameters = ", ".join(f":{column}" for column in columns) diff --git a/tests/conftest.py b/tests/conftest.py index af33f39..a2fb757 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -37,6 +37,7 @@ from app.users.models import User # noqa: E402 TABLES = [ "audit_log", "product", + "missed_visit", "doctor_call", "chemist_call", "daily_call_report", @@ -169,6 +170,34 @@ async def org(db: AsyncSession) -> dict: } +@pytest.fixture +async def approved_plan(db: AsyncSession, org: dict): + """An empty day plan, already approved, so an end-of-day report may be filed. + + For tests about end-of-day rules where the plan is incidental; the plan flow itself + is exercised through the API in tests/e2e/test_day_plan.py. + """ + from app.common.enums import DCRStatus, ReportType + from app.common.utils import utcnow + from app.reports.models import DailyCallReport + + async def make(day: date | None = None, mr: str = "mr1") -> DailyCallReport: + plan = DailyCallReport( + id=uuid.uuid4(), + mr_user_id=org[mr].id, + headquarters_id=org[mr].headquarters_id, + report_date=day or date.today(), + report_type=ReportType.PLAN, + status=DCRStatus.APPROVED, + approved_at=utcnow(), + ) + db.add(plan) + await db.commit() + return plan + + return make + + async def login(client: AsyncClient, user: User) -> dict[str, str]: resp = await client.post( "/api/v1/auth/login", diff --git a/tests/e2e/test_area_manager_headquarters.py b/tests/e2e/test_area_manager_headquarters.py index 40cbee2..b739279 100644 --- a/tests/e2e/test_area_manager_headquarters.py +++ b/tests/e2e/test_area_manager_headquarters.py @@ -38,16 +38,26 @@ class TestAreaManagerHeadquarters: mine = {r["headquarters_id"] for r in listing.json() if r["area_manager_user_id"] == str(org["am1"].id)} assert mine == set(wanted) - async def test_taking_a_headquarters_moves_it_from_the_other_am(self, client, auth, org): - # am2 currently manages hq2; give hq2 to am1 as well. + async def test_a_headquarters_can_have_several_area_managers(self, client, auth, org): + # am2 manages hq2; give hq2 to am1 as well — am2 keeps it. + resp = await client.put( + f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], + json={"headquarters_ids": [str(org["hq1"].id), str(org["hq2"].id)]}, + ) + assert resp.status_code == 200, resp.text + listing = (await client.get(f"{API}/area-managers", headers=auth["admin"])).json() + managers = {r["area_manager_user_id"] for r in listing if r["headquarters_id"] == str(org["hq2"].id)} + assert managers == {str(org["am1"].id), str(org["am2"].id)} + # Both see it, so either may review its MRs' reports and forms. + for am in ("am1", "am2"): + assert str(org["hq2"].id) in {h["id"] for h in (await client.get(API, headers=auth[am])).json()} + # Saving the same set again adds nothing twice. await client.put( f"{API}/area-managers/{org['am1'].id}", headers=auth["admin"], json={"headquarters_ids": [str(org["hq1"].id), str(org["hq2"].id)]}, ) - listing = (await client.get(f"{API}/area-managers", headers=auth["admin"])).json() - owners = {r["headquarters_id"]: r["area_manager_user_id"] for r in listing} - assert owners[str(org["hq2"].id)] == str(org["am1"].id) # one AM per HQ still holds - assert len(listing) == len(owners) + again = (await client.get(f"{API}/area-managers", headers=auth["admin"])).json() + assert len(again) == len(listing) async def test_omitted_headquarters_are_released(self, client, auth, org): resp = await client.put( diff --git a/tests/e2e/test_dashboard_hq_filter.py b/tests/e2e/test_dashboard_hq_filter.py new file mode 100644 index 0000000..7bc8337 --- /dev/null +++ b/tests/e2e/test_dashboard_hq_filter.py @@ -0,0 +1,24 @@ +"""The dashboard's headquarters filter narrows a manager's reports and counts, never widens them.""" + +API = "/api/v1" + + +class TestHeadquartersFilter: + async def test_reports_and_counts_narrow_to_the_chosen_headquarters(self, client, auth, org, approved_plan): + await approved_plan(mr="mr1") # hq1 + await approved_plan(mr="mr2") # hq2 + hq1, hq2 = str(org["hq1"].id), str(org["hq2"].id) + + everything = (await client.get(f"{API}/dcr", headers=auth["gm"])).json() + assert len(everything) == 2 + only_hq1 = (await client.get(f"{API}/dcr", headers=auth["gm"], params={"headquarters_id": hq1})).json() + assert [r["mr_name"] for r in only_hq1] == ["Ravi Das"] + counts = (await client.get(f"{API}/dcr/counts", headers=auth["admin"], params={"headquarters_id": hq2})).json() + assert counts["APPROVED"] == 1 + + async def test_a_headquarters_outside_the_users_scope_gives_nothing(self, client, auth, org, approved_plan): + await approved_plan(mr="mr2") # hq2, not am1's + outside = await client.get(f"{API}/dcr", headers=auth["am1"], params={"headquarters_id": str(org["hq2"].id)}) + assert outside.status_code == 200 and outside.json() == [] + counts = (await client.get(f"{API}/dcr/counts", headers=auth["am1"], params={"headquarters_id": str(org["hq2"].id)})).json() + assert counts["APPROVED"] == 0 diff --git a/tests/e2e/test_day_plan.py b/tests/e2e/test_day_plan.py new file mode 100644 index 0000000..b9a336c --- /dev/null +++ b/tests/e2e/test_day_plan.py @@ -0,0 +1,244 @@ +"""Morning day plan → Area Manager approval → end-of-day report against it (rules P1-P5).""" + +from datetime import date + +from tests.e2e.test_full_approval_flow import _create_masters + +API = "/api/v1" +TODAY = date.today().isoformat() + + +async def _plan(client, auth, doctors, chemists, *, approve=True): + """A plan for today with the given doctors/chemists, submitted (and by default approved).""" + resp = await client.post( + f"{API}/dcr", headers=auth["mr1"], + json={ + "report_date": TODAY, "report_type": "PLAN", + "doctor_calls": [{"doctor_id": d["id"], "visit_time": "23:00:00"} for d in doctors], + "chemist_calls": [{"chemist_id": c["id"], "visit_time": "23:30:00"} for c in chemists], + }, + ) + assert resp.status_code == 201, resp.text + plan_id = resp.json()["id"] + assert (await client.post(f"{API}/dcr/{plan_id}/submit", headers=auth["mr1"])).status_code == 200 + if approve: + approved = await client.post(f"{API}/dcr/{plan_id}/approve", headers=auth["am1"], json={}) + assert approved.status_code == 200, approved.text + return plan_id + + +async def _actual(client, auth): + resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"}) + assert resp.status_code == 201, resp.text + return resp.json() + + +async def _visit_doctor(client, auth, dcr_id, doctor): + return await client.post( + f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctor["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, + ) + + +class TestDayPlan: + async def test_a_plan_has_planned_times_and_no_outcomes(self, client, auth, org): + doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "PLAN"}) + assert resp.status_code == 201, resp.text + plan = resp.json() + assert plan["report_type"] == "PLAN" and plan["plan_id"] is None + # P1 — a planned time later than now is fine, and an outcome is not stored. + added = await client.post( + f"{API}/dcr/{plan['id']}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctors[0]["id"], "visit_time": "23:59:00", "call_outcome": "MET"}, + ) + assert added.status_code == 201, added.text + assert added.json()["doctor_calls"][0]["call_outcome"] is None + + async def test_an_empty_plan_cannot_be_submitted(self, client, auth, org): + resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "PLAN"}) + submit = await client.post(f"{API}/dcr/{resp.json()['id']}/submit", headers=auth["mr1"]) + assert submit.status_code == 422 + assert "at least one" in submit.json()["error"]["message"] + + async def test_one_plan_and_one_end_of_day_report_per_day(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await _plan(client, auth, doctors[:1], []) + again = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "PLAN"}) + assert again.status_code == 409 + await _actual(client, auth) + twice = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"}) + assert twice.status_code == 409 + + async def test_the_area_manager_can_return_a_plan(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + plan_id = await _plan(client, auth, doctors, [], approve=False) + resp = await client.post( + f"{API}/dcr/{plan_id}/return", headers=auth["am1"], json={"reason": "Add the chemists on MG Road."} + ) + assert resp.status_code == 200 and resp.json()["status"] == "RETURNED" + + async def test_reports_can_be_listed_by_type(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + plan_id = await _plan(client, auth, doctors, []) + actual = await _actual(client, auth) + plans = (await client.get(f"{API}/dcr?type=PLAN", headers=auth["mr1"])).json() + actuals = (await client.get(f"{API}/dcr?type=ACTUAL", headers=auth["mr1"])).json() + assert [r["id"] for r in plans] == [plan_id] + assert [r["id"] for r in actuals] == [actual["id"]] + + +class TestEndOfDayNeedsAnApprovedPlan: + async def test_without_a_plan_it_is_refused(self, client, auth, org): + resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"}) + assert resp.status_code == 422 + assert "Daily Call Report (Plan)" in resp.json()["error"]["message"] + + async def test_while_the_plan_awaits_approval_it_is_refused(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await _plan(client, auth, doctors, [], approve=False) + resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"}) + assert resp.status_code == 422 + assert "approved by the Area Manager" in resp.json()["error"]["message"] + + async def test_once_approved_it_is_linked_to_the_plan(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + plan_id = await _plan(client, auth, doctors, []) + actual = await _actual(client, auth) + assert actual["plan_id"] == plan_id + plan = (await client.get(f"{API}/dcr/{plan_id}", headers=auth["mr1"])).json() + assert plan["actual_report_id"] == actual["id"] + + +class TestPlanVersusActual: + async def test_every_planned_visit_must_be_made_or_explained(self, client, auth, org): + doctors, chemists = await _create_masters(client, auth, org, n_doctors=3, n_chemists=1) + await _plan(client, auth, doctors[:2], chemists) + dcr = await _actual(client, auth) + assert {p["status"] for p in dcr["planned_visits"]} == {"PENDING"} + assert len(dcr["planned_visits"]) == 3 + + # P5 — an unplanned visit is allowed; it is not on the checklist. + assert (await _visit_doctor(client, auth, dcr["id"], doctors[2])).status_code == 201 + body = (await _visit_doctor(client, auth, dcr["id"], doctors[0])).json() + by_name = {p["name"]: p["status"] for p in body["planned_visits"]} + assert by_name[doctors[0]["name"]] == "VISITED" + + # P3 — the doctor and chemist still pending block the submit, by name. + blocked = await client.post(f"{API}/dcr/{dcr['id']}/submit", headers=auth["mr1"]) + assert blocked.status_code == 422 + assert set(blocked.json()["error"]["details"]["pending"]) == {doctors[1]["name"], chemists[0]["name"]} + + await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr1"], + json={"doctor_id": doctors[1]["id"], "reason": "Clinic closed for the day"}) + explained = await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr1"], + json={"chemist_id": chemists[0]["id"], "reason": "Shop shut"}) + assert explained.status_code == 200, explained.text + missed = [p for p in explained.json()["planned_visits"] if p["status"] == "MISSED"] + assert {p["missed_reason"] for p in missed} == {"Clinic closed for the day", "Shop shut"} + + submit = await client.post(f"{API}/dcr/{dcr['id']}/submit", headers=auth["mr1"]) + assert submit.status_code == 200, submit.text + approve = await client.post(f"{API}/dcr/{dcr['id']}/approve", headers=auth["am1"], json={}) + assert approve.status_code == 200, approve.text + pdf = await client.get(f"{API}/dcr/{dcr['id']}/pdf", headers=auth["mr1"]) + assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-") + + async def test_recording_the_visit_later_replaces_the_missed_note(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await _plan(client, auth, doctors, []) + dcr = await _actual(client, auth) + await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr1"], + json={"doctor_id": doctors[0]["id"], "reason": "Doctor on leave"}) + body = (await _visit_doctor(client, auth, dcr["id"], doctors[0])).json() + assert body["planned_visits"][0]["status"] == "VISITED" + assert body["planned_visits"][0]["missed_reason"] is None + # ...and removing the visit brings the line back to pending, not to the old note. + call_id = body["doctor_calls"][0]["id"] + removed = (await client.delete(f"{API}/dcr/{dcr['id']}/calls/{call_id}", headers=auth["mr1"])).json() + assert removed["planned_visits"][0]["status"] == "PENDING" + + async def test_missed_notes_are_validated(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + plan_id = await _plan(client, auth, doctors[:1], []) + dcr = await _actual(client, auth) + url = f"{API}/dcr/{dcr['id']}/missed" + # Not on the plan. + off_plan = await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[1]["id"], "reason": "Busy day"}) + assert off_plan.status_code == 422 + # Exactly one target, and a real reason. + assert (await client.put(url, headers=auth["mr1"], json={"reason": "Busy day"})).status_code == 422 + assert (await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": " "})).status_code == 422 + # Already visited. + await _visit_doctor(client, auth, dcr["id"], doctors[0]) + visited = await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": "Busy day"}) + assert visited.status_code == 409 + # Not on a plan at all. + plan_note = await client.put(f"{API}/dcr/{plan_id}/missed", headers=auth["mr1"], + json={"doctor_id": doctors[0]["id"], "reason": "Busy day"}) + assert plan_note.status_code in (409, 422) + + async def test_a_note_can_be_reworded_and_cleared(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await _plan(client, auth, doctors, []) + dcr = await _actual(client, auth) + url = f"{API}/dcr/{dcr['id']}/missed" + await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": "Doctor on leave"}) + body = (await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": "On leave till Monday"})).json() + line = body["planned_visits"][0] + assert line["missed_reason"] == "On leave till Monday" + cleared = await client.delete(f"{url}/{line['missed_visit_id']}", headers=auth["mr1"]) + assert cleared.status_code == 200 and cleared.json()["planned_visits"][0]["status"] == "PENDING" + + async def test_an_end_of_day_visit_needs_an_outcome(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await _plan(client, auth, doctors, []) + dcr = await _actual(client, auth) + resp = await client.post( + f"{API}/dcr/{dcr['id']}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00"}, + ) + assert resp.status_code == 422 + assert "call_outcome" in resp.json()["error"]["message"] + + async def test_only_the_owning_mr_writes_missed_notes(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await _plan(client, auth, doctors, []) + dcr = await _actual(client, auth) + body = {"doctor_id": doctors[0]["id"], "reason": "Doctor on leave"} + assert (await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["am1"], json=body)).status_code == 403 + assert (await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr2"], json=body)).status_code == 403 + + +class TestPlanPdf: + async def test_an_approved_plan_downloads_as_a_daily_call_plan(self, client, auth, org, db): + from app.pdf.service import PDFService + from app.reports.repository import DCRRepository + + doctors, chemists = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + plan_id = await _plan(client, auth, doctors, chemists) + pdf = await client.get(f"{API}/dcr/{plan_id}/pdf", headers=auth["mr1"]) + assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-") + assert 'filename="PLAN-MR001-' in pdf.headers["content-disposition"] + + import uuid + + context = await PDFService(db)._context(await DCRRepository(db).get_full(uuid.UUID(plan_id))) + assert context["title"] == "DAILY CALL REPORT (PLAN)" + assert "PLANNED TIME" in context["grids"]["doctors"]["headers"] + assert context["grids"]["doctors"]["rows"][0]["cells"][3] == "23:00" + + +class TestPlanningAhead: + async def test_a_plan_may_be_made_up_to_two_days_ahead_but_a_done_report_never(self, client, auth, org): + from datetime import timedelta + + from app.common.utils import local_today + + day = lambda n: (local_today() + timedelta(days=n)).isoformat() # noqa: E731 + made = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": day(2), "report_type": "PLAN"}) + assert made.status_code == 201, made.text + too_far = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": day(3), "report_type": "PLAN"}) + assert too_far.status_code == 422 + done = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": day(1), "report_type": "ACTUAL"}) + assert done.status_code == 422 diff --git a/tests/e2e/test_dcr_form_fields.py b/tests/e2e/test_dcr_form_fields.py index b990236..84ca92e 100644 --- a/tests/e2e/test_dcr_form_fields.py +++ b/tests/e2e/test_dcr_form_fields.py @@ -7,16 +7,17 @@ from tests.e2e.test_full_approval_flow import _create_masters API = "/api/v1" -async def _draft(client, auth, org): +async def _draft(client, auth, org, approved_plan): doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + await approved_plan() resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()}) assert resp.status_code == 201, resp.text return resp.json()["id"], doctors, chemists class TestFormFields: - async def test_fields_round_trip_through_the_api(self, client, auth, org): - dcr_id, doctors, chemists = await _draft(client, auth, org) + async def test_fields_round_trip_through_the_api(self, client, auth, org, approved_plan): + dcr_id, doctors, chemists = await _draft(client, auth, org, approved_plan) d = await client.post( f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET", @@ -35,27 +36,48 @@ class TestFormFields: assert patched.status_code == 200, patched.text report = (await client.get(f"{API}/dcr/{dcr_id}", headers=auth["mr1"])).json() - assert report["doctor_calls"][0]["product_promote"] == "Calcium" # trimmed - assert report["doctor_calls"][0]["product_sample"] == "Vit D" + # A single name is still accepted, and comes back as a one-item list. + assert report["doctor_calls"][0]["product_promote"] == ["Calcium"] # trimmed + assert report["doctor_calls"][0]["product_sample"] == ["Vit D"] assert report["chemist_calls"][0]["pob"] == "12,500" assert report["stockist_visit"] == "Ganesh Distributors" assert report["order_value"] == "45,000" - async def test_blank_values_are_stored_as_null_and_can_be_cleared(self, client, auth, org): - dcr_id, _, _ = await _draft(client, auth, org) + async def test_several_products_per_visit(self, client, auth, org, approved_plan): + dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan) + d = await client.post( + f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET", + "product_promote": ["Calcium", " Vit D ", "calcium", "", "Zinc"], + "product_sample": ["Iron", "Omega 3", "B12"]}, + ) + assert d.status_code == 201, d.text + call = d.json()["doctor_calls"][0] + assert call["product_promote"] == ["Calcium", "Vit D", "Zinc"] # trimmed, de-duplicated + assert call["product_sample"] == ["Iron", "Omega 3", "B12"] + + too_many = await client.post( + f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctors[1]["id"], "visit_time": "09:40:00", "call_outcome": "MET", + "product_promote": [f"P{i}" for i in range(11)]}, + ) + assert too_many.status_code == 422 + + async def test_blank_values_are_stored_as_null_and_can_be_cleared(self, client, auth, org, approved_plan): + dcr_id, _, _ = await _draft(client, auth, org, approved_plan) await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "X"}) cleared = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": " "}) assert cleared.json()["stockist_visit"] is None - async def test_patching_one_field_leaves_the_others_alone(self, client, auth, org): - dcr_id, _, _ = await _draft(client, auth, org) + async def test_patching_one_field_leaves_the_others_alone(self, client, auth, org, approved_plan): + dcr_id, _, _ = await _draft(client, auth, org, approved_plan) await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"mr_remarks": "Good day", "order_value": "10"}) again = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "S"}) body = again.json() assert (body["mr_remarks"], body["order_value"], body["stockist_visit"]) == ("Good day", "10", "S") - async def test_too_long_values_are_rejected(self, client, auth, org): - dcr_id, doctors, _ = await _draft(client, auth, org) + async def test_too_long_values_are_rejected(self, client, auth, org, approved_plan): + dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan) too_long = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "x" * 201}) assert too_long.status_code == 422 bad_call = await client.post( @@ -64,8 +86,8 @@ class TestFormFields: ) assert bad_call.status_code == 422 - async def test_only_the_owner_can_edit_them_and_only_while_editable(self, client, auth, org): - dcr_id, doctors, _ = await _draft(client, auth, org) + async def test_only_the_owner_can_edit_them_and_only_while_editable(self, client, auth, org, approved_plan): + dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan) assert (await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr2"], json={"order_value": "1"})).status_code in (403, 404) assert (await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["am1"], json={"order_value": "1"})).status_code == 403 await client.post( @@ -76,8 +98,8 @@ class TestFormFields: locked = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"order_value": "1"}) assert locked.status_code == 409 # submitted reports are locked - async def test_approved_pdf_is_generated_with_the_new_layout(self, client, auth, org): - dcr_id, doctors, chemists = await _draft(client, auth, org) + async def test_approved_pdf_is_generated_with_the_new_layout(self, client, auth, org, approved_plan): + dcr_id, doctors, chemists = await _draft(client, auth, org, approved_plan) await client.post( f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET", "product_promote": "Calcium"}, @@ -91,8 +113,8 @@ class TestFormFields: class TestResponsesReflectTheChange: """Regression: the response to an add/remove used to list the visits as they were BEFORE it.""" - async def test_adding_a_visit_returns_it_in_the_same_response(self, client, auth, org): - dcr_id, doctors, chemists = await _draft(client, auth, org) + async def test_adding_a_visit_returns_it_in_the_same_response(self, client, auth, org, approved_plan): + dcr_id, doctors, chemists = await _draft(client, auth, org, approved_plan) added = await client.post( f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, @@ -105,8 +127,8 @@ class TestResponsesReflectTheChange: body = second.json() assert len(body["doctor_calls"]) == 1 and len(body["chemist_calls"]) == 1 - async def test_removing_a_visit_returns_the_report_without_it(self, client, auth, org): - dcr_id, doctors, _ = await _draft(client, auth, org) + async def test_removing_a_visit_returns_the_report_without_it(self, client, auth, org, approved_plan): + dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan) for i, doctor in enumerate(doctors): r = await client.post( f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], @@ -123,14 +145,14 @@ class TestResponsesReflectTheChange: class TestDownloadUsesTheCurrentLayout: - async def test_a_report_approved_under_an_older_layout_still_downloads_in_the_new_one(self, client, auth, org, db): + async def test_a_report_approved_under_an_older_layout_still_downloads_in_the_new_one(self, client, auth, org, db, approved_plan): from pathlib import Path from sqlalchemy import select from app.reports.models import DailyCallReport - dcr_id, doctors, _ = await _draft(client, auth, org) + dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan) await client.post( f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, @@ -146,14 +168,15 @@ class TestDownloadUsesTheCurrentLayout: assert pdf.status_code == 200 assert b"OLD DESIGN" not in pdf.content and pdf.content.startswith(b"%PDF-") and len(pdf.content) > 5000 # ...and still only for approved reports. - draft_id, _, _ = await _draft_second(client, auth, org) + draft_id, _, _ = await _draft_second(client, auth, org, approved_plan) assert (await client.get(f"{API}/dcr/{draft_id}/pdf", headers=auth["mr1"])).status_code == 422 -async def _draft_second(client, auth, org): +async def _draft_second(client, auth, org, approved_plan): """A second, unapproved report (yesterday's) for the same MR.""" from datetime import timedelta + await approved_plan(date.today() - timedelta(days=1)) resp = await client.post( f"{API}/dcr", headers=auth["mr1"], json={"report_date": (date.today() - timedelta(days=1)).isoformat()} ) @@ -162,14 +185,14 @@ async def _draft_second(client, auth, org): class TestApprovedPdfIsSigned: - async def test_the_pdf_names_the_approving_area_manager_from_the_audit_trail(self, client, auth, org, db): + async def test_the_pdf_names_the_approving_area_manager_from_the_audit_trail(self, client, auth, org, db, approved_plan): from sqlalchemy import select from app.pdf.service import PDFService from app.reports.models import DailyCallReport from app.reports.repository import DCRRepository - dcr_id, doctors, _ = await _draft(client, auth, org) + dcr_id, doctors, _ = await _draft(client, auth, org, approved_plan) await client.post( f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, @@ -184,10 +207,10 @@ class TestApprovedPdfIsSigned: assert context["approval"]["name"] == "Suresh Behera" assert len(context["approval"]["at"]) == 16 and context["mr_signed"] == "Ravi Das (MR001)" - async def test_a_report_that_is_not_approved_has_no_approval_signature(self, client, auth, org, db): + async def test_a_report_that_is_not_approved_has_no_approval_signature(self, client, auth, org, db, approved_plan): from app.pdf.service import PDFService from app.reports.repository import DCRRepository - dcr_id, _, _ = await _draft(client, auth, org) + dcr_id, _, _ = await _draft(client, auth, org, approved_plan) report = await DCRRepository(db).get_full(dcr_id if not isinstance(dcr_id, str) else __import__("uuid").UUID(dcr_id)) assert (await PDFService(db)._context(report))["approval"] is None diff --git a/tests/e2e/test_duplicate_requests.py b/tests/e2e/test_duplicate_requests.py index 5668a61..f27f938 100644 --- a/tests/e2e/test_duplicate_requests.py +++ b/tests/e2e/test_duplicate_requests.py @@ -33,8 +33,9 @@ async def _audit_count(db, entity_id, action) -> int: class TestReports: - async def _submitted(self, client, auth, org): + async def _submitted(self, client, auth, org, approved_plan): doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1) + await approved_plan() dcr = (await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})).json()["id"] await client.post( f"{API}/dcr/{dcr}/calls/doctor", headers=auth["mr1"], @@ -42,8 +43,8 @@ class TestReports: ) return dcr, doctors - async def test_submitting_twice_is_one_submission(self, client, auth, org, db): - dcr, _ = await self._submitted(client, auth, org) + async def test_submitting_twice_is_one_submission(self, client, auth, org, db, approved_plan): + dcr, _ = await self._submitted(client, auth, org, approved_plan) first = await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"]) second = await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"]) assert first.status_code == second.status_code == 200 @@ -51,8 +52,8 @@ class TestReports: assert second.json()["submitted_at"] == first.json()["submitted_at"] assert await _audit_count(db, dcr, "SUBMIT") == 1 - async def test_approving_twice_is_one_approval(self, client, auth, org, db): - dcr, _ = await self._submitted(client, auth, org) + async def test_approving_twice_is_one_approval(self, client, auth, org, db, approved_plan): + dcr, _ = await self._submitted(client, auth, org, approved_plan) await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"]) first = await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am1"], json={}) second = await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am1"], json={}) @@ -60,14 +61,14 @@ class TestReports: assert second.json()["approved_at"] == first.json()["approved_at"] assert await _audit_count(db, dcr, "APPROVE") == 1 - async def test_an_area_manager_of_another_hq_still_cannot_approve(self, client, auth, org): - dcr, _ = await self._submitted(client, auth, org) + async def test_an_area_manager_of_another_hq_still_cannot_approve(self, client, auth, org, approved_plan): + dcr, _ = await self._submitted(client, auth, org, approved_plan) await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"]) await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am1"], json={}) assert (await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am2"], json={})).status_code == 403 - async def test_adding_the_same_visit_or_creating_the_same_report_twice_is_refused(self, client, auth, org): - dcr, doctors = await self._submitted(client, auth, org) + async def test_adding_the_same_visit_or_creating_the_same_report_twice_is_refused(self, client, auth, org, approved_plan): + dcr, doctors = await self._submitted(client, auth, org, approved_plan) again = await client.post( f"{API}/dcr/{dcr}/calls/doctor", headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "visit_time": "10:30:00", "call_outcome": "MET"}, diff --git a/tests/e2e/test_form_approval.py b/tests/e2e/test_form_approval.py index e50f7da..2e79f6e 100644 --- a/tests/e2e/test_form_approval.py +++ b/tests/e2e/test_form_approval.py @@ -32,7 +32,7 @@ class TestFormApproval: async def test_every_approval_form_starts_as_draft_for_everyone(self, client, auth): # An MR sees all three forms as drafts; an Area Manager or admin has nothing to act on yet. forms = _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"])) - assert set(forms) == {"tour_plan", "standard_tour_plan", "fare_chart"} + assert set(forms) == {"tour_plan", "standard_tour_plan", "fare_chart", "expense_statement"} assert {f["status"] for f in forms.values()} == {"draft"} for who in ("am1", "admin"): resp = await client.get(f"{API}/approvals", headers=auth[who]) @@ -94,10 +94,9 @@ class TestFormApproval: state = _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))["fare_chart"] assert state["status"] == "draft" and state["approved_by_name"] is None # A pending (not yet approved) form is withdrawn too, so the AM never signs stale data. - # Deleting a row is admin-only — an MR can no longer delete their own rows. + # An MR may remove a row they entered by mistake; that too withdraws the request. await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"]) - assert (await client.delete(f"{API}/fare_chart/rows/{row_id}", headers=auth["mr1"])).status_code == 403 - assert (await client.delete(f"{API}/fare_chart/rows/{row_id}", headers=auth["admin"])).status_code == 204 + assert (await client.delete(f"{API}/fare_chart/rows/{row_id}", headers=auth["mr1"])).status_code == 204 assert (await client.get(f"{API}/approvals", headers=auth["am1"])).json() == [] # withdrawn: nothing left for the AM async def test_the_pdf_signature_comes_from_the_server_not_the_browser(self, client, auth, org, db, fare_chart): diff --git a/tests/e2e/test_form_gm_oversight.py b/tests/e2e/test_form_gm_oversight.py new file mode 100644 index 0000000..edf5d38 --- /dev/null +++ b/tests/e2e/test_form_gm_oversight.py @@ -0,0 +1,67 @@ +"""Company forms: listed in the company's order, and readable (never writable) by the GM.""" + +from tests.e2e.test_form_ownership import forms # noqa: F401 - creates the form tables + +API = "/api/v1" + +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", +] + + +class TestFormsForTheGeneralManager: + async def test_forms_are_listed_in_the_company_order(self, client, auth, org): + for who in ("mr1", "gm", "admin"): + tables = (await client.get(f"{API}/csv-data/tables", headers=auth[who])).json() + assert [t["name"] for t in tables] == ORDER + + async def test_gm_reads_every_mrs_rows_and_pdf(self, client, auth, org, forms): # noqa: F811 + added = await client.post( + f"{API}/csv-data/tour_plan/rows", headers=auth["mr1"], json={"from_location": "Puri"} + ) + assert added.status_code == 201, added.text + rows = (await client.get(f"{API}/csv-data/tour_plan/rows", headers=auth["gm"])).json() + assert [r["from_location"] for r in rows] == ["Puri"] + owners = (await client.get(f"{API}/csv-data/owners", headers=auth["gm"])).json() + assert {o["id"] for o in owners} == {str(org["mr1"].id), str(org["mr2"].id)} + mine = await client.get( + f"{API}/csv-data/tour_plan/rows?owner_id={org['mr1'].id}", headers=auth["gm"] + ) + assert mine.status_code == 200 and len(mine.json()) == 1 + pdf = await client.get(f"{API}/csv-data/tour_plan/pdf", headers=auth["gm"]) + assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-") + + async def test_gm_cannot_change_or_approve_a_form(self, client, auth, org, forms): # noqa: F811 + row = ( + await client.post( + f"{API}/csv-data/tour_plan/rows", headers=auth["mr1"], json={"to_location": "Puri"} + ) + ).json() + add = await client.post( + f"{API}/csv-data/tour_plan/rows?owner_id={org['mr1'].id}", + headers=auth["gm"], + json={"to_location": "Cuttack"}, + ) + assert add.status_code == 403 + edit = await client.patch( + f"{API}/csv-data/tour_plan/rows/{row['id']}", headers=auth["gm"], json={"pob": "1"} + ) + assert edit.status_code == 403 + delete = await client.delete( + f"{API}/csv-data/tour_plan/rows/{row['id']}", headers=auth["gm"] + ) + assert delete.status_code == 403 + approve = await client.post( + f"{API}/csv-data/tour_plan/approval/approve?owner_id={org['mr1'].id}", + headers=auth["gm"], + ) + assert approve.status_code == 403 diff --git a/tests/e2e/test_form_ownership.py b/tests/e2e/test_form_ownership.py index 5e8b4fe..00e7875 100644 --- a/tests/e2e/test_form_ownership.py +++ b/tests/e2e/test_form_ownership.py @@ -151,3 +151,48 @@ class TestMonthlyDownload: async def test_a_form_without_a_month_rejects_the_filter(self, client, auth, forms): resp = await client.get(f"{API}/fare_chart/pdf", headers=auth["mr1"], params={"month": "November"}) assert resp.status_code == 422 + + +class TestExpenseStatement: + async def test_the_total_is_worked_out_by_the_server(self, client, auth, db): + await _make_table(db, "expense_statement", owned=True) + made = await client.post( + f"{API}/expense_statement/rows", headers=auth["mr1"], + json={"report_date": "2026-10-01", "fare": "120.50", "da_hq": "100", "total": "1"}, + ) + assert made.status_code == 201, made.text + assert made.json()["total"] == "220.50" and made.json()["name"] == "Ravi Das" + row = made.json()["id"] + changed = await client.patch(f"{API}/expense_statement/rows/{row}", headers=auth["mr1"], json={"postage": "9.50", "total": "5"}) + assert changed.status_code == 200 and changed.json()["total"] == "230.00" + bad = await client.patch(f"{API}/expense_statement/rows/{row}", headers=auth["mr1"], json={"fare": "abc"}) + assert bad.status_code == 422 + + async def test_clearing_one_cell_of_a_row_that_has_other_values_is_allowed(self, client, auth, db): + await _make_table(db, "expense_statement", owned=True) + made = await client.post(f"{API}/expense_statement/rows", headers=auth["admin"], json={"from_location": "A", "fare": "10"}, params={"owner_id": str((await client.get(f"{API}/owners", headers=auth["admin"])).json()[0]["id"])}) + assert made.status_code == 201, made.text + cleared = await client.patch(f"{API}/expense_statement/rows/{made.json()['id']}", headers=auth["admin"], json={"fare": ""}) + assert cleared.status_code == 200, cleared.text + assert cleared.json()["fare"] == "" and cleared.json()["total"] is None + + +class TestRemovingAMistake: + async def test_an_mr_removes_their_own_row_and_the_rest_are_renumbered(self, client, auth, forms): + first = await _add(client, auth["mr1"], "A") + await _add(client, auth["mr1"], "B") + assert (await client.delete(f"{API}/fare_chart/rows/{first}", headers=auth["mr1"])).status_code == 204 + mine = (await client.get(f"{API}/fare_chart/rows", headers=auth["mr1"])).json() + assert [(r["from_location"], r["serial_no"]) for r in mine] == [("B", "1")] + + async def test_an_mr_removes_an_extra_copy_of_a_product_but_never_its_only_row(self, client, auth, db): + await _make_table(db, "sales_projection", owned=False) + made = await client.post(f"{API}/sales_projection/rows", headers=auth["admin"], json={"product_name": "INTEZOLE-PD", "pts": "106.92"}) + assert made.status_code == 201, made.text + row = made.json()["id"] + extra = await client.post(f"{API}/sales_projection/rows", headers=auth["mr1"], json={"product_name": "INTEZOLE-PD", "month": "August"}) + assert extra.status_code == 201, extra.text + assert (await client.delete(f"{API}/sales_projection/rows/{extra.json()['id']}", headers=auth["mr1"])).status_code == 204 + assert (await client.delete(f"{API}/sales_projection/rows/{row}", headers=auth["mr1"])).status_code == 403 + cleared = await client.patch(f"{API}/sales_projection/rows/{row}", headers=auth["mr1"], json={"closing_qty": "", "month": ""}) + assert cleared.status_code == 200 and cleared.json()["product_name"] == "INTEZOLE-PD" diff --git a/tests/e2e/test_full_approval_flow.py b/tests/e2e/test_full_approval_flow.py index d7827de..e3db0c8 100644 --- a/tests/e2e/test_full_approval_flow.py +++ b/tests/e2e/test_full_approval_flow.py @@ -137,18 +137,40 @@ class TestStep3And5Masters: resp = await client.get(f"{API}/doctors", headers=auth["mr2"]) assert resp.json() == [] - async def test_mr_cannot_create_a_doctor(self, client, auth, org): - """A3/Q3 — not permitted in the MVP.""" + async def test_mr_creates_doctor_and_chemist_in_own_hq(self, client, auth, org): + """The MR's new doctor and chemist are assigned to them, so they show up at once.""" + hq1 = str(org["hq1"].id) + doctor = await client.post( + f"{API}/doctors", + headers=auth["mr1"], + json={"headquarters_id": hq1, "name": "Dr Selfmade", "clinic_address": "Nowhere"}, + ) + assert doctor.status_code == 201 + chemist = await client.post( + f"{API}/chemists", + headers=auth["mr1"], + json={"headquarters_id": hq1, "name": "Selfmade Pharmacy", "shop_address": "Nowhere"}, + ) + assert chemist.status_code == 201 + + doctors = (await client.get(f"{API}/doctors", headers=auth["mr1"])).json() + assert doctor.json()["id"] in {d["id"] for d in doctors} + chemists = (await client.get(f"{API}/chemists", headers=auth["mr1"])).json() + assert chemist.json()["id"] in {c["id"] for c in chemists} + + async def test_mr_cannot_create_outside_own_hq_or_a_product(self, client, auth, org): resp = await client.post( f"{API}/doctors", headers=auth["mr1"], json={ - "headquarters_id": str(org["hq1"].id), - "name": "Dr Selfmade", + "headquarters_id": str(org["hq2"].id), + "name": "Dr Elsewhere", "clinic_address": "Nowhere", }, ) assert resp.status_code == 403 + resp = await client.post(f"{API}/products", headers=auth["mr1"], json={"name": "Pill"}) + assert resp.status_code == 403 class TestStep4Login: @@ -193,9 +215,10 @@ class TestStep4Login: class TestFullApprovalFlow: - async def test_thirteen_step_flow(self, client, auth, org): + async def test_thirteen_step_flow(self, client, auth, org, approved_plan): doctors, chemists = await _create_masters(client, auth, org) today = date.today().isoformat() + await approved_plan() # --- Step 5: MR creates a DCR --------------------------------- resp = await client.post( @@ -391,8 +414,9 @@ class TestFullApprovalFlow: class TestNegativeScope: - async def _submitted_report(self, client, auth, org): + async def _submitted_report(self, client, auth, org, approved_plan): doctors, _ = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + await approved_plan() resp = await client.post( f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()} ) @@ -409,8 +433,8 @@ class TestNegativeScope: await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"]) return dcr_id - async def test_x1_second_mr_cannot_see_the_first_mrs_report(self, client, auth, org): - dcr_id = await self._submitted_report(client, auth, org) + async def test_x1_second_mr_cannot_see_the_first_mrs_report(self, client, auth, org, approved_plan): + dcr_id = await self._submitted_report(client, auth, org, approved_plan) resp = await client.get(f"{API}/dcr/{dcr_id}", headers=auth["mr2"]) assert resp.status_code == 403, resp.text @@ -420,8 +444,8 @@ class TestNegativeScope: listing = await client.get(f"{API}/dcr", headers=auth["mr2"]) assert listing.json() == [] - async def test_x2_area_manager_of_another_hq_cannot_see_it(self, client, auth, org): - dcr_id = await self._submitted_report(client, auth, org) + async def test_x2_area_manager_of_another_hq_cannot_see_it(self, client, auth, org, approved_plan): + dcr_id = await self._submitted_report(client, auth, org, approved_plan) resp = await client.get(f"{API}/dcr/{dcr_id}", headers=auth["am2"]) assert resp.status_code == 403, resp.text @@ -435,9 +459,9 @@ class TestNegativeScope: listing = await client.get(f"{API}/dcr?status=PENDING_AM", headers=auth["am2"]) assert listing.json() == [] - async def test_x3_admin_cannot_approve(self, client, auth, org): + async def test_x3_admin_cannot_approve(self, client, auth, org, approved_plan): """SEC-2 — the single most important governance rule in the design.""" - dcr_id = await self._submitted_report(client, auth, org) + dcr_id = await self._submitted_report(client, auth, org, approved_plan) resp = await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["admin"], json={}) assert resp.status_code == 403, resp.text @@ -489,7 +513,8 @@ class TestValidationRules: assert resp.status_code == 422 assert "back-dating" in resp.json()["error"]["message"] - async def test_v4_cannot_submit_without_a_doctor_call(self, client, auth, org): + async def test_v4_cannot_submit_without_a_doctor_call(self, client, auth, org, approved_plan): + await approved_plan() resp = await client.post( f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()} ) @@ -499,8 +524,9 @@ class TestValidationRules: assert submit.status_code == 422 assert "at least one doctor call" in submit.json()["error"]["message"] - async def test_v6_unassigned_doctor_rejected(self, client, auth, org): + async def test_v6_unassigned_doctor_rejected(self, client, auth, org, approved_plan): """A doctor from another HQ, not assigned to this MR.""" + await approved_plan() other = await client.post( f"{API}/doctors", headers=auth["admin"], @@ -527,8 +553,9 @@ class TestValidationRules: assert call.status_code == 422 assert "not assigned" in call.json()["error"]["message"] - async def test_v7_conversion_status_only_for_core_doctors(self, client, auth, org): + async def test_v7_conversion_status_only_for_core_doctors(self, client, auth, org, approved_plan): doctors, _ = await _create_masters(client, auth, org, n_doctors=5, n_chemists=1) + await approved_plan() non_core = next(d for d in doctors if not d["is_core_doctor"]) resp = await client.post( @@ -549,8 +576,9 @@ class TestValidationRules: assert call.status_code == 422 assert "core doctor" in call.json()["error"]["message"] - async def test_cannot_edit_while_pending_with_the_manager(self, client, auth, org): + async def test_cannot_edit_while_pending_with_the_manager(self, client, auth, org, approved_plan): doctors, _ = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + await approved_plan() resp = await client.post( f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()} ) @@ -572,8 +600,9 @@ class TestValidationRules: assert edit.status_code == 409 assert edit.json()["error"]["code"] == "REPORT_LOCKED" - async def test_pdf_unavailable_before_approval(self, client, auth, org): + async def test_pdf_unavailable_before_approval(self, client, auth, org, approved_plan): doctors, _ = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + await approved_plan() resp = await client.post( f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()} ) @@ -585,8 +614,9 @@ class TestValidationRules: class TestStatusCounts: - async def test_counts_replace_the_dashboard(self, client, auth, org): + async def test_counts_replace_the_dashboard(self, client, auth, org, approved_plan): doctors, _ = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + await approved_plan() resp = await client.post( f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()} ) @@ -595,4 +625,4 @@ class TestStatusCounts: counts = await client.get(f"{API}/dcr/counts", headers=auth["mr1"]) assert counts.status_code == 200 assert counts.json()["DRAFT"] == 1 - assert counts.json()["APPROVED"] == 0 + assert counts.json()["APPROVED"] == 1 # the day plan the report follows diff --git a/tests/e2e/test_master_approval.py b/tests/e2e/test_master_approval.py new file mode 100644 index 0000000..9c858c7 --- /dev/null +++ b/tests/e2e/test_master_approval.py @@ -0,0 +1,102 @@ +"""A doctor or chemist an MR adds waits for their Area Manager; a doctor visit records a gift.""" + +from datetime import date + +API = "/api/v1" + + +async def _add_doctor(client, auth, org, name="Dr New", who="mr1", hq="hq1"): + resp = await client.post( + f"{API}/doctors", headers=auth[who], + json={"headquarters_id": str(org[hq].id), "name": name, "clinic_address": "Clinic 1, Bhubaneswar"}, + ) + assert resp.status_code == 201, resp.text + return resp.json() + + +async def _names(client, headers, path="/doctors"): + return [d["name"] for d in (await client.get(f"{API}{path}", headers=headers)).json()] + + +class TestAnMrsDoctorWaitsForTheAreaManager: + async def test_it_is_pending_and_unlisted_until_approved(self, client, auth, org): + doctor = await _add_doctor(client, auth, org) + assert doctor["approval_status"] == "PENDING" and doctor["requested_by_name"] == "Ravi Das" + assert "Dr New" not in await _names(client, auth["mr1"]) + assert "Dr New" not in await _names(client, auth["am1"]) + assert await _names(client, auth["mr1"], "/doctors/requests") == ["Dr New"] + assert await _names(client, auth["am1"], "/doctors/requests") == ["Dr New"] + assert await _names(client, auth["am2"], "/doctors/requests") == [] # another headquarters + + approved = await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["am1"]) + assert approved.status_code == 200 and approved.json()["approval_status"] == "APPROVED" + assert "Dr New" in await _names(client, auth["mr1"]) # in the list, assigned to the MR who asked + assert await _names(client, auth["mr1"], "/doctors/requests") == [] + + async def test_only_the_mrs_own_area_manager_or_the_admin_decides(self, client, auth, org): + doctor = await _add_doctor(client, auth, org) + assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["am2"])).status_code == 403 + assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["mr1"])).status_code == 403 + assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["gm"])).status_code == 403 + assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["admin"])).status_code == 200 + + async def test_a_rejection_tells_the_mr_why_and_adding_it_again_resends_it(self, client, auth, org): + doctor = await _add_doctor(client, auth, org) + rejected = await client.post(f"{API}/doctors/{doctor['id']}/reject", headers=auth["am1"], json={"reason": "Wrong clinic"}) + assert rejected.status_code == 200 + mine = (await client.get(f"{API}/doctors/requests", headers=auth["mr1"])).json() + assert [(d["approval_status"], d["rejection_reason"]) for d in mine] == [("REJECTED", "Wrong clinic")] + assert await _names(client, auth["am1"], "/doctors/requests") == [] + + again = await _add_doctor(client, auth, org) + assert again["id"] == doctor["id"] and again["approval_status"] == "PENDING" and again["rejection_reason"] is None + assert await _names(client, auth["am1"], "/doctors/requests") == ["Dr New"] + + async def test_a_pending_doctor_cannot_be_visited(self, client, auth, org, approved_plan): + doctor = await _add_doctor(client, auth, org) + await approved_plan() + dcr = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()}) + assert dcr.status_code == 201, dcr.text + visit = await client.post( + f"{API}/dcr/{dcr.json()['id']}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctor["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, + ) + assert visit.status_code == 422 + + async def test_the_admins_own_doctor_is_approved_at_once(self, client, auth, org): + doctor = await _add_doctor(client, auth, org, who="admin") + assert doctor["approval_status"] == "APPROVED" and doctor["requested_by_name"] is None + + +class TestAnMrsChemistWaitsToo: + async def test_it_is_pending_until_approved(self, client, auth, org): + resp = await client.post( + f"{API}/chemists", headers=auth["mr1"], + json={"headquarters_id": str(org["hq1"].id), "name": "New Pharmacy", "shop_address": "Shop 2, Bhubaneswar"}, + ) + assert resp.status_code == 201 and resp.json()["approval_status"] == "PENDING" + assert "New Pharmacy" not in await _names(client, auth["mr1"], "/chemists") + assert (await client.post(f"{API}/chemists/{resp.json()['id']}/approve", headers=auth["am1"])).status_code == 200 + assert "New Pharmacy" in await _names(client, auth["mr1"], "/chemists") + + +class TestGift: + async def test_a_doctor_visit_records_the_gift_and_the_pdf_notes_it(self, client, auth, org, approved_plan): + doctor = await _add_doctor(client, auth, org, who="admin", name="Dr Gifted") + assert (await client.post(f"{API}/doctors/{doctor['id']}/assign", headers=auth["admin"], json={"mr_user_id": str(org["mr1"].id)})).status_code == 201 + await approved_plan() + dcr = (await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})).json() + visit = await client.post( + f"{API}/dcr/{dcr['id']}/calls/doctor", headers=auth["mr1"], + json={"doctor_id": doctor["id"], "visit_time": "09:30:00", "call_outcome": "MET", "gift": "Diary"}, + ) + assert visit.status_code == 201, visit.text + calls = (await client.get(f"{API}/dcr/{dcr['id']}", headers=auth["mr1"])).json()["doctor_calls"] + assert [c["gift"] for c in calls] == ["Diary"] + + from types import SimpleNamespace + + from app.pdf.forms import _doctor_remarks + + call = SimpleNamespace(call_outcome="MET", gift="Diary", remarks="Interested") + assert _doctor_remarks(call) == "Gift: Diary. Interested" diff --git a/tests/e2e/test_self_report.py b/tests/e2e/test_self_report.py new file mode 100644 index 0000000..d137928 --- /dev/null +++ b/tests/e2e/test_self_report.py @@ -0,0 +1,141 @@ +"""An Area Manager's own Daily Call Report (Self): no plan, no approval, final on submit.""" + +from datetime import date + +from tests.e2e.test_full_approval_flow import _create_masters + +API = "/api/v1" +TODAY = date.today().isoformat() + + +async def _self_report(client, auth, **extra): + return await client.post( + f"{API}/dcr", + headers=auth["am1"], + json={"report_date": TODAY, "report_type": "SELF", **extra}, + ) + + +class TestSelfReport: + async def test_am_files_and_submits_without_a_plan(self, client, auth, org): + doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1) + created = await _self_report(client, auth) + assert created.status_code == 201, created.text + report = created.json() + assert report["report_type"] == "SELF" + assert report["headquarters_id"] == str(org["hq1"].id) # the one HQ am1 manages + dcr_id = report["id"] + + # Any doctor of the headquarters, though it is assigned to an MR, not to the AM. + visit = await client.post( + f"{API}/dcr/{dcr_id}/calls/doctor", + headers=auth["am1"], + json={ + "doctor_id": doctors[0]["id"], + "visit_time": "09:30:00", + "call_outcome": "MET", + "product_promote": ["Calcium", "Zinc"], + "product_sample": ["Iron"], + }, + ) + assert visit.status_code == 201, visit.text + chem = await client.post( + f"{API}/dcr/{dcr_id}/calls/chemist", + headers=auth["am1"], + json={"chemist_id": chemists[0]["id"], "visit_time": "10:00:00"}, + ) + assert chem.status_code == 201, chem.text + + submitted = await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["am1"]) + assert submitted.status_code == 200, submitted.text + assert submitted.json()["status"] == "APPROVED" # final: nobody approves it + assert submitted.json()["is_locked"] is True + + locked = await client.patch( + f"{API}/dcr/{dcr_id}", headers=auth["am1"], json={"mr_remarks": "x"} + ) + assert locked.status_code == 409 + pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth["am1"]) + assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-") + + async def test_self_list_is_own_and_kept_out_of_the_mr_lists(self, client, auth, org): + dcr_id = (await _self_report(client, auth)).json()["id"] + own = (await client.get(f"{API}/dcr?type=SELF", headers=auth["am1"])).json() + assert [r["id"] for r in own] == [dcr_id] + assert (await client.get(f"{API}/dcr?type=SELF", headers=auth["am2"])).json() == [] + everything = (await client.get(f"{API}/dcr", headers=auth["am1"])).json() + assert dcr_id not in {r["id"] for r in everything} + counts = (await client.get(f"{API}/dcr/counts", headers=auth["am1"])).json() + assert counts["DRAFT"] == 0 + + async def test_one_self_report_per_date(self, client, auth, org): + assert (await _self_report(client, auth)).status_code == 201 + assert (await _self_report(client, auth)).status_code == 409 + + async def test_needs_a_headquarters_the_am_manages(self, client, auth, org): + resp = await _self_report(client, auth, headquarters_id=str(org["hq2"].id)) + assert resp.status_code == 403 + + async def test_only_an_am_files_one_and_an_am_files_no_plan(self, client, auth, org): + mr = await client.post( + f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "SELF"} + ) + assert mr.status_code == 403 + plan = await client.post( + f"{API}/dcr", headers=auth["am1"], json={"report_date": TODAY, "report_type": "PLAN"} + ) + assert plan.status_code == 403 + + async def test_doctor_from_another_headquarters_is_refused(self, client, auth, org): + other = await client.post( + f"{API}/doctors", + headers=auth["admin"], + json={ + "headquarters_id": str(org["hq2"].id), + "name": "Dr Far", + "clinic_address": "Sambalpur", + }, + ) + dcr_id = (await _self_report(client, auth)).json()["id"] + resp = await client.post( + f"{API}/dcr/{dcr_id}/calls/doctor", + headers=auth["am1"], + json={"doctor_id": other.json()["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, + ) + assert resp.status_code == 422 + + async def test_nobody_else_can_touch_it(self, client, auth, org): + dcr_id = (await _self_report(client, auth)).json()["id"] + assert ( + await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"]) + ).status_code == 403 + approve = await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["am1"], json={}) + assert approve.status_code == 409 # a draft cannot be approved; submitting finalises it + + +class TestGeneralManagerOversight: + async def test_gm_sees_every_self_report_read_only(self, client, auth, org): + doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=0) + dcr_id = (await _self_report(client, auth)).json()["id"] + await client.post( + f"{API}/dcr/{dcr_id}/calls/doctor", + headers=auth["am1"], + json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"}, + ) + await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["am1"]) + + listed = (await client.get(f"{API}/dcr?type=SELF", headers=auth["gm"])).json() + assert [r["id"] for r in listed] == [dcr_id] + assert listed[0]["mr_name"] == org["am1"].full_name + assert (await client.get(f"{API}/dcr/{dcr_id}", headers=auth["gm"])).status_code == 200 + pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth["gm"]) + assert pdf.status_code == 200 + + edit = await client.patch( + f"{API}/dcr/{dcr_id}", headers=auth["gm"], json={"mr_remarks": "x"} + ) + assert edit.status_code == 403 + own = await client.post( + f"{API}/dcr", headers=auth["gm"], json={"report_date": TODAY, "report_type": "SELF"} + ) + assert own.status_code == 403 diff --git a/tests/e2e/test_user_role_change.py b/tests/e2e/test_user_role_change.py new file mode 100644 index 0000000..c115700 --- /dev/null +++ b/tests/e2e/test_user_role_change.py @@ -0,0 +1,76 @@ +"""An Administrator can change another user's role; the old role's assignments end.""" + +import uuid + +from sqlalchemy import select + +from app.doctors.models import DoctorAssignment +from tests.conftest import DEFAULT_PASSWORD + +API = "/api/v1" + + +async def _patch(client, auth, user, body, as_="admin"): + return await client.patch(f"{API}/users/{user.id}", headers=auth[as_], json=body) + + +class TestAdminChangesRole: + async def test_mr_becomes_am_and_loses_hq_and_doctors(self, client, auth, org, db): + doctor = await client.post( + f"{API}/doctors", + headers=auth["mr1"], + json={"headquarters_id": str(org["hq1"].id), "name": "Dr A", "clinic_address": "Puri"}, + ) + assert doctor.status_code == 201 + + resp = await _patch(client, auth, org["mr1"], {"role": "AM"}) + assert resp.status_code == 200, resp.text + assert resp.json()["role"] == "AM" + assert resp.json()["headquarters_id"] is None + + # The doctor stays in the headquarters but is no longer assigned to anyone. + active = await db.execute( + select(DoctorAssignment).where( + DoctorAssignment.doctor_id == uuid.UUID(doctor.json()["id"]), + DoctorAssignment.effective_to.is_(None), + ) + ) + assert active.scalars().first() is None + + async def test_am_becomes_mr_and_releases_headquarters(self, client, auth, org): + resp = await _patch( + client, auth, org["am1"], {"role": "MR", "headquarters_id": str(org["hq2"].id)} + ) + assert resp.status_code == 200, resp.text + assert resp.json()["headquarters_id"] == str(org["hq2"].id) + + mappings = await client.get(f"{API}/headquarters/area-managers", headers=auth["admin"]) + assert str(org["am1"].id) not in {m["area_manager_user_id"] for m in mappings.json()} + + async def test_gm_becomes_am(self, client, auth, org): + resp = await _patch(client, auth, org["gm"], {"role": "AM"}) + assert resp.status_code == 200, resp.text + assert resp.json()["role"] == "AM" + + async def test_role_change_signs_the_user_out(self, client, auth, org): + login = await client.post( + f"{API}/auth/login", + json={"email": org["gm"].email, "password": DEFAULT_PASSWORD}, + ) + await _patch(client, auth, org["gm"], {"role": "AM"}) + again = await client.post( + f"{API}/auth/refresh", json={"refresh_token": login.json()["refresh_token"]} + ) + assert again.status_code == 401 + + async def test_becoming_mr_needs_a_headquarters(self, client, auth, org): + resp = await _patch(client, auth, org["gm"], {"role": "MR"}) + assert resp.status_code == 422 + + async def test_admin_cannot_change_own_role(self, client, auth, org): + resp = await _patch(client, auth, org["admin"], {"role": "GM"}) + assert resp.status_code == 422 + + async def test_only_admin_can_change_a_role(self, client, auth, org): + resp = await _patch(client, auth, org["mr1"], {"role": "AM"}, as_="gm") + assert resp.status_code == 403 diff --git a/tests/unit/test_business_timezone.py b/tests/unit/test_business_timezone.py index 65e6a9d..a422c55 100644 --- a/tests/unit/test_business_timezone.py +++ b/tests/unit/test_business_timezone.py @@ -11,6 +11,7 @@ from zoneinfo import ZoneInfo import pytest +from app.common.enums import ReportType from app.common.exceptions import ValidationError from app.reports import service as svc @@ -36,7 +37,7 @@ def _service() -> svc.DCRService: def _report(day: date) -> SimpleNamespace: - return SimpleNamespace(report_date=day) + return SimpleNamespace(report_date=day, report_type=ReportType.ACTUAL) class TestVisitTime: @@ -57,12 +58,19 @@ class TestVisitTime: class TestReportDate: def test_today_is_todays_ist_date_just_after_midnight(self, at): at(2026, 9, 21, 1, 0) # still 20 Sep in UTC - _service()._validate_report_date(date(2026, 9, 21)) + _service()._validate_report_date(date(2026, 9, 21), ReportType.ACTUAL) - def test_tomorrow_is_rejected(self, at): + def test_tomorrow_is_rejected_for_a_done_or_self_report(self, at): at(2026, 9, 21, 1, 0) + for report_type in (ReportType.ACTUAL, ReportType.SELF): + with pytest.raises(ValidationError): + _service()._validate_report_date(date(2026, 9, 22), report_type) + + def test_a_plan_may_be_made_up_to_two_days_ahead(self, at): + at(2026, 9, 21, 1, 0) + _service()._validate_report_date(date(2026, 9, 23), ReportType.PLAN) with pytest.raises(ValidationError): - _service()._validate_report_date(date(2026, 9, 22)) + _service()._validate_report_date(date(2026, 9, 24), ReportType.PLAN) def test_local_helpers_use_the_configured_timezone(): diff --git a/tests/unit/test_csv_access_for_am.py b/tests/unit/test_csv_access_for_am.py index 5c90624..12460d7 100644 --- a/tests/unit/test_csv_access_for_am.py +++ b/tests/unit/test_csv_access_for_am.py @@ -30,7 +30,7 @@ async def test_am_can_list_only_approval_csv_tables_and_download_approval_pdf(cl resp = await client.get(f"{API}/csv-data/tables", headers=auth["am1"]) assert resp.status_code == 200, resp.text table_names = {item["name"] for item in resp.json()} - assert table_names == {"standard_tour_plan", "tour_plan", "fare_chart"} + assert table_names == {"standard_tour_plan", "tour_plan", "fare_chart", "expense_statement"} assert "daily_call_report_csv" not in table_names pdf = await client.get(f"{API}/csv-data/fare_chart/pdf", headers=auth["am1"]) diff --git a/tests/unit/test_csv_form_pdfs.py b/tests/unit/test_csv_form_pdfs.py index 08929a5..34c0c53 100644 --- a/tests/unit/test_csv_form_pdfs.py +++ b/tests/unit/test_csv_form_pdfs.py @@ -45,8 +45,8 @@ def test_identity_is_gathered_from_all_rows_not_just_the_first(): PRINTED_HEADERS = { "tour_plan": ["Sl.No.", "Date", "From", "To", "POB", "Stockiest Visit"], "standard_tour_plan": ["Sl.No.", "PLACE"], - "fare_chart": ["Sl.No.", "Date", "From", "To", "Kilometers (KM)"], - "area_covered": ["SALE STATUS", "AREA TO COVER", "SALE OBJECTIVE", "STATUS"], + "fare_chart": ["Sl.No.", "Date", "From", "To", "Kilometers (KM)\n(One Way)"], + "area_covered": ["AREA TO COVER", "SALE OBJECTIVE", "STATUS"], "core_dr_call_conversion": ["DOCTOR NAME", "DATE OF VISIT", "PRODUCT TARGETED", "STATUS"], "field_activity_report": [ "DATE", @@ -57,21 +57,19 @@ PRINTED_HEADERS = { "sales_and_stock_statement": [ "Sl.No.", "Name of the Product", + "Value in PTS", "Opening", "Receipt", "Sale", "Closing", - "Value in PTS", ], "sales_projection": [ "SL. No.", "NAME OF THE PRODUCT", "PTS", - "SEC. QTY", + "QUANTITY", "VALUE", - "CLOSING", - "SEC. VALUE", - "SALES", + "QUANTITY", "VALUE", ], } @@ -120,10 +118,10 @@ def test_blank_lines_carry_on_numbering_and_tour_plan_numbers_have_a_dot(): def test_product_lists_print_in_their_own_columns(): rows = _rows("sales_projection", 2) rows[0].update(product_name="INTEZOLE-PD", pts="106.92") - rows[1].update(product_name="INTEZOLE-RD", pts="93.59", secondary_qty="4") + rows[1].update(product_name="INTEZOLE-RD", pts="93.59", closing_qty="4", projection_value="250") cells = [r["cells"] for r in _ctx("sales_projection", rows)["grids"]["main"]["rows"][:2]] assert cells[0][:3] == ["1", "INTEZOLE-PD", "106.92"] - assert cells[1][:4] == ["2", "INTEZOLE-RD", "93.59", "4"] + assert cells[1] == ["2", "INTEZOLE-RD", "93.59", "4", "", "", "250"] def test_printing_every_month_together_renumbers_instead_of_repeating(): @@ -140,11 +138,11 @@ def test_printing_every_month_together_renumbers_instead_of_repeating(): assert [r["cells"][0] for r in body[:2]] == ["1", "1"] -def test_sales_projection_sales_column_names_the_actual_month(): - rows = _rows("sales_projection", 2, month="September") - assert _ctx("sales_projection", rows)["grids"]["main"]["headers"][7] == "SEP. SALES" - # No month on any row (e.g. a blank template): a generic label, never a hardcoded month. - assert _ctx("sales_projection", _rows("sales_projection", 2))["grids"]["main"]["headers"][7] == "SALES" +def test_sales_projection_groups_closing_and_projection_under_their_own_headings(): + from app.pdf.service import _env + + html = _env.get_template("forms/sales_projection.html").render(**_ctx("sales_projection", _rows("sales_projection", 1))) + assert '
CLOSINGPROJECTION