Save Documentation
Save Controller (Cash Management) Documentation
File: /controllers/saveController.php
Purpose: Manages cash registers (safes) and cash flow operations
Last Updated: December 19, 2024
Total Functions: 9
Lines of Code: 628
---
๐ Overview
The Save Controller manages cash registers (safes/vaults) in the ERP system. It handles:
- โข Cash register creation and management
- โข Multi-currency cash register support
- โข Cash flow tracking and reconciliation
- โข Opening balance management for cash registers
- โข Cash register categorization by type
- โข Automatic chart of accounts integration
- โข Cash movement logging (savedaily)
- โข Currency conversion handling
- โข Cash register sorting and organization
- โข Integration with daily journal entries
Primary Functions
- โ Create and configure cash registers
- โ Manage multiple currencies per cash register
- โ Track cash movements and balances
- โ Handle opening balances with journal entries
- โ Categorize cash registers by type
- โ Sort and organize cash registers
- โ Generate chart of accounts entries
- โ Log all cash movements for audit
- โ Support CURL API operations
- โ Multi-language support
Related Controllers
- โข dailyentry.php - Auto journal entries for cash operations
- โข expensesController.php - Cash expense payments
- โข sellbillController.php - Cash sales receipts
- โข buyBillController.php - Cash purchase payments
- โข bankController.php - Bank account integration
- โข accountstree.php - Chart of accounts integration
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **save** | Main cash registers | saveid, savename, savecurrentvalue, currencyId, savetypeid, treeId, conditions, sortby | |
| **savedaily** | Cash movement history | savedailydate, saveid, savedailychangeamount, savedailychangetype, processname, userid | |
| **savetype** | Cash register categories | id, name, del |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **currency** | Multi-currency support | Used by save.currencyId | |
| **accountstree** | Chart of accounts | Auto-created for each cash register | |
| **dailyentry** | Journal entries | Auto-generated for cash operations | |
| **dailyentrycreditor** | Credit entries | Cash account credits | |
| **dailyentrydebtor** | Debit entries | Cash account debits |
| Table Name | Purpose | Relationship | |
|---|---|---|---|
| **user** | User management | Tracks who creates/modifies cash registers | |
| **branch** | Branch management | Multi-branch cash register support |
๐ง Key Functions
1. Main Display (Default Action)
Purpose: Display cash register creation form
Line: 116
Process Flow:
2. add()
Purpose: Create new cash register with opening balance
Line: 338
Parameters (via $_POST):
- โข
savename- Cash register name - โข
savecurrentvalue- Opening balance amount - โข
savedetails- Additional details - โข
currencyId- Currency ID - โข
sortby- Display sort order - โข
saveTreeParentType- Account tree parent type (0=assets, 1=liabilities) - โข
savetypeid- Cash register type/category
Process Flow:
Critical Business Logic:
1. Name Validation: Checks for duplicate cash register names
2. Currency Conversion: Converts amounts to main currency using conversion factor
3. Chart of Accounts: Auto-creates account tree element
4. Journal Entry: Creates opening balance entry (Dr. Cash, Cr. Capital)
5. Movement Logging: Records initial cash movement
Account Tree Integration:
// Parent assignment based on type
$parent = 40; // Assets (default)
if ($saveTreeParentType == 1) {
$parent = 408; // Liabilities
}
3. show()
Purpose: Display cash register listing with filtering
Line: 428
Filtering Options:
- โข Cash register type filtering
- โข User permission-based filtering
- โข Single vs. multiple cash register view
Access Control:
if ($_SESSION['searchinonesave'] == 0) {
// User can see multiple cash registers
if ($_SESSION['saveids'] == 0) {
// See all cash registers
} else {
// See specific cash register IDs
}
} else {
// User restricted to single cash register
}
4. delete()
Purpose: Soft delete/restore cash register
Line: 460
Parameters:
- โข
condition- Delete flag (0=active, 1=deleted) - โข
saveid- Cash register ID
Process: Changes condition status without removing data
5. deleteFinaly()
Purpose: Permanently delete cash register
Line: 485
Process Flow:
Warning: Permanent deletion removes all associated data including chart of accounts element.
6. edit()
Purpose: Load cash register data for editing
Line: 502
Returns: Cash register data with currency information
7. editprint()
Purpose: Load cash register data for print view
Line: 520
**Similar to edit() but optimized for printing
8. update()
Purpose: Update existing cash register
Line: 539
Process Flow:
Chart Account Sync:
$oldTree->name = $saveName;
$oldTree->customName = $saveName;
$parent = 40; // Assets
if ($saveTreeParentType == 1) {
$parent = 408; // Liabilities
}
$oldTree->parent = $parent;
9. insertSavedaily(...)
Purpose: Log cash movement in daily cash log
Line: 608
Parameters:
- โข
$savedailysavebefore- Balance before transaction - โข
$savedailychangeamount- Transaction amount - โข
$savedailychangetype- Change type (0=decrease, 1=increase) - โข
$saveid- Cash register ID - โข
$processname- Description of operation - โข
$savedailymodelid- Related record ID - โข
$savedailysaveafter- Balance after transaction - โข
$tablename- Source table name
Usage: Called by all cash operations for audit trail
---
๐ Business Logic Flow
Cash Register Creation Workflow
Currency Conversion Logic
$saveConversionFactor = R::getCell("select conversionFactor from currency where id = ?", [$currencyId]);
$savecurrentValueInMainCurrency = $savecurrentValue / $saveConversionFactor;
Journal Entry for Opening Balance
Dr. Cash Account (Auto-created) $amount
Cr. Capital Account (121) $amount
Entry Comment: "ุฅุถุงูุฉ ูู [Cash Register Name]"
Access Control Patterns
1. Single Cash Register: User restricted to one cash register
2. Multiple Cash Registers: User can access all or specific subset
3. Permission-Based: Filter based on user's assigned cash registers
---
โ ๏ธ Common Issues
1. Duplicate Cash Register Names
Issue: Attempting to create cash register with existing name
Prevention: Name uniqueness validation in add() function:
$checkName = $mySaveRecord->queryBySavename($saveName);
if (!empty($checkName)) {
return 1; // Name exists error
}
2. Currency Conversion Errors
Issue: Incorrect currency conversions
Solution: Verify currency conversion factors:
$saveConversionFactor = R::getCell("select conversionFactor from currency where id = ?", [$currencyId]);
3. Chart of Accounts Sync Issues
Issue: Cash register name changes not reflected in accounts
Solution: Update both save record and chart account:
$oldTree->name = $saveName;
$oldTree->customName = $saveName;
editTreeElement($oldTree);
4. Transaction Rollback Failures
Issue: Partial updates on transaction failure
Prevention: Proper transaction handling:
$mytransactions = new Transaction();
try {
// Operations
$mytransactions->commit();
} catch (Exception $ex) {
$mytransactions->rollback();
}
5. Cash Movement Audit Gaps
Issue: Missing cash movement logs
Solution: Always call insertSavedaily() for cash operations
---
๐ Dependencies
Required Files
- โข
../public/impOpreation.php- Core operations - โข
../public/authentication.php- Security - โข
../public/include_dao.php- Database layer - โข
dailyentryfun.php- Journal entry utilities - โข
initiateStaticSessionCommingWithCurl.php- API session
Critical DAOs
- โข
SaveDAO- Cash register CRUD operations - โข
SavedailyDAO- Cash movement logging - โข
CurrencyDAO- Multi-currency support - โข
AccountstreeDAO- Chart of accounts integration
JavaScript Integration
- โข Form validation (customValidation flag)
- โข AJAX operations for CURL posts
- โข Sorting functionality
---
๐ฏ Cash Register Types and Categories
Standard Categories
- โข Operating Cash - Daily operational cash
- โข Petty Cash - Small expense cash
- โข Change Fund - Customer change reserves
- โข Foreign Currency - Multi-currency holdings
Save Tree Parent Types
- โข 0: Assets (ููุฏูุฉ ูู ุงูุตูุฏูู) - Standard cash assets
- โข 1: Liabilities (ุฃู ุงูุงุช ููุฏูุฉ) - Cash held in trust
Sort Order Management
- โข Numeric sorting for display order
- โข Can be updated via AJAX (sortby action)
---
๐ฒ Best Practices
1. Cash Register Setup
- โข Use descriptive names for easy identification
- โข Set appropriate currency for multi-currency operations
- โข Assign correct parent type (assets vs. liabilities)
- โข Set logical sort order for user interface
2. Opening Balances
- โข Verify opening balances before creation
- โข Document source of opening cash
- โข Ensure currency conversions are accurate
3. Access Control
- โข Configure user access to appropriate cash registers
- โข Use single vs. multiple cash register permissions
- โข Regular review of cash register access rights
4. Audit and Reconciliation
- โข Regular review of savedaily movement logs
- โข Reconcile physical cash with system balances
- โข Monitor currency conversion impacts
---
๐ API Support
CURL Integration
The controller supports API operations via CURL posts:
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
// API response format
$data = array(
'status' => 1, // 1=success, 2=error
'message' => 'ุชู
ุช ุงูุนู
ููู ุจูุฌุงุญ',
'message_en' => 'Success'
);
echo json_encode($data);
}
API Actions Supported:
- โข Add cash register
- โข Update cash register
- โข Delete cash register
- โข Sort order updates
---
Critical Note: Cash registers are the foundation of cash management in the system. All cash transactions (sales, purchases, expenses, receipts) ultimately affect cash register balances. Changes to cash registers impact the chart of accounts and require careful consideration of existing transaction history.