ClientPayedDeptReport Documentation

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:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**bills**Service bills master dataid, billno, clientid, finalnetbillvalue, waitvalue, cashvalue, cardvalue, companycarry, billdate
**clientdebtchange**Client debt transaction logclientid, clientdebtchangeamount, billid (referenced via billno)
### Reference Tables

Table NamePurposeKey Columns
**client**Customer master dataclientid, clientname
**youtubelink**Tutorial video linksyoutubelinkid, 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:

---

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:

---

๐Ÿ”„ Workflows

Workflow 1: Client Debt Payment Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Client Debt Payment Report
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Parse Filter Parameters
- Client ID (optional)
- Date range (optional)
- Bill number (optional)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Build Dynamic WHERE Clause
IF client selected:
โ”‚ โ””โ”€ Add clientid filter โ”‚
IF bill number provided:
โ”‚ โ””โ”€ Add bill ID filter โ”‚
IF date range provided:
โ”‚ โ””โ”€ Add date range filter โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Query Bills with Filters
- Query bills table with WHERE clause
- Get all matching bill records
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Calculate Outstanding Balances
FOR EACH bill:
โ”‚
โ†’ Get total payments from clientdebtchange
โ”‚
โ†’ Calculate outstanding: waitvalue - payments
โ”‚
โ†’ Calculate total paid: cash+card+carry+payments
โ”‚
โ”‚ โ””โ”€โ†’ Include only if outstanding > 0 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Prepare Display Data
- Load client information if specified
- Assign bill data to template
- Set header message with client and date info
- Load YouTube tutorial links
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Display Report
- Show via clientPayedDeptReportView/show.html
- Include custom script references
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty) or `do=show`Default actionGenerate client debt payment report
### Required/Optional Parameters

Report Generation (do=show):

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:

Total Paid Calculation

$total_paid = $bill->cashvalue + $bill->cardvalue + $bill->companycarry + $debt_payments;

Where:

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

SQL Injection Protection

---

๐Ÿ“Š 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

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When debt calculation logic changes