<?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");


//here the db files that include in the file
// GOES HERE ....................
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');

///////////////////////////kempila////////////////////////
require_once('../models/dao/KempialaDAO.class.php');
require_once('../models/dto/Kempiala.class.php');
require_once('../models/mysql/KempialaMySqlDAO.class.php');
require_once('../models/mysql/ext/KempialaMySqlExtDAO.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');

//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');

//client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

//Notics
require_once('../models/dao/NoticsDAO.class.php');
require_once('../models/dto/Notic.class.php');
require_once('../models/mysql/NoticsMySqlDAO.class.php');
require_once('../models/mysql/ext/NoticsMySqlExtDAO.class.php');

//Premiumclient
require_once('../models/dao/PremiumclientDAO.class.php');
require_once('../models/dto/Premiumclient.class.php');
require_once('../models/mysql/PremiumclientMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumclientMySqlExtDAO.class.php');

//Premium
require_once('../models/dao/PremiumDAO.class.php');
require_once('../models/dto/Premium.class.php');
require_once('../models/mysql/PremiumMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumMySqlExtDAO.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');

//breadcrumb
require_once("../library/breadcrumb.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 :- workstation lvh;. hghkjh[

  OPERTATION in Controller

  1-display add form
  2- add in workstation,workstationworker,workstationasset tble
  3-update savefrom value (-)
  4-insert into savedaily tbl
  5-update saveto value (+)
  6-insert into savedaily tbl
  7-display sucess or error
  8-display show and delete
  9-tempdelete from Expenses tbl
  10-returndelete save value (+)

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

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

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

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


//supplier
$SupplierDAO = new SupplierMySqlDAO();
$Supplier = new Supplier();
$SupplierEX = new SupplierMySqlExtDAO();
$newsupplier = new Supplier();


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

//Notics
$Notics = new Notic();
$NoticsDAO = new NoticsMySqlDAO();
$NoticsExt = new NoticsMySqlExtDAO();

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

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

//Branch
$branchDAO = new BranchMySqlDAO();

//paymentnetworks
$insuranceCompanyDAO = new InsurancecompaniesMySqlDAO();
//breadcrumb
$breadcrumbObject = new Breadcrumb();

//# usergroup
$myUserGroup = new Usergroup();
$myUserGroupRecord = new UsergroupMySqlDAO();
$myUserGroupEx = new UsergroupMySqlExtDAO();

//Premiumclient
$Premiumclient = new Premiumclient();
$PremiumclientDAO = new PremiumclientMySqlDAO();
$PremiumclientExt = new PremiumclientMySqlExtDAO();

//Premium
$premium = new Premium();
$premiumDAO = new PremiumMySqlDAO();
$premiumExt = new PremiumMySqlExtDAO();

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

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

$userGroupId = $_SESSION['usergroupid'];
$loadData = $myUserGroupRecord->load($userGroupId);
$smarty->assign("loadData", $loadData);

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

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

    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);

    $allstore = loadstore();
    ///product
    $allproductdata = showallproductx2();
    $allproduct = $allproductdata[0];
    $productcount = $allproductdata[1];

    //productnotsell
    $allnotselldata = productnotsell();
    $allsell = $allnotselldata[0];
    $productnotsellcount = $allnotselldata[1];

    //kempiladata
    $allkempiladata = showallkempila();
    $allkempila = $allkempiladata[0];
    $allkempilacount = $allkempiladata[1];

    //clientcheck
    $clientcheckdata = checkforclient();
    $clientcheck = $clientcheckdata[0];
    $clientcheckcount = $clientcheckdata[1];

    //suppiercheck
    $suppiercheckdata = checkforsupplier();
    $suppiercheck = $suppiercheckdata[0];
    $suppiercheckcount = $suppiercheckdata[1];


    getProductRequest();


    getOtherRequests();


    getToWorkshopOrderNoticeInDetails();
    getInsuranceBillNotif();

    $premiums = getPremiumTodays();


    $smarty->assign("userid", $_SESSION['userid']);

    $smarty->assign("allstore", $allstore);
    $smarty->assign("premiums", $premiums);
    $smarty->assign("suppiercheck", $suppiercheck);
    $smarty->assign("allproduct", $allproduct);
    $smarty->assign("allsell", $allsell);
    $smarty->assign("allkempila", $allkempila);
    $smarty->assign("clientcheck", $clientcheck);

    $smarty->display("noticesview/show.html");
}
###
else if ($do == "show") {
    include_once("../public/authentication.php");

    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);

    //allproduct
    $allproductdata = showallproductbystorename();
    $allproduct = $allproductdata[0];
    $allproductcount = $allproductdata[1];
    $allstore = loadstore();
    $storeid  = loadstorename();

    //productnotsell
    $allnotselldata = productnotsell();
    $allsell = $allnotselldata[0];
    $productnotsellcount = $allnotselldata[1];

    //kempiladata
    $allkempiladata = showallkempila();
    $allkempila = $allkempiladata[0];
    $allkempilacount = $allkempiladata[1];

    //clientcheck
    $clientcheckdata = checkforclient();
    $clientcheck = $clientcheckdata[0];
    $clientcheckcount = $clientcheckdata[1];

    //suppiercheck
    $suppiercheckdata = checkforsupplier();
    $suppiercheck = $suppiercheckdata[0];
    $suppiercheckcount = $suppiercheckdata[1];



    $smarty->assign("allstore", $allstore);
    $smarty->assign("storeid", $storeid);
    $smarty->assign("allsell", $allsell);
    $smarty->assign("allproduct", $allproduct);
    $smarty->assign("allkempila", $allkempila);
    $smarty->assign("clientcheck", $clientcheck);
    $smarty->assign("suppiercheck", $suppiercheck);
    $smarty->display("noticesview/show.html");
}
###
else if ($do == "showproductnotsell") {
    include_once("../public/authentication.php");

    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);

    ///product
    $allproductdata = showallproductx2();
    $allproduct = $allproductdata[0];
    $productcount = $allproductdata[1];

    $allstore = loadstore();
    $storeid2 = loadstorename2();

    $allnotselldata = productnotsellbystorename();
    $allsell = $allnotselldata[0];
    $allproductnotsellcount = $allnotselldata[1];

    //kempiladata
    $allkempiladata = showallkempila();
    $allkempila = $allkempiladata[0];
    $allkempilacount = $allkempiladata[1];

    //clientcheck
    $clientcheckdata = checkforclient();
    $clientcheck = $clientcheckdata[0];
    $clientcheckcount = $clientcheckdata[1];

    //suppiercheck
    $suppiercheckdata = checkforsupplier();
    $suppiercheck = $suppiercheckdata[0];
    $suppiercheckcount = $suppiercheckdata[1];
    $DDD = 1;

    /* $allnotics = getallnotics();
      if(count($allnotics) > 0)
      {
      $Notics->noticesid=1;
      $Notics->productno=$productcount;
      $Notics->productnotsellno=$allproductnotsellcount;
      $Notics->kempilano=$allkempilacount;
      $Notics->clientcheckno=$clientcheckcount;
      $Notics->suppliercheckno=$suppiercheckcount;

      $NoticsDAO->update($Notics);

      } */

    $smarty->assign("DDD", $DDD);
    $smarty->assign("allstore", $allstore);
    $smarty->assign("storeid2", $storeid2);
    $smarty->assign("allsell", $allsell);
    $smarty->assign("allproduct", $allproduct);
    $smarty->assign("allkempila", $allkempila);
    $smarty->assign("clientcheck", $clientcheck);
    $smarty->assign("suppiercheck", $suppiercheck);
    $smarty->display("noticesview/show.html");
}
###
elseif ($do == "editprint") {
    include_once("../public/authentication.php");

    //breadcrumb
    $breadcrumbObject->add('الحسابات><a href="noticesController.php?do=show">الكمبياله</a> > عرض كمبياله', 'noticesController.php', 0);
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);


    $showData = edit();
    $smarty->assign("showData", $showData);


    $smarty->assign("customPrint", 1);
    $smarty->display("noticesview/editprint.html");
}
###
elseif ($do == "editprint2") {
    include_once("../public/authentication.php");

    //breadcrumb
    $breadcrumbObject->add('الحسابات><a href="noticesController.php?do=show"> الشيك</a> > عرض الشيك', 'noticesController.php', 0);
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);

    $showdatacheck = showallcheck();
    $smarty->assign("showdatacheck", $showdatacheck);

    $smarty->assign("customPrint", 1);
    $smarty->display("noticesview/editprint2.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 loadstore()
{
    global $StoreDAO;

    $allstore = $StoreDAO->queryAll();

    return $allstore;
}

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

    return $storeid;
}

function loadstorename2()
{
    global $StoreDAO;
    $storename1 = $_POST['supplierid2'];
    $storeid2 = $StoreDAO->load($storename1);

    return $storeid2;
}

function showallproductbystorename()
{

    global $ProductEX;
    global $productDAO;
    global $productCatDAO;
    $storename = $_POST['supplierid'];
    $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));
    }
    $querystring = '';
    if ($productCatId && $productCatId != -1)
        $querystring = ' and product.productCatId = ' . $productCatId;
    // echo $storename;
    $allproduct = $ProductEX->queryWithStoreId22($storename, $querystring);
    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;
    }
    $allproductcount = count($allproduct);
    return array($allproduct, $allproductcount);
}

///////////////////////show all product/////////////////////////
//function showallproductx2() {
//
//    global $ProductEX;
//    $allproduct = $ProductEX->queryallproduct();
//
//    $productcount = count($allproduct);
//    return array($allproduct, $productcount);
//}
//////////////////////////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 productnotsell() {
//    global $SellbilldetailEX;
//
//    $today = date("Y-m-d");
//    $todayfor7days = date("Y-m-d", strtotime($today . ' - 7 day'));
//
//    $allsell = $SellbilldetailEX->queryproductnotsell($todayfor7days, $today);
//    $productnotsellcount = count($allsell);
//    return array($allsell, $productnotsellcount);
//}

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'];

    $level = filter_input(INPUT_POST, 'levelt');
    $productCatId = filter_input(INPUT_POST, 'productCatItd' . $level);
    if (empty($productCatId) || $productCatId == -1) {
        $productCatId = filter_input(INPUT_POST, 'productCattId' . ($level - 1));
    }
    $querystring = '';
    if ($productCatId && $productCatId != -1)
        $querystring = ' and product.productCatId = ' . $productCatId;

    $allsell = $SellbilldetailEX->queryproductnotsellwithstorename($todayfor7days, $today, $storename, $querystring);
    foreach ($allsell as $request) {

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

        $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;
    }

    $allproductnotsellcount = count($allsell);

    return array($allsell, $allproductnotsellcount);
}

