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

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

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

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

//productionExecution
require_once('../models/dao/ProductionexecutionDAO.class.php');
require_once('../models/dto/Productionexecution.class.php');
require_once('../models/mysql/ProductionexecutionMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionexecutionMySqlExtDAO.class.php');
//productionExecutionProduct
require_once('../models/dao/ProductionexecutionoutputDAO.class.php');
require_once('../models/dto/Productionexecutionoutput.class.php');
require_once('../models/mysql/ProductionexecutionoutputMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionexecutionoutputMySqlExtDAO.class.php');
//productionExecutionOutput
require_once('../models/dao/ProductionexecutionproductDAO.class.php');
require_once('../models/dto/Productionexecutionproduct.class.php');
require_once('../models/mysql/ProductionexecutionproductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionexecutionproductMySqlExtDAO.class.php');
//productionExecutionWaste
require_once('../models/dao/ProductionexecutionwasteDAO.class.php');
require_once('../models/dto/Productionexecutionwaste.class.php');
require_once('../models/mysql/ProductionexecutionwasteMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductionexecutionwasteMySqlExtDAO.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'];
$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");
/* ======================

  Controller Name :- productionExecutionCombinedReportController

  OPERTATION in Controller///////////////////////////////////////////////////

  1-display add form
  2- add in productCat tble
  4-display sucess or error
  5-display show and delete
  6-delete from productCat tbl
  7-display edit
  8- update productCat tbl
  ======================== */


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

//here goes the instances and general variables
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//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();

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

//productionExecution
$productionExecution = new Productionexecution();
$productionExecutionDao = new ProductionexecutionMySqlDAO();
$productionExecutionExt = new ProductionexecutionMySqlExtDAO();

//productionExecutionProduct
$productionExecutionProduct = new Productionexecutionproduct();
$productionExecutionProductDao = new ProductionexecutionproductMySqlDAO();
$productionExecutionProductExt = new ProductionexecutionproductMySqlExtDAO();

//productionExecutionOutput
$productionExecutionOutput = new Productionexecutionoutput();
$productionExecutionOutputDao = new ProductionexecutionoutputMySqlDAO();
$productionExecutionOutputExt = new ProductionexecutionoutputMySqlExtDAO();

//productionExecutionWaste
$productionExecutionWaste = new Productionexecutionwaste();
$productionExecutionWasteDao = new ProductionexecutionwasteMySqlDAO();
$productionExecutionWasteExt = new ProductionexecutionwasteMySqlExtDAO();

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

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



$ProgramsettingDAO = new ProgramsettingsMySqlDAO();



//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    $from = filter_input(INPUT_POST, 'from');
    $to = filter_input(INPUT_POST, 'to');

    $query = "";

    if ($from)
        $query .= " and productionexecution.orderdate >= '$from'";

    if ($to)
        $query .= " and productionexecution.orderdate <= '$to'";

    $allOutput = $productionExecutionOutputExt->queryAllOutputGroupedEx($query);
    $proArr = array();

    $allTotalCost = 0;
    $execId = 0;
    $allActual = 0;
    $allOther = 0;
    $allTotal = 0;
    $allActualTotal = 0;
    foreach ($allOutput as $output) {
        $allProducts = $productionExecutionProductDao->queryByExecutionid($output->exeutionid);
        $totalActualCost = 0;
        $totalCost = 0;

        foreach ($allProducts as $pro) {

            $totalActualCost += $pro->actualcost;
            $totalCost += $pro->cost;
        }
        //إجمالي التكلفة الفعلية
        $output->actualCost = $totalActualCost;
        $output->cost = $totalCost;

        $output->othrCost = $output->rentcost + $output->othercost;
        $output->totalCost = $output->othrCost + $totalActualCost;
        $output->totalPredictedCost = $output->othrCost + $totalCost;

        array_push($proArr, $output);

        $allActual += $totalActualCost;
        $allOther += $output->othrCost;
        $allTotal += $output->totalCost;
        $allTotalCost += $output->totalPredictedCost;
//        $allActualTotal +=($output->totalCost / $output->num);
    }
    $smarty->assign('allActual', $allActual);
    $smarty->assign('allOther', $allOther);
    $smarty->assign('allTotal', $allTotal);
    $smarty->assign('allActualTotal', $allActualTotal);
    $smarty->assign('allTotalCost', $allTotalCost);

    $smarty->assign("allOrders", $proArr);

    $smarty->display("productionExecutionReportView/combinedReport.html");
} elseif ($do == "show") {

    $from = filter_input(INPUT_POST, 'from');
    $to = filter_input(INPUT_POST, 'to');

    $query = "";

    if ($from)
        $query .= " and productionexecution.orderdate >= '$from'";

    if ($to)
        $query .= " and productionexecution.orderdate <= '$to'";

    $allOutput = $productionExecutionOutputExt->queryAllOutputGroupedEx($query);
    $proArr = array();

    $allTotalCost = 0;
    $execId = 0;
    $allActual = 0;
    $allOther = 0;
    $allTotal = 0;
    $allActualTotal = 0;
    foreach ($allOutput as $output) {
        $allProducts = $productionExecutionProductDao->queryByExecutionid($output->exeutionid);
        $totalActualCost = 0;
        $totalCost = 0;

        foreach ($allProducts as $pro) {

            $totalActualCost += $pro->actualcost;
            $totalCost += $pro->cost;
        }
        //إجمالي التكلفة الفعلية
        $output->actualCost = $totalActualCost;
        $output->cost = $totalCost;

        $output->othrCost = $output->rentcost + $output->othercost;
        $output->totalCost = $output->othrCost + $totalActualCost;
        $output->totalPredictedCost = $output->othrCost + $totalCost;

        array_push($proArr, $output);

        $allActual += $totalActualCost;
        $allOther += $output->othrCost;
        $allTotal += $output->totalCost;
        $allTotalCost += $output->totalPredictedCost;
//        $allActualTotal +=($output->totalCost / $output->num);
    }
    $smarty->assign('allActual', $allActual);
    $smarty->assign('allOther', $allOther);
    $smarty->assign('allTotal', $allTotal);
    $smarty->assign('allActualTotal', $allActualTotal);
    $smarty->assign('allTotalCost', $allTotalCost);

    $smarty->assign("allOrders", $proArr);

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

    $smarty->display("productionExecutionReportView/combinedReport.html");
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
//$smarty->assign("productScripts",1);

$smarty->display("footer.html");
?>