๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Maintenances Controller Documentation

File: /controllers/maintenances.php

Purpose: Manages maintenance requests, tracking products sent for repair/warranty service

Last Updated: December 20, 2024

Total Functions: 13

Lines of Code: ~491

---

๐Ÿ“‹ Overview

The Maintenances Controller manages the complete lifecycle of maintenance requests for products under warranty or repair. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**maintenances**Main maintenance recordsid, clientid, supplierid, branchid, productid, maintenancecenterid, maintenancecaseid, receiptnumber, defect, accessories, estimatedcost, prepaid, conditions, userid
**maintenancecomments**Comments on maintenance casesid, maintenanceid, comments, userid, maintenancecommentdate, conditions
**maintenancecases**Maintenance case typesid, casename, maintenancecasedate, userid, conditions
**maintenancecenters**Service centersid, centername, centerphone, centermobile, centeraddress, administrator, administratorphone, administratordebt, conditions
### Reference Tables

Table NamePurposeKey Columns
**client**Customer informationclientid, clientname, clientphone
**supplier**Supplier/manufacturer datasupplierid, suppliername
**branch**Branch locationsbranchId, branchName, branchAddress, phone, mobile
**product**Product catalogproductId, productName
**user**System usersuserid, employeename
---

๐Ÿ”‘ Key Functions

1. Default Action - Add New Maintenance

Location: Lines 21-26

Purpose: Display form for creating new maintenance requests

Process Flow:

1. Assign current date and time to template

2. Set maintenance module flag for navigation

3. Display add form template

---

2. show() - Display All Maintenance Records

Location: Lines 26-36

Purpose: Show maintenance listing with filtering and search capabilities

Features:

---

3. edit() - Edit Maintenance Record

Location: Lines 37-58

Purpose: Edit existing maintenance record with comments

Function Signature:

$id = filter_input(INPUT_GET, 'id');
$del = filter_input(INPUT_GET, 'del');

Process Flow:

1. Load maintenance data by ID

2. Get employee name from user table

3. Parse datetime for separate date/time fields

4. Load associated comments with employee names

5. Assign data to edit template

---

4. savedata() - Save/Update Maintenance Record

Location: Lines 213-298

Purpose: Main function for saving maintenance requests with comments

Function Signature:

function savedata()

Process Flow:

1. Validate and sanitize all input fields

2. Create new maintenance record or update existing

3. Set default maintenance case ID and policy ID

4. Loop through comments and save each one

5. Return JSON response with record ID

Key Fields Handled:

Comment Handling:

for ($i = 1; $i <= $commentitr; $i++) {
    $maintenancecommentid = filter_input(INPUT_POST, 'maintenancecommentid_' . $i);
    $comments = filter_input(INPUT_POST, 'comments_' . $i);
    // Save each comment with maintenance ID
}

---

5. savecomment() - Individual Comment Management

Location: Lines 86-112

Purpose: Save or update individual comments via AJAX

Function Signature:

function savecomment()

Process Flow:

1. Get comment data and maintenance ID

2. Create new comment or update existing

3. Return JSON with comment ID and selection data

---

6. savecase() - Maintenance Case Management

Location: Lines 114-137

Purpose: Create or update maintenance case types

Function Signature:

function savecase()

Process Flow:

1. Validate case name input

2. Create new case or update existing

3. Return JSON response for dynamic dropdown updates

---

7. savebranch() - Branch Management

Location: Lines 140-167

Purpose: Manage branch information for maintenance services

Function Signature:

function savebranch()

Process Flow:

1. Collect branch details (name, phone, mobile, address)

2. Insert new branch or update existing via raw SQL

3. Return JSON with branch data for dropdown

Database Operation:

