<?php

//the global file operation
include("../public/impOpreation.php");
include_once("../library/uploadImages.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");

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

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

$store = new Store();
$storeDAO = new StoreMySqlDAO();
$storeEX = new StoreMySqlExtDAO();

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

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

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

    $storeId = $userdata->userstoreid;
    if ($storeId == 0 || empty($storeId)) {
        $store = $storeDAO->queryByConditions(0);
        $smarty->assign("stores", $store);
    }
    $smarty->assign("storeId", $storeId);
    //here the smarty templates
    $smarty->display("productrequestview/add.html");
}

####
elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    $storeId = $userdata->userstoreid;
    $smarty->assign("storeId", $storeId);


    ## الطلبات التى تم عملها من المخزن الخاص بالمستخدم
    ## و الطلبات القادمة من المخازن الأخرى الى  المخزن الخاص بالمستخدم






    if ($storeId == 0 || empty($storeId)) {
        ##  all new  requests
        $myNewRequests = $productRequestExt->getRequestsWithString('and p.status = 0 ', '', 0);
        ##  all accepted requests
        $myAcceptRequests = $productRequestExt->getRequestsWithString('and p.status in ( 2 , 3 ) ', '', 0);
        ##  all refused requests
        $myRefusedRequests = $productRequestExt->getRequestsWithString('and p.status = 1 ', '', 0);
    }
    ##
    else {
        ##  new requests from my store to others
        $string1 = 'and p.status = 0 and p.requeststoreid = ' . $storeId . ' ';
        $myNewRequests = $productRequestExt->getRequestsWithString($string1, '', 0);

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

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

        ##  accepted requests from other stores to me
        $string4 = 'and p.status in ( 2 , 3 ) and p.storeid = ' . $storeId . ' ';
        $otherAcceptRequests = $productRequestExt->getRequestsWithString($string4, '', 0);

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

        ##  refused requests from other stores to me
        $string6 = 'and p.status = 1 and p.storeid = ' . $storeId . ' ';
        $otherRefusedRequests = $productRequestExt->getRequestsWithString($string6, '', 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_recursive($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        foreach ($otherAcceptRequests 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_recursive($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        foreach ($otherRefusedRequests 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_recursive($parentId, $categories, 0);
            $request->productName = $product->productName . '/' . $pathArr;
        }

        $smarty->assign('otherNewRequests1', $otherNewRequests);
        $smarty->assign('otherAcceptRequests1', $otherAcceptRequests);
        $smarty->assign('otherRefusedRequests1', $otherRefusedRequests);
    }

    foreach ($myNewRequests 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_recursive($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_recursive($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_recursive($parentId, $categories, 0);
        $request->productName = $product->productName . '/' . $pathArr;
    }




    $smarty->assign('myNewRequests1', $myNewRequests);
    $smarty->assign('myAcceptRequests1', $myAcceptRequests);
    $smarty->assign('myRefusedRequests1', $myRefusedRequests);
    $smarty->display("productrequestview/show.html");
}


/*
  
  ####
  elseif ($do == "edit") {//edit  client
  include_once("../public/authentication.php");
  $companyId = filter_input(INPUT_GET, "id");

  $insurancecompany = $insuranceCompanyDAO->load($companyId);
  $smarty->assign('company', $insurancecompany);

  $smarty->display("productrequestview/edit.html");
  }
  ####
  elseif ($do == "update") { //edit update
  include_once("../public/authentication.php");
  try {
  update();
  header("location:?do=sucess");
  } catch (Exception $e) {
  header("location:?do=error");
  }
  }
 */

####
elseif ($do == "delete") {
    include_once("../public/authentication.php");

    $productRequestId = filter_input(INPUT_GET, "id");
    $productRequest = $productRequestDAO->load($productRequestId);
    $productRequest->deleted = 1;
    try {
        $productRequestDAO->update($productRequest);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}



####
elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
}
####
elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}



$smarty->assign("productrequest", 1);

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

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

// add in client tbl
function add() {
    global $insurancecompany;
    global $insuranceCompanyDAO;

    $companyName = filter_input(INPUT_POST, "companyName");
    $discountPercent = filter_input(INPUT_POST, "discountPercent");
    $contractStart = filter_input(INPUT_POST, "contractStart");
    $contractEnd = filter_input(INPUT_POST, "contractEnd");
    $instruction = filter_input(INPUT_POST, "instruction");


    $handle = new upload($_FILES['contractPhoto']);
    $path = "../upload/insuranceCompany";
    $contractPhoto = uploadImages2($handle, $path);

    $insurancecompany->name = $companyName;
    $insurancecompany->discountpercent = $discountPercent;
    $insurancecompany->contractstart = $contractStart;
    $insurancecompany->contractend = $contractEnd;
    $insurancecompany->contractphoto = $contractPhoto;
    $insurancecompany->instruction = $instruction;
    $insurancecompany->userid = $_SESSION['userid'];
    $insurancecompany->deleted = 0;

    $insuranceCompanyDAO->insert($insurancecompany);
}

//show
function show() {
    global $clientDAO;
    global $clientExt;
    global $today;
    global $smarty;

    $alldata = $clientDAO->queryAll();
    $smarty->assign("alldata", $alldata);

    $shownData = $clientExt->queryAllForShow();

    $sum;
    foreach ($shownData as $data) {
        if ($data->conditions == 0) {
            $sum += $data->clientdebt;
        }
    }
    $smarty->assign('sum', $sum);

    $smarty->assign('shownData', $shownData);
}

function update() {
    global $insurancecompany;
    global $insuranceCompanyDAO;

    $companyId = filter_input(INPUT_POST, "companyId");
    $companyName = filter_input(INPUT_POST, "companyName");
    $discountPercent = filter_input(INPUT_POST, "discountPercent");
    $contractStart = filter_input(INPUT_POST, "contractStart");
    $contractEnd = filter_input(INPUT_POST, "contractEnd");
    $instruction = filter_input(INPUT_POST, "instruction");

    $insurancecompany = $insuranceCompanyDAO->load($companyId);

    if ($_FILES['contractPhoto']['name']) {
        $handle = new upload($_FILES['contractPhoto']);
        $path = "../upload/insuranceCompany";
        $contractPhoto = uploadImages2($handle, $path);
        $insurancecompany->contractphoto = $contractPhoto;
    }

    $insurancecompany->name = $companyName;
    $insurancecompany->discountpercent = $discountPercent;
    $insurancecompany->contractstart = $contractStart;
    $insurancecompany->contractend = $contractEnd;
    $insurancecompany->instruction = $instruction;
    $insurancecompany->userid = $_SESSION['userid'];

    $insuranceCompanyDAO->update($insurancecompany);
}

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

?>