<?php

include("../public/include_dao.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');

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

$myProductcatEx = new ProductcatMySqlExtDAO();

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


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

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

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


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

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


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

//bill
$billsEX = new BillsMySqlExtDAO();

$UsergroupDAO = new UsergroupMySqlDAO();

$premiumExt = new PremiumMySqlExtDAO();
###########################################################################################################################################################

function calcNotice() {
    global $Notics;
    global $NoticsDAO;
    global $UsergroupDAO;

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

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

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

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

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

    $allnotics = getallnotics();
    $Usergroup = $UsergroupDAO->load($_SESSION['usergroupid']);
    //update notictable
    if (count($allnotics) > 0) {
        $Notics->noticesid = 1;
        $Notics->productno = $productcount;
        $Notics->productnotsellno = $productnotsellcount;
        $Notics->kempilano = $allkempilacount;
        $Notics->clientcheckno = $clientcheckcount;
        $Notics->suppliercheckno = $suppiercheckcount;

        $NoticsDAO->update($Notics);
    } else {
        $Notics->productno = $productcount;
        $Notics->productnotsellno = $productnotsellcount;
        $Notics->kempilano = $allkempilacount;
        $Notics->clientcheckno = $clientcheckcount;
        $Notics->suppliercheckno = $suppiercheckcount;

        $NoticsDAO->insert($Notics);
    }

    $sum = 0;
    if ($Usergroup->productno == 0) {
        $sum += $productcount;
    }
    if ($Usergroup->productnotsellno == 0) {
        $sum += $productnotsellcount;
    }
    if ($Usergroup->kempilano == 0) {
        $sum += $allkempilacount;
    }
    if ($Usergroup->clientcheckno == 0) {
        $sum += $clientcheckcount;
    }
    if ($Usergroup->suppliercheckno == 0) {
        $sum += $suppiercheckcount;
    }

    //$sum = $productcount + $productnotsellcount + $allkempilacount + $clientcheckcount + $suppiercheckcount;
    return $sum;
}

function getProductRequestNotice() {
    global $userDAO;
    global $productRequestExt;

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


    if ($storeId == 0 || empty($storeId)) {
        ##  all new  requests
        $myNewRequests = $productRequestExt->getRequestsWithString('and p.status = 0 ', ' and p.seen = 0 ', 0);
        ##  all accepted requests
        $myAcceptRequests = $productRequestExt->getRequestsWithString('and p.status in ( 2 , 3 ) ', ' and p.seen = 0 ', 0);
        ##  all refused requests
        $myRefusedRequests = $productRequestExt->getRequestsWithString('and p.status = 1 ', ' and p.seen = 0 ', 0);

        $sum = count($myNewRequests) + count($myAcceptRequests) + count($myRefusedRequests);
    }
    ##
    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);


        $sum = count($otherNewRequests) + count($myAcceptRequests) + count($myRefusedRequests);
    }




    return $sum;
}

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 getOtherRequestsNotice() {
    global $otherRequestEX;
    global $userDAO;

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

    $sum = count($toBranchRequests) + count($toGroupRequests) + count($toUserRequests);
    // print_r($sum) . 'xxxxxxxxxxxxxxxxxxx';
    // $sum = $toBranchRequests->branchName + $toGroupRequests->branchName + $toUserRequests->branchName;

    return $sum;
}

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

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

    $sum = count($delayGo) + count($delayWorkshop) + count($delayBack);
    return $sum;
}

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

///////////////////////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;
        }
    $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;
    global $productDAO;
    global $productCatDAO;

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

    $allsell = $SellbilldetailEX->queryproductnotsell($todayfor7days, $today);
    /*  print_r("<pre>");

    print_r($allsell);
      print_r("<pre>");*/
  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;
        }
    $productnotsellcount = count($allsell);
    return array($allsell, $productnotsellcount);
}

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

function getallnotics() {
    global $Notics;
    global $NoticsDAO;

    $allnotics = $NoticsDAO->load(1);
    return $allnotics;
}

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 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 getInsuranceBillNotifNotice() {
    global $billsEX;

    $waitBills = $billsEX->queryAllNotDelWithQuerySrting(' and insurance = 1 and approved = 0 and notifSeen = 0 ');
    $approvedBills = $billsEX->queryAllNotDelWithQuerySrting(' and insurance = 1 and approved = 1 and notifSeen = 0 ');
    $rejectedBills = $billsEX->queryAllNotDelWithQuerySrting(' and insurance = 1 and approved = 2 and notifSeen = 0 ');

    return count($waitBills) + count($approvedBills) + count($rejectedBills);
}

function getPremiumToday() {
    global $premiumExt;

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

?>