This is the complete **Technical Documentation** for the **Fon2Fix Repair Tracker**.

You can save this text as a file named `README.md` or `DOCUMENTATION.txt` and keep it in the server folder. This will explain everything to the next developer or IT person who manages the system.

-----

# Fon2Fix Repair Tracker - System Documentation

**Version:** 1.0
**Last Updated:** November 20, 2025
**Description:** A lightweight, web-based repair tracking system designed for multi-branch management (Gombak & Sri Rampai). It handles job entry, status tracking, sales reporting, and digital invoicing via WhatsApp.

-----

## 1\. Technology Stack

The system is built on a standard **LAMP Stack** (Linux, Apache, MySQL, PHP) and requires no special frameworks or installation.

  * **Frontend:** HTML5, CSS3, Vanilla JavaScript (Fetch API).
  * **Backend:** Native PHP (PDO for database connections).
  * **Database:** MySQL / MariaDB.
  * **Authentication:** PHP Sessions (Hardcoded credentials in `index.php`).

-----

## 2\. File Structure

Here is the map of the files in the `public_html` folder:

| File Name | Type | Description |
| :--- | :--- | :--- |
| **`index.php`** | Login | The entry point. Handles user authentication. Contains the list of authorized users and passwords. |
| **`dashboard.php`** | UI | The main application. Contains the Job Entry Form, the Job List Table, and all JavaScript logic (WhatsApp, Reports). |
| **`api.php`** | Backend | The "Brain." Receives data from the dashboard, connects to the Database, generates Job IDs, and saves/retrieves data. |
| **`invoice.php`** | View | A readonly page that generates a professional printable/PDF invoice based on the Job ID in the URL. |
| **`logout.php`** | Utility | Destroys the user session and redirects to login. |
| **`logo.png`** | Asset | The company logo used in the Invoice header. |

-----

## 3\. Database Schema

The system uses a single database table named **`jobs`**.

**Database Name:** `spartanm_db`
**User:** `spartanm_user`

### SQL Structure

If the database is ever deleted, run this SQL command to recreate the table with all necessary columns:

```sql
CREATE TABLE jobs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    job_id VARCHAR(20) NOT NULL,      -- Format: SG0001 or SR0001
    branch VARCHAR(50) NOT NULL,      -- 'Gombak' or 'Sri Rampai'
    date DATE NOT NULL,
    customer VARCHAR(100) NOT NULL,
    phone VARCHAR(50),                -- Customer Phone
    model VARCHAR(100),               -- Device Model
    imei VARCHAR(100),                -- IMEI or Serial
    passcode VARCHAR(100),            -- Device Passcode
    repair VARCHAR(255),              -- Repair Description
    price DECIMAL(10, 2),             -- Cost (0.00)
    status VARCHAR(50),               -- 'Pending' or 'Completed'
    remarks TEXT,                     -- Internal Notes
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

-----

## 4\. Key Features & Logic

### A. Job ID Generation (Auto-Increment per Branch)

  * **Logic:** Located in `api.php` -\> function `getNextJobId()`.
  * **Behavior:** It checks the last ID in the database for that specific branch prefix (`SG` or `SR`). It strips the letters, adds +1 to the number, and pads it with zeros.
  * *Example:* If the last job was `SG0049`, the next will be `SG0050`.

### B. WhatsApp E-Receipt

  * **Logic:** Located in `dashboard.php` -\> function `sendWhatsapp()`.
  * **Behavior:** It generates a dynamic link to the invoice file: `yourdomain.com/invoice.php?id=SG0001`.
  * It creates a WhatsApp link (`wa.me`) that includes a pre-written text message containing that link.

### C. Sales Reporting

  * **Logic:** Located in `api.php` (Action: `get_report`).
  * **Behavior:** It runs a SQL SUM query `SUM(price)` but **ONLY** for rows where `status = 'Completed'`. Pending jobs do not count towards sales totals.

-----

## 5\. Configuration & Maintenance

### How to Change Passwords

User accounts are hardcoded in **`index.php`** for simplicity.
To change a password, edit `index.php` and modify the array:

```php
$users = [
    'gombak'     => ['pass' => 'NEW_PASSWORD_HERE', 'branch' => 'Gombak'],
    'sri_rampai' => ['pass' => 'NEW_PASSWORD_HERE', 'branch' => 'Sri Rampai']
];
```

### How to Update Database Credentials

If the cPanel database password changes, you must update **TWO** files:

1.  **`api.php`** (Lines 6-8)
2.  **`invoice.php`** (Lines 4-6)

<!-- end list -->

```php
$host = 'localhost';
$db   = 'spartanm_db';
$user = 'spartanm_user';
$pass = 'YOUR_NEW_PASSWORD';
```

-----

## 6\. Troubleshooting

**Problem:** "Unknown column 'phone' in field list"
**Solution:** The database table is missing columns. Run the SQL command in Section 3 via phpMyAdmin to recreate the table.

**Problem:** "WhatsApp link sends a blank text"
**Solution:** Ensure the customer has a valid phone number saved. The script attempts to auto-format `012...` to `6012...`.

**Problem:** "Invoice page is blank"
**Solution:** Ensure the URL includes the ID, e.g., `invoice.php?id=SG0001`. If the ID does not exist in the database, the page will die with "Invoice not found."