<?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");
include("../library/uploadImages.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');


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

//Simplemanufacturesetting
require_once('../models/dao/SimplemanufacturesettingDAO.class.php');
require_once('../models/dto/Simplemanufacturesetting.class.php');
require_once('../models/mysql/SimplemanufacturesettingMySqlDAO.class.php');
require_once('../models/mysql/ext/SimplemanufacturesettingMySqlExtDAO.class.php');
//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.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 :- taskOrderController

  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();

//Productcat
$productCat = new Productcat();
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();
//Simplemanufacturesetting
$simplesetting = new Simplemanufacturesetting();
$simplesettingDao = new SimplemanufacturesettingMySqlDAO();
$simplesettingExt = new SimplemanufacturesettingMySqlExtDAO();

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

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

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

    //$rows=$productExt->GetRawMaterials();


    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $simpleSettingData = $simplesettingDao->load(1);
    $smarty->assign("simpleSettingData", $simpleSettingData);
    if ($simpleSettingData->orderclient == 0) {
        $clientData = $clientDAO->queryAll();
        $smarty->assign("clientData", $clientData);
    }

    $smarty->assign("customTaskOrder", 1);
    //$smarty->assign("rows", $rows);
    $smarty->display("taskOrderView/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");



    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    $id = $_GET["id"];

    $simpleSettingData = $simplesettingDao->load(1);
    $smarty->assign("simpleSettingData", $simpleSettingData);
    if ($simpleSettingData->orderclient == 0) {
        $clientData = $clientDAO->queryAll();
        $smarty->assign("clientData", $clientData);
    }

    $taskOrderData = $taskorderDao->load($id);
    $smarty->assign('taskOrderData', $taskOrderData);

    $product = $productDAO->load($taskOrderData->productId);
    $productCat = $productCatDAO->load($product->productCatId);

    $smarty->assign("oldfinalproduct", $taskOrderData->productId);
    $smarty->assign("oldfinalproductname", $product->productName . '/ ' . $productCat->productCatName);

    $taskOrderProductsData = $taskorderproductsExt->queryAllProductsByTaskOrderId($id);
    $smarty->assign('taskOrderProductsData', $taskOrderProductsData);

    $smarty->assign("customTaskOrder", 1);
    $smarty->assign("customTaskOrderEdit", 1);

    $smarty->display("taskOrderView/edit.html");
    //print_r($Productionrateproduct->conditions);
} elseif ($do == "update") {
    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {

    $startDate = filter_input(INPUT_POST, 'startDate');
    $endDate = filter_input(INPUT_POST, 'endDate');

    $smarty->assign("startDate", $startDate);
    $smarty->assign("endDate", $endDate);

    $simpleSettingData = $simplesettingDao->load(1);
    $smarty->assign("simpleSettingData", $simpleSettingData);

    $query = '';
    if ($startDate)
        $query.=' and thedate >= "' . $startDate . '"';

    if ($endDate)
        $query.=' and thedate <= "' . $endDate . '"';


    $rows = $taskorderExt->queryAllExt($query);

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

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $smarty->display("taskOrderView/show.html");
} elseif ($do == "stop") {
    try {

        stop_active();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "delete") {

    try {

        delete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} 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");

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

//add
function add() {
    global $taskorder;
    global $taskorderDao;
    global $taskorderproducts;
    global $taskorderproductsDao;

    $startdate = filter_input(INPUT_POST, 'from');
    $enddate = filter_input(INPUT_POST, 'to');
    $clientid = (int) filter_input(INPUT_POST, 'clientid');
    $today = date("Y-m-d");

    $handle = new upload($_FILES['file']);
    $file = uploadImageswhioutresize($handle, '../upload/taskOrder');

    if (!$startdate || !$enddate) {
        $taskorder->startdate = $today;
        $taskorder->enddate = $today;
    } else {
        $taskorder->startdate = $startdate;
        $taskorder->enddate = $enddate;
    }

    $taskorder->title = filter_input(INPUT_POST, 'title');
    $taskorder->productId = filter_input(INPUT_POST, 'finalProductId');
    $taskorder->num = filter_input(INPUT_POST, 'proNumber');
    $taskorder->clientid = $clientid;
    $taskorder->file = $file;
    $taskorder->conditions = 0;
    $taskorder->thedate = date("Y-m-d");
    $taskorder->userId = $_SESSION['userid'];

    $id = $taskorderDao->insert($taskorder);

    $rowitr = filter_input(INPUT_POST, 'rowitr');

    for ($i = 1; $i <= $rowitr; $i++) {
        $taskorderproducts->taskorderid = $id;
        $taskorderproducts->productid = filter_input(INPUT_POST, 'product' . $i);
        $taskorderproducts->unitid = filter_input(INPUT_POST, 'unit' . $i);
        $taskorderproducts->quantity = filter_input(INPUT_POST, 'quantity' . $i);
        $taskorderproducts->quantity = filter_input(INPUT_POST, 'quantity' . $i);
        $taskorderproductsDao->insert($taskorderproducts);
    }
}

//update
function update() {
    global $taskorder;
    global $taskorderDao;
    global $taskorderproducts;
    global $taskorderproductsDao;

    $id = filter_input(INPUT_POST, 'taskOrderId');

    $taskorder = $taskorderDao->load($id);

    $startdate = filter_input(INPUT_POST, 'from');
    $enddate = filter_input(INPUT_POST, 'to');
    $clientid = (int) filter_input(INPUT_POST, 'clientid');

    $handle = new upload($_FILES['file']);
    $file = updateImagesWithoutresiz($handle, "oldfile", '../upload/taskOrder');

    $today = date("Y-m-d");
    if (!$startdate || !$enddate) {
        $taskorder->startdate = $today;
        $taskorder->enddate = $today;
    } else {
        $taskorder->startdate = $startdate;
        $taskorder->enddate = $enddate;
    }

    $taskorder->title = filter_input(INPUT_POST, 'title');
    $taskorder->productId = filter_input(INPUT_POST, 'finalProductId');
    $taskorder->num = filter_input(INPUT_POST, 'proNumber');
    $taskorder->clientid = $clientid;
    $taskorder->file = $file;
    $taskorder->thedate = date("Y-m-d");
    $taskorder->userId = $_SESSION['userid'];
    $taskorder->id = $id;

    $taskorderDao->update($taskorder);

    $taskorderproductsDao->deleteByTaskorderid($id);

    $rowitr = filter_input(INPUT_POST, 'rowitr');

    for ($i = 1; $i <= $rowitr; $i++) {
        $taskorderproducts->taskorderid = $id;
        $taskorderproducts->productid = filter_input(INPUT_POST, 'product' . $i);
        $taskorderproducts->unitid = filter_input(INPUT_POST, 'unit' . $i);
        $taskorderproducts->quantity = filter_input(INPUT_POST, 'quantity' . $i);

        $taskorderproductsDao->insert($taskorderproducts);
    }
}

//stop
function stop_active() {
    global $taskorder;
    global $taskorderDao;

    $id = $_GET["id"];
    $taskorder = $taskorderDao->load($id);

    if ($taskorder->conditions == 0) {
        $taskorder->conditions = 1;
    } elseif ($taskorder->conditions == 1) {
        $taskorder->conditions = 0;
    }
    $taskorderDao->update($taskorder);
}

//delete
function delete() {
    global $taskorderDao;
    global $taskorderproductsDao;

    $id = $_GET["id"];
    print_r($id);
    $taskorderproductsDao->deleteByTaskorderid($id);
    $taskorderDao->delete($id);
}

?>