Freesales Documentation

Free Sales Controller Documentation

File: /controllers/freesales.php

Purpose: Free sales transactions reporting and analysis system

Last Updated: December 20, 2024

Total Functions: 6+

Lines of Code: ~813

---

๐Ÿ“‹ Overview

The Free Sales Controller manages and reports on sales transactions with zero cost (free sales/giveaways). It provides:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Sales Bill Tables (Regular Sales)

Table NamePurposeKey Columns
**sellbilldetail**Sales bill line itemssellbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, sellbilldetailtotalprice, productunitid, pricetype
**returnsellbilldetail**Sales return detailsreturnsellbilldetailid, returnsellbillid, returnsellbilldetailproductid, returnsellbilldetailquantity, returnsellbilldetailtotalprice
**sellandruternbilldetail**Combined sales/returnsellandruternbilldetailid, sellbillid, sellbilldetailproductid, sellbilldetailquantity, sellbilldetailtotalprice, selltype
### Optical Bill Tables

Table NamePurposeKey Columns
**billsproducts**Optical sales productsbillsproductsid, billid, productid, productno, producttotalprice
**billsreturnproducts**Optical return productsbillsreturnproductsid, billproductid, productid, productno, producttotalprice
### Bill Header Tables

Table NamePurposeKey Columns
**sellbill**Sales billssellbillid, sellbilldate, sellbillclientid, sellbillstoreid
**returnsellbill**Sales returnsreturnsellbillid, returnsellbilldate, returnsellbillclientid
**sellbillandrutern**Combined billssellbillid, sellbilldate, sellbillclientid
**bills**Optical sales billsbillid, billdate, clientid, pricetype, card, paymentnetworkid, cardvalue, netdiscountpercent
**billsreturn**Optical returnsbillproductid, date
### Reference Tables

Table NamePurposeKey Columns
**product**Product master dataproductId, productName, productCatId, productBuyPrice, isOptic
**productcat**Product categoriesproductCatId, productCatName, productCatParent
**productunit**Unit conversionsproductunitid, productid, unitid, productnumber
---

๐Ÿ”‘ Key Functions

1. Default Action - Free Sales Report Interface

Location: Line 134

Purpose: Display search interface and generate comprehensive free sales reports

Process Flow:

1. Load category hierarchy for filtering

2. Process search parameters (dates, categories, products)

3. Build query strings for all bill types (5 different types)

4. Filter for zero-cost transactions across all tables

5. Call getData() to process and consolidate results

Multi-Bill Type Support:

2. getData() - Comprehensive Report Processing

Location: Line 326

Purpose: Process free sales across all bill types and calculate profitability

Function Signature:

function getData($queryString, $queryString1, $queryStringR, $queryString1R, $queryString1SR, $searchtype, $productCatId, $theStore)

Zero-Cost Query Filters:

$queryString .= 'and  billsproducts.producttotalprice = 0 ';
$queryString1 .= 'and  sellbilldetail.sellbilldetailtotalprice = 0 ';
$queryStringR .= 'and  billsreturnproducts.producttotalprice = 0 ';
$queryString1R .= 'and  returnsellbilldetail.returnsellbilldetailtotalprice = 0 ';
$queryString1SR .= 'and  sellandruternbilldetail.sellbilldetailtotalprice = 0 ';

Bill Type Processing:

Optical Sales (bills):

Regular Sales (sellbill):

Combined Bills (sellbillandrutern):

3. Network Payment Discount Calculation

Purpose: Handle optical shop payment method discounts

if ($theBill->card == 1) {
    if ($theBill->paymentnetworkid == 4) {
        // MADA payment processing
        $madaData = $billsEX->queryTotalNetworkReportMadaSimple($theBill->billdate);
        if ($madaData->totalCarry < 5000)
            $dicount = (7 * $madaData->totalCarry) / 1000;
        else
            $dicount = 40;
    } else {
        // Other card networks
        $dicount = ($theBill->cardvalue * $theBill->netdiscountpercent) / 100;
    }
}

4. getDetails() - Multi-Source Transaction Details

Location: Line 640

Purpose: Provide detailed transaction drill-down across all bill types

Process Flow:

1. Query each bill type separately

2. Format results with appropriate bill URLs

3. Handle unit conversions where applicable

4. Consolidate into single result set

Bill URL Generation:

// Optical bills
$myproduct->billURL = "bills.php?do=details&id=" . $value->id;

// Sales bills  
$myproduct->billURL = "sellbillController.php?do=showDetail&sellbillid=" . $value->sellbillid;

// Return bills
$myproduct->billURL = "returnsellbillController.php?do=showDetail&returnsellbillid=" . $value->returnsellbillid;

// Combined bills
$myproduct->billURL = "sellbillandruternController.php?do=showDetail&sellbillid=" . $value->sellbillid;

---

๐Ÿ”„ Workflows

