Profitdetail Documentation

Profit Detail Controller Documentation

File: /controllers/profitdetailController.php

Purpose: Generates detailed profit breakdown reports for specific date ranges

Last Updated: December 20, 2024

Total Functions: 13

Lines of Code: 574

---

๐Ÿ“‹ Overview

The Profit Detail Controller provides comprehensive profit analysis by breaking down all revenue and cost components over a specified date range. It calculates:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Sales Tables

Table NamePurposeKey Columns
**sellbill**Sales transactionssellbillid, sellbillaftertotalbill, sellbilldiscount, sellbilldiscounttype
**sellbilldetail**Sales line itemssellbilldetailproductid, sellbilldetailquantity, sellbilldetailprice, buyprice, buydiscount
**sellbillandrutern**Combined sell/return billssellbillid, sellbillaftertotalbill, sellbillprice, returnsellbillprice
**sellandruternbilldetail**Combined bill detailssellbilldetailproductid, sellbilldetailquantity, selltype, buyprice
### Return Tables

Table NamePurposeKey Columns
**returnsellbill**Sales returnsreturnsellbillid, returnsellbillaftertotalbill, returnsellbilldiscount
**returnsellbilldetail**Return line itemsreturnsellbilldetailproductid, returnsellbilldetailquantity, buyprice
### Product & Inventory Tables

Table NamePurposeKey Columns
**product**Product master dataproductid, productname, productbuyprice, productsellprice
**productunit**Unit of measureproductunitid, productnumber, productunitname
**expenses**Business expensesexpensesid, expensesvalue, expensesdate
---

๐Ÿ”‘ Key Functions

1. show() / Default Action - Main Report Generator

Location: Line 154

Purpose: Generate comprehensive profit report for specified date range

Function Signature:

// Triggered when: do=show or empty $do
$startDate = $_REQUEST['from'];
$endDate = $_REQUEST['to'];

Process Flow:

1. Validate date parameters

2. Calculate total sales revenue

3. Calculate returns and refunds

4. Compute cost of goods sold

5. Process expenses

6. Calculate final profit

7. Display via profitdetailview/show.html

Calculations:

$totalSellPrice = getTotalSellbillByDate($startDate, $endDate) + getTotalAditionalSellbillByDate($startDate, $endDate);
$totalReturnSellPrice = getTotalReturnSellbillByDate($startDate, $endDate) + getTotalAditionalReturnSellbillByDate($startDate, $endDate);
$totalQuantityBuyPrice = getTotalQuantityBuyPriceByDate($startDate, $endDate) + getTotalAditionalQuantityBuyPriceByDate($startDate, $endDate);
$profitFinal = ($totalSellCost - $totalBuyCost) - $totalExpenses;

---

2. getTotalSellbillByDate() - Regular Sales Revenue

Location: Line 245

Purpose: Calculate total revenue from regular sales bills

Function Signature:

function getTotalSellbillByDate($startDate, $endDate)

Process Flow:

1. Query sellbill table with date filter

2. Sum sellbillaftertotalbill values

3. Return total sales amount

SQL Logic:

$sellbillData = $mySellbillEx->queryWithDateAndConditions($startDate, $endDate);
foreach ($sellbillData as $sellbill) {
    $totalSellbill += $sellbill->sellbillaftertotalbill;
}

---

3. getTotalAditionalSellbillByDate() - Combined Bill Sales

Location: Line 267

Purpose: Calculate revenue from combined sell/return bills (sell portion only)

Process Flow:

1. Query sellbillandrutern table

2. Filter for positive totals (sells, not returns)

3. Sum sellbillaftertotalbill where >= 0

Business Logic:

foreach ($sellbillandruternData as $sellbillandrutern) {
    if ($sellbillandrutern->sellbilltotalbill >= 0) {
        $totalSellbill += $sellbillandrutern->sellbillaftertotalbill;
    }
}

---

4. getTotalQuantityBuyPriceByDate() - Cost of Goods Sold

Location: Line 341

Purpose: Calculate total cost of goods sold with discount handling

Function Signature:

function getTotalQuantityBuyPriceByDate($startDate, $endDate)

Process Flow:

1. Query sellbilldetail for sold quantities

2. Get buy prices and discount information

3. Apply unit conversions

4. Calculate total cost with discounts

Discount Calculation:

if ($buyDiscount > 0 && $buyDiscountType == 0) {
    $productBuyPrice = $buyPrice - $buyDiscount; // Fixed amount
} elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
    $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice); // Percentage
}

