Profitproduct Documentation

Profit Product Controller Documentation

File: /controllers/profitproductController.php

Purpose: Generates detailed product-level profitability analysis and reports

Last Updated: December 20, 2024

Total Functions: 23

Lines of Code: 1,668

---

๐Ÿ“‹ Overview

The Profit Product Controller provides comprehensive product-level profit analysis with multiple buy price evaluation methods. It handles:

Primary Functions

Buy Price Evaluation Methods

1. first - Original purchase price (buyprice)

2. last - Last purchase price (lastbuyprice)

3. mean - Average purchase price (meanbuyprice)

4. last_discount - Last price after purchase discount

5. mean_discount - Average price after purchase discount

6. generalPrice - Overall average price

7. tax - Last price including VAT

8. mean_tax - Average price including VAT

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Sales Tables

Table NamePurposeKey Columns
**sellbilldetail**Sales line itemssellbilldetailproductid, sellbilldetailquantity, sellbilldetailprice, buyprice, lastbuyprice, meanbuyprice, discountvalue
**returnsellbilldetail**Return line itemsreturnsellbilldetailproductid, returnsellbilldetailquantity, returnsellbilldetailprice, buyprice
**sellandruternbilldetail**Combined bill detailssellbilldetailproductid, sellbilldetailquantity, selltype, buyprice, sellbilldiscount
### Product & Inventory Tables

Table NamePurposeKey Columns
**product**Product master dataproductid, productname, productcatid, overallaverageprice, lastbuyprice, meanbuyprice
**productcat**Product categoriesproductcatid, productcatname, productcatparent
**productunit**Units of measureproductunitid, productnumber
### Optical Products Tables

Table NamePurposeKey Columns
**billsproducts**Optical salesproductid, productno, productprice, producttotalprice, netbillvalue, buyprice
**billsreturnproducts**Optical returnsproductid, productno, productprice, producttotalprice, returnedprice
### Configuration Tables

Table NamePurposeKey Columns
**programsettings**System settingsreportsplusHours, vatvalue
**youtubelink**Tutorial linkstitle, url
---

๐Ÿ”‘ Key Functions

1. show() / Default Action - Product Profit Report

Location: Line 175

Purpose: Generate comprehensive profit analysis for a specific product

Function Signature:

// Triggered when: do=show or empty $do
$productSearchId = $_REQUEST['productId'];
$startDate = $_REQUEST['from'];
$endDate = $_REQUEST['to'];
$isOptic = filter_input(INPUT_POST, 'proIsOptic');

Process Flow:

1. Load category data and YouTube tutorials

2. Validate input parameters

3. Handle time zone adjustments

4. Process product vs category selection

5. Call getProductInBillByDateAndProductId() for calculations

6. Display results via show.html or showdiscount.html

Features:

---

2. getProductInBillByDateAndProductId() - Core Profit Calculator

Location: Line 256

Purpose: Calculate detailed profit metrics for a specific product

Function Signature:

function getProductInBillByDateAndProductId($startDate, $endDate, $productId, $overAllAveragePrice)

Process Flow:

1. Calculate sales data from multiple sources

2. Calculate return data from multiple sources

3. Calculate buy/cost data

4. Calculate return buy data

5. Apply VAT calculations

6. Compute final profit

Calculation Structure:

// Sales Revenue
$sellPrice = $sellPriceData[0] + $sellPricePulsData[0] + $sellOpticData[0];
$quantityUnit = $sellPriceData[1] + $sellPricePulsData[1] + $sellOpticData[1];

// Returns
$returnSellPrice = $returnSellPriceData[0] + $returnSellPricePulsData[0] + $returnOpticData[0];

// Final Profit Calculation  
$yy = $sellPrice - $returnSellPrice;
$profitForOneProduct = ($yy) - ($quantityForOneProduct * $xx * $vatValue);

---

3. getTotalSellPriceByDateAndProductId() - Regular Sales Analysis

Location: Line 371

Purpose: Calculate sales revenue and costs for regular sales bills

Function Signature:

function getTotalSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice)

Complex Discount Calculation:

// Product-level discount
$firstStep = $discountValue;

// Bill-level discount calculation
$thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;

// Final price after all discounts
$sellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);

Buy Price Selection Logic:

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

---

4. getTotalAditionalSellPriceByDateAndProductId() - Combined Bills

Location: Line 465

Purpose: Process sales from combined sell/return bills

Special Handling:

// Handle combined bill discount calculation
$sellbillTotalBillnor = $sellbillTotalBillnor + $totalPrice;
$sellbillTotalBillnor = $sellbillTotalBillnor - $returnsellbillprice;

if ($sellbillDiscountType == 2) {
    $sellbillDiscountxx = ($sellbillDiscount / 100) * $sellbillTotalBillnor;
} else {
    $sellbillDiscountxx = ($sellbillDiscount);
}

$sellPrice = $sellPrice - $sellbillDiscountxx;

---

5. getTotalOpticByDateAndProductId() - Optical Products

Location: Line 1527

Purpose: Handle optical product sales with different pricing structure

Process Flow:

1. Query billsproducts table for optical sales

2. Apply simpler discount structure (no unit conversions)

3. Calculate based on direct product pricing

Optical-specific Calculation:

$quantity = $sellbilldetail->productno;  // Direct quantity (no unit conversion)
$price = $sellbilldetail->productprice;
$totalPrice = $sellbilldetail->producttotalprice;
$sellbillTotalBill = $sellbilldetail->netbillvalue;

---

6. getTotalReturnOpticByDateAndProductId() - Optical Returns

Location: Line 1601

Purpose: Process optical product returns

Return Calculation:

$sellbillTotalBill = $returnsellbilldetail->returnedprice;
$thirdStep = ($totalPrice / $sellbillTotalBill);
$ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - $thirdStep;

