<?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");
	
	require_once('../models/dao/CostcenterDAO.class.php');
	require_once('../models/dto/Costcenter.class.php');
	require_once('../models/mysql/CostcenterMySqlDAO.class.php');
	require_once('../models/mysql/ext/CostcenterMySqlExtDAO.class.php');
	require_once('../models/dao/CostcenterdetailDAO.class.php');
	require_once('../models/dto/Costcenterdetail.class.php');
	require_once('../models/mysql/CostcenterdetailMySqlDAO.class.php');
	require_once('../models/mysql/ext/CostcenterdetailMySqlExtDAO.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 :- clientTypeCTRL 
	 
	 OPERTATION in Controller
	 
	 1-
	 2- 
	 3-
	 4-
	
	========================*/
	$userID = $_SESSION['userid'];
	
	//here the global templates
	$smarty->display("header.html");
	
	//here goes the instances and general variables

 	$Costcenterdetail= new Costcenterdetail();
	$CostcenterdetailDAO= new CostcenterdetailMySqlDAO();
	$CostcenterdetailEX= new CostcenterdetailMySqlExtDAO();
	
	//Costcenter
	$Costcenter= new Costcenter();
	$CostcenterDAO= new CostcenterMySqlDAO();
	$CostcenterEX= new CostcenterMySqlExtDAO();
	


	
	//check and use the condetion that suite this action
	if(empty($do))
	{
		
		$smarty->display("costcenterview/add.html");
	}
	
	
	
	
	elseif($do=="add")
	{
		try
		{
			add();
			header("location:?do=sucess");
		}
		catch(Exception $e)
		{
			//echo $e;
			header("location:?do=error");
		}
	}
	
	
	
	
	elseif($do=="ruturndelete")
	{
		try
		{
			    $id=$_GET["id"];
				$note=	ruturndelete($id);
				if($note !=="sucess")
				{
						$url = "costcenterController.php?do=show";
						$smarty->assign('urldirect', $url);
						$note =$note;
						$smarty->assign('msgnote', $note);
						$smarty->display("notes.html");
						
				}
				else
				{
						header("location:?do=sucess");
				}
				
		}
				
		catch(Exception $e)
		{
			header("location:?do=error");
					
		}
	}
	
	
	
	
	elseif($_GET['do']=="show")
	{
		
		show();
		$smarty->assign("customCheck",1);
		$smarty->display("costcenterview/show.html");
	}
	
	
	
	elseif($do=="edit")
	{
		$data = edit();
		$smarty->assign("data",$data);
	
	
		$smarty->assign("expensestypeid",$expensestypeid);

		$smarty->display("costcenterview/Edit.html");
	}
	
	
	
	elseif($_GET['do']=="update")
	{
		try
		{
			update();
			header("location:?do=sucess");
		}
		catch(Exception $e)
		{
			header("location:?do=error");
			//echo $e;
		}
	}
	
	
	
	elseif($_GET['do'] == "executeOperation")
	{
		try
		{
			execute();
			show();
			$smarty->assign("customCheck",1);
		   $smarty->display("costcenterview/show.html");
		}
		catch(Exception $e)
		{
			$smarty->display("error.html");
		}
	}
	
	
	elseif($_GET['do']=="delete")
	{
		try
		{
			$id=$_GET["id"];
			$note=delete($id);
			
			if($note !=="sucess")
			{
				$url = "costcenterController.php?do=show";
				$smarty->assign('urldirect', $url);
				$note =$note;
				
				$smarty->assign('msgnote', $note);
				$smarty->display("notes.html");
				
		    }
			else
			{
				
				header("location:?do=sucess");
			}
		}
		catch(Exception $e)
		{
			header("location:?do=error");
			
		}
	}
	

	
	
	elseif($_GET['do']=="sucess")
	{
		$smarty->display("succes.html");
	}
	
	
	elseif($_GET['do']=="error")
	{
		$smarty->display("error.html");
	}
	
	
	
	$smarty->assign("customValidation",1);

	$smarty->display("footer.html");
	
	//******************    Functions    ************************//

	
	
	function add()
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
	
		$name=$_POST['name'];
		$descripe=$_POST['descripe'];
		
		$Costcenter->name=$name;
		$Costcenter->condation=0;
		$Costcenter->comment=$descripe;
		$Costcenter->userid=$_SESSION['userid'];
		
		$CostcenterDAO->insert($Costcenter);
	}
	
	
	//show tabel data
	function show()
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
		global $smarty;
		
		$qname=$CostcenterDAO->queryAll();
		$smarty->assign('name', $qname);
	}



// edit row
	function edit()
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
	
		$id=$_GET['id'];
		$data=$CostcenterDAO->load($id);
		return $data;	
	}


//update row
	function update()
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
		
		$userID = $_SESSION['userid'];
		$id=$_POST['id'];
		$name=$_POST['name'];
		$descripe=$_POST['descripe'];
	
		$Costcenter->name=$name;
		
		$Costcenter->del=0;
		$Costcenter->comment=$descripe;
		$Costcenter->id=$id;
		
	
		$CostcenterDAO->update($Costcenter);	
	}


// temp delte row
	function delete($id)
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
		global	$CostcenterdetailDAO;
		global	$Costcenterdetail;
		
		global $smarty;
		
		$userID = $_SESSION['userid'];
		$id=$id;//$_GET['id'];
		
		//1- check if there are any asset cat  associated with asset
		$alldata=$CostcenterdetailDAO->queryByCostcenterid($id);
	
		if(count($alldata) > 0 )
		{
			
			$note ="لا يمكن حذف هذا النوع لانه مرتبط ببيانات اخرى";
			
			
		}
		else
		{
			$Costcenter->id=$id;
			$Costcenter->condation=1;
			$CostcenterEX->updatedel($Costcenter);
			
			$note ="sucess";
		}	
		
		return $note;	
	}
	
	
	
	// cancel deleting row
	function ruturndelete($id)
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
		global	$CostcenterdetailDAO;
		global	$Costcenterdetail;
		global	$CostcenterdetailEX;
		global $smarty;
		
		$id=$id;
		
		
			$Costcenter->id=$id;
			$Costcenter->condation=0;
			if($CostcenterEX->updatedel($Costcenter))
			{
				$note ="sucess";
				
			}
		
			else
			{
				
				$note ="لم تتم العمليه بنجاح لان هذا العنصر لم يتم حذف من قبل ";
					
			}
		return $note;
			
	}
	
	
	
	
	function execute()
	{
		global	$CostcenterEX;
		global	$CostcenterDAO;
		global	$Costcenter;
		
		global	$CostcenterdetailDAO;
		global	$Costcenterdetail;
		global	$CostcenterdetailEX;
		global $smarty;
		global $outputString;
		
		$operationType = $_POST['operation'];
		$smarty->assign("operationType",$operationType);
		
		$choosedItemArr = $_POST['choosedItem'];
		//print_r($choosedItemArr);
		if(count($choosedItemArr) > 0)
		{
			$i=1;
			foreach($choosedItemArr as $assetscatid	)
			{
				$assetdata = $CostcenterDAO->load($assetscatid);
				//print_r($assetdata);
				$cattitel = $assetdata->name;
				if($operationType == '1') 
				{//tempdelete
					try
					{
						$note = delete($assetscatid);
						if($note != "sucess")
						{
							//get product category name
							
							if(count($assetscatid) > 0)
							{
								
								$outputString .= $cattitel. ": ".$note."<br/>";
							}
						}
					}
					catch(Exception $e)
					{
					}
				}
				
				elseif($operationType == "2")
				{//returndelete
					try
					{
						$note=ruturndelete($assetscatid);
						if($note != "sucess")
						{
							//get product category name
							
							if(count($assetscatid) > 0)
							{
								
								$outputString .= $cattitel. ": ".$note."<br/>";
							}
						}
					}
					catch(Exception $e)
					{
						$note = "حدث خطأ أثناء إتمام العملية";	
					}
				}
				$i++;
			}
			$smarty->assign("outputString",$outputString);
			
			//print_r($outputString);
		}
	}


?>