<?php extract($_POST);
$PageSecurity = 1;
 
global $db;?>


<?php

if(isset($_POST['Save']))
{
for($i=0;$i<count($_REQUEST['mstitemid']);$i++)
{
       $totalqty=$_REQUEST['mrnqty'][$i]+$_REQUEST['shortclose'][$i];
        if($_REQUEST['poqty'][$i]==$totalqty)
		{$status="Receive"; }
		 else {$status='Pending';}
		 $update=DB_query("update dbl_enquiry_details set
		 close_qty='".$_REQUEST['shortclose'][$i]."' ,close_enquiry_status='".$status."' 
		  where mst_itemid='".$_REQUEST['mstitemid'][$i]."' 
		 and parent_id='".$_REQUEST['mrnid']."'",$db);

}

//echo "select * from purchorderdetails where orderno='".$_REQUEST['orderno']."'";

$row=0;
$check=DB_query("select * from dbl_enquiry_details where id='".$_REQUEST['mrnid']."'",$db);
$numrow=DB_num_rows($check);$row=0;
while($num=DB_fetch_array($check))
{
if($num['close_enquiry_status']=='Receive')
{$row=$row+1;}}


if($numrow==$row)
{
 $update1=DB_query("update dbl_enquiry_form set close_status=1,close_by='".$_SESSION['UserID']."'  where  id='".$_REQUEST['mrnid']."'",$db);
}

//echo "<script>location.href='index.php?route=DealerDistrbutions/Transaction/enquiry_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";




}








?>




 
<?php 

  $sql_po = "Select * from dbl_enquiry_form where id='".$_GET['mrnid']."'";
 //echo $sql_po ;
	$result=DB_query($sql_po,$db);
	$myrow=DB_fetch_array($result);
	//echo "<pre>";
	//print_r($myrow);
	
	$dealer1=$myrow['dealer'];
	$form_type=$myrow['form_type'];
	$form_no=$myrow['form_no'];
	$customer=$s->getField('clientmaster','id',$myrow['customer'],'c_name');
	$customercode=$s->getField('clientmaster','id',$myrow['customer'],'client_code');
	$dealer=$s->getField('dbl_dealer','id',$myrow['dealer'],'dealer_name');
	$zone=$s->getField('zone_master','id',$myrow['zone'],'ZoneTitle');
	$subzone=$s->getField('subzone_master','id',$myrow['subzone'],'subzone');
	$engineer=$s->getField('dbl_engineer_master','id',$myrow['engineer'],'engineer_name');
	$zonecode=$s->getField('zone_master','id',$myrow['zone'],'zonecode');
	$subzonecode=$s->getField('subzone_master','id',$myrow['subzone'],'subzonecode');
	$engineercode=$s->getField('dbl_engineer_master','id',$myrow['engineer'],'engineer_code');
		

 ?>
 

 <div align="center"><h3>Enquiry Close</h3></div>	  	  

 <table width="100%" border="1" cellpadding="2" cellspacing="1" align="center">
  <tr>
    <td width="56%" ><table width="100%" border="0" cellspacing="1" cellpadding="2">
      <tr>
        <th width="35%"><strong>Form Type</strong></th>
        <td width="65%"><?php echo $form_type;?>		</td>
      </tr>
      <tr>
        <th><strong>Customer Name</strong></th>
        <td><?php echo $customer;?>     </td>
      </tr>
      <tr>
        <th>Under Associate</th>
        <td><?php echo $dealer;?></td>
      </tr>
      <tr>
        <th><strong>Area Name</strong></th>
        <td><?php echo $zone;?></td>
      </tr>
      <tr>
        <th nowrap="nowrap">Sub Area Name</th>
        <td><?php echo $subzone;?> </td>
      </tr>
      <tr>
        <th>Engineer Name</th>
		 <td><?php echo $engineer;?> </td>
      </tr>
    </table></td>
    <td width="44%" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="2">
  
	  <tr>
        <th nowrap="nowrap">Form No</th>
        <td><?php echo $form_no;?>  </td>
      </tr>
      <tr>
        <th nowrap="nowrap">Customer Code</th>
        <td><?php echo $customercode;?>  </td>
      </tr>
        <tr>
        <th><strong>Area Code</strong></th>
        <td><?php echo $zonecode;?>      </td>
      </tr>
      <tr>
        <th>Sub Area Code</th>
        <td><?php echo $subzonecode;?>   </td>
      </tr>
      <tr>
        <th scope="row">Engineer Code</th>
        <td><?php echo $engineercode;?>    </td>
      </tr>
    </table></td>
  </tr>
  </table>
 
 
 
 
 
 
<form action="" method="post">
<div align="center">
<table width="100%" border="0" cellpadding="5">
<tr>
<th><strong>Item Code</strong></th>
<th><strong>Item Name</strong></th>
<th><strong>Unit</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Rate</strong></th>
<th><strong>Order Qty</strong></th>
<th><strong>Max Close Qty</strong></th>
<th><strong>Short close Qty</strong></th>
<input type="hidden" name="orderno" value="<?php echo $_REQUEST['mrnid']; ?>" />
</tr>
<?php  
$sql="SELECT * FROM dbl_enquiry_details WHERE parent_id='".$_REQUEST['mrnid']."' and  close_enquiry_status='Pending' and status=0";
//echo $sql;
$result=DB_query($sql,$db);
$i=1;
while($myrow=DB_fetch_array($result))
{
//echo "<pre>";
//print_r($myrow);
if($i%2==0)
{$bg="#CCCCCC";}
else
$bg="#C8BECB";
?>
    <tr bgcolor="<?php echo $bg;?>">
       <td><?php echo $s->getField('item_master','id',$myrow['mst_itemid'],'StockID');?>
	   <input type="hidden" name="mstitemid[]" value="<?php echo $myrow['mst_itemid'];?>" />
	    </td>
    <td><?php echo $s->getField('item_master','id',$myrow['mst_itemid'],'item_name');?> </td>
      <td><?php
	   $itemid=$s->getField('item_master','id',$myrow['mst_itemid'],'mst_unitid'); 
	  $unitname=$s->getField('unitsofmeasure','unitid',$itemid,'unitname');
	  echo $unitname;?>
        </td>
    
    <td><?php echo $myrow['pendingqty'];?> <input type="hidden" name="poqty[]" value="<?php echo $myrow['pendingqty'];?>" /></td>
		
		
    <td><?php echo $myrow['price'];?></td>
	
	<?php  
	
  $check_last_mrn="Select sum(order_qty),req_qty from dbl_process_sale_order_details  inner join dbl_process_sale_order on 
dbl_process_sale_order.id=dbl_process_sale_order_details.parentid   where dbl_process_sale_order.enquiry_no= '".$_GET['mrnid']."'
	  and dbl_process_sale_order.dealer='".$dealer1."'
	   and dbl_process_sale_order_details.mst_itemid='".$myrow['mst_itemid']."' and dbl_process_sale_order_details.status=0";
	 
	$last_mrn_res=DB_query($check_last_mrn,$db);
	$last_mrn_row=DB_fetch_array($last_mrn_res);
	$shortqty=DB_query("select close_qty from dbl_enquiry_details where 
	parent_id='".$_GET['mrnid']."' and mst_itemid='".$myrow['mst_itemid']."'",$db);
	$short_qty=DB_fetch_array($shortqty);
	if($short_qty['close_qty']=='') $closeqty1=0; else $closeqty1=$short_qty['close_qty'];
	//echo $closeqty1;
	if($last_mrn_row['req_qty']!='' && $last_mrn_row['sum(order_qty)']!='')
	{
	$pending_qty=$last_mrn_row['req_qty']-($last_mrn_row['sum(order_qty)']+$closeqty1);
	$mrnqty=$last_mrn_row['sum(order_qty)'];
	}
	else{$pending_qty=$myrow['pendingqty']-$closeqty1;
	$mrnqty=0;
	
	}	
	?>
	<td><?php echo $mrnqty;?><input type="hidden" name="mrnqty[]" value="<?php echo $mrnqty;?>" />     </td>
	<td><input type="hidden" name="shortclose1[]" id="shortclode1_<?php echo $i; ?>"   value="<?php echo $pending_qty; ?>"  />
	<?php echo $pending_qty; ?>
	</td>
	<td>
	<input type="text" name="shortclose[]" id="shortclode_<?php echo $i; ?>"   value="<?php echo $pending_qty; ?>"  onkeyup="caltotal(<?php echo $i; ?>)"/> </td>

      
   

      <?php 
	  
	  
$i++;
}?>
     
    </tr>

	
	
  </table>
  
  
  
  </div>
  <div>
  <table width="100%">
  <tr>
<th><strong>Remarks</strong></th>
<td><textarea name="reason" style="width:100%" ></textarea></td></tr>	
</table>
  </div>
  <p align="center"><input type="submit" name="Save" value="Save" /></p>
  </form>

 
<script>
function caltotal(i)
{
var closeqty=parseFloat(document.getElementById("shortclode_"+i).value);
var closeqty1=parseFloat(document.getElementById("shortclode1_"+i).value);
if(closeqty>closeqty1)
{
alert("Short Qty Should be less or equal then Max qty");
document.getElementById("shortclode_"+i).value=closeqty1;
return false;
}
return true;

}

</script>
