๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

Balance Report Controller Documentation

File: /controllers/balancereportController.php

Purpose: Generates comprehensive balance sheet reports and financial position analysis

Last Updated: December 19, 2024

Total Functions: 22

Lines of Code: 818

---

๐Ÿ“‹ Overview

The Balance Report Controller provides comprehensive financial reporting and balance sheet analysis for the ERP system. It handles:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
`sellbill`Sales revenuesellbillid, sellbillaftertotalbill, sellbilldate, conditions
`returnsellbill`Sales returnsreturnsellbillid, returnsellbillaftertotalbill, returnsellbilldate
`sellbillandrutern`Combined sales/returnsid, sellaftertotalbill, returnaftertotalbill, sellbilldate
`expenses`Business expensesexpensesid, expensesamount, expensesdate
### Asset & Financial Tables

Table NamePurposeKey Columns
`assets`Fixed assetsassetsid, assetvalue, assetsdate, assetscatid
`storedetail`Inventory valuationstoredetailid, productid, productquantity, storeid
`product`Product pricingproductId, productBuyPrice, lastbuyprice, meanbuyprice
`save`Cash/petty cashsaveid, saveamount, savedate
`bankaccount`Bank balancesbankaccountid, bankaccountbalance
### Debt & Capital Tables

Table NamePurposeKey Columns
`client`Customer debtsclientid, clientname, clientdebt
`supplier`Supplier debtssupplierid, suppliername, supplierdebt
`capital`Company capitalcapitalid, capitalamount, capitaldate
`partner`Partner withdrawalspartnerid, partnermoney, conditions
`kempiala`Bills of exchangekempialaId, kempialaValue, kempialaisselling
### Detail & Check Tables

Table NamePurposeKey Columns
`sellbilldetail`Sales line itemssellbilldetailid, sellbilldetailquantity, buyprice
`returnsellbilldetail`Return line itemsreturnsellbilldetailid, returnsellbilldetailquantity
`sellandruternbilldetail`Combined bill detailsid, sellbilldetailquantity, sellbilldetailtype
`datedchecked`Check trackingid, checkValue, checktype, checkdate
---

๐Ÿ”ง Key Functions

Core Balance Sheet Functions

Main Balance Sheet Display (Lines 242-368)

Purpose: Generates comprehensive balance sheet with assets, liabilities, and equity

Parameters:

Process Flow:

โ”Œโ”€ Validate Date Range โ”€โ”
Check from/to dates
Sales - Returns
COGS from purchases
Inventory + Fixed
Client/Supplier
Balance sheet format
โ””โ”€ Display Results โ”€โ”€โ”€โ”€โ”€โ”˜

Key Calculations:

$totalSellCost = $totalSellPrice - $totalReturnSellPrice;
$totalBuyCost = $totalQuantityBuyPrice - $totalQuantityReturnBuyPrice;
$profitFinal = ($totalSellCost - $totalBuyCost) - $totalExpenses;
$forYou = $assetValue + $productPrice + $saveValue + $bankAccountbalance + $notSellingKempialaValue + $clientDebt + $totalclientcheck;

Revenue Calculation Functions

getTotalSellbillByDate($startDate, $endDate) (Lines 378-388)

Purpose: Calculates total sales revenue for date range

Parameters: Start and end dates

Returns: Total sales amount excluding taxes and discounts

getTotalAditionalSellbillByDate($startDate, $endDate) (Lines 391-401)

Purpose: Gets additional sales from combined sell/return bills

Parameters: Start and end dates

Returns: Additional sales revenue

getTotalReturnSellbillByDate($startDate, $endDate) (Lines 404-414)

Purpose: Calculates total sales returns for date range

Parameters: Start and end dates

Returns: Total return amount

getTotalAditionalReturnSellbillByDate($startDate, $endDate) (Lines 417-427)

Purpose: Gets additional returns from combined bills

Parameters: Start and end dates

Returns: Additional return amount

Cost of Goods Sold Functions

getTotalQuantityBuyPriceByDate($startDate, $endDate) (Lines 430-466)

Purpose: Calculates total cost of goods sold for sales

Parameters: Start and end dates

Process Flow:

โ”Œโ”€ Get Sold Items โ”€โ”€โ”€โ”€โ”
From sellbilldetail
Get product number
Handle % and fixed
Quantity ร— Cost
โ””โ”€ Return COGS โ”€โ”€โ”€โ”€โ”€โ”˜

Discount Calculation Logic:

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

getTotalQuantityReturnBuyPriceByDate($startDate, $endDate) (Lines 513-555)

Purpose: Calculates cost of returned goods

Parameters: Start and end dates

Returns: Total cost of returned merchandise

Asset Valuation Functions

getTotalAssetByDate($startDate, $endDate) (Lines 622-638)

Purpose: Calculates total fixed asset value by date range

Parameters: Start and end dates

Process: Groups assets by category and sums values

Returns: Total fixed asset value

getTotalProductPrice() (Lines 640-702)

Purpose: Calculates inventory value using configured valuation method

