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

require_once('../models/dao/StorereportDAO.class.php');
require_once('../models/dto/Storereport.class.php');
require_once('../models/mysql/StorereportMySqlDAO.class.php');
require_once('../models/mysql/ext/StorereportMySqlExtDAO.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');
//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');

//get the do the action


require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');

//
require_once('../models/dao/SizecolorstoredetailDAO.class.php');
require_once('../models/dto/Sizecolorstoredetail.class.php');
require_once('../models/mysql/SizecolorstoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SizecolorstoredetailMySqlExtDAO.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');

//Usergroup
require_once ('../models/dao/UsergroupDAO.class.php');
require_once ('../models/dto/Usergroup.class.php');
require_once ('../models/mysql/UsergroupMySqlDAO.class.php');
require_once ('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');


$do = $_GET['do'];

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

  Controller Name :- storedetailCTRL تقرير مخزون اول مدة

  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
//Store
$myStoreRecord = new StoreMySqlDAO();
//Product
$myProductRecord = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Storereport
$Storereport = new Storereport();
$StorereportDAO = new StorereportMySqlDAO();
$StorereportEX = new StorereportMySqlExtDAO();
//
$sizeColorStoreDetail = new Sizecolorstoredetail();
$sizeColorStoreDetailDAO = new SizecolorstoredetailMySqlDAO();
$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();
//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();



$Usergroupdata = $UsergroupDAO->load($_SESSION['usergroupid']);
$smarty->assign("Usergroupdata", $Usergroupdata);


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

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

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

    $productId = $_REQUEST['productId'];
    $storeId = $_REQUEST['storeId'];


    //echo "$myusergroupdata->hidecat".$myusergroupdata->hidecat;
    $smarty->assign('hidecat', $myusergroupdata->hidecat);


    if (isset($productId) && $productId != '-1') {
        $storedetailData = show();



        $smarty->assign('storedetailData', $storedetailData[0]);
    }

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);
    //here the smarty templates
    $smarty->display("productcardview/show.html");
    $smarty->assign("settlementstoreshow", 1);
}
//here the global templates
$smarty->display("footer.html");

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

//select all product data
function loadProducts() {
    //to use the variable out side the funcion
    global $myProductRecord;

    //load all product data
    $productData = $myProductRecord->queryByConditions(0);

    return $productData;
}

//select all store data
function loadStore() {
    //to use the variable out side the funcion
    global $myStoreRecord;

    //load all store data
    $storeData = $myStoreRecord->queryByConditions(0);

    return $storeData;
}