---

๐Ÿ”„ Workflows

Workflow 1: Product Profit Analysis

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Select Product & Date Range
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Input Validation & Setup
- Validate product ID
- Parse date range
- Apply timezone adjustments
- Determine buy price evaluation method
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Calculate Sales Data
- Regular sales (getTotalSellPriceByDateAndProductId)
- Combined bills (getTotalAditionalSellPrice...)
- Optical sales (getTotalOpticByDateAndProductId)
- Sum all sales revenue and quantities
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Calculate Return Data
- Regular returns (getTotalReturnSellPrice...)
- Combined bill returns (getTotalAditionalReturn...)
- Optical returns (getTotalReturnOpticByDate...)
- Sum all return values and quantities
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Calculate Cost Data
- Regular buy costs (getTotalBuyPriceByDate...)
- Combined bill costs (getTotalAditionalBuyPrice...)
- Apply selected buy price method
- Handle unit conversions
- Apply discount calculations
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Apply VAT & Final Calculations
- Net Sales = Sales - Returns
- Net Quantity = Sold - Returned
- Unit Cost = Total Cost / Total Quantity
- Profit = Net Sales - (Net Quantity ร— Unit Cost ร— VAT)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
6Display Results
- Assign profit and quantity to template
- Include discount breakdown if requested
- Display appropriate view (regular/discount)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

Workflow 2: Discount Calculation Process

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Process Bill Line Item
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Calculate Base Price
$totalPriceBeforeDiscount = $price * $quantity
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Apply Product-Level Discount
$productDiscount = $discountValue
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Calculate Bill-Level Discount Share
if ($sellbillDiscountType == 2) {
$sellbillDiscount = ($sellbillDiscount / 100)
* $sellbillTotalBill
}
* $sellbillDiscount
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Calculate Final Price
$finalPrice = $totalPriceBeforeDiscount
- $productDiscount
- $billDiscountShare
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty) or `do=show`Default actionProduct profit analysis
`dis=T`Discount viewShow discount-focused report
### Required Parameters

Product Analysis (do=show):

---

๐Ÿงฎ Calculation Methods

Multi-Level Discount Calculation

// Step 1: Product-level discount
$productDiscount = $discountValue;

// Step 2: Bill-level discount (proportional)
$billDiscountShare = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;

// Step 3: Final price
$sellPriceForOneProduct = $totalPriceBeforeDiscount - ($productDiscount + $billDiscountShare);

Buy Price Selection with Tax

switch ($buyPriceType) {
    case "tax":
        $buyprice = (float) $sellbilldetail->lastbuyprice_withTax;
        break;
    case "mean_tax":
        $buyprice = (float) $sellbilldetail->meanbuyprice_withTax;
        break;
}

VAT-Adjusted Profit Calculation

$vatValue = 1 + ($Programsetting->vatValue / 100);
$profitForOneProduct = ($netSales) - ($quantityForOneProduct * $unitCost * $vatValue);

Unit Conversion Handling

$productnumber = $myProductunitEx->getProductNumber($productunitId);
$quantityUnit += ($quantity * $productnumber);
$costPerBaseUnit = $buyPrice * $productnumber;

---

๐Ÿ”’ Security & Permissions

Authentication

Input Validation

Potential Issues

---

๐Ÿ“Š Performance Considerations

Database Optimization

1. Critical Indexes:

- sellbilldetail(sellbilldetailproductid, sysdate)

- product(productid)

- billsproducts(productid, sysdate)

- productunit(productunitid)

2. Query Performance Issues:

- Multiple separate queries for each transaction type

- Could benefit from JOIN optimization

- No result caching implemented

3. Memory Usage:

- Loads all matching records simultaneously

- Large product datasets may cause memory issues

- No pagination for results

Optimization Opportunities

-- Current: Multiple queries
SELECT * FROM sellbilldetail WHERE productid = ? AND date BETWEEN ? AND ?
SELECT * FROM returnsellbilldetail WHERE productid = ? AND date BETWEEN ? AND ?
SELECT * FROM sellandruternbilldetail WHERE productid = ? AND date BETWEEN ? AND ?

-- Optimized: Single UNION query possible

---

๐Ÿ› Common Issues & Troubleshooting

1. Incorrect Profit Calculations

Issue: Profit values don't match expected results

Common Causes:

Debug Steps:

// Add debugging output
echo "Buy Price Type: " . $buyPriceType . "\n";
echo "VAT Value: " . $vatValue . "\n";
echo "Unit Number: " . $productnumber . "\n";
echo "Discount Value: " . $discountValue . "\n";

2. Missing Optical Products

Issue: Optical products not included in calculations

Cause: isOptic flag not set correctly

Solution: Ensure proIsOptic parameter is passed correctly

3. Zero Division Errors

Issue: Division by zero in unit cost calculations

Locations: Lines where $quantityUnit is used as divisor

Fix: Add zero checks before division operations

4. Unit Conversion Errors

Issue: Incorrect quantities due to unit mismatches

Solution: Verify productunit table data integrity

---

๐Ÿงช Testing Scenarios

Test Case 1: Basic Product Profit

1. Create product with known buy/sell prices
2. Create sales transaction
3. Run profit report
4. Verify calculations match manual computation

Test Case 2: Complex Discount Scenarios

1. Create sales with both product and bill discounts
2. Test percentage vs fixed amount discounts
3. Verify multi-level discount calculations
4. Check discount report view accuracy

Test Case 3: Multiple Price Methods

1. Create product with different price history
2. Test each buy price evaluation method
3. Verify price selection logic
4. Check VAT calculations

Test Case 4: Optical Products

1. Create optical product transactions
2. Verify different calculation methodology
3. Check integration with regular products
4. Test optical returns processing

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur