<?php
class userdefinefunctions {
	//----------------calculate sales cost
		function calculatecost_sale($field,$ord,$orderby,$limit,$like,$operand ,$cols,$cnd,$join,$start,$db,$count ,$innertable)
			{
							$names = $db->fetchAlljoinrowise($field,$ord,$orderby,$limit,$like,$operand ,$cols,$cnd,$join,$start );//,'cat_id DESC'
							if(count($names)>0)
							{
								$totcost=0;
								foreach($names as $names)
								{
										    $unitval1=1;
											$ing=$names['ingredient_id'];
											$quantity=$names['quantity'];
											$unit=$names['unit'];
											if($unit==1){  $unitval1=(1/1000)*(1/1000); }if($unit==2){  $unitval1=(1/1000); }if($unit==3){  $unitval1=1; }if($unit==4){  $unitval1=(1/1000); } if($unit==5){  $unitval1=1; }
											if($quantity=='')
											$quantity=1;
											$join1=' a.Ingredient= '.$ing;
											$names1 = $db->fetchAlljoineachrow($innertable, $innerord, $innertorderby, $innerlimit,$cols,$innerord,$join1,$start );//,'cat_id DESC'
											//echo $names1[0]['rate'].'<br>';
											$totcost+=($names1[0]['rate']*$quantity*$unitval1) ;
								}
							}
							else
							{
							$totcost=0;
							}
							return $totcost;
			}
//------------------------------------------------function quantity reduced and price reduced----------------//
function calculate_qty( $db,$limit,$cols,$ordid,$join,$start,$count,$order_item_price )
	{
					$names = $db->fetchAlljoin('Ingredients as a, receipe_ingredient as b', '', 'a.id', $limit, $like=false, $operand='AND',$cols,$ordid,$join,$start );//,'cat_id DESC'
					$arr=array();
					$i=0;
					foreach($names as $names)
					{
								$arr[$i]['name']=$names['name'];
								$unit =$names['unit'];
								$qty=$names['qty']; 
								//-------------category fetching
								$ingredient=$names['ingredient'];
								if($ingredient!=''){
								$cat='     Ingredient= "'.$ingredient.'" ';   }   
								$from='purchase';
								$field=' sum(quantity)	 as quantity,unitval ';
								$group=' ingredient ';
								//---take quantity from purchase table
								$quantity1=$db->getindividualdatagrp($from,$cat,$field,$group);
								$quantity=$quantity1->quantity;
								$unitval=$quantity1->unitval;
								if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
								$arr[$i]['totalquantity']=$quantity*$unitvals;
								//---------------------------toal qty get
								//---------------sales qty
								//take unit
								/*$from1='ingredient_ units';
								$cat1='     ing_id= "'.$ingredient.'" ';   }   
								$field1=' sum(quantity)	 as quantity ';
								$group1=' ingredient ';
								$unit=$db->getindividualdatagrp($from1,$cat1,$field1,$group1);*/
								
								if($unit==1){  $unitval1=(1/1000)*(1/1000); }if($unit==2){  $unitval1=(1/1000); }if($unit==3){  $unitval1=1; }if($unit==4){  $unitval1=(1/1000); } if($unit==5){  $unitval1=1; }
							    $salesqty=$qty*$count*$unitval1;
								//-------------------------------------------------//
								$arr[$i]['salesqty']=$salesqty;
     						//	echo 'qty'.$qty.'unit'.$unitval1.'count'.$count.'<br>saleqty'.$salesqty.'<br>';
								//----------------------remaining qty
								//----------------------making cost----------------
							 	$makingcost=$this->makingcostval($ingredient,$salesqty,$db);
								
								$arr[$i]['makingcost']=$makingcost;
								//-------------------------------------------------------//
									//----------------------sales cost----------------
								$salecost=$order_item_price*$salesqty;
								$arr[$i]['salecost']=$salecost;
								//-------------------------------------------------------//
								$rem_qty=$quantity-$salesqty;
								$arr[$i]['rem_qty']=$rem_qty;
								$arr[$i]['id']=$names['id'];
								$i++;
								
					}
					return $arr;
	}
	function makingcostval($ingredient,$salesqty,$db)
	{
		$totcost=0;$start=0;$cost=0;
		$join1=' a.Ingredient= '.$ingredient;
		$names11 = $db->fetchAlljoineachrow('purchase as a ', '', 'a.date', '',$cols=' a.rate,a.quantity,a.unitval' ,' asc',$join1,$start );//,'cat_id DESC'
		//echo 'Qty  --Saleqty <br>';
		foreach($names11 as $names11)
			{
			//	echo '-saled qty-';echo $salesqty;echo '<br>';
				$quantity=$names11['quantity']; 
				$rate=$names11['rate'];
				$unitval=$names11['unitval'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				 $qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				if($salesqty>0 && $qty>0)
				{
					if($salesqty<=$qty)
					{
						$cost=$salesqty*$rate;
						//echo 'saleqty1r'.$salesqty;echo '--';echo '<br>';
					//	echo 'saleqty1--'.$salesqty.'--';echo 'rate1--'.$rate.'--';echo $salesqty.'*'.$rate;echo '--cost1--'.$cost.'<br>';
						$salesqty=$qty-$salesqty;$salesqty=0;$totcost+=$cost;
					}
				}
				if($salesqty>0 && $qty>0)
				{
					if($salesqty>$qty){$cost=$qty*$rate;   $totcost+=$cost;$salesqty=$salesqty-$qty; }
				//	echo 'saleqty2r'.$salesqty;echo '--';echo '<br>';
				//   echo 'saleqty2--'.$salesqty.'--';echo 'rate2--'.$rate.'--';echo $qty.'*'.$rate;echo '--cost2--'.$cost.'<br>';
				}
				
				
				
				
			//	echo 'sales'.$salesqty.'<br>';
				//echo $quantity.'--'.$qty.'--'.$rate.'--'.$salesqty.'--'.$cost.'<br>';
				//echo $quantity .'--'.$salesqty.'<br>';
			}
			return $totcost;
		//	echo 'totalcost'.$totcost;
			//echo $totcost;
	}
	//-------------------------qty updated-----------------------------------
	//------------------------------------------------function quantity reduced and price reduced----------------//
function calculate_qtyupdate( $db,$limit,$cols,$ordid,$join,$start,$count,$order_item_price )
	{
					$names = $db->fetchAlljoin('Ingredients as a, receipe_ingredient as b', '', 'a.id', $limit, $like=false, $operand='AND',$cols,$ordid,$join,$start );//,'cat_id DESC'
					$arr=array();
					$i=0;
					foreach($names as $names)
					{
								$arr[$i]['name']=$names['name'];
								$unit =$names['unit'];
								$qty=$names['qty']; 
								//-------------category fetching
								$ingredient=$names['ingredient'];
								if($ingredient!=''){
								$cat='     Ingredient= "'.$ingredient.'" ';   }   
								$from='purchase';
								$field=' sum(quantity)	 as quantity,unitval ';
								$group=' ingredient ';
								//---take quantity from purchase table
								$quantity1=$db->getindividualdatagrp($from,$cat,$field,$group);
								$quantity=$quantity1->quantity;
								$unitval=$quantity1->unitval;
								if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
								$arr[$i]['totalquantity']=$quantity*$unitvals;
								//---------------------------toal qty get
								//---------------sales qty
								//take unit
								/*$from1='ingredient_ units';
								$cat1='     ing_id= "'.$ingredient.'" ';   }   
								$field1=' sum(quantity)	 as quantity ';
								$group1=' ingredient ';
								$unit=$db->getindividualdatagrp($from1,$cat1,$field1,$group1);*/
								
								if($unit==1){  $unitval1=(1/1000)*(1/1000); }if($unit==2){  $unitval1=(1/1000); }if($unit==3){  $unitval1=1; }if($unit==4){  $unitval1=(1/1000); } if($unit==5){  $unitval1=1; }
							   // $salesqty=$qty*$count*$unitval1;
								//-------------------------------------------------//
								//$arr[$i]['salesqty']=$salesqty;
     						//	echo 'qty'.$qty.'unit'.$unitval1.'count'.$count.'<br>saleqty'.$salesqty.'<br>';
								//----------------------remaining qty
								//----------------------making cost----------------
							 	$makingcost=$this->makingqty_updated($ingredient,$salesqty,$db);
								
								$arr[$i]['makingcost']=$makingcost;
								//-------------------------------------------------------//
									//----------------------sales cost----------------
								$salecost=$order_item_price*$salesqty;
								$arr[$i]['salecost']=$salecost;
								//-------------------------------------------------------//
								$rem_qty=$quantity-$salesqty;
								$arr[$i]['rem_qty']=$rem_qty;
								$arr[$i]['id']=$names['id'];
								$i++;
					}
					return $arr;
	}
	
function makingqty_updated_in_wastage($ingredient,$salesqty,$date,$db)
	{
		$totcost=0;$start=0;$cost=0;
		
		$join1=' a.Ingredient= '.$ingredient;
		if($date=='')
		{
		$names11 = $db->fetchAlljoineachrows('purchase as a ', '1', 'a.date', '',$cols=' a.id,a.rate,a.quantity,a.unitval,a.date' ,' asc',$join1,$start );
		}else{
		$names11 = $db->fetchAlljoineachrows('purchase as a ', 'a.date="'.$date.'"', 'a.date', '',$cols=' a.id,a.rate,a.quantity,a.unitval,a.date' ,' asc',$join1,$start );}//,'cat_id DESC'
		//echo 'Qty  --Saleqty <br>';
	 
		foreach($names11 as $names11)
			{
			//	echo '-saled qty-';echo $salesqty;echo '<br>';
				 $quantity=$names11['quantity']; 
				$rate=$names11['rate'];
				$purchaseid=$names11['id'];
				$date=$names11['date'];
				
				$unitval=$names11['unitval'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				 $qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				
				if($salesqty>0 && $qty>0)
				{
				
					if($salesqty<=$qty)
					{
						$cost=$salesqty*$rate;
						
						//echo 'saleqty1r'.$salesqty;echo '--';echo '<br>';
					//	echo 'saleqty1--'.$salesqty.'--';echo 'rate1--'.$rate.'--';echo $salesqty.'*'.$rate;echo '--cost1--'.$cost.'<br>';
						$salesqty=$qty-$salesqty;$totcost+=$cost;
						
						mysql_query("update purchase set quantity='".$salesqty."' where  id='".$purchaseid."' and date='".$date."'"); 
					}
				}
				if($salesqty>0 && $qty>0)
				{ 
					if($salesqty>$qty)
					{
						$cost=$qty*$rate;   
						$totcost+=$cost;
						$salesqty=$salesqty-$qty; 
						mysql_query("update purchase set quantity=0 where  id='".$purchaseid."' "); 
					}
				//	echo 'saleqty2r'.$salesqty;echo '--';echo '<br>';
				//   echo 'saleqty2--'.$salesqty.'--';echo 'rate2--'.$rate.'--';echo $qty.'*'.$rate;echo '--cost2--'.$cost.'<br>';
				}
				
				
				
				
			//	echo 'sales'.$salesqty.'<br>';
				//echo $quantity.'--'.$qty.'--'.$rate.'--'.$salesqty.'--'.$cost.'<br>';
				//echo $quantity .'--'.$salesqty.'<br>';
			}
			return $totcost;
		//	echo 'totalcost'.$totcost;
			//echo $totcost;
	}
	
	function makingqty_updated($ingredient,$salesqty,$db)
	{
		$totcost=0;$start=0;$cost=0;
		$join1=' a.Ingredient= '.$ingredient;
		$names11 = $db->fetchAlljoineachrow('purchase as a ', '', 'a.date', '',$cols=' a.id,a.rate,a.quantity,a.unitval' ,' asc',$join1,$start );//,'cat_id DESC'
		//echo 'Qty  --Saleqty <br>';
		foreach($names11 as $names11)
			{
			//	echo '-saled qty-';echo $salesqty;echo '<br>';
				$quantity=$names11['quantity']; 
				$rate=$names11['rate'];
				$purchaseid=$names11['id'];
				$unitval=$names11['unitval'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				 $qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				if($salesqty>0 && $qty>0)
				{
					if($salesqty<=$qty)
					{
						$cost=$salesqty*$rate;
						//echo 'saleqty1r'.$salesqty;echo '--';echo '<br>';
					//	echo 'saleqty1--'.$salesqty.'--';echo 'rate1--'.$rate.'--';echo $salesqty.'*'.$rate;echo '--cost1--'.$cost.'<br>';
						$salesqty=$qty-$salesqty;$salesqty=0;$totcost+=$cost;
						mysql_query("update purchase set quantity='".$salesqty."' where  id='".purchaseid."' "); 
					}
				}
				if($salesqty>0 && $qty>0)
				{
					if($salesqty>$qty)
					{
						$cost=$qty*$rate;   
						$totcost+=$cost;
						$salesqty=$salesqty-$qty; 
						mysql_query("update purchase set quantity=0 where  id='".purchaseid."' "); 
					}
				//	echo 'saleqty2r'.$salesqty;echo '--';echo '<br>';
				//   echo 'saleqty2--'.$salesqty.'--';echo 'rate2--'.$rate.'--';echo $qty.'*'.$rate;echo '--cost2--'.$cost.'<br>';
				}
				
				
				
				
			//	echo 'sales'.$salesqty.'<br>';
				//echo $quantity.'--'.$qty.'--'.$rate.'--'.$salesqty.'--'.$cost.'<br>';
				//echo $quantity .'--'.$salesqty.'<br>';
			}
			return $totcost;
		//	echo 'totalcost'.$totcost;
			//echo $totcost;
	}
	//calculate average cost
	function currentavgcost($ingredientid,$db,$date)
	{
			$join1=' a.Ingredient= '.$ingredientid;
			$cols=' a.id,a.rate,a.value,a.quantity,a.unitval ';
			$join1='  a.date<"'.$date.'"';
			$start=0;
			$costqry = $db->fetchAlljoineachrow('purchase as a ', '', 'a.date', '',$cols,' desc',$join1,$start );//,'cat_id DESC'
			$avgcost=0;	$avgrate=0;
			$i=1;
			foreach($costqry as $costqry)
			{
			$costamt+=$costqry['rate'];
			$costval+=($costqry['quantity']*$costqry[0]['rate']);
			$qty+=$costqry['quantity'];
			$i++;
			}
			$costamt=ceil($costamt/$i);
			$qty=ceil($qty/$i);
			$costval=ceil($costamt*$qty);
			return $costamt.'@'.$costval.'@'.$qty;
	}
	function currentQty_cost($ingredientid,$db,$date)
	{
			$join1=' a.Ingredient= '.$ingredientid;
			$cols=' a.id,a.rate,a.value,a.quantity,a.unitval ';
			$join1='  a.date<"'.$date.'"';
			$start=0;
			$costqry = $db->fetchAlljoineachrow('purchase as a ', '', 'a.date', '',$cols,' desc',$join1,$start );//,'cat_id DESC'
			$avgcost=0;	$avgrate=0;
			$i=1;
			foreach($costqry as $costqry)
			{
			$costamt+=$costqry['rate'];
			$costval+=($costqry['quantity']*$costqry[0]['rate']);
			$qty+=$costqry['quantity'];
			$i++;
			}
			$costamt=ceil($costamt/$i);
			//$qty=ceil($qty/$i);
			$qty=ceil($qty/1);
			$costval=ceil($costamt*$qty);
			return $costamt.'@'.$costval.'@'.$qty;
	}
	//-----------------calculate current cost
	function currentamt($ingredientid,$db)
	{
			$join1=' a.Ingredient= '.$ingredientid;
			$cols=' a.id,a.rate,a.value,a.quantity,a.unitval' ;
			$start=0;
			$costqry = $db->fetchAlljoineachrow('purchase as a ', '', 'a.date', 1,$cols,' desc',$join1,$start );//,'cat_id DESC'
			$costamt=$costqry[0]['rate'];
			$costval=($costqry[0]['rate']*$costqry[0]['quantity']);
			$costqty=$costqry[0]['quantity'];
			return $costamt.'@'.$costval.'@'.$costqty;
	}
	function leastcostvendor($ingredientid,$db)
	{
			$join11=' a.Ingredient= '.$ingredientid." and a.vendor=b.id ";
			$cols1=' a.id,a.vendor,b.name' ;
			$start1=0;
			$vendorqry = $db->fetchAlljoineachrow(' purchase as a,vendors as b ', '', 'a.rate', 1,$cols1,' asc',$join11,$start1 );//,'cat_id DESC'
			$vendorname=$vendorqry[0]['name'];
			return $vendorname;
	}
	function newquantity($ingredientid,$db)
	{
			$join11=' a.Ingredient= '.$ingredientid;
			$cols1=' a.id,a.quantity ' ;
			$start1=0;
			$quantityqry = $db->fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
			$quantity=$quantityqry[0]['quantity'];
			return $quantity;
	
	}
	function lastpurchasedate($ingredientid,$db)
	{
			$join11=' a.Ingredient= '.$ingredientid;
			$cols1=' a.date ' ;
			$start1=0;
			$datesqry = $db->fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
			$date=$datesqry[0]['date'];
			return $date;
	
	}
	function lastpurchasedateselect($ingredientid,$db,$sdate1,$sdate2)
	{
			$join11=' a.Ingredient= '.$ingredientid;
			if($sdate1!='' && $sdate1!='1970-01-01' &&  $searchdate11!='' && $searchdate11=='1970-01-01'){$join11.='  and a. date= "'.$sdate1.'" ';  }
			else{
			 if($sdate1!='' && $sdate1!='1970-01-01'){$join11.='  and a. date>= "'.$sdate1.'" ';  } if($sdate2!='' && $sdate2!='1970-01-01'){$join11.='  and a. date<= "'.$sdate2.'" ';  }
			 }
			$cols1=' a.date ' ;
			$start1=0;
			$datesqry = $db->fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
			$date=$datesqry[0]['date'];
			return $date;
	}
	function inventorytotalquantity($ingredientid,$db,$searchdate,$searchdate11,$group='')
	{
			$join11=' a.Ingredient= '.$ingredientid;
		//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			if($searchdate!='1970-01-01' && $searchdate11=='1970-01-01'){$join11.='  and a. date= "'.$searchdate.'" ';  }
			else{
			 if($searchdate!='1970-01-01'){$join11.='  and a. date>= "'.$searchdate.'" ';  } if($searchdate11!='1970-01-01'){$join11.='  and a. date<= "'.$searchdate11.'" ';  }
			 }
			//$cols=' sum(a.quantity) as totqty' ;
			//$group=' a.date ';
			$cols='  a.quantity,a.unitval,a.rate ' ;
			$totqry = $db->fetchAlljoinGrpquery(' purchase as a  ' , $where='', $orderBy, $limit='', $like=false, $operand='AND',$cols,$asc='',$join11,$start='',$group);
			//------------------------------------------------------
			$total=0;$totalrate=0;
			foreach($totqry as $names11)
			{
			    $unitval=$names11['unitval'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				$rate=$names11['rate'];
				$quantity=$names11['quantity'];
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				$qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				$total+=$qty;
				$totalrate+=$qty*$rate;
			} 
			 $tot=$totalrate;
			 
				 //-------------------------------------------------------
			$totqty=round($total);
			//echo $totqty.'<br>';
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty.'@'.$tot;
	
	}
	function inventorysaledquantity($ingredientid,$db,$searchdate,$searchdate11,$group='')
	{
			$join11=" b.item_id=a.menu_id ";
			$join11.='  and a.ingredient	= '.$ingredientid;
			//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			if($searchdate!='1970-01-01' && $searchdate11=='1970-01-01'){$join11.='  and b. date= "'.$searchdate.'" ';  }
			else{
			 if($searchdate!='1970-01-01'){$join11.='  and b.date>= "'.$searchdate.'" ';  } if($searchdate11!='1970-01-01'){$join11.='  and b.date<= "'.$searchdate11.'" ';  }
			 }
			$cols='  a.quantity,a.unit ' ;
		//	$group=' a.date ';
		    
			$totqry = $db->fetchAlljoinGrpquery(' receipe_ingredient as a,order_details as b  ' , $where='', $orderBy, $limit='', $like=false, $operand='AND',$cols,$asc='',$join11,$start='',$group);
			//------------------------------------------------------
			$total=0;
			foreach($totqry as $names11)
			{
			    $unitval=$names11['unit'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				$quantity=$names11['quantity'];
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				$qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				$total+=$qty;
			}
			$totqty=round($total);
				 //-------------------------------------------------------
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty;
	}
	function inventoryextraquantity($ingredientid,$db,$searchdate,$searchdate11,$type, $group='')
	{
			$join11=" b.item_id=a.menu_id ";
			$join11.='  and a.ingredient	= '.$ingredientid.' and b.type="'.$type.'" ';
			//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			if($searchdate!='1970-01-01' && $searchdate11=='1970-01-01'){$join11.='  and b. date= "'.$searchdate.'" ';  }
			else{
			 if($searchdate!='1970-01-01'){$join11.='  and b.date>= "'.$searchdate.'" ';  } if($searchdate11!='1970-01-01'){$join11.='  and b.date<= "'.$searchdate11.'" ';  }
			 }
			$cols='  a.quantity,a.unit ' ;
		//	$group=' a.date ';
		   
			$totqry = $db->fetchAlljoinGrpquery(' receipe_ingredient as a,extramenuloss as b  ' , $where='', $orderBy, $limit='', $like=false, $operand='AND',$cols,$asc='',$join11,$start='',$group);
			//------------------------------------------------------
			$total=0;
			foreach($totqry as $names11)
			{
			    $unitval=$names11['unit'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				$quantity=$names11['quantity'];
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				$qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				$total+=$qty;
			}
			$totqty=round($total);
				 //-------------------------------------------------------
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty;
	}
	function totalcurrentstocktaking($ingredientid,$db,$searchdate,$searchdate11,$type, $group='')
	{
	     //   $join11=" b.item_id=a.menu_id ";
			$join11.='    a.ingnt_id	= '.$ingredientid;
			//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			if($searchdate!='1970-01-01' && $searchdate11=='1970-01-01'){$join11.='  and a.date= "'.$searchdate.'" ';  }
			else{
			 if($searchdate!='1970-01-01'){$join11.='  and a.date>= "'.$searchdate.'" ';  } if($searchdate11!='1970-01-01'){$join11.='  and a.date<= "'.$searchdate11.'" ';  }
			 }
			$cols='  sum(a.stk_qty) as quantity ' ;
		//	$group=' a.date ';
		   
			$totqry = $db->fetchAlljoinGrpquery(' stock_taking as a  ' , $where='', $orderBy, $limit='', $like=false, $operand='AND',$cols,$asc='',$join11,$start='',$group);
			if($totqry[0]['quantity']!='')
			$total=$totqry[0]['quantity'];
			else
			$total=0;
			//------------------------------------------------------
		
			$totqty=round($total);
				 //-------------------------------------------------------
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty;
	
	}
	function openingstock($ingredientid,$db,$searchdate,$searchdate11,$group='')
	{
		//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			//   $join11=" b.item_id=a.menu_id ";
			$join11.='    a.ingnt_id	= '.$ingredientid;
			//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			if($searchdate!='1970-01-01' && $searchdate11=='1970-01-01'){$join11.='  and a.date= "'.$searchdate.'" ';  }
			else{
			 if($searchdate!='1970-01-01'){$join11.='  and a.date>= "'.$searchdate.'" ';  } if($searchdate11!='1970-01-01'){$join11.='  and a.date<= "'.$searchdate11.'" ';  }
			 }
			$cols='  a.stk_qty as quantity ' ;
		//	$group=' a.date ';
		   
			$totqry = $db->fetchAlljoinGrpquery(' stock_taking as a  ' , $where='', $orderBy='a.date', $limit='1', $like=false, $operand='AND',$cols,$asc=' desc',$join11,$start=0,$group);
			if($totqry[0]['quantity']!='')
			$total=$totqry[0]['quantity'];
			else
			$total=0;
			//------------------------------------------------------
		
			$totqty=round($total);
				 //-------------------------------------------------------
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty;
	
	}
	function fetchopeningstock($ingredientid,$db,$searchdate,$searchdate11,$group='',$join11='',$cols='')
	{
			
			$totqry = $db->fetchAlljoinGrpquery(' stock_taking as a  ' , $where='', $orderBy='a.stk_id', $limit='1', $like=false, $operand='AND',$cols,$asc=' desc',$join11,$start=0,$group);
			if($totqry[0]['quantity']!='')
			$total=$totqry[0]['quantity'];
			else
			$total=0;
			//------------------------------------------------------
		
			$totqty=round($total);
				 //-------------------------------------------------------
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty;
	
	}
	function finddamage($ingredientid,$db,$searchdate,$searchdate11,$group=' a.ingredient_id ')
	{
		//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			//   $join11=" b.item_id=a.menu_id ";
			$join11.='    a.ingredient_id	= '.$ingredientid;
			//	if($searchdate=='1970-01-01' && $searchdate11=='1970-01-01')
		//	{$searchdate=date("Y-m-d");}
			if($searchdate!='1970-01-01' && $searchdate11=='1970-01-01'){$join11.='  and a.date= "'.$searchdate.'" ';  }
			else{
			 if($searchdate!='1970-01-01'){$join11.='  and a.date>= "'.$searchdate.'" ';  } if($searchdate11!='1970-01-01'){$join11.='  and a.date<= "'.$searchdate11.'" ';  }
			 }
			$cols='  sum(a.quantity) as quantity ' ;
		//	$group=' a.date ';
		   
			$totqry = $db->fetchAlljoinGrpquery(' wastage as a  ' , $where='', $orderBy='a.date', $limit='1', $like=false, $operand='AND',$cols,$asc=' desc',$join11,$start=0,$group=' a.ingredient_id ');
			if($totqry[0]['quantity']!='')
			$total=$totqry[0]['quantity'];
			else
			$total=0;
			//------------------------------------------------------
		
			$totqty=round($total);
				 //-------------------------------------------------------
		//	fetchAlljoineachrow(' purchase as a  ', '', 'a.date', 1,$cols1,' desc',$join11,$start1 );//,'cat_id DESC'
		//	$totqty=$datesqry[0]['totqty'].'@0@0';
			return $totqty;
	
	}
	/*function profit_in_percentage($sellprice,$purchaseprice)
	{
				$revenue=$sellprice-$purchaseprice;
				$profit=($revenue/$purchaseprice )* 100;
				return $profit;
	}*/
	function makingqty_updatedstocktaking($ingredient,$salesqty,$db)
	{
		$join1=' a.Ingredient= '.$ingredient;
		$names11 = $db->fetchAlljoineachrow('stock_taking as a ', '', 'a.date', '',$cols=' a.stk_id,a.stk_qty,a.units' ,' asc',$join1,$start );//,'cat_id DESC'
		//echo 'Qty  --Saleqty <br>';
		foreach($names11 as $names11)
			{
			//	echo '-saled qty-';echo $salesqty;echo '<br>';
				$quantity=$names11['stk_qty']; 
				$purchaseid=$names11['stk_id'];
				$unitval=$names11['units'];//echo '-receipe qty*unit-';echo $quantity;echo '*'.$unitval.'<br>';
				if($unitval==1){  $unitvals=(1/1000)*(1/1000); }if($unitval==2){  $unitvals=(1/1000); }if($unitval==3){  $unitvals=1; }if($unitval==4){  $unitvals=(1/1000); } if($unitval==5){  $unitvals=1; }
				 $qty=$quantity*$unitvals;// echo '-receipe qty-';echo $qty;echo '<br>';
				if($salesqty>0 && $qty>0)
				{
					if($salesqty<=$qty)
					{
						$salesqty=$qty-$salesqty;$salesqty=0;
						mysql_query("update stock_taking set stk_qty='".$salesqty."' where  stk_id='".$purchaseid."' "); 
					}
				}
				if($salesqty>0 && $qty>0)
				{
					if($salesqty>$qty)
					{
						$salesqty=$salesqty-$qty; 
						mysql_query("update stock_taking set stk_qty=0 where  stk_id='".$purchaseid."' "); 
					}
				//	echo 'saleqty2r'.$salesqty;echo '--';echo '<br>';
				//   echo 'saleqty2--'.$salesqty.'--';echo 'rate2--'.$rate.'--';echo $qty.'*'.$rate;echo '--cost2--'.$cost.'<br>';
				}
				
				
				
				
			//	echo 'sales'.$salesqty.'<br>';
				//echo $quantity.'--'.$qty.'--'.$rate.'--'.$salesqty.'--'.$cost.'<br>';
				//echo $quantity .'--'.$salesqty.'<br>';
			}
			return $totcost;
		//	echo 'totalcost'.$totcost;
			//echo $totcost;
	}
	 //------------------------------function----------------//
 function  calldisplay($empid,$datewise,$db)
 { 
		 $time_query="Select CAST(SUM(timediff(e_out, e_in)) as time) as totalhours
		FROM TIME 
		WHERE e_date =  '".$datewise."' AND  e_id =".$empid."    "; 
		$sql_time=$db->fetchQuery($time_query);
		$totalhours=$sql_time[0]['totalhours'];
		if($totalhours=='00:00:00')$totalhours='';
		if($totalhours=='')$totalhours='<span class="employee-row-02">L</span>';
		return $totalhours;
 }
//--------------------------------------------------------------------------//
}
?>