Inventory Valuation Methods:

switch ($Programsettingdata->Inventoryevaluation) {
    case "first": $pro_price = (float) $storedetail->productBuyPrice; break;
    case "last": $pro_price = (float) $storedetail->lastbuyprice; break;
    case "mean": $pro_price = (float) $storedetail->meanbuyprice; break;
    case "last_discount": $pro_price = (float) $storedetail->lastbuyprice_withDiscount; break;
    case "mean_discount": $pro_price = (float) $storedetail->meanbuyprice_withDiscount; break;
    case "generalPrice": $pro_price = (float) $storedetail->overAllAveragePrice; break;
    case "tax": $pro_price = (float) $storedetail->lastbuyprice_withTax; break;
    case "mean_tax": $pro_price = (float) $storedetail->meanbuyprice_withTax; break;
}

Size/Color Handling:

if($storedetail->hasSizeAndColor == 1) {
    $scquantity = R::getCell('select sum(quantity) from sizecolorstoredetail where productid = '.$storedetail->productId);
    $productQuantity = $scquantity;
} else {
    $productQuantity = $storedetail->productquantity;
}

Cash & Bank Functions

getTotalSaveValue() (Lines 704-712)

Purpose: Gets total petty cash/save value

Returns: Current total in all save accounts

getBankAccountbalance() (Lines 714-723)

Purpose: Calculates total bank account balances

Returns: Sum of all bank account balances

Debt Analysis Functions

getClientDebt() (Lines 799-807)

Purpose: Calculates total customer debt

Returns: Total amount owed by all clients

getSupplierDebt() (Lines 767-775)

Purpose: Calculates total supplier debt

Returns: Total amount owed to all suppliers

clientcheckvalue($startDate, $endDate) (Lines 736-749)

Purpose: Calculates value of checks from clients

Parameters: Date range for check calculation

Returns: Total value of client checks

suppliercheckvalue($startDate, $endDate) (Lines 752-765)

Purpose: Calculates value of checks to suppliers

Parameters: Date range for check calculation

Returns: Total value of supplier checks

Capital & Investment Functions

getCapitalamount() (Lines 777-785)

Purpose: Gets total company capital investment

Returns: Total capital amount

getPartnerwithdrawalvalue($startDate, $endDate) (Lines 787-797)

Purpose: Calculates partner withdrawal amounts

Parameters: Date range (currently not used in query)

Returns: Total partner money withdrawals

Bill of Exchange Functions

getNotSellingKempialaValue() (Lines 725-733)

Purpose: Gets value of bills of exchange not yet sold

Returns: Total value of unsold Kempiala (bills of exchange)

getSellingKempialaValue() (Lines 809-818)

Purpose: Gets value of bills of exchange that have been sold

Returns: Total value of sold Kempiala

Expense Tracking

getTotalExpensesByDate($startDate, $endDate) (Lines 611-620)

Purpose: Calculates total expenses for date range

Parameters: Start and end dates

Returns: Total expense amount

---

๐Ÿ”„ Business Logic Flow

Balance Sheet Generation Process

User Request โ†’ Date Validation โ†’ Data Collection
     โ†“
Revenue Calculation โ†’ Cost Calculation โ†’ Expense Calculation
     โ†“
Asset Valuation โ†’ Debt Analysis โ†’ Capital Tracking
     โ†“
Balance Sheet Assembly โ†’ Profit/Loss Calculation โ†’ Display

Financial Position Analysis

โ”œโ”€ Inventory
โ”‚ โ”œโ”€ Cash (Saves) โ”‚ โ””โ”€ Supplier Checks
โ”‚ โ”œโ”€ Bank Accounts โ””โ”€ Equity
โ”œโ”€ Client Debt
โ”œโ”€ Client Checks
โ”‚ โ””โ”€ Unsold Kempiala โ”‚ โ””โ”€ Partner Withdrawals
โ””โ”€ Fixed Assets โ””โ”€ Sold Kempiala

Inventory Valuation Logic

Get Product โ†’ Check Size/Color โ†’ Apply Valuation Method โ†’ Calculate Total
    โ†“
FIFO/LIFO โ†’ Last Price โ†’ Mean Price โ†’ With/Without Discount โ†’ With/Without Tax

---

โš ๏ธ Common Issues

Data Accuracy Issues

1. Date range dependency: All calculations depend on accurate date filtering

2. Inventory valuation: Different methods can show significant value differences

3. Currency precision: Financial calculations need proper rounding

Performance Considerations

1. Large date ranges: Extensive periods may cause slow report generation

2. Complex queries: Multiple table joins for cost calculations

3. Real-time calculations: No caching of frequently accessed totals

Business Logic Warnings

1. Incomplete transactions: Pending/draft bills may not be included

2. Multi-currency: No apparent support for foreign currency conversion

3. Tax calculations: Complex tax scenarios may need manual verification

---

๐Ÿ”— Dependencies

Required Files

Database Dependencies

External Dependencies

Configuration Dependencies

---

Financial Formula Notes:

โ†‘