<?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
include("../public/include_dao.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');
//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');
//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');
//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');
//Productionrate
require_once('../models/dao/ProductionrateDAO.class.php');
require_once('../models/dto/Productionrate.class.php');
require_once('../models/mysql/ProductionrateMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionrateMySqlExtDAO.class.php');
//Productionrateproduct
require_once('../models/dao/ProductionrateproductDAO.class.php');
require_once('../models/dto/Productionrateproduct.class.php');
require_once('../models/mysql/ProductionrateproductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionrateproductMySqlExtDAO.class.php');

//task order
require_once('../models/dao/TaskorderDAO.class.php');
require_once('../models/dto/Taskorder.class.php');
require_once('../models/mysql/TaskorderMySqlDAO.class.php');
require_once('../models/mysql/ext/TaskorderMySqlExtDAO.class.php');
//task order products
require_once('../models/dao/TaskorderproductsDAO.class.php');
require_once('../models/dto/Taskorderproduct.class.php');
require_once('../models/mysql/TaskorderproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/TaskorderproductsMySqlExtDAO.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');
//get the do the action
$do = $_GET['do'];

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

  Controller Name :- productReportsAjaxController

  OPERTATION in Controller
  ======================== */

//here goes the instances and general variables
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();
//Storereport
$storeReportDAO = new StorereportMySqlDAO();
$storeReportExt = new StorereportMySqlExtDAO();
//Store
$storeDAO = new StoreMySqlDAO();
//Productcat
$myProductcatRecord = $productCatDAO = new ProductcatMySqlDAO();
$myProductcatEx = $productCatExt = new ProductcatMySqlExtDAO();

//Productionrate
$productionrate = new Productionrate();
$productionrateDAO = new ProductionrateMySqlDAO();
$productionrateEX = new ProductionrateMySqlExtDAO();

//Productionrateproduct
$Productionrateproduct = new Productionrateproduct();
$ProductionrateproductDAO = new ProductionrateproductMySqlDAO();
$ProductionrateproductEX = new ProductionrateproductMySqlExtDAO();

//taskorder
$taskorder = new Taskorder();
$taskorderDao = new TaskorderMySqlDAO();
$taskorderExt = new TaskorderMySqlExtDAO();

//taskorder products
$taskorderproducts = new Taskorderproduct();
$taskorderproductsDao = new TaskorderproductsMySqlDAO();
$taskorderproductsExt = new TaskorderproductsMySqlExtDAO();
//Programsetting
$programSettingDAO = new ProgramsettingsMySqlDAO();


$Programsettingdata = $programSettingDAO->load(1);
$today = date("Y-m-d");
$smarty->assign("today", $today);


if ($do == "getProductionEquationProducts") {
    //$allEquations = $productionrateEX->queryAllProductEquation($id);


    $productsData = getFinalProductsWithOutProductionEquation();
    $smarty->assign("productsData", $productsData);
} elseif ($do == "getEquationProducts") {

    $proId = filter_input(INPUT_POST, 'proId');
    $proNumber = filter_input(INPUT_POST, 'proNumber');

    $productEquationRow = $productionrateEX->queryOneRateByfinalName($proId);
    $allRawProducts = $ProductionrateproductEX->queryAllByProductionRateId($productEquationRow->productionRateId);
    $smarty->assign('allRawProducts', $allRawProducts);
    $smarty->assign('proNumber', $proNumber);

    $smarty->display('taskOrderView/rawProducts.html');
} elseif ($do == "getEqProducts") {

    $proId = filter_input(INPUT_POST, 'proId');
    $proNumber = filter_input(INPUT_POST, 'proNumber');

    $productEquationRow = $productionrateEX->queryOneRateByfinalName($proId);
    $allRawProducts = $ProductionrateproductEX->queryAllByProductionRateId($productEquationRow->productionRateId);
    $smarty->assign('allRawProducts', $allRawProducts);
    $smarty->assign('proNumber', $proNumber);

    $smarty->display('productionExecutionView/rawProducts.html');
}

function getFinalProductsWithOutProductionEquation() {
    global $myProductEx;
    global $Programsettingdata;

    $categories;
    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = $Programsettingdata->searchlimit; //intval($_GET['page_limit']);
    $storeid = $_REQUEST["storeid"];
    $hiddenpricetype = $_REQUEST["hiddenpricetype"];

    $productsData = $myProductEx->getFinalProductsWithProductionRate($name);
    if (count($productsData) > 0) {
        foreach ($productsData as $pro) {
            $parentId = $pro->productCatId;
//names of product's parents
            $pathArr = fetch_recursive($parentId, $categories);

//send product id and product name as something that select2 expects.
            $row_array['id'] = $pro->productId;
            $row_array['text'] = $pro->productName . '/' . $pathArr;


            $row_array['productquantity'] = $pro->productquantity;


//جملة
            if ($hiddenpricetype == 0) {

                $price = 'productSellAllPrice';
                $row_array['price'] = $pro->productSellAllPrice;
            }
//تجزئة
            elseif ($hiddenpricetype == 1) {
                $price = 'productSellUnitPrice';
                $row_array['price'] = $pro->productSellUnitPrice;
            }
//نصف جملة
            elseif ($hiddenpricetype == 2) {
                $price = 'productSellHalfPrice';
                $row_array['price'] = $pro->productSellHalfPrice;
            }


            array_push($return_arr, $row_array);
        }
    }

//echo $return_arr;
    echo json_encode($return_arr);
}

function fetch_recursive($parentid, $categories) {
    global $myProductcatEx;
//print($parentid."<br>");
    $catData = $myProductcatEx->getCategoryAndParentByCatId($parentid);
//print_r($catData);
    if (count($catData) > 0) {
        $categories .= $catData->productCatName . '/';
        $newParentId = $catData->productCatParent;

//if($newParentId != 0 && isset($newParentId))
        {
//$newParentName = $catData->parentName;
//$categories .= $newParentName.'/';
            return fetch_recursive($newParentId, $categories);
        }
    }
    $categories = substr($categories, 0, strlen($categories) - 1);
    return $categories;
}

?>