Bind Controller Documentation
File: /controllers/bind.php
Purpose: Employee cash custody and bond management with accounting integration
Last Updated: December 20, 2024
Total Functions: 4
Lines of Code: ~432
---
๐ Overview
The Bind Controller manages employee cash custody and bond transactions within the ERP system. It handles:
- โข Employee cash advance management (custody/bond system)
- โข Cash safe vs bank account funding sources
- โข Automated double-entry accounting integration
- โข Employee custody account management in chart of accounts
- โข Check and cash transaction tracking
- โข Daily entry generation for accounting records
- โข Transaction reversal and modification support
Primary Functions
- โ Create new employee custody transactions
- โ Support both cash safe and bank account sources
- โ Generate automatic accounting entries (double-entry)
- โ Create employee custody accounts in chart of accounts
- โ Handle check payments with beneficiary tracking
- โ Update and modify existing transactions
- โ Reverse accounting entries on updates
- โ Validate employee and funding source data
Related Controllers
- โข employeeController.php - Employee management
- โข saveController.php - Cash safe management
- โข bankaccountController.php - Bank account operations
- โข dailyentry.php - Accounting entry management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| **bind** | Employee custody transactions | bindId, employeeid, bindtype, saveid, savevalue, bankid, bankaccount, ckekno, benefitname, ckekvalue, binddate, userid, dailyentryid, deleted |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **employee** | Employee master data | employeeId, employeeName, conditions | |
| **user** | System users | userid, username, conditions |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **save** | Cash registers/safes | saveid, savename, savevalue, treeId | |
| **bank** | Bank master data | bankid, bankname | |
| **bankaccount** | Bank accounts | accountid, bankid, accountname, accountnumber, accountbeginingbalance, treeId |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **dailyentry** | Journal entry headers | dailyentryid, dDateTime, entryComment, fromFlag | |
| **dailyentrycreditor** | Credit side entries | dailyentryid, accountstreeid, value | |
| **dailyentrydebtor** | Debit side entries | dailyentryid, accountstreeid, value | |
| **accountstree** | Chart of accounts | id, name, parentid, level, isLeaf |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **youtubelink** | Tutorial/help videos | youtubelinkid, title, url |
๐ Key Functions
1. Default Action / Add Form - Empty do
Location: Line 122
Purpose: Display form for creating new employee custody transaction
Process Flow:
1. Load active employees for dropdown
2. Load available cash safes
3. Load bank data for bank selection
4. Display bindview/add.html template
Template Variables:
- โข
$employees- Active employee list - โข
$saves- Available cash safes - โข
$bankData- Bank master data
---
2. add() - Create New Custody Transaction
Location: Line 140 (route), Line 333 (function)
Purpose: Process new employee custody transaction with accounting integration
Form Parameters:
$employee = $_POST["employee"]; // Employee ID
$bindtype = $_POST["bindtype"]; // 1 = Cash safe, 2 = Bank
$save = $_POST["save"]; // Cash safe ID (if bindtype = 1)
$saveValue = $_POST["saveValue"]; // Cash amount (if bindtype = 1)
$bank = $_POST["bank"]; // Bank ID (if bindtype = 2)
$account = $_POST["account"]; // Bank account ID (if bindtype = 2)
$ckekNo = $_POST["ckekNo"]; // Check number (if bindtype = 2)
$benefitName = $_POST["benefitName"]; // Check beneficiary (if bindtype = 2)
$ckekValue = $_POST["ckekValue"]; // Check amount (if bindtype = 2)
Process Flow:
1. Validate form inputs based on transaction type
2. Create new bind record with transaction details
3. Set system fields (userid, date, deleted = 0)
4. Insert record and get new bind ID
5. Call saveDailyEntry() for accounting integration
6. Redirect to success page
Transaction Types:
- โข Type 1 (Cash Safe): Employee receives cash from safe
- โข Type 2 (Bank/Check): Employee receives check or bank transfer
---
3. edit() - Display Edit Form
Location: Line 152
Purpose: Load existing transaction for modification
Process Flow:
1. Get bind ID from URL parameter
2. Load existing bind record
3. Load related bank accounts for selected bank
4. Load employees, safes, and banks for dropdowns
5. Display bindview/edit.html with pre-populated data
Key Feature: Dynamically loads bank accounts based on previously selected bank using queryByBankidAndaccountdele().
---
4. update() - Modify Existing Transaction
Location: Line 213 (route), Line 380 (function)
Purpose: Update existing custody transaction with accounting reversal
Process Flow:
1. Load existing bind record by ID
2. Update record with new form values
3. Reverse previous accounting entry using reverseEntryWithItsID()
4. Generate new accounting entry with updated values
5. Update bind record with new daily entry ID
Critical Feature: Automatically reverses the original accounting entry before creating a new one, maintaining accounting integrity.
---
5. saveDailyEntry() - Accounting Integration
Location: Line 247
Purpose: Generate double-entry accounting records for custody transactions
Function Signature:
function saveDailyEntry($value, $bindtype, $employeeId, $id, $bankaccount, $bindId)
Accounting Logic:
DEBIT Side (Employee Custody Account):
// Create or find employee custody account
$accountsTree33 = $accountsTreeDAO->queryByName('ุนูุฏุฉ ' . $employee->employeeName);
if (count($accountsTree33) > 0) {
$treeId3 = $accountsTree33[0]->id;
} else {
// Create new custody account under parent ID 46
$treeId3 = addTreeElement('ุนูุฏุฉ ' . $employee->employeeName, 46, 3, 0, 1, '', 0, 0);
}
CREDIT Side (Funding Source):
if ($bindtype == 1) { // Cash Safe
$mySave = $mySaveDAO->load($id);
if ($mySave->treeId > 0) {
$treeId2 = $mySave->treeId;
} else {
// Create safe account under parent ID 20
$treeId2 = addTreeElement($mySave->savename, 20, 3, 0, 1, '', 0, 0);
}
} else { // Bank Account
$bankAccountData = $myBankaccountDAO->load($bankaccount);
if ($bankAccountData->treeId > 0) {
$treeId2 = $bankAccountData->treeId;
} else {
// Create bank account under parent ID 38
$treeId2 = addTreeElement("$bankAccountData->accountname / $bankData->bankname", 38, 3, 0, 1, '', 0, 0);
}
}
Transaction Creation:
$data = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
$dailyEntryId = $data[1];
$bind->dailyentryid = $dailyEntryId; // Link back to bind record
---
๐ Workflows
Workflow 1: Cash Safe Custody Transaction
---
Workflow 2: Bank/Check Custody Transaction
---
๐ URL Routes & Actions
| URL Parameter | Function | Description | |
|---|---|---|---|
| `do=` (empty) | Default | Display add form with dropdowns | |
| `do=add` | `add()` | Process new custody transaction | |
| `do=edit&id=X` | Load edit form | Display existing transaction for editing | |
| `do=update` | `update()` | Process transaction modifications | |
| `do=show` | Display list | Show custody transactions (view only) | |
| `do=delete&id=X` | Soft delete | Mark transaction as deleted (deleted=1) | |
| `do=sucess` | Success page | Display operation success message | |
| `do=error` | Error page | Display operation error message |
๐งฎ Calculation Methods
Chart of Accounts Structure
Double-Entry Accounting
// For cash advance from safe
DEBIT: Employee Custody Account $1,000
CREDIT: Cash Safe Account $1,000
// For check advance from bank
DEBIT: Employee Custody Account $2,500
CREDIT: Bank Account $2,500
Account Linking Logic
// Safes linked to tree with parent ID 20 (Cash accounts)
$treeId2 = addTreeElement($mySave->savename, 20, 3, 0, 1, '', 0, 0);
// Bank accounts linked to tree with parent ID 38 (Bank accounts)
$treeId2 = addTreeElement("$accountname / $bankname", 38, 3, 0, 1, '', 0, 0);
// Employee custody accounts under parent ID 46 (Employee advances)
$treeId3 = addTreeElement('ุนูุฏุฉ ' . $employee->employeeName, 46, 3, 0, 1, '', 0, 0);
---
๐ Security & Permissions
Access Control
include_once("../public/authentication.php"); // Session validation on all actions
Input Validation
$bindId = filter_input(INPUT_GET, "id"); // Sanitize GET parameters
$employee = filter_input(INPUT_POST, "employee"); // Sanitize POST data
Session Management
- โข All actions require active user session
- โข User ID tracked in bind records (
useridfield) - โข Date stamps automatically applied (
binddate)
Data Integrity
- โข Soft deletes only (
deleted = 1) - โข Transaction rollback on accounting entry failures
- โข Accounting entry reversal before updates
---
๐ Performance Considerations
Database Optimization
1. Indexes Required:
- bind(employeeid, deleted, binddate)
- employee(employeeId, conditions)
- save(saveid, treeId)
- bankaccount(accountid, bankid, treeId)
2. Query Patterns:
- Frequent employee lookups for dropdowns
- Tree ID checks for account linking
- Daily entry insertions with transaction support
3. Chart of Accounts Growth:
- New employee accounts created automatically
- Tree structure may grow large over time
- Consider archiving inactive employee accounts
---
๐ Common Issues & Troubleshooting
1. Missing Employee Custody Accounts
Issue: Employee transactions fail to create accounting entries
Cause: Chart of accounts structure missing parent account (ID 46)
Fix:
-- Verify parent account exists for employee custody
SELECT * FROM accountstree WHERE id = 46;
-- Create if missing
INSERT INTO accountstree (id, name, parentid, level, isLeaf)
VALUES (46, 'Employee Custody Accounts', [PARENT_ID], 2, 0);
2. Bank Account Linking Failures
Issue: Bank transactions don't create proper accounting entries
Cause: Bank accounts not linked to chart of accounts
Debug:
-- Check bank account tree linkage
SELECT ba.accountname, ba.treeId, at.name
FROM bankaccount ba
LEFT JOIN accountstree at ON ba.treeId = at.id
WHERE ba.accountid = [ACCOUNT_ID];
3. Transaction Update Errors
Issue: Updates fail or create duplicate accounting entries
Cause: Daily entry reversal function not working
Fix: Verify reverseEntryWithItsID() function exists and works:
-- Check if daily entry was properly reversed
SELECT * FROM dailyentry WHERE dailyentryid = [ENTRY_ID];
SELECT * FROM dailyentrycreditor WHERE dailyentryid = [ENTRY_ID];
SELECT * FROM dailyentrydebtor WHERE dailyentryid = [ENTRY_ID];
4. Form Dropdown Issues
Issue: Employee or bank dropdowns empty
Cause: Active record filtering too restrictive
Debug:
-- Check active employees
SELECT COUNT(*) FROM employee WHERE conditions = 0;
-- Check active banks
SELECT COUNT(*) FROM bank WHERE conditions = 0;
-- Check active safes
SELECT COUNT(*) FROM save WHERE conditions = 0;
---
๐งช Testing Scenarios
Test Case 1: Cash Safe Transaction
1. Create new employee if needed
2. Create cash safe with positive balance
3. Submit bind form with cash safe option
4. Verify bind record created
5. Check accounting entry generated
6. Confirm employee custody account created
7. Verify safe account linked properly
Test Case 2: Bank Check Transaction
1. Setup bank and bank account
2. Submit bind form with check details
3. Verify check number and beneficiary stored
4. Check bank account tree linkage
5. Confirm accounting entry balances
6. Test check number uniqueness (if required)
Test Case 3: Transaction Update
1. Create initial transaction
2. Note original daily entry ID
3. Update transaction with new amount
4. Verify old entry reversed
5. Confirm new entry created
6. Check bind record updated
7. Verify accounting totals correct
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข dailyentry.md - Accounting entry management
- โข employeeController.md - Employee management
- โข saveController.md - Cash safe operations
- โข bankaccountController.md - Bank account management
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When accounting integration changes or new custody features added