<?php
$PageSecurity = 2;
$title = _('Material Return Slip');
?>
<style>
#slidingDiv {
    display: none;
	height:100px;
	background-color: #D9D6DA;
	padding:20px;
}
</style>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $s->getField('modules_form','id',$_REQUEST['frmid'],'frm_name'); ?></h1>

<?php if($_SESSION['frmAdd']=='1') { ?>
<p align="left"><a href="index.php?route=Inventory/transation/Return_Slip_add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&NewReturn=yes"><input type="button" class="search" value="Add New Receive Slip" /></a></p>
<?php } ?>

<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<tr>
    <th width="10%" rowspan="2">Received Slip No.</th>
    <th width="7%" rowspan="2">Received Date</th>
    <th rowspan="2">Branch</th>
    <th width="20%" rowspan="2">From Store</th>
	  <th width="20%" rowspan="2">To Store</th>
	  <th colspan="4" style="text-align:center;">Action</th>
	</tr>
  <tr>
    <th width="4%" style="text-align:center;">View</th>
	 <!-- <th width="4%" style="text-align:center;">Edit</th> -->
    <th width="4%" style="text-align:center;">Print</th>
	  <th width="4%" style="text-align:center;">Delete</th>
  </tr>
  <?php  
    
   $sql=DB_query("Select * from material_return  where site in('".$_SESSION['AssignedBranch']."') and returndate between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."'",$db);
 
  $i=0;  
  if(DB_num_rows($sql)>0)
  {
  while($myrow=DB_fetch_array($sql))
  {
  ?>
    <tr>
      <td><?php echo $myrow['return_slipno']; ?></td>
      <td><?php echo ConvertSQLDate($myrow['returndate']); ?></td>
      <td><?php $sitename=$s->getField('locations','loccode',$myrow['site'],'locationname'); echo $sitename;?></td>
      <td><?php $storename=$s->getField('storetype','id',$myrow['store'],'StoreName');echo $storename;?></td>
	    <td><?php echo $s->getField('storetype','id',$myrow['store_to'],'StoreName'); ?>
      </td>
	    <td style="text-align:center;"><?php 
            if($_SESSION['frmView']=='1')
            { ?>
              <a href="index.php?route=Inventory/transation/Return_Slip_view&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id']; ?>&action=View"><img src="images/search.gif" height="15" width="15" title="View" /></a><?php }else{ ?><img src="images/search.gif" height="15" width="15" title="No Permission" style="opacity:0.5;" /><?php } ?>
      </td>
	 <!--  <td style="text-align:center;">
	   <?php 
        // echo "***".$pending=checkPending($myrow['id']);
        if($_SESSION['frmEdit']=='1')
        {
          $check = checkEdit($myrow['id']);
          if($check!=0 )
          {?>
	         <a href="index.php?route=Inventory/transation/Return_Slip_edit&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id']; ?>&action=Edit"><img src="images/icon-edit.png" height="15" width="15" title="Edit" /></a><?php }
          else
          { ?>
           <img src="images/icon-edit.png" height="15" width="15" title="No Permission" style="opacity:0.5;" />
          <?php }
        }
        else echo "Return Used";?>
	   </td> -->
      <td style="text-align:center;">
      <?php 
        if($_SESSION['frmEdit']=='1')
        {
          $check = checkEdit($myrow['id']);
          if($check!=0 )
          {?>
          <a target="_blank" href="return_slip_print.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=Report&id=<?php echo $myrow['id']; ?>"><img src="images/icon_print.jpg" height="18" width="18" title="Print" /></a>
        <?php 
          } 
        }
        else
        { ?>
         <img src="images/icon_print.jpg" height="18" width="18" title="No Permission" style="opacity:0.5;" />
        <?php } ?>
      </td>
	     <td style="text-align:center;">
        <?php
        if($_SESSION['frmDelete']=='1')
        {
          if($check!=0)
          {?>
	           <a href="index.php?route=Inventory/transation/Return_Slip_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id']; ?>&action=Delete&sitecode=<?php echo $myrow['site']; ?>&store=<?php echo $myrow['store'];?>&date=<?php echo $myrow['returndate']; ?>" onclick="return confirm('Are you sure you want to delete?')"><img src="images/Icon_Delete.png" height="15" width="15" title="Delete" /></a><?php }else { ?><img src="images/Icon_Delete.png" height="15" width="15" title="No Permission" style="opacity:0.5;" /><?php } ?>
  <?php }
       else
        {
           echo  "Return Used";
        }
       ?>
	   </td>
   </tr>
  <?php $i++;} }
  else
  {?>
  <tr>
  <td colspan="12" style="text-align:center;"><strong>No Record Found</strong></td>
  </tr>
  <?php 	
  }
  ?>
</table>
</div>
</div>
<?php 
  function checkEdit($id)
  {
    global $db;
    $query="SELECT id FROM stock_valuation_tbl WHERE against_tra_id in( SELECT id FROM stock_valuation_tbl WHERE TYPE='RTN' AND against_voucher_main_id='".$id."') ";
    $result=Db_query($query,$db);
   
    if(DB_num_rows($result)>0)
      return 0;
    else
      return 1;
  }

?>
<?php 

if($_REQUEST['id']!='' && $_REQUEST['action']=='Delete') 
{     
$slipno=$s->getField('material_return','id',$_REQUEST['id'],'issue_slipno');
$update=DB_query("update issueqty set status_Receive=0 where issue_id='".$slipno."'",$db);
$update1=DB_query("update addissue set status='Pending' where id='".$slipno."'",$db);
$del1=DB_query("delete from material_return_details where parentId='".$_REQUEST['id']."'",$db);
$del=Db_query("delete from material_return where id='".$_REQUEST['id']."'",$db);
$del_stocktable=DB_query("delete from stock_valuation_tbl where against_voucher_main_id='".$_REQUEST['id']."' and type='RTN' ",$db);

echo "<script>location.href='index.php?route=Inventory/transation/Return_Slip_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>

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


<?php
  function checkPending($id)
  {
      $detailsSql="select * from material_return_details where parentId='".$id."' ";
      $detailsResult=DB_query($detailsSql,$db);
      $pending_qty=0;
      while($detailsRow=DB_fetch_array($detailsResult))
      {
          $unitname=$s->getField('unitsofmeasure','unitid',$detailsRow['mst_unitid'],'unitname');
          $unitname1=$s->getField('unitsofmeasure','unitid',$detailsRow['conv_unitid'],'unitname');
          $issue_slipno=$s->getField('material_return','id',$id,'issue_slipno');

     
          $check_last_mrn="Select sum(receiveqty),issueqty,sum(stock_qty2) as stock_qty2 from material_return_details 
          inner join material_return       on material_return.id=material_return_details.parentId 
          where material_return.issue_slipno='".$issue_slipno."' and material_return_details.itemid='".$detailsRow['itemid']."'  and mst_unitid='".$detailsRow['mst_unitid']."'";
      
         $last_mrn_res=DB_query($check_last_mrn,$db);
         $last_mrn_row=DB_fetch_array($last_mrn_res);
         if($last_mrn_row['issueqty']!='' && $last_mrn_row['sum(receiveqty)']!='' )
         {
            $qty=$last_mrn_row['issueqty']-($last_mrn_row['sum(receiveqty)']);
            if($qty==0)
              $pending_qty=$qty;
         }
         else
         {
            $qty=$PORecDetails['issueqty'];
            if($qty==0)
              $pending_qty=$qty;
         }
      }
      if($pending_qty==0)
        return 0;
      else
        return 1;
  }
 ?>
