Debtclient Documentation

Client Debt Analysis Controller Documentation

File: /controllers/debtclientController.php

Purpose: Generates comprehensive debt analysis reports for customers, tracking sales, returns, payments, and debt balances by customer types and date ranges

Last Updated: December 20, 2024

Total Functions: 4+

Lines of Code: ~682

---

๐Ÿ“‹ Overview

The Client Debt Analysis Controller provides detailed financial analysis of customer accounts, focusing on debt tracking and payment analysis. It processes:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Analysis Tables

Table NamePurposeKey Columns
**client**Customer master dataclientid, clientname, clientdebt, typeclientid, clientarea
**clientdebtchange**Customer debt transaction historyclientdebtchangeid, clientid, clientdebtchangeamount, clientdebtchangetype, clientdebtchangedate, tablename
**typeclient**Customer type classificationstypeclientid, typeclientname
### Transaction Analysis Tables

Table NamePurposeKey Columns
**sellbill**Sales transactionssellbillid, sellbillclientid, sellbilltotalbill, sellbillaftertotalbill, sellbilltotalpayed, sellbilldate
**returnsellbill**Return transactionsreturnsellbillid, returnsellbillclientid, returnsellbilltotalbill, returnsellbillaftertotalbill, returnsellbilldate
**sellbillandrutern**Combined sale/return billssellbillid, sellbillclientid, sellbillprice, returnsellbillprice, sellbilldate
### Geographic Reference Tables

Table NamePurposeKey Columns
**clientarea**Customer area definitionsid, name, description
**government**Government/province datagovernmentid, governmentname
**goverarea**Government area subdivisionsgoverareaid, governmentid, goverareaname
---

๐Ÿ”‘ Key Functions

1. Default Action - Report Setup

Location: Line 178

Purpose: Display the debt analysis form with customer type filters

Process Flow:

1. Load all customer types for filtering

2. Load YouTube tutorial links

3. Build initial query strings for different bill types

4. Apply date filtering if provided

5. Display analysis form template

Key Variables:

---

2. show - Generate Debt Analysis Report

Location: Line 227

Purpose: Main analysis function that processes customer debt by type and date range

Function Signature:

// Triggered when: do=show
$TypeClient = $_POST['TypeClient'];
$showDebitOnly = $_POST['showDebit'];
$datefrom = filter_input(INPUT_POST, 'datefrom');
$dateto = filter_input(INPUT_POST, 'dateto');

Process Flow:

1. Filter Setup: Build customer type and debt filters

2. Date Range Processing: Apply date range to all transaction queries

3. Multi-Table Analysis:

- Query sales bills with customer joins

- Query return bills with customer data

- Query combined sale/return bills

4. Data Consolidation: Merge all transaction data by customer

5. Financial Calculations: Calculate totals and discounts

6. Payment Analysis: Call showAllOperations() for each customer

7. Report Generation: Display consolidated analysis

Customer Data Class:

class clientData {
    public $clientid;
    public $clientname;
    public $totalsell = 0;
    public $totalreturn = 0;
    public $totaldiscount = 0;
    public $clientdebt = 0;
    public $clientareaName;
    public $clientgovernmentname;
}

---

3. showAllOperations() - Customer Payment Analysis

Location: Line 438

Purpose: Analyze all financial operations for a specific customer

Function Signature:

function showAllOperations($clientid, $datefrom, $dateto)

Process Flow:

1. Sales Analysis: Call showSellBillsByClientAndDate()

2. Returns Analysis: Call showReturnSellBillsByClientAndDate()

3. Payment Analysis: Query clientdebtchange table for payments

4. Transaction Processing: Handle different payment types

5. Double-counting Prevention: Avoid counting bill payments twice

6. Net Calculation: Return total payment amount

Payment Types Handled:

---

4. showSellBillsByClientAndDate() - Sales Transaction Analysis

Location: Line 515

Purpose: Retrieve and process sales bills for customer within date range

Function Signature:

function showSellBillsByClientAndDate($clientid, $datefrom, $dateto)

