<?php 
$PageSecurity = 4;
include('includes/session.inc');
include('includes/DefineSuppTransClass.php');
include('includes/function_lib.php');
include('includes/stockcalculation.php');

   $function_filter[0]=$_REQUEST['branch'];
    $function_filter[1]=$_REQUEST['store'];
    $function_filter[2]=date('Y-m-d');
    $grand_total=0;
?>
<table width="100%" border="1" cellspacing="0" id="tableDataProduction" cellpadding="3" style="border-collapse:collapse;">
      <tr>
        <th colspan="2"><strong>Item Code</strong></th>
        <th><strong>Descriptions</strong></th>
        <th><strong>Unit</strong></th>
        <!-- <th colspan="2"><strong><center>Specification</center></strong></th> -->
         <th><strong>Batch no.</strong></th>
        <th><strong>Stock Balance </strong></th>
        <th><strong>Issue Qty </strong></th>
        <th><strong>Rem Qty </strong></th>
        <th><strong>Return Qty </strong></th>
        <th><strong>Rate </strong></th>
        <th><strong>Basic Amount</strong></th>
         <th><strong>GST(%)</strong></th>
        <th><strong>GST Amount</strong></th>
        <th><strong>SGST</strong></th>
        <th><strong>CGST</strong></th>
        <th><strong>IGST</strong></th>
        <th><strong>UTGST</strong></th>
        <th><strong>Total Amount </strong></th>
      </tr>
      <?php
           $MRV_return=array();
           $resultreturnQty=DB_query("select * from inv_material_return_details where mrn_id='".$_REQUEST['site']."'",$db);
            while($rowQty=DB_fetch_array($resultreturnQty))
            {
            $MRV_return[$rowQty['itemid']]=$rowQty['ReturnQty'];
            $MRV_return_gst[$rowQty['itemid']]=array('gst'=>$rowQty['gst_tot'],'sgst'=>$rowQty['sgst_tot'],'cgst'=>$rowQty['cgst_tot'],'igst'=>$rowQty['igst_tot'],'ugst'=>$rowQty['ugst_tot']);
            }     


	  $i=0;
   
	 // echo "select * from addmrnqty where id='".$_GET['site']."'";
	  $sql=DB_query("select * from addmrnqty where issue_id='".$_REQUEST['site']."'",$db);
	  while($row=DB_fetch_array($sql))
	  {   $sgst_amt=0;
      $cgst_amt=0;
      $igst_amt=0;
      $ugst_amt=0;
      $total_gst=0;
  
	   $itemcode=$s->getField('item_master','id',$row['mst_itemid'],'StockID');
	    $itemname=$s->getField('item_master','id',$row['mst_itemid'],'item_name');
	   $unitid=$s->getField('item_master','id',$row['mst_itemid'],'mst_unitid');
	  $unit=$s->getField('unitsofmeasure','unitid',$unitid,'unitname');
       

     $purchase_bill_id=$s->GetFieldValue('purchase_billbook',"mrn_no='".$_REQUEST['site']."'",'id');
     $purchase_bill_pending_amt=$s->GetFieldValue('purchase_billbook',"id='".$purchase_bill_id."'",'pending_amount');
     $purchase_bill_dtls_id=$s->GetFieldValue('purchase_bill_details',"purch_billbookid='".$purchase_bill_id."' and mst_productid='".$row['mst_itemid']."'",'id');
     $sgst_amt=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'sgst_amt');
     $sgst_amt=$sgst_amt-$MRV_return_gst[$row['mst_itemid']]['sgst'];

     $cgst_amt=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'cgst_amt');
     $cgst_amt=$cgst_amt-$MRV_return_gst[$row['mst_itemid']]['cgst'];

     $igst_amt=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'igst_amt');
     $igst_amt=$igst_amt-$MRV_return_gst[$row['mst_itemid']]['igst'];

     $ugst_amt=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'ugst_amt');
     $ugst_amt=$ugst_amt-$MRV_return_gst[$row['mst_itemid']]['ugst'];

     $gst_per=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'gst_tax_perc');
     $total_gst=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'gst_tax_amt');
     $total_gst-=$MRV_return_gst[$row['mst_itemid']]['gst'];
     $item_rate=$s->GetFieldValue('purchase_bill_details',"id='".$purchase_bill_dtls_id."'",'rate');
	  
   $sql_stock=DB_fetch_array(DB_query("select sum(inwardqty_primary) as inward,sum(outwardqty_primary) as outward from stock_valuation_tbl where site='".$_REQUEST['branch']."' and store='".$_REQUEST['store']."' and mst_itemid='".$row['mst_itemid']."' and batch_no='".$row['batch_no']."'",$db));
   $tot_stock=$sql_stock[0]-$tot_stock[1];
	
		$outward_rate=$in_1[1];
	  
	    $grand_total+=$item_rate*($row['reciveqty'] - $MRV_return[$row['mst_itemid']]);
	  
	   ?>
	  	  
        <tr bgcolor="#e7e7e7">
          <td><input type="text" value="<?php echo $itemcode; ?>" readonly tabindex="" id="itemcode_<?php echo $i; ?>" name="delitem[]"size="20"
		    /></td>
          <td><input  type="hidden"   readonly id="itemdataid_<?php echo $i; ?>"  name="itemdataid[]" 
		  value="<?php echo $row['mst_itemid']; ?>" /></td>
          <td><input type="text" name="desc[]" readonly  id="descriptions_<?php echo $i; ?>" size="25"  value="<?php echo $itemname; ?>"/></td>
          <td><input type="text" name="uom[]" id="uom_<?php echo $i; ?>" value="<?php echo $unit; ?>" readonly  size="30"/>
          </td>
          <!--  <td><input type="hidden" name="sizesp[]" value="<?php //echo $row['size']; ?>" /><?php //echo $s->GetFieldValue('size_master',"id='".$row['size']."'",'size'); ?></td>
   <td><input type="hidden" name="gradesp[]" value="<?php //echo $row['Gread']; ?>" /><?php //echo $s->GetFieldValue('product_grade',"id='".$row['Gread']."'",'grade'); ?></td>  -->
          <td><?php echo $row['batch_no']; ?><input type="hidden" value="<?php echo $row['batch_no']; ?>" name="batch_no[]"></td>
          <td><input type="text" name="stock[]"  id="Stockbalance_<?php echo $i; ?>"  size="30" value="<?php echo $tot_stock; ?>"   readonly/></td>
          <td><input  type="text" name="issueqty[]" readonly  value="<?php echo $row['reciveqty']; ?>" size="30"   /></td>
          <td><input name="remainingqty[]" readonly value="<?php  echo ($row['reciveqty'] - $MRV_return[$row['mst_itemid']]);  ?>" onkeypress="return isNumberKey(event)"></td>
           <td><input  type="text" name="returnqty[]" autocomplete="off" value="<?php  echo ($row['reciveqty'] - $MRV_return[$row['mst_itemid']]);  ?>" onkeypress="return isNumberKey(event)" onkeyup="chk_recived_qty(this)" size="30"   /></td>
           <td><input  type="text" name="rate[]" value="<?php echo $item_rate; ?>" onkeypress="return isNumberKey(event)" onkeyup="chk_recived_qty(this)" size="30"   /></td>
           <td><input  type="text" name="basic[]" value="<?php echo $basic=$item_rate*($row['reciveqty'] - $MRV_return[$row['mst_itemid']]); ?>" onkeypress="return isNumberKey(event)" onkeyup="chk_recived_qty(this)" size="30"   /></td>
            <td><input  type="text" name="gstper[]" value="<?php echo $gst_per; ?>" onkeypress="return isNumberKey(event)" readonly size="30"   /></td>
          <td><input  type="text" name="gstamt[]" value="<?php echo $total_gst; ?>" onkeypress="return isNumberKey(event)" readonly size="30"   /></td>
          <td><input  type="text" name="sgstamt[]" value="<?php echo $sgst_amt; ?>" onkeypress="return isNumberKey(event)" readonly size="30"   /></td>
          <td><input  type="text" name="cgstamt[]" value="<?php echo $cgst_amt; ?>" onkeypress="return isNumberKey(event)" readonly size="30"   /></td>
          <td><input  type="text" name="igstamt[]" value="<?php echo $igst_amt; ?>" onkeypress="return isNumberKey(event)" readonly size="30"   /></td>
          <td><input  type="text" name="ugstamt[]" value="<?php echo $ugst_amt; ?>" onkeypress="return isNumberKey(event)" readonly size="30"   /></td>
           <td><input  type="text" name="totalamt[]" value="<?php echo $basic+$total_gst; $sum+=$basic+$total_gst; ?>" onkeypress="return isNumberKey(event)" class="total" readonly onkeyup="chk_recived_qty(this)" size="30"   /></td>
         
        </tr>
		<?php $i++; } ?>
     
    </table>
    <table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata">Remarks:</td>
    <td><textarea name="remarks" id="remarks" cols="" rows="" style="width:100%; height:60px;"></textarea></td>
    <td style="text-align:right;"><strong>Total Purchase Bill Amount</strong></td>
    <td><input type="text" name="purchase_bill" id="purchase_bill" value="<?php echo $purchase_bill_pending_amt; ?>" readonly /></td>
    <td style="text-align:right;"><strong>Total Amount</strong></td>
    <td><input type="text" name="grand_total" id="grand_total" value="<?php echo $sum; ?>" readonly /><input type="hidden" name="purchase_bill_id" id="purchase_bill_id" value="<?php  echo $purchase_bill_id; ?>"/></td>
  </tr>
</table>
    <p align="center"><input type="submit" name="Save" value="Save" /></p>


	
	