R::exec("INSERT INTO `branch`(`branchName`, `branchDate`, `phone`, `mobile`, `branchNo`, `branchAddress`) 
         VALUES ('$branchName','$date','$phone','$mobile','$branchNo','$branchAddress')");

---

8. savecenter() - Maintenance Center Management

Location: Lines 170-210

Purpose: Manage maintenance service centers

Function Signature:

function savecenter()

Process Flow:

1. Collect center information and administrator details

2. Create or update maintenance center record

3. Optionally link to specific maintenance request

4. Return JSON response

Center Data Fields:

---

9. showajax() - DataTables AJAX Handler

Location: Lines 301-436

Purpose: Provide server-side processing for maintenance listing table

Function Signature:

function showajax()

Filter Parameters:

Process Flow:

1. Build dynamic WHERE clause based on filters

2. Add search functionality across multiple columns

3. Apply sorting and pagination

4. Join with related tables for display data

5. Generate action buttons based on record status

SQL Query Structure:

SELECT maintenances.*, employeename, clientname, suppliername, branchName, centername 
FROM maintenances 
LEFT JOIN user ON maintenances.userid = user.userid 
LEFT JOIN client ON maintenances.clientid = client.clientid 
LEFT JOIN supplier ON maintenances.supplierid = supplier.supplierid 
LEFT JOIN branch ON maintenances.branchid = branch.branchId  
LEFT JOIN maintenancecenters ON maintenances.maintenancecenterid = maintenancecenters.id

Special Features:

---

10. changecase() - Update Maintenance Case

Location: Lines 440-451

Purpose: Change maintenance case status via AJAX

Function Signature:

function changecase()

---

11. changecenter() - Update Maintenance Center

Location: Lines 454-465

Purpose: Assign maintenance center to request

Function Signature:

function changecenter()

---

12. updatecomment() - Update Comment Content

Location: Lines 467-486

Purpose: Update comment text via inline editing

Function Signature:

function updatecomment()

---

๐Ÿ”„ Workflows

Workflow 1: New Maintenance Request Creation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Create New Maintenance
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Fill Basic Information
- Select client, supplier, branch
- Choose products needing maintenance
- Enter receipt number and date
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Document Problem Details
- Describe defect and symptoms
- List accessories included
- Add technical notes
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Cost and Warranty Information
- Enter estimated repair cost
- Record any prepayment made
- Set warranty vs. repair type
- Add purchase date and invoice info
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Add Comments (Optional)
- Multiple comments supported
- Each comment tracked with user and timestamp
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Save and Track
- Generate maintenance ID
- Set initial case status
- Ready for center assignment
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Maintenance Case Management

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Maintenance Tracking
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Initial Status (Case 1)
- New maintenance request created
- Awaiting center assignment
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Center Assignment
IF warranty (maintorguar = 1):
โ”‚
โ†’ Assign to maintenance center
โ”‚ โ”œโ”€ Select existing center
โ”‚ โ”‚ โ””โ”€ Create new center if needed โ”‚
โ”‚
ELSE (warranty = 0):
โ”‚ โ””โ”€โ†’ Display "ุถู…ุงู†" (under warranty) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Update Case Status
- Case 2: Sent to maintenance
- Case 3: Work in progress
- Case 4: Ready for pickup
- Case 5: Completed
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Communication & Updates
- Add progress comments
- Update cost estimates
- Track completion status
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionDisplay add maintenance form
`do=show`show()Display maintenance listing
`do=edit`edit()Edit maintenance record
`do=savedata`savedata()Save/update maintenance
`do=showajax`showajax()DataTables AJAX data
`do=savecomment`savecomment()Save individual comment
`do=savecase`savecase()Save maintenance case type
`do=savebranch`savebranch()Save branch information
`do=savecenter`savecenter()Save maintenance center
`do=changecase`changecase()Update case status
`do=changecenter`changecenter()Update center assignment
`do=updatecomment`updatecomment()Update comment text
`do=appentcomment`append templateAppend comment form
---

๐Ÿงฎ Calculation Methods

Cost Tracking

// Estimated cost calculation
$estimatedcost = filter_input(INPUT_POST, 'estimatedcost');
$prepaid = filter_input(INPUT_POST, 'prepaid');

// Remaining cost calculation (done on frontend)
$remaining = $estimatedcost - $prepaid;

Maintenance Center Debt

// Center administrator debt tracking
$administratordebt = filter_input(INPUT_POST, 'administratordebt');
// Stored for accounting purposes

---

๐Ÿ”’ Security & Permissions

Input Sanitization

// All inputs properly filtered
$productId = filter_input(INPUT_POST, 'product');
$supplierid = filter_input(INPUT_POST, 'supplier');
$receiptnumber = filter_input(INPUT_POST, 'receiptnumber');

Authentication

Data Validation

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Indexes Needed:

- maintenances(clientid, maintenancedate)

- maintenances(supplierid, conditions)

- maintenancecomments(maintenanceid, conditions)

2. Query Optimization:

- JOINs limited to necessary tables

- Date filtering with proper indexing

- Conditional loading of comments

AJAX Performance

---

๐Ÿ› Common Issues & Troubleshooting

1. Maintenance Center Assignment Issues

Issue: Cannot assign center to warranty items

Cause: Logic prevents center assignment when maintorguar = 0

Solution: Check warranty flag before attempting assignment

2. Comment Update Problems

Issue: Comments not saving properly

Cause: Missing CSRF validation or session expiry

Debug:

SELECT * FROM maintenancecomments WHERE maintenanceid = [ID] ORDER BY id DESC;

3. Product Display Issues

Issue: Product names not showing correctly

Cause: Multiple product IDs stored as comma-separated string

Fix: Ensure proper parsing of product ID string:

$products = R::getAll("SELECT productName FROM product WHERE productId in (".$row['productid'].")");

4. Date/Time Handling

Issue: DateTime splitting errors

Cause: Inconsistent datetime format

Fix: Validate datetime before splitting:

$new_time = explode(" ", $maintenancedata['dateatime']);
if (count($new_time) >= 2) {
    $smarty->assign('get_date', $new_time[0]);
    $smarty->assign('get_time', $new_time[1]);
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Maintenance Creation

1. Create new maintenance request with all required fields
2. Verify record saved with proper user ID and timestamp
3. Check maintenance case assignment
4. Confirm comment association

Test Case 2: Warranty vs. Repair Workflow

1. Create warranty maintenance (maintorguar = 0)
2. Verify center assignment is disabled
3. Create repair maintenance (maintorguar = 1)
4. Verify center assignment interface appears

Test Case 3: Comment System

1. Add multiple comments to maintenance record
2. Edit existing comment via inline editing
3. Verify user tracking and timestamps
4. Test comment deletion (soft delete)

Test Case 4: AJAX Filtering

1. Test date range filtering
2. Filter by supplier, client, branch
3. Test search functionality
4. Verify pagination works correctly

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur

โ†‘