<?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
include("reportfunctions.php");


//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");


//Storedetail
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');

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

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

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 :- productlimitreportCtrl تقرير المنتجات الموجود بالمخزن اقل من الحد المطلوب

  OPERTATION in Controller

  1-load save data
  2-display show form

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


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



//here goes the instances and general variables
//Storedetail
$myStoredetailRecord = new StoredetailMySqlDAO();
$myStoredetailEx = new StoredetailMySqlExtDAO();

//Store
$myStoreRecord = new StoreMySqlDAO();
$StoreEX = new StoreMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Product
$myProductRecord = new ProductMySqlDAO();

$ProductDAO = new ProductMySqlDAO();
$Product = new Product();
$ProductEX = new ProductMySqlExtDAO();

//Breadcrumb
$breadcrumbObject = new Breadcrumb();


$lastLevelCatIDS;
$catsIDS;
//check and use the condetion that suite this action

if ($do == "show" || empty($do)) {

    //here the permssion check
    include_once("../public/authentication.php");


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

    // here the function that do the action
    //prepare search tools
    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);


    $smarty->assign("searchinonestore", $_SESSION['searchinonestore']);
    if ($_SESSION['searchinonestore'] == 0) {
        if ($_SESSION['storeids'] == 0) {
            $stores = $StoreEX->queryByConditions();
        } else {
            $stores = $StoreEX->queryByConditions(' and store.storeId in (' . $_SESSION['storeids'] . ')');
        }
        $smarty->assign("storeData", $stores);
        $storedef = $StoreEX->queryByConditionsOne(' and store.storeId = ' . $_SESSION['storeid'] . ' ');
        $smarty->assign("storedef", $storedef);
        $smarty->assign("storeid", $_SESSION['storeid']);
    } else {
        $storedef = $StoreEX->queryByConditionsOne(' and store.storeId = ' . $_SESSION['storeid'] . ' ');
        $smarty->assign("storedef", $storedef);
        $smarty->assign("storeid", $_SESSION['storeid']);
    }




    // $storeData = loadStore();
    // $smarty->assign("storeData", $storeData);

    $storeId = $_REQUEST['storeId'];

    $smarty->assign("hidecat", $_SESSION['hidecat']);
    //$mystordata = $myStoreRecord->load($storeId);
    //$message = "تقرير المنتجات بالمخزن اقل من الحد الأدنى لمخزن :" . $mystordata->storeName;
    $message = "تقرير المنتجات بالمخزن اقل من الحد الأدنى";
    $smarty->assign('message', $message);


    $storedetailData = showBystoreName();

    //print_r($storedetailData);


    $smarty->assign('storedetailData', $storedetailData);




    //breadcrumb
    $breadcrumbObject->add('التقارير><a href="productlimitreportController.php?do=show">تقرير المنتجات بالمخزن اقل من الحد الأدنى</a> ', 'productlimitreportController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);


    $smarty->assign("salesreport", 1);
    //here the smarty templates
    $smarty->display("productlimitreportview/show.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
  ================================ */

// show the form by store Name
function showBystoreName() {
    //to use the variable out side the funcion
    global $myStoredetailEx;
    global $ProductEX;
    global $lastLevelCatIDS;
    global $productCatDAO;
    global $catsIDS;
    global $smarty;
    $allDataArr = array();

    $storeId = $_REQUEST['storeId'];
    $order = $_REQUEST['order'];
    $searchStoreLimitType = $_REQUEST['searchStoreLimitType'];

    $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));
    }
    $productId = filter_input(INPUT_POST, 'productId');
    $searchtype = filter_input(INPUT_POST, 'searchtype');
    $smarty->assign('searchtype', $searchtype);

    $queryString = '';
    if (isset($storeId) && !empty($storeId) && $storeId != -1) {
        $queryString .= 'and  storedetail.storeid = ' . $storeId . ' ';
    } else if ($_SESSION['searchinonestore'] == 0) {
        if ($_SESSION['storeids'] != 0) {
            $queryString .= 'and  storedetail.storeid in (' . $_SESSION['storeids'] . ') ';
        }
    } else {
        $queryString .= 'and  storedetail.storeid = ' . $_SESSION['storeid'] . ' ';
        $storeId = $_SESSION['storeid'];
    }
    $queryString .= ' and product.isService = 0 ';


    if ($searchtype == 0) {
        ##productId
        if (isset($productId) && !empty($productId) && $productId != -1) {
            $queryString .= 'and  storedetail.productid  = ' . $productId . ' ';
        } elseif (isset($productCatId) && !empty($productCatId) && $productCatId != -1) {
            ##get subcat of cat
            $catsIDS = '' . $productCatId;
            getAllSubCat($productCatId, 1); //mode = 1 get all sub cats
            $productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS);
            $IDS = '0,' . $productCatId;
            foreach ($productsOfCat as $value) {
                $IDS .= ',' . $value->productId;
            }
            $queryString .= 'and  storedetail.productid  in (' . $IDS . ') ';
        }

        //select all data from Savedaily by saveid
        if ($searchStoreLimitType == 'product') {//getting limits from product table
            $storedetailData = $myStoredetailEx->queryWithStoreIdAndProductandorderLimit_f($order, $queryString);
        } else if ($searchStoreLimitType == 'productstore') {//getting limits from productstore table
            $storedetailData = $myStoredetailEx->queryWithStoreIdAndProductandorderLimit_f2($order, $queryString);
        }
    } elseif ($searchtype == 1) {
        if (isset($productCatId) && !empty($productCatId) && $productCatId != -1) {
            $lastLevelCatIDS = array();
            array_push($lastLevelCatIDS, $productCatId);
            getAllSubCat($productCatId, 2); //mode = 2 get last level cats
            foreach ($lastLevelCatIDS as $mycatId) {
                ##get subcat of cat
                $catsIDS = '' . $mycatId;
                getAllSubCat($mycatId, 1); //mode = 1 get all sub cats
                $productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS);
                $IDS = '0';
                foreach ($productsOfCat as $value) {
                    $IDS .= ',' . $value->productId;
                }
                $queryStringProduct = 'and  storedetail.productid  in (' . $IDS . ') ';

                ##now like search type == 0
                if ($searchStoreLimitType == 'product') {//getting limits from product table
                    $storedetailData = $myStoredetailEx->queryWithStoreIdAndProductandorderLimit_f($order, $queryString . $queryStringProduct);
                } else if ($searchStoreLimitType == 'productstore') {//getting limits from productstore table
                    $storedetailData = $myStoredetailEx->queryWithStoreIdAndProductandorderLimit_f2($order, $queryString . $queryStringProduct);
                }

                $resultsCount = count($storedetailData);

                if ($resultsCount > 0) {

                    $mycatObj = $productCatDAO->load($mycatId);
                    $myObj = new Storedetail();
                    $myObj->productName = $mycatObj->productCatName;

                    ##unique product cat
                    ##مبيعات البصريات
                    foreach ($storedetailData as $value) {
                        $myObj->productquantity += $value->productquantity;
                    }
                    array_push($allDataArr, $myObj);
                }
            }

            $storedetailData = $allDataArr;
        }
    }


    return $storedetailData;
}

function getAllSubCat($catid, $mode) {
    global $productCatExt;
    global $productCatDAO;
    global $catsIDS;
    global $lastLevelCatIDS;
    //mode = 1 get all sub cats
    //mode = 2 get last level cats

    $result = $productCatExt->queryByParentExt($catid);
    if (count($result) > 0) {
        foreach ($result as $data) {
            if ($mode == 1) {
                $catsIDS .= "," . $data->productCatId;
                getAllSubCat($data->productCatId, $mode);
            } elseif ($mode == 2) {
                $childData = $productCatExt->queryByParentExt($data->productCatId);
                if (count($childData) > 0) {
                    getAllSubCat($data->productCatId, $mode);
                } else {
                    if (!empty($data->productCatId)) {
                        array_push($lastLevelCatIDS, $data->productCatId);
                    }
                }
            }
        }
    } else {
//        if ($mode == 2) {
//            $childData = $productCatExt->queryByParentExt($data->productCatId);
//            if (count($childData) > 0) {
//                getAllSubCat($data->productCatId, $mode);
//            } else {
//                if (!empty($data->productCatId)) {
//                    array_push($lastLevelCatIDS, $data->productCatId);
//                }
//            }
//        }
    }
}

?>