Process Flow:

1. Regular Sales Bills: Query sellbill table

2. Combined Bills: Query sellbillandrutern table

3. Quantity Calculation: Count products in each bill

4. Discount Processing: Handle percentage and fixed discounts

5. Status Filtering: Exclude cancelled bills

6. Data Merging: Combine both datasets

Discount Calculation:

if ($sellbilldiscounttype == 1) {
    // Fixed amount discount
    $sellbillprice = ($bill->sellbillprice) - ($bill->sellbilldiscount);
} else {
    // Percentage discount  
    $sellbillprice = ($bill->sellbillprice) - ((($bill->sellbillprice) * ($bill->sellbilldiscount)) / 100);
}

---

5. showReturnSellBillsByClientAndDate() - Returns Analysis

Location: Line 597

Purpose: Retrieve and process return bills for analysis

Function Signature:

function showReturnSellBillsByClientAndDate($clientid, $datefrom, $dateto)

Process Flow:

1. Regular Returns: Query returnsellbill table

2. Combined Returns: Query sellbillandrutern for return portions

3. Quantity Tracking: Count returned products

4. Total Calculation: Sum return amounts

5. Data Structure: Return array with both datasets

Return Value Structure:

$returnsellbill = array($returnsellbillData, $sellbillandruternData);
// [0] = Pure return bills
// [1] = Combined bill return portions

---

๐Ÿ”„ Workflows

Workflow 1: Comprehensive Debt Analysis

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Need Customer Debt Analysis
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Set Analysis Parameters
- Select customer type filter
- Choose debt-only vs all customers
- Set date range (from/to dates)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Build Query Filters
- Customer type WHERE clause
- Date range filters for each bill type
- Debt balance filters if debt-only selected
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Query Sales Transactions
- Regular sales bills with customer data
- Combined sale/return bills (sale portion)
- Calculate quantities and totals
- Apply date and customer type filters
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Query Return Transactions
- Regular return bills with customer data
- Combined bills (return portion)
- Calculate returned quantities and amounts
- Apply same filters as sales
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Consolidate Customer Data
FOR EACH customer found in any transaction:
โ”‚
โ†’ Create or update clientData object
โ†’ Add sales totals and discounts
โ†’ Add return totals and discounts
โ†’ Include area and government info
โ”‚ โ””โ”€โ†’ Store current debt balance โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Calculate Financial Metrics
FOR EACH customer:
โ”‚
โ†’ Net sales = Total sales - Total returns
โ†’ Net discount = Sales discount - Return discount
โ†’ Call showAllOperations() for payment analysis
โ”‚ โ””โ”€โ†’ Calculate payment vs debt relationship โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
7Generate Analysis Report
- Display customer list with financial summary
- Show totals by customer type
- Include geographic groupings
- Present payment analysis results
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Workflow 2: Payment Analysis Deep Dive

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Analyze Customer Payments
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Get Sales with Payments
- Query sellbill table for date range
- Query sellbillandrutern for combined bills
- Extract sellbilltotalpayed amounts
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Get Return Credit Applications
- Query return bills for credit amounts
- Check combined bills for return credits
- Calculate net return impact
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Analyze Debt Change Transactions
FOR EACH debt change record:
โ”‚
โ†’ Check transaction source table
โ”œโ”€โ†’ IF sellbillController: Use bill payment amount
โ†’ ELSE: Use debt change amount
โ†’ Apply type (0=increase debt, 1=decrease debt)
โ”‚ โ””โ”€โ†’ Exclude clientController.php entries โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Prevent Double Counting
- Add bill payments from step 1
- Subtract bill payments already in debt changes
- Add bill payments from combined bills
- Calculate net payment total
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Return Payment Analysis
- Total payment amount received
- Payment breakdown by source
- Net cash flow for the period
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)DefaultDisplay debt analysis form with customer type filters
`do=show`Main analysisGenerate comprehensive debt analysis report
### Required Parameters

Debt Analysis (do=show):

Special Customer Type Values

