<!DOCTYPE HTML>
<?php 
include('config.php');

$cust_autoid 	= $_POST['cust_autoid'];
$pm_autoid 		= $_POST['pm_autoid'];
ini_set('error_reporting',0);
$pagename = "Lap Penjualan";

$fdate = dtosql($_POST['fdate']);
$tdate = dtosql($_POST['tdate']);


	$filter_q = "";
    foreach($cust_autoid as $value)
    {
		if($value){
			$txt = " a.cust_autoid = '$value' or";	
			$filter_q = $filter_q.$txt;
		}
		
    }

	if($filter_q){
		$filter_query = " AND ( ".substr_replace($filter_q ,"", -2)." )";
	}


	$filter_q_2 = "";
    foreach($pm_autoid as $value)
    {
		if($value){
			$txt = " b.pm_autoid = '$value' or";	
			$filter_q_2 = $filter_q_2.$txt;
		}
		
    }


	if($filter_q_2){
		$filter_query_2 = " AND ( ".substr_replace($filter_q_2 ,"", -2)." )";
	}

?>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo $pagename?></title>
    <?php include('../../common/script_css.php');?>

</head>
<body class="print_body">
<?php include('../../sys/navprint.php')?>
<div>
    <table class="table" width="1600" border="1" bordercolor="#CCCCCC" style="font-size:12px">
    <thead>
    	<tr class="bold">
        	<td colspan="15">
            Laporan Penjualan HPP
            <br>
            <?php echo iddate2($fdate)?> -  <?php echo iddate($tdate)?> 
            </td>
        </tr>
    	<tr class="bold">
        	<td style="width:30px;">No</td>
                	<td style="width:135px">Outlet</td>
                	<td style="width:135px">Order No</td>
                    <td style="width:120px">Order Date</td>
                    <td style="width:130px">Order Source</td>
                    <td style="width:130px">Kode Item</td>
                    <td>Nama Item</td>
                    <td style="width:120px">Quantity</td>
                    <td>Harga Jual</td>
                    <td>Disc (%)</td>
                    <td>Disc (Rp)</td>
                    <td>Subtotal</td>
                    <td style="color:#0066CC" class="tr">HPP</td>
					<td style="color:#0066CC" class="tr">Subtotal HPP</td>
                    <td>Profit/Loss</td>
	</tr>
	</thead>

<?php
$i = 1;
$gtotal_qty = 0;
$gtotal_amount = 0;
$gtotal_hpp = 0;
  $sql = " SELECT * FROM import_table WHERE (order_date BETWEEN '$fdate' AND '$tdate') ORDER BY order_date,order_no";
  $dcon = $conn->query($sql) or die($sql);
  while($rs = $dcon->fetch_assoc()){
		
	$outlet_name = $_cust_name[$rs['outlet_id']];
	$pm_code = $rs['pm_code'];
	
	$sqlpr = "SELECT * FROM mst_product WHERE pm_code = '$pm_code'";
	$dcon_pr = $conn->query($sqlpr) or die($sqlrp);
	$pr = $dcon_pr->fetch_assoc();
	

	if($pr['pm_gtotal']==0)
	{
		$hpp = $pr['pm_bprice'];
	}
	else{
		$hpp = $pr['pm_gtotal'];		
		}
		$qty = $rs['qty'];
		$subtotal_hpp = $hpp * $qty;
		
		
		$profit = $rs['subtotal'] - $subtotal_hpp;
		
		if($hpp==0){$profit = $rs['subtotal'] - ($rs['subtotal']  * 2); }
?> 
	<tr>
                	<td><?php echo $i?></td>
                    <td><?php echo $outlet_name?></td>
                	<td><?php echo $rs['order_no']?></td>
                    <td><?php echo dtostr(substr($rs['order_date'],0,10))?></td>
                    <td><?php echo $rs['order_source']?></td>
					<td><?php echo $rs['pm_code']?></td>
                    <td><?php echo $rs['pm_name']?></td>
                    <td class="tr"><?php echo $rs['qty']?></td>
                    <td class="tr"><?php echo $rs['price']?></td>
                    <td class="tr"><?php echo $rs['disc_percen']?></td>
                    <td class="tr"><?php echo $rs['disc_amount']?></td>
                    <td class="tr"><?php echo $rs['subtotal']?></td>
                    <td class="tr" style="color:#0066CC"><?php echo $hpp?></td>
                    <td class="tr" style="color:#0066CC"><?php echo $subtotal_hpp?></td>
                    <td class="tr" style="color:#093"><?php echo $profit?></td>
	</tr>
<?php 
$i = $i + 1;
$gtotal_qty 	= $gtotal_qty + $rs['qty'];
$gtotal_amount 	= $gtotal_amount + $rs['subtotal'];
$gtotal_hpp = $gtotal_hpp + $subtotal_hpp;
$gtotal_profit = $gtotal_profit + $profit;
} ?>
	<tr class="bold">
    	<td colspan="7" class="tr">Jumlah</td>
        <td class="tr"><?php echo $gtotal_qty?></td>

        <td colspan="3"></td>        
        <td class="tr"><?php echo $gtotal_amount?></td>
        <td></td>
		<td style="color:#0066CC" class="tr"><?php echo $gtotal_hpp?></td>
        <td style="color:#093" class="tr"><?php echo $gtotal_profit?></td>
	</tr>

</div>
</body>

</html>