ClientPayedDeptSellBills Documentation
Client Paid Debt Sell Bills Controller Documentation
File: /controllers/clientPayedDeptSellBillsController.php
Purpose: Manages debt payment tracking for sales bills and return sales bills with comprehensive payment analysis
Last Updated: December 20, 2024
Total Functions: 8 main actions + print functionality
Lines of Code: ~649
---
๐ Overview
The Client Paid Debt Sell Bills Controller is a specialized payment tracking system for sales transactions. It handles both regular sales bills (sellbill) and return sales bills (returnsellbill), providing detailed debt management, payment history, and receipt printing capabilities.
Primary Functions
- โ Sales bill debt tracking and payment monitoring
- โ Return sales bill payment tracking (credit/refund management)
- โ Outstanding debt calculation for sales transactions
- โ Comprehensive payment history with drill-down details
- โ Date range filtering and client-specific analysis
- โ Receipt printing for payment confirmations
- โ Dual operation support (sales and returns in single interface)
- โ Arabic text conversion for printed amounts
Related Controllers
- โข sellbillController.php - Sales bill creation and management
- โข returnsellbillController.php - Return sales bill management
- โข clientReportsController.php - Comprehensive client reports
- โข clientPayedDeptController.php - General debt payment processing
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **sellbill** | Sales bills master | sellbillid, sellbillclientid, sellbilldate, sellbilltotalpayed, sellbillfinalbill, sellbillaftertotalbill, conditions | |
| **returnsellbill** | Return sales bills | returnsellbillid, returnsellbillclientid, returnsellbilldate, returnsellbilltotalpayed, returnsellbillfinalbill, returnsellbillaftertotalbill | |
| **clientdebtchange** | Payment tracking log | clientdebtchangeid, clientid, clientdebtchangeamount, billid, tablename, clientdebtchangedate, paytype, comment, dailyentryid |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **client** | Customer master data | clientid, clientname | |
| **user** | System users | userid, employeename | |
| **programsettings** | System configuration | programsettingsid, currancy, laterNakdiClientId | |
| **youtubelink** | Tutorial/help links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action (empty $do) - Outstanding Sales Bills
Location: Lines 115-207
Purpose: Display sales bills with outstanding debt balances
Function Signature:
// Parameters: client, from, to, bill_no, obgyBillType
$client = $_POST['client'];
$bill_no = $_REQUEST['bill_no'];
$start_date = $_REQUEST['from'];
$end_date = $_REQUEST['to'];
$obgyBillType = (int) $_POST['obgyBillType'];
Process Flow:
1. Build dynamic SQL query with filters
2. Query sellbill table with conditions = 0 (active bills)
3. Batch load related data (users, clients, payments)
4. Calculate outstanding debt per bill
5. Filter bills with waitvalue > 0
Key SQL Query:
SELECT sellbillid,sellbilldate,pricetype,sellbilltotalpayed,sellbillfinalbill,
sellbillaftertotalbill,conditions,userid,sellbillclientid
FROM sellbill WHERE conditions = 0 AND [filters]
ORDER BY sellbilldate DESC, sellbillid DESC
Debt Calculation:
$waitvalue = $value['sellbillfinalbill'] - $client_dept_change_amount;
if ($waitvalue > 0) {
// Include in outstanding bills
}
---
2. show - Paid Sales Bills Summary
Location: Lines 208-290
Purpose: Show summary of sales bills that have received payments
Process Flow:
1. Query clientdebtchange for bills with payments
2. Filter by tablename = "clientPayedDeptSellBillsController.php"
3. Load corresponding bill details
4. Calculate totals across all paid bills
Payment Aggregation:
SELECT billid, sum(clientdebtchangeamount) as clientdebtchangeamount
FROM clientdebtchange
WHERE billid IN([bill_ids]) AND tablename = 'clientPayedDeptSellBillsController.php'
GROUP BY billid
---
3. show_details / editprint / details - Payment History Details
Location: Lines 291-355
Purpose: Detailed payment breakdown for a specific sales bill
Features:
- โข Complete payment timeline with dates
- โข Running balance calculations
- โข User information for each payment
- โข Daily entry ID tracking for audit trails
- โข Payment type classification
Running Balance Logic:
if ($x == 1) {
$wait_before = $waitvalue;
$wait_aftre = $wait_before - $clientdebtchangeamount;
} else {
$wait_before = $wait_aftre;
$wait_aftre = $wait_before - $clientdebtchangeamount;
}
---
4. ret - Outstanding Return Bills
Location: Lines 358-449
Purpose: Track return sales bills with outstanding credit/refund amounts
Key Differences from Sales Bills:
- โข Uses
returnsellbilltable - โข Debt calculation:
waitvalue + payment_amount(credits increase debt) - โข Filter by tablename = "clientPayedDeptReturnSellBillsController.php"
- โข Different business logic for credit management
---
5. showret - Paid Return Bills Summary
Location: Lines 450-534
Purpose: Summary of return bills with processed payments/credits
---
6. show_details_ret / editprintret / detailsret - Return Payment Details
Location: Lines 535-599
Purpose: Detailed payment/credit history for return bills
Return Balance Calculation:
// For returns, payments increase the debt (credits to customer)
$wait_aftre = $wait_before + $clientdebtchangeamount;
---
7. print - Payment Receipt Printing
Location: Lines 603-644
Purpose: Generate printable payment receipts
Features:
- โข Arabic number-to-text conversion for amounts
- โข Support for both sales (type=0) and return (type=1) bills
- โข Special handling for cash customers (laterNakdiClientId)
- โข Currency formatting from program settings
Arabic Text Conversion:
$ar_number = new convert_ar($totalpaid, "male");
$totalWritten = $ar_number->convert_number();
$totalWritten .= ' ' . $Programsettingdata->currancy . ' ููุท ูุงุบูุฑ';
---
๐ Workflows
Workflow 1: Outstanding Sales Bills Analysis
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default action | Outstanding sales bills | |
| `do=show` | show() | Paid sales bills summary | |
| `do=show_details` | show_details() | Sales bill payment history | |
| `do=editprint` | show_details() | Payment details for editing | |
| `do=details` | show_details() | Payment details view | |
| `do=ret` | ret() | Outstanding return bills | |
| `do=showret` | showret() | Paid return bills summary | |
| `do=show_details_ret` | show_details_ret() | Return bill payment history | |
| `do=editprintret` | show_details_ret() | Return payment details for editing | |
| `do=detailsret` | show_details_ret() | Return payment details view | |
| `do=print` | print() | Payment receipt printing |
Outstanding Sales 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 ID for exact match (optional) - โข
obgyBillType- Bill type filter (optional)
Payment Receipt (do=print):
- โข
bill- Bill ID (required) - โข
type- Bill type (0=sales, 1=return, required) - โข
m- Payment amount (required)
---
๐งฎ Calculation Methods
Outstanding Debt for Sales Bills
// Final bill amount minus total payments made
$waitvalue = $sellbillfinalbill - $client_dept_change_amount;
// Total payments = bill payments + additional debt payments
$total_payed = $sellbilltotalpayed + $client_dept_change_amount;
Outstanding Credit for Return Bills
// For returns, payments increase the outstanding amount (credit owed)
$waitvalue = $sellbillfinalbill + $client_dept_change_amount;
Payment Receipt Totals
// Convert numeric amount to Arabic text
$ar_number = new convert_ar($totalpaid, "male");
$totalWritten = $ar_number->convert_number() . ' ' . $currency . ' ููุท ูุงุบูุฑ';
---
๐ Security & Permissions
Authentication
- โข All actions require authentication via
include_once("../public/authentication.php") - โข Session validation before database operations
Payment Tracking Security
- โข Payments linked by
tablenameto prevent cross-controller contamination - โข Daily entry ID tracking for audit trails
- โข User ID logging for payment accountability
---
๐ Performance Considerations
Optimized Query Strategy
1. Batch Loading: Single queries for users, clients, and payments instead of N+1 queries
2. Indexed Filtering: Efficient WHERE clauses with proper indexing
3. Array Optimization: Custom indexing functions (customArrayIndexOne)
Required Indexes:
- โข
sellbill(sellbillclientid, sellbilldate, conditions) - โข
returnsellbill(returnsellbillclientid, returnsellbilldate, conditions) - โข
clientdebtchange(billid, tablename)
---
๐ Common Issues & Troubleshooting
1. Incorrect Payment Totals
Issue: Payment amounts don't match expected values
Cause: Mixed tablename values in clientdebtchange
Fix: Ensure proper tablename filtering:
-- Check payments for specific controller
SELECT * FROM clientdebtchange
WHERE billid = [BILL_ID]
AND tablename = 'clientPayedDeptSellBillsController.php';
2. Missing Bills in Outstanding Report
Issue: Bills with debt not appearing
Cause: conditions flag not properly filtered
Debug:
-- Check bill conditions
SELECT sellbillid, conditions, sellbillfinalbill
FROM sellbill
WHERE sellbillid = [BILL_ID];
---
๐งช Testing Scenarios
Test Case 1: Outstanding Debt Accuracy
1. Create sales bill with partial payment
2. Verify correct outstanding amount calculation
3. Make additional payment via debt system
4. Confirm updated outstanding balance
5. Check bill removal when fully paid
Test Case 2: Return Bill Credit Tracking
1. Create return bill with partial refund
2. Verify credit amount calculation (addition logic)
3. Process additional credit payment
4. Confirm updated credit balance
Test Case 3: Receipt Printing
1. Select bill with payment
2. Generate receipt with Arabic text conversion
3. Verify currency formatting
4. Check client name handling for cash customers
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข sellbillController.md - Sales bill management
- โข clientReportsController.md - Client reporting system
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur