Upload markdown files and organize them in a tree structure
⚠️ Warning: This action cannot be undone!
controllers/accountstree.php (686 lines)
accountstree
// Line 130-176 in accountstree.php
">if (empty($do)) {
// Set up 6-level tree structure
$maxNoOfLevels = 6;
for (">$i = 1; ">$i <= ">$maxNoOfLevels; $i++) {
">$level = $accountsTreeSettingDAO->queryByLevelno($i);
">if (count($level) < 1) {
// Create level if doesn't exist
$accountsTreeSetting->levelno = $i;
$accountsTreeSetting->nooffields = 1;
">$id = $accountsTreeSettingDAO->insert($accountsTreeSetting);
}
">$levelSetting[">$i] = $level;
}
// Get expense type parents for integration
$allParents = getExtepensesTypeParents();
// Generate complete tree HTML
$outputString = display_children(0, 0, 0, 0);
// Check for opening inventory
checkStartGoods();
$smarty->display("accountstreeview/add.html");
}
SQL Operations:
-- Get level settings for each tree level
SELECT * FROM accountstreesetting WHERE levelno = ?
-- Create level setting if not exists
INSERT INTO accountstreesetting (levelno, nooffields) VALUES (?, 1)
-- Get all accounts for tree display (done in display_children function)
SELECT accountstree.*, parent.name as parentName
FROM accountstree
LEFT JOIN accountstree as parent ON accountstree.parent = parent.id
WHERE accountstree.conditions = 0
ORDER BY accountstree.theOrder, accountstree.id
-- Get expense type parents
SELECT * FROM expensestype WHERE parent = 0 AND conditions = 0
-- Check for opening inventory account
SELECT * FROM accountstree WHERE itemtype3 = 'بضاعة اول المدة039;
API Endpoint: GET /controllers/accountstree.php
Business Logic:
// Line 177-214 in accountstree.php
">elseif ($do == "set") {
// Set long-term cookies (20 years)
setcookie("calTreeNodes", (int) $_POST['calTreeNodes039;], (time() + (20 365 24 60 60)));
setcookie("showClientsAtTree", (int) $_POST['showClientsAtTree039;], (time() + (20 365 24 60 60)));
setcookie("showSuppliersAtTree", (int) $_POST['showSuppliersAtTree039;], (time() + (20 365 24 60 60)));
// Configure 6 levels of tree structure
$maxNoOfLevels = 6;
for (">$i = 1; ">$i <= ">$maxNoOfLevels; $i++) {
">$level = $accountsTreeSettingDAO->queryByLevelno($i);
">if (count($level) < 1) {
// Create new level setting
$accountsTreeSetting->levelno = $i;
$accountsTreeSetting->nooffields = 1;
">$id = $accountsTreeSettingDAO->insert($accountsTreeSetting);
} else {
// Update existing level
">$level = $level[0];
$leveldigits = filter_input(INPUT_POST, "level" . $i);
">if (!empty($leveldigits)) {
">$level->nooffields = $leveldigits;
$accountsTreeSettingDAO->update($level);
}
}
}
header("location:accountstree.php");
}
SQL Operations:
-- Get existing level setting
SELECT * FROM accountstreesetting WHERE levelno = ?
-- Insert new level setting
INSERT INTO accountstreesetting (levelno, nooffields) VALUES (?, ?)
-- Update level setting
UPDATE accountstreesetting SET nooffields = ? WHERE id = ?
API Endpoint: POST /controllers/accountstree.php?do=set
Request Body:
{
"calTreeNodes": 1,
"showClientsAtTree": 1,
"showSuppliersAtTree": 1,
"level1": 2,
"level2": 2,
"level3": 3,
"level4": 3,
"level5": 3,
"level6": 3
}
---
// Line 215-216 in accountstree.php
">elseif ($do == "uploadtree") {
$smarty->display("accountstreeview/uploadexceltree.html");
}
API Endpoint: GET /controllers/accountstree.php?do=uploadtree
Business Logic: Simple form display for Excel file upload
---
// Line 217-224 in accountstree.php
">elseif ($do == "addtreefromexcel") {
addTreeFromExcel(); // Complex function starting at line 492
}
Complete addTreeFromExcel() function implementation:
// Line 492-617 in accountstree.php
function addTreeFromExcel() {
global $accountsTree, $accountsTreeDAO, $accountsTreeEX;
// Upload Excel file
">$handle = ">new upload($_FILES['productssheet039;]);
">$excelfileName = uploadfile($handle, "../upload/products");
$inputFileName = "../upload/products/" . $excelfileName;
// Transaction for data integrity
$mytransactions = new Transaction();
// Truncate existing tree (WARNING: Deletes all accounts!)
$accountsTreeEX->Truncate();
// Process Excel file
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
">$objPHPExcel = $objReader->load($inputFileName);
">$sheet = $objPHPExcel->getSheet(0);
">$highestRow = $sheet->getHighestRow();
for ($row = 2; $row <= ">$highestRow; $row++) {
$rowData = $sheet->rangeToArray('A039; . $row . 039;:039; . ">$highestColumn . $row, NULL, TRUE, FALSE);
$rowData = $rowData[0];
// Extract account data from Excel
">$code = $rowData[1]; // Account code
">$accountName = $rowData[3]; // Arabic name
">$accountNameEn = $rowData[4]; // English name
">$itemType3 = $rowData[5]; // Account category
$parentCode = $rowData[6]; // Parent account code
">$level = $rowData[7]; // Tree level
$accountNature = $rowData[16]; // Debit/Credit nature
">$listName = $rowData[17]; // Balance Sheet/Income Statement
">if (!empty($accountName)) {
// Find parent account
$parentId = 0;
$parentData = $accountsTreeDAO->queryByLayingOrder($parentCode);
">if (count($parentData) > 0) {
$parentId = $parentData[0]->id;
}
// Process account nature
switch ($accountNature) {
case 'مدين039;: // Debit
$accountNature = 0;
break;
case 'دائن039;: // Credit
$accountNature = 1;
break;
}
// Process report assignment
$listId = 0;
switch ($listName) {
case 'ميزانية039;: // Balance Sheet
$listId = 1;
break;
case 'قائمة الدخل039;: // Income Statement
$listId = 2;
break;
}
// Create account tree entry
$accountsTree->customName = $accountName;
$accountsTree->name = $accountName;
$accountsTree->customNameEn = $accountNameEn;
$accountsTree->nameEn = $accountNameEn;
$accountsTree->parent = $parentId;
$accountsTree->itemtype = -1;
$accountsTree->itemtype2 = 1; // Final element (leaf)
$accountsTree->itemtype3 = $itemType3;
$accountsTree->notes = '039;;
$accountsTree->theOrder = 0;
$accountsTree->layingOrder = $code;
$accountsTree->accountNature = $accountNature;
$accountsTree->reportid = $listId;
$accountsTree->conditions = 0;
$accountsTree->theValue = 0;
$accountsTreeDAO->insert($accountsTree);
}
}
}
SQL Operations:
-- Truncate existing accounts tree (DANGEROUS!)
TRUNCATE TABLE accountstree
-- Find parent account by code
SELECT * FROM accountstree WHERE layingOrder = ? AND conditions = 0
-- Insert new account from Excel
INSERT INTO accountstree (
customName, name, customNameEn, nameEn, parent, itemtype, itemtype2,
itemtype3, notes, theOrder, layingOrder, accountNature, reportid,
conditions, theValue
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0)
API Endpoint: POST /controllers/accountstree.php?do=addtreefromexcel
Request: Multipart form with Excel file
Business Logic:
// Line 225-283 in accountstree.php
">elseif ($do == "exportAsExcel") {
// Create PHPExcel object
">$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle("شجرة الحسابات");
// Set headers in Arabic
$objPHPExcel->getActiveSheet()
->SetCellValue('A1039;, 039;الكود039;) // Code
->SetCellValue('B1039;, 039;اسم العنصر039;) // Account Name
->SetCellValue('C1039;, 039;والد العنصر039;) // Parent
->SetCellValue('D1039;, 039;طبيعة العنصر039;) // Nature
->SetCellValue('E1039;, 039;فى تقرير039;) // In Report
->SetCellValue('F1039;, 039;القائمة039;); // Statement
// Style formatting
$styleArray = array(
'font039; => array(
'bold039; => true,
'size039; => 12,
'color039; => array(039;rgb039; => 039;blue039;),
'name039; => 039;Verdana039;
)
);
// Export all accounts with exportToExcel function
exportToExcel(0, 2); // Start from root level
// Output Excel file
header('Content-Type: application/vnd.ms-excel039;);
header('Content-Disposition: attachment;filename="AccountsTree.xlsx"039;);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007039;);
$objWriter->save('php://output039;);
}
SQL Operations from exportToExcel() function:
-- Get all accounts at specific level
SELECT accountstree.*, parent.name as parentName
FROM accountstree
LEFT JOIN accountstree as parent ON accountstree.parent = parent.id
WHERE accountstree.parent = ? AND accountstree.conditions = 0
ORDER BY accountstree.theOrder, accountstree.id
-- Recursive call for child accounts
-- (Function calls itself for each child account)
API Endpoint: GET /controllers/accountstree.php?do=exportAsExcel
Response: Excel file download (AccountsTree.xlsx)
Business Logic:
// Line 284-286 in accountstree.php
">elseif ($do == "sucess") {
$smarty->display("succes.html");
}
API Endpoint: GET /controllers/accountstree.php?do=sucess
---
// Line 287-289 in accountstree.php
">elseif ($do == "error") {
$smarty->display("error.html");
}
API Endpoint: GET /controllers/accountstree.php?do=error
---
CREATE TABLE accountstree (
id INT PRIMARY KEY AUTO_INCREMENT,
customName VARCHAR(255), -- Arabic account name
name VARCHAR(255), -- Account name
customNameEn VARCHAR(255), -- English account name
nameEn VARCHAR(255), -- English name
parent INT, -- Parent account ID
itemtype INT, -- Account category (-1, 0, 1, etc.)
itemtype2 INT, -- Branch (0) or Leaf (1)
itemtype3 VARCHAR(255), -- Account subcategory
notes TEXT, -- Account notes
theOrder INT, -- Display order
layingOrder VARCHAR(50), -- Account code
accountNature TINYINT, -- 0=Debit, 1=Credit
reportid INT, -- 1=Balance Sheet, 2=Income Statement
conditions TINYINT, -- 0=Active, 1=Deleted
theValue DECIMAL(15,2), -- Current balance
-- Additional fields for integration
branchid INT, -- Branch assignment
userid INT -- User who created
);
CREATE TABLE accountstreesetting (
id INT PRIMARY KEY AUTO_INCREMENT,
levelno INT, -- Tree level (1-6)
nooffields INT -- Number of digits for this level
);
GET /api/v1/accounts-tree # Get complete tree
POST /api/v1/accounts-tree/settings # Update level settings
POST /api/v1/accounts-tree/import # Import from Excel
GET /api/v1/accounts-tree/export # Export to Excel
GET /api/v1/accounts-tree/{id} # Get specific account
POST /api/v1/accounts-tree # Create new account
PUT /api/v1/accounts-tree/{id} # Update account
DELETE /api/v1/accounts-tree/{id} # Delete account