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/sellreportpricetype.php
<?php

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

// get the config file
include_once("../public/config.php");

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


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

//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');


//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');
//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');
//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');


//Billsproduct
require_once('../models/dao/BillsproductsDAO.class.php');
require_once('../models/dto/Billsproduct.class.php');
require_once('../models/mysql/BillsproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsproductsMySqlExtDAO.class.php');
//Billsreturnproduct
require_once('../models/dao/BillsreturnproductsDAO.class.php');
require_once('../models/dto/Billsreturnproduct.class.php');
require_once('../models/mysql/BillsreturnproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsreturnproductsMySqlExtDAO.class.php');

/////////////////////product////////////////////////
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');

//Unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.php');
//Productunit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.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');




$do = $_GET['do'];

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

  Controller Name :- storedetailCTRL تقرير مخزون اول مدة

  OPERTATION in Controller

  1-load save data
  2-display show form

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

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

//here goes the instances and general variables
//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//Sellbilldetail
$sellbilldetail = new Sellbilldetail();
$sellbilldetailDAO = new SellbilldetailMySqlDAO();
$sellbilldetailEX = new SellbilldetailMySqlExtDAO();
//Returnsellbilldetail
$returnSellBillDetail = new Returnsellbilldetail();
$returnSellBillDetailDAO = new ReturnsellbilldetailMySqlDAO();
$returnSellBillDetailEX = new ReturnsellbilldetailMySqlExtDAO();
//Sellandruternbilldetail
$sellAndRuternBillDetail = new Sellandruternbilldetail();
$sellAndRuternBillDetailDAO = new SellandruternbilldetailMySqlDAO();
$sellAndRuternBillDetailEX = new SellandruternbilldetailMySqlExtDAO();
##################################
//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();
//billsproducts
$billsProducts = new Billsproduct();
$billsProductsDAO = new BillsproductsMySqlDAO();
$billsProductsEX = new BillsproductsMySqlExtDAO();

//bill
$billsReturn = new Billsreturn();
$billsReturnDAO = new BillsreturnMySqlDAO();
$billsReturnEX = new BillsreturnMySqlExtDAO();
//billsReturnproducts
$billsReturnProducts = new Billsreturnproduct();
$billsReturnProductsDAO = new BillsreturnproductsMySqlDAO();
$billsReturnProductsEX = new BillsreturnproductsMySqlExtDAO();

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

///////////////product///////////////
$ProductDAO = new ProductMySqlDAO();
$Product = new Product();
$ProductEX = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();


//Unit
$unitDAO = new UnitMySqlDAO();
$unitEX = new UnitMySqlExtDAO();
//Productunit
$productUnit = new Productunit();
$productUnitDAO = new ProductunitMySqlDAO();
$productUnitEX = new ProductunitMySqlExtDAO();

$userGroup = new Usergroup();
$userGroupDAO = new UsergroupMySqlDAO();
$userGroupEX = new UsergroupMySqlExtDAO();

$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();


