Bank Account Deficit Controller Documentation
File: /controllers/bankAccountDeficitController.php
Purpose: Manages bank account deficit resolution and balance adjustments for financial reconciliation
Last Updated: December 20, 2024
Total Functions: 3
Lines of Code: ~329
---
๐ Overview
The Bank Account Deficit Controller is a specialized financial module that handles bank account deficit resolution and balance corrections. It provides:
- โข Bank account deficit adjustment operations (increase/decrease)
- โข Account movement tracking with audit trails
- โข Double-entry accounting integration
- โข Financial reconciliation through expense/income categorization
- โข Automatic journal entry creation for accounting compliance
- โข Transaction history viewing and reporting
- โข Print functionality for deficit adjustment records
Primary Functions
- โ Process bank account deficit adjustments
- โ Track account movements with before/after balances
- โ Generate automated journal entries for accounting
- โ Link deficit adjustments to expense/income accounts
- โ Provide audit trail for all balance modifications
- โ Support both increase and decrease operations
- โ Display transaction history with filtering
- โ Generate printable adjustment records
Related Controllers
- โข bankaccountController.php - Bank account management
- โข bankAccountOpController.php - Bank operations
- โข dailyentry.php - Journal entry management
- โข accountstreeController.php - Chart of accounts
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **accountmovement** | Account transaction log | accountmovementid, accountid, accountmovementamount, accountmovementtype, accountmovementdate, tablename, bankid | |
| **bankaccount** | Bank account master data | accountid, accountbeginingbalance, accountname, bankid, userid, treeId | |
| **accountmovementkind** | Transaction type categories | accountmovementkindid, accountmovementkindname, accountmovementkindcomment |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **dailyentry** | Journal entry headers | dailyentryid, entryComment, entryDate | |
| **dailyentrycreditor** | Credit side of journal entries | dailyentrycreditorid, dailyentryid, accountstreeid, value | |
| **dailyentrydebtor** | Debit side of journal entries | dailyentrydebtoreid, dailyentryid, accountstreeid, value | |
| **accountstree** | Chart of accounts | accountstreeid, name, parentid, accounttype |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **bank** | Bank master data | bankid, bankname, bankcode | |
| **youtubelink** | Tutorial/help links | youtubelinkid, title, url | |
| **user** | System users | userid, username |
๐ Key Functions
1. Default Action - Deficit Adjustment Form
Location: Line 117
Purpose: Display form for creating new deficit adjustments
Process Flow:
1. Load authentication and permission checks
2. Query all banks for dropdown selection
3. Load account movement kinds (transaction types)
4. Display deficit adjustment form via add.html template
Template Variables:
- โข
$bank- Available banks for selection - โข
$accountmovementkind- Transaction type categories
---
2. add() - Process Deficit Adjustment
Location: Line 222
Purpose: Execute deficit adjustment with complete accounting integration
Function Signature:
function add()
Process Flow:
1. Extract Form Data:
$bankid = $_POST["ddlBank"];
$accountid = $_POST["accountid"];
$changetype = (int) $_POST["changetype"]; // 0=increase, 1=decrease
$changeamount = $_POST["changeamount"];
```
2. **Load Current Account Balance**:
```php
$account = $myBankaccountRecord->load($accountid);
$balanceBefore = $account->accountbeginingbalance;
```
3. **Calculate New Balance Based on Operation Type**:
- **Increase (changetype = 0)**: `$balanceAfter = $balanceBefore + $changeamount`
- **Decrease (changetype = 1)**: `$balanceAfter = $balanceBefore - $changeamount`
4. **Create Account Movement Record**:
```php
$myAccountmovement->accountmovementbefore = $balanceBefore;
$myAccountmovement->accountmovementamount = $changeamount;
$myAccountmovement->accountmovementafter = $balanceAfter;
$myAccountmovement->tablename = "bankAccountDeficitController.php";
```
5. **Update Account Balance**:
```php
$account->accountbeginingbalance = $balanceAfter;
$myBankaccountRecord->update($account, $account->comment);
```
6. **Generate Journal Entries**:
- **For Deficit Increase (Interest Income)**:
- **Debit**: Bank Account (Asset)
- **Credit**: Account #393 (Interest Income)
- **For Deficit Decrease (Bank Charges)**:
- **Debit**: Account #65 (Bank Expenses)
- **Credit**: Bank Account (Asset)
**Accounting Logic**:
php
if ($changetype == 1) { // Decrease - Bank charges
$dailyEntryDebtor->accountstreeid = 65; // Bank expenses
$dailyEntryCreditor->accountstreeid = $account->treeId; // Bank account
} elseif ($changetype == 0) { // Increase - Interest income
$dailyEntryDebtor->accountstreeid = $account->treeId; // Bank account
$dailyEntryCreditor->accountstreeid = 393; // Interest income
}
---
### 3. **show()** - Transaction History Display
**Location**: Line 146
**Purpose**: Display filtered deficit adjustment history with search capabilities
**Function Signature**:
php
// Parameters from $_REQUEST
$startDate = $_REQUEST['from'];
$endDate = $_REQUEST['to'];
$bankid = $_REQUEST['bankid'];
$accountid = $_REQUEST['accountid'];
$accountmovementkindid = $_REQUEST['accountmovementkindid'];
**Process Flow**:
1. **Build Dynamic Query String**:
```php
$queryString = '';
if (isset($bankid) && $bankid > 0) {
$queryString .= ' AND accountmovement.bankid = ' . $bankid;
}
if (isset($accountid) && $accountid > 0) {
$queryString .= ' AND accountmovement.accountid = ' . $accountid;
}
```
2. **Apply Date Filters**:
```php
if (isset($startDate) && !empty($startDate)) {
$queryString .= ' AND DATE(accountmovement.accountmovementdate) >= "' . $startDate . '"';
}
if (isset($endDate) && !empty($endDate)) {
$queryString .= ' AND DATE(accountmovement.accountmovementdate) <= "' . $endDate . '"';
}
```
3. **Default to Today if No Filters**:
```php
if ($queryString == '') {
$queryString .= ' AND DATE(accountmovement.accountmovementdate) >= "' . $today . '"';
}
```
4. **Filter by Controller Source**:
```php
$queryString .= ' AND accountmovement.tablename = "bankAccountDeficitController.php"';
```
5. **Load and Display Results**:
- Query movement data with extended information
- Load reference data (banks, accounts, movement kinds)
- Display via `show.html` template
---
### 4. **edit($id)** - Load Single Transaction
**Location**: Line 318
**Purpose**: Load specific deficit adjustment record for display/editing
**Function Signature**:
php
function edit($id)
**Process Flow**:
php
$data = $myAccountmovementEx->loadMovementEX($id);
return $data;
**Used by**: `editprint` action for displaying printable transaction records
---
## ๐ Workflows
### Workflow 1: Deficit Adjustment Process
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ START: Select Account & Amount โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Load Current Account Balance โ
โ - Query bankaccount table โ
โ - Get current accountbeginingbalance โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. Determine Operation Type โ
โ - changetype = 0: Increase (Interest) โ
โ - changetype = 1: Decrease (Bank Charges) โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Calculate New Balance โ
โ - Increase: balanceAfter = balanceBefore + amount โ
โ - Decrease: balanceAfter = balanceBefore - amount โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. Create Account Movement Record โ
โ - Record before/after balances โ
โ - Set transaction type and amount โ
โ - Link to source controller โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 5. Update Account Balance โ
โ - Update accountbeginingbalance โ
โ - Set modification date and user โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 6. Generate Journal Entry โ
โ - Create daily entry header โ
โ - Add debit/credit entries โ
โ - Link to appropriate expense/income accounts โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
### Workflow 2: Transaction History Viewing
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ START: Apply Search Filters โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Build Query String โ
โ - Add bank filter if specified โ
โ - Add account filter if specified โ
โ - Add date range filters โ
โ - Add movement kind filter โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. Apply Controller Filter โ
โ - Only show bankAccountDeficitController transactions โ
โ - Order by transaction ID descending โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Query and Display Results โ
โ - Load movement data with extended info โ
โ - Include bank names, account names โ
โ - Show before/after balances โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
## ๐ URL Routes & Actions
| URL Parameter | Function Called | Description |
|---------------|----------------|-------------|
| `do=` (empty) | Default action | Display deficit adjustment form |
| `do=add` | `add()` | Process new deficit adjustment |
| `do=show` | Show transactions | Display filtered transaction history |
| `do=editprint` | `edit()` | Load single transaction for printing |
| `do=sucess` | - | Show success message |
| `do=error` | - | Show error message |
### Required Parameters by Action
**Add Deficit Adjustment** (`do=add`):
- `ddlBank` - Bank ID
- `accountid` - Account ID
- `changetype` - Operation type (0=increase, 1=decrease)
- `changeamount` - Adjustment amount
- `accountmovementcomment` - Comment/description
- `accountmovementkindid` - Transaction type category
**Show Transactions** (`do=show`):
- `from` - Start date (YYYY-MM-DD) [optional]
- `to` - End date (YYYY-MM-DD) [optional]
- `bankid` - Bank filter [optional]
- `accountid` - Account filter [optional]
- `accountmovementkindid` - Movement kind filter [optional]
**Print Transaction** (`do=editprint`):
- `id` - Account movement ID
---
## ๐งฎ Calculation Methods
### Balance Calculation
php
// Current balance retrieval
$account = $myBankaccountRecord->load($accountid);
$balanceBefore = $account->accountbeginingbalance;
// New balance calculation
if ($changetype == 1) { // Decrease
$balanceAfter = $balanceBefore - $changeamount;
} elseif ($changetype == 0) { // Increase
$balanceAfter = $balanceBefore + $changeamount;
}
### Journal Entry Logic
php
// For decreases (bank charges)
if ($changetype == 1) {
$dailyEntryDebtor->accountstreeid = 65; // Bank expenses
$dailyEntryCreditor->accountstreeid = $account->treeId; // Bank account
}
// For increases (interest income)
elseif ($changetype == 0) {
$dailyEntryDebtor->accountstreeid = $account->treeId; // Bank account
$dailyEntryCreditor->accountstreeid = 393; // Interest income
}
---
## ๐ Security & Permissions
### Authentication
- All actions require `include_once("../public/authentication.php")`
- Session-based user authentication required
- User ID tracked in all database modifications
### Transaction Safety
- Uses Transaction class for database consistency
- Rollback on any errors during processing
- Atomic operations ensure data integrity
### Input Validation
- Numeric casting: `(int) $_POST["changetype"]`
- Amount validation through form controls
- SQL injection prevented by DAO layer
---
## ๐งช Common Use Cases
### 1. Bank Charge Processing
Scenario: Bank charged $25 for monthly maintenance
- โข Select affected bank account
- โข Choose "Decrease" operation type
- โข Enter $25 as adjustment amount
- โข System debits "Bank Expenses" and credits bank account
### 2. Interest Income Recording
Scenario: Bank credited $150 interest income
- โข Select affected bank account
- โข Choose "Increase" operation type
- โข Enter $150 as adjustment amount
- โข System debits bank account and credits "Interest Income"
### 3. Reconciliation Adjustments
Scenario: Bank statement shows different balance
- โข Calculate difference amount
- โข Use appropriate increase/decrease operation
- โข Add detailed comment explaining reconciliation
- โข Generate audit trail for accounting review
---
## ๐ Troubleshooting
### Common Issues
1. **Transaction Fails to Commit**
- Check database connections
- Verify account exists and is active
- Ensure sufficient permissions
2. **Journal Entries Not Created**
- Verify chart of accounts setup (accounts 65, 393)
- Check dailyentry function availability
- Confirm account tree ID exists
3. **Balance Calculation Errors**
- Validate input amounts are numeric
- Check for concurrent account modifications
- Verify account balance before operation
### Debug Commands
sql
-- Check account movement history
SELECT * FROM accountmovement
WHERE tablename = 'bankAccountDeficitController.php'
ORDER BY accountmovementdate DESC;
-- Verify account balances
SELECT accountid, accountname, accountbeginingbalance
FROM bankaccount
WHERE conditions = 0;
-- Check journal entry creation
SELECT * FROM dailyentry
WHERE entryComment LIKE '%ุชุณููู%'
ORDER BY entryDate DESC;
```
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข bankaccountController.md - Bank account management
- โข dailyentryfun.php - Journal entry functions
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When major changes occur