"""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", "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"], "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", "Opening", "Receipt", "Sale", "Closing", "Value in PTS", ], "sales_projection": [ "SL. No.", "NAME OF THE PRODUCT", "PTS", "SEC. QTY", "VALUE", "CLOSING", "SEC. VALUE", "SALES", "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", secondary_qty="4") 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"] 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_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_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: "