<?php
    include('config.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"){
		$query_f1 = " AND outlet_autoid = '$f1'";
		}
      
   	$query_string = " FROM soheader WHERE (so_transno LIKE '%$q%' or cust_name LIKE '%$q%') AND so_status > '0' $query_f1";


   	$sum_sql = "SELECT COUNT(*) as num $query_string";
	$conn_sum_sql = $conn->query($sum_sql);
	$pagenum = $conn_sum_sql->fetch_assoc();
	$total_pages = $pagenum["num"];
   
  	include ("../../sys/paging.php");
 
   	$sql = "SELECT * $query_string ORDER BY so_transdate DESC, so_transno DESC LIMIT $start,$limit";
	$sql_con = $conn->query($sql) or die($sql);

?>

<div class="rec_wrapper">
    <table class="table-striped table-hover table-bordered table table-condensed">
    <thead>
    	<tr class="bold">
     	<td style="width:130px">No Penjualan</td>
        <td style="width:100px;">Tanggal</td>
        <td class="tl" style="width:250px">Nama</td>
        <td class="tc" style="width:150px">Qty</td>
        <td class="tc" style="width:120px">Subtotal</td>
        <td class="tc" style="width:120px">Pot</td>
        <td class="tc" style="width:120px">Retur</td>
        <td class="tc" style="width:150px">Total</td>
        <td class="tc" style="width:150px">Outlet</td>
        <td class="tc" style="width:150px">Status</td>
        <td class="tc" style="width:110px">Action</td>
        </tr>
        </thead>

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




			$id = $rs['so_autoid']	;
			if($rs['so_status']==2){
				$status = "OK";
				}
			if($rs['so_status']==99){
				$status = "DELETED";
				}
		$sqln = "SELECT sum(sd_qty) as qty, sum(sd_potongan) as total_pot, sum(sd_price * sd_qtyr) as total_ret from sodetail WHERE so_autoid = '$id'";
		$dcon_n = $conn->query($sqln) or die($sqln);
		$rst = $dcon_n->fetch_assoc();
		$qty 		= $rst['qty'];	
		$total_pot 	= $rst['total_pot'];
		$total_ret 	= $rst['total_ret'];
		$gtotal 	= ($rs['so_gtotal'] - $total_pot - $total_ret);
		?>
        <tr class="rec_body">
	   		<td><?php echo $rs['so_transno']?></td>
    	    <td><?php echo dtostr($rs['so_transdate'])?></td>
	        <td class="tl"><?php echo $rs['cust_name']?></td>
            <td class="tr"><?php echo number_format($qty,2)?> Kg</td>
            <td class="tr"><?php echo number_format($rs['so_gtotal'],0)?> </td>
            <td class="tr"><?php echo number_format($total_pot,0)?> </td>
            <td class="tr"><?php echo number_format($total_ret,0)?> </td>
            <td class="tr"><?php echo number_format($gtotal,0)?> </td>
            <td class="tc"><?php echo $_outlet_name[$rs['outlet_autoid']]?></td>
            <td class="tc"><?php echo $status?></td>
            <td class="tc">
            	<a href="view?id=<?php echo $id?>"  data-toggle="tooltip" title="View">
                <img src="../../image/view.png" title="View Penjualan" class="icon"/></a>


            	<a href="unpost?id=<?php echo $id?>"  data-toggle="tooltip" title="Unpost Penjualan">
                <img src="../../image/edit.png" title="Unpost Penjualan" 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>No Records</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?> Penjualan</td>
            </tr>
        </table>
    </div>
