# Backend integration - October 2026

This backend has been connected to the school-configurable result engine used by the updated frontend SRC.

## Result configuration

- Production endpoint: `api/result_configuration.php`
- Shared calculation engine: `api/shared/ResultConfiguration.php`
- Migration: `migrations/20261001_result_configuration.sql`
- Admins can save result configurations; authenticated teachers can read them.
- Configurations are stored by `school_key` and profile (`secondary`, `primary`, `play`).
- The endpoint can create the configuration table on the first admin save. The SQL migration is still included for deployments where the DB user does not have `CREATE TABLE` permission.
- Formula evaluation is server-side and does **not** use `eval()`.

## Result writes

`api/teachers_results_and_comments_update.php` and `api/results.php` now use the same configuration engine. Teacher writes are restricted to fields enabled by the active school profile. Calculated fields (`st1`, `st2`, `cat`, `total`) and `grade` are recalculated on the server, so a browser cannot override configured formulas by submitting a different total.

The frontend also sends `school_key` with result writes so multiple school deployments do not accidentally share the wrong configuration when they use the same backend/database.

## Bursary/account balances

The supplied backend already contained the required `session-fee-defaults` resource in the fees service. The updated frontend uses that endpoint to show the configured term fee as the visible balance when a student has no normal fee record. Dashboard `Recorded due` and `Total outstanding` are then derived from those same visible account balances. No duplicate debt rows are created just to display the default balance.

## Existing server configuration

This ZIP intentionally does not invent or replace `config/Database.php`. The supplied backend already expects that file outside this package, and the new result endpoint uses the same existing database bootstrap/authentication pattern.

## Verification performed

- PHP syntax checked across all backend PHP files.
- Shared result engine tested with default secondary and primary formulas, custom formulas, disabled-input handling, and min/max validation.