// show the form by product name
function show() {
    global $smarty;
    //to use the variable out side the funcion
    global $StorereportDAO;
    global $ProgramsettingDAO;
    global $StorereportEX;
    global $Storereport;

    global $myStoreRecord;
    global $myProductRecord;
    global $productExt;
    global $sizeColorStoreDetailEX;


    global $Usergroupdata;



    $productId = $_REQUEST['productId'];
    $storeId = $_REQUEST['storeId'];
    $to = $_REQUEST['to'];
    $from = $_REQUEST['from'];
    $productBuyPrice = $_REQUEST['productBuyPrice'];
    $productSellUnitPrice = $_REQUEST['productSellUnitPrice'];
    $productSellAllPrice = $_REQUEST['productSellAllPrice'];
    $productSellHalfPrice = $_REQUEST['productSellHalfPrice'];
    $myprodactdata;
    $productquantity;
    $queryString = ' AND';
    $sizeColorStoreDetails = '';
    if (isset($productId) && $productId != '-1' && $productId != '' && isset($storeId) && $storeId != '-1') {
        $sizeId = 0;
        $colorId = 0;
        $sizeColorStoreDetailId = 0;
        if (strpos($productId, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
            $productId = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
            $sizeColorStoreDetails = $sizeColorStoreDetailEX->getParcodeByProductSizeAndColorAndStoreEX($productId, $sizeId, $colorId, $storeId);
        }
        $myprodactdata = $productExt->loadExtforcard($productId, $storeId);
        if (isset($sizeColorStoreDetails) && !empty($sizeColorStoreDetails)) {
            $myprodactdata->productName .= "/$sizeColorStoreDetails->sizeName/$sizeColorStoreDetails->colorName";
            $myprodactdata->parcode = $sizeColorStoreDetails->parcode;
            $myprodactdata->productquantity = $sizeColorStoreDetails->quantity;
            $queryString .= " sizeid = $sizeId and colorid = $colorId AND";
        }


        //  print_r($myprodactdata);

        $message = $message . " اسم الصنف  :" . $myprodactdata->productName . "  ";
        $message = $message . "  <br>  رقم الصنف : " . $myprodactdata->parcode . "";
        // $message = $message . "  <br>  الكميه الحاليه : " . $myprodactdata->productquantity . "";
        $productquantity = $myprodactdata->productquantity;


        $queryString .= ' storereport.productid = ' . $productId . ' AND';

        //	print_r('ddddddddddddd'.$queryString);
    }

    if (isset($productId) && $productId != '-1' && $productId != '' && $storeId == '-1') {
        $sizeId = 0;
        $colorId = 0;
        $sizeColorStoreDetailId = 0;
        if (strpos($productId, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
            $productId = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
            $sizeColorStoreDetails = $sizeColorStoreDetailEX->getSumProductSizeColorQuantityInAllStores($productId, $sizeId, $colorId);
        }

        $myprodactdata = $productExt->loadExtByProductId2($productId);
        if (isset($sizeColorStoreDetails) && !empty($sizeColorStoreDetails)) {
            $myprodactdata->productName .= "/$sizeColorStoreDetails->sizeName/$sizeColorStoreDetails->colorName";
            $myprodactdata->parcode = $sizeColorStoreDetails->parcode;
            $myprodactdata->productquantity = $sizeColorStoreDetails->quantity;
            $queryString .= " sizeid = $sizeId and colorid = $colorId AND";
        }


        // print_r($myprodactdata);

        $message = $message . " اسم الصنف  :" . $myprodactdata->productName . "  ";
        $message = $message . "  <br>  رقم الصنف : " . $myprodactdata->parcode . "";
        //$message = $message . "  <br>  الكميه الحاليه : " . $myprodactdata->productquantity . "";
        $productquantity = $myprodactdata->productquantity;



        $queryString .= ' storereport.productid = ' . $productId . ' AND';

        //	print_r('ddddddddddddd'.$queryString);
    }

    if (isset($productBuyPrice) && $productBuyPrice != '' && $Usergroupdata->hideBillPrices == 0) {
        $message = $message . "  <br>    سعر شراء الصنف : " . $myprodactdata->productBuyPrice . "";
    }


    if (isset($productSellAllPrice) && $productSellAllPrice != '' && $Usergroupdata->hideBillPrices == 0) {
        $message = $message . "  <br>  سعر   بيع الصنف جملة : " . $myprodactdata->productSellAllPrice . "";
    }


    if (isset($productSellHalfPrice) && $productSellHalfPrice != '' && $Usergroupdata->hideBillPrices == 0) {
        $message = $message . "  <br>  سعر  بيع  الصنف نصف جمله : " . $myprodactdata->productSellHalfPrice . "";
    }

    if (isset($productSellUnitPrice) && $productSellUnitPrice != '' && $Usergroupdata->hideBillPrices == 0) {
        $message = $message . "  <br>  سعر  بيع الصنف  تجزئة : " . $myprodactdata->productSellUnitPrice . "";
    }



    if (isset($storeId) && $storeId != '-1') {
        $queryString .= '  storereport.storeid = ' . $storeId . ' AND';
        $mystordata = $myStoreRecord->load($storeId);
        $message = $message . "  <br>  المخزن : " . $mystordata->storeName . "";
    }


    if (isset($from) && $from != '' && isset($to) && $to != '') {
        $queryString .= '  storereportdate >=  "' . $from . '" AND storereportdate <= "' . $to . '" ';



        $message = $message . "<br> من تاريخ  " . $from . "  الى " . $to . "";
    }


    $arr = explode(' ', $queryString);
    //print_r($arr);
    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') {//remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
        }

        //print("<br>queryString: ".$queryString."<br>");
    }

    //select all data from storedetail by productId
    $storedetailData = $StorereportEX->queryAllqueryString($queryString);
    //select all data from storedetail where storereporttype = 1
    $totalStoreReportTypeOneData = $StorereportEX->queryAllStoreReportTypeEqualOne($queryString);

    //select all data from storedetail where storereporttype = 0
    $totalStoreReportTypeZeroData = $StorereportEX->queryAllStoreReportTypeEqualZero($queryString);

    //select Get Quantity Of First Period from storedetail   ( limit 1 )
    $quantityOfFirstPeriod = $StorereportEX->getQuantityOfFirstPeriod($queryString);


    $smarty->assign('message', $message);
    $smarty->assign('totalStoreReportTypeOneData', $totalStoreReportTypeOneData[0]);
    $smarty->assign('totalStoreReportTypeZeroData', $totalStoreReportTypeZeroData[0]);
    $smarty->assign('productquantity', $productquantity);
    $smarty->assign('quantityOfFirstPeriod', $quantityOfFirstPeriod[0]);



    return array($storedetailData, $sumValue);
}

// show the form
function showAll() {
    global $myStoredetailEx;

    $storedetailData = $myStoredetailEx->queryWithOrder();

    $sumValue = 0;
    if (count($storedetailData) > 0) {
        foreach ($storedetailData as $storedetail) {
            //إجمالي سعر المنتجات
            $productBuyPrice = $storedetail->productBuyPrice;
            $productQuantity = $storedetail->productquantity;
            $SumProductPrice = $productBuyPrice * $productQuantity;

            $sumValue += $SumProductPrice;

            ///////////To show product category name (the product whole path)
            $parentId = $storedetail->productCatId;
            //names of product's parents
            $pathArr = getProductPath_recursive($parentId, $categories);

            //new product name with all path
            $storedetail->productName = $storedetail->productName . '/' . $pathArr;
        }
    }
    return array($storedetailData, $sumValue);
}

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

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

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

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

function loadProductUnitWithProductAndUnit($productId, $unitId) {

    //to use the variable out side the funcion
    global $myProductunitEx;

    // select all data from productunit tbl
    $productunitData = $myProductunitEx->queryWithProductIdAndUnitId($productId, $unitId);

    return $productunitData;
}

?>