<?php 
include('functions.php');
$s=new db_query();
//GET Heirarchy from users wise
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');
$r=GETUpperDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$userlist=  UserDeptList($r);
if(count($userlist)>0)
$userlist=implode(",",$userlist).",".$_SESSION['UserID'];
else
$userlist="'".$_SESSION['UserID']."'";

$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);





$lowerlist=  UserDeptList($LowerDeptList);
//echo "<br>--------222--------<br>";
//print_r($LowerDeptList);
if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist).",".$_SESSION['UserID'];
else
$lowerlist="'".$_SESSION['UserID']."'";

 //	echo "<br>******************************userlist****************************************************<br>";
$user_id = $_SESSION['ACG_DashBoard_User'];


$Client1=array();
$ZoneSQL=DB_query("select * from www_users where id in($lowerlist) and assigned_sites in(".$_SESSION['AssignedSites'].")",$db);

while($ZoneClient=DB_fetch_array($ZoneSQL))
{
	$Client1[]=$ZoneClient['id'];
}
$Client2=implode(",",$Client1);
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $page_title;?></h1>
<div class="centre" id="slidingDiv">
<?php  $table_name="salesorder"?>
<form method="post" action="">	
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td class="tabledata1">Branch</td>
    <td><select style="width:150px;" name="Site">
<!--	<option value="0">--Select--</option>-->
<?php $SiteName=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')",$db);
while($SiteRow=DB_fetch_array($SiteName)){ ?>
	<option value="<?php echo $SiteRow['loccode']?>"<?php echo($_REQUEST['Site']==$SiteRow['loccode']?'selected':'');?>><?php echo $SiteRow['locationname']?></option>	
<?php }?>
	</select> 	</td>
	<td class="tabledata1">User</td>
	<td>
<?php $UserSQL=DB_query("select * from www_users where id in($Client2)",$db); ?>
	<select style="width:150px;" name="user" id="user">
			<option value="0">Select Users</option>
<?php while($GetUser=DB_fetch_array($UserSQL)) {?>
			<option value="<?php echo $GetUser['id']; ?>"<?php echo($_REQUEST['user']==$GetUser['id']?'selected':''); ?>><?php echo $GetUser['userid']; ?></option>
<?php } ?>
		</select>	</td>
</tr>
<tr class="row1">
    <td class="tabledata1">From Date</td>
    <td><input type="text" name="fdate" style="width:150px;"  class="datepicker" value="<?php echo $_REQUEST['fdate']?>"/></td>
    <td class="tabledata1">To Date</td>
    <td><input type="text" name="todate" style="width:150px;" class="datepicker"  value="<?php echo $_REQUEST['todate']?>"  /></td>
</tr>
<tr class="row1">
    <td colspan="4"><p align="center"><input name="Go" type="submit" value="Go" class="search" /></p></td>
</tr>
</table>
</form>
</div>


<?php 
if($_REQUEST['Go']){
	
	$filter=array();
	$fromDate=ConvertSQLDate($_REQUEST['fdate']);
	$toDate=ConvertSQLDate($_REQUEST['todate']);
	if($_REQUEST['Site']!='0')
	{
	$filter[]="branch='".$_REQUEST['Site']."'";
	}
	if($_REQUEST['branch']=='0')
	{
	$filter[]="branch='".$_REQUEST['Site']."'";
	}
	if($_REQUEST['user']!='0')
	{
	$filter[]="createdBy='".$_REQUEST['user']."'";
	}
	if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='')
	{
	$filter[]="orderDate between '".$fromDate."' and '".$toDate."'";
	}
	
	if($filter!='')
	{
	$filter1=implode(" and ",$filter);
	
	 $sql="Select * from salesorder where CompanyId='".$_SESSION['ACGCompanyId']."' and $filter1 order by createdDate Desc";
	$_SESSION['SalesOrderPrint']=$sql;
	  $condition1="CompanyId='".$_SESSION['ACGCompanyId']."' and $filter1 order by createdDate Desc";
	
	  $result1=$s->GetRecordById('salesorder',  $condition1);
	}
	if(DB_num_rows($result1)>0)	
	{?>
	<!--      <a href="#" onclick="popup('SalesOrderApproval-print.php?modid=<?php echo $_REQUEST['modid'];?>&amp;frmid=<?php echo $_REQUEST['frmid'];?>',1000,600)">Print</a>
	-->	  
	<a  href="#"  onclick='popup("sales_order_print.php",1000,600)' >Print</a>
	
	&nbsp;&nbsp;<a href="tcpdf/Pdf_SalesOrderApproval.php?loccode=<?php echo $_REQUEST['Site'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fdate'];?>">PDF</a>
	
	&nbsp;&nbsp;<a href="Export_SalesOrderApproval.php?loccode=<?php echo $_REQUEST['Site'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fdate'];?>">Export</a>
	
	<?php 
	while($rowRecord=DB_fetch_array($result1))
	{ 
	$s=new db_query();
	//  ($table_name,  $condition) and A.id=B.masterId
	 $condition="masterId='".$rowRecord['id']."' and (ApprovalStatus='2' || ApprovalStatus='3')";
	$result=$s->GetRecordById('salesorder_details',$condition);
	if(DB_num_rows($result)>0)	
	{
	 $remarks_sql="Select dispatch.*,dispatch_details.* from dispatch inner join dispatch_details on dispatch.id=dispatch_details.parentId where dispatch.againstSOP='".$rowRecord['id']."' or dispatch_details.SOPNo='".$rowRecord['id']."'";
	$remarks_res=DB_query($remarks_sql,$db);
	$remarks_row=DB_fetch_array($remarks_res);
	//$remarks=$remarks_row['remarks'];
	if($rowRecord['cate']=='Urgent'){$urgent="<font size='2' style='color:#FFFF99'><b>Urgent</b></font>";}else{$urgent="Normal";}
	//echo $rowRecord['cate'];
	?>
<table width="100%" border="1"  cellpadding="4" cellspacing="0" style="border:#666666 solid 1px;">
	
	<tr style="background-color:#747474; color:#FFFFFF">
	<td width="354" style="width:300px;">SOP No:<?php echo $rowRecord['orderNo']; echo "&nbsp;&nbsp;&nbsp;&nbsp;";echo $urgent;?></td>
	<td width="300" style="width:310px;">Client:<?php echo  $s->GetFieldValue('clientmaster',"id='".$rowRecord['client']."'",'c_name');?></td>
	<td width="173">Order Date:<?php echo ConvertSQLDate($rowRecord['orderDate']);?></td>
	<td width="106">Delivery Date:<?php echo ConvertSQLDate($rowRecord['DeliveryDate']);?></td>
	</tr>
</table>
<table width="100%" border="1" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="9%" class="tabledata">SNo.</td>
	<td width="9%" class="tabledata">Code</td>
	<td width="16%" class="tabledata">Shade Name</td>
	<td width="24%" class="tabledata">Item Desc</td>
	<td width="10%" class="tabledata">Qty</td>
	<td width="20%" class="tabledata">Remarks</td>
	<td width="12%" class="tabledata">Delivery Status</td>
</tr>
<?php $detailsQuery = "select * from salesorder_details where masterId='".$rowRecord['id']."'";
$resNew = DB_query($detailsQuery,$db);
$i=1;while($rownew=DB_fetch_array($resNew)){
?>
	<tr class="row1">
	<td style=" padding-top:5px; padding-bottom:5px;"><?php echo  $i++;?></td>
	<td><?php echo  $s->GetFieldValue('item_master',"id='".$rownew['mst_itemid']."'",'StockID'); // echo $row['ItemCode'];?></td>
	<td><?php  echo  $s->GetFieldValue('item_master',"id='".$rownew['mst_itemid']."'",'item_name');	//echo $row['ItemCode'];?></td>
	<td><?php  echo  $s->GetFieldValue('item_master',"id='".$rownew['mst_itemid']."'",'item_desc');	//echo $row['ItemCode'];?></td>
	<td><?php  echo $rownew['Approved_qty'];?></td>
	<td><?php  echo $rownew['remarks'];?></td>
	<td><?php  if($rownew['pendingQty']=='0'){echo 'Delivered';}else{echo 'Pending';}?></td>
	</tr>
	<?php }    ?>
	</table>
	<?php }}}} ?>
</div>
</div>