Return Sell Bill Controller Documentation
File: /controllers/returnSellBillController.php
Purpose: Manages sales return operations, customer refunds, and inventory restoration for returned goods
Last Updated: December 19, 2024
Total Functions: 47
Lines of Code: 2,994
---
๐ Overview
The Return Sell Bill Controller handles all operations related to customers returning sold goods. It manages:
- โข Creating and editing sales return bills (return sell bills)
- โข Processing customer refunds and credit adjustments
- โข Restoring inventory levels after returns
- โข Managing cash register adjustments for refunds
- โข Serial number tracking for returned items
- โข Daily accounting entries for returns
- โข Client debt adjustments for returns
- โข Ajax-based return processing interface
Primary Functions
- โ Create new sales return bills
- โ Edit existing return bills
- โ Delete return bills and reverse effects
- โ View return bill details
- โ Print return bills
- โ Adjust client debts
- โ Restore inventory quantities
- โ Track serial numbers for returns
- โ Generate accounting entries
- โ Ajax data tables for returns list
- โ Handle payment reversals
Related Controllers
- โข sellbillController.php - Original sales operations
- โข clientController.php - Client management
- โข productController.php - Product management
- โข storeController.php - Warehouse/store management
- โข returnBuyBillController.php - Purchase returns
- โข storedetailController.php - Inventory management
- โข dailyentryController.php - Accounting entries
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **returnsellbill** | Main return bills | returnsellbillid, returnsellbillserial, returnsellbilldate, returnsellbilltotalbill, returnsellbillclientid, returnsellbillstoreid | |
| **returnsellbilldetail** | Return bill line items | returnsellbilldetailid, returnsellbillid, returnsellbilldetailproductid, returnsellbilldetailquantity, returnsellbilldetailprice | |
| **returnsellbillcurr** | Multi-currency returns | returnsellbillcurrid, returnsellbillid, returnsellbillcurrencyid, returnsellbillexchangerate |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **storedetail** | Stock quantities by store | Increased on returns | |
| **sizecolorstoredetail** | Stock by size/color variants | For variant products | |
| **storereport** | Stock movement history | Audit trail for returns | |
| **buypriceshistorybook** | Purchase cost tracking | Cost recalculation | |
| **productserial** | Serial number tracking | For serialized returns |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **client** | Client master data | returnsellbill.returnsellbillclientid | |
| **clientdebtchange** | Client debt changes | Credit adjustment tracking | |
| **save** | Cash registers/safes | returnsellbill.returnsellbillsaveid | |
| **savedaily** | Daily cash movements | Financial reconciliation | |
| **dailyentry** | Accounting journal entries | Auto-generated from returns |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **product** | Product master data | Foreign key in details | |
| **productunit** | Units of measurement | returnsellbilldetail.productunitid | |
| **store** | Warehouses/stores | returnsellbill.returnsellbillstoreid | |
| **billname** | Bill templates/types | returnsellbill.billnameid | |
| **billsettings** | Bill configuration | Per bill type settings | |
| **user** | Sales representatives | returnsellbill.userid |
๐ง Key Functions
1. editprint()
Purpose: Main entry point for creating/editing sales return bills
Called By: Form submission with ?do=editprint
Line: 400
Parameters (via $_POST):
- โข
returnsellbillId- ID of existing bill (empty for new) - โข
returnsellbillserial- Serial number - โข
returnsellbillclientid- Client ID - โข
returnsellbillstoreid- Store ID - โข
returnsellbillsaveid- Cash register ID - โข
returnsellbilldate- Return date - โข
returnsellbilltotalbill- Total amount - โข
savePaymentDateWithBillDate- Payment date option
Database Operations:
- โข INSERT/UPDATE
returnsellbill - โข INSERT/UPDATE
returnsellbilldetail - โข UPDATE
client(debt adjustment) - โข UPDATE
storedetail(inventory increase) - โข INSERT
storereport(audit trail) - โข INSERT
savedaily(cash movement) - โข INSERT
clientdebtchange(debt tracking)
Business Logic Flow:
2. showDetail($returnsellbillId)
Purpose: Display detailed view of a specific sales return bill
Parameters: $returnsellbillId - Return bill ID
Line: 742
Returns: Formatted HTML view with:
- โข Return bill header information
- โข Line items with products and quantities
- โข Client information
- โข Total amounts and refund details
- โข Serial numbers if applicable
3. delete($returnsellbillid, $savePaymentDateWithBillDate)
Purpose: Delete a sales return bill and reverse all its effects
Called By: Form with ?do=delete
Line: 1242
Parameters:
- โข
$returnsellbillid- Return bill ID to delete - โข
$savePaymentDateWithBillDate- Payment date handling
Database Operations:
- โข Reverse client debt changes
- โข Reverse inventory adjustments
- โข Reverse cash register changes
- โข Handle serial number restoration
- โข DELETE from
returnsellbilldetail - โข DELETE from
returnsellbill - โข INSERT audit records
4. showallajax()
Purpose: Ajax endpoint for DataTables to display returns list
Called By: Ajax request with ?do=showallajax
Line: 2493
Returns: JSON formatted data for DataTables with:
- โข Return bill information
- โข Client names
- โข Dates and amounts
- โข Action buttons
- โข Pagination support
5. increaseProductQuantity($storedetailId, $productquantityBefore, $productChangeAmount, $sizeColorStoreDetailId, $sizeId, $colorId)
Purpose: Increase inventory when products are returned
Line: 1764
Database Operations:
- โข UPDATE
storedetailSET quantity = quantity + amount - โข UPDATE
sizecolorstoredetailif variants exist - โข INSERT
storereportfor audit trail
6. quickProfitRow($myproduct, $finalQuantity, $isreturn, $isadd, $prototal, $billDiscountVal, $billTotalBeforeDiscount, $storeId)
Purpose: Calculate profit adjustments for returned items
Line: 2117
Parameters:
- โข
$myproduct- Product object - โข
$finalQuantity- Quantity being returned - โข
$isreturn- Whether this is a return operation - โข
$isadd- Whether adding or subtracting - โข
$prototal- Product total amount - โข
$billDiscountVal- Bill discount value - โข
$billTotalBeforeDiscount- Total before discount - โข
$storeId- Store ID
Business Logic: Handles complex profit recalculation based on:
- โข Original purchase costs
- โข Inventory evaluation methods (FIFO, LIFO, Average)
- โข Discount allocations
- โข Multi-unit conversions
7. updateClientDebt($clientid, $clientdebtAfter)
Purpose: Adjust client debt when goods are returned
Line: 1966
Parameters:
- โข
$clientid- Client identifier - โข
$clientdebtAfter- New debt amount (usually increased for credit)
8. insertClientdebtchange($clientid, $clientdebtchangebefore, $clientdebtchangeamount, $clientdebtchangetype, $processname, $clientdebtchangemodelid, $clientdebtchangeafter, $tablename, $comment, $totalOperationCost, $dailyentryid, $visa, $visaAccount, $cash)
Purpose: Record client debt change for audit trail
Line: 1981
Parameters include all financial tracking data for comprehensive audit
9. decreaseProductParcodeSerialQuantity($productId, $sizeId, $colorId, $requiredQuantity, $proSerials, $sellbillId, $sellbillclientId, $sellbilldetailid, $sellbilltype, $operation)
Purpose: Handle serial number tracking for returned products
Line: 2800
Business Logic: Complex serial number management including:
- โข Finding available serial numbers
- โข Updating serial quantities
- โข Tracking serial ownership
- โข Handling barcode products
10. executeOperation()
Purpose: Execute the complete return operation workflow
Called By: Form with ?do=executeOperation
Line: 1593
Workflow: Orchestrates the entire return process including validation, calculations, and database updates
---
๐ Business Logic Flow
Sales Return Workflow
Ajax Data Loading Flow
Files Involved:
- โข View:
/views/default/returnsellbill/returnsellbill_add.html - โข Model:
/models/dto/Returnsellbill.class.php - โข DAO:
/models/mysql/ReturnsellbillMySqlDAO.class.php - โข Ajax:
/views/default/assets/js/returnsellbill.js
---
โ ๏ธ Common Issues
Issue 1: Profit Calculation Complexity
Problem: Complex profit recalculations when returns affect inventory costs
Cause: Multiple inventory evaluation methods and discount allocations
Fix: Proper handling in quickProfitRow() function
Line: 2117
Issue 2: Serial Number Management
Problem: Tracking serial numbers across returns and re-sales
Cause: Complex barcode and serial number relationships
Fix: Comprehensive serial tracking in decreaseProductParcodeSerialQuantity()
Line: 2800
Issue 3: Multi-Currency Returns
Problem: Exchange rate fluctuations affecting return amounts
Cause: Currency conversion complexity
Fix: Proper exchange rate handling and currency table updates
Issue 4: Ajax DataTables Performance
Problem: Large datasets causing slow page loads
Cause: Unoptimized database queries
Fix: Proper indexing and query optimization in showallajax()
---
๐ Dependencies
Includes
include("../public/impOpreation.php");
include_once("../public/config.php");
include_once("dailyentryfun.php");
include("../public/include_dao.php");
Required DAOs
- โข
ReturnsellbillDAO- Return bill operations - โข
ReturnsellbilldetailDAO- Return line items - โข
ClientDAO- Client management - โข
StoredetailDAO- Inventory operations - โข
StorereportDAO- Audit trail - โข
SaveDAO- Cash register operations - โข
ProductDAO- Product information - โข
ProductserialDAO- Serial number tracking
Related Views
- โข
/views/default/returnsellbill/returnsellbill_add.html- Return form - โข
/views/default/returnsellbill/returnsellbill_show.html- Return list - โข
/views/default/returnsellbill/returnsellbill_details.html- Return details
JavaScript Files
- โข
/views/default/assets/js/returnsellbill.js- Return bill functionality - โข
/views/default/assets/js/datatables/- DataTables components
---
๐ฏ URL Routes & Actions
| Action (`?do=`) | Method | Description | View Template | |
|---|---|---|---|---|
| show | GET | Display returns list | returnsellbill_show.html | |
| editprint | POST | Create/edit return bill | returnsellbill_add.html | |
| showDetail | GET | Show return bill details | returnsellbill_details.html | |
| delete | POST | Delete return bill | - | |
| executeOperation | POST | Execute return workflow | - | |
| showallajax | AJAX | DataTables data source | JSON response | |
| delLaterNakdiPayed | POST | Delete later payments | - | |
| editsellBill | GET | Edit related sell bill | sellbill_edit.html | |
| updateReservation | POST | Update reservations | - |
๐ Permissions & Security
Required Permissions
- โข Sales return creation/editing rights
- โข Client management access
- โข Inventory adjustment permissions
- โข Cash register access
Security Checks
include_once("../public/authentication.php");
include_once("initiateStaticSessionCommingWithCurl.php");
CURL Protection
if ($do != "showallajax" && (!isset($_POST['curlpost']) || empty($_POST['curlpost']))) {
include("../public/impOpreation.php");
}
---
๐ Notes
Important Considerations
- โข Return bills must reference original sales bills
- โข Inventory restoration is immediate and permanent
- โข Client debt changes must be properly tracked
- โข Serial numbers require careful handling for resale
- โข Profit calculations are complex with multiple variables
- โข Ajax endpoints require proper authentication
Future Improvements
- โป๏ธ Add return reason codes and analytics
- โป๏ธ Implement return approval workflow
- โป๏ธ Add return quality control integration
- โป๏ธ Implement partial return processing
- โป๏ธ Add return analytics dashboard
- โป๏ธ Optimize Ajax performance for large datasets
---