Clientdebtmessdiscoverer Documentation
Client Debt Discrepancy Discovery Controller Documentation
File: /controllers/clientdebtmessdiscoverer.php
Purpose: Discovers and reports discrepancies between actual customer debt records and calculated debt from transaction history, providing debt reconciliation tools
Last Updated: December 20, 2024
Total Functions: 1 main process + SQL procedures
Lines of Code: ~195
---
๐ Overview
The Client Debt Discrepancy Discovery Controller is a specialized tool designed to identify inconsistencies in customer debt calculations. It creates temporary tables to recalculate debt balances from transaction history and compares them with recorded debt amounts. This is essential for:
- โข Debt Reconciliation: Finding customers whose recorded debt doesn't match transaction history
- โข Data Integrity: Ensuring accounting accuracy across the system
- โข Audit Trails: Providing detailed transaction-by-transaction debt calculations
- โข System Maintenance: Cleaning up inconsistent debt records
- โข Financial Accuracy: Maintaining reliable customer account balances
Primary Functions
- โ Create temporary calculation tables for debt analysis
- โ Recalculate customer debt from complete transaction history
- โ Compare calculated debt with recorded debt balances
- โ Identify customers with debt discrepancies
- โ Provide transaction-by-transaction audit trail
- โ Handle concurrent transaction processing
- โ Generate comprehensive discrepancy reports
Related Controllers
- โข clientReportsController.php - Customer debt reporting
- โข debtclientController.php - Debt analysis
- โข clientController.php - Customer management
- โข clientPayedDeptController.php - Payment processing
---
๐๏ธ Database Tables
Temporary Analysis Tables (Created by Process)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **clienttemp** | Calculated debt storage | clientid, clientdebtcalc | |
| **clientdebtchangetemp** | Transaction history copy | clientdebtchangeid, clientid, clientdebtchangebefore, clientdebtchangeamount, clientdebtchangetype, clientdebtchangeafter, tablename |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **client** | Customer master data | clientid, clientname, clientdebt, userid | |
| **clientdebtchange** | Customer debt transaction log | clientdebtchangeid, clientid, clientdebtchangeamount, clientdebtchangetype, clientdebtchangedate, tablename |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **programsettings** | System configuration | programsettingsid, settingkey, settingvalue | |
| **youtubelink** | Tutorial links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action - Debt Discrepancy Analysis
Location: Line 65
Purpose: Main process that performs complete debt reconciliation analysis
Process Flow:
Phase 1: Table Structure Creation
// Create temporary tables if they don't exist
$sqlQuery[] = 'CREATE TABLE IF NOT EXISTS `clienttemp` (
`clientid` int(11) NOT NULL AUTO_INCREMENT,
`clientdebtcalc` float NOT NULL,
PRIMARY KEY (`clientid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
$sqlQuery[] = 'CREATE TABLE IF NOT EXISTS `clientdebtchangetemp` (
`clientdebtchangeid` int(11) NOT NULL,
`clientid` int(11) NOT NULL,
`clientdebtchangebefore` float NOT NULL,
`clientdebtchangeamount` float NOT NULL,
`clientdebtchangetype` int(11) NOT NULL,
`clientdebtchangeafter` float NOT NULL,
`tablename` varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;';
Phase 2: Data Population
// Initialize clienttemp with all customers (except ID 1)
$sqlQuery[] = 'INSERT IGNORE INTO clienttemp
SELECT clientid,0 FROM client where clientid !=1';
Phase 3: Incremental Transaction Loading
// Get last processed transaction ID
$last_clientdebtchangeid_inTemp = (int) $programSettingEX->runSqlQueryGetSingleResult(
'select clientdebtchangeid from clientdebtchangetemp order by clientdebtchangeid desc limit 1'
);
// Insert only new transactions
$sqlQuery[] = 'INSERT INTO clientdebtchangetemp
SELECT clientdebtchangeid,clientid,clientdebtchangebefore,clientdebtchangeamount,clientdebtchangetype,clientdebtchangeafter,tablename
FROM clientdebtchange
where del = 0 and tablename != "clientDeficitController.php" and clientid !=1 and clientdebtchangeid > ' . $last_clientdebtchangeid_inTemp . ';';
Phase 4: Data Type Correction
// Reverse clientPayedDeptController.php transaction types (they are entered in reverse)
$sqlQuery[] = 'UPDATE clientdebtchangetemp
SET clientdebtchangetype = CASE clientdebtchangetype WHEN 0 THEN 1 WHEN 1 THEN 0 ELSE clientdebtchangetype END
where tablename="clientPayedDeptController.php" and id > ' . $last_clientdebtchangeid_inTemp . ';';
Phase 5: Debt Recalculation Procedure
CREATE PROCEDURE ROWPERROW(IN i INT)
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE debtbefore INT DEFAULT 0;
DECLARE clientdebtchangetypeinrow INT DEFAULT 0;
DECLARE clientidinrow INT DEFAULT 0;
SELECT COUNT(*) FROM clientdebtchangetemp INTO n;
WHILE i <= n DO
-- Get client ID for current row
SELECT clientid FROM clientdebtchangetemp where id=i INTO clientidinrow;
SET debtbefore=0;
-- Calculate debt before from previous transactions
SELECT clientdebtchangeafter FROM clientdebtchangetemp
where id<i and clientid = clientidinrow
order by id desc limit 1 INTO debtbefore;
-- Update debt before in current row
update clientdebtchangetemp set clientdebtchangebefore = debtbefore where id=i;
-- Calculate debt after based on transaction type
SELECT clientdebtchangetype FROM clientdebtchangetemp where id=i INTO clientdebtchangetypeinrow;
IF clientdebtchangetypeinrow = 1 THEN
-- Type 1: Debt decrease (payment)
update clientdebtchangetemp set clientdebtchangeafter = clientdebtchangebefore - clientdebtchangeamount where id = i;
ELSE
-- Type 0: Debt increase (sale/charge)
update clientdebtchangetemp set clientdebtchangeafter = clientdebtchangebefore + clientdebtchangeamount where id = i;
END IF;
-- Update final calculated debt in clienttemp
update clienttemp set clientdebtcalc = (select clientdebtchangeafter from clientdebtchangetemp where id=i) where clientid=clientidinrow;
SET i = i + 1;
END WHILE;
End;
Phase 6: Results Display
// Get customers with debt discrepancies
$shownData = $clientEX->getClientsDebtAfterRevision();
// Display results showing calculated vs recorded debt
---
๐ Workflows
Workflow 1: Complete Debt Reconciliation Process
Workflow 2: Incremental Processing Strategy
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Main process | Execute complete debt reconciliation analysis | |
| `do=show` | Error message | Shows "Try again Later" message | |
| `do=sucess` | Success page | Display success template | |
| `do=error` | Error page | Display error template |
๐ง Technical Implementation
SQL Procedures Used
The system creates a sophisticated stored procedure ROWPERROW that:
- โข Processes transactions in chronological order
- โข Maintains running debt balances for each customer
- โข Handles different transaction types correctly
- โข Updates both before/after amounts for audit trails
Data Type Corrections
-- Correct reversed transaction types from payment controller
UPDATE clientdebtchangetemp
SET clientdebtchangetype = CASE clientdebtchangetype
WHEN 0 THEN 1
WHEN 1 THEN 0
ELSE clientdebtchangetype
END
WHERE tablename="clientPayedDeptController.php";
Incremental Processing Benefits
1. Performance: Only processes new transactions on subsequent runs
2. Scalability: Handles large transaction volumes efficiently
3. Reliability: Maintains calculation history for audit purposes
4. Flexibility: Can be run frequently without performance impact
---
๐ Analysis Results
Discrepancy Types Identified
1. Calculation Errors: Where recorded debt โ calculated debt
2. Missing Transactions: Gaps in transaction history
3. Type Errors: Incorrect transaction type assignments
4. Data Corruption: Inconsistent before/after amounts
Report Columns
The analysis provides:
- โข Customer ID and name
- โข Current recorded debt (from client table)
- โข Calculated debt (from transaction history)
- โข Difference amount
- โข Last transaction date
- โข Transaction count
---
๐ Security & Permissions
Data Protection
- โข Creates temporary tables that don't affect live data
- โข Uses transaction isolation for data consistency
- โข Preserves original data while performing analysis
Access Control
- โข Requires standard authentication
- โข Uses session-based user identification
- โข Maintains audit trail of analysis execution
Performance Safeguards
- โข Excludes system customer (ID=1) from analysis
- โข Filters out deleted transactions
- โข Uses efficient incremental processing
---
๐ Performance Considerations
Optimization Strategies
1. Incremental Processing: Only processes new transactions
2. Indexed Queries: Uses primary keys for efficient lookups
3. Memory Management: Uses temporary tables to avoid large result sets
4. Stored Procedures: Leverages database-level processing
Resource Requirements
- โข Temporary disk space for clientdebtchangetemp table
- โข Processing time proportional to new transactions
- โข Memory for stored procedure execution
- โข Network bandwidth for result display
Scalability Factors
- โข First run processes all historical data (slow)
- โข Subsequent runs only process new transactions (fast)
- โข Performance degrades with very large customer bases
- โข Consider pagination for enterprise-scale implementations
---
๐ Common Issues & Troubleshooting
1. Procedure Creation Errors
Issue: ROWPERROW procedure creation fails
Cause: Database permission issues or syntax errors
Solution:
-- Check procedure exists
SHOW PROCEDURE STATUS LIKE 'ROWPERROW';
-- Manually drop and recreate if needed
DROP PROCEDURE IF EXISTS ROWPERROW;
2. Large Dataset Performance
Issue: Process takes very long time on first run
Cause: Processing entire transaction history
Solutions:
- โข Run during off-peak hours
- โข Consider processing in smaller date ranges
- โข Monitor database performance during execution
3. Discrepancy Investigation
Issue: Many customers show debt discrepancies
Cause: May indicate systematic data issues
Investigation Steps:
-- Check transaction type distribution
SELECT tablename, clientdebtchangetype, COUNT(*)
FROM clientdebtchangetemp
GROUP BY tablename, clientdebtchangetype;
-- Find customers with largest discrepancies
SELECT c.clientname, c.clientdebt, ct.clientdebtcalc,
(ct.clientdebtcalc - c.clientdebt) as difference
FROM client c
JOIN clienttemp ct ON c.clientid = ct.clientid
WHERE ABS(ct.clientdebtcalc - c.clientdebt) > 100
ORDER BY ABS(ct.clientdebtcalc - c.clientdebt) DESC;
4. Memory Issues
Issue: Process runs out of memory or disk space
Cause: Very large clientdebtchangetemp table
Solutions:
- โข Increase MySQL temp table size
- โข Add disk space for temporary tables
- โข Process in smaller batches
---
๐งช Testing Scenarios
Test Case 1: Clean Data Verification
1. Run analysis on known-good customer data
2. Verify no discrepancies appear
3. Check that calculated debt = recorded debt
4. Confirm transaction counts are accurate
Test Case 2: Known Discrepancy Detection
1. Manually create debt discrepancy (update client debt)
2. Run analysis process
3. Verify discrepancy appears in report
4. Check difference amount is correct
Test Case 3: Incremental Processing
1. Run full analysis and record results
2. Add new transactions to system
3. Run analysis again
4. Verify only new transactions were processed
5. Confirm final results include new data
Test Case 4: Performance Testing
1. Measure runtime on various data sizes
2. Test incremental vs full processing times
3. Monitor database resource usage
4. Verify memory and disk space consumption
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข clientReportsController.md - Customer debt reporting
- โข Database Schema Documentation - Table relationships
- โข System Maintenance Guide - Regular maintenance procedures
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur