<?php

//the global file operation
include("../public/impOpreation.php");
//Transaction
include_once("../models/sql/Transaction.class.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
include("../public/include_dao.php");

//Workshop
require_once('../models/dao/WorkshopDAO.class.php');
require_once('../models/dto/Workshop.class.php');
require_once('../models/mysql/WorkshopMySqlDAO.class.php');
require_once('../models/mysql/ext/WorkshopMySqlExtDAO.class.php');
//Bill
require_once('../models/dao/BillsDAO.class.php');
require_once('../models/dto/Bill.class.php');
require_once('../models/mysql/BillsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsMySqlExtDAO.class.php');
//Billsproduct
require_once('../models/dao/BillsproductsDAO.class.php');
require_once('../models/dto/Billsproduct.class.php');
require_once('../models/mysql/BillsproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsproductsMySqlExtDAO.class.php');
//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');
//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');
//Workshop
require_once('../models/dao/WorkshopDAO.class.php');
require_once('../models/dto/Workshop.class.php');
require_once('../models/mysql/WorkshopMySqlDAO.class.php');
require_once('../models/mysql/ext/WorkshopMySqlExtDAO.class.php');
//Billsproduct
require_once('../models/dao/BillsproductsDAO.class.php');
require_once('../models/dto/Billsproduct.class.php');
require_once('../models/mysql/BillsproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsproductsMySqlExtDAO.class.php');

//Toworkshoporder
require_once('../models/dao/ToworkshoporderDAO.class.php');
require_once('../models/dto/Toworkshoporder.class.php');
require_once('../models/mysql/ToworkshoporderMySqlDAO.class.php');
require_once('../models/mysql/ext/ToworkshoporderMySqlExtDAO.class.php');
//Toworkshoporderbill
require_once('../models/dao/ToworkshoporderbillDAO.class.php');
require_once('../models/dto/Toworkshoporderbill.class.php');
require_once('../models/mysql/ToworkshoporderbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ToworkshoporderbillMySqlExtDAO.class.php');
//Branch
require_once('../models/dao/BranchDAO.class.php');
require_once('../models/dto/Branch.class.php');
require_once('../models/mysql/BranchMySqlDAO.class.php');
require_once('../models/mysql/ext/BranchMySqlExtDAO.class.php');

//get the do the action
$do = $_GET['do'];
if ($do != "orderproducts") {
    $smarty->display("header.html");
}
/* ======================

  Controller Name :- accountstree

  OPERTATION in Controller

  1- display add form, add
  2- add in tbl
  3- display show and tempdelete
  4- edit
  5- update

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

//here goes the instances and general variables
//Workshop
$workshop = new Workshop();
$workshopDAO = new WorkshopMySqlDAO();
$workshopEX = new WorkshopMySqlExtDAO();

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

//billsproducts
$billsProducts = new Billsproduct();
$billsProductsDAO = new BillsproductsMySqlDAO();
$billsProductsEX = new BillsproductsMySqlExtDAO();

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

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

//billsproducts
$billsProducts = new Billsproduct();
$billsProductsDAO = new BillsproductsMySqlDAO();
$billsProductsEX = new BillsproductsMySqlExtDAO();
//Toworkshoporder
$toWorkshopOrder = new Toworkshoporder();
$toWorkshopOrderDAO = new ToworkshoporderMySqlDAO();
$toWorkshopOrderEX = new ToworkshoporderMySqlExtDAO();
//Toworkshoporderbill
$toWorkshopOrderBill = new Toworkshoporderbill();
$toWorkshopOrderBillDAO = new ToworkshoporderbillMySqlDAO();
$toWorkshopOrderBillEX = new ToworkshoporderbillMySqlExtDAO();
//Branch
$Branch = new Branch();
$BranchDAO = new BranchMySqlDAO();
$BranchEX = new BranchMySqlExtDAO();

$today = date("Y-m-d");

//check and use the condition that suite this action
if (empty($do)) {
//here the permssion check
    include_once ("../public/authentication.php");
    #############################################
    ##البحث tab1
    $orderNo = filter_input(INPUT_POST, "orderNo");
    $datefrom = filter_input(INPUT_POST, "datefrom");
    $dateto = filter_input(INPUT_POST, "dateto");
    if (empty($orderNo) && empty($datefrom) && empty($dateto)) {
        $datefrom = date('Y-m-d', strtotime('-10 day', strtotime($today)));
    }
    $queryString = '';
    $queryString2 = '';
    $msg = '';
    if (isset($orderNo) && !empty($orderNo)) {
        $queryString .=' and orderNo = "' . $orderNo . '"';
        $queryString2 .=' and orderNo = "' . $orderNo . '"';
        $msg .= ' رقم الطلبية"' . $orderNo . '"';
    }
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString .=' and date(deliverWorkshopDate) >= "' . $datefrom . '"';
        $queryString2 .=' and date(deliverBranchDate) >= "' . $datefrom . '"';
        $msg .= ' التاريخ من"' . $datefrom . '"';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString .=' and date(deliverWorkshopDate) <= "' . $dateto . '"';
        $queryString2 .=' and date(deliverBranchDate) <= "' . $dateto . '"';
        $msg .= ' التاريخ الى"' . $dateto . '"';
    }
    $smarty->assign('msg', $msg);
    #############################################
    $userid = $_SESSION['userid'];
    $driverQueryString = ' and (driverIdBack=' . $userid . ' or driverId= ' . $userid . ') ';
    $ordersAtWorkshop = $toWorkshopOrderEX->queryByOrderStatusEX(0, $queryString . $driverQueryString);
    foreach ($ordersAtWorkshop as $data) {
        ##عامل التوصيل "السائق"
        $driver = $userDAO->load($data->driverId);
        $data->driverId = $driver->employeename;
        ##الفرع
        $branch = $BranchDAO->load($data->branchId);
        $data->branchId = $branch->branchName;
        ##الورشة
        $workshop = $workshopDAO->load($data->workshopId);
        $data->workshopId = $workshop->name;
    }
    $smarty->assign('ordersAtWorkshop', $ordersAtWorkshop);
    $ordersAtWorkshop_2 = $toWorkshopOrderEX->queryByOrderStatusEX(2, $queryString2 . $driverQueryString);
    foreach ($ordersAtWorkshop_2 as $data) {
        ##عامل التوصيل "السائق"
        $driver = $userDAO->load($data->driverId);
        $data->driverId = $driver->employeename;
        ##الفرع
        $branch = $BranchDAO->load($data->branchId);
        $data->branchId = $branch->branchName;
        ##الورشة
        $workshop = $workshopDAO->load($data->workshopId);
        $data->workshopId = $workshop->name;
    }
    $smarty->assign('ordersAtWorkshop_2', $ordersAtWorkshop_2);
    #############################################
    ##البحث tab2
    $orderNo = filter_input(INPUT_POST, "orderNo2");
    $datefrom = filter_input(INPUT_POST, "datefrom2");
    $dateto = filter_input(INPUT_POST, "dateto2");
    if (empty($orderNo) && empty($datefrom) && empty($dateto)) {
        $datefrom = date('Y-m-d', strtotime('-10 day', strtotime($today)));
    }
    $queryString = '';
    $queryString2 = '';
    $msg = '';
    if (isset($orderNo) && !empty($orderNo)) {
        $queryString .=' and orderNo = "' . $orderNo . '"';
        $queryString2 .=' and orderNo = "' . $orderNo . '"';
        $msg .= ' رقم الطلبية"' . $orderNo . '"';
    }
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString .=' and date(recieveWorkshopDate) >= "' . $datefrom . '"';
        $queryString2 .=' and date(recieveBranchDate) >= "' . $datefrom . '"';
        $msg .= ' التاريخ من"' . $datefrom . '"';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString .=' and date(recieveWorkshopDate) <= "' . $dateto . '"';
        $queryString2 .=' and date(recieveBranchDate) <= "' . $dateto . '"';
        $msg .= ' التاريخ الى"' . $dateto . '"';
    }
    $smarty->assign('msg2', $msg);
    #############################################
    $deliveredToWorkshop = $toWorkshopOrderEX->queryByOrderStatusEX(1, $queryString . $driverQueryString);
    foreach ($deliveredToWorkshop as $data) {
        ##عامل التوصيل "السائق"
        $driver = $userDAO->load($data->driverId);
        $data->driverId = $driver->employeename;
        ##الفرع
        $branch = $BranchDAO->load($data->branchId);
        $data->branchId = $branch->branchName;
    }
    $smarty->assign('deliveredToWorkshop', $deliveredToWorkshop);

    $deliveredToBranch = $toWorkshopOrderEX->queryByOrderStatusEX(3, $queryString2 . $driverQueryString);
    foreach ($deliveredToBranch as $data) {
        ##عامل التوصيل "السائق"
        $driver = $userDAO->load($data->driverId);
        $data->driverId = $driver->employeename;
        ##الفرع
        $branch = $BranchDAO->load($data->branchId);
        $data->branchId = $branch->branchName;
    }
    $smarty->assign('deliveredToBranch', $deliveredToBranch);

    ##كل الطلبيات
    #############################################
    ##البحث tab3
    $orderNo = filter_input(INPUT_POST, "orderNo3");
    $datefrom = filter_input(INPUT_POST, "datefrom3");
    $dateto = filter_input(INPUT_POST, "dateto3");
    if (empty($orderNo) && empty($datefrom) && empty($dateto)) {
        $datefrom = date('Y-m-d', strtotime('-10 day', strtotime($today)));
    }
    $queryString = '';
    $msg = '';
    if (isset($orderNo) && !empty($orderNo)) {
        $queryString .=' and orderNo = "' . $orderNo . '"';
        $msg .= ' رقم الطلبية"' . $orderNo . '"';
    }
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString .=' and date(deliverWorkshopDate) >= "' . $datefrom . '"';
        $msg .= ' التاريخ من"' . $datefrom . '"';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString .=' and date(deliverWorkshopDate) <= "' . $dateto . '"';
        $msg .= ' التاريخ الى"' . $dateto . '"';
    }
    $smarty->assign('msg3', $msg);
    #############################################
    $allOrders = $toWorkshopOrderEX->queryAllEX($queryString . $driverQueryString);
    foreach ($allOrders as $data) {
        ##الفرع
        $branch = $BranchDAO->load($data->branchId);
        $data->branchId = $branch->branchName;
        $data->orderStatus = getStatus($data->orderStatus);
    }
    $smarty->assign('allOrders', $allOrders);

    //here the smarty templates
    $smarty->display("driveropticreport/add.html");
} elseif ($do == "orderproducts") {
    $id = filter_input(INPUT_POST, "id");
    $orderproducts = array();
    $existId = array();

    class productData {

        public $id;
        public $productName;
        public $productNo;

    }

    $order = $toWorkshopOrderDAO->load($id);
    $orderBillsSimple = $toWorkshopOrderBillDAO->queryByToworkshoporderid($id);
    foreach ($orderBillsSimple as $myBill) {
        $bill = $billsDAO->load($myBill->billid);
        ## client name
        $client = $clientDAO->load($bill->clientid);
        $bill->clientname = $client->clientname;
        ## get products
        $productNo = 0;
        $productName = "";
        $products = $billsProductsEX->getproducts($bill->id, 0);
        $categories;
        foreach ($products as $pro) {
            if (in_array($pro->productid, $existId)) {
                $key = array_search($pro->productid, $existId);
                $myproduct = $orderproducts[$key];
            } else {
                $key = -1;
                $myproduct = new productData();
                $myproduct->id = $pro->productid;
                //$product->service = cat id
                $pathArr = getProductPath_recursive($pro->service, $categories, 0);
                $myproduct->productName = $pro->deleted . '/' . $pathArr;
                array_push($existId, $pro->productid);
            }
            $myproduct->productNo += $pro->productno;

            if ($key == -1) {
                array_push($orderproducts, $myproduct);
            }
        }
    }

    $smarty->assign("orderproducts", $orderproducts);
    //here the smarty templates
    $smarty->display("driveropticreport/orderproducts.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->assign("workshoptransport", 1);

if ($do != "orderproducts") {
    $smarty->display("footer.html");
}

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

function getStatus($status) {

    $stMsg = "";
    switch ($status) {
        case 0:
            $stMsg = "تحت التوصيل للورشة";
            break;
        case 1:
            $stMsg = "تحت التصنيع فى الورشة";
            break;
        case 2:
            $stMsg = "تحت التوصيل للفرع";
            break;
        case 3:
            $stMsg = "منتهى";
            break;

        default:
            break;
    }
    return $stMsg;
}

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

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

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

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

?>