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/tradingreport.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
//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    $debtorArr2 = array();
    $creditorArr2 = array();
    $totalDebtor2 = 0;
    $totalCreditor2 = 0;
    //بضاعة اول المدة
//    $accountsTree = $accountsTreeDAO->load(19);
//    $startGoodsValTreeObj = $accountsTree;
//    $startGoodsVal = $accountsTree->theValue;
//    $smarty->assign("startGoodsVal", $startGoodsVal);
    //بضاعة اخر المدة
    //نحسب قيمة البضاعة حاليا
    $endGoodsVal = getStartGoodsVal();
    $endGoodsValTreeObj = new Accountstree();
    $endGoodsValTreeObj->name = 'بضاعة اخر المدة';
    $endGoodsValTreeObj->customName = 'بضاعة اخر المدة';
    $endGoodsValTreeObj->theValue = $endGoodsVal;
    $smarty->assign("endGoodsVal", $endGoodsVal);

    //كل المصروفات
    $myArray = array();
//    array_push($myArray, $startGoodsValTreeObj);
    display_childrenExcept(158);
    foreach ($myArray as $value) {
        array_push($debtorArr2, $value);
        $totalDebtor2 += $value->theValue;
    }
    //كل الايرادات
    $myArray = array();
    array_push($myArray, $endGoodsValTreeObj);
    display_childrenExcept(140);
    foreach ($myArray as $value) {
        array_push($creditorArr2, $value);
        $totalCreditor2 += $value->theValue;
    }

    $smarty->assign("totalDebtor", $totalDebtor2);
    $smarty->assign("totalCreditor", $totalCreditor2);


    //prepare final array
    $reportArr = array();
    $creditorCount = count($creditorArr2);
    $debtorCount = count($debtorArr2);
    $totalCount = $creditorCount + $debtorCount;

    $accountsTree = new Accountstree();
    $debtorItr = 0;
    $creditorItr = 0;

    //ليه  $totalCountFixed * 2
    //لوفيه جمب فيه نتيجتين وجمب فاضى مش هيجيب غير نتيجة واحده من الجمب اللى فيه نتيجتين عشان فيه لفة ضاعت مع الجمب الفاضى فقلت الففه الضعف
    $realCount = 0;
    for ($i = 0; $i < ($totalCount * 2); $i++) {
        if (empty($debtorArr2[$debtorItr]) && empty($creditorArr2[$creditorItr])) {
            //do no thing
        } else {
            $realCount++;
            if ($i % 2 == 0) {
                if (empty($debtorArr2[$debtorItr])) {
                    array_push($reportArr, $accountsTree);
                } else {
                    array_push($reportArr, $debtorArr2[$debtorItr]);
                    $debtorItr++;
                }
            } else {
                if (empty($creditorArr2[$creditorItr])) {
                    array_push($reportArr, $accountsTree);
                } else {
                    array_push($reportArr, $creditorArr2[$creditorItr]);
                    $creditorItr++;
                }
            }
        }
    }
    $smarty->assign("realCount", $realCount);
    $smarty->assign("reportArr", $reportArr);


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

    $netvalue = $totalCreditor2 - $totalDebtor2;
    $gainLossMsg;
    if ($netvalue >= 0) {
        $gainLossMsg = 'اجمالى الربح : ' . $netvalue;
    } elseif ($netvalue < 0) {
        $netvalue2 = abs($netvalue2);
        $gainLossMsg = 'اجمالى الخسارة : ' . $netvalue;
    }
    $smarty->assign("gainLossMsg", $gainLossMsg);

    //here the smarty templates
    $smarty->display("tradingreportview/add.html");
}
//if (empty($do)) {
//    //here the permssion check
//    include_once("../public/authentication.php");
//    ##
//
//    /* ---------------------------------------------------------------------- */
//    //way1
//    /* //تكلفة مبيعات
//      $salesCost = $accountsTreeDAO->load(32);//not in tree تكلفة مبيعات
//      $smarty->assign("salesCost", $salesCost->theValue);
//      //تكلفة مردودات مبيعات
//      $salesReturnCost = $accountsTreeDAO->load(33);//not in tree تكلفة مردودات مبيعات
//      $smarty->assign("salesReturnCost", $salesReturnCost->theValue); */
//    ############### this part is comon
//    //اجمالى المبيعات
//    $sales = $accountsTreeDAO->load(16);
//    $smarty->assign("sales", $sales->theValue);
//    //اجمالى مردودات المبيعات
//    $salesReturn = $accountsTreeDAO->load(13);
//    $smarty->assign("salesReturn", $salesReturn->theValue);
//    ###############
//
//    $totalDeptor1 = $salesCost->theValue - $salesReturnCost->theValue;
//    $totalCreditorCommon = $sales->theValue - $salesReturn->theValue;
//
//    $netVal1 = $totalCreditorCommon - $totalDeptor1;
//    $smarty->assign("totalDeptor1", $totalDeptor1);
//    $smarty->assign("totalCreditorCommon", $totalCreditorCommon);
//    $smarty->assign("netVal1", $netVal1);
//    if ($netVal1 >= 0) {
//        $gainLossMsg1 = 'اجمالى الربح : ' . $netVal1;
//    } elseif ($netVal1 < 0) {
//        $netVal1 = abs($netVal1);
//        $gainLossMsg1 = 'اجمالى الخسارة : ' . $netVal1;
//    }
//    $smarty->assign("gainLossMsg1", $gainLossMsg1);
//    /* ---------------------------------------------------------------------- */
//    //way2
//    //بضاعة اول المدة
//    $accountsTree = $accountsTreeDAO->load(19);
//    $startGoodsVal = $accountsTree->theValue;
//    $smarty->assign("startGoodsVal", $startGoodsVal);
//
//    //بضاعة اخر المدة
//    //نحسب قيمة البضاعة حاليا
//    $endGoodsVal = getStartGoodsVal();
//    $smarty->assign("endGoodsVal", $endGoodsVal);
//
//    //اجمالى المشتريات
//    $purchases = $accountsTreeDAO->load(12);
//    $smarty->assign("purchases", $purchases->theValue);
//    //اجمالى مردودات المشتريات
//    $purchasesReturn = $accountsTreeDAO->load(17);
//    $smarty->assign("purchasesReturn", $purchasesReturn->theValue);
//
//    $totalDeptor2 = $startGoodsVal + ($purchases->theValue - $purchasesReturn->theValue) - $endGoodsVal;
//    $netVal2 = $totalCreditorCommon - $totalDeptor2;
//    $smarty->assign("totalDeptor2", $totalDeptor2);
//    $smarty->assign("netVal2", $netVal2);
//
//    if ($netVal2 >= 0) {
//        $gainLossMsg2 = 'اجمالى الربح : ' . $netVal2;
//    } elseif ($netVal2 < 0) {
//        $netVal2 = abs($netVal2);
//        $gainLossMsg2 = 'اجمالى الخسارة : ' . $netVal2;
//    }
//    $smarty->assign("gainLossMsg2", $gainLossMsg2);
//    /* ---------------------------------------------------------------------- */
//
//
//    //here the smarty templates
//    $smarty->display("tradingreportview/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 $sum;
    $result = $accountsTreeEX->queryByParentExt($parent);
    if (count($result) > 0) {

        foreach ($result as $type) {
//check for expenses for this category

            $childData = $accountsTreeDAO->queryByParent($type->id);
            if (count($childData) > 0) {
                $sum = 0;
                getTheTrueValue($type->id, $sum);
                $type->theValue = $sum;
                array_push($myArray, $type);
            } else {
                array_push($myArray, $type);
            }
##to show the first level of childs only
##if i open it . this show all childs under parent
//            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;
}

?>