๐Ÿ“š ERP Documentation Viewer

Beautiful, colorful documentation for your ERP system

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:

Primary Functions

Related Controllers

---

๐Ÿ—„๏ธ Database Tables

Primary Tables (Direct Operations)

Table NamePurposeKey Columns
**save**Main cash registerssaveid, savename, savecurrentvalue, currencyId, savetypeid, treeId, conditions, sortby
**savedaily**Cash movement historysavedailydate, saveid, savedailychangeamount, savedailychangetype, processname, userid
**savetype**Cash register categoriesid, name, del
### Integration Tables

Table NamePurposeRelationship
**currency**Multi-currency supportUsed by save.currencyId
**accountstree**Chart of accountsAuto-created for each cash register
**dailyentry**Journal entriesAuto-generated for cash operations
**dailyentrycreditor**Credit entriesCash account credits
**dailyentrydebtor**Debit entriesCash account debits
### Tracking Tables

Table NamePurposeRelationship
**user**User managementTracks who creates/modifies cash registers
**branch**Branch managementMulti-branch cash register support
---

๐Ÿ”ง Key Functions

1. Main Display (Default Action)

Purpose: Display cash register creation form

Line: 116

Process Flow:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Authentication
Check
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Set Breadcrumb
Navigation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. add()

Purpose: Create new cash register with opening balance

Line: 338

Parameters (via $_POST):

Process Flow:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Validate Name
Uniqueness
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Insert Cash
Register Record
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

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:

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:

Process: Changes condition status without removing data

5. deleteFinaly()

Purpose: Permanently delete cash register

Line: 485

Process Flow:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Load Cash
Register Data
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

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:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Load Existing
Record
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

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:

Usage: Called by all cash operations for audit trail

---

๐Ÿ”„ Business Logic Flow

Cash Register Creation Workflow

โ–ผ
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Name Uniqueness
Validation
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
Database Insert
(Transaction)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

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

Critical DAOs

JavaScript Integration

---

๐ŸŽฏ Cash Register Types and Categories

Standard Categories

Save Tree Parent Types

Sort Order Management

---

๐ŸŽฒ Best Practices

1. Cash Register Setup

2. Opening Balances

3. Access Control

4. Audit and Reconciliation

---

๐Ÿ”„ 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:

---

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.

โ†‘