ClientPayedDeptBills Documentation
Client Paid Debt Bills Controller Documentation
File: /controllers/clientPayedDeptBillsController.php
Purpose: Manages debt payment tracking for service bills (bills table) with detailed payment history and analysis
Last Updated: December 20, 2024
Total Functions: 3 main actions
Lines of Code: ~294
---
๐ Overview
The Client Paid Debt Bills Controller handles debt payment tracking specifically for service bills stored in the bills table. It provides comprehensive reporting on bill payment status, debt tracking, and payment history with detailed drill-down capabilities.
Primary Functions
- โ Service bill debt reporting with payment tracking
- โ Payment history analysis for individual bills
- โ Outstanding debt calculation and monitoring
- โ Client-specific bill payment summaries
- โ Date range filtering for payment analysis
- โ Detailed payment breakdown with dates and amounts
- โ Bill status tracking (paid vs unpaid portions)
Related Controllers
- โข billsController.php - Service bill creation and management
- โข clientController.php - Customer management
- โข clientPayedDeptController.php - General debt payments
- โข clientReportsController.php - Comprehensive client reports
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **bills** | Service bills master | id, billno, clientid, finalnetbillvalue, waitvalue, cashvalue, cardvalue, companycarry, billdate | |
| **clientdebtchange** | Payment tracking log | clientdebtchangeid, clientid, clientdebtchangeamount, billid, clientdebtchangedate, paytype |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **client** | Customer master data | clientid, clientname | |
| **youtubelink** | Tutorial/help links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action (empty $do) - Outstanding Bills Report
Location: Lines 78-159
Purpose: Display service bills with outstanding debt balances
Function Signature:
// Parameters: client, from, to, bill_no
$client = $_POST['client'];
$bill_no = $_POST['bill_no'];
$start_date = $_REQUEST['from'];
$end_date = $_REQUEST['to'];
Process Flow:
1. Build WHERE clause based on filters
2. Query bills table for matching records
3. Calculate payment amounts from clientdebtchange
4. Filter bills with remaining debt (waitvalue > 0)
5. Display filtered results with payment totals
Key Features:
- โข Client-specific filtering
- โข Date range filtering on
billdate - โข Bill number exact match search
- โข Outstanding debt calculation:
waitvalue - total_payments - โข Payment total calculation:
cashvalue + cardvalue + companycarry + payments
---
2. show - Paid Bills Summary
Location: Lines 160-226
Purpose: Show summary of bills that have received payments
Function Signature:
// Parameters: client, from, to, bill_no
$client = $_POST['client'];
$bill_id = $_POST['bill_no'];
$start_date = $_REQUEST['from'];
$end_date = $_REQUEST['to'];
Process Flow:
1. Build WHERE clause for clientdebtchange table
2. Query bills that have payment records
3. Calculate totals for each bill
4. Generate summary with totals across all bills
Calculations:
$waitvalue = $Bill_detail->waitvalue - $dept_Bill_detail->clientdebtchangeamount;
$total_payed = $Bill_detail->cashvalue + $Bill_detail->cardvalue +
$Bill_detail->companycarry + $dept_Bill_detail->clientdebtchangeamount;
Template Variables:
- โข
$total_bills- Sum of all bill final values - โข
$total_wait_bills- Sum of all remaining debt - โข
$total_payed_bills- Sum of all payments made
---
3. show_details - Individual Bill Payment History
Location: Lines 227-291
Purpose: Detailed payment breakdown for a specific bill
Function Signature:
$bill_no = $_GET['id']; // Bill number to analyze
Process Flow:
1. Load bill master data by bill number
2. Extract bill totals and client information
3. Query all payment records for this bill
4. Calculate running balance after each payment
5. Display chronological payment history
Running Balance Calculation:
if ($x == 1) {
$wait_before = $waitvalue;
$wait_aftre = $wait_before - ($cardvalue + $cashvalue + $companycarry + $clientdebtchangeamount);
} else {
$wait_before = $wait_aftre;
$wait_aftre = $wait_before - $clientdebtchangeamount;
}
Output Data:
- โข Sequential payment history with running totals
- โข Payment types and dates
- โข Remaining balance after each transaction
- โข Complete audit trail for bill settlement
---
๐ Workflows
Workflow 1: Outstanding Bills Analysis
---
Workflow 2: Payment History Drill-Down
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default action | Outstanding bills report | |
| `do=show` | show() | Paid bills summary | |
| `do=show_details` | show_details() | Individual bill payment history |
Outstanding Bills (default):
- โข
client- Customer ID (optional, -1 for all) - โข
from- Start date (YYYY-MM-DD, optional) - โข
to- End date (YYYY-MM-DD, optional) - โข
bill_no- Bill number for exact match (optional)
Paid Bills Summary (do=show):
- โข
client- Customer ID (optional) - โข
bill_no- Bill number (optional) - โข
from- Start date for payment filter - โข
to- End date for payment filter
Payment Details (do=show_details):
- โข
id- Bill number (required via GET)
---
๐งฎ Calculation Methods
Outstanding Debt Calculation
// For each bill, calculate remaining debt
$waitvalue = $cli_bill_val->waitvalue - $client_dept_change->clientdebtchangeamount;
// Only include bills with remaining debt
if ($waitvalue > 0) {
// Include in outstanding bills report
}
Total Payment Calculation
$total_payed = $cashvalue + $cardvalue + $companycarry + $client_dept_change_amount;
// cashvalue: Cash payments on bill
// cardvalue: Card payments on bill
// companycarry: Company credit applied
// client_dept_change_amount: Additional payments via debt system
Running Balance for Payment History
// First payment includes all bill payments
if ($x == 1) {
$wait_before = $waitvalue;
$wait_aftre = $wait_before - ($cardvalue + $cashvalue + $companycarry + $clientdebtchangeamount);
}
// Subsequent payments
else {
$wait_before = $wait_aftre;
$wait_aftre = $wait_before - $clientdebtchangeamount;
}
---
๐ Security & Permissions
Authentication
- โข All actions require authentication via
include_once("../public/authentication.php") - โข User session validation before any database operations
Input Validation
- โข POST parameters validated for client selection
- โข Date parameters checked for proper format
- โข Bill numbers validated as integers
---
๐ Performance Considerations
Database Optimization
1. Indexes Required:
- bills(clientid, billdate) for client/date filtering
- bills(billno) for bill number lookups
- clientdebtchange(billid) for payment aggregation
2. Query Performance:
- Efficient WHERE clause construction
- Proper date range filtering with time stamps
- Aggregation of payments per bill using sumByBillId()
---
๐ Common Issues & Troubleshooting
1. Incorrect Outstanding Amounts
Issue: Outstanding debt calculations don't match actual amounts
Cause: Missing payment records or incorrect payment linking
Debug:
-- Check payment sum for specific bill
SELECT SUM(clientdebtchangeamount)
FROM clientdebtchange
WHERE billid = [BILL_NUMBER];
-- Verify bill amounts
SELECT waitvalue, cashvalue, cardvalue, companycarry
FROM bills
WHERE billno = [BILL_NUMBER];
2. Missing Bills in Reports
Issue: Bills not appearing in outstanding or paid reports
Cause: Date range issues or filtering problems
Fix: Ensure proper date formatting and check filter logic:
// Proper date filtering
$where .= ' and date(billdate) >= "' . $start_date . '" and date(billdate) <= "' . $end_date . '"';
---
๐งช Testing Scenarios
Test Case 1: Outstanding Bills Accuracy
1. Create service bill with partial payment
2. Verify outstanding amount calculation
3. Make additional payment
4. Confirm updated outstanding balance
5. Check bill removal when fully paid
Test Case 2: Payment History Completeness
1. Select bill with multiple payments
2. Verify all payments appear in history
3. Check running balance calculations
4. Confirm payment dates and amounts
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข clientReportsController.md - Comprehensive client reports
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur