HEX
Server: Apache
System: Linux server1.royalgt4.com 4.18.0-553.89.1.lve.el8.x86_64 #1 SMP Wed Dec 10 13:58:50 UTC 2025 x86_64
User: mostafedeg (1125)
PHP: 5.6.40
Disabled: mail,passthru,parse_ini_file,show_source,eval,assert,pcntl_exec,dl,putenv,proc_open,popen
Upload Files
File: /home/mostafedeg/public_html/erp/controllers/thebalancesheet.php
<?php

//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");
//Transaction
include_once("../models/sql/Transaction.class.php");

//here the db files that include in the file
include("../public/include_dao.php");

//Accountstree
require_once('../models/dao/AccountstreeDAO.class.php');
require_once('../models/dto/Accountstree.class.php');
require_once('../models/mysql/AccountstreeMySqlDAO.class.php');
require_once('../models/mysql/ext/AccountstreeMySqlExtDAO.class.php');
//Dailyentry
require_once('../models/dao/DailyentryDAO.class.php');
require_once('../models/dto/Dailyentry.class.php');
require_once('../models/mysql/DailyentryMySqlDAO.class.php');
require_once('../models/mysql/ext/DailyentryMySqlExtDAO.class.php');
//----------------------------------------
//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.class.php');
//Storedetail
require_once('../models/dao/StoredetailDAO.class.php');
require_once('../models/dto/Storedetail.class.php');
require_once('../models/mysql/StoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/StoredetailMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');
//get the do the action
$do = $_GET['do'];
$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");
/* ======================

  Controller Name :- accountstree

  OPERTATION in Controller

  1- display add form, add
  2- add in tbl
  3- display show and tempdelete
  4- edit
  5- update

  ======================== */

//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables
//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();
//Dailyentry
$dailyEntry = new Dailyentry();
$dailyEntryDAO = new DailyentryMySqlDAO();
$dailyEntryEX = new DailyentryMySqlExtDAO();
//-----------------------------------------------------------
//Storedetail
$storeDetail = new Storedetail();
$storeDetailDAO = new StoredetailMySqlDAO();
$storeDetailEX = new StoredetailMySqlExtDAO();
//Store
$store = new Store();
$storeDAO = new StoreMySqlDAO();
$storeEX = new StoreMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();


$today = date("Y-m-d");

//check and use the condition that suite this action
if (empty($do)) {//newBalanceSheet
    //الاصول
    //اصول ثابتة
    $accountsTreeOrdered = array();
    $assets = $accountsTreeDAO->load(1);
    array_push($accountsTreeOrdered, $assets);
    $fixedAssets = $accountsTreeDAO->load(2);
    $fixedAssets->notes = 1;
    array_push($accountsTreeOrdered, $fixedAssets);
    orderAsTree(2, 2);
    $smarty->assign("fixedAssets", $accountsTreeOrdered);
    //اصول متداولة
    $accountsTreeOrdered = array();
    $currentAssets = $accountsTreeDAO->load(36);
    $currentAssets->notes = 1;
    array_push($accountsTreeOrdered, $currentAssets);
    //
    $accountsTreeOrdered = array();
    $ids = [38, 40, 42, 49, 57, 62];
    foreach ($ids as $treeElementId) {
        $element = $accountsTreeDAO->load($treeElementId);
        $element->notes = 2;
        array_push($accountsTreeOrdered, $element);
        orderAsTree($treeElementId, 3);
    }
    $smarty->assign("currentAssets", $accountsTreeOrdered);





    //الالتزامات
    $accountsTreeOrdered = array();
    $currentAssets = $accountsTreeDAO->load(78);
    $currentAssets->notes = 1;
    array_push($accountsTreeOrdered, $currentAssets);
    $currentAssets = $accountsTreeDAO->load(118);
    $currentAssets->notes = 1;
    array_push($accountsTreeOrdered, $currentAssets);
    //
    $ids = [120, 122, 125, 128, 110];
    foreach ($ids as $treeElementId) {
        $element = $accountsTreeDAO->load($treeElementId);
        $element->notes = 2;
        array_push($accountsTreeOrdered, $element);
        orderAsTree($treeElementId, 3);
    }
    $smarty->assign("ownerequity", $accountsTreeOrdered);
    //
    $accountsTreeOrdered = array();
    $nonCurrentLiabilities = $accountsTreeDAO->load(113);
    $nonCurrentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $nonCurrentLiabilities);
    orderAsTree(113, 3);
    $smarty->assign("nonCurrentLiabilities", $accountsTreeOrdered);
    //
    $accountsTreeOrdered = array();
    $currentLiabilities = $accountsTreeDAO->load(79);
    $currentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $currentLiabilities);

    $currentLiabilities = $accountsTreeDAO->load(80);
    $currentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $currentLiabilities);
    orderAsTree(80, 3);

    $currentLiabilities = $accountsTreeDAO->load(88);
    $currentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $currentLiabilities);
    orderAsTree(88, 3);

    $currentLiabilities = $accountsTreeDAO->load(92);
    $currentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $currentLiabilities);
    $currentLiabilities = $accountsTreeDAO->load(93);
    $currentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $currentLiabilities);
    orderAsTree(93, 3);
    $currentLiabilities = $accountsTreeDAO->load(96);
    $currentLiabilities->notes = 1;
    array_push($accountsTreeOrdered, $currentLiabilities);
    orderAsTree(96, 3);

    $smarty->assign("currentLiabilities", $accountsTreeOrdered);


    $smarty->display("thebalancesheetview/addnew.html");
} elseif ($do == "old") {//the old BalanceSheet
//here the permssion check
    include_once("../public/authentication.php");

    /* ------------------------------------------------------------------------ */
    //ايجاد صافى الربح من تقرير ارباح وخسائر(net)
    ###لحساب صافى الربح
    ##يتم اضافة المصروفات والايرادات على اجمالى الربح
    //1-اجمالى الربح او الخسارة من تقرير متاجرة
    ############### this part is comon
    //اجمالى المبيعات
    $sales = $accountsTreeDAO->load(16);
    //اجمالى مردودات المبيعات
    $salesReturn = $accountsTreeDAO->load(13);
    ###############
    $totalCreditorCommon = $sales->theValue - $salesReturn->theValue;


    //way2
    //بضاعة اول المدة
    $accountsTree = $accountsTreeDAO->load(19);
    $startGoodsVal = $accountsTree->theValue;

    //بضاعة اخر المدة
    //نحسب قيمة البضاعة حاليا
    $endGoodsVal = getStartGoodsVal();

    //اجمالى المشتريات
    $purchases = $accountsTreeDAO->load(12);
    //اجمالى مردودات المشتريات
    $purchasesReturn = $accountsTreeDAO->load(17);

    $totalDeptor2 = $startGoodsVal + ($purchases->theValue - $purchasesReturn->theValue) - $endGoodsVal;
    $netVal2 = $totalCreditorCommon - $totalDeptor2; //مجمل الربح
    $totalDebtor = 0;
    $totalCreditor = 0;
    if ($netVal2 >= 0) {
        $totalCreditor += $netVal2;
    } else {
        $totalDebtor += $netVal2;
    }

    //2-المصروفات
    //3-الايرادات
    //
    //*-------------------------*/
    $exceptArr = array(12, 13, 16, 17);
    //كل المصروفات
    $myArray = array();
    display_childrenExcept(3);
    foreach ($myArray as $value) {
        $totalDebtor += $value->theValue;
    }
    //كل الايرادات
    $myArray = array();
    display_childrenExcept(4);
    foreach ($myArray as $value) {
        $totalCreditor += $value->theValue;
    }

    $netvalue = $totalCreditor - $totalDebtor; ##########صافى الربح##########

    /* ------------------------------------------------------------------------ */
    $exceptArr = array();
    //
    $fixedAssetsArr = array();
    $circulatingAssetsArr = array();
    $fixedOpponentsArr = array();
    $circulatingOpponentsArr = array();

    ##1
    $myArray = array();
    display_childrenExcept(5);
    $fixedAssetsArr = $myArray;
    $totalFixedAssets = 0;
    foreach ($myArray as $value) {
        $value->customName = $value->layingOrder . " " . $value->customName;
        $totalFixedAssets += $value->theValue;
    }
    $smarty->assign("totalFixedAssets", $totalFixedAssets);
    $fixedAssetsCount = count($myArray);

    ##2
    $myArray = array();
    display_childrenExcept(6);
    $circulatingAssetsArr = $myArray;
    $totalCirculatingAssets = 0;
    foreach ($myArray as $value) {
        $value->customName = $value->layingOrder . " " . $value->customName;
        if ($value->id == 19) {
            //بضاعة اول المدة
            //نحط مكانها بضاعة اخر المدة
            $value->name = "بضاعة (من المخازن ارصدة)";
            $value->customName = "بضاعة (من المخازن ارصدة)";
            $value->theValue = $endGoodsVal;
        }
        $totalCirculatingAssets += $value->theValue;
    }
    $smarty->assign("totalCirculatingAssets", $totalCirculatingAssets);
    $circulatingAssetsCount = count($myArray);
    ##3
    $myArray = array();
    display_childrenExcept(25);
    $fixedOpponentsArr = $myArray;
    $totalFixedOpponents = 0;
    foreach ($myArray as $value) {
        $value->customName = $value->layingOrder . " " . $value->customName;
        $totalFixedOpponents += $value->theValue;
    }
    $smarty->assign("totalFixedOpponents", $totalFixedOpponents);
    $fixedOpponentsCount = count($myArray);
    ##4
    $myArray = array();
    display_childrenExcept(26);
    $circulatingOpponentsArr = $myArray;
    $totalCirculatingOpponents = 0;
    foreach ($myArray as $value) {
        $value->customName = $value->layingOrder . " " . $value->customName;
        $totalCirculatingOpponents += $value->theValue;
    }
    $smarty->assign("totalCirculatingOpponents", $totalCirculatingOpponents);
    $circulatingOpponentsCount = count($myArray);


    ###############
    //prepare fixed array
    $fixedArr = array();

    $totalCountFixed = $fixedAssetsCount + $fixedOpponentsCount;

    $accountsTree = new Accountstree();
    $fixedAssetsItr = 0;
    $fixedOpponentsItr = 0;
    //ليه  $totalCountFixed * 2
    //لوفيه جمب فيه نتيجتين وجمب فاضى مش هيجيب غير نتيجة واحده من الجمب اللى فيه نتيجتين عشان فيه لفة ضاعت مع الجمب الفاضى فقلت الففه الضعف
    $realCountFixed = 0;
    for ($i = 0; $i < ($totalCountFixed * 2); $i++) {
        if (empty($fixedAssetsArr[$fixedAssetsItr]) && empty($fixedOpponentsArr[$fixedOpponentsItr])) {
            //do nothing
        } else {
            $realCountFixed++;
            if ($i % 2 == 0) {
                if (empty($fixedAssetsArr[$fixedAssetsItr])) {
                    array_push($fixedArr, $accountsTree);
                } else {
                    array_push($fixedArr, $fixedAssetsArr[$fixedAssetsItr]);
                    $fixedAssetsItr++;
                }
            } else {
                if (empty($fixedOpponentsArr[$fixedOpponentsItr])) {
                    array_push($fixedArr, $accountsTree);
                } else {
                    array_push($fixedArr, $fixedOpponentsArr[$fixedOpponentsItr]);
                    $fixedOpponentsItr++;
                }
            }
        }
    }

    $smarty->assign("realCountFixed", $realCountFixed);
    $smarty->assign("fixedArr", $fixedArr);

    ###############
    //prepare Circulating array
    $circulatingArr = array();

    $totalCountCirculating = $circulatingAssetsCount + $circulatingOpponentsCount;

    $accountsTree = new Accountstree();
    $circulatingAssetsItr = 0;
    $circulatingOpponentsItr = 0;
    //ليه  $totalCountFixed * 2
    //لوفيه جمب فيه نتيجتين وجمب فاضى مش هيجيب غير نتيجة واحده من الجمب اللى فيه نتيجتين عشان فيه لفة ضاعت مع الجمب الفاضى فقلت الففه الضعف
    $realCountCirculating = 0;
    for ($i = 0; $i < ($totalCountCirculating * 2); $i++) {
        if (empty($circulatingAssetsArr[$circulatingAssetsItr]) && empty($circulatingOpponentsArr[$circulatingOpponentsItr])) {
            //do nothing
        } else {
            $realCountCirculating++;
            if ($i % 2 == 0) {
                if (empty($circulatingAssetsArr[$circulatingAssetsItr])) {
                    array_push($circulatingArr, $accountsTree);
                } else {
                    array_push($circulatingArr, $circulatingAssetsArr[$circulatingAssetsItr]);
                    $circulatingAssetsItr++;
                }
            } else {
                if (empty($circulatingOpponentsArr[$circulatingOpponentsItr])) {
                    array_push($circulatingArr, $accountsTree);
                } else {
                    array_push($circulatingArr, $circulatingOpponentsArr[$circulatingOpponentsItr]);
                    $circulatingOpponentsItr++;
                }
            }
        }
    }

    $smarty->assign("realCountCirculating", $realCountCirculating);
    $smarty->assign("circulatingArr", $circulatingArr);
    /* ------------------------------------------------------------------------------------------------------------------------- */



    //أصول اخرى
    $exceptArr = array(5, 6);
    $myArray = array();
    display_childrenExcept(1);
    $otherAssetsArr = $myArray;
    $totalOtherAssets = 0;
    foreach ($myArray as $value) {
        $value->customName = $value->layingOrder . " " . $value->customName;
        $totalOtherAssets += $value->theValue;
    }
    $smarty->assign("totalOtherAssets", $totalOtherAssets);
    $otherAssetsCount = count($myArray);
    //خصوم اخرى
    $exceptArr = array(25, 26);
    $myArray = array();
    display_childrenExcept(2);
    $otherOpponentsArr = $myArray;
    //صافى ارباح وخسائر
    $accountsTree = new Accountstree();

    if ($netvalue >= 0) {
        $accountsTree->name = "صافى ارباح";
        $accountsTree->customName = "صافى ارباح";
    } else {
        $accountsTree->name = "صافى خسائر";
        $accountsTree->customName = "صافى خسائر";
    }

    $accountsTree->theValue = $netvalue;
    array_push($otherOpponentsArr, $accountsTree);
    $totalOtherOpponents = 0;
    foreach ($myArray as $value) {
        $value->customName = $value->layingOrder . " " . $value->customName;
        $totalOtherOpponents += $value->theValue;
    }
    $totalOtherOpponents = $totalOtherOpponents + $netvalue;
    $smarty->assign("totalOtherOpponents", $totalOtherOpponents);
    $otherOpponentsCount = count($myArray) + 1;



    ###############
    //prepare other array
    $otherArr = array();

    $totalCountOther = $otherAssetsCount + $otherOpponentsCount;

    $accountsTree = new Accountstree();
    $otherAssetsItr = 0;
    $otherOpponentsItr = 0;
    //ليه  $totalCountFixed * 2
    //لوفيه جمب فيه نتيجتين وجمب فاضى مش هيجيب غير نتيجة واحده من الجمب اللى فيه نتيجتين عشان فيه لفة ضاعت مع الجمب الفاضى فقلت الففه الضعف
    $realCountOther = 0;
    for ($i = 0; $i < ($totalCountOther * 2); $i++) {
        if (empty($otherAssetsArr[$otherAssetsItr]) && empty($otherOpponentsArr[$otherOpponentsItr])) {
            //do nothing
        } else {
            $realCountOther++;
            if ($i % 2 == 0) {
                if (empty($otherAssetsArr[$otherAssetsItr])) {
                    array_push($otherArr, $accountsTree);
                } else {
                    array_push($otherArr, $otherAssetsArr[$otherAssetsItr]);
                    $otherAssetsItr++;
                }
            } else {
                if (empty($otherOpponentsArr[$otherOpponentsItr])) {
                    array_push($otherArr, $accountsTree);
                } else {
                    array_push($otherArr, $otherOpponentsArr[$otherOpponentsItr]);
                    $otherOpponentsItr++;
                }
            }
        }
    }

    $smarty->assign("realCountOther", $realCountOther);
    $smarty->assign("otherArr", $otherArr);

    ###############
    //prepare Circulating array
    $circulatingArr = array();

    $totalCountCirculating = $circulatingAssetsCount + $circulatingOpponentsCount;

    $accountsTree = new Accountstree();
    $circulatingAssetsItr = 0;
    $circulatingOpponentsItr = 0;
    //ليه  $totalCountFixed * 2
    //لوفيه جمب فيه نتيجتين وجمب فاضى مش هيجيب غير نتيجة واحده من الجمب اللى فيه نتيجتين عشان فيه لفة ضاعت مع الجمب الفاضى فقلت الففه الضعف
    $realCountCirculating = 0;
    for ($i = 0; $i < ($totalCountCirculating * 2); $i++) {
        if (empty($circulatingAssetsArr[$circulatingAssetsItr]) && empty($circulatingOpponentsArr[$circulatingOpponentsItr])) {
            //do nothing
        } else {
            $realCountCirculating++;
            if ($i % 2 == 0) {
                if (empty($circulatingAssetsArr[$circulatingAssetsItr])) {
                    array_push($circulatingArr, $accountsTree);
                } else {
                    array_push($circulatingArr, $circulatingAssetsArr[$circulatingAssetsItr]);
                    $circulatingAssetsItr++;
                }
            } else {
                if (empty($circulatingOpponentsArr[$circulatingOpponentsItr])) {
                    array_push($circulatingArr, $accountsTree);
                } else {
                    array_push($circulatingArr, $circulatingOpponentsArr[$circulatingOpponentsItr]);
                    $circulatingOpponentsItr++;
                }
            }
        }
    }

    $smarty->assign("realCountCirculating", $realCountCirculating);
    $smarty->assign("circulatingArr", $circulatingArr);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

//here the smarty templates
    $smarty->display("thebalancesheetview/add.html");
} elseif ($do == "sucess") {
//here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
//here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */

function display_childrenExcept($parent) {
    global $accountsTreeDAO;
    global $accountsTreeEX;
    global $myArray;
    global $exceptArr;
    global $sum;
    $result = $accountsTreeEX->queryByParentExt($parent);
    if (count($result) > 0) {

        foreach ($result as $type) {
            if (!in_array($type->id, $exceptArr)) {
//check for expenses for this category

                $childData = $accountsTreeDAO->queryByParent($type->id);
                //print_r($childData);
                if (count($childData) > 0) {
                    foreach ($childData as $value) {
                        array_push($exceptArr, $value->id);
                    }
                }
                if (count($childData) > 0) {
                    $sum = 0;
                    getTheTrueValue($type->id, $sum);
                    $type->theValue = $sum;
                    if ($type->theValue != 0 || $type->id == 19) {
                        array_push($myArray, $type);
                    }
                } else {

                    //if ($type->itemtype2 == 1) {
                    if (!in_array($type->id, $exceptArr)) {
                        if ($type->theValue != 0 || $type->id == 19) {
                            array_push($myArray, $type);
                        }
                    }
                }

                if (count($childData) > 0) {
                    display_childrenExcept($type->id);
                }
            }
        }
    }
}

function getTheTrueValue($parent, $sum) {
    global $accountsTreeDAO;
    global $accountsTreeEX;
    global $sum;

    $result = $accountsTreeEX->queryByParentExt($parent);
    if (count($result) > 0) {
        foreach ($result as $type) {
            $sum += $type->theValue;
//check for expenses for this category
            $childData = $accountsTreeDAO->queryByParent($type->id);
            if (count($childData) > 0) {
                getTheTrueValue($type->id, $sum);
            }
        }
    }
}

function getStartGoodsVal() {
    //Storedetail
    global $storeDetailEX;

    $productsTotalValue = 0;
    $storeData = loadStore();
    foreach ($storeData as $value) {
        $products = $storeDetailEX->queryWithStoreId($value->storeId);
        foreach ($products as $pro) {
            $productsTotalValue += $pro->productquantity * $pro->productBuyPrice;
        }
    }
    $productsTotalValue = round($productsTotalValue, 2);
    return $productsTotalValue;
}

//select all store data
function loadStore() {
//to use the variable out side the funcion
    global $storeDAO;

//load all store data
    $storeData = $storeDAO->queryByConditions(0);
    return $storeData;
}

function orderAsTree($parent, $level) {
    global $accountsTreeDAO;
    global $accountsTreeEX;
    global $accountsTreeOrdered;
    //global $outputString;
    // retrieve all children of $parent
    $result = $accountsTreeEX->queryByParentExt($parent);
    if (count($result) > 0) {
        // display each child
        foreach ($result as $type) {
            $type->notes = $level;
            $type->theValue = (float) $type->theValue;
            array_push($accountsTreeOrdered, $type);
            orderAsTree($type->id, $level + 1);
        }
    }
}

?>