<?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
// GOES HERE ....................
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');

/////////////////////////setting operation//////////////
require_once('../models/dao/SettingoperationDAO.class.php');
require_once('../models/dto/Settingoperation.class.php');
require_once('../models/mysql/SettingoperationMySqlDAO.class.php');
require_once('../models/mysql/ext/SettingoperationMySqlExtDAO.class.php');

///////////////////////////stage//////////////////////
require_once('../models/dao/StageDAO.class.php');
require_once('../models/dto/Stage.class.php');
require_once('../models/mysql/StageMySqlDAO.class.php');
require_once('../models/mysql/ext/StageMySqlExtDAO.class.php');

//////////////////////stagestep//////////////////
require_once('../models/dao/StagestepDAO.class.php');
require_once('../models/dto/Stagestep.class.php');
require_once('../models/mysql/StagestepMySqlDAO.class.php');
require_once('../models/mysql/ext/StagestepMySqlExtDAO.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");

$stageId = $_GET['stageId'];

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

  Controller Name :- stage ctrl مراحل الانتاج

  OPERTATION in Controller

  1-display add form
  2- add in workstation,workstationworker,workstationasset tble
  3-update savefrom value (-)
  4-insert into savedaily tbl
  5-update saveto value (+)
  6-insert into savedaily tbl
  7-display sucess or error
  8-display show and delete
  9-tempdelete from Expenses tbl
  10-returndelete save value (+)

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


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

//here goes the instances and general variables
///////////////product/////////////
$ProductDAO = new ProductMySqlDAO();
$Product = new Product();

/////////////////////setting opration///////////////
$SettingoperationDAO = new SettingoperationMySqlDAO();
$Settingoperation = new Settingoperation();
$setting = new Settingoperation();
$SettingoperationEX = new SettingoperationMySqlExtDAO();
$newstage = new Settingoperation();

///////////////////stage/////////////
$StageDAO = new StageMySqlDAO();
$Stage = new Stage();
$StageEX = new StageMySqlExtDAO();

/////////////////stagestep///////////////////////////
$StagestepDAO = new StagestepMySqlDAO();
$Stagestep = new Stagestep();
$StagestepEX = new StagestepMySqlExtDAO();

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



//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");
    $allproduct = loadproduct();
    $alloperation = loadopration();
    $smarty->assign("allproduct", $allproduct);
    $smarty->assign("alloperation", $alloperation);
    $smarty->display("stageview/add.html");
} elseif ($do == "show") {
    include_once("../public/authentication.php");
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    $allstage = $StageDAO->queryAll();
    foreach ($allstage as $newstage) {
        $totaloperation = $StagestepDAO->queryByStageId($newstage->stageId);
        $newstage->countoperation = count($totaloperation);
    }
    $smarty->assign("allstage", $allstage);
    $smarty->display("stageview/show.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");

    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    include_once("../public/authentication.php");
    $operationid = $_POST['operationid'];

    $prdid = $ProductDAO->load($operationid);

    $allproduct = loadproduct();
    $alloperation = loadopration();

    $loadstage = $StageDAO->load($stageId);
    $stagerow = $StagestepDAO->queryByStageId($stageId);

    foreach ($stagerow as $new) {
        $steprow = $SettingoperationEX->loadByopertaionid($new->operationId);
        $new->newstage = $steprow->realTime;
        $new->counter = count($stagerow);
    }
    $smarty->assign("stgcounter", count($stagerow));
    $smarty->assign("stagerow", $stagerow);
    $smarty->assign("loadstage", $loadstage);
    $smarty->assign("allproduct", $allproduct);
    $smarty->assign("alloperation", $alloperation);
    $smarty->display("stageview/edit.html");
} elseif ($do == "update") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "deletetemp") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        deletetemp($stageId);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "returndelete") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        returndelete($stageId);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "delete") {
    include_once("../public/authentication.php");

    $operationdeleteValid = delete($stageId);

    if ($operationdeleteValid[1] == 0) {
        header("location:?do=sucess");
    } elseif ($operationdeleteValid[1] == 2) {
        $url = "stageController.php?do==show";
        $smarty->assign('urldirect', $url);
        $note = "لقد حدث خطأ اثناء اجراء العملية";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes.html");
    } else {
        $url = "stageController.php?do==show";
        $smarty->assign('urldirect', $url);
        $note = "لا يمكن حذف  هذه المرحله الا بعد حذف كل البيانات المرتبطه به";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes.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("customstage", 1);
//here the global templates
$smarty->display("footer.html");


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

function loadproduct() {
    global $ProductDAO;
    $allproduct = $ProductDAO->queryAll();
    return $allproduct;
}

function loadopration() {
    global $SettingoperationDAO;
    $alloperation = $SettingoperationDAO->queryAll();
    return $alloperation;
}

function add() {
    global $StagestepDAO;
    global $Stagestep;

    global $StageDAO;
    global $Stage;

    $stagehour = $_POST['totaloperationhour1'];
    $stagename = $_POST['setoperation'];
    $productid = $_POST['productname'];
    //$stepno=$_POST['stepno'];

    $Stage->conditions = 0;
    $Stage->date = date("Y-m-d");
    $Stage->userid = $_SESSION['userid'];
    $Stage->productId = $productid;
    $Stage->stagename = $stagename;
    $Stage->totalhour = $stagehour;

    $stageno = $StageDAO->insert($Stage);

    $operationitr = $_POST["operationitr"];

    for ($h = 1; $h <= $operationitr; $h++) {
        $operationname = $_POST['oprationname' . $h . ''];
        $no = $_POST['no' . $h . ''];

        if (isset($operationname) && $operationname != -1) {

            $Stagestep->operationId = $operationname;
            $Stagestep->stageId = $stageno;
            $Stagestep->stepno = $no;

            $StagestepDAO->insert($Stagestep);
        }
    }
}

///////////////////////////////update///////////////////////////////////
function update() {
    global $StagestepDAO;
    global $Stagestep;
    global $StagestepEX;

    global $StageDAO;
    global $Stage;
    global $StageEX;

    $stageid = $_POST['stageid'];
    $stagehour = $_POST['totaloperationhour1'];
    $stagename = $_POST['setoperation'];
    $productid = $_POST['productname'];

    $Stage->conditions = 0;
    $Stage->date = date("Y-m-d");
    $Stage->userid = $_SESSION['userid'];
    $Stage->productId = $productid;
    $Stage->stagename = $stagename;
    $Stage->totalhour = $stagehour;
    $Stage->stageId = $stageid;

    $stagenumber = $StageDAO->update($Stage);

    ///////////////////////////////////////////////

    $StagestepDAO->deleteByStageId($stageid);

    $operationitr = $_POST["operationitr"];

    for ($h = 1; $h <= $operationitr; $h++) {
        $operationname = $_POST['oprationname' . $h . ''];
        $no = $_POST['no' . $h . ''];
        $stagestepid = $_POST['stagestep' . $h . ''];

        if (!empty($operationname) && $operationname != '-1' && !empty($no)) {

            $Stagestep->operationId = $operationname;
            $Stagestep->stageId = $stageid;
            $Stagestep->stepno = $no;
            $Stagestep->stagestepId = $stagestepid;

            if (isset($stagestepid) && $stagestepid > 0) {
                $Stagestep->stagestepId = $stagestepid;
                $StagestepEX->insertwithid($Stagestep);
            } else {
                $StagestepDAO->insert($Stagestep);
            }
        }
    }
}

///////////////////////////////////////////deletetemp//////////////////////////
function deletetemp($stageId) {
    global $StagestepDAO;
    global $Stagestep;

    global $StageDAO;
    global $Stage;
    global $StageEX;


    try {
        if (!empty($stageId)) {
            $Stage->conditions = 1;
            $Stage->date = date("Y-m-d");
            $Stage->userid = $_SESSION["userid"];
            $Stage->stageId = $stageId;

            $StageEX->updateConditions($Stage);


            $note = "success";
        } else {
            $note = "حدث خطأ أثناء إخفاء المرحله";
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء إخفاء المرحله";
    }
}

/////////////////////////////////////returndelete////////////////////////
function returndelete($stageId) {
    global $StagestepDAO;
    global $Stagestep;

    global $StageDAO;
    global $Stage;
    global $StageEX;


    try {
        if (!empty($stageId)) {
            $Stage->conditions = 0;
            $Stage->date = date("Y-m-d");
            $Stage->userid = $_SESSION["userid"];
            $Stage->stageId = $stageId;

            $StageEX->updateConditions($Stage);


            $note = "success";
        } else {
            $note = "حدث خطأ أثناء إخفاء المرحله";
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء إخفاء المرحله";
    }
}

///////////////////////////////////////////delete///////////////////////////////////////
function delete($stageId) {

    global $StagestepDAO;
    global $Stagestep;

    global $StageDAO;
    global $Stage;
    global $StageEX;

    try {
        if (!empty($stageId)) {
            $alloperation = $StagestepDAO->queryByStageId($stageId);
            if (count($alloperation) <= 0) {
                $operationdeleteValid = 0;
                $StageDAO->delete($stageId);

                $note = "success";
            } else {
                $operationdeleteValid = 1;
                $note = "لا يمكن حذف  هذه المرحله";
            }
        } else {
            $operationdeleteValid = 2;
            $note = "حدث خطأ أثناء حذف هذه المرحله ";
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء حذف المرحله";
    }


    return array($note, $operationdeleteValid);
}