$lastLevelCatIDS = array();
$catsIDS = "";
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $storeData = $myStoreRecord->queryByConditions(0);
    $smarty->assign("storeData", $storeData);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    ##########################################################################
    //prepare search tools
    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);
    $userGroup = $userGroupDAO->load($_SESSION['usergroupid']);
    $smarty->assign("userGroup", $userGroup);
    $allClients = $clientDAO->queryAll();
    $smarty->assign("allClients", $allClients);
    ###########################################################################
    //search
    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');
    $pricetype = filter_input(INPUT_POST, 'pricetype');
    $order = filter_input(INPUT_POST, 'order');
    $clientid = filter_input(INPUT_POST, 'clientid');
    $storeId = filter_input(INPUT_POST, 'storeId');

    if ($order != "asc" && $order != "desc") {
        $order = "no";
    }

    $level = filter_input(INPUT_POST, 'level');
    $productCatId = filter_input(INPUT_POST, 'productCatId' . $level);
    if (empty($productCatId) || $productCatId == -1) {
        $productCatId = filter_input(INPUT_POST, 'productCatId' . ($level - 1));
    }
    $productId = filter_input(INPUT_POST, 'productId');
    $searchtype = filter_input(INPUT_POST, 'searchtype');

    $isOptic = filter_input(INPUT_POST, 'proIsOptic');
    if (!isset($isOptic) || empty($isOptic)) {
        $isOptic = 0;
    }
    if ($isOptic == 2 && $searchtype == 1) {
        $productCatId = $productId;
    }



    $queryString = " where 1 "; //bills
    $queryStringR = " where 1 "; //billsreturn
    $queryString1 = " where 1 "; //sellbill
    $queryString1R = " where 1 "; //sellbillreturn
    $queryString1SR = " where 1 "; //sellbillandreturn
    ##date
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString .= 'and  date(bills.billdate)  >= "' . $datefrom . '" ';
        $queryStringR .= 'and  date(billsreturn.`date`)  >= "' . $datefrom . '" ';
        $queryString1 .= 'and  sellbill.sellbilldate  >= "' . $datefrom . '" ';
        $queryString1R .= 'and  returnsellbill.returnsellbilldate  >= "' . $datefrom . '" ';
        $queryString1SR .= 'and  sellbillandrutern.sellbilldate  >= "' . $datefrom . '" ';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString .= 'and  date(bills.billdate)  <= "' . $dateto . '" ';
        $queryStringR .= 'and   date(billsreturn.`date`)  <= "' . $dateto . '" ';
        $queryString1 .= 'and  sellbill.sellbilldate  <= "' . $dateto . '" ';
        $queryString1R .= 'and  returnsellbill.returnsellbilldate  <= "' . $dateto . '" ';
        $queryString1SR .= 'and  sellbillandrutern.sellbilldate  <= "' . $dateto . '" ';
    }
    ##pricetype
//    print_r('<pre>');
//    print_r('$pricetype = ' . $pricetype);
//    print_r('<pre>');

    if (isset($pricetype) && $pricetype != -1) {
        $queryString .= 'and  bills.pricetype = ' . $pricetype . ' ';
        $queryStringR .= 'and bills.pricetype = ' . $pricetype . ' ';
        $queryString1 .= 'and  sellbilldetail.pricetype = ' . $pricetype . ' ';
        $queryString1R .= 'and  returnsellbilldetail.pricetype = ' . $pricetype . ' ';
        $queryString1SR .= 'and  sellandruternbilldetail.pricetype = ' . $pricetype . ' ';
    }
//    print_r('<pre>');
//    print_r($queryString1);
//    print_r('<pre>');
    ##client
    if (isset($clientid) && !empty($clientid) && $clientid != -1) {
        $queryString .= 'and  bills.clientid = ' . $clientid . ' ';
        $queryStringR .= 'and bills.clientid = ' . $clientid . ' ';
        $queryString1 .= 'and  sellbill.sellbillclientid = ' . $clientid . ' ';
        $queryString1R .= 'and  returnsellbill.returnsellbillclientid = ' . $clientid . ' ';
        $queryString1SR .= 'and  sellbillandrutern.sellbillclientid = ' . $clientid . ' ';
    }


    ##productId
    if (isset($productId) && !empty($productId) && $productId != -1) {
        if ($isOptic == 2) {
            $productsOfCat = $ProductEX->queryByProductCatIdIn($productId);
            $productId = '0';
            foreach ($productsOfCat as $value) {
                $productId .= ',' . $value->productId;
            }
        }
        $queryString .= 'and  billsproducts.productid  in( ' . $productId . ' ) ';
        $queryStringR .= 'and  billsreturnproducts.productid  in( ' . $productId . ' ) ';
        $queryString1 .= 'and  sellbilldetail.sellbilldetailproductid  in( ' . $productId . ' ) ';
        $queryString1R .= 'and  returnsellbilldetail.returnsellbilldetailproductid  in( ' . $productId . ' ) ';
        $queryString1SR .= 'and  sellandruternbilldetail.sellbilldetailproductid  in( ' . $productId . ' ) ';
    } elseif (isset($productCatId) && !empty($productCatId) && $productCatId != -1) {
        ##get subcat of cat
        $catsIDS = '' . $productCatId;
        getAllSubCat($productCatId, 1); //mode = 1 get all sub cats
        $productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS);
        $IDS = '0';
        foreach ($productsOfCat as $value) {
            $IDS .= ',' . $value->productId;
        }

        $queryString .= 'and  billsproducts.productid  in (' . $IDS . ') ';
        $queryString1 .= 'and  sellbilldetail.sellbilldetailproductid  in (' . $IDS . ') ';
        $queryStringR .= 'and  billsreturnproducts.productid   in (' . $IDS . ') ';
        $queryString1R .= 'and  returnsellbilldetail.returnsellbilldetailproductid  in (' . $IDS . ') ';
        $queryString1SR .= 'and  sellandruternbilldetail.sellbilldetailproductid  in (' . $IDS . ') ';
    }


    if (isset($storeId) && !empty($storeId) && $storeId != -1) {
        $queryString .= ' and storeid = ' . $storeId . ' ';
        $queryString1 .= ' and storeid = ' . $storeId . ' ';
        //$queryStringR .= ' and storeid = 1 ';
        $queryString1R .= ' and storeid = ' . $storeId . ' ';
        $queryString1SR .= ' and storeid = ' . $storeId . ' ';
    }

    getData($queryString, $queryString1, $queryStringR, $queryString1R, $queryString1SR, $order);

    $smarty->assign("salesreport", 1);
    //here the smarty templates
    $smarty->display("sellreportpricetypeview/show.html");

    //$smarty->assign("settlementstoreshow", 1);
}
//here the global templates
$smarty->display("footer.html");

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