Unit Conversion:

$productnumber = $myProductunitRecord->load($productunitId)->productnumber;
$productBuyPrice = $productBuyPrice * $productnumber;
$totalQuantityBuyPrice += ($quantity * $productBuyPrice);

---

5. getTotalReturnSellbillByDate() - Return Revenue

Location: Line 294

Purpose: Calculate total value of sales returns

Process Flow:

1. Query returnsellbill table

2. Sum returnsellbillaftertotalbill values

3. Return total return amount

---

6. getTotalExpensesByDate() - Business Expenses

Location: Line 558

Purpose: Calculate total business expenses for the period

Function Signature:

function getTotalExpensesByDate($startDate, $endDate)

Process Flow:

1. Query expenses table with date filter

2. Sum expensesValue column

3. Return total expenses

---

๐Ÿ”„ Workflows

Workflow 1: Profit Report Generation

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Enter Date Range
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Validate Date Parameters
- Check from/to dates provided
- Set default message
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Sales Revenue
- Get regular sales (getTotalSellbillByDate)
- Get combined bill sales (getTotalAditionalSellbill)
- Sum total sales revenue
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Calculate Returns & Refunds
- Get regular returns (getTotalReturnSellbillByDate)
- Get combined bill returns
- Sum total returns
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Calculate Cost of Goods Sold
- Get sold quantities with buy prices
- Apply discount calculations
- Handle unit conversions
- Sum total cost
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Calculate Business Expenses
- Query expenses for date range
- Sum all expense values
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Calculate Final Profit
- Net Sales = Sales - Returns
- Net Cost = COGS - Return Costs
- Profit = Net Sales - Net Cost - Expenses
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
7Display Results
- Assign all values to Smarty template
- Display profitdetailview/show.html
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty) or `do=show`Default actionGenerate profit detail report
`do=success`Success pageDisplay success message
`do=error`Error pageDisplay error message
### Required Parameters

Date Range Report (do=show):

---

๐Ÿงฎ Calculation Methods

Revenue Calculation

$totalSellPrice = getTotalSellbillByDate($startDate, $endDate) + 
                 getTotalAditionalSellbillByDate($startDate, $endDate);

Cost Calculation with Discounts

// Fixed discount
if ($buyDiscountType == 0) {
    $productBuyPrice = $buyPrice - $buyDiscount;
}
// Percentage discount  
else {
    $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
}

Unit Conversion

$productnumber = $myProductunitRecord->load($productunitId)->productnumber;
$productBuyPrice = $productBuyPrice * $productnumber;
$totalCost += ($quantity * $productBuyPrice);

Final Profit Formula

$totalSellCost = $totalSellPrice - $totalReturnSellPrice;
$totalBuyCost = $totalQuantityBuyPrice - $totalQuantityReturnBuyPrice;
$profitFinal = ($totalSellCost - $totalBuyCost) - $totalExpenses;

---

๐Ÿ”’ Security & Permissions

Input Validation

Potential Issues

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Indexes Required:

- sellbill(sellbilldate, conditions)

- sellbilldetail(sellbilldetaildate)

- returnsellbill(returnsellbilldate)

- expenses(expensesdate)

2. Query Performance:

- Multiple separate queries could be optimized with JOINs

- Large date ranges may cause timeouts

- No pagination implemented

Memory Usage

---

๐Ÿ› Common Issues & Troubleshooting

1. Incorrect Profit Calculations

Issue: Profit values don't match manual calculations

Causes:

Debug:

// Add debugging output
echo "Sales: " . $totalSellPrice . "\n";
echo "Returns: " . $totalReturnSellPrice . "\n";
echo "COGS: " . $totalQuantityBuyPrice . "\n";
echo "Expenses: " . $totalExpenses . "\n";

2. Missing Data

Issue: Some transactions not included in calculations

Causes:

Fix: Check conditions filters in queries

3. Unit Conversion Errors

Issue: Incorrect cost calculations due to unit mismatches

Solution: Verify productunit table data integrity

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Profit Calculation

1. Create test sales with known values
2. Add corresponding expense entries
3. Run report for test date range  
4. Verify calculations match expected values

Test Case 2: Discount Handling

1. Create sales with various discount types
2. Verify both fixed amount and percentage discounts
3. Check unit conversion accuracy
4. Confirm final calculations

Test Case 3: Return Processing

1. Create sales with subsequent returns
2. Verify return amounts subtract correctly
3. Check cost calculations for returned items
4. Confirm net profit accuracy

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur