<?php

//the global file operation
include("../public/impOpreation.php");

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.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");
//here the global templates
$smarty->display("header.html");
//$smarty->display("maintennanceHeader.html");

//here goes the instances and general variables

//MStage
	require_once('../models/dao/MStageDAO.class.php');
	require_once('../models/dto/MStage.class.php');
	require_once('../models/mysql/MStageMySqlDAO.class.php');
	require_once('../models/mysql/ext/MStageMySqlExtDAO.class.php');
	
//MStageproperity
	require_once('../models/dao/MStageproperityDAO.class.php');
	require_once('../models/dto/MStageproperity.class.php');
	require_once('../models/mysql/MStageproperityMySqlDAO.class.php');
	require_once('../models/mysql/ext/MStageproperityMySqlExtDAO.class.php');
//Capital
//check and use the condetion that suite this action

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

//MStage
$MStage = new MStage();
$MStageDAO = new MStageMySqlDAO();
$MStageEX =new MStageMySqlExtDAO();

//MStageproperity
$MStageproperity = new MStageproperity();
$MStageproperityDAO= new MStageproperityMySqlDAO();
$MStageproperityEX = new MStageproperityMySqlExtDAO();

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

if (empty($do)) {
	
	include_once("../public/authentication.php");
	
	$allstage = getstage();
	
	$smarty->assign("title1",'الصيانة');
	$smarty->assign("title2",'خواص المراحل');
	$smarty->assign("title3",'اضافة  خواص المراحل');
	$smarty->assign("link",'stagepropertyController.php?do=show');
	$smarty->display("maintennanceHeader.html");
	
	$smarty->assign("allstage",$allstage);
	$smarty->display("stagepropertyview/add.html");
	//date
	
} 
elseif ($do =="add") {
  // include_once("../public/authentication.php");
   include_once("../public/authentication.php");
		//add();
		try
		{
			// here the function that do the action 
			add();
			header("location:?do=sucess");
		}
		catch(Exception $e)
		{
			header("location:?do=error");
		}
		
		
		
		
}

elseif ($do =="show") {
 include_once("../public/authentication.php");
    
	/*$allstage = getstage();
	$smarty->assign("allstage",$allstage);
	
	$stageid=$_REQUEST['stageid'];
		
		if((!empty($stageid)) )	
		{
			$satgename = $MStageDAO->load($stageid);
			$message = " اسم المرحله ".$satgename->title;
			$smarty->assign("message",$message);
			
		   show($stageid);
		}
		else
		{
			$today= date("Y-m-d");
			// show($clientid,$serialid,$serialproductid,$documentid,$today, $today);
			
		}*/
	 //show();
	 
	 show(2);

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

	 $smarty->assign("title1",'الصيانة');
	 $smarty->assign("title2",'خواص المراحل');
	 $smarty->assign("title3",'عرض خواص المراحل');
	 $smarty->assign("link",'stagepropertyController.php?do=show');
	 $smarty->display("maintennanceHeader.html");
	
	 $smarty->display("stagepropertyview/show.html");
}

//edit
elseif ($do == "edit") {
	
	include_once("../public/authentication.php");
	
	$allstage = getstage();
	$smarty->assign("allstage",$allstage);

	$alldata = edit();
	$smarty->assign("alldata",$alldata);
	//print_r($alldata);
	
	 $smarty->assign("title1",'الصيانة');
	 $smarty->assign("title2",'خواص المراحل');
	 $smarty->assign("title3",'تعديل خواص المراحل');
	 $smarty->assign("link",'stagepropertyController.php?do=show');
	 $smarty->display("maintennanceHeader.html");
	 
	 
	 $smarty->display("stagepropertyview/edit.html");

}

elseif($do=="editprint")
{
	
	include_once("../public/authentication.php");
	$allstage = getstage();
	$smarty->assign("allstage",$allstage);

	$alldata = edit();
	$smarty->assign("alldata",$alldata);
	//print_r($alldata);
	 $smarty->display("stagepropertyview/editprint.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=="delete")
{
	
	
	include_once("../public/authentication.php");
		//add();
		try
		{
			// here the function that do the action 
			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");
	}
$smarty->assign("customreceipt",1);
$smarty->assign("GeneralSearch",1);
$smarty->display("footer.html");

////****************************  Functions  ********************///

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

function getstage()
{
	global $MStageDAO;
	
	$allstage = $MStageDAO->queryAll();
	
	return $allstage;
}

	
//here will create random dealerid


function add()
{
	global $MStageproperityDAO;
	global $MStageproperity;
	
	$propertyname = $_POST['propertyname'];
	$stagename = 2;//$_POST['stagename'];
	$stagevalue = $_POST['stagevalue'];
	
		$MStageproperity->stageId = $stagename;
		$MStageproperity->initialValue = $stagevalue;
		$MStageproperity->name=$propertyname;
		$MStageproperity->stageProperityDate = date("y-m-d");
		$MStageproperity->userId = $_SESSION['userid'];
		$MStageproperity->branchId =  $_SESSION['branchId']; 
		$MStageproperity->del = 0;
		
		$MStageproperityDAO->insert($MStageproperity);
		
	

}


function show($stageid)
{
	global $MStageproperity;
	global $MStageproperityEX;
	global $MStageDAO;
	global $smarty;
	$queryString = ' WHERE';
			
			if(isset($stageid) && $stageid != '-1' && $stageid != "-2")
		{
					$stagename=$MStageDAO->load($stageid);
					$message=" اسم المرحله ".$stagename->title;
					//$smarty->assign('message',$message);
					
					$queryString .= '  m_stageproperity.stageId = '.$stageid.' AND';
		}
		if(isset($stageid) && $stageid != "-1" && $stageid == "-2")
		{
						$message=" كل المراحل";
						$smarty->assign('message',$message);
						$queryString .= '  m_stageproperity.stageId > 0  AND';
		}
		
		
	
		
			$arr = explode(' ', $queryString);
				if(isset($arr) && count($arr) > 0)
				{
				  $lastWord = end($arr);
					if($lastWord == 'AND')
					{//remove it
						array_pop($arr);
						//form the string again
						$queryString = implode(' ', $arr);
					}
					else if($lastWord == 'WHERE')
					{//remove it
						array_pop($arr);
						$queryString = ' ';
					}
					//print("<br>queryString: ".$queryString."<br>");
				}
		
		$showndata = $MStageproperityEX->queryAllproperty($queryString);
		
		
			$cost = 0;
			foreach ($showndata as $stagedata)
			{
				
						$cost +=$stagedata->initialValue;
						//$cost= round(($cost1 *2))/2;
					
			}
		$smarty->assign("showndata",$showndata);
		$smarty->assign("cost",$cost);
}

function edit()
{
	global $MStageproperityEX;
	
	$id = $_GET['id'];
	$alldata = $MStageproperityEX->loadstage($id);
	
	return $alldata;
}
function update()
{
	global $MStageproperityDAO;
	global $MStageproperity;
	
	$propertyname = $_POST['propertyname'];
	$stagename = 2;//$_POST['stagename'];
	$stagevalue = $_POST['stagevalue'];
	$propertyid = $_POST['propertyid'];
	
		$MStageproperity->stageProperityId = $propertyid;
		$MStageproperity->stageId = $stagename;
		$MStageproperity->initialValue = $stagevalue;
		$MStageproperity->name=$propertyname;
		$MStageproperity->stageProperityDate = date("y-m-d");
		$MStageproperity->userId = $_SESSION['userid'];
		$MStageproperity->branchId =  $_SESSION['branchId']; 
		$MStageproperity->del = 0;
		
		$MStageproperityDAO->update($MStageproperity);

}

function delete()
{
	global $MStageproperityEX;
	global $MStageproperity;
	
	$id = $_GET['id'];
		$MStageproperity->stageProperityId = $id;
		$MStageproperity->stageProperityDate = date("y-m-d");
		$MStageproperity->userId = $_SESSION['userid'];
		$MStageproperity->branchId =  $_SESSION['branchId']; 
		$MStageproperity->del = 1;
		
		
		$MStageproperityEX->updatestage($MStageproperity);

}

?>