<?php

//the global file operation
session_start();
ob_start();

//global varable
global $showoutside;

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

//here the db files that include in the file
include("../public/include_dao.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');

//////////////////////////store detail///////////////
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');

/////////////////////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');

////////////////////sellbillandreturn/////////////////////////
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');

/////////////////////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');

//Dated Checked
require_once('../models/dao/DatedcheckedDAO.class.php');
require_once('../models/dto/Datedchecked.class.php');
require_once('../models/mysql/DatedcheckedMySqlDAO.class.php');
require_once('../models/mysql/ext/DatedcheckedMySqlExtDAO.class.php');


//get the do the action
$do = $_GET['do'];


//here goes the instances and general variables
//////////////////store///////////////////
$StoreDAO = new StoreMySqlDAO();
$Store = new Store();
$StoreEX = new StoreMySqlExtDAO();

///////////////////store detail//////////////
$StoredetailDAO = new StoredetailMySqlDAO();
$Storedetail = new Storedetail();
$StoredetailEX = new StoredetailMySqlExtDAO();

///////////////product///////////////
$ProductDAO = new ProductMySqlDAO();
$Product = new Product();
$ProductEX = new ProductMySqlExtDAO();

////////////////kempila//////////////////
$KempialaDAO = new KempialaMySqlDAO();
$Kempiala = new Kempiala();
$KempialaEX = new KempialaMySqlExtDAO();

$myProductcatEx = new ProductcatMySqlExtDAO();

////////////////sellbilland return///////////////

$ReturnsellbilldetailDAO = new ReturnsellbilldetailMySqlDAO();
$Returnsellbilldetail = new Returnsellbilldetail();
$ReturnsellbilldetailEX = new ReturnsellbilldetailMySqlExtDAO();


//////////////////////sellbilldetail///////////////
$SellbilldetailDAO = new SellbilldetailMySqlDAO();
$Sellbilldetail = new Sellbilldetail();
$SellbilldetailEX = new SellbilldetailMySqlExtDAO();

//Datedchecked
$DatedcheckedDAO = new DatedcheckedMySqlDAO();
$Datedchecked = new Datedchecked();
$DatedcheckedEX = new DatedcheckedMySqlExtDAO();

$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//client
$ClientDAO = new ClientMySqlDAO();
$Client = new Client();
$ClientEX = new ClientMySqlExtDAO();
$newclient = new Client();

$productRequest = new Productrequest();
$productRequestDAO = new ProductrequestsMySqlDAO();
$productRequestExt = new ProductrequestsMySqlExtDAO();

//bill
$bills = new Bill();
$billsDAO = new BillsMySqlDAO();
$billsEX = new BillsMySqlExtDAO();

//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();

//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

//other request
$otherRequest = new Otherrequest();
$otherRequestDAO = new OtherrequestMySqlDAO();
$otherRequestEX = new OtherrequestMySqlExtDAO();

//Toworkshoporder
$toWorkshopOrder = new Toworkshoporder();
$toWorkshopOrderDAO = new ToworkshoporderMySqlDAO();
$toWorkshopOrderEX = new ToworkshoporderMySqlExtDAO();

//paymentnetworks
$insuranceCompanyDAO = new InsurancecompaniesMySqlDAO();
if ($do == "show") {

    $allproduct = showallproductbystorename();
    //$allkempila= showallkempila();
    $allstore = loadstore();
    $storeid = loadstorename();
    $allsell = productnotsellbystorename();
    //$allcheck=showallcheck();

    $smarty->assign("allstore", $allstore);
    $smarty->assign("storeid", $storeid);
    $smarty->assign("allsell", $allsell);
    $smarty->assign("allproduct", $allproduct);
    //$smarty->assign("allkempila",$allkempila);
    //$smarty->assign("allcheck",$allcheck);
    $smarty->display("noticesview/show.html");
} elseif ($do == 'print_tab') {
    $tab = $_POST['tab'];
    $storeid = $_POST['supplierid'];
    $storename = $_POST['supplierid2']; //# this is store id which get from kempiala search

    //  echo ' i will print here my tab  $storeid = '.$storeid;
    switch ($tab) {
        case 'print_tab1':
            if ($storeid == '') {
                $allproduct = showallproductx2();
            } else {
                $allproduct = showallproductbystorename();
            }
            $smarty->assign("allproduct", $allproduct);
            $smarty->display("noticesview/print_tab1.html");
            break;

        case 'print_tab2':

            $allkempiladata = showallkempila();
            $allkempila = $allkempiladata[0];
            $smarty->assign("allkempila", $allkempila);


            $smarty->display("noticesview/print_tab2.html");
            break;

        case 'print_tab3':

            if (!empty($storename)) {
                $allnotselldata = productnotsellbystorename();
                $allsell = $allnotselldata;
                $smarty->assign("allsell", $allsell);
            } else {
                $allnotselldata = productnotsell();
                $allsell = $allnotselldata;
                $smarty->assign("allsell", $allsell);
            }
            $smarty->display("noticesview/print_tab3.html");
            break;

        case 'print_tab4':

            //clientcheck
            $clientcheckdata = checkforclient();
            $clientcheck = $clientcheckdata[0];
            $smarty->assign("clientcheck", $clientcheck);
            $smarty->display("noticesview/print_tab4.html");
            break;

        case 'print_tab5':

            //clientcheck
            //suppiercheck
            $suppiercheckdata = checkforsupplier();
            $suppiercheck = $suppiercheckdata[0];
            $smarty->assign("suppiercheck", $suppiercheck);
            $smarty->display("noticesview/print_tab5.html");
            break;

        case 'print_tab6_1':

            getProductRequest();
            $smarty->display("noticesview/print_tab6_1.html");
            break;
        case 'print_tab6_2':

            getProductRequest();
            $smarty->display("noticesview/print_tab6_2.html");
            break;
        case 'print_tab6_3':

            getProductRequest();
            $smarty->display("noticesview/print_tab6_3.html");
            break;
        case 'print_tab7_1':

            getOtherRequests();
            $smarty->display("noticesview/print_tab7_1.html");
            break;

        case 'print_tab7_2':

            getOtherRequests();
            $smarty->display("noticesview/print_tab7_2.html");
            break;
        case 'print_tab7_3':

            getOtherRequests();
            $smarty->display("noticesview/print_tab7_3.html");
            break;
        case 'print_tab8':

            getToWorkshopOrderNoticeInDetails();
            $smarty->display("noticesview/print_tab8.html");
            break;
        case 'print_tab9_1':

            getInsuranceBillNotif();
            $smarty->display("noticesview/print_tab9_1.html");
            break;
        case 'print_tab9_2':

            getInsuranceBillNotif();
            $smarty->display("noticesview/print_tab9_2.html");
            break;
        case 'print_tab9_3':

            getInsuranceBillNotif();
            $smarty->display("noticesview/print_tab9_3.html");
            break;
        default:
            break;
    }
}
if ($do == "getsubcat") {

    $parentid = (int) filter_input(INPUT_POST, "id");
    $level = (int) filter_input(INPUT_POST, "level");
    $t =  (int) filter_input(INPUT_POST, "t");
    if ($parentid == 0) {
        echo -1;
    } else {
        ##التصنيفات
        $catDataReport = getCategoryChilds($parentid);
        if (count($catDataReport[1]) > 0) {
            $smarty->assign("catDataReport", $catDataReport[1]);
            $smarty->assign("level", $level);
            $smarty->assign("t", $t);
            $smarty->display("noticesview/catchilds.html");
        } else {
            echo -1;
        }
    }
}

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


function getInsuranceBillNotif()
{
    global $billsDAO;
    global $billsEX;
    global $ClientDAO;
    global $branchDAO;
    global $insuranceCompanyDAO;
    global $smarty;

    $waitBills = $billsEX->queryAllNotDelWithQuerySrting(' and insurance = 1 and approved = 0 and notifSeen = 0 ');
    foreach ($waitBills as $bill) {
        ## client name
        $client = $ClientDAO->load($bill->clientid);
        $bill->clientname = $client->clientname;

        ## branch
        if ($bill->branchid == 0) {
            $bill->branchid = '';
        } else {
            $branch = $branchDAO->load($bill->branchid);
            $bill->branchid = $branch->branchName;
        }


        ## insurance company name
        $company = $insuranceCompanyDAO->load($bill->insurancecompanyid);
        $bill->insurancecompanyid = $company->name;
    }
    $approvedBills = $billsEX->queryAllNotDelWithQuerySrting(' and insurance = 1 and approved = 1 and notifSeen = 0 ');
    foreach ($approvedBills as $bill) {
        ## client name
        $client = $ClientDAO->load($bill->clientid);
        $bill->clientname = $client->clientname;

        ## branch
        if ($bill->branchid == 0) {
            $bill->branchid = '';
        } else {
            $branch = $branchDAO->load($bill->branchid);
            $bill->branchid = $branch->branchName;
        }


        ## insurance company name
        $company = $insuranceCompanyDAO->load($bill->insurancecompanyid);
        $bill->insurancecompanyid = $company->name;
    }
    $rejectedBills = $billsEX->queryAllNotDelWithQuerySrting(' and insurance = 1 and approved = 2 and notifSeen = 0 ');
    foreach ($rejectedBills as $bill) {
        ## client name
        $client = $ClientDAO->load($bill->clientid);
        $bill->clientname = $client->clientname;

        ## branch
        if ($bill->branchid == 0) {
            $bill->branchid = '';
        } else {
            $branch = $branchDAO->load($bill->branchid);
            $bill->branchid = $branch->branchName;
        }


        ## insurance company name
        $company = $insuranceCompanyDAO->load($bill->insurancecompanyid);
        $bill->insurancecompanyid = $company->name;
    }

    $smarty->assign("waitBills", $waitBills);
    $smarty->assign("approvedBills", $approvedBills);
    $smarty->assign("rejectedBills", $rejectedBills);
}

function getToWorkshopOrderNoticeInDetails()
{
    //Toworkshoporder
    global $toWorkshopOrder;
    global $toWorkshopOrderDAO;
    global $toWorkshopOrderEX;

    global $smarty;

    $now = date("Y-m-d H:i:s");
    if ($_SESSION['userid'] == 1) {
        //admin see all
        $showOnlyToQyeryString = "";
    } else {
        //معرفس ال متغيرات دى تيجى منين
        //or workshopId  = " . $workshopId . "
        $showOnlyToQyeryString = " and ( branchId = " . $_SESSION['branchId'] . "  or driverId  = " . $_SESSION['userid'] . " or driverIdBack  = " . $_SESSION['userid'] . " ) ";
    }
    ##1-متاخرات ذهاب
    //-لم تذهب
    $delayGo = $toWorkshopOrderEX->queryByOrderStatusEX(0, ' and recieveWorkshopDate = "0000-00-00 00:00:00"' . $showOnlyToQyeryString);
    $i = 0;

    foreach ($delayGo as $value) {
        $hours = dateTimeDiff($value->deliverWorkshopDate, $now); //$value->recieveWorkshopDate
        if ($hours < $value->drivertimeout) {
            unset($delayGo[$i]);
        }
        $i++;
    }
    $smarty->assign("delayGo", $delayGo);
    ##2-متاخرات ورشة
    $delayWorkshop = $toWorkshopOrderEX->queryByOrderStatusEX(1, ' and deliverBranchDate = "0000-00-00 00:00:00"' . $showOnlyToQyeryString);
    $i = 0;
    foreach ($delayWorkshop as $value) {
        $hours = dateTimeDiff($value->recieveWorkshopDate, $now); //$value->deliverBranchDate
        if ($hours < $value->workshoptimeout) {
            unset($delayWorkshop[$i]);
        }
        $i++;
    }
    $smarty->assign("delayWorkshop", $delayWorkshop);
    ##3-متاخرات عودة
    $delayBack = $toWorkshopOrderEX->queryByOrderStatusEX(2, ' and recieveBranchDate = "0000-00-00 00:00:00"' . $showOnlyToQyeryString);
    $i = 0;
    foreach ($delayBack as $value) {
        $hours = dateTimeDiff($value->deliverBranchDate, $now); //$value->recieveBranchDate
        if ($hours < $value->drivertimeoutback) {
            unset($delayBack[$i]);
        }
        $i++;
    }
    $smarty->assign("delayBack", $delayBack);
}

function dateTimeDiff($dateTime1, $dateTime2)
{
    $dateTime1 = new DateTime($dateTime1);
    $dateTime2 = new DateTime($dateTime2);
    $diff = $dateTime2->diff($dateTime1);
    $hours = $diff->h;
    $hours = $hours + ($diff->days * 24);
    return $hours;
}

function checkforsupplier()
{
    global $DatedcheckedEX;
    global $DatedcheckedDAO;

    $today = date("Y-m-d");
    $todayfor7days = date("Y-m-d", strtotime($date . ' + 7 day'));

    $suppiercheck = $DatedcheckedEX->queryallcheckforsupplier($today, $todayfor7days);
    $suppiercheckcount = count($suppiercheck);

    return array($suppiercheck, $suppiercheckcount);
}


function dateTimeDiff2($dateTime1, $dateTime2)
{
    $dateTime1 = new DateTime($dateTime1);
    $dateTime2 = new DateTime($dateTime2);
    $diff = $dateTime2->diff($dateTime1);
    $hours = $diff->h;
    $hours = $hours + ($diff->days * 24);
    return $hours;
}

function checkforclient()
{
    global $DatedcheckedEX;
    global $DatedcheckedDAO;

    $today = date("Y-m-d");
    $todayfor7days = date("Y-m-d", strtotime($date . ' + 7 day'));

    $clientcheck = $DatedcheckedEX->queryallcheckforclient($today, $todayfor7days);
    $clientcheckcount = count($clientcheck);

    return array($clientcheck, $clientcheckcount);
}

//////////////////////////kempila/////////////////////////
function showallkempila()
{
    global $KempialaEX;
    global $SupplierDAO;
    global $ClientDAO;

    $today = date("Y-m-d");
    $todayfor7days = date("Y-m-d", strtotime($today . ' + 7 day'));

    $allkempila = $KempialaEX->queryAllkempila($today, $todayfor7days);
    $allkempilacount = count($allkempila);

    return array($allkempila, $allkempilacount);
}

function loadstore()
{
    global $StoreDAO;

    $allstore = $StoreDAO->queryAll();

    return $allstore;
}

function loadstorename()
{
    global $StoreDAO;
    $storename = $_POST['supplierid'];
    $storeid = $StoreDAO->load($storename);

    return $storeid;
}

function showallproductbystorename()
{

    global $ProductEX;
    global $productDAO;
    global $productCatDAO;
    $storename = $_POST['supplierid'];

    //echo $storename;
    $allproduct = $ProductEX->queryWithStoreId22($storename);


    foreach ($allproduct as $request) {
        $product = $productDAO->load($request->productId);
        $request->productName = $product->productName;
        $productCat = $productCatDAO->load($product->productCatId);
        $request->productCatName = $productCat->productCatName;


        $parentId = $product->productCatId;
        $pathArr = getProductPath_recursive1($parentId, $categories, 0);
        $request->productName = $product->productName . '/' . $pathArr;
    }
    return $allproduct;
}

///////////////////////show all product/////////////////////////
function showallproductx2()
{

    global $ProductEX;
    global $productDAO;
    global $productCatDAO;
    $allproduct = $ProductEX->queryallproduct();

    foreach ($allproduct as $request) {
        $product = $productDAO->load($request->productId);
        $request->productName = $product->productName;
        $productCat = $productCatDAO->load($product->productCatId);
        $request->productCatName = $productCat->productCatName;


        $parentId = $product->productCatId;
        $pathArr = getProductPath_recursive1($parentId, $categories, 0);
        $request->productName = $product->productName . '/' . $pathArr;
    }
    return $allproduct;
}


function fetch_recursive($parentid, $categories)
{
    global $myProductcatEx;
    //print($parentid."<br>");
    $catData = $myProductcatEx->getCategoryAndParentByCatId($parentid);
    //print_r($catData);
    if (count($catData) > 0) {
        $categories .= $catData->productCatName . '/';
        $newParentId = $catData->productCatParent;

        //if($newParentId != 0 && isset($newParentId))
        {
            //$newParentName = $catData->parentName;
            //$categories .= $newParentName.'/';
            return fetch_recursive($newParentId, $categories);
        }
    }
    $categories = substr($categories, 0, strlen($categories) - 1);
    return $categories;
}

function productnotsell()
{
    global $SellbilldetailEX;

    $today = date("Y-m-d");
    $todayfor7days = date("Y-m-d", strtotime($today . ' - 7 day'));

    $allsell = $SellbilldetailEX->queryproductnotsell($todayfor7days, $today);

    return $allsell;
}

function productnotsellbystorename()
{
    global $SellbilldetailEX;
    global $productDAO;
    global $productCatDAO;
    $today = date("Y-m-d");
    $todayfor7days = date("Y-m-d", strtotime($date . ' - 7 day'));
    $storename = $_POST['supplierid2'];
    $allsell = $SellbilldetailEX->queryproductnotsellwithstorename($todayfor7days, $today, $storename);

    //print_r($allsell);
    foreach ($allsell as $request) {

        $product = $productDAO->load($request->productId);

        $request->productName = $product->productName;
        $productCat = $productCatDAO->load($product->productCatId);
        $request->productCatName = $productCat->productCatName;


        $parentId = $product->productCatId;
        $pathArr = getProductPath_recursive1($parentId, $categories, 0);
        $request->productName = $product->productName . '/' . $pathArr;
    }


    return $allsell;
}


function getProductRequest()
{
    global $userDAO;
    global $productRequestExt;
    global $smarty;
    global $productCatDAO;
    global $productDAO;

    $userdata = $userDAO->load($_SESSION['userid']);
    $storeId = $userdata->userstoreid;
    ##  الطلبات القادمة من المخازن الأخرى سيتم عرضها فى الاشعارات
    #
    if ($storeId == 0 || empty($storeId)) {
        ##  all new  requests
        $newRequests = $productRequestExt->getRequestsWithString('and p.status = 0 ', ' and p.seen = 0 ', 0);
        ##  all accepted requests
        $acceptRequests = $productRequestExt->getRequestsWithString('and p.status in ( 2 , 3 ) ', ' and p.seen = 0 ', 0);
        ##  all refused requests
        $refusedRequests = $productRequestExt->getRequestsWithString('and p.status = 1 ', ' and p.seen = 0 ', 0);


        foreach ($newRequests as $request) {
            $product = $productDAO->load($request->productId);
            $request->productName = $product->productName;
            $productCat = $productCatDAO->load($product->productCatId);
            $request->productCatName = $productCat->productCatName;


            $parentId = $product->productCatId;
            $pathArr = getProductPath_recursive1($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        foreach ($acceptRequests as $request) {
            $product = $productDAO->load($request->productId);
            $request->productName = $product->productName;
            $productCat = $productCatDAO->load($product->productCatId);
            $request->productCatName = $productCat->productCatName;


            $parentId = $product->productCatId;
            $pathArr = getProductPath_recursive1($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        foreach ($refusedRequests as $request) {
            $product = $productDAO->load($request->productId);
            $request->productName = $product->productName;
            $productCat = $productCatDAO->load($product->productCatId);
            $request->productCatName = $productCat->productCatName;


            $parentId = $product->productCatId;
            $pathArr = getProductPath_recursive1($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }


        $smarty->assign("newRequests", $newRequests);
        $smarty->assign("acceptRequests", $acceptRequests);
        $smarty->assign("refusedRequests", $refusedRequests);
    }
    ##
    else {

        ##  new requests from other stores to me
        $string2 = 'and p.status = 0 and p.storeid = ' . $storeId . ' ';
        $otherNewRequests = $productRequestExt->getRequestsWithString($string2, ' and p.seen = 0 ', 0);

        ##  accepted requests from my store to others
        $string3 = 'and p.status in ( 2 , 3 ) and p.requeststoreid = ' . $storeId . ' ';
        $myAcceptRequests = $productRequestExt->getRequestsWithString($string3, ' and p.seen = 0 ', 0);


        ##  refused requests from my store to others
        $string5 = 'and p.status = 1 and p.requeststoreid = ' . $storeId . ' ';
        $myRefusedRequests = $productRequestExt->getRequestsWithString($string5, ' and p.seen = 0 ', 0);


        foreach ($otherNewRequests as $request) {
            $product = $productDAO->load($request->productid);
            $request->productName = $product->productName;
            $productCat = $productCatDAO->load($product->productCatId);
            $request->productCatName = $productCat->productCatName;


            $parentId = $product->productCatId;
            $pathArr = getProductPath_recursive1($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        foreach ($myAcceptRequests as $request) {
            $product = $productDAO->load($request->productid);
            $request->productName = $product->productName;
            $productCat = $productCatDAO->load($product->productCatId);
            $request->productCatName = $productCat->productCatName;


            $parentId = $product->productCatId;
            $pathArr = getProductPath_recursive1($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        foreach ($myRefusedRequests as $request) {
            $product = $productDAO->load($request->productid);
            $request->productName = $product->productName;
            $productCat = $productCatDAO->load($product->productCatId);
            $request->productCatName = $productCat->productCatName;


            $parentId = $product->productCatId;
            $pathArr = getProductPath_recursive1($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        $smarty->assign("newRequests", $otherNewRequests);
        $smarty->assign("acceptRequests", $myAcceptRequests);
        $smarty->assign("refusedRequests", $myRefusedRequests);
    }
}

function getProductPath_recursive1($parentid, $categories, $level)
{
    global $productCatExt;

    $catData = $productCatExt->getCategoryAndParentByCatId($parentid);

    if (count($catData) > 0 && $level < 2) {
        $categories .= $catData->productCatName . '/';
        $newParentId = $catData->productCatParent;

        return getProductPath_recursive1($newParentId, $categories, ($level + 1));
    }
    $categories = substr($categories, 0, strlen($categories) - 1);
    return $categories;
}

function getOtherRequests()
{
    global $otherRequestEX;
    global $userDAO;
    global $smarty;

    $userdata = $userDAO->load($_SESSION['userid']);

    $branchId = $_SESSION['branchId'];         ## 0 all branches    , else see its branch only
    $userGroupId = $userdata->usergroupid;     ## 1 admin   see every thing   , else see its group only
    $userId = $_SESSION['userid'];


    if ($branchId == 0) {

        ##  requests come to all branchs
        $string2 = 'and o.requestTo = 0  ';
        $toBranchRequests = $otherRequestEX->getRequestsWithStringEx($string2, ' 0 ', 0);


        ###################################################to user group  requests #####################################################################

        if ($userGroupId == 1) {
            ##  requests come to all user groups
            $string3 = 'and o.requestTo = 1  ';
            $toGroupRequests = $otherRequestEX->getRequestsWithStringEx($string3, ' 0 ', 0);


            ##  requests come to all users
            $string4 = 'and o.requestTo = 2   ';
            $toUserRequests = $otherRequestEX->getRequestsWithStringEx($string4, ' 0 ', 0);
        }
        ##
        else {
            ##  requests come to my user group
            $string3 = 'and o.requestTo = 1   and o.requestToId  =  ' . $userGroupId . ' ';
            $toGroupRequests = $otherRequestEX->getRequestsWithStringEx($string3, ' 0 ', 0);

            ##  requests come to my user
            $string4 = 'and o.requestTo = 2   and o.requestToId  =  ' . $userId . ' ';
            $toUserRequests = $otherRequestEX->getRequestsWithStringEx($string4, ' 0 ', 0);
        }
    }
    ##
    else {
        ##  requests come to my branch
        $string2 = 'and o.requestTo = 0   and o.requestToId  =  ' . $branchId . ' ';
        $toBranchRequests = $otherRequestEX->getRequestsWithString($string2, ' 0 ', 0);

        ###################################################to user group  requests #####################################################################

        if ($userGroupId == 1) {
            ##  requests come to all user groups
            $string3 = 'and o.requestTo = 1  ';
            $toGroupRequests = $otherRequestEX->getRequestsWithString($string3, ' 0 ', 0);


            ##  requests come to all users
            $string4 = 'and o.requestTo = 2   ';
            $toUserRequests = $otherRequestEX->getRequestsWithString($string4, ' 0 ', 0);
        }
        ##
        else {
            ##  requests come to my user group
            $string3 = 'and o.requestTo = 1   and o.requestToId  =  ' . $userGroupId . ' ';
            $toGroupRequests = $otherRequestEX->getRequestsWithString($string3, ' 0 ', 0);

            ##  requests come to my user
            $string4 = 'and o.requestTo = 2   and o.requestToId  =  ' . $userId . ' ';
            $toUserRequests = $otherRequestEX->getRequestsWithString($string4, ' 0 ', 0);
        }
    }


    $smarty->assign('branchId', $branchId);
    $smarty->assign('userGroupId', $userGroupId);
    $smarty->assign('toBranchRequests', $toBranchRequests);
    $smarty->assign('toUserRequests', $toUserRequests);
    $smarty->assign('toGroupRequests', $toGroupRequests);
}

function getCategoryChilds($parentid = 0)
{
    global $productCatExt;
    global $productCatDAO;
    //load cat
    $parentobject = $productCatDAO->load($parentid);
    $childsArray = $productCatExt->getChilds($parentid);

    return array($parentobject, $childsArray);
}
