<?php 
ob_start();
session_name('CAKEPHP'); session_start();
//echo $rurl;
 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);


if($_SESSION['UserType']=='SuperAdmin')
{
	$sqlchkprhead=DB_query("select * from dept_indet_approval where ApprovalType='POApproval'",$db);
}	
else
{
	$sqlchkprhead=DB_query("select * from dept_indet_approval where ApprovalType='POApproval' and find_in_set('".$_SESSION['UserID']."',userid)",$db);
}

$Numrows=DB_num_rows($sqlchkprhead);


if($_REQUEST['Save']){
 $count=0;
	$count1=0;
	for($i=0;$i<count($_REQUEST['sc_qty']);$i++)
	{
		if($_REQUEST['sc_qty'][$i]!=''){
                
		$data['so_id']=$_REQUEST['hid_poid'][$i];
		$data['item_id'] = $_REQUEST['hid_itemid'][$i];
                $data['so_qty'] = $_REQUEST['hid_poqty'][$i];
                $data['short_close'] = $_REQUEST['sc_qty'][$i]; 
                $data['short_date'] = FormatDateForSQL($_REQUEST['sc_date'][$i]);
                
		$s->RecordInsert('so_short_close',$data);
             if($_REQUEST['hidpend_qty'][$i]==$_REQUEST['sc_qty'][$i]){
              $dataarr['pendingForDispatch']='0'; 
                 }else{
                $dataarr['pendingForDispatch']='1'; 
                }
	        $con = "id = '".$_REQUEST['hid_podeid'][$i]."'";
	        $s->RecordEdit('salesorder_details',$dataarr,$con);
                             
                 $_SESSION['success_msg']="Save Successfully";
	$s->pageLocation("index.php?route=CRM/short_close_so_qty".$rurl);
               
		}else{
             $_SESSION['success_msg']="Sorry! Failed To Save";
             $s->pageLocation("index.php?route=CRM/short_close_so_qty".$rurl);
        }
               
	}
        
	
}
//$array=array('IO'=>'Import Order','DO'=>'Domestic Order','SO'=>'Service Order','AO'=>'Asset Order');

 ?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Short Close</h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1"><strong>Branch</strong></td>
	<td><select style="width:150px;" name="Site" id="Site">
			<?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>
	<td class="tabledata1"><strong>Client</strong></td>
	<td><?php
	  
	  $sql="Select id,c_name from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' order by c_name ASC";
			$result = DB_query($sql, $db);
			?>
            <select name="client" id="client" style="width:150px;">
              <option value="">Select</option>
              <?php
			while($myrow = DB_fetch_array($result))
			{ 
			$p='';
			if($myrow['id']==$_REQUEST['client'])
			$p="selected";
			?>
              <option  value="<?php echo $myrow['id']; ?>"<?php echo $p;?>><?php echo  $myrow['c_name']; ?></option>
              <?php }
			?>
            </select></td>
	<td class="tabledata1"><strong>Po No</strong></td>
	<td><?php
	  
	  $sql=DB_query("select id,orderNo from salesorder", $db);
			?>
            <select name="so_no" id="so_no" style="width:150px;">
              <option value="">Select</option>
              <?php
			while($myrow = DB_fetch_array($sql))
			{ 
			$p='';
			if($myrow['id']==$_REQUEST['so_no'])
			$p="selected";
			?>
              <option  value="<?php echo $myrow['id']; ?>"<?php echo $p;?>><?php echo $myrow['orderNo']; ?></option>
              <?php }?>
			  </select>
			  </td>
	
	
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>From Date</strong></td>
	<td><input type="text" name="fdate" id="fdate" style="width:150px;"  class="datepicker" value="<?php echo $_REQUEST['fdate']?>"/></td>
	<td class="tabledata1"><strong>To Date</strong></td>
	<td><input type="text" name="todate" id="todate" style="width:150px;" class="datepicker"  value="<?php echo $_REQUEST['todate']?>"  /></td>
	<td class="tabledata1"><strong>&nbsp;</strong></td>
	<td>&nbsp;</td>
	
	
</tr>
<tr class="row1">
  <td colspan="8" style="text-align:center;"><input name="Go" type="submit" value="Go" onclick="return frmvalidate()" class="search" /></td>
</tr>
</table>
</form>
<?php 

if($Numrows>0)
{
if($_REQUEST['Go']){
if($_REQUEST['Site']!='')
{
	$filter[]="salesorder.branch='".$_REQUEST['Site']."'";
}
if($_REQUEST['client']!='')
{
	$filter[]="salesorder.client='".$_REQUEST['client']."'";
}
if($_REQUEST['so_no']!='')
{
	$filter[]="salesorder.orderNo='".$_REQUEST['so_no']."'";
}
if($_REQUEST['fdate']!='' && $_REQUEST['todate']!='')
{
	$filter[]="salesorder.orderDate between '".FormatDateFORSQL($_REQUEST['fdate'])."' and '".FormatDateFORSQL($_REQUEST['todate'])."'";
}
if($_REQUEST['fdate']!='' && $_REQUEST['todate']=='')
{
	$filter[]="salesorder.orderDate='".FormatDateFORSQL($_REQUEST['fdate'])."'";
}
if($_REQUEST['fdate']=='' && $_REQUEST['todate']!='')
{
	$filter[]="salesorder.orderDate='".FormatDateFORSQL($_REQUEST['todate'])."'";
}
if($_REQUEST['fdate']!='' && $_REQUEST['todate']=='')
{
	$filter[]="salesorder.orderDate='".FormatDateFORSQL($_REQUEST['fdate'])."'";
}
if($filter!='')
{
      $filter1=implode(" and ",$filter);
  
      //$sqlPoApp=DB_query("SELECT id,orderNo,client,SalesOrderType,orderDate FROM salesorder WHERE Po_recive_status='Pending' and Approval_status='1' and Purchase_Approval_status='1' and $filter1 ORDER BY purchase_ordNo",$db); 
      $sqlPoApp=DB_query("SELECT salesorder.id,orderNo,CLIENT,SalesOrderType,orderDate 
                       FROM salesorder INNER JOIN  salesorder_details ON salesorder_details.masterId=salesorder.id
                      WHERE $filter1  AND  salesorder_details.ApprovalStatus!=1 AND salesorder_details.pendingForDispatch='1'  group BY orderNo",$db);
   
}
?>
<form method="post">
<table width="1200" cellpadding="3" cellspacing="0" border="1" style="border-collapse:collapse; border:#999999 solid 1px;" align="center">
<tr align="center" bgcolor="#d4d4d4">
	<td class="tabledata" width="30"><strong>Sr No.</strong></td>
	<td class="tabledata" width="50"><strong>Date</strong></td>
	<td class="tabledata" width="120"><strong>SO No</strong></td>
	<td class="tabledata" width="120"><strong>Client</strong></td>
	<td class="tabledata" width="60"><strong>SO Type</strong></td>
	<td width="100" class="tabledata"><strong>Item Code</strong></td>
	<td class="tabledata" width="200"><strong>Item Name</strong></td>
	<td class="tabledata" width="34"><strong>Unit</strong></td>
	<td class="tabledata" width="40"><strong>SO Qty</strong></td>
	<td class="tabledata" width="40"><strong>Dispatch Qty</strong></td>
	<td class="tabledata" width="40"><strong>pending Qty</strong></td>
	<td width="60" class="tabledata"><strong>Short Close Qty</strong></td>
	<td class="tabledata" width="61"><strong>Short Close Date</strong></td>
        <td class="tabledata"><strong>Action</strong></td>
</tr>
<?php 
$i=0;$j=0; 
if(DB_num_rows($sqlPoApp)>0){
while($PoApp=DB_fetch_array($sqlPoApp)) {
    $i++;
//$sqlItem=DB_query("SELECT item_name,StockID,mst_unitid FROM item_master where id='".$PoApp['mst_itemid']."'",$db);
//$sqlItemInfo=DB_fetch_array($sqlItem);
//$sqlqty=DB_query("SELECT reciveqty FROM addmrnqty where id='".$PoApp['mst_itemid']."'",$db);
//$sqlqtyInfo=DB_fetch_array($sqlqty);
?>
<tr bgcolor="#c1c1c1">
	<td><?php echo $i; ?></td>
	<td><?php echo ConvertSQLDate($PoApp['orderDate']); ?></td>
	<td><?php echo $PoApp['orderNo']; ?></td>
	<td><?php echo $s->GetFieldValue('clientmaster',"id='".$PoApp['client']."'",'c_name'); ?></td>
	<td><?php echo $PoApp['SalesOrderType']; ?></td>
        <td colspan="9" align="left" valign="top" style="padding:0px;">
        <?php //$sqlPoDe=DB_query("SELECT id,mst_itemid,Quantity FROM salesorder_details WHERE masterId='".$PoApp['id']."' and ApprovalStatus='Pending'",$db);
        $sqlPoDe=DB_query("SELECT id,mst_itemid,Quantity FROM salesorder_details WHERE masterId='".$PoApp['id']."' and pendingForDispatch='1'",$db);
        while($sqlPoDetails=DB_fetch_array($sqlPoDe)){
             $j++;
 $sqlItem=DB_query("SELECT id,item_name,StockID,mst_unitid FROM item_master where id='".$sqlPoDetails['mst_itemid']."'",$db);
 $sqlItemInfo=DB_fetch_array($sqlItem);
 $sqlappqty=DB_query("SELECT sum(Approved_qty) AS Approved_qty FROM salesorder_details_approv WHERE OrderId='".$PoApp['id']."' AND OrderDetailId='".$sqlPoDetails['id']."' and ItemId='".$sqlPoDetails['mst_itemid']."'",$db);
 $appqty=DB_fetch_array($sqlappqty);
 //echo "SELECT B.reciveqty FROM addmrn AS A INNER JOIN addmrnqty AS B ON (A.id=B.issue_id) where A.order_no='".$PoApp['id']."' and B.mst_itemid='".$sqlPoDetails['mst_itemid']."'";
 $sqlQty=DB_query("SELECT sum(B.dispatchQty) AS dispatchQty FROM dispatch AS A INNER JOIN dispatch_details AS B ON (A.id=B.parentId) where A.againstSOP='".$PoApp['id']."' and B.item='".$sqlPoDetails['mst_itemid']."'",$db);
 $sqlQtyInfo=DB_fetch_array($sqlQty);
 $pendQty=$appqty['Approved_qty']-$sqlQtyInfo['dispatchQty'];
 ?>
<table width="100%" cellpadding="2" cellspacing="0">
     <tr bgcolor="99CCCC">
	<td style="border-right:#666666 1px solid;" width="105"><input type="hidden" name="hid_podeid[]" id="hid_podeid_<?php echo $j; ?>" size="5" value="<?php echo $sqlPoDetails['id']; ?>">
            <input type="hidden" name="hid_poid[]" id="hid_poid_<?php echo $j; ?>" size="5" value="<?php echo $PoApp['id']; ?>">
             <input type="hidden" name="hid_itemid[]" id="hid_itemid_<?php echo $j; ?>" size="5" value="<?php echo $sqlItemInfo['id']; ?>">
               <?php echo $sqlItemInfo['StockID']; ?></td>
	<td style="border-right:#666666 1px solid;" width="206"><?php echo $sqlItemInfo['item_name']; ?></td>
	<td style="border-right:#666666 1px solid;" width="40"><?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$sqlItemInfo['mst_unitid']."'",'unitname'); ?></td>
	<td style="border-right:#666666 1px solid;" width="46"><input type="hidden" name="hid_poqty[]" id="hid_poqty_<?php echo $j; ?>" size="5" value="<?php echo $appqty['Approved_qty']; ?>"><?php echo round($appqty['Approved_qty'],2); ?></td>
	<td style="border-right:#666666 1px solid;" width="53"><?php echo $sqlQtyInfo['dispatchQty']; ?></td>
	<td style="border-right:#666666 1px solid;" width="51"><?php echo $pendQty; ?><input type="hidden" name="hidpend_qty[]" id="hidpend_qty_<?php echo $j; ?>" size="5" value="<?php echo $pendQty; ?>"></td>
	<td style="border-right:#666666 1px solid;" width="66"><input type="text" name="sc_qty[]" id="sc_qty_<?php echo $j; ?>" size="5" value="" onkeyup="checkqty(this.value,this.id)" readonly></td>
	<td style="border-right:#666666 1px solid;" width="66"><input type="text" name="sc_date[]" id="sc_date_<?php echo $j; ?>" value="<?php echo date('d/m/Y') ?>" class="datepicker" size="10"></td>
        <td><input name="chkid[]" type="checkbox" id="chkid_<?php echo $j; ?>"   value="<?php  echo $PoApp['id']?>" onClick="fillQty(this.id);" /></td>
     </tr>
	</table>
        <?php } ?>
            </td>
</tr>
<?php }?>
<tr class="row1">
<td colspan="14"><p align="center"><input type="submit" name="Save" value="Save"  class="search" onclick="return confirm('Are You Sure?')" ></p></td>
<!--onclick="return confirm('This Associate will be deleted. Are you sure?')"-->
</tr>
<?php  }else{?>
<tr class="row1">
	<td colspan="14" style="color:#FF0000;">No Record Found</td>
</tr>
<?php  }?>
</table>
</form>
<?php  }

}else{
?>
<p align="center"><font style="font-size:12px; color:#FF0000;"><strong>You Have Not Permission To Approve The Purchase Orders</strong></font></p>
<?php }?>
</div>
</div>
</div>
<script>
    function fillQty(id){
       //alert(id);
     id1=id.split("_");
     i=id1[1];
     var pendQty=document.getElementById("hidpend_qty_"+i).value;
       if(document.getElementById("chkid_"+i).checked==true)
       {
    document.getElementById("sc_qty_"+i).value=pendQty;
     document.getElementById("sc_qty_"+i).removeAttribute('readonly','');
       }
       else if(document.getElementById("chkid_"+i).checked==false)
       {
    document.getElementById("sc_qty_"+i).value='';
     document.getElementById("sc_qty_"+i).setAttribute('readonly','true');
       }
    }
    function checkqty(val,id){
     id1=id.split("_");
     i=id1[2];
     var pendQty=document.getElementById("hidpend_qty_"+i).value;
     if( isNaN(document.getElementById("sc_qty_"+i).value)){
      alert("Please Enter A Valid Number");
      document.getElementById("sc_qty_"+i).value='';
      return false;
      }else if(pendQty<val){
      alert("Short Close Qty Should not be Greater then pending Quantity");
      document.getElementById("sc_qty_"+i).value='';
      return false;
      }else{
      return true;
      }
        
    }
    
</script>