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

//breadcrumb
require_once("../library/breadcrumb.php");

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

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

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

//breadcrumb
$breadcrumbObject = new Breadcrumb();


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

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

    $smarty->assign("allstore", $allstore);
    $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");

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

    ///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;
    $storename = $_POST['supplierid'];

    //echo $storename;
    $allproduct = $ProductEX->queryWithStoreId22($storename);
    $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;
    $today = date("Y-m-d");
    $todayfor7days = date("Y-m-d", strtotime($date . ' - 7 day'));
    $storename = $_POST['supplierid2'];

    $allsell = $SellbilldetailEX->queryproductnotsellwithstorename($todayfor7days, $today, $storename);
    $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;
}
