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

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


//Storemovement
require_once('../models/dao/StoremovementDAO.class.php');
require_once('../models/dto/Storemovement.class.php');
require_once('../models/mysql/StoremovementMySqlDAO.class.php');
require_once('../models/mysql/ext/StoremovementMySqlExtDAO.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');
//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');
//Productserial
require_once('../models/dao/ProductserialDAO.class.php');
require_once('../models/dto/Productserial.class.php');
require_once('../models/mysql/ProductserialMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductserialMySqlExtDAO.class.php');

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


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

  Controller Name :- transfermoneyAjaxCTRL

  OPERTATION in Controller

  1-get save cuurent value by saveid using ajax
  2-load save data tbl expect saveid


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


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

//Store
$myStoreEx = new StoreMySqlExtDAO();


//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEX = new ProductMySqlExtDAO();

//Storemovement
$Storemovement = new Storemovement();
$StoremovementDAO = new StoremovementMySqlDAO();
$StoremovementEX = new StoremovementMySqlExtDAO();

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

$OtherstoremovementDAO = new OtherstoremovementMySqlDAO();

//Programsetting
$programSettingDAO = new ProgramsettingsMySqlDAO();
$programSettingEx = new ProgramsettingsMySqlExtDAO();
//Productserial
$Productserial = $myProductserial = new Productserial();
$ProductserialDAO = new ProductserialMySqlDAO();
$ProductserialEX = $myProductserialEx = new ProductserialMySqlExtDAO();
//check and use the condetion that suite this action


$Programsettingdata = $programSettingDAO->load(1);
if ($do == "loadstoreExpectId") {
    $storeidFrom = $_GET['storeidfrom'];

    // here the function that do the action
    $storeToData = loadstoreExpectId($storeidFrom);
    $smarty->assign("storeToData", $storeToData);


    //here the smarty templates
    $smarty->display("storemovementview/storeto.html");
} elseif ($do == "getserial") {
    $productId = $_POST['proid'];

    $parcode = '';
    if (strpos($productId, "hasSizeColor") !== false) {
        $productIdComplex = explode('-', str_replace("hasSizeColor", "", $productId));
        $productId = $productIdComplex[0];
        $sizeId = $productIdComplex[1];
        $colorId = $productIdComplex[2];
        $storeid = 1; //store id value is not important now make it fixed and = 1
        $sizeColorStoreDetailId = $sizeColorStoreDetailEX->getIdByProductStoreSizeAndColorEX($productId, $storeid, $sizeId, $colorId);
        if (isset($sizeColorStoreDetailId) && $sizeColorStoreDetailId > 0) {//sizecolor product
            $storedetailData = $sizeColorStoreDetailDAO->load($sizeColorStoreDetailId);
            $parcode = $storedetailData->parcode;
        }
    } else {
        $pro = $myProductRecord->load($productId);
        $parcode = $pro->parcode;
    }
    echo $parcode;
} elseif ($do == "getproductbyparcode") {
    $parcode = $_POST['parcode'];
    if ($parcode[0] == 'i' && $Programsettingdata->usedParcode == 1) {//usedParcode == 1 it is id coming after i
        $parcode = str_replace("i", "", $parcode);
        $productData = $myProductEX->loadProduct((int) $parcode);
        $pro = array($productData);
    } else {
        $pro = $myProductEX->queryByParcodeEX($parcode);
    }
    if (count($pro) == 1) {
        echo $pro[0]->productId . "///" . $pro[0]->productName . "///" . $pro[0]->productDescription."///".$pro[0]->productBuyPrice . "///" . $pro[0]->productSellAllPrice . "///" . $pro[0]->productSellUnitPrice . "///" . $pro[0]->productSellHalfPrice;
    } else {
        $sizeColor = $sizeColorStoreDetailEX->queryByParcodeEX($parcode);

        $arr = -1;
        if ($sizeColor->id > 0) {
            $productId = "hasSizeColor" . $sizeColor->productid . "-" . $sizeColor->sizeid . "-" . $sizeColor->colorid;
            $productName = $sizeColor->productName . "/" . $sizeColor->productCatName . "/" . $sizeColor->sizeName . "/" . $sizeColor->colorName;
            echo $productId . "///" . $productName . "///" . $sizeColor->productDescription;
        } else {
            $store = (int) $_POST["store"];
            $proSerials = $ProductserialEX->queryByQueryString(" and productserial.serialnumber = '" . $parcode . "' and productserial.storeid = $store");
            if (count($proSerials) > 0) {
                echo $proSerials[0]->productId . "///" . $proSerials[0]->productName . "///" . $proSerials[0]->productDescription . "/" . $proSerials[0]->productCatName . "///" . $proSerials[0]->don . "///" . $proSerials[0]->productserailid . "///" . $proSerials[0]->enddate;
            } else {
                echo -1;
            }
        }
    }
} elseif ($do == "loadstoreExpectIditr") {
    $storeidFrom = $_GET['storeidfrom'];
    $itr = $_GET["itr"];

    // here the function that do the action
    $storeToData = loadstoreExpectId($storeidFrom);
    $smarty->assign("storeToData", $storeToData);
    $smarty->assign("itr", $itr);

    //here the smarty templates
    $smarty->display("otherstoremovementview/storeto.html");
} elseif ($do == "checkserail") {
    $serail = $_POST['serail'];
    // here the function that do the action
    // $savecurrentValue = $StoremovementEX->queryStoremovementByoperationnum2($serail);
    $savecurrentValue = $OtherstoremovementDAO->queryByOprationnumber($serail);


    if (!empty($savecurrentValue)) {
        echo -1;
    } else {
        echo 0;
    }
} elseif ($do == "loadproducts") {
    $storeidFrom = $_GET['storeidfrom'];
    $itr = $_GET['itr'];

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

    $smarty->assign("itr", $itr);

    //here the smarty templates
    $smarty->display("storemovementview/product.html");
} elseif ($do == "getquantity") {
    $storeIdfrom = $_GET['storeidfrom'];
    $productId = $_GET['productid'];


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

    if ($productQuantity != "") {
        echo $productQuantity;
    } else {
        echo '';
    }
} elseif ($do == "anotherProduct") {
    $storeIdfrom = $_GET['storeidfrom'];
    $productsArr = $_GET['productsArr'];

    $productsArr = substr($productsArr, 0, strlen($productsArr) - 1);

    // here the function that do the action
    $productData = getAnotherProduct($storeIdfrom, $productsArr);


    $smarty->assign("productData", $productData);

    $smarty->display("storemovementview/anotherproduct.html");
} elseif ($do == "checkOpeNo") {
    $operationnum = $_POST['operationnum'];

    $operationData = $StoremovementEX->operationData($operationnum);
    if (empty($operationData)) {
        //its ok its unique
        echo 0;
    } else {
        //repeated generate new one
        $nextId = $StoremovementEX->queryAlloperationnum();
        $last_val = $nextId[0];
        $last = $last_val->operationnum + 1;
        //echo -1;
        echo $last;
    }
}









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

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

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

    //load all save data
    $storeData = $myStoreEx->queryWithConditionExpectId($storeidFrom);

    return $storeData;
}

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

// get product without array of product from storedetail tbl
function getAnotherProduct($storeid, $productsArr) {

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

    //select data from storerawmaterialdetails by storeid and rawmaterialid
    $productData = $myStoredetailEx->queryWithStoreIdWithoutProductsId($storeid, $productsArr);


    return $productData;
}

?>