๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Quick Profit Calculator Controller Documentation

File: /controllers/calcquickprofit.php

Purpose: Advanced profit calculation system with real-time analytics and multi-dimensional profit tracking

Last Updated: December 20, 2024

Total Functions: 4

Lines of Code: ~600+

---

๐Ÿ“‹ Overview

The Quick Profit Calculator Controller is a sophisticated financial analytics module that provides real-time profit calculations across multiple dimensions. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Quick Profit Tracking Tables

Table NamePurposeKey Columns
**quickprofitclient**Client-based profit trackingquickprofitclientid, clientid, profit, date
**quickprofitday**Daily profit aggregationquickprofitdayid, date, profit, store
**quickprofitgeneral**Overall profit summaryquickprofitgeneralid, totalprofit, date
**quickprofitproduct**Product-specific profitsquickprofitproductid, productid, profit, quantity
**quickprofitstore**Store-based profit trackingquickprofitsfamilyid, storeid, profit, date
**lastbillidsquickprofit**Tracking last processed billsid, lastSellBillId, lastReturnSellBillId, lastSellAndReturnBillId
### Source Data Tables

Table NamePurposeKey Columns
**sellbill**Sales transactionssellbillid, sellbillclientid, sellbillaftertotalbill, sellbilldiscount
**sellbilldetail**Sales line itemssellbilldetailid, sellbilldetailproductid, sellbilldetailquantity, sellbilldetailtotalprice
**returnsellbill**Return transactionsreturnsellbillid, returnsellbillclientid, returnsellbillaftertotalbill
**returnsellbilldetail**Return line itemsreturnsellbilldetailid, returnsellbilldetailproductid, returnsellbilldetailquantity
**sellbillandrutern**Combined transactionssellbillid, sellbillclientid, sellbillprice, returnsellbillprice
### Cost Tracking Tables

Table NamePurposeKey Columns
**buybilldetail**Purchase costsbuybilldetailid, buybilldetailproductid, buybilldetailprice
**storedetail**Current inventory costsstoredetailid, productid, storeid, productlastbuyprice
**product**Product master dataproductid, productname, productCatId
**productunit**Unit conversionsproductunitid, productid, productnumber
---

๐Ÿ”‘ Key Functions

1. calcProfits() - Main Profit Calculation Engine

Purpose: Calculate profits for specific bill types and update profit tracking tables

Function Signature:

function calcProfits($billType, $bill, $isadd)

Parameters:

Process Flow:

1. Bill Type Identification: Determine bill structure based on type

2. Detail Processing: Process each line item for profit calculation

3. Cost Basis Calculation: Determine cost of goods sold

4. Profit Calculation: Calculate profit per item and total

5. Multi-Dimensional Updates: Update profit tracking across all dimensions

Bill Type Processing Logic:

switch ($billType) {
    case 1: // Sales bills
        foreach ($bill->details as $detail) {
            $profit = calculateRowProfit($detail);
            updateProfitTables($profit, $detail);
        }
        break;
        
    case 2: // Return bills  
        foreach ($bill->details as $detail) {
            $profit = calculateRowProfit($detail) * -1; // Negative profit
            updateProfitTables($profit, $detail);
        }
        break;
        
    case 3: // Combined bills
        // Process both sale and return portions
        processCombinedBillProfit($bill);
        break;
}

---

2. quickProfitRow() - Row-Level Profit Calculation

Purpose: Calculate profit for individual transaction line items with detailed cost analysis

Function Signature:

function quickProfitRow($myproduct, $finalQuantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId, $billDate)

Parameters:

Cost Calculation Logic:

// Get latest purchase cost
$latestCost = getLatestPurchaseCost($myproduct->productid, $storeId);

// Calculate per-unit cost
$unitCost = $latestCost / $conversionFactor;

// Calculate total cost for quantity
$totalCost = $unitCost * $finalQuantity;

// Apply discount allocation
$discountAllocation = ($prototal / $billTotalBeforeDiscount) * $billDiscountVal;
$adjustedRevenue = $prototal - $discountAllocation;

// Calculate profit
$rowProfit = $adjustedRevenue - $totalCost;

---

3. quickProfitBill() - Bill-Level Profit Aggregation

Purpose: Calculate total bill profit with comprehensive discount and tax considerations

Function Signature:

function quickProfitBill($sellbillPrice, $returnsellbillPrice, $billDiscountVal, $clientId, $isadd, $billDate)

Parameters:

Bill Profit Calculation:

// Calculate net bill amount
$netBillAmount = $sellbillPrice - $returnsellbillPrice - $billDiscountVal;

// Get total cost from line items (already calculated)
$totalCost = sumLineItemCosts($billId);

// Calculate bill profit
$billProfit = $netBillAmount - $totalCost;

// Update multi-dimensional tracking
if ($isadd) {
    updateClientProfit($clientId, $billProfit, $billDate);
    updateDailyProfit($billDate, $billProfit);
    updateGeneralProfit($billProfit);
} else {
    // Reverse profit calculations
    updateClientProfit($clientId, -$billProfit, $billDate);
    updateDailyProfit($billDate, -$billProfit);
    updateGeneralProfit(-$billProfit);
}

