<?php 
include('functions.php');
$s=new db_query();
global $db;
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');		 

$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$lowerlist=  UserDeptList($LowerDeptList);
$lowerlist[]=$_SESSION['UserID'];
 $lowerdata=$lowerlist;
 
$userdata=$_SESSION['UserID'];
if(count($lowerlist)>0)
$lowerlist=implode(",",$lowerlist) ;
else
$lowerlist="'".$_SESSION['UserID']."'";
$key=array_search($_SESSION['UserID'],$lowerdata);
  		 if($key!==false)
		 unset($lowerdata[$key]);
		 $lowerdata=implode(",",$lowerdata);
		 
$user_ids1=$lowerlist;
$id1=array();
 $user_ids1;
$user_id = $_SESSION['ACG_DashBoard_User'];
//echo $userdata;
if($userdata==44)
{
$lowerdata=$lowerlist;
}
//echo $lowerdata;
?>
<script type="text/javascript" language="javascript">
function checkqty()
{
	var courter=document.getElementById("count1").value;
	var courter1=courter-1;
	for(var i=1;i<=courter1;i++)
	{
	var totlqty=parseFloat(document.getElementById("qty_"+i).value);
	var Appqty=parseFloat(document.getElementById("app_qty_"+i).value);
	if(Appqty>totlqty)
	{
	alert("Approval Qty is Not Greater Than Total Qty");
	document.getElementById("app_qty_"+i).value=0;
	}
	}
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>SALE ORDER APPROVAL</h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" cellpadding="3" cellspacing="0">
	<tr class="row1">
	<td>Order Type</td>
		<td><select style="width:150px;" name="sales_ordertype">
		<option value="Tel,Process"<?php echo($_REQUEST['sales_ordertype']=='Tel,Process'?'selected':''); ?>>Both</option>
		<option value="Process"<?php echo($_REQUEST['sales_ordertype']=='Process'?'selected':''); ?>>Process</option>
		<option value="Tel"<?php echo($_REQUEST['sales_ordertype']=='Tel'?'selected':''); ?>>Direct</option>
		</select>
		</td>
	<td>Branch</td>
		<td><select style="width:150px;" name="Site">
			<option value="0">--Select--</option>
			<?php
			$SiteName=DB_query("Select * from locations where mst_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>
		</tr>
		<tr class="row1">
	<td>Users</td>
	<td>
<?php //$UserSQL=DB_query("select * from www_users where id in($lowerdata)",$db); 
$UserSQL=DB_query("select * from www_users ",$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>
	<td>From Date</td>
	<td><input type="text" name="fdate" style="width:150px;"  class="datepicker" value="<?php echo $_REQUEST['fdate']?>"/></td>
	</tr>
	<tr class="row1">
	<td>To Date</td>
	<td colspan="3"><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>
<br />
<?php
//echo $user_ids;
//echo "<pre>"; print_r($_SESSION);

if($_REQUEST['Save'])
{ 
	
	for($i=1;$i<=count($_REQUEST['SOPNo']);$i++)
	{
	if(0==($i-1)){
		$getApp= "select ApprId from salesorder_details_approv order by id DESC LIMIT 0,1";
		$getApp1=DB_query($getApp,$db);
		$getApprow=DB_fetch_array($getApp1);
		if(empty($getApprow['ApprId'])){
			$ApprId=1;
		}else{
			$ApprId=$getApprow['ApprId']+1;
		}
	}else if($_REQUEST['apporderid'][$i]==$_REQUEST['apporderid'][$i-1]){
		$getApp= "select ApprId from salesorder_details_approv order by id DESC LIMIT 0,1";
		$getApp1=DB_query($getApp,$db);
		$getApprow=DB_fetch_array($getApp1);
		if(empty($getApprow['ApprId'])){
			$ApprId=1;
		}else{
			$ApprId=$getApprow['ApprId'];
		}
	}
		else
		{
		$getApp= "select ApprId from salesorder_details_approv order by id DESC LIMIT 0,1";
		$getApp1=DB_query($getApp,$db);
		$getApprow=DB_fetch_array($getApp1);
		if(empty($getApprow['ApprId'])){
			$ApprId=1;
		}
		else
		{
			$ApprId=$getApprow['ApprId']+1;
		}
	}
	
	if($_REQUEST['Approval'][$i]==1){
	if($_REQUEST['qty'][$i]==$_REQUEST['app_qty'][$i])
	{
	$sts='3';
	}
	else
	{
	$sts='2';
	}
	if($_REQUEST['app_qty1'][$i]=='')
	{
	$Totalqty=$_REQUEST['app_qty'][$i];
	}
	else
	{
	$Totalqty=$_REQUEST['app_qty'][$i]+$_REQUEST['app_qty1'][$i];
	}
	$data['ApprovalStatus']=$sts;
	$data['SalesCategory']=$_REQUEST['SalesCategory'][$i];
	$data['Approved_qty']=$Totalqty;
	$cond="id='".$_REQUEST['SOPNo'][$i]."'";
	//echo "<pre>";
	//print_r($data);die;
	$row= $s->RecordEdit('salesorder_details',$data,$cond);
	
	$data_app['OrderId']=$_REQUEST['apporderid'][$i];
	$data_app['OrderDetailId']=$_REQUEST['SOPNo'][$i];
	$data_app['ApprId']=$ApprId;
	$data_app['ItemId']=$_REQUEST['itm_approv'][$i];
	$data_app['Approved_qty']=$_REQUEST['app_qty'][$i];
	$data_app['approv_date']=date("Y-m-d");
	$data_app['approv_by']=$_SESSION['UserID'];
	$insertsql= $s->insertRecord('salesorder_details_approv', $data_app);
	$insertResult = DB_query($insertsql,$db);
	}
	}
	//exit;
	$s->pageLocation($target."&daction=1");
}
?>
<?php if($_REQUEST['daction']==1){?>
<div class="success"><?php echo "Record Approved Successfully";?></div>
	<?php } ?>
<div style="width:100%; margin-left:auto; margin-right:auto;">
<form method="post" action="">
<?php
if($_REQUEST['Go'])
{
	$todate=ConvertSQLDate($_REQUEST['todate']);
	$fromdate=ConvertSQLDate($_REQUEST['fdate']);
	$Site=$_REQUEST['Site'];
	$SalesOrderType=$_REQUEST['sales_ordertype'];
	if($_REQUEST['todate']!='' && $_REQUEST['fdate']!='' )
	{
	$filter[]="orderDate between '".$fromdate."' and '".$todate."'";
	}
	if($_REQUEST['Site']!='0')
	{
	$filter[]="branch='".$Site."'";
	}
	if($_REQUEST['user']!='0')
	{
	$filter[]="createdBy='".$_REQUEST['user']."'";
	}
	if($SalesOrderType=='Tel,Process')
	{
	$filter[]="(SalesOrderType='Direct' or SalesOrderType='Process')";
	}
	if($SalesOrderType=='Tel')
	{
	$filter[]="SalesOrderType='Direct'";
	}
	if($SalesOrderType=='Process')
	{
	$filter[]="SalesOrderType='Process'";
	}
	if($filter!='')
	{
	$filter1=implode(" and ",$filter);
	
		if($lowerdata!='')
		{
			if($_SESSION['UserType']=='SuperAdmin')
                        {// Approve All Records //
			$getlead= "select * from salesorder where createdby in($lowerlist) and  CompanyId='".$_SESSION['ACGCompanyId']."' 
			and  $filter1 and dealerid=0 order by id desc " ;
                        
                        $getlead2= "select * from salesorder where createdby in($lowerlist) and  CompanyId='".$_SESSION['ACGCompanyId']."' 
			and  $filter1 and dealerid!='0' order by id desc " ;
                        }
			else
                        {
			$getlead= "select * from salesorder where createdby in($lowerlist) and CompanyId='".$_SESSION['ACGCompanyId']."' and  $filter1 and dealerid=0 order by id desc " ;
                        
                        $getlead2= "select * from salesorder where createdby in($lowerlist) and CompanyId='".$_SESSION['ACGCompanyId']."' and  $filter1 and dealerid!='0' order by id desc " ;
                        }
		}
	}
		if($SalesOrderType=='0')
		{
			if($_SESSION['UserType']=='SuperAdmin') // Approve All Records //
                        {
			$getlead= "select * from salesorder where createdby in($lowerlist) and CompanyId='".$_SESSION['ACGCompanyId']."' and dealerid=0 order by id desc";
                        
                        $getlead2= "select * from salesorder where createdby in($lowerlist) and CompanyId='".$_SESSION['ACGCompanyId']."' and dealerid!=0 order by id desc";
                        }
                        else
                        {
			$getlead= "select * from salesorder where createdby in($lowerlist) and CompanyId='".$_SESSION['ACGCompanyId']."' and dealerid=0 order by id desc";
                        
                        $getlead2= "select * from salesorder where createdby in($lowerlist) and CompanyId='".$_SESSION['ACGCompanyId']."' and dealerid=!0 order by id desc";
                        }
		}
	//echo $getlead;
	$getlead1=DB_query($getlead,$db);
        
        //echo $getlead2;
        $getlead2=DB_query($getlead2,$db);
	$i=1;
	$p=1;
	
	?>
    <fieldset><legend style="font-size:12px; font-family:Arial; color:#000000; font-weight:bold;">Client Orders</legend>
		<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse; border:#999999 solid 1px;">
		<?php if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='') {?>
		<tr>
		<td colspan="10" style="color:#006699;"><strong>UnApproved SalesOrder List From <?php echo ConvertSQLDate($fromdate);?> TO <?=ConvertSQLDate($todate)?></strong></td>
		</tr>
		
		<tr>
		<td colspan="10" style="height:5px;"></td>
		</tr>
		<?php } ?>
		<?php 
		if(DB_num_rows($getlead1)>0){
		while($TelRes=DB_fetch_array($getlead1))
		{
		//echo $TelRes['id'];
	?>
	<?php 
		 //echo "select * from salesorder_details where masterId='".$TelRes['id']."' and ApprovalStatus='1' or ApprovalStatus='2'";
	$sqlqry=DB_query("select * from salesorder_details where masterId='".$TelRes['id']."' and (ApprovalStatus='1' || ApprovalStatus='2')",$db);
		$n= DB_num_rows($sqlqry);
	 //  $n= count($sqlqry);
	  if($n>0)
	  {
	  $k=1;
	//echo "<br>=====================<br>";
		$q="Select S1.cate As catedata,S1.createdBy,S1.DeliveryDate,S1.PendingPayment, S2.* from salesorder S1,salesorder_details  S2 where S2.masterId ='".$TelRes['id']."' and (S2.ApprovalStatus='1' || S2.ApprovalStatus='2') and S1.id=S2.masterId";
	   $itemSql=DB_query($q ,$db); 
	 //echo DB_num_rows($itemSql);
	   ?>
	 <tr align="center" bgcolor="#d4d4d4">
	  <td colspan="4" style="font-size:12px;"><strong>Client Name:&nbsp;
	  <?php
	//echo  $TelRes['SalesOrderType'];
	 if($TelRes['SalesOrderType']=='Tel'){
	 echo  $ClientName=$s->getField('clientmaster','id',$TelRes['client'],'c_name');
	 }else{
	 
	  
	   echo  $ClientName=$s->GetFieldValue('clientmaster',"id='".$TelRes['client']."'",'c_name');
	
	  }?></strong>
	  
	  </td>
	  
	  
	   <td  colspan="3"  style="font-size:12px;"><strong>TO No & Date :</strong>&nbsp;&nbsp;&nbsp;<a href="index.php?route=CRM/TelephonicEdit&action=Edit&SOrderId=<?php echo $TelRes['id'];?>&modid=12&frmid=322" target="_blank"><font size="2"><?php echo '<b>'.$TelRes['orderNo'].'</b>';?></font></a><strong><?php echo "  ".ConvertSQLDate($TelRes['orderDate']); ?></strong></td>
			<td colspan="4"> <strong>Created By :</strong>&nbsp;&nbsp;<strong><?php  echo  ucfirst($s->GetFieldValue('www_users',"id='".$TelRes['createdBy']."'" ,'realname')); ?></strong></td>
	  </tr> 
	  <!--<tr><td colspan="10"><h3>Item Details</h3></td>
	  </tr>-->
	  <tr>
		<td class="tabledata">S. No.</td>
		<td class="tabledata">Item/Shade Name</td>
		 <td class="tabledata">Item Desc</td>	
		<td class="tabledata">QTY</td>
		<td class="tabledata">Rate</td>
		<td class="tabledata">Delivery Date</td>
		<td class="tabledata">Pending Payment</td>
		<td class="tabledata">Approve Qty</td>
		<td class="tabledata">Category</td>
		<td class="tabledata">Action</td>		
	  </tr>
	  <?php
	
	  
	while($itemRow=DB_fetch_array($itemSql))
	{
		echo $itemRow['cate'];
	 ?>
	<tr bgcolor="#c1c1c1">
		<td><?php echo $k;?></td>
		<td>
			<?php
		echo 	$Itemname=$s->getField('item_master','id',$itemRow['mst_itemid'],'item_name'); 
			?> 
			<input type="hidden" name="itm_approv[<?php echo $p?>]" value="<?php echo $itemRow['mst_itemid'];?>" />
			 	</td>
			<td><?php
		echo 	$Itemname=$s->getField('item_master','id',$itemRow['mst_itemid'],'item_desc'); 
			?> </td>
		<td> <?php if($itemRow['Approved_qty']=='')	
				{
					$Finalqty=$itemRow['Quantity'];
				}
				else
				{
				$Finalqty=$itemRow['Quantity']-$itemRow['Approved_qty'];
				}
		
		echo $Finalqty; ?>
		<input type="hidden" name="qty[<?php echo $p?>]" id="qty_<?php echo $p;?>" value="<?php echo $Finalqty; ?>" />
		</td>
		<td> <?php echo 	$itemRow['Rate']; ?></td>
		<td> 
			<?php  echo ConvertSQLDate($itemRow['DeliveryDate']) ?>
			</td>
			<td> 
			<?php  echo $itemRow['PendingPayment'] ?>
			</td>
			 <td>
			 <input type="text" name="app_qty[<?php echo $p?>]" id="app_qty_<?php echo $p;?>" onblur="checkqty()" />
			 <input type="hidden" name="app_qty1[<?php echo $p?>]" id="app_qty1_<?php echo $p;?>" value="<?php echo $itemRow['Approved_qty'] ?>" />
			 </td>
		<td><select style="width:107px;" name="SalesCategory[<?php echo $k ?>]">
			<option value="Normal" <?php if($itemRow['catedata']=="Normal"){ ?>  selected="selected" <?php } ?> >Normal</option>
			<option value="Urgent" <?php if($itemRow['catedata']=="Urgent"){ ?>  selected="selected" <?php } ?> >Urgent</option>
		</select></td>
		 <td>
		 	<input type="hidden" name="apporderid[<?php echo $p;?>]" id="orderid" value="<?php echo $TelRes['id'];?>">
			<input type="hidden" name="SOPNo[<?php echo $p ?>]" value="<?php echo $itemRow['id']?>">
			<input type="checkbox" name="Approval[<?php echo $p?>]" id="Approval_<?php echo $p;?>" value="1">
			</td>
		</tr>
<?php $p++; ;$k++; } ?>
<tr>
		<td colspan="10"><?php echo "&nbsp;"; ?></td>
</tr>
<?php $i++; } ?>
		<input type="hidden" name="count" id="count" value="<?php echo $p;?>">
<?php } ?>
	  <input type="hidden" name="count1" id="count1" value="<?php echo $p;?>">
<?php   } else{ ?>
	<tr><td clospan="10"><font color="#FF0000">No Records Found</font></td></tr>
<?php } ?>
		</table>
    
    </fieldset>
    
    
    
    
    
    
    
    
    
    
    
    <fieldset><legend style="font-size:12px; font-family:Arial; color:#000000; font-weight:bold;">Associate Orders</legend> 
    <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse; border:#999999 solid 1px;">
		<?php if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='') {?>
		<tr>
		<td colspan="10" style="color:#006699;"><strong>UnApproved SalesOrder List From <?php echo ConvertSQLDate($fromdate);?> TO <?=ConvertSQLDate($todate)?></strong></td>
		</tr>
		
		<tr>
		<td colspan="10" style="height:5px;"></td>
		</tr>
		<?php } ?>
		<?php 
		if(DB_num_rows($getlead2)>0){
		while($TelRes=DB_fetch_array($getlead2))
		{
		//echo $TelRes['id'];
	?>
	<?php 
		 //echo "select * from salesorder_details where masterId='".$TelRes['id']."' and ApprovalStatus='1' or ApprovalStatus='2'";
	$sqlqry=DB_query("select * from salesorder_details where masterId='".$TelRes['id']."' and (ApprovalStatus='1' || ApprovalStatus='2')",$db);
		$n= DB_num_rows($sqlqry);
	 //  $n= count($sqlqry);
	  if($n>0)
	  {
	  $k=1;
	//echo "<br>=====================<br>";
		$q="Select S1.cate As catedata,S1.createdBy,S1.DeliveryDate,S1.PendingPayment, S2.* from salesorder S1,salesorder_details  S2 where S2.masterId ='".$TelRes['id']."' and (S2.ApprovalStatus='1' || S2.ApprovalStatus='2') and S1.id=S2.masterId";
	   $itemSql=DB_query($q ,$db); 
	 //echo DB_num_rows($itemSql);
	   ?>
	 <tr align="center" bgcolor="#d4d4d4">
	  <td colspan="4" style="font-size:12px;"><strong>Client Name:&nbsp;
	  <?php
	//echo  $TelRes['SalesOrderType'];
	 if($TelRes['SalesOrderType']=='Tel'){
	 echo  $ClientName=$s->getField('clientmaster','id',$TelRes['client'],'c_name');
	 }else{
	 
	  
	   echo  $ClientName=$s->GetFieldValue('clientmaster',"id='".$TelRes['client']."'",'c_name');
	
	  }?></strong>
	  
	  </td>
	  
	  
	   <td  colspan="3"  style="font-size:12px;"><strong>TO No & Date :</strong>&nbsp;&nbsp;&nbsp;<a href="index.php?route=CRM/TelephonicEdit&action=Edit&SOrderId=<?php echo $TelRes['id'];?>&modid=12&frmid=322" target="_blank"><font size="2"><?php echo '<b>'.$TelRes['orderNo'].'</b>';?></font></a><strong><?php echo "  ".ConvertSQLDate($TelRes['orderDate']); ?></strong></td>
			<td colspan="4"> <strong>Created By :</strong>&nbsp;&nbsp;<strong><?php  echo  ucfirst($s->GetFieldValue('www_users',"id='".$TelRes['createdBy']."'" ,'realname')); ?></strong></td>
	  </tr> 
	  <!--<tr><td colspan="10"><h3>Item Details</h3></td>
	  </tr>-->
	  <tr>
		<td class="tabledata">S. No.</td>
		<td class="tabledata">Item/Shade Name</td>
		 <td class="tabledata">Item Desc</td>	
		<td class="tabledata">QTY</td>
		<td class="tabledata">Rate</td>
		<td class="tabledata">Delivery Date</td>
		<td class="tabledata">Pending Payment</td>
		<td class="tabledata">Approve Qty</td>
		<td class="tabledata">Category</td>
		<td class="tabledata">Action</td>		
	  </tr>
	  <?php
	
	  
	while($itemRow=DB_fetch_array($itemSql))
	{
		echo $itemRow['cate'];
	 ?>
	<tr bgcolor="#c1c1c1">
		<td><?php echo $k;?></td>
		<td>
			<?php
		echo 	$Itemname=$s->getField('item_master','id',$itemRow['mst_itemid'],'item_name'); 
			?> 
			<input type="hidden" name="itm_approv[<?php echo $p?>]" value="<?php echo $itemRow['mst_itemid'];?>" />
			 	</td>
			<td><?php
		echo 	$Itemname=$s->getField('item_master','id',$itemRow['mst_itemid'],'item_desc'); 
			?> </td>
		<td> <?php if($itemRow['Approved_qty']=='')	
				{
					$Finalqty=$itemRow['Quantity'];
				}
				else
				{
				$Finalqty=$itemRow['Quantity']-$itemRow['Approved_qty'];
				}
		
		echo $Finalqty; ?>
		<input type="hidden" name="qty[<?php echo $p?>]" id="qty_<?php echo $p;?>" value="<?php echo $Finalqty; ?>" />
		</td>
		<td> <?php echo 	$itemRow['Rate']; ?></td>
		<td> 
			<?php  echo ConvertSQLDate($itemRow['DeliveryDate']) ?>
			</td>
			<td> 
			<?php  echo $itemRow['PendingPayment'] ?>
			</td>
			 <td>
			 <input type="text" name="app_qty[<?php echo $p?>]" id="app_qty_<?php echo $p;?>" onblur="checkqty()" />
			 <input type="hidden" name="app_qty1[<?php echo $p?>]" id="app_qty1_<?php echo $p;?>" value="<?php echo $itemRow['Approved_qty'] ?>" />
			 </td>
		<td><select style="width:107px;" name="SalesCategory[<?php echo $k ?>]">
			<option value="Normal" <?php if($itemRow['catedata']=="Normal"){ ?>  selected="selected" <?php } ?> >Normal</option>
			<option value="Urgent" <?php if($itemRow['catedata']=="Urgent"){ ?>  selected="selected" <?php } ?> >Urgent</option>
		</select></td>
		 <td>
		 	<input type="hidden" name="apporderid[<?php echo $p;?>]" id="orderid" value="<?php echo $TelRes['id'];?>">
			<input type="hidden" name="SOPNo[<?php echo $p ?>]" value="<?php echo $itemRow['id']?>">
			<input type="checkbox" name="Approval[<?php echo $p?>]" id="Approval_<?php echo $p;?>" value="1">
			</td>
		</tr>
<?php $p++; ;$k++; } ?>
<tr>
		<td colspan="10"><?php echo "&nbsp;"; ?></td>
</tr>
<?php $i++; } ?>
		<input type="hidden" name="count" id="count" value="<?php echo $p;?>">
<?php } ?>
	  <input type="hidden" name="count1" id="count1" value="<?php echo $p;?>">
<?php   } else{ ?>
	<tr><td clospan="10"><font color="#FF0000">No Records Found</font></td></tr>
<?php } ?>
		</table>
    </fieldset>
<?php	 if($k>0){?>
	<p align="center"><input type="submit" name="Save" value="Save" class="search" ></p>
<?php	} }?>
</form>
</div>
</div>
</div>
<!--main div Close -->