function getData($queryString, $queryString1, $queryStringR, $queryString1R, $queryString1SR, $order) {
    global $billsProductsEX;
    global $billsDAO;
    global $billsEX;
    global $billsReturnDAO;
    global $billsReturnProductsEX;
    global $sellbilldetailEX;
    global $returnSellBillDetailEX;
    global $sellAndRuternBillDetailEX;
    global $ProductDAO;
    global $productCatDAO;
    global $ProductEX;
    global $smarty;
    global $lastLevelCatIDS;
    global $catsIDS;
    global $productUnitEX;
    global $unitEX;

    class productUnitData {

        public $productId;
        public $productName;
        public $unitId;
        public $unitName;
        public $amount = 0;
        public $price = 0;
        public $pricetype;

    }

    $AllDataIndexArr = array();
    $unitTotal = array();
    $allDataArr = array();
    global $allDataArr;
    ##if no search make it today
    if ($queryString == " where 1 ") {
        ##date
        $today = date("Y-m-d");
        $queryString .= 'and  date(bills.billdate)  = "' . $today . '" ';
        $queryStringR .= 'and  date(billsreturn.`date`)  = "' . $today . '" ';
        $queryString1 .= 'and  sellbill.sellbilldate  = "' . $today . '" ';
        $queryString1R .= 'and  returnsellbill.returnsellbilldate  = "' . $today . '" ';
        $queryString1SR .= 'and  sellbillandrutern.sellbilldate  = "' . $today . '" ';
    }
    $billsData = $billsProductsEX->queryAllGeneral($queryString);
    $billsDataReturn = $billsReturnProductsEX->queryAllGeneralPriceType($queryStringR);
//    print_r('<pre>');
//    print_r($queryString1);
//    print_r('<pre>');

    $sellBillData = $sellbilldetailEX->queryAllGeneralPriceType($queryString1);
    $sellBillDataReturn = $returnSellBillDetailEX->queryAllGeneralPriceType($queryString1R);
    $sellBillDataSellAndReturn = $sellAndRuternBillDetailEX->queryAllGeneralPriceType($queryString1SR);
    $resultsCount = count($billsData) + count($billsDataReturn) + count($sellBillData) + count($sellBillDataReturn) + count($sellBillDataSellAndReturn);
    $smarty->assign("resultsCount", $resultsCount);
    if ($resultsCount > 0) {
        ##unique products
        ##فاتورة البصريات
        foreach ($billsData as $value) {
            $productid = $value->productid;
            $value->producttotalprice = round($value->producttotalprice, 2);
            ##all products come in unit of one piece  ##get this unit
            $unitData = $productUnitEX->getUnitDataOfUnityWithProductId($productid);
            $unitid = 0;
            $unitname = 'وحدة';
            if (count($unitData) > 0) {
                $unitid = $unitData->unitid;
                $unitname = $unitData->conditions;
            }

            ##check if product is here
            if (isset($allDataArr[$productid])) {
                ##product exists
                ##check if unit is here
                if (in_array($unitid, $AllDataIndexArr[$productid])) {
                    ##unit exists
                    $key2 = array_search($unitid, $AllDataIndexArr[$productid]);
                    $myproduct = $allDataArr[$productid][$key2];
                    ## common data ##
                    $myproduct->price += $value->producttotalprice;
                    $myproduct->amount += $value->productno;
                    #################
                } else {
                    ##new unit
                    $myproduct = new productUnitData();
                    $myproduct->unitId = $unitid;
                    $myproduct->unitName = $unitname;
                    ## common data ##
                    $myproduct->productId = '';
                    $myproduct->productName = '';
                    $myproduct->price += $value->producttotalprice;
                    $myproduct->amount += $value->productno;
                    #################
                    array_push($allDataArr[$productid], $myproduct);
                    array_push($AllDataIndexArr[$productid], $unitid);
                }
            } else {
                ##new product&& new unit
                $myproduct = new productUnitData();
                $myproduct->unitId = $unitid;
                $myproduct->unitName = $unitname;
                ## common data ##
                $myproduct->productId = $value->productid;
                $myproduct->productName = $value->productName;
                $myproduct->price = $value->producttotalprice;
                $myproduct->amount = $value->productno;
                #################
                //handle indexes
                $allDataArr[$productid] = array();
                array_push($allDataArr[$productid], $myproduct);
                $AllDataIndexArr[$productid] = array();
                array_push($AllDataIndexArr[$productid], $unitid);
            }

            if (!isset($unitTotal[$myproduct->unitName])) {
                $unitTotal[$myproduct->unitName] = 0;
            }
            $unitTotal[$myproduct->unitName] += $value->productno;
        }

        ##فاتورة البصريات مردود
        foreach ($billsDataReturn as $value) {
            $productid = $value->productid;
            $value->producttotalprice = round($value->producttotalprice, 2);
            ##all products come in unit of one piece  ##get this unit
            $unitData = $productUnitEX->getUnitDataOfUnityWithProductId($productid);
            $unitid = 0;
            $unitname = 'وحدة';
            if (count($unitData) > 0) {
                $unitid = $unitData->unitid;
                $unitname = $unitData->conditions;
            }

            ##check if product is here
            if (isset($allDataArr[$productid])) {
                ##product exists
                ##check if unit is here
                if (in_array($unitid, $AllDataIndexArr[$productid])) {
                    ##unit exists
                    $key2 = array_search($unitid, $AllDataIndexArr[$productid]);
                    $myproduct = $allDataArr[$productid][$key2];
                    ## common data ##
                    $myproduct->price -= $value->producttotalprice;
                    $myproduct->amount -= $value->productno;
                    #################
                } else {
                    ##new unit
                    $myproduct = new productUnitData();
                    $myproduct->unitId = $unitid;
                    $myproduct->unitName = $unitname;
                    ## common data ##
                    $myproduct->productId = '';
                    $myproduct->productName = '';
                    $myproduct->price -= $value->producttotalprice;
                    $myproduct->amount -= $value->productno;
                    #################
                    array_push($allDataArr[$productid], $myproduct);
                    array_push($AllDataIndexArr[$productid], $unitid);
                }
            } else {
                ##new product&& new unit
                $myproduct = new productUnitData();
                $myproduct->unitId = $unitid;
                $myproduct->unitName = $unitname;
                ## common data ##
                $myproduct->productId = $value->productid;
                $myproduct->productName = $value->productName;
                $myproduct->price = $value->producttotalprice * -1;
                $myproduct->amount = $value->productno * -1;
                #################
                //handle indexes
                $allDataArr[$productid] = array();
                array_push($allDataArr[$productid], $myproduct);
                $AllDataIndexArr[$productid] = array();
                array_push($AllDataIndexArr[$productid], $unitid);
            }

            if (!isset($unitTotal[$myproduct->unitName])) {
                $unitTotal[$myproduct->unitName] = 0;
            }
            $unitTotal[$myproduct->unitName] -= $value->productno;
        }

        ##فاتورة المبيعات
        foreach ($sellBillData as $value) {

            $productid = $value->sellbilldetailproductid;
            $unitid = $value->unitid;
            $unitname = $value->unitName;

            $quantity = $value->sellbilldetailquantity;
            $value->sellbilldetailtotalprice = round($value->sellbilldetailtotalprice, 2);
            ##############
            ##check if product is here
            if (isset($allDataArr[$productid])) {
                ##product exists
                ##check if unit is here
                if (in_array($unitid, $AllDataIndexArr[$productid])) {
                    ##unit exists
                    $key2 = array_search($unitid, $AllDataIndexArr[$productid]);
                    $myproduct = $allDataArr[$productid][$key2];
                    ## common data ##
                    $myproduct->price += $value->sellbilldetailtotalprice;
                    $myproduct->amount += $quantity;
                    #################
                } else {
                    ##new unit
                    $myproduct = new productUnitData();
                    $myproduct->unitId = $unitid;
                    $myproduct->unitName = $unitname;
                    ## common data ##
                    $myproduct->productId = '';
                    $myproduct->productName = '';
                    $myproduct->pricetype = $value->pricetype;
                    $myproduct->price += $value->sellbilldetailtotalprice;
                    $myproduct->amount += $quantity;
                    #################
                    array_push($allDataArr[$productid], $myproduct);
                    array_push($AllDataIndexArr[$productid], $unitid);
                }
            } else {
                ##new product&& new unit
                $myproduct = new productUnitData();
                $myproduct->unitId = $unitid;
                $myproduct->unitName = $unitname;
                ## common data ##
                $myproduct->productId = $productid;
                $myproduct->productName = $value->productName;
                $myproduct->price = $value->sellbilldetailtotalprice;
                $myproduct->amount = $quantity;
                $myproduct->pricetype = $value->pricetype;

                #################
                //handle indexes
                $allDataArr[$productid] = array();
                array_push($allDataArr[$productid], $myproduct);
                $AllDataIndexArr[$productid] = array();
                array_push($AllDataIndexArr[$productid], $unitid);
            }
            if (!isset($unitTotal[$myproduct->unitName])) {
                $unitTotal[$myproduct->unitName] = 0;
            }
            $unitTotal[$myproduct->unitName] += $quantity;
        }

        ##فاتورة مردود المبيعات
        foreach ($sellBillDataReturn as $value) {
            $productid = $value->returnsellbilldetailproductid;
            $unitid = $value->unitid;
            $unitname = $value->unitName;
            $quantity = $value->returnsellbilldetailquantity;
            $value->returnsellbilldetailtotalprice = round($value->returnsellbilldetailtotalprice, 2);
            ##############
            ##check if product is here
            if (isset($allDataArr[$productid])) {
                ##product exists
                ##check if unit is here
                if (in_array($unitid, $AllDataIndexArr[$productid])) {
                    ##unit exists
                    $key2 = array_search($unitid, $AllDataIndexArr[$productid]);
                    $myproduct = $allDataArr[$productid][$key2];
                    ## common data ##
                    $myproduct->price -= $value->returnsellbilldetailtotalprice;
                    $myproduct->amount -= $quantity;
                    #################
                } else {
                    ##new unit
                    $myproduct = new productUnitData();
                    $myproduct->unitId = $unitid;
                    $myproduct->unitName = $unitname;
                    ## common data ##
                    $myproduct->productId = '';
                    $myproduct->productName = '';
                    $myproduct->price -= $value->returnsellbilldetailtotalprice;
                    $myproduct->amount -= $quantity;
                    #################
                    array_push($allDataArr[$productid], $myproduct);
                    array_push($AllDataIndexArr[$productid], $unitid);
                }
            } else {
                ##new product&& new unit
                $myproduct = new productUnitData();
                $myproduct->unitId = $unitid;
                $myproduct->unitName = $unitname;
                ## common data ##
                $myproduct->productId = $productid;
                $myproduct->productName = $value->productName;
                $myproduct->price = $value->returnsellbilldetailtotalprice * -1;
                $myproduct->amount = $quantity * -1;
                #################
                //handle indexes
                $allDataArr[$productid] = array();
                array_push($allDataArr[$productid], $myproduct);
                $AllDataIndexArr[$productid] = array();
                array_push($AllDataIndexArr[$productid], $unitid);
            }
            if (!isset($unitTotal[$myproduct->unitName])) {
                $unitTotal[$myproduct->unitName] = 0;
            }
            $unitTotal[$myproduct->unitName] -= $quantity;
        }

        ##فاتورة المبيعات والمردود
        foreach ($sellBillDataSellAndReturn as $value) {
            $productid = $value->sellbilldetailproductid;
            $unitid = $value->unitid;
            $unitname = $value->unitName;
            $quantity = $value->sellbilldetailquantity;
            $billType = $value->selltype;
            $value->sellbilldetailtotalprice = round($value->sellbilldetailtotalprice, 2);
            ##############
            ##check if product is here
            if (isset($allDataArr[$productid])) {
                ##product exists
                ##check if unit is here
                if (in_array($unitid, $AllDataIndexArr[$productid])) {
                    ##unit exists
                    $key2 = array_search($unitid, $AllDataIndexArr[$productid]);
                    $myproduct = $allDataArr[$productid][$key2];
                    ## common data ##
                    if ($billType == 0) {
                        $myproduct->price += $value->sellbilldetailtotalprice;
                        $myproduct->amount += $quantity;
                    } else {
                        $myproduct->price -= $value->sellbilldetailtotalprice;
                        $myproduct->amount -= $quantity;
                    }
                    #################
                } else {
                    ##new unit
                    $myproduct = new productUnitData();
                    $myproduct->unitId = $unitid;
                    $myproduct->unitName = $unitname;
                    ## common data ##
                    $myproduct->productId = '';
                    $myproduct->productName = '';
                    if ($billType == 0) {
                        $myproduct->price += $value->sellbilldetailtotalprice;
                        $myproduct->amount += $quantity;
                    } else {
                        $myproduct->price -= $value->sellbilldetailtotalprice;
                        $myproduct->amount -= $quantity;
                    }
                    #################
                    array_push($allDataArr[$productid], $myproduct);
                    array_push($AllDataIndexArr[$productid], $unitid);
                }
            } else {
                ##new product&& new unit
                $myproduct = new productUnitData();
                $myproduct->unitId = $unitid;
                $myproduct->unitName = $unitname;
                ## common data ##
                $myproduct->productId = $productid;
                $myproduct->productName = $value->productName;
                if ($billType == 0) {
                    $myproduct->price += $value->sellbilldetailtotalprice;
                    $myproduct->amount += $quantity;
                } else {
                    $myproduct->price -= $value->sellbilldetailtotalprice;
                    $myproduct->amount -= $quantity;
                }
                #################
                //handle indexes
                $allDataArr[$productid] = array();
                array_push($allDataArr[$productid], $myproduct);
                $AllDataIndexArr[$productid] = array();
                array_push($AllDataIndexArr[$productid], $unitid);
            }
            if (!isset($unitTotal[$myproduct->unitName])) {
                $unitTotal[$myproduct->unitName] = 0;
            }
            if ($billType == 0) {
                $unitTotal[$myproduct->unitName] += $quantity;
            } else {
                $unitTotal[$myproduct->unitName] -= $quantity;
            }
        }

        ##finally get total foreach project
        $productTotalArr = array();
        global $productTotalArr;
        $totalOfTotals = 0;
        foreach ($allDataArr as $data) {
            $prototal = 0;
            $proid = 0;
            $i = 0;
            foreach ($data as $product) {
                $prototal += $product->price;
                if ($i == 0) {
                    $proid = $product->productId;
                }
                $i++;
            }

            $productTotalArr[$proid] = $prototal;
            $totalOfTotals += $prototal;
        }

        if ($order == "asc" || $order == "desc") {
            $allDataArr = sortByTotal($order, $allDataArr, $productTotalArr);
        }
    }

    $smarty->assign("unitTotal", $unitTotal);
    $smarty->assign("totalOfTotals", $totalOfTotals);
    $smarty->assign("productTotalArr", $productTotalArr);

    foreach ($allDataArr as $val) {

        $product_id = $val[0]->productId;

        $full_product_name = get_parents_of_products($product_id);

        $val[0]->productName = $full_product_name;
    }

//    print_r('<pre>');
//    print_r($allDataArr);
//    print_r('<pre>');

    $smarty->assign("allDataArr", $allDataArr);
}

//sort key value pair
function sortByTotal($type, $allDataArrTemp, $productTotalArrTemp) {
    $allDataArr = array();
    //1-sort by date
    foreach ($productTotalArrTemp as $key => $value) {
        if ($type == "asc") {
            $minTotalIndex = array_keys($productTotalArrTemp, min($productTotalArrTemp));
            $minTotalIndex = $minTotalIndex[0];
            unset($productTotalArrTemp[$minTotalIndex]);
            $allDataArr[$minTotalIndex] = $allDataArrTemp[$minTotalIndex];
        } elseif ($type == "desc") {
            $maxTotalIndex = array_keys($productTotalArrTemp, max($productTotalArrTemp));
            $maxTotalIndex = $maxTotalIndex[0];
            unset($productTotalArrTemp[$maxTotalIndex]);
            $allDataArr[$maxTotalIndex] = $allDataArrTemp[$maxTotalIndex];
        }
    }

    return $allDataArr;
}

function getAllSubCat($catid, $mode) {
    global $productCatExt;
    global $productCatDAO;
    global $catsIDS;
    global $lastLevelCatIDS;
    //mode = 1 get all sub cats
    //mode = 2 get last level cats

    $result = $productCatExt->queryByParentExt($catid);
    if (count($result) > 0) {
        foreach ($result as $data) {
            if ($mode == 1) {
                $catsIDS .= ", " . $data->productCatId;
                getAllSubCat($data->productCatId, $mode);
            } elseif ($mode == 2) {
                $childData = $productCatExt->queryByParentExt($data->productCatId);
                if (count($childData) > 0) {
                    getAllSubCat($data->productCatId, $mode);
                } else {
                    if (!empty($data->productCatId)) {
                        array_push($lastLevelCatIDS, $data->productCatId);
                    }
                }
            }
        }
    } else {
//        if ($mode == 2) {
//            $childData = $productCatExt->queryByParentExt($data->productCatId);
//            if (count($childData) > 0) {
//                getAllSubCat($data->productCatId, $mode);
//            } else {
//                if (!empty($data->productCatId)) {
//                    array_push($lastLevelCatIDS, $data->productCatId);
//                }
//            }
//        }
    }
}

//# pass $product_id to get parent of this product . M7md
function get_parents_of_products($product_id) {

    global $ProductDAO;


    $product_data = $ProductDAO->load($product_id);


    $product_name = $product_data->productName;
    $productCatId = $product_data->productCatId;

    $parent_name = '';
    $parents_name = get_parents($productCatId, $parent_name);

    $full_product_name = $parents_name . $product_name;

    return $full_product_name;
}

//# this function used to get productcat id and check its parent if = 0 or not . M7md
function get_parents($productCatId, $parent_name) {

    global $productCatDAO;

    $product_cat_data = $productCatDAO->load($productCatId);

    $parent_name = $product_cat_data->productCatName . ' / ' . $parent_name;

    $productCatParent = $product_cat_data->productCatParent;

    //# check if $productCatParent > 0 to get parent of parent .
    if ($productCatParent != 0) {

        $product_cat_data_parent = $productCatDAO->load($productCatParent);


        $parent_name = $product_cat_data_parent->productCatName . ' / ' . $parent_name;

        $productCatParent_parent = $product_cat_data_parent->productCatParent;

        if ($productCatParent_parent != 0) {

            get_parents($productCatParent_parent, $parent_name);
        }
    }

    return $parent_name;
    //print_r('<br> '.$parent_name);
}

?>