<?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");
	//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');
	//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');
	//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');
	
	//get the do the action 
	
	
	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');
	
	
	//Buybilldetail
	require_once('../models/dao/BuybilldetailDAO.class.php');
	require_once('../models/dto/Buybilldetail.class.php');
	require_once('../models/mysql/BuybilldetailMySqlDAO.class.php');
	require_once('../models/mysql/ext/BuybilldetailMySqlExtDAO.class.php');
	
	
	require_once('../models/dao/ReturnbuybilldetailDAO.class.php');
	require_once('../models/dto/Returnbuybilldetail.class.php');
	require_once('../models/mysql/ReturnbuybilldetailMySqlDAO.class.php');
	require_once('../models/mysql/ext/ReturnbuybilldetailMySqlExtDAO.class.php');

	//Productunit
	require_once('../models/dao/ProductunitDAO.class.php');
	require_once('../models/dto/Productunit.class.php');
	require_once('../models/mysql/ProductunitMySqlDAO.class.php');
	require_once('../models/mysql/ext/ProductunitMySqlExtDAO.class.php');
	
	
	
	$do = $_GET['do'];
	
	/* ======================
	
	 Controller Name :- storedetailCTRL تقرير مخزون اول مدة 
	 
	 OPERTATION in Controller
	 
	 1-load save data
	 2-display show form
	
	========================*/
	
	//here the global templates
	$smarty->display("header.html");

	//here goes the instances and general variables
	//Storedetail
	$myStoredetailRecord=new StoredetailMySqlDAO();
	$myStoredetailEx=new StoredetailMySqlExtDAO();
	//Store
	$myStoreRecord=new StoreMySqlDAO();
	//Product
	$myProductRecord=new ProductMySqlDAO();
	$productExt = new ProductMySqlExtDAO();
	//Productcat
	$productCatDAO = new ProductcatMySqlDAO();
	$productCatExt = new ProductcatMySqlExtDAO();
	
	$ProgramsettingDAO= new ProgramsettingsMySqlDAO();
	
	//Buybilldetail
	$buyBillDetail = new Buybilldetail();
	$buyBillDetailDAO = new BuybilldetailMySqlDAO();
	$buyBillDetailExt = new BuybilldetailMySqlExtDAO();

	//Returnbuybilldetail
	$returnBuyBillDetail = new Returnbuybilldetail();
	$returnBuyBillDetailDAO = new ReturnbuybilldetailMySqlDAO();
	$returnBuyBillDetailExt = new ReturnbuybilldetailMySqlExtDAO();
	//Productunit
	$myProductunitEx=new ProductunitMySqlExtDAO();
	
	
	//check and use the condition that suite this action
	if($do == "show" || empty($do))
	{
		//here the permission check
		include_once("../public/authentication.php");

		$storeData=loadStore();
		$smarty->assign("storeData",$storeData);

		$productCatData = loadProductCategories();
		$smarty->assign("productCatData",$productCatData);

		$productId=$_REQUEST['productId'];
		$storeId=$_REQUEST['storeId'];
		$productCatId = $_REQUEST['productCatId'];
		
		//echo "$myusergroupdata->hidecat".$myusergroupdata->hidecat;
		$smarty->assign('hidecat',$myusergroupdata->hidecat);
		
		
		if(isset($productId) && $productId != "-1" && $productId != "" || isset($storeId) && $storeId != "-1"  || isset($productCatId) && $productCatId != "-1" )
		{
				$storedetailData=show();
		}
		else
		{
		}
				
		$smarty->assign('storedetailData',$storedetailData[0]);
		
		
		$smarty->assign('sumValue',$storedetailData[1]);
	
	
	$Programsettingdata=$ProgramsettingDAO->load(1);
		$smarty->assign("Programsettingdata",$Programsettingdata);
		//here the smarty templates
		$smarty->display("storedetailview/show.html");
		$smarty->assign("settlementstoreshow",1);
	}
	//here the global templates
	$smarty->display("footer.html");
	
	/*===============================
		function in this CONTROLLER
	================================*/
	
	
	//select all product data
	function loadProducts()
	{
		//to use the variable out side the funcion
		global $myProductRecord;
		
		//load all product data
		$productData=$myProductRecord->queryByConditions(0);
		
		return $productData;
	}

	
	//select all store data
	function loadStore()
	{
		//to use the variable out side the funcion
		global $myStoreRecord;
		
		//load all store data
		$storeData=$myStoreRecord->queryByConditions(0);
		
		return $storeData;
	}
	
	
	function loadProductCategories()
	{
		global $productExt;
		global $productCatDAO;
		global $smarty;
		$categories;
		
		//NOTICE: select categories which have products in it only, not all categories.
		$productsData = $productExt->queryAllProducts();
		
		if(count($productsData) > 0)
		{
			$i=0;
			foreach($productsData as $pro)
			{
				$parentId = $pro->productCatId;
				$pathArr = fetch_recursive($parentId, $categories);
				$smarty->assign("names".$i,$pathArr);
				$smarty->assign("parentId".$i,$parentId);
				$i++;
			}
			$itr = $i - 1;
			$smarty->assign("itr",$itr);
		}
		
		return $productsData;
	}
	
	
	function fetch_recursive($parentid, $categories)
	{
		global $productCatExt;
		$catData = $productCatExt->getCategoryAndParentByCatId($parentid);

		if(count($catData) > 0)
		{
			$categories .= $catData->productCatName.'/'; 	
			$newParentId = $catData->productCatParent;

			if($newParentId != 0)
			{
				$newParentName = $catData->parentName;
				$categories .= $newParentName.'/'; 	
				fetch_recursive($newParentId, $categories);
			}
		}
		$categories = substr($categories, 0, strlen($categories) - 1);
		return $categories;	
	}

	
	// show the form by product name
	function show()
	{
		global $smarty;
		//to use the variable out side the funcion
		global $myStoredetailEx;
		global $ProgramsettingDAO;
		global $buyBillDetail;
		global $buyBillDetailDAO;
		global $buyBillDetailExt;
		global $returnBuyBillDetail;
		global $returnBuyBillDetailDAO ;
		global $returnBuyBillDetailExt;
		global $myStoreRecord;
		global $myProductRecord;
		global $productCatDAO;
		
	include_once("../public/authentication.php");
		$productId=$_REQUEST['productId'];
		$storeId=$_REQUEST['storeId'];
		$productCatId = $_REQUEST['productCatId'];
		$order=$_REQUEST["order"];
       
	   
	   $queryString = ' AND';
	  $message="تقرير بضاعة أول مدة : ";
		
		if(isset($productId) && $productId != '-1' && $productId != '')
		{
			
			$myprodactdata=$myProductRecord->load($productId);
			
			$message =$message." للمنتج  :".$myprodactdata->productName."  ";
			
			
			$queryString .= ' storedetail.productId = '.$productId.' AND';
		}
		
		if(isset($storeId) && $storeId != '-1')
		{
			$queryString .= '  storedetail.storeid = '.$storeId.' AND';
			$mystordata=$myStoreRecord->load($storeId);
			$message =$message."    والمخزن : ".$mystordata->storeName."";
		}
		
		if(isset($productCatId) && $productCatId != '-1')
		{
			$queryString .= '  product.productCatId = '.$productCatId.' AND';
			$myProductCatData=$productCatDAO->load($productCatId);
			
			
			$message=$message.$myProductCatData->productCatName ." والمخزن ".$mystordata->storeName."";
		}
		
		
		$arr = explode(' ', $queryString);
				//print_r($arr);
				if(isset($arr) && count($arr) > 0)
				{
					$lastWord = end($arr);
					if($lastWord == 'AND')
					{//remove it
						array_pop($arr);
						//form the string again
						$queryString = implode(' ', $arr);
						
					}
				
					//print("<br>queryString: ".$queryString."<br>");
				}
		
		//select all data from storedetail by productId
		$storedetailData=$myStoredetailEx->queryWithqueryString($queryString,$order);
		
		$smarty->assign('message',$message);
		$sumValue=0;
		foreach($storedetailData as $storedetail)
		{	
		
				$Programsettingdata=$ProgramsettingDAO->load(1);
			if($Programsettingdata->lastprice=="0")
			{
				
			
			$buylastprice = $buyBillDetailDAO->queryByBuybilldetailproductid($storedetail->productid);
				//print_r($buylastprice);
				if(count($buylastprice)!="0"){
					
					$productunitid=$buylastprice[0]->productunitid;
					$buybilldetailquantity=$buylastprice[0]->buybilldetailquantity;
					
						$productunitData=loadProductUnitWithProductAndUnit($storedetail->productid,$productunitid);
					    $productnumber=$productunitData->productnumber;
					    $finalquantity=$buybilldetailquantity*$productnumber;
					
				$storedetail->productBuyPrice = ($buylastprice[0]->buybilldetailprice)
				/$finalquantity;
			
			}
		}
			
			$productBuyPrice=$storedetail->productBuyPrice;
			$productQuantity=$storedetail->productquantity;
			$SumProductPrice=$productBuyPrice * $productQuantity;
			
			$sumValue=$SumProductPrice+$sumValue;
				
			///////////To show product category name (the product whole path)
			$parentId = $storedetail->productCatId;
			//names of product's parents
			$pathArr = getProductPath_recursive($parentId, $categories);
			
			//new product name with all path
	
			if($_SESSION['hidecat'] ==1)
			{
				
				
			
			$storedetail->productName = $storedetail->productName.'/'.$pathArr;
			}
			else
			{
					
				$storedetail->productName = $storedetail->productName;
			}
		}
	//print_r($storedetailData);
		return array($storedetailData,$sumValue);
	}
	

	// show the form by product name
	function showByProductNameAndStore()
	{
		global $myStoredetailEx;
		global $ProgramsettingDAO;
		global $buyBillDetail;
		global $buyBillDetailDAO;
		global $buyBillDetailExt;
		global $returnBuyBillDetail;
		global $returnBuyBillDetailDAO ;
		global $returnBuyBillDetailExt;
		
		$productId=$_REQUEST['productId'];
		$storeId=$_REQUEST['storeId'];

		//select all data from storedetail by productId
		$storedetailData=$myStoredetailEx->queryWithProductIdAndStoreId($productId, $storeId);
		
		$sumValue=0;
		foreach($storedetailData as $storedetail)
		{	$Programsettingdata=$ProgramsettingDAO->load(1);
			if($Programsettingdata->lastprice=="0")
			{
				$buylastprice = $buyBillDetailDAO->queryByBuybilldetailproductid($storedetail->productid);
				if(count($buylastprice)!="0"){
				$storedetail->productBuyPrice = $buylastprice[0]->buybilldetailprice;
			}
		}
			$productBuyPrice=$storedetail->productBuyPrice;
			$productQuantity=$storedetail->productquantity;
			$SumProductPrice=$productBuyPrice * $productQuantity;
			
			$sumValue=$SumProductPrice+$sumValue;
				
			///////////To show product category name (the product whole path)
			$parentId = $storedetail->productCatId;
			//names of product's parents
			$pathArr = getProductPath_recursive($parentId, $categories);
			
			//new product name with all path
			$storedetail->productName = $storedetail->productName.'/'.$pathArr;
		}
	
		return array($storedetailData,$sumValue);
	}

	
	// show the form by store Name
	function showBystoreName()
	{
		global $myStoredetailEx;
		
		global $ProgramsettingDAO;
		global $buyBillDetail;
		global $buyBillDetailDAO;
		global $buyBillDetailExt;
		global $returnBuyBillDetail;
		global $returnBuyBillDetailDAO ;
		global $returnBuyBillDetailExt;
		

		
		
		$storeId=$_REQUEST['storeId'];

		$storedetailData=$myStoredetailEx->queryWithStoreId($storeId);
		
	
		$sumValue=0;
		foreach($storedetailData as $storedetail)
		{
				$Programsettingdata=$ProgramsettingDAO->load(1);
				if($Programsettingdata->lastprice=="0"){
			$buylastprice = $buyBillDetailDAO->queryByBuybilldetailproductid($storedetail->productid);
			if(count($buylastprice)!="0"){
			$storedetail->productBuyPrice = $buylastprice[0]->buybilldetailprice;
			}}
			$productBuyPrice=$storedetail->productBuyPrice;
			$productQuantity=$storedetail->productquantity;
			$SumProductPrice=$productBuyPrice * $productQuantity;
			
			$sumValue=$SumProductPrice+$sumValue;
				
			///////////To show product category name (the product whole path)
			$parentId = $storedetail->productCatId;
			//names of product's parents
			$pathArr = getProductPath_recursive($parentId, $categories);
			
			//new product name with all path
			$storedetail->productName = $storedetail->productName.'/'.$pathArr;
		}
		return array($storedetailData,$sumValue);
	}
	
		
	// show the form by product category Name
	function showByProductCatNameAndStoreId()
	{
		global $myStoredetailEx;
		global $ProgramsettingDAO;
		global $buyBillDetail;
		global $buyBillDetailDAO;
		global $buyBillDetailExt;
		global $returnBuyBillDetail;
		global $returnBuyBillDetailDAO ;
		global $returnBuyBillDetailExt;
		
		$productCatId=$_REQUEST['productCatId'];
		$storeId=$_REQUEST['storeId'];
		
		if(isset($productCatId))
		{
			$storedetailData=$myStoredetailEx->queryWithProductCatAndStoreId($productCatId, $storeId);
		
			$sumValue=0;
			foreach($storedetailData as $storedetail)
			{	$Programsettingdata=$ProgramsettingDAO->load(1);
					if($Programsettingdata->lastprice=="0"){
				$buylastprice = $buyBillDetailDAO->queryByBuybilldetailproductid($storedetail->productid);
			if(count($buylastprice)!="0"){
			$storedetail->productBuyPrice = $buylastprice[0]->buybilldetailprice;
			}}
				
				$productBuyPrice=$storedetail->productBuyPrice;
				$productQuantity=$storedetail->productquantity;
				$SumProductPrice=$productBuyPrice * $productQuantity;
				
				$sumValue=$SumProductPrice+$sumValue;
				
				///////////To show product category name (the product whole path)
				$parentId = $storedetail->productCatId;
				//names of product's parents
				$pathArr = getProductPath_recursive($parentId, $categories);
				
				//new product name with all path
				$storedetail->productName = $storedetail->productName.'/'.$pathArr;
			}
		}
		return array($storedetailData,$sumValue);
	}


	// show the form 
	function showAll()
	{
		global $myStoredetailEx;

		$storedetailData = $myStoredetailEx->queryWithOrder();

		$sumValue=0;
		if(count($storedetailData) > 0)
		{
			foreach($storedetailData as $storedetail)
			{
				//إجمالي سعر المنتجات
				$productBuyPrice = $storedetail->productBuyPrice;
				$productQuantity = $storedetail->productquantity;
				$SumProductPrice = $productBuyPrice * $productQuantity;
				
				$sumValue += $SumProductPrice;
				
				///////////To show product category name (the product whole path)
				$parentId = $storedetail->productCatId;
				//names of product's parents
				$pathArr = getProductPath_recursive($parentId, $categories);
				
				//new product name with all path
				$storedetail->productName = $storedetail->productName.'/'.$pathArr;
			}
		}
		return array($storedetailData,$sumValue);
	}
	
	
	function getProductPath_recursive($parentid, $categories)
	{
		global $productCatExt;

		$catData = $productCatExt->getCategoryAndParentByCatId($parentid);

		if(count($catData) > 0)
		{
			$categories .= $catData->productCatName.'/'; 	
			$newParentId = $catData->productCatParent;

			return getProductPath_recursive($newParentId, $categories);
		}
		$categories = substr($categories, 0, strlen($categories) - 1);
		return $categories;	
	}
	
function loadProductUnitWithProductAndUnit($productId,$unitId)
	{
		
		//to use the variable out side the funcion
		global $myProductunitEx;
		
		// select all data from productunit tbl 
		$productunitData=$myProductunitEx->queryWithProductIdAndUnitId($productId,$unitId);
		
		return $productunitData;
		
	}
	
?>