0d30c58bf0
Snapshot of the working tree on srv1071960 (branch main), taken during server cleanup. .env files, node_modules, builds, logs and media were excluded.
241 lines
9.6 KiB
Python
241 lines
9.6 KiB
Python
"""Company-form PDF layouts: what each form prints, independent of the PDF engine."""
|
|
|
|
import pytest
|
|
|
|
from app.csv_data.config import CSV_LABELS, TABLES
|
|
from app.pdf.forms import build_form_context
|
|
from app.pdf.service import _html_to_text, _minimal_pdf, csv_pdf_bytes, csv_table_pdf_bytes
|
|
|
|
|
|
def _rows(table: str, count: int = 2, **values) -> list[dict[str, object]]:
|
|
return [
|
|
{**{c: "" for c in TABLES[table]}, "serial_no": str(i + 1), **values} for i in range(count)
|
|
]
|
|
|
|
|
|
def _ctx(table: str, rows, **kw):
|
|
return build_form_context(table, CSV_LABELS[table], TABLES[table], rows, **kw)
|
|
|
|
|
|
def test_every_form_has_its_own_template():
|
|
templates = {_ctx(name, [])["template"] for name in TABLES}
|
|
assert len(templates) == len(TABLES)
|
|
|
|
|
|
def test_every_stored_row_is_printed_and_blank_lines_pad_to_the_form_length():
|
|
ctx = _ctx("tour_plan", _rows("tour_plan", 3, from_location="BBSR", pob="10"))
|
|
body = ctx["grids"]["main"]["rows"]
|
|
assert len(body) == 25
|
|
assert [r["blank"] for r in body[:4]] == [False, False, False, True]
|
|
assert body[0]["cells"][2] == "BBSR"
|
|
|
|
|
|
def test_rows_beyond_the_printed_form_length_are_not_dropped():
|
|
ctx = _ctx("standard_tour_plan", _rows("standard_tour_plan", 40, place="Puri"))
|
|
assert len(ctx["grids"]["main"]["rows"]) == 40
|
|
|
|
|
|
def test_identity_is_gathered_from_all_rows_not_just_the_first():
|
|
rows = _rows("fare_chart", 2)
|
|
rows[0]["name"], rows[1]["name"] = "", "Ravi Das"
|
|
assert _ctx("fare_chart", rows)["meta_rows"][0][0] == ("NAME:", "Ravi Das")
|
|
|
|
|
|
# Headings exactly as printed on the company forms, in printed order.
|
|
PRINTED_HEADERS = {
|
|
"tour_plan": ["Sl.No.", "Date", "From", "To", "POB", "Stockist Visit"],
|
|
"standard_tour_plan": ["Sl.No.", "PLACE"],
|
|
"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",
|
|
"STATION WORKED",
|
|
"NO. OF DOCTORS CALLED",
|
|
"NO. OF CHEMISTS/PDS CALLED",
|
|
],
|
|
"sales_and_stock_statement": [
|
|
"Sl.No.",
|
|
"Name of the Product",
|
|
"Value in PTS",
|
|
"Opening",
|
|
"Receipt",
|
|
"Sale",
|
|
"Closing",
|
|
],
|
|
"sales_projection": [
|
|
"SL. No.",
|
|
"NAME OF THE PRODUCT",
|
|
"PTS",
|
|
"QUANTITY",
|
|
"VALUE",
|
|
"QUANTITY",
|
|
"VALUE",
|
|
],
|
|
}
|
|
PRINTED_LINES = {
|
|
"tour_plan": 25,
|
|
"standard_tour_plan": 25,
|
|
"fare_chart": 25,
|
|
"area_covered": 28,
|
|
"core_dr_call_conversion": 28,
|
|
"field_activity_report": 25,
|
|
"sales_and_stock_statement": 26,
|
|
"sales_projection": 26,
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("table", list(PRINTED_HEADERS))
|
|
def test_form_headings_and_ruled_lines_match_the_printed_form(table):
|
|
grid = _ctx(table, [])["grids"]["main"]
|
|
assert grid["headers"] == PRINTED_HEADERS[table]
|
|
assert len(grid["rows"]) == PRINTED_LINES[table]
|
|
assert all(r["blank"] for r in grid["rows"])
|
|
|
|
|
|
def test_daily_call_report_has_both_printed_tables():
|
|
grids = _ctx("daily_call_report_csv", [])["grids"]
|
|
assert grids["doctors"]["headers"] == [
|
|
"SR. NO.",
|
|
"DR. NAME",
|
|
"QUALIFICATION",
|
|
"PRODUCT PROMOTE",
|
|
"PRODUCT SAMPLE",
|
|
"REMARKS",
|
|
]
|
|
assert len(grids["doctors"]["rows"]) == 10
|
|
assert grids["chemists"]["headers"] == ["SR. NO.", "NAME OF THE CHEMIST", "POB"]
|
|
assert len(grids["chemists"]["rows"]) == 5
|
|
|
|
|
|
def test_blank_lines_carry_on_numbering_and_tour_plan_numbers_have_a_dot():
|
|
body = _ctx("tour_plan", _rows("tour_plan", 2))["grids"]["main"]["rows"]
|
|
assert [r["cells"][0] for r in body[:4]] == ["1.", "2.", "3.", "4."]
|
|
body = _ctx("sales_projection", _rows("sales_projection", 1))["grids"]["main"]["rows"]
|
|
assert [r["cells"][0] for r in body[:3]] == ["1", "2", "3"]
|
|
|
|
|
|
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", 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] == ["2", "INTEZOLE-RD", "93.59", "4", "", "", "250"]
|
|
|
|
|
|
def test_printing_every_month_together_renumbers_instead_of_repeating():
|
|
# Each stored the server's own month-reset numbering (1, 1) — printed side by side that
|
|
# would repeat, so a combined PDF is asked to renumber instead of trusting it.
|
|
rows = _rows("sales_projection", 0) + [
|
|
{**{c: "" for c in TABLES["sales_projection"]}, "serial_no": "1", "month": "August", "product_name": "VIVACELLIX-360"},
|
|
{**{c: "" for c in TABLES["sales_projection"]}, "serial_no": "1", "month": "September", "product_name": "INFEZOX-O"},
|
|
]
|
|
body = _ctx("sales_projection", rows, renumber=True)["grids"]["main"]["rows"]
|
|
assert [r["cells"][0] for r in body[:2]] == ["1", "2"]
|
|
# A single month's own PDF (no renumber) keeps the server's real stored numbers.
|
|
body = _ctx("sales_projection", rows, renumber=False)["grids"]["main"]["rows"]
|
|
assert [r["cells"][0] for r in body[:2]] == ["1", "1"]
|
|
|
|
|
|
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 '<th colspan="2">CLOSING</th>' in html and '<th colspan="2">PROJECTION</th>' in html
|
|
|
|
|
|
def test_no_form_prints_dashboard_extras():
|
|
for table in TABLES:
|
|
ctx = _ctx(table, _rows(table, 2, pob="5", kilometers_km="9"))
|
|
for grid in ctx["grids"].values():
|
|
assert "totals" not in grid
|
|
assert "stats_items" not in ctx
|
|
|
|
|
|
def test_import_script_columns_match_the_tables():
|
|
"""A drifted column list rolled back the whole import, leaving the product lists empty."""
|
|
from scripts.import_csv_data import CSV_TABLES
|
|
|
|
for table, columns in CSV_TABLES.values():
|
|
assert tuple(columns) == TABLES[table], table
|
|
|
|
|
|
def test_iso_dates_print_day_first_and_free_text_is_untouched():
|
|
rows = _rows("fare_chart", 2)
|
|
rows[0]["report_date"], rows[1]["report_date"] = "2026-09-18", "week 3"
|
|
cells = [r["cells"][1] for r in _ctx("fare_chart", rows)["grids"]["main"]["rows"][:2]]
|
|
assert cells == ["18/09/2026", "week 3"]
|
|
|
|
|
|
def test_stored_serial_numbers_are_kept_for_single_row_pdfs():
|
|
rows = _rows("tour_plan", 1)
|
|
rows[0]["serial_no"] = "7"
|
|
assert _ctx("tour_plan", rows)["grids"]["main"]["rows"][0]["cells"][0] == "7."
|
|
|
|
|
|
def test_weekly_report_prints_one_report_per_stored_row():
|
|
rows = _rows("weekly_report", 3, employee_name="Ravi", pob_booked="5")
|
|
assert len(_ctx("weekly_report", rows)["reports"]) == 3
|
|
|
|
|
|
def test_daily_call_report_splits_doctor_and_chemist_lines():
|
|
rows = _rows("daily_call_report_csv", 3)
|
|
rows[0]["doctor_name"] = "Dr. A"
|
|
rows[1]["chemist_name"], rows[1]["pob"] = "Sahu Medical", "100"
|
|
rows[2]["stockist_visit"], rows[2]["order_value"] = "Yes", "5000"
|
|
ctx = _ctx("daily_call_report_csv", rows)
|
|
assert ctx["grids"]["doctors"]["rows"][0]["cells"][:2] == ["1", "Dr. A"]
|
|
assert ctx["grids"]["chemists"]["rows"][0]["cells"][:2] == ["1", "Sahu Medical"]
|
|
assert (ctx["stockist_visit"], ctx["order_value"]) == ("Yes", "5000")
|
|
|
|
|
|
def test_only_approval_forms_carry_an_approval_block():
|
|
assert _ctx("fare_chart", [], approval_status="approved", am_name="Meera")["approval"][
|
|
"line"
|
|
] == ("APPROVAL SIGNATURE: Meera")
|
|
assert _ctx("fare_chart", [])["approval"]["value"] == "PENDING"
|
|
assert _ctx("sales_and_stock_statement", [])["approval"] is None
|
|
|
|
|
|
@pytest.mark.parametrize("table", list(TABLES))
|
|
def test_every_form_renders_a_pdf_even_when_empty_or_hostile(table):
|
|
rows = _rows(table, 2, **{c: "<script>&\"'" for c in TABLES[table] if c != "serial_no"})
|
|
for data in (rows, []):
|
|
pdf = csv_table_pdf_bytes(table, CSV_LABELS[table], TABLES[table], data)
|
|
assert pdf.startswith(b"%PDF")
|
|
|
|
|
|
def test_unknown_table_row_pdf_still_renders():
|
|
assert csv_pdf_bytes("Some Row", {"id": "x", "a": "1"}).startswith(b"%PDF")
|
|
|
|
|
|
def test_plain_text_fallback_keeps_the_form_content_readable():
|
|
from app.pdf.service import _env
|
|
|
|
ctx = _ctx(
|
|
"fare_chart",
|
|
_rows("fare_chart", 1, from_location="Puri", kilometers_km="60"),
|
|
approval_status="approved",
|
|
am_name="Meera",
|
|
)
|
|
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"
|