StoreProfite Documentation

Store Profit Controller Documentation

File: /controllers/storeProfiteController.php

Purpose: Comprehensive store profitability analysis with multi-pricing method support and detailed cost calculations

Last Updated: December 21, 2024

Total Functions: 2

Lines of Code: ~600

---

๐Ÿ“‹ Overview

The Store Profit Controller provides detailed profitability analysis across stores with comprehensive cost calculation methods. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Sales Tables

Table NamePurposeKey Columns
**sellbill**Sales bills mastersellbillid, sellbillstoreid, sellbillsysdate, sellbilltotalbill, sellbillaftertotalbill, deletedsellid
**sellbilldetail**Sales line itemssellbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, sellbilldetailtotalprice, productunitid
**returnsellbill**Return bills masterreturnsellbillid, returnsellbillstoreid, returnsellbillsysdate, returnsellbilltotalbill, returnsellbillaftertotalbill
**returnsellbilldetail**Return line itemsreturnsellbilldetailid, returnsellbillid, returnsellbilldetailproductid, returnsellbilldetailquantity, productunitid
**sellbillandrutern**Combined billssellbillid, sellbillstoreid, sellbillsysdate, sellbilltotalbill, sellbillaftertotalbill
**sellandruternbilldetail**Combined bill detailssellandruternbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, productunitid
### Product & Pricing Tables

Table NamePurposeKey Columns
**productunit**Product units with pricingproductunitid, productid, productnumber, overAllAveragePrice, buyprice, lastbuyprice, meanbuyprice
**product**Product master dataproductId, productName, productBuyPrice
### Store & User Tables

Table NamePurposeKey Columns
**store**Store informationstoreId, storeName
**user**System usersuserid, username, usergroupid
**usergroup**User groupsusergroupid, groupname
### System Tables

Table NamePurposeKey Columns
**programsettings**System settingsprogramsettingsid, settingkey, settingvalue
**youtubelink**Tutorial linksyoutubelinkid, title, url
---

๐Ÿ”‘ Key Functions

1. all Action - Main Profit Analysis

Location: Line 184

Purpose: Comprehensive profit analysis across stores with flexible cost calculation

Process Flow:

1. Load program settings and user permissions

2. Process search parameters (store, date range, pricing method)

3. Build dynamic query strings with store access control

4. Query sales bills, return bills, and combined bills

5. Calculate profits using selected pricing method

6. Apply discount calculations and generate totals

7. Display results via storeProfitView template

Key Variables:

---

2. loadProductUnitWithProductAndUnit() - Product Unit Data Loading

Location: Line 589

Purpose: Loads detailed product unit information with pricing data

Function Signature:

function loadProductUnitWithProductAndUnit($productId, $unitId)

Returns: Complete product unit object with all pricing fields and conversion factors

---

๐Ÿ”„ Workflows

Workflow 1: Store Profitability Analysis

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Select Store & Analysis Period
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Process Analysis Parameters
- Store selection (specific or all stores)
- Date range selection
- Cost calculation method selection
- User permission validation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Build Store Access Control
- Check user store permissions
- Apply searchinonestore restrictions
- Build store ID lists for filtering
- Default to today if no date specified
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Query Sales Bills
- Load sellbill records with store/date filters
- Apply user permission restrictions
- Exclude deleted bills (deletedsellid = 0)
- Group by store for analysis
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Process Each Sales Bill
FOR EACH sales bill:
โ”‚
โ†’ Load all bill detail records
โ”‚
โ†’ For each line item:
โ”‚ โ”œโ”€ Load product unit information
โ”‚ โ”œโ”€ Calculate final quantities (qty ร— unit)
โ”‚ โ”œโ”€ Select cost price based on method:
โ”‚
โ”‚
โ”‚
โ”‚
โ”‚
โ”‚
โ”‚
โ”‚ โ”‚ โ”‚ โ””โ”€ mean_tax: meanbuyprice_withTax โ”‚
โ”‚
โ”‚ โ”œโ”€ Calculate line cost (qty ร— unit ร— price)
โ”‚ โ”‚ โ””โ”€ Sum line sale value โ”‚
โ”‚
โ†’ Apply bill-level discount calculations
โ”‚ โ”œโ”€ Handle fixed amount discounts
โ”‚ โ”‚ โ””โ”€ Handle percentage discounts โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Calculate bill profit (sales - costs - discount) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Query Return Bills
- Load returnsellbill records
- Process return bill details
- Calculate return costs and values
- Apply return-specific discount logic
- Subtract from profit calculations
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Query Combined Bills
- Load sellbillandrutern records
- Process sellandruternbilldetail records
- Handle mixed sale/return transactions
- Apply appropriate cost calculations
- Include in overall profit analysis
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
7Calculate Final Profit Metrics
- Net profit per bill = (after total - costs)
- Aggregate profit by store
- Calculate profit margins and percentages
- Generate grand total across all stores
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
8Generate Profit Report
- Assign bill-level data to template
- Include store-wise summaries
- Display profit margins and totals
- Show cost calculation method used
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=all`Main analysisComprehensive store profit analysis
`do=sucess`Success pageDisplay success message
`do=error`Error pageDisplay error message
### Required Parameters for Analysis

Cost Calculation Methods

---

๐Ÿงฎ Calculation Methods

Cost Calculation by Method

switch ($buyPriceType) {
    case "first":
        $buyprice = (float) $mySellBillDetail->buyprice;
        break;
    case "last":
        $buyprice = (float) $mySellBillDetail->lastbuyprice;
        break;
    case "mean":
        $buyprice = (float) $mySellBillDetail->meanbuyprice;
        break;
    case "last_discount":
        $buyprice = (float) $mySellBillDetail->lastbuyprice_withDiscount;
        break;
    case "mean_discount":
        $buyprice = (float) $mySellBillDetail->meanbuyprice_withDiscount;
        break;
    case "generalPrice":
        $buyprice = (float) $overAllAveragePrice;
        break;
    case "tax":
        $buyprice = (float) $mySellBillDetail->lastbuyprice_withTax;
        break;
    case "mean_tax":
        $buyprice = (float) $mySellBillDetail->meanbuyprice_withTax;
        break;
}

// Calculate total cost for line item
$myBuyPrice = $buyprice * ($mySellBillDetail->sellbilldetailquantity * $myproductNumber);

Discount Processing

// Get bill discount settings
$sellbilldiscounttype = $mysellBill->sellbilldiscounttype;
$sellbilldiscount = $mysellBill->sellbilldiscount;

if ($sellbilldiscounttype == 1) {
    // Fixed amount discount
    $sellBillTotal = $mysellBill->sellbilldiscount;
} elseif ($sellbilldiscounttype == 2) {
    // Percentage discount
    $sellBillTotal = ($mysellBill->sellbilldiscount / 100) * $mysellBill->sellbilltotalbill;
}

Final Profit Calculation

// Calculate bill profit
$thisProfits += ($mySellBillDetail->sellbilldetailtotalprice) - $myBuyPrice;
$totalBuy += $myBuyPrice;
$totalSell += $mySellBillDetail->sellbilldetailtotalprice;

// Apply discount to final profit
$mysellBill->sellBillTotalx = $sellBillTotal;
$mysellBill->totalBuyx = $totalBuy;
$mysellBill->totalSellx = $totalSell;

// Net profit for bill
$totalOfTotal += ($mysellBill->sellbillaftertotalbill - $mysellBill->totalBuyx);

Product Unit Conversion

// Load product unit data
$productunitData = loadProductUnitWithProductAndUnit($productId, $unitId);
$overAllAveragePrice = $productunitData->overAllAveragePrice;
$myproductNumber = $productunitData->productnumber;

// Convert to base units for costing
$finalQuantity = $sellbilldetailquantity * $myproductNumber;
$lineCost = $buyprice * $finalQuantity;

---

๐Ÿ”’ Security & Permissions

Store Access Control

// Multi-store vs single-store access
if ($_SESSION['searchinonestore'] == 0) {
    // Multi-store user
    if ($_SESSION['storeids'] != 0) {
        $queryString .= ' sellbill.sellbillstoreid in (' . $_SESSION['storeids'] . ') AND';
    }
} else {
    // Single-store user
    $queryString .= ' sellbill.sellbillstoreid = ' . $_SESSION['storeid'] . ' AND';
}

// Admin override for store selection
if (isset($storeId) && $storeId != -1) {
    $queryString .= ' sellbill.sellbillstoreid = ' . $storeId . ' AND';
}

User Group Permissions

// Load user group data for permissions
$myUserGroupRecord = new UsergroupMySqlDAO();
$loadData = $myUserGroupRecord->load($_SESSION['usergroupid']);
$smarty->assign("loadData", $loadData);

Query Security

// Exclude deleted records
$queryString .= ' and sellbill.deletedsellid = 0 ';

// Date filtering with proper format
if (isset($dateFrom) && !empty($dateFrom) && isset($dateTo) && !empty($dateTo)) {
    $queryString .= ' DATE( sellbill.sellbillsysdate )  >= "' . $dateFrom . '" AND DATE( sellbill.sellbillsysdate ) <= "' . $dateTo . '"';
}

---

๐Ÿ“Š Performance Considerations

Query Optimization

1. Efficient Joins:

- Uses getSellBillsUsingQueryString() for optimized queries

- Proper JOIN relationships between bills and details

- Date indexing for performance

2. Memory Management:

- Processes bills in sequence rather than loading all at once

- Efficient aggregation of totals

- Minimal template variable usage

3. Database Indexes Required:

- sellbill(sellbillsysdate, sellbillstoreid, deletedsellid)

- sellbilldetail(sellbillid, sellbilldetailproductid)

- productunit(productid, productunitid)

Performance Monitoring

// Monitor query performance for large date ranges
$startTime = microtime(true);
$sellBill = $sellBillEx->getSellBillsUsingQueryString($queryString);
$queryTime = microtime(true) - $startTime;

// Alert if queries are slow
if ($queryTime > 2.0) {
    error_log("Slow profit analysis query: " . $queryTime . "s");
}

---

๐Ÿ› Common Issues & Troubleshooting

1. Incorrect Profit Calculations

Issue: Profit margins don't match manual calculations

Cause: Wrong pricing method selection or missing cost data

Debug:

// Debug cost calculation
echo "Selected Method: " . $buyPriceType . "<br>";
echo "Unit Data: " . print_r($productunitData, true) . "<br>";
echo "Buy Price: " . $buyprice . "<br>";
echo "Quantity: " . $sellbilldetailquantity . "<br>";
echo "Product Number: " . $myproductNumber . "<br>";
echo "Final Cost: " . $myBuyPrice . "<br>";

2. Store Access Issues

Issue: User can't see expected stores or data

Cause: Session permissions or store assignment problems

Debug:

// Check user store permissions
echo "User Store ID: " . $_SESSION['userid'] . "<br>";
echo "Search In One Store: " . $_SESSION['searchinonestore'] . "<br>";
echo "Store IDs: " . $_SESSION['storeids'] . "<br>";
echo "Default Store: " . $_SESSION['storeid'] . "<br>";

3. Missing Bill Data

Issue: Known bills not appearing in profit analysis

Cause: Date range issues or deleted bill filtering

Fix:

// Check bill status
SELECT sellbillid, sellbilldate, deletedsellid, conditions 
FROM sellbill 
WHERE sellbillid = [MISSING_BILL_ID];

// Verify date filtering
echo "Date From: " . $dateFrom . "<br>";
echo "Date To: " . $dateTo . "<br>";
echo "Query String: " . $queryString . "<br>";

---

๐Ÿงช Testing Scenarios

Test Case 1: Multi-Store Profit Comparison

1. Create sales in different stores using same products
2. Set different cost prices for comparison
3. Run profit analysis for all stores
4. Verify store-wise profit calculations
5. Check grand total accuracy across stores

Test Case 2: Pricing Method Accuracy

1. Set up products with different cost price types
2. Create sales using various pricing methods
3. Run profit analysis with each method (first, last, mean, etc.)
4. Verify cost calculations use correct price source
5. Compare profit margins across methods

Test Case 3: Discount Impact Analysis

1. Create bills with various discount types:
   - Fixed amount discounts
   - Percentage discounts
   - No discounts
2. Run profit analysis
3. Verify discount impact on profit calculations
4. Check that discounts reduce profitability correctly

Test Case 4: Combined Bill Type Analysis

1. Create regular sales, returns, and combined bills
2. Use same products across different bill types
3. Run comprehensive profit analysis
4. Verify returns reduce profits appropriately
5. Check combined bill profit calculations

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur