<?php

//the global file operation
session_start();
ob_start();

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

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



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


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

  Controller Name :- settlementsaveCTRL  تسوية خزنة بالخصم والاإضافة

  OPERTATION in Controller

  1-display add form
  3-update save value (-) or (+)
  4-insert into savedaily tbl
  5-display sucess or error
  6-display show form

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


//here goes the instances and general variables
//Storereport
$myStorereport = new Storereport();
$myStorereportRecord = new StorereportMySqlDAO();
$myStorereportEx = new StorereportMySqlExtDAO();

//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();
$myStoredetailRecord = new StoredetailMySqlDAO();

//Store
$myStoreRecord = new StoreMySqlDAO();


//Product
$myProductRecord = new ProductMySqlDAO();

//
$sizeColorStoreDetail = new Sizecolorstoredetail();
$sizeColorStoreDetailDAO = new SizecolorstoredetailMySqlDAO();
$sizeColorStoreDetailEX = new SizecolorstoredetailMySqlExtDAO();


if ($do == "loadproducts") {
    $storeId = $_GET['storeid'];

    // here the function that do the action
    $productData = loadProductInStore($storeId);
    $smarty->assign("productData", $productData);


    //here the smarty templates
    $smarty->display("settlementstoreview/product.html");
}


//check and use the condetion that suite this action
elseif ($do == "getquantity") {
    $storeId = $_GET['storeid'];
    $productId = $_GET['productid'];


    // here the function that do the action
    $productQuantity = getProductAmountInStore($storeId, $productId);

    echo $productQuantity;
}











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

//select all save data
function loadProductInStore($storeidFrom) {

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

    //load all save data
    $productData = $myStoredetailEx->queryWithStoreId($storeidFrom);

    return $productData;
}

// get productquantity from storedetail tbl
function getProductAmountInStore($storeid, $productId) {

    //to use the variable out side the funcion
    global $myStoredetailEx;
    global $sizeColorStoreDetailDAO;
    global $sizeColorStoreDetailEX;

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



    if (isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) {//sizecolor product
        $storedetailData = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);
        $storedetailId = $storedetailData->id;
        $productquantityBefore = $storedetailData->quantity;
    } else {
        //select data from storerawmaterialdetails by storeid and rawmaterialid
        $storedetailData = $myStoredetailEx->queryWithStoreAndProduct($productId, $storeid);

        $productquantityBefore = $storedetailData->productquantity;
    }



    return $productquantityBefore;
}

?>