<style>
  .ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-draggable.ui-resizable {
    position: absolute!important;
    height: auto!important;
    width: 50%!important;
    top: 190px!important;
    left: 274.5px!important;
    z-index: 101!important;
}
.table-bordered, .table-bordered td, .table-bordered th {
    border: 1px solid #626e82;
}
th {
    white-space: break-spaces;
    padding: 5px;
}
td{
  padding: 5px;
}
</style>
<div class="container">
      <form action="" id="invoice_form_validate" class="uk-form-stacked"  method="post" autocomplete="off">
        <?php if(!empty($history1)) { ?>
        <h4 style="text-align:center;"><?php echo $item_name; ?></h4>
        <table id="" class="table-bordered">
        <thead>
            <tr class="data_heading">
				<th><strong>Month</strong></th>
				<th><strong>Opening Stock</strong></th>
				<th><strong>Inward</strong></th>
				<th><strong>Closing Stock</strong></th>
				<th><strong> Consumption</strong></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (isset($branchname)) {
				$count = 1; 
				
				//$results = $purchase->get_item_history1($item_name,$branch);
				$sql = $this->db->query("select max(`stock_id`) as stid,opening_stock,item_name,DATE_FORMAT(transaction_date,'%M') as month1,DATE_FORMAT(transaction_date,'%m') as month2,sum(in_qty)as inward,sum(out_qty) as outward,branchname,max(last_modified) as maxdate1 from stock_detail where branchname='$branchname' and item_name='$item_name' and (transaction_date >= CURDATE() - INTERVAL 3 MONTH )and trans_ref_no like 'G%' group by month1 order by month2 asc");
				
				$rowcount = $sql->num_rows();
          ?>
            <?php
				//$count = 1; 
				
				//$results = $purchase->get_item_history1($item_name,$branch);
				
				$sql1 = $this->db->query("select max(`stock_id`) as stid1,opening_stock,item_name,DATE_FORMAT(transaction_date,'%M') as month,DATE_FORMAT(transaction_date,'%m') as month2,sum(in_qty)as inward,sum(out_qty) as outward,branchname,max(last_modified) as maxdate1 from stock_detail where branchname='$branchname' and item_name='$item_name' and (transaction_date >= CURDATE() - INTERVAL 3 MONTH )group by month order by month2 asc");
				//$results1 = $purchase->get_item_history2($item_name,$branch);
				//$rowcount = $results->num_rows();

			        //$history1 = $results->result();
			        //foreach($history1 as $h1) { 
					 foreach($sql->result_array() as $row)  {
                    
                  $inward = $row['inward'];
                  foreach($sql1->result_array() as $row5) 
                  $opening_stock= $row5['opening_stock'];  
                  $month1= $row['month1']; 
					?>
					<tr class="data_list">
						<td><?php echo $month1; ?></td>
						<td><?php echo $opening_stock; ?></td>
						<td><?php echo $inward; ?></td>
						<td>
						    <?php $stock_id = $row5['stid1'];
						//echo $stock_id;
							$sql3 = $this->db->query("select * from stock_detail where stock_id='$stock_id'");
							foreach($sql3->result_array() as $r2){
						        echo $r2['closing_stock'];
							    $sum = ($row['opening_stock']+$row['inward']) - $r2['closing_stock'];
							//echo $sum;
							}	 ?>					</td>
						<td>	
						<?php
						$stock_id = $row5['stid1'];
							//echo $stock_id;
							$sql4 = $this->db->query("select * from stock_detail where stock_id='$stock_id'");
							
						   foreach($sql3->result_array() as $r3){
						
								//echo $row4['closing_stock'];
							
							
							$sum = ($row5['opening_stock']+$row['inward']) - $r3['closing_stock'];
							echo $sum; 
							?>
							<input type="hidden" class="consum" value="<?php echo $sum ?>">
							<?php
						   }
							?>
						</td>
						
					</tr>
					
					
					<?php
					}
					
				?> 
				<input type="hidden" id="rowcount" value="<?php echo $rowcount ?>">
				<tr>
				<td colspan="4">Avg Consumption
				</td>
				<td>
					<input type="hidden" id="avg" value="">
					<span id="avg2"></span>
				</td>
				</tr>
			<?php $count++;  
			
			}
			?>  
        </tbody>
      </table>
      <?php } else { ?>
      <h3>No records..</h3>
      <?php } ?>
      </form>
    </div>
    <script>
	$(document).ready(function(){
	var rowcnt = $('#rowcount').val();
		var sum = 0;
		$('.consum').each(function() {
			sum += Number($(this).val());
		});
		var sum1 = (sum/rowcnt).toFixed(2)
		$('#avg').val(sum1);
		$('#avg2').html(sum1);
	});
</script>