Client Paid Debt Report Controller Documentation
File: /controllers/clientPayedDeptReportController.php
Purpose: Generate client debt payment reports with outstanding balance analysis
Last Updated: December 20, 2024
Total Functions: 3 (main workflow + data processing)
Lines of Code: ~155
---
๐ Overview
The Client Paid Debt Report Controller is a specialized financial reporting module that provides detailed analysis of client debt payments and outstanding balances. It handles:
- โข Client bill payment tracking with filters
- โข Outstanding debt calculation and analysis
- โข Date range and client-specific filtering
- โข Bill number search functionality
- โข Payment vs outstanding balance reconciliation
- โข Financial status reporting for clients
Primary Functions
- โ Generate client debt payment reports
- โ Calculate outstanding balances per bill
- โ Filter by client, date range, and bill number
- โ Track total payments vs bill values
- โ Display bills with remaining debt only
- โ Integrate with YouTube tutorial links
Related Controllers
- โข clientController.php - Customer management
- โข billController.php - Bill management
- โข clientdebtchangeController.php - Debt tracking
- โข clientReportsController.php - Other client reports
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **bills** | Service bills master data | id, billno, clientid, finalnetbillvalue, waitvalue, cashvalue, cardvalue, companycarry, billdate | |
| **clientdebtchange** | Client debt transaction log | clientid, clientdebtchangeamount, billid (referenced via billno) |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **client** | Customer master data | clientid, clientname | |
| **youtubelink** | Tutorial video links | youtubelinkid, title, url |
๐ Key Functions
1. show() / Default Action - Client Debt Payment Report
Location: Lines 78-151
Purpose: Generate comprehensive debt payment report with outstanding balance analysis
Function Signature:
// Triggered when: empty($do) OR $do == "show"
$start_date = $_REQUEST['from'];
$end_date = $_REQUEST['to'];
$client = $_POST['client'];
$bill_no = $_POST['bill_no'];
Process Flow:
1. Build dynamic WHERE clause based on filters
2. Query bills with conditional filters
3. Calculate outstanding balances per bill
4. Filter out fully paid bills
5. Display results with client information
Filter Logic:
$where = '';
if ((isset($client) && $client != "-1")) {
$where = ' clientid = ' . $client . ' ';
}
if ((isset($bill_no) && $bill_no != "")) {
if (empty($where)) {
$where .= ' id = ' . $bill_no . ' ';
} else {
$where .= ' and id = ' . $bill_no . ' ';
}
}
if (isset($start_date) && $start_date != "" && isset($end_date) && $end_date != "") {
if (empty($where)) {
$where .= ' date(billdate) >= "' . $start_date . '" and date(billdate) <= "' . $end_date . '" ';
} else {
$where .= ' and date(billdate) >= "' . $start_date . '" and date(billdate) <= "' . $end_date . '" ';
}
}
---
2. Outstanding Balance Calculation - Debt Analysis Logic
Location: Lines 122-141
Purpose: Calculate remaining debt per bill after payments
Calculation Process:
foreach ($clientBills as $cli_bill_val) {
$bill_id = $cli_bill_val->billno;
// Get total payments for this bill
$client_dept_change = $clientDeptChangeExt->sumByBillId($bill_id);
// Calculate outstanding balance
$waitvalue = $cli_bill_val->waitvalue - $client_dept_change->clientdebtchangeamount;
// Only include bills with outstanding debt
if ($waitvalue > 0) {
$client_bills[] = array(
'id' => $cli_bill_val->id,
'billno' => $bill_id,
'finalnetbillvalue' => $cli_bill_val->finalnetbillvalue,
'waitvalue' => $waitvalue, // Outstanding amount
'total_payed' => $cli_bill_val->cashvalue + $cli_bill_val->cardvalue +
$cli_bill_val->companycarry + $client_dept_change->clientdebtchangeamount
);
}
}
Key Calculations:
- โข Outstanding Balance:
waitvalue - total_debt_payments - โข Total Paid:
cash + card + company_carry + debt_payments - โข Filter Condition: Only bills with
waitvalue > 0are included
---
3. Client Information Display - Header Information
Location: Lines 113-121
Purpose: Load and display client information for the selected customer
Process Flow:
if ((isset($client) && $client != "-1")) {
$messageData = $clientDAO->load($client);
$smarty->assign("client_id", $messageData->clientid);
$message = " ุงุณู
ุงูุนู
ูู : " . $messageData->clientname .
" ุงูุชุงุฑูุฎ: ู
ู : " . $start_date . " ุฅูู: " . $end_date;
$smarty->assign("message", $message);
}
Display Information:
- โข Client name in Arabic
- โข Date range filter applied
- โข Client ID for reference
---
๐ Workflows
Workflow 1: Client Debt Payment Report Generation
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description |
|---|---|---|
| `do=` (empty) or `do=show` | Default action | Generate client debt payment report |
Report Generation (do=show):
- โข
client- Client ID (-1 for all clients) - โข
from- Start date (YYYY-MM-DD) (optional) - โข
to- End date (YYYY-MM-DD) (optional) - โข
bill_no- Specific bill number (optional)
Parameter Combinations
1. All Clients, All Dates: No parameters needed
2. Specific Client: client=[ID]
3. Date Range: from=[DATE]&to=[DATE]
4. Specific Bill: bill_no=[NUMBER]
5. Combined Filters: Any combination of the above
---
๐งฎ Calculation Methods
Outstanding Balance Formula
$outstanding = $bill->waitvalue - $total_debt_payments;
Where:
- โข
waitvalue= Original amount due on bill - โข
total_debt_payments= Sum of all payments made via clientdebtchange
Total Paid Calculation
$total_paid = $bill->cashvalue + $bill->cardvalue + $bill->companycarry + $debt_payments;
Where:
- โข
cashvalue= Cash payments - โข
cardvalue= Card payments - โข
companycarry= Company credit/carry amount - โข
debt_payments= Additional debt payments
Bill Status Logic
if ($waitvalue > 0) {
// Bill has outstanding balance - include in report
$client_bills[] = $bill_data;
}
// Bills with $waitvalue <= 0 are fully paid and excluded
---
๐ Security & Permissions
Authentication
include_once("../public/authentication.php");
Input Validation
- โข Client ID is checked for valid selection (
!= "-1") - โข Date strings are used in SQL with proper formatting
- โข Bill number is treated as integer ID
SQL Injection Protection
- โข Uses DAO layer for database operations
- โข Parameters are validated before inclusion in WHERE clauses
---
๐ Performance Considerations
Database Optimization Tips
1. Indexes Required:
- bills(clientid, billdate)
- bills(billno)
- clientdebtchange(billid) or equivalent correlation
2. Query Optimization:
- Date filtering uses date() function - consider indexed date columns
- N+1 query issue: One query per bill for debt change sum
- Consider JOINing bills with aggregated debt changes
3. Potential Performance Issues:
-- Current approach (N+1 queries)
SELECT * FROM bills WHERE [conditions];
-- Then for each bill:
SELECT SUM(amount) FROM clientdebtchange WHERE billno = ?;
-- Optimized approach (single query)
SELECT b.*, COALESCE(SUM(cdc.clientdebtchangeamount), 0) as total_payments
FROM bills b
LEFT JOIN clientdebtchange cdc ON b.billno = cdc.billno
WHERE [conditions]
GROUP BY b.id;
---
๐ Common Issues & Troubleshooting
1. Missing Payment Data
Issue: Bills show incorrect outstanding balances
Cause: clientdebtchange records not properly linked to bills
Debug:
-- Check bill-to-payment correlation
SELECT b.billno, b.waitvalue, SUM(cdc.clientdebtchangeamount) as payments
FROM bills b
LEFT JOIN clientdebtchange cdc ON b.billno = cdc.billno
WHERE b.clientid = [CLIENT_ID]
GROUP BY b.billno;
2. Date Filter Not Working
Issue: Date range filters return no results
Cause: Date format mismatch or timezone issues
Fix:
// Ensure proper date format
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = date('Y-m-d', strtotime($end_date));
3. Performance Issues
Issue: Report takes too long to generate
Cause: N+1 queries for debt change sums
Solution: Implement batch query or JOIN approach
---
๐งช Testing Scenarios
Test Case 1: Basic Report Generation
1. Select specific client
2. Set date range covering known bills
3. Verify outstanding balances are calculated correctly
4. Check that fully paid bills are excluded
Test Case 2: Filter Combinations
1. Test client-only filter
2. Test date-only filter
3. Test bill number filter
4. Test combined filters
5. Verify proper WHERE clause construction
Test Case 3: Calculation Accuracy
1. Create test bill with known amounts
2. Add various payment types (cash, card, debt payments)
3. Verify total paid calculation
4. Verify outstanding balance calculation
Test Case 4: Edge Cases
1. Test with no matching bills
2. Test with bills having zero outstanding balance
3. Test with invalid client ID
4. Test with invalid date ranges
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข clientReportsController.md - Related client reporting
- โข Bill Management - Bill creation and management
- โข Client Debt Management - Debt tracking system
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When debt calculation logic changes