---

4. loadProductUnitWithProductAndUnit() - Product Unit Resolution

Purpose: Load product unit conversion data for accurate quantity and cost calculations

Function Signature:

function loadProductUnitWithProductAndUnit($productId, $unitId)

Unit Conversion Logic:

// Load product unit relationship
$productUnit = $productUnitDAO->loadByProductAndUnit($productId, $unitId);

if ($productUnit) {
    return [
        'productunitid' => $productUnit->productunitid,
        'productnumber' => $productUnit->productnumber, // Conversion factor
        'unitname' => $productUnit->unitname,
        'basecost' => $productUnit->basecost
    ];
}

// Return default unit if no specific unit found
return getDefaultProductUnit($productId);

---

๐Ÿ”„ Workflows

Workflow 1: Real-Time Profit Calculation Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Sales Transaction
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Transaction Processing Trigger
- Sales bill completion
- Return bill processing
- Combined bill finalization
- Bill modification/deletion
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Bill Analysis and Preparation
- Determine bill type and structure
- Load all line item details
- Extract discount and tax information
- Identify client and store context
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Row-Level Profit Calculations
FOR EACH line item:
โ”‚
โ†’ Load product and unit information
โ”‚
โ†’ Calculate final quantities
โ”‚ โ”œโ”€ Apply unit conversions
โ”‚ โ”œโ”€ Handle size/color variants
โ”‚ โ”‚ โ””โ”€ Adjust for return quantities โ”‚
โ”‚
โ†’ Determine cost basis
โ”‚ โ”œโ”€ Get latest purchase cost
โ”‚ โ”œโ”€ Apply FIFO/LIFO/Average costing
โ”‚ โ”‚ โ””โ”€ Handle inventory adjustments โ”‚
โ”‚
โ†’ Calculate allocated revenue
โ”‚ โ”œโ”€ Apply proportional discounts
โ”‚ โ”œโ”€ Handle line-level discounts
โ”‚ โ”‚ โ””โ”€ Account for tax implications โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ Compute row profit โ”‚
โ”œโ”€ Revenue - Cost = Gross Profit
โ”œโ”€ Apply margin calculations
โ”‚ โ””โ”€ Store in quickprofitproduct table โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Bill-Level Profit Aggregation
- Sum all row profits for bill total
- Apply bill-level adjustments
- Account for freight and handling
- Calculate net bill profit
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Multi-Dimensional Profit Updates
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ Client Dimension (quickprofitclient):
โ”‚ - Update client-specific profit totals
โ”‚ - Track customer profitability trends
โ”‚ - Maintain profit history by client
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ Product Dimension (quickprofitproduct):
โ”‚ - Update product profitability metrics
โ”‚ - Track product performance trends
โ”‚ - Maintain product margin analysis
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ Store Dimension (quickprofitstore):
โ”‚ - Update store-level profit totals
โ”‚ - Track location performance
โ”‚ - Maintain store comparison data
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ Time Dimension (quickprofitday):
โ”‚ - Update daily profit aggregations
โ”‚ - Maintain profit trend analysis
โ”‚ - Support period-over-period comparisons
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ General Totals (quickprofitgeneral):
โ”‚ - Update overall system profitability
โ”‚ - Maintain master profit totals
โ”‚ - Support executive reporting
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Tracking and Audit
- Update lastbillidsquickprofit tracking
- Create audit trail entries
- Update performance metrics
- Validate calculation accuracy
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐Ÿงฎ Calculation Methods

Cost Basis Determination

// FIFO (First In, First Out) Method
function getFIFOCost($productId, $quantity, $storeId) {
    $purchases = getPurchaseHistory($productId, $storeId, 'ASC');
    $remainingQuantity = $quantity;
    $totalCost = 0;
    
    foreach ($purchases as $purchase) {
        if ($remainingQuantity <= 0) break;
        
        $useQuantity = min($remainingQuantity, $purchase->availableQuantity);
        $totalCost += $useQuantity * $purchase->unitCost;
        $remainingQuantity -= $useQuantity;
    }
    
    return $totalCost;
}

Discount Allocation Algorithm

// Proportional discount allocation across line items
function allocateDiscount($lineTotal, $billTotal, $billDiscount) {
    if ($billTotal == 0) return 0;
    
    $allocationPercentage = $lineTotal / $billTotal;
    $allocatedDiscount = $billDiscount * $allocationPercentage;
    
    return $allocatedDiscount;
}

Profit Margin Calculations

// Gross profit margin calculation
function calculateGrossMargin($revenue, $cost) {
    if ($revenue == 0) return 0;
    
    $grossProfit = $revenue - $cost;
    $grossMarginPercentage = ($grossProfit / $revenue) * 100;
    
    return [
        'gross_profit' => $grossProfit,
        'gross_margin_percentage' => $grossMarginPercentage,
        'markup_percentage' => ($cost > 0) ? (($revenue - $cost) / $cost) * 100 : 0
    ];
}

Multi-Currency Profit Calculations

// Handle multi-currency profit calculations
function calculateMultiCurrencyProfit($revenue, $cost, $exchangeRate) {
    // Convert to base currency
    $baseCurrencyRevenue = $revenue * $exchangeRate;
    $baseCurrencyCost = $cost * $exchangeRate;
    
    return [
        'profit_local' => $revenue - $cost,
        'profit_base' => $baseCurrencyRevenue - $baseCurrencyCost,
        'exchange_rate' => $exchangeRate
    ];
}

---

๐Ÿ”’ Security & Permissions

Data Access Control

// User permission validation for profit access
function validateProfitAccess($userId) {
    global $UsergroupEX, $userEX;
    
    $userData = $userEX->load($userId);
    $userGroup = $UsergroupEX->load($userData->usergroupid);
    
    if ($userGroup->viewProfits != 1) {
        throw new Exception('Insufficient permissions to view profit data');
    }
    
    return true;
}

Audit Trail Requirements

// Log profit calculation changes
function logProfitChange($type, $billId, $oldProfit, $newProfit, $userId) {
    $logEntry = [
        'timestamp' => date('Y-m-d H:i:s'),
        'user_id' => $userId,
        'operation_type' => $type,
        'bill_id' => $billId,
        'profit_before' => $oldProfit,
        'profit_after' => $newProfit,
        'change_amount' => $newProfit - $oldProfit
    ];
    
    insertProfitAuditLog($logEntry);
}

---

๐Ÿ“Š Performance Considerations

Calculation Optimization

1. Batch Processing: Process multiple transactions together

2. Caching: Cache frequently accessed cost data

3. Indexing: Optimize database indexes for profit queries

// Optimized batch profit calculation
function batchCalculateProfit($billIds) {
    // Preload all required data
    $bills = preloadBillData($billIds);
    $products = preloadProductData($bills);
    $costs = preloadCostData($products);
    
    foreach ($bills as $bill) {
        // Use preloaded data for calculations
        $profit = calculateProfitWithCachedData($bill, $products, $costs);
        updateProfitTables($profit);
    }
}

Memory Management

// Memory-efficient processing for large datasets
function processLargeProfitCalculation($dateFrom, $dateTo) {
    $batchSize = 1000;
    $offset = 0;
    
    do {
        $bills = loadBillsBatch($dateFrom, $dateTo, $offset, $batchSize);
        processProfitBatch($bills);
        
        // Clear memory
        unset($bills);
        gc_collect_cycles();
        
        $offset += $batchSize;
    } while (count($bills) == $batchSize);
}

---

๐Ÿ› Common Issues & Troubleshooting

1. Negative Profit Calculations

Issue: Products showing negative profits unexpectedly

Cause: Incorrect cost basis or missing purchase data

Debug:

function debugNegativeProfit($productId, $billId) {
    echo "Product: $productId, Bill: $billId<br>";
    
    $latestCost = getLatestPurchaseCost($productId);
    echo "Latest cost: $latestCost<br>";
    
    $sellPrice = getSellPrice($productId, $billId);
    echo "Sell price: $sellPrice<br>";
    
    $calculatedProfit = $sellPrice - $latestCost;
    echo "Calculated profit: $calculatedProfit<br>";
}

2. Profit Totals Don't Match

Issue: Sum of row profits doesn't equal bill profit

Cause: Discount allocation or rounding errors

Validation:

function validateProfitConsistency($billId) {
    $rowProfitSum = sumRowProfits($billId);
    $billProfit = getBillProfit($billId);
    $difference = abs($rowProfitSum - $billProfit);
    
    if ($difference > 0.01) { // Allow for minor rounding
        error_log("Profit inconsistency for bill $billId: Row sum: $rowProfitSum, Bill: $billProfit");
        return false;
    }
    
    return true;
}

3. Performance Degradation

Issue: Slow profit calculations for large transactions

Cause: Inefficient cost lookups or missing indexes

Optimization:

// Optimize cost lookups
function optimizedCostLookup($productIds, $storeId) {
    // Single query instead of multiple lookups
    $costs = queryBatch("
        SELECT productid, productlastbuyprice 
        FROM storedetail 
        WHERE productid IN (" . implode(',', $productIds) . ") 
        AND storeid = $storeId
    ");
    
    return array_column($costs, 'productlastbuyprice', 'productid');
}

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Profit Calculation

1. Create sale with known cost and price
2. Trigger profit calculation
3. Verify profit = price - cost
4. Check all dimension tables updated
5. Validate profit totals match

Test Case 2: Discount Allocation

1. Create bill with multiple items and discount
2. Calculate individual item profit allocation
3. Verify discount properly distributed
4. Check total profit accuracy
5. Validate margin calculations

Test Case 3: Return Transaction Handling

1. Process original sale
2. Process return for same items
3. Verify profit reversal
4. Check dimension table adjustments
5. Validate net profit calculations

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When profit calculation methods change

โ†‘