<?php
    include('config.php');
	include('../../sys/hpp_outlet.php');
	$limit = 10;	
	$targetpage = "record.php"; 	//your file name  (the name of this file)
	$page = $_POST['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0

	if($f1=="X"){$f1_filter = " AND a.outlet_id = '0'";}
	else{
		$f1_filter = " AND a.outlet_id = '$f1'";
		}
	
	$q_filter = " WHERE a.pm_autoid = b.pm_autoid AND (a.pm_name LIKE '%$q%' or a.pm_code LIKE '%$q%')  $f1_filter";
   

   $total_pages = 0;
   	$sum_sql = "SELECT COUNT(*) as num FROM invlog a, mst_product b  $q_filter  GROUP BY a.pm_autoid, a.outlet_id ";				
	$conn_sum_sql = $conn->query($sum_sql) or die($sum_sql);
	while($pagenum = $conn_sum_sql->fetch_assoc()){
	$total_pages = $total_pages + 1;
	}


   


   	$sql = "SELECT * FROM invlog a , mst_product b $q_filter 
			GROUP BY a.pm_autoid,  a.outlet_id 
			ORDER BY a.pm_name LIMIT $start,$limit";
	$sql_con = $conn->query($sql) or die($sql);

   	include ("../../sys/paging.php");

?>

<div class="rec_wrapper">
    <table class="table-striped table-hover table-bordered table table-condensed">
    <thead>
    	<tr class="bold">
        	<td style="width:80px;">Kode Produk</td>
            <td style="width:320px;">Nama Produk</td>
            <td class="tc" style="width:100px;">Stok Qty</td>
            <td class="tc" style="width:100px">Avg Price</td>
            <td class="tc" style="width:100px">Avg Price</td>
            <td class="tc" style="width:70px;">Action</td>
        </tr>
        </thead>

		<tbody>
        <?php
		$i = 0;
			while($rs = $sql_con->fetch_assoc()){

			$id = $rs['pm_autoid']	;
			$pm_autoid = $rs['pm_autoid'];
			$stok_qty = get_stok_info($pm_autoid,$rs['outlet_id'],$conn);
			$whs = $rs['whs_autoid'];
			
			
			$convert = $rs['pm_ums_convert'];
			if(!$convert){$convert = 1;}
			
			if($rs['outlet_id']==0){$outlet = "Central";
			$avg_price = get_avg_price($pm_autoid,0,$conn,"BIG");//$rs['pm_avg_price'];
			}
			else{
				$outlet = $_outlet_name[$rs['outlet_id']];
				$avg_price = get_last_price_outlet($pm_autoid,$f1,$conn);//$rs['pm_avg_price'];
				$avg_price * $convert;
			}
			
			$ums_price = $avg_price/$convert;
			
			
			 $sqlr = "SELECT SUM(curr_qty) as real_stok FROM invlog where pm_autoid = '$pm_autoid' AND outlet_id = 0";
			$sqlr_con = $conn->query($sqlr) or die($sqlr);
			$rd = $sqlr_con->fetch_assoc();
			$curr_qty = $rd['real_stok'];

			
		?>
        <tr class="rec_body">
        	<td><?php echo $rs['pm_code']?></td>
            <td><?php echo $rs['pm_name']?></td>
            <td class="tr">
				<?php echo number_format($stok_qty). " ".$rs['pm_ums'] ?>
                </td>
            <td class="tc">
				<b class="text text-info">
					<?php echo number_format($avg_price)?> 
                    </b>
                </td>
                <td class="tc">
					<b class="text text-success">
						<?php echo number_format($ums_price,2)?> (<?php echo $rs['pm_ums']?>)
                    </b>
                </td>
            <td class="tc">
            	<a href="view?id=<?php echo $id?>&outlet_id=<?php echo $rs['outlet_id']?>"  data-toggle="tooltip">
                <img src="../../image/view.png" title="Lihat Stok Produk" class="icon"/></a>

            </td>         
        </tr>
	<?php 
	$i = $i + 1;
	} 
	if($i==0){
		?>
        <tr class="rec_body">
        	<td colspan="10" style="height:400px;" class="tc"><h4>Belum Ada Data Stok</h4></td>       
        </tr>
	<?php	} ?>
	</tbody>
    </table>
    </div>
    <div class="record_footer">
            <table class="tbl">
        	<tr class="pad5">
            	<td colspan="5" class="tl pad5" style="border:none"><?php echo $pagination?></td>
                <td colspan="4" class="tr pad5 bold" style="border:none;color:#333">Jumlah <?php echo $total_pages?> Item</td>
            </tr>
        </table>
    </div>