//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);
//}
//
//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);
//}
// edit in table
function edit()
{
    global $KempialaEX;

    $id = $_GET['id'];

    $showData = $KempialaEX->loadEX($id);

    return $showData;
}

function showallcheck()
{
    global $DatedcheckedEX;
    global $DatedcheckedDAO;
    global $SupplierDAO;
    global $ClientDAO;
    global $smarty;

    $checkid = $_GET['checkid'];

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

    $allcheck = $DatedcheckedEX->queryallcheck($checkid, $today, $todayfor7days);

    foreach ($allcheck as $check) {

        $suppliername = $SupplierDAO->load($check->clientNum);
        $clientname = $ClientDAO->load($check->clientNum);
        $check->newsupplier = $suppliername;
        $check->newclient = $clientname;

        $smarty->assign("suppliername", $suppliername);
        $smarty->assign("clientname", $clientname);
    }


    return $allcheck;
}

//function getallnotics() {
//    global $Notics;
//    global $NoticsDAO;
//
//    $allnotics = $NoticsDAO->queryAll();
//    return $allnotics;
//}
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 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 getPremiumTodays()
{
    global $premiumExt;

    $today = date("Y-m-d");
    $user_id = $_SESSION['userid'];
    $allpremiumToday = $premiumExt->getbyuseridtonotic($user_id, $today);
    //    var_dump($allpremiumToday);
    return $allpremiumToday;
}

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

    return array($parentobject, $childsArray);
}
