<?php

//the global file operation
//include("../public/impOpreation.php");
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");
include_once("initiateStaticSessionCommingWithCurl.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');


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


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

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

  OPERTATION in Controller

  1-load save data
  2-display show form

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

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

//Store
$myStoreRecord = new StoreMySqlDAO();


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


$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();



//check and use the condetion that suite this action

if ($do == "show") {


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

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

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

    if (isset($productId) && $productId != "-1") {
        $storedetailData = showByProductName();
    } elseif (isset($storeId) && $storeId != "-1") {
        $storedetailData = showBystoreName();
    } else {
        //$storedetailData=showAll();
    }

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



    //here the smarty templates
    $smarty->display("storedetailview/storedetail.html");
} elseif ($do == 'catwithpro') {
    $catwipro = $_POST['catwipro'];
    $childitr = $_POST['childitr'] + 1;
    //print_r($childitr);
    echo '<input type="hidden" name="counter" value="' . $childitr . '"/>';
    echo '<input type="hidden" id="nonrepeat" name="nonrepeat" value="' . $catwipro . '"/>';

    $getChild = $productCatExt->getChilds($catwipro);
    $product = $myProductEx->queryByProductCatIdNotService($catwipro);
    if (count($getChild) > 0 && empty($product)) {
        $slct = '<div class="form-group subchild"  id="subchild' . $childitr . '">';
        $slct .= '<label class="sr-only">تصنيف المنتج</label>';
        $slct .= '<select class="select2 wdt300 productCatId2" data-notrepeat="' . $catwipro . '"  name="productChild' . $childitr . '" id="productChild' . $childitr . '"  style="">';
        $slct .= '<option value="-1">اختر تصنيف</option>';
        foreach ($getChild as $child) {
            $slct .= '<option value="' . $child->productCatId . '">' . $child->productCatName . '</option>';
        }
        $slct .= '</select>';
        $slct .= '</div>';

        echo $slct;
    } elseif (count($product) > 0) {
        $slct = '<div class="form-group subchild" id="prochild' . $childitr . '">';


        $slct .= '<label class="sr-only"> المنتج</label>';
        $slct .= '<select class="select2 wdt300 " data-notrepeat="' . $catwipro . '"  name="productId' . $childitr . '" id="productId' . $childitr . '">';
        $slct .= '<option value="-1">اختر المنتج</option>';
        foreach ($product as $child) {
            $slct .= '<option value="' . $child->productId . '">' . $child->productName . '</option>';
        }
        $slct .= '</select>';
        $slct .= '</div>';

        echo $slct;
    }
} elseif ($do == "productsquantity") {
    $storeid = (int) $_POST['storeid'];
    $proids = $_POST['proids'];
    //
    $ids = '';
    foreach (explode(',', $_POST['proids']) as $value) {
        $ids .= (int) $value . ',';
    }
    $ids = rtrim($ids, ',');
    //
    $query = " and storeid =$storeid and product.webApiId in(" . $ids . ")";
    $result = $myStoredetailEx->getApiProductsData($query);
    $ret_arr = array();
    foreach ($result as $value) {
        $obj = new stdClass();
        $obj->id = $value->webApiId;
        $obj->quantity = $value->productquantity;
        array_push($ret_arr, $obj);
    }

    echo json_encode($ret_arr);
} elseif ($do == "getProSerials") {
    $proId = (int) $_POST['proId'];
    $qty = (int) $_POST['qty'];
    $productSerials = R::getAll('SELECT productserial.*,soldserialproduct.quantity
                FROM productserial
		left join (
                    select productserailid,sum(soldserialproduct.quantity) as quantity
                    from soldserialproduct
                    where del=0
                    group by soldserialproduct.productserailid
                ) as soldserialproduct on soldserialproduct.productserailid=productserial.productserailid
		where productid = ' . $proId . ' order by productserial.productserailid');
    if (count($productSerials) > 0) {
        $smarty->assign("productSerials", $productSerials);
        $smarty->display("storedetailview/proSerials.html");
    } else
        echo -1;
}



/* ===============================
  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 showByProductName()
{
    //to use the variable out side the funcion
    global $myStoredetailEx;

    $productId = $_REQUEST['productId'];


    //select all data from storedetail by productId
    $storedetailData = $myStoredetailEx->queryWithProductId($productId);

    $sumValue = 0;
    foreach ($storedetailData as $storedetail) {
        $productSellUnitPrice = $storedetail->productSellUnitPrice;
        $productQuantity = $storedetail->productquantity;
        $SumProductPrice = $productSellUnitPrice * $productQuantity;

        $sumValue = $SumProductPrice + $sumValue;
    }

    return array($storedetailData, $sumValue);
}

// show the form by store Name
function showBystoreName()
{
    //to use the variable out side the funcion
    global $myStoredetailEx;

    $storeId = $_REQUEST['storeId'];

    //select all data from Savedaily by saveid
    $storedetailData = $myStoredetailEx->queryWithStoreId($storeId);

    $sumValue = 0;
    foreach ($storedetailData as $storedetail) {
        $productSellUnitPrice = $storedetail->productSellUnitPrice;
        $productQuantity = $storedetail->productquantity;
        $SumProductPrice = $productSellUnitPrice * $productQuantity;

        $sumValue = $SumProductPrice + $sumValue;
    }

    return array($storedetailData, $sumValue);
}

// show the form
function showAll()
{
    //to use the variable out side the funcion
    global $myStoredetailEx;

    //select all data from Savedaily by saveid
    $storedetailData = $myStoredetailEx->queryWithOrder();

    $sumValue = 0;
    foreach ($storedetailData as $storedetail) {
        $productSellUnitPrice = $storedetail->productSellUnitPrice;
        $productQuantity = $storedetail->productquantity;
        $SumProductPrice = $productSellUnitPrice * $productQuantity;

        $sumValue = $SumProductPrice + $sumValue;
    }

    return array($storedetailData, $sumValue);
}