Workflow 1: Comprehensive Free Sales Analysis

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
START: Multi-Source Analysis
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
1Query All Bill Types
- Optical sales (bills/billsproducts)
- Optical returns (billsreturn/billsreturnproducts)
- Regular sales (sellbill/sellbilldetail)
- Sales returns (returnsellbill/returnsellbilldetail)
- Combined bills (sellbillandrutern/detail)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
2Filter Zero-Cost Transactions
- Apply producttotalprice = 0 filters
- Apply sellbilldetailtotalprice = 0 filters
- Include date and category filters
- Handle pricetype filtering for sales
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
3Process Each Transaction Type
FOR EACH bill type:
โ”‚
โ†’ OPTICAL: Process without unit conversion
โ”‚ โ”œโ”€ Calculate network payment discounts
โ”‚ โ”œโ”€ Handle MADA special rates
โ”‚ โ”‚ โ””โ”€ Apply card payment discounts โ”‚
โ”‚
โ†’ REGULAR: Convert units to standard quantities
โ”‚ โ”œโ”€ Apply bill-level discount allocation
โ”‚ โ”œโ”€ Subtract line-level discounts
โ”‚ โ”‚ โ””โ”€ Handle pricetype filtering โ”‚
โ”‚
โ”‚ โ””โ”€โ†’ COMBINED: Process by transaction type โ”‚
โ”œโ”€ Handle sale vs return differentiation
โ”‚ โ””โ”€ Apply unified discount calculations โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
4Consolidate and Analyze
- Aggregate by product across all sources
- Calculate net quantities (sold - returned)
- Determine real costs from product master
- Calculate profit/loss on free items
- Get current inventory levels
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
5Generate Comprehensive Report
- Display consolidated product analysis
- Show multi-source transaction summary
- Provide drill-down to source transactions
- Calculate category and overall totals
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

---

๐ŸŒ URL Routes & Actions

URL ParameterFunction CalledDescription
`do=` (empty)Default actionFree sales comprehensive report
`do=detail``getDetails()`Multi-source transaction details
### Required Parameters

Summary Report:

Detail Report (do=detail):

---

๐Ÿงฎ Calculation Methods

Optical Shop Discount Calculation

// MADA payment special rates
if ($theBill->paymentnetworkid == 4) {
    $madaData = $billsEX->queryTotalNetworkReportMadaSimple($theBill->billdate);
    if ($madaData->totalCarry < 5000)
        $dicount = (7 * $madaData->totalCarry) / 1000;
    else
        $dicount = 40;
} else {
    // Standard card discount
    $dicount = ($theBill->cardvalue * $theBill->netdiscountpercent) / 100;
}

Regular Sales Unit Conversion

$quantity = $value->sellbilldetailquantity;
$productunitId = $value->productunitid;
$productunitData = loadProductUnitWithProductAndUnit($productId, $productunitId);
$productnumber = $productunitData->productnumber;
$finalquantity = $quantity * $productnumber;

Bill-Level Discount Allocation

$dicount = $value->parcode - $value->note;  // Bill discount
if ($dicount != 0) {
    $billpecies = $sellbilldetailEX->queryBillNoOfPecies($value->sellbillid);
    $billNoOfProduct = $billpecies->note;
    $theDiscount = ($finalquantity * $dicount) / $billNoOfProduct;
    $theDiscount -= $value->discountvalue;  // Subtract line discount
}

---

๐Ÿ”’ Security & Permissions

Authentication Required

Multi-Source Data Access

---

๐Ÿ“Š Performance Considerations

Query Optimization

1. Multi-Source Efficiency: Parallel querying of different bill types

2. Zero-Cost Early Filtering: Applied before joins

3. Product Aggregation: Efficient array indexing across sources

4. Unit Conversion Caching: Minimizes repeated calculations

Memory Management

---

๐Ÿ› Common Issues & Troubleshooting

1. Missing Optical Transactions

Issue: Optical shop free items not appearing

Cause: Different table structure or field naming

Debug:

-- Check optical sales structure
SELECT bp.productid, bp.productno, bp.producttotalprice
FROM billsproducts bp
JOIN bills b ON b.billid = bp.billid
WHERE bp.producttotalprice = 0
AND DATE(b.billdate) = CURDATE();

2. Unit Conversion Errors

Issue: Regular sales quantities incorrect

Cause: Missing or wrong productunit records

Fix:

-- Verify unit conversion data
SELECT p.productName, pu.productnumber, u.unitName
FROM product p
LEFT JOIN productunit pu ON pu.productid = p.productId
LEFT JOIN unit u ON u.unitId = pu.unitid
WHERE p.productId = [PRODUCT_ID];

3. MADA Discount Calculation Issues

Issue: Incorrect optical discount calculations

Cause: MADA daily total calculation errors

Debug: Check queryTotalNetworkReportMadaSimple() method

4. Combined Bill Type Confusion

Issue: Combined bills showing wrong sale/return split

Cause: selltype field interpretation

Fix: Verify selltype values (0=sale, 1=return) in database

---

๐Ÿงช Testing Scenarios

Test Case 1: Multi-Source Free Sales Report

1. Set date range covering known free sales
2. Run report without filters  
3. Verify items from all bill types appear
4. Check optical and regular sales integration
5. Confirm quantity and discount calculations

Test Case 2: Optical vs Regular Sales

1. Test with optical products (isOptic=2)
2. Test with regular products
3. Verify different calculation methods
4. Check discount handling differences
5. Confirm proper categorization

Test Case 3: Payment Method Discounts

1. Test optical sales with card payments
2. Verify MADA special rate calculations
3. Check standard card discount calculations
4. Test cash sales (no discounts)

Test Case 4: Combined Bill Processing

1. Create combined sales/return bill with free items
2. Verify correct sale/return split
3. Check quantity calculations
4. Test drill-down functionality

---

๐Ÿ“š Related Documentation

---

Documented By: AI Assistant

Review Status: โœ… Complete

Next Review: When major changes occur