<?php 

function openingBalance($function_filter,$item)
							{
								global $db;
								$site=$function_filter[0];
								$store=$function_filter[1];
								$from=$function_filter[2];								
								if($site!='')
								$filter[]="site = '".$site."'";
								if($store!='')
								$filter[]="store = '".$store."'";
								if($from!='--' && $to!='--')
								$filter[]="opening_date <= '".$from."'";
								if($item!='')
								$filter[]="machine_id = '".$item."'";
								
								$filter=implode(' and ',$filter);
								$sql1="Select stock, rate from opening_stock_For_Toolsplants_machine where $filter";
							
								$result1=DB_query($sql1,$db);
								$myrow1=DB_fetch_array($result1);
								return $myrow1['stock'];
							}
                       ////////////Opening Balance End/////////////////////////
					   
					   

						////1. MRN/////
                    function receivedMRN($function_filter,$item)
						{
							global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							if($site!='')
							$filter[]="addmrn.site = '".$site."'";
							if($store!='')
							$filter[]="addmrn.StoreCode = '".$store."'";
							if($item!='')
							$filter[]="addmrnqty.mst_itemid = '".$item."'";
							if($from!='')
							$filter[]="addmrn.MRNDate <= '".$from."'";
							
					
							
							$filter=implode(' and ',$filter);
						    	$sql1="Select SUM(addmrnqty.reciveqty) from addmrn left join addmrnqty 
							       on(addmrn.id=addmrnqty.issue_id) where Cancel_Status='N' and $filter";
								 
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(addmrnqty.reciveqty)'];
						}
						

							////1. Issue Slip/////
                       function issueSlip($function_filter,$item)
							{
								global $db;
								$site=$function_filter[0];
								$store=$function_filter[1];
								$from=$function_filter[2];
								if($site!='')
								$filter[]="I.site = '".$site."'";
								if($store!='')
								$filter[]="I.mst_storeid = '".$store."'";
								if($item!='')
								$filter[]="IA.asset_id = '".$item."'";
								if($from!='--')
								$filter[]="I.issue_date<='".$from."'";
								
								$filter=implode(' and ',$filter);
							
							    $sql1="Select SUM(IA.issue_qty) from tpl_asset_transfer_issue I left join 
								 tpl_asset_transfer_issue_details IA on(I.id=IA.issue_id) where I.cancel_issue=0 and $filter";
								
								$result1=DB_query($sql1,$db);
								$myrow1=DB_fetch_array($result1);
								
								return $myrow1['SUM(IA.issue_qty)'];
							}




/// added new function 

////2. Material return/////

				function receivedReturn($function_filter,$item)
				{
					global $db;
					$site=$function_filter[0];
					$store=$function_filter[1];
					$from=$function_filter[2];
					$to=$function_filter[3];
					
					if($site!='')
					$filter[]="material_return.site = '".$site."'";
					if($store!='')
					$filter[]="material_return.store = '".$store."'";
					if($from!='--')
					$filter[]="material_return.returndate <= '".$from."' ";
					if($item!='')
					$filter[]="material_return_details.mst_itemid = '".$item."'";
					
					$filter=implode(' and ',$filter);
					   $sql1="Select SUM(material_return_details.receiveqty)
					   from material_return left join material_return_details   
					   on(material_return.id=material_return_details.parentId) where $filter";
				 
					
					$result1=DB_query($sql1,$db);
					$myrow1=DB_fetch_array($result1);
					return $myrow1['SUM(material_return_details.receiveqty)'];
				}



				function receivedSiteToSite($function_filter,$item)
				{
					global $db;
					$site=$function_filter[0];
					$store=$function_filter[1];
					$from=$function_filter[2];
					$to=$function_filter[3];
					
					if($site!='')
					$filter[]="A.receive_site = '".$site."'";
					if($store!='')
					$filter[]="A.store = '".$store."'";
					if($from!='--')
					$filter[]="A.receive_date <='".$from."' ";
					if($item!='')
					$filter[]="B.asset_id = '".$item."'";
					
					$filter=implode(' and ',$filter);
					
					 $sql1="Select SUM(B.received_qty) 
					 from tpl_asset_transfer_receive A left join tpl_asset_transfer_receive_details B
					 on(A.id=B.receive_id) where $filter";
					$result1=DB_query($sql1,$db);
					$myrow1=DB_fetch_array($result1);
					return $myrow1['SUM(B.received_qty)'];
				}

				function issueSiteToSite($function_filter,$item)
				{
					global $db;
				 
					$site=$function_filter[0];
					$store=$function_filter[1];
					$from=$function_filter[2];
					$to=$function_filter[3];
					
					if($site!='')
					$filter[]="I.sitecode = '".$site."'";
					if($store!='')
					$filter[]="I.fromstore = '".$store."'";
					if($from!='--')
					$filter[]="I.issuedate <= '".$from."' ";
					if($item!='')
					$filter[]="ID.mst_itemid = '".$item."'";
					
					$filter=implode(' and ',$filter);
					
					$sql1="Select SUM(ID.issueqty) 
						 from tpl_asset_transfer_issue I left join tpl_asset_transfer_issue_details ID
						 on(I.id=ID.transfer_issue_id) where $filter";
					$result1=DB_query($sql1,$db);
					$myrow1=DB_fetch_array($result1);
					$return_array=$myrow1['SUM(ID.issueqty)'];
					//$avg_rate=$myrow1['SUM(transfer_issue_details.rate)'];
					//$return_array[]=$avg_rate;
					return $return_array;
					
				}

					function issueMatarialReturn($function_filter,$item)
					{
						global $db;
						$site=$function_filter[0];
						$store=$function_filter[1];
						$from=$function_filter[2];
						$to=$function_filter[3];
						
						if($site!='')
						$filter[]="inv_material_return.sitecode = '".$site."'";
						if($store!='')
						$filter[]="inv_material_return.storecode = '".$store."'";
						if($from!='--')
						$filter[]="inv_material_return.Reqdate <= '".$from."' ";
						if($item!='')
						$filter[]="inv_material_return_details.mst_itemid = '".$item."'";
						
						$filter=implode(' and ',$filter);
						
					  $sql1="Select SUM(inv_material_return_details.ReturnQty) 
					  from inv_material_return left join inv_material_return_details  
					  on(inv_material_return.id=inv_material_return_details.parentId) where $filter";
						$result1=DB_query($sql1,$db);
						$myrow1=DB_fetch_array($result1);
						return $myrow1['SUM(inv_material_return_details.ReturnQty)'];
					}


					/*function return_rejection($function_filter,$item)
					{
						global $db;
						$site=$function_filter[0];
						$store=$function_filter[1];
						$from=$function_filter[2];
						$to=$function_filter[3];
						
						if($site!='')
						$filter[]="rejection_tbl.issue_branch = '".$site."'";
						if($store!='')
						$filter[]="rejection_tbl.issue_stroe = '".$store."'";
						if($from!='--' && $to!='--')
						$filter[]="rejection_tbl.date <= '".$from."' ";
						if($item!='')
						$filter[]="rejection_tbl_details.itemid = '".$item."'";
						
						$filter=implode(' and ',$filter);
						
						  $sql1="Select SUM(rejection_tbl_details.returnqty) 
						  from rejection_tbl left join rejection_tbl_details
						   on(rejection_tbl.id=rejection_tbl_details.parentId) where $filter";
						$result1=DB_query($sql1,$db);
						$myrow1=DB_fetch_array($result1);
						return $myrow1['SUM(rejection_tbl_details.returnqty)'];
					}*/
?>