BuyAndReturnBill Documentation
Buy and Return Bill Controller Documentation
File: /controllers/buyAndReturnBillController.php
Purpose: Manages combined buy and return purchase transactions, handling both buy operations and return operations within a single bill
Last Updated: December 20, 2024
Total Functions: 25+
Lines of Code: ~2,100
---
๐ Overview
The Buy and Return Bill Controller handles complex purchase transactions that include both buying new products and returning previously purchased products within a single bill. This controller manages:
- โข Combined buy and return operations on a single invoice
- โข Product serial number generation and tracking
- โข Store inventory management with quantity increases/decreases
- โข Supplier debt tracking and multi-currency support
- โข Average purchase price calculations
- โข Buy price history maintenance
- โข Cost center and store detail reporting
- โข Safe/cash register operations
Primary Functions
- โ Combined buy and return bill processing
- โ Product serial number generation
- โ Store quantity management (increase/decrease)
- โ Supplier debt tracking in multiple currencies
- โ Average purchase price calculations
- โ Product price history maintenance
- โ Store reporting and audit trails
- โ Safe/cash register transactions
- โ Cost center accounting
- โ Size/color variant management
Related Controllers
- โข buyBillController.php - Pure purchase operations
- โข returnBuyBillController.php - Pure return operations
- โข productController.php - Product management
- โข supplierController.php - Supplier management
- โข storeController.php - Store management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **buyandruternbill** | Combined buy/return bills | buyandruternbillid, supplierid, buybilltotalbill, buybillaftertotalbill, conditions, billnameid | |
| **buyandruternbilldetail** | Combined bill line items | buyandruternbilldetailid, buyandruternbillid, productid, quantity, buyprice, totalprice, operation | |
| **buyandruternbillcurr** | Multi-currency for combined bills | buyandruternbillcurrid, buyandruternbillid, currencyid, buyandruternbillcurrencyprice |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **storedetail** | Main inventory quantities | storedetailid, storeid, productid, productquantity, lastbuyprice | |
| **sizecolorstoredetail** | Size/color variant inventory | sizecolorstoredetailid, storeid, productid, sizeid, colorid, quantity | |
| **storereport** | Inventory change audit log | storereportid, productid, storeid, storereporttype, storereportmodelid, processname |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **supplier** | Supplier master data | supplierid, suppliername, supplierdebt, supplierdebtmaincurr | |
| **supplierdebtchange** | Supplier debt transaction log | supplierdebtchangeid, supplierid, supplierdebtchangeamount, supplierdebtchangetype | |
| **save** | Cash registers/safes | saveid, savename, savevalue | |
| **savedaily** | Daily cash register operations | savedailyid, saveid, savedailychangeamount, savedailychangetype |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **product** | Product master data | productid, productname, productisservice, averageprice | |
| **productserial** | Product serial numbers | productserialid, productid, serialno, billid, tablename | |
| **buypriceshistorybook** | Purchase price history | buypriceshistorybookid, productid, sizeid, colorid, buyprice, buyquantity | |
| **costcenter** | Cost centers | costcenterid, costcentername | |
| **billname** | Bill types/names | billnameid, billname |
๐ Key Functions
1. showBillDetails() - Load Bill Data
Location: Line 723
Purpose: Retrieve complete bill data including details for viewing/editing
Function Signature:
function showBillDetails($buyAndReturnBill_Id)
Process Flow:
1. Load main bill record from buyandruternbill table
2. Load all line item details from buyandruternbilldetail table
3. Join with product data for names and categories
4. Return array containing bill header and details
Return Value:
array(
0 => $buyBillData, // Main bill record
1 => $buyBillDetails // Array of line items with product data
)
---
2. increaseProductQuantity() - Add Inventory
Location: Line 1510
Purpose: Increase product quantity in store when items are purchased
Function Signature:
function increaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount,
$lastBuyPrice, $productnumber, $colName, $detailId, $productId, $tableName, $operation,
$billnameId, $prototal, $rowDiscount, $billDiscountVal, $billTotalBeforeDiscount,
$sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $billPayedTaxPer, $cFactor)
Process Flow:
1. Calculate new quantity after increase
2. Update storedetail or sizecolorstoredetail table
3. Update last buy price for the product
4. Insert audit record in storereport table
5. Update average price calculations
6. Handle price history tracking
Key Variables:
- โข
$productquantityBefore- Current inventory level - โข
$productChangeAmount- Quantity being added - โข
$lastBuyPrice- New purchase price to set - โข
$cFactor- Currency conversion factor
---
3. decreaseProductQuantity() - Remove Inventory
Location: Line 1565
Purpose: Decrease product quantity in store when items are returned
Function Signature:
function decreaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount,
$lastBuyPrice, $productnumber, $colName, $detailId, $productId, $tableName, $operation,
$billnameId, $prototal, $rowDiscount, $billDiscountVal, $billTotalBeforeDiscount,
$sizeColorStoreDetailId, $sizeId, $colorId, $rowtaxval, $billPayedTaxPer, $cFactor)
Process Flow:
1. Calculate new quantity after decrease
2. Update inventory tables
3. Create audit trail
4. Handle negative inventory scenarios
5. Update price histories
---
4. insertSupplierDebtChange() - Track Supplier Debt
Location: Line 1697
Purpose: Log changes to supplier debt balances in multiple currencies
Function Signature:
function insertSupplierDebtChange($supplierId, $supplierDebtChangeBefore,
$supplierDebtChangeBeforeInMainCurr, $supplierDebtChangeAmountInMainCurr,
$supplierDebtChangeAmount, $supplierDebtChangeType, $processname,
$supplierDebtChangeModelId, $supplierDebtChangeAfter,
$supplierDebtChangeAfterInMainCurr, $tablename, $comment)
Process Flow:
1. Create debt change record
2. Set debt change type (0=increase debt, 1=decrease debt)
3. Record amounts in both transaction currency and main currency
4. Link to source transaction (bill ID)
5. Add descriptive comment
Debt Types:
- โข
0- Debt increase (new purchases) - โข
1- Debt decrease (returns or payments)
---
5. getserail() - Generate Serial Numbers
Location: Line 1771
Purpose: Generate unique serial numbers for products that require tracking
Function Signature:
function getserail($length = 6, $productid)
Process Flow:
1. Generate random alphanumeric string
2. Check for uniqueness in productserial table
3. Regenerate if duplicate found
4. Return unique serial number
Serial Format: 6-character alphanumeric (e.g., "A1B2C3")
---
6. lastAndMeanBuyPrice() - Price Calculations
Location: Line 1856
Purpose: Calculate and maintain average purchase prices and price history
Function Signature:
function lastAndMeanBuyPrice($lastBuyPriceOnePiece, $colName, $detailId, $productId,
$sizeId, $colorId, $tableName, $productquantityBefore, $productquantityAfter,
$productChangeAmount, $operation = "add", $billnameId, $prototal, $rowDiscount,
$billDiscountVal, $billTotalBeforeDiscount, $rowtaxval, $billPayedTaxPer, $cFactor)
Process Flow:
1. Calculate effective price after discounts and taxes
2. Determine weighted average price calculation
3. Update product's average price field
4. Insert/update price history record
5. Handle different operations (add/subtract quantities)
Price Calculation:
// Effective price with discounts
$effectivePrice = ($prototal - $rowDiscount + $rowtaxval) / $productChangeAmount * $cFactor;
// Weighted average calculation
$newAveragePrice = (($productquantityBefore * $currentAvgPrice) +
($productChangeAmount * $effectivePrice)) / $productquantityAfter;
---
7. updateOverallAveragePriceOfProducts() - Batch Price Updates
Location: Line 2035
Purpose: Update average prices for all products in a bill
Function Signature:
function updateOverallAveragePriceOfProducts($billData)
Process Flow:
1. Loop through all bill details
2. For each product, calculate new average price
3. Update product table with new average
4. Handle both buy and return operations
5. Consider currency conversions
---
๐ Workflows
Workflow 1: Combined Buy and Return Bill Processing
---
Workflow 2: Serial Number Generation and Tracking
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default display | Show add/edit form | |
| `do=editprint` | Display/Print | Show bill details for printing | |
| `do=details` | `showBillDetails()` | View complete bill information | |
| `do=addserial` | Serial form | Add serial numbers to products | |
| `do=addoneserial` | Single serial | Add one serial number | |
| `do=show` | `showAll()` | List all bills with filters | |
| `do=delete` | `delete()` | Soft delete a bill | |
| `do=saveserail` | `saveserail()` | Save serial number assignments |
View Bill Details (do=details):
- โข
id- Bill ID to display
Add Serial Numbers (do=addserial):
- โข
id- Bill ID - โข
productid- Product requiring serials - โข
quantity- Number of serials needed
Show Bills List (do=show):
- โข
from- Start date (optional) - โข
to- End date (optional) - โข
supplierid- Supplier filter (optional) - โข
del- Include deleted bills (optional)
---
๐งฎ Calculation Methods
Discount Processing
// Fixed amount discount
if ($buybilldiscountrype == 1) {
$discountvalue = $buybilldiscount;
}
// Percentage discount
else {
$discountvalue = ($buybilltotalbill / 100) * $buybilldiscount;
}
Tax Calculation
$taxvalue = $buybillaftertotalbill - ($buybilltotalbill - $discountvalue);
Average Price Calculation
// Weighted average for new purchases
$newAveragePrice = (($currentQty * $currentAvgPrice) + ($newQty * $newPrice)) /
($currentQty + $newQty);
Multi-Currency Conversion
// Convert to main currency
$amountInMainCurrency = $foreignAmount * $currencyFactor;
---
๐ Security & Permissions
User Permission Checks
// Authentication required for most operations
include_once("../public/authentication.php");
Input Sanitization
- โข All
$_GETand$_POSTparameters are filtered - โข Numeric IDs cast to integer
- โข SQL injection prevented by DAO layer parameterized queries
- โข User session validation for all operations
---
๐ Common Issues & Troubleshooting
1. Negative Inventory After Returns
Issue: Store quantity becomes negative after return operations
Cause: Return quantity exceeds available inventory
Debug:
SELECT storeid, productid, productquantity
FROM storedetail
WHERE productquantity < 0;
2. Average Price Calculation Errors
Issue: Product average prices become incorrect
Cause: Currency conversion issues or discount calculation problems
Fix:
// Verify currency factor
$cFactor = ($cFactor > 0) ? $cFactor : 1;
// Ensure positive quantities
$productChangeAmount = abs($productChangeAmount);
3. Serial Number Duplicates
Issue: Duplicate serial numbers generated
Cause: Race condition in serial generation
Solution: Enhanced uniqueness check in getserail() function
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข buyBillController.php - Pure purchase operations
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur