<?php include('functions.php');
include('functionsgetdep.php');
include('includes/Class_Client.php');
include('Class_PickListEditor.php');
$date=date('Y-m-d');
$back_days=calculateBackDate($_SESSION['ACGRoleId']);
$back_date=date('Y-m-d', strtotime('-'.$back_days.' day', strtotime($date)));
?>
<style type="text/css">
<!--
.style2 {color: #74F0FA}
-->
</style>
 <?php
    $catid= $s->GetFieldValue('crm_picklist',"picklist_title='Leads' and status=1",'picklist_id');
   $table="crm_accounts";
   
    if($_REQUEST['Delete'])
 {
 	for($i=0;$i<count($_REQUEST['salesid']);$i++)
	{
	
	//-------Delete From salesOrder_deatils From Here-----------//
	
				$cond="masterId='".$_REQUEST['salesid'][$i]."'";
			    $row= $s->DeleteRecord('salesorder_details', $cond);
				
	//-------Delete From salesOrder_Comments End-----------//	
	
	
	//-------Delete From salesOrder_Comments From Here-----------//			
				
				$cond="sopno='".$_REQUEST['salesid'][$i]."'";
				$row=$s->DeleteRecord('salesorder_comments',$cond);
	//-------Delete From salesOrder_Comments End-----------//				
				
	//-------Delete From salesOrder_Comments From Here-----------//	
				
	 			$cond="id='".$_REQUEST['salesid'][$i]."'";
			    $row= $s->DeleteRecord('salesorder', $cond);
				
				
	//-------Delete From salesOrder_Comments End-----------//	
 	}
	 
  }
 
 ?>
 <script language="javascript" >
function LeadData()
{	

	myCheckboxArray = document.myform.elements["salesid[]"];
	var t=myCheckboxArray.length;
	
	
	if(document.getElementById('salesid').checked==true)
	{	
	
		if(t==undefined)
	
		document.getElementById('salesid_'+1).checked=true;		
			for (var i = 1; i <= t; i++)
	{
	document.getElementById('salesid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
		document.getElementById('salesid_'+1).checked=false;		
			
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('salesid_'+i).checked=false;	
	}
	}
	

	
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>VIEW SALES ORDER</h1>
<form action="" method="post" name="myform" >
<div class="centre" id="slidingDiv">
<table width="100%" cellpadding="3" cellspacing="0">
  <tr class="row1">
    <td class="tabledata1">Branch</td>
	<td colspan="3" class="tabledata1">
	<select name="site"  style="width:150px;">
	<option value="0">--All--</option>	
	<?php
	$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode IN ('".$_SESSION['AssignedBranch']."')";
	$SiteRes=DB_query($Sitesql,$db);
	while($SiteRow=DB_fetch_array($SiteRes))
	{?>
	<option value="<?php  echo $SiteRow['loccode'];?>" <?php  echo $_REQUEST['site']==$SiteRow['loccode']?'selected':''?> ><?php  echo $SiteRow['locationname'];?></option>
	<?php 
	}
	?>
	</select>
	</td>
	</tr>
	 <tr class="row1">
	<td class="tabledata1">From:</td>
	<td class="tabledata1">
	<input name="fromdate" value="<?php echo $_REQUEST['fromdate']; ?>" type="text" style="width:150px;"  class="datepicker"/></td>
	<td class="tabledata1">To:</td>
	<td class="tabledata1">
	<input name="todate" value="<?php echo $_REQUEST['todate']; ?>" type="text" style="width:150px;"  class="datepicker"/></td>
	</tr>
	<tr class="row1">
	<td colspan="4"><p align="center"><input name="Search" type="submit"  value="Search" class="search"/></p></td>
  </tr>
</table> 
</div>	
<p align="left">
<!--<a href="<?php echo "index.php?route=CRM/AddProcessTo".$rurl.'&action=AddNew'?>"><input type="button" name="Create" value="Add Process TO" class="search" /></a>-->

<?php if($_SESSION['frmAdd']==0) {?>
<input type="button" name="Create" value="Add New Order" alt="Not Allowed" title="Not Allowed" class="search" />
<?php }else{ ?>
<a href="<?php echo "index.php?route=CRM/saleordmanagment".$rurl.'&action=AddNew'?>"><input type="button" name="Create" value="Add New Order" class="search" /></a>
<?php }?>
	
	
<!--<a href="<?php echo "index.php?route=CRM/sale_order_approval".$rurl?>"><input type="button"  value="Sales Order Approval" style="background-color:#d1d4be; color:#006699; font-weight:800; padding-top:3px; padding-bottom:3px;" /></a>-->
	 
	  <input type="submit" name="Delete" value="Delete" class="search" />
	  
<?php if($_REQUEST['Search']){ ?>
		<a href="SalesExp.php"><input type="button" name="Export" value="Export" class="search" /></a>
<?php }?>
</p>

<div style="width:100%; overflow:auto;">
<table width="35%" border="0" align="left" cellpadding="2" cellspacing="0">
<tr>
	<td style="background-color:#B2B2B2;width:13px;border:#FF0000 solid 1px;"></td>
	<td>Pending</td>
	<td style="background-color:#FFFF33;width:13px;border:#FF0000 solid 1px;"></td>
	<td>Partially Approved</td>
	<td style="background-color:#c3fffb;width:13px;border:#FF0000 solid 1px;"></td>
	<td>Approved</td>
</tr>
</table>
<br /><br />
<table width="100%" border="1" cellpadding="3" cellspacing="0">
  <tr>
    <td class="tabledata"><input name="salesid" type="checkbox"   id="salesid" onclick="LeadData()"    />  </td>
	<td class="tabledata">Date</td>
	<td class="tabledata">Sales Order No</td>
	<td class="tabledata">Client Name</td>
	<td class="tabledata">PO No.</td>
	<td class="tabledata">PO Date.</td>
	<td class="tabledata">Order Type</td>
	<!--<td class="tabledata">Approved Qty.</td>-->
	<!--<td class="tabledata">Total Order Amt.</td>-->
	<td class="tabledata">Pending Payment</td>
	<td class="tabledata">Created By</td>	
	<td class="tabledata">Modify By</td>
	<td class="tabledata">Status</td>
	<td class="tabledata" colspan="2">Add Comment</td>
  </tr>
<?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'];

if($_REQUEST['Search'])
	{
	$todate=ConvertSQLDate($_REQUEST['todate']);
	$fromdate=ConvertSQLDate($_REQUEST['fromdate']);
	$site=$_REQUEST['site'];
	if($_REQUEST['todate']!='' && $_REQUEST['fromdate']!='' )
	{
	$filter[]="B.orderDate between '".$todate."' and '".$fromdate."'";
	}
	if($_REQUEST['site']!='0')
	{
	$filter[]="B.branch='".$site."'";
	}
	if($_REQUEST['site']=='0')
	{
	$GetSales= "select * from salesorder where createdBy in(".$user_ids.")   ";
	$getlead=DB_query("select * from salesorder where  createdBy in(".$user_ids.")  order by orderdate desc ",$db);
	}
	if($filter!='')
	{
	$filter1=implode(" and ",$filter);
	$x1= $s->GetFieldValue('www_users'," id='". $_SESSION['UserID'] ."' " ,'assigned_sites');
	$GetSales="select A.id,A.userid,A.assigned_sites ,B.* FROM salesorder B,www_users A  
	where   A.assigned_sites in($x1) and  B.CompanyID='".$_SESSION['ACGCompanyId']."' and  A.id= B.createdBy and $filter1 ORDER BY B.orderdate DESC";
	
	// $GetSales="select * from salesorder where  (createdBy in(".$lowerlist.") or  createdBy in($userlist) )and $filter1 order by orderdate desc ";
	$getlead=DB_query($GetSales,$db);
	}
}

else 
{
	$i=1;
	if($_SESSION['UserID']=='3' ||$_SESSION['UserID']=='10' || $_SESSION['UserID']=='13')
	{
	//echo "select * from salesorder where 1=1 and CompanyID='".$_SESSION['ACGCompanyId']."' ORDER BY orderNo DESC ";
	$getlead=DB_query("select * from salesorder where 1=1 and CompanyID='".$_SESSION['ACGCompanyId']."' ORDER BY id DESC ",$db);
	}
	else{
	// echo "<br>***********************************************************************************<br>";
	$x1= $s->GetFieldValue('www_users'," id='". $_SESSION['UserID'] ."' " ,'assigned_sites');
	$x2= GETFinalUserList($x1);
	$l=implode(",",$x2);
	if($l!='')
	{	
	$GetSales ="SELECT * FROM salesorder WHERE CompanyID='".$_SESSION['ACGCompanyId']."'  AND  (createdBy IN(".$l.") or createdBy in('".$_SESSION['UserID']."'))       ORDER BY orderNo DESC";
	}
	else{
	$GetSales="SELECT * FROM salesorder WHERE createdBy in('".$_SESSION['UserID']."')   ORDER BY orderNo DESC";
	}	  
	// $GetSales="select A.id,A.userid,A.assigned_sites ,B.* FROM salesorder B,www_users A	   		  where   A.assigned_sites in($x1) and  B.CompanyID='".$_SESSION['ACGCompanyId']."' and  A.id= B.createdBy ORDER BY B.orderdate DESC";
	//  $GetSales="select * from salesorder where createdBy in(".$lowerlist.") or  createdBy in($userlist) and  CompanyID='".$_SESSION['ACGCompanyId']."' order by orderdate desc";
	//echo "<br>***********************************************************************************<br>";
	$getlead=DB_query($GetSales,$db);
	}
}  
//echo "<br>==================<br>";
$x1= $s->GetFieldValue('www_users'," id='". $_SESSION['UserID'] ."' " ,'assigned_sites');
// echo "<br>==================<br>";
$_SESSION['Sales']=$GetSales;
$lop=0;
 if(DB_num_rows($getlead)>0)
{
	 while($row_quotes=DB_fetch_array($getlead))
	 {
	    $d=explode(',',$x1);
	  // $row_quotes['client'];
if($row_quotes['SalesOrderType']=='Tel'){
 	    $ClientName=$s->getField('clientmaster','id',$row_quotes['client'],'c_name');
} else{
	   //$ClientName=$Class_Client->GetClientByLedger($row_quotes['client']);
	   $ClientName=$s->getField('clientmaster','id',$row_quotes['client'],'c_name');
 	 }
 	  
	     $czone= $s->GetFieldValue('clientmaster',"id='".$row_quotes['client']."'  ",'Zone');
	   $createdby_zone= $s->GetFieldValue('www_users',"id='".$row_quotes['createdBy']."'  ",'assigned_sites');
	   if(in_array($czone,$d))
	  {
	$flag=0; $flag1=0;
	//echo "select * from salesorder_details where masterId='".$row_quotes['id']."'"."<br/>";
	$dsql=DB_query("select * from salesorder_details where masterId='".$row_quotes['id']."'",$db);
	$num5=DB_num_rows($dsql);
	while($rowsql=DB_fetch_array($dsql))
	{
	if($rowsql['ApprovalStatus']=='3')	
	{
	$flag=$flag+1;	
	$flag1=1;
    }
	else if($rowsql['ApprovalStatus']=='2')	
	{
	$flag1=1;	
    }
	}
	
	if($flag==$num5)
	{
	$status_salesorder='Approved';	
	}
	else if($flag1==1)
	{
	$status_salesorder='Partially Approved';	
	}
	else
	{
	$status_salesorder='Pending';		
	}
    
	 ?>
	 <?php 
	 //echo $status_salesorder."<br/>";
	 if($status_salesorder=='Approved') { 
	 $bgcolor="#c3fffb";  
	  } 
	  if($status_salesorder=='Partially Approved') { 
	 $bgcolor="#FFFF33";  
	  } 
	  if($status_salesorder=='Pending') { 
	 $bgcolor="#B2B2B2";  
	  } 
	  
	  
	  ?>
    <tr bgcolor="<?php echo $bgcolor; ?>" style="border:1px thin;">
    <td style="white-space:nowrap;" valign="top">
   <?php
 if($row_quotes['SalesOrderType']!='Tel'){
$SOURL="index.php?route=CRM/AddProcessToEdit&action=Edit&SID=".$row_quotes['id']."&QID=".$row_quotes['quoteNo'].$rurl;
$SO_VIEW="index.php?route=CRM/AddProcessToView&action=View&SID=".$row_quotes['id']."&QID=".$row_quotes['quoteNo'].$rurl;
}else{
$SOURL="index.php?route=CRM/viewsalesorderTelephonicEdit&action=Edit&SOrderId=".$row_quotes['id'].$rurl;
$SO_VIEW="index.php?route=CRM/ViewSalesOrdMgn_view&action=View&SOrderId=".$row_quotes['id'].$rurl;
}?>
   <input name="salesid[]" type="checkbox" id="salesid_<?php echo $lop+1; ?>"   value="<?php  echo $row_quotes['id']?>"  />
<?php if($row_quotes['orderDate'] >= $back_date){?>
<?php if($_SESSION['frmEdit']==0){?>
<img src="images/icon_edit.gif" width="10px" alt="Not Allowed" title="Not Allowed"  height="10" />
<?php }else{
?>
<a href="<?php echo $SOURL?>"><img src="images/icon_edit.gif" width="10px"  height="10" /></a>
<?php } ?>
<?php }else {?> <img src="images/icon_edit.gif" width="10px"  height="10" />
<?php } ?>
<?php if($row_quotes['orderDate'] >= $back_date){?>
<?php if($_SESSION['frmView']==0) {?>
<img src="images/search.gif" width="10px" alt="Not Allowed" title="Not Allowed"  height="10" />
<?php }else{?>
<a href="<?php echo $SO_VIEW?>" ><img src="images/search.gif" width="10px"  height="10" /></a>
<?php } ?>
<?php }else {?> <img src="images/search.gif" width="10px"  height="10" /><?php } ?>
   
  </td>
  <td valign="top" > <p>
    <?php  echo ConvertSQLDate($row_quotes['orderDate']); //echo "==="; echo $back_date;
	?>
</p>
    </td>
  <td valign="top" style="white-space:nowrap"></center>
    <?php  echo $row_quotes['orderNo']; ?></td>
<td valign="top" style="white-space:nowrap"><?php // echo $s->GetFieldValue('accountledger',"AccountsID='".$row_quotes['client']."'  ",'AccountName'); $row_lead['party']?></center>
<?php echo $ClientName;//."--".$czone ?>
	</td>
	<td valign="top"><?php  echo $row_quotes['PORefNo']?></td>
	<td valign="top"><?php  echo ConvertSQLDate($row_quotes['orderDate']);?></td>
	<td valign="top"><?php echo $row_quotes['SalesOrderType']; ?></td>
	<!--<td valign="top"><?php $amits='';
	$ApprQty=DB_query("select * from salesorder_details where masterId='".$row_quotes['id']."' and ApprovalStatus='Approved'",$db);
	while($ApprQtyRow=DB_fetch_array($ApprQty))
	{
		if($ApprQtyRow['pendingQty']=='')
		{
			//echo "Hii";
			$amits=$amits+$ApprQtyRow['Quantity'];
		}
		else
		{
			//echo "Hii";
			$amits=$amits+$ApprQtyRow['pendingQty'];
		}
	}
	if($amits=='')
	{ $amits=0;
	}
	echo $amits;
	 ?></td>-->
	<!--<td valign="top"><?php  //echo $row_quotes['grandTotal']?></td>-->
	<td valign="top" style="width:300px;"><?php  echo $row_quotes['PendingPayment']?></td>
	<td valign="top"><?php  echo $s->GetFieldValue('www_users',"id='".$row_quotes['createdBy']."'",'userid');  //.'<br>'.$row_quotes['createdBy'].'@'.$createdby_zone;?>
	
	</center></td>
	<td valign="top"><?php echo $s->GetFieldValue('www_users',"id='".$row_quotes['modifyBy']."'",'userid'); ?></td>
	<?php
	$comments=DB_query("select count(*) as total from salesorder_comments where sopno='".$row_quotes['id']."'",$db);
	$comments1=DB_fetch_array($comments);
	?>
	<?php if($status_salesorder=='Approved'){ ?>
	<td  valign="top"><?php echo $status_salesorder; ?></td>
	<?php } else { ?>
	<td valign="top"><?php echo $status_salesorder; ?></td>
	<?php } ?>
	<td valign="top"><?php if($row_quotes['orderDate'] >= $back_date){?><a href="<?php echo $SOURL?>#tabs-2"><?php echo $comments1['total']; ?>&nbsp; Comments</a><?php }else { ?>Add<?php } ?></td>
   </tr>
   <?php   }
   $lop++; }  }
   else
   {?>
   	<tr>
   		<td colspan="14"><font color="#FF0000"><?php echo "No Records Found"; ?></font></td>
	</tr>
  <?php }?>
</table>
	</div>
</form>
</div>
 </div>