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

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


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

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

  Controller Name :- StorereportCTRL تقرير حركة المنتجات بالمخازن

  OPERTATION in Controller

  1-load storerawmaterials data
  2-load rawmaterials data
  3-display show form

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

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

//here goes the instances and general variables
$StoremovementDAO = new StoremovementMySqlDAO();
//Storereport
$myStorereportEx = new StorereportMySqlExtDAO();
//Storedetail
$myStoredetailRecord = new StoredetailMySqlDAO();
$myStoredetailEx = new StoredetailMySqlExtDAO();
//Store
$myStoreRecord = new StoreMySqlDAO();
$myStoreRecordEx = new StoreMySqlExtDAO();
//Product
$myProductRecord = new ProductMySqlDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");
    $productId = $_REQUEST['productId'];
    $productparcode = $_REQUEST['parcode'];
    $selldiscount = $_REQUEST['selldiscount'];


    if (isset($selldiscount) && $selldiscount == '1') {
        $smarty->assign("selldiscount", 1);
    }
    $smarty->assign("selldiscount", $selldiscount);
} else if ($do == "showAll") {
    $data = showAll();
    $smarty->assign("data", $data);
}


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

//here the smarty templates
$smarty->display("StoreInquiries/show.html");
$smarty->assign("settlementstoreshow", 1);

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

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

function showAll() {

    global $myStoredetailEx;
    global $smarty;


    $productId = $_REQUEST['productId'];
    $productparcode = $_REQUEST['parcode'];
    $selldiscount = $_REQUEST['selldiscount'];

    if (isset($selldiscount) && $selldiscount == '1') {
        $smarty->assign("selldiscount", 1);
    }
    $smarty->assign("selldiscount", $selldiscount);

    $queryString = 'WHERE ';

    if (isset($productId) && empty($productparcode) && $productId != '') {
        $sizeId = 0;
        $colorId = 0;
        if (strpos($productId, "hasSizeColor") !== false) {
            $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
            $productId = $productIdComplex[0];
            $sizeId = $productIdComplex[1];
            $colorId = $productIdComplex[2];
        }

        $queryString .= " product.productId = $productId";
    } elseif (isset($productparcode) && empty($productId) && $productparcode != '') {
        $queryString .= " product.productId = $productparcode";
    } elseif (empty($productId) && empty($productparcode)) {
        $queryString .= "product.productId = ''";
    }

    $all = $myStoredetailEx->queryAllByProudctnameandParcode($queryString);
    //var_dump($all);

    foreach ($all as $item)
        if ($selldiscount == 1 && $item->selldiscount > 0) {//want to make discount and discount value >0
            if ($all->discounttype == 0) {//direct discount
                $item->productSellAllPrice -= $item->selldiscount;
                $item->productSellHalfPrice -= $item->selldiscount;
                $item->productSellUnitPrice -= $item->selldiscount;
            } elseif ($item->discounttype == 1) {//percentage disount
                $item->productSellAllPrice -= $item->productSellAllPrice * ($item->selldiscount / 100);
                $item->productSellHalfPrice -= $item->productSellHalfPrice * ($item->selldiscount / 100);
                $item->productSellUnitPrice -= $item->productSellUnitPrice * ($item->selldiscount / 100);
                //
                $item->productSellAllPrice = round($item->productSellAllPrice, 2);
                $item->productSellHalfPrice = round($item->productSellHalfPrice, 2);
                $item->productSellUnitPrice = round($item->productSellUnitPrice, 2);
            }
        }

    return $all;
}

?>