---

๐Ÿ“Š Analysis Metrics

Customer Financial Summary

// Net sales calculation
$t_data->t_bure = $t_data->sellbillaftertotalbill - $t_data->returnsellbillaftertotalbill;

// Total discount calculation  
$t_data->t_dis = $t_data->selldis - $t_data->returnselldis + $t_data->sellandreturndis;

// Payment analysis
$dataa->totalPrice = showAllOperations($clientid, $datefrom, $dateto);

Geographic Analysis

Transaction Analysis

---

๐Ÿ”’ Security & Permissions

Authentication Requirements

include_once("../public/authentication.php");

Data Access Control

Session Management

---

๐Ÿ“Š Performance Considerations

Query Optimization

1. Complex Joins: Multiple table joins for customer, geographic, and transaction data

2. Date Range Filtering: Efficient indexing needed on date columns

3. Customer Type Filtering: Index on typeclientid for performance

Memory Management

Recommended Indexes

-- Customer analysis optimization
CREATE INDEX idx_client_type_debt ON client(typeclientid, clientdebt);
CREATE INDEX idx_sellbill_client_date ON sellbill(sellbillclientid, sellbilldate);
CREATE INDEX idx_returnsellbill_client_date ON returnsellbill(returnsellbillclientid, returnsellbilldate);
CREATE INDEX idx_clientdebtchange_client_date ON clientdebtchange(clientid, clientdebtchangedate);

---

๐Ÿ› Common Issues & Troubleshooting

1. Incomplete Geographic Data

Issue: Customers show "ู„ุงูŠูˆุฌุฏ" for area or government

Cause: Missing geographic assignments or deleted reference data

Solution:

-- Check for customers without geographic data
SELECT c.clientid, c.clientname, c.clientarea, ca.name as areaname
FROM client c 
LEFT JOIN clientarea ca ON c.clientarea = ca.id
WHERE ca.id IS NULL;

2. Discount Calculation Errors

Issue: Discount amounts don't match expected values

Cause: Mixed percentage and fixed amount discount types

Debug:

// Verify discount calculation logic
if ($sellbilldiscounttype == 1) {
    // Fixed amount - direct subtraction
    $discount = $sellbilldiscount;
} else {  
    // Percentage - calculate from total
    $discount = $sellbilltotalbill * $sellbilldiscount / 100;
}

3. Payment Double-Counting

Issue: Payment totals appear inflated

Cause: Bill payments counted in both debt changes and bill records

Verification:

-- Check for duplicate payment counting
SELECT 
    sb.sellbillid, 
    sb.sellbilltotalpayed,
    cdc.clientdebtchangeamount
FROM sellbill sb
JOIN clientdebtchange cdc ON cdc.clientdebtchangemodelid = sb.sellbillid 
    AND cdc.tablename = 'sellbillController.php'
WHERE sb.sellbilltotalpayed > 0 AND cdc.clientdebtchangeamount > 0;

4. Date Range Performance Issues

Issue: Analysis takes too long with large date ranges

Cause: Insufficient indexing or too broad date range

Solutions:

---

๐Ÿงช Testing Scenarios

Test Case 1: Customer Type Analysis

1. Create customers of different types with varying debt levels
2. Run analysis with specific customer type filter
3. Verify only correct customer type appears in results
4. Check debt-only filter excludes zero-debt customers

Test Case 2: Date Range Filtering

1. Create transactions across multiple date periods
2. Set specific date range for analysis
3. Verify only transactions within range are included
4. Test edge cases (same day, single day ranges)

Test Case 3: Payment Analysis Accuracy

1. Create customer with various payment types
2. Add sales with payments, direct payments, check deposits
3. Run payment analysis
4. Verify no double-counting occurs
5. Check totals match manual calculation

Test Case 4: Geographic Grouping

1. Set up customers in different areas and governments
2. Run analysis and verify geographic data appears
3. Test with customers missing geographic assignments
4. Confirm "ู„ุงูŠูˆุฌุฏ" appears for missing data

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur