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

$pagename = "Laporan Stok";

$month = dtosql($_POST['month']);
$tahun = dtosql($_POST['tahun']);



	
$pm_autoid = $_POST['pm_autoid'];



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


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




function stok_awal($conn,$pm_autoid,$xbln,$xtahun){
	$sqln = "SELECT SUM(log_inqty) as qty, SUM(log_inqty * log_price) as subtotal FROM invlog  WHERE pm_autoid = '$pm_autoid'
			AND log_inqty > 0  AND month(log_refdate) < '$month' AND  year(log_refdate) <= '$tahun'";
	$sql_con = $conn->query($sqln) or die($sqln);
	$rsn = $sql_con->fetch_assoc();
	
	$subtotal 		= $rsn['subtotal'];
	$in_qty 		= $rsn['qty'];
	$avg_price 		= $subtotal / $in_qty;
	
	
	$sqlz = "SELECT SUM(log_outqty) as qty FROM invlog  WHERE pm_autoid = '$pm_autoid'
			AND log_outqty > 0  AND month(log_refdate) < '$month' AND  year(log_refdate) <= '$tahun'";
	$sqlz_con = $conn->query($sqlz) or die($sqlz);
	$rsnz = $sqlz_con->fetch_assoc();
	
	$out_qty 		= $rsnz['qty'];

}

?>
<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 class="px-1">
    <table class="table" width="1600" border="1" bordercolor="#CCCCCC" style="font-size:12px">
    <thead>
    	<tr class="bold">
        	<td colspan="10">
            <?php echo $pagename?>
            <br>
            <?php echo $arr_month[$month]?> -  <?php echo ($tahun)?> 
            </td>
        </tr>
    	<tr class="bold">
        	<td style="width:30px;">No</td>         
            <td style="width:120px;">Kode Produk</td>
        	<td style="width:300px;">Nama Produk</td>
            <td class="tr" style="width:100px;">Harga Rata2</td>
            <td class="tr" style="width:100px;">Qty Masuk</td>
            <td class="tr" style="width:100px;">Qty Keluar</td>
            <td class="tr" style="width:100px;">Stok Akhir</td>
 		   	<td class="tr" style="width:100px;">Jumlah</td>
	</tr>
	</thead>

<?php
$i = 1;
$total_in_qty = 0;
$total_out_qty = 0;
$sql = "SELECT * FROM mst_product WHERE 1 $filter_query ORDER BY pm_name";
$dcon = $conn->query($sql);
while($rs = $dcon->fetch_assoc()){
	$pm_autoid = $rs['pm_autoid'];
	


	
	
	$sqln = "SELECT SUM(log_inqty) as qty, SUM(log_inqty * log_price) as subtotal FROM invlog  WHERE pm_autoid = '$pm_autoid'
			AND log_inqty > 0  AND month(log_refdate) <= '$month' AND  year(log_refdate) <= '$tahun'";
	$sql_con = $conn->query($sqln) or die($sqln);
	$rsn = $sql_con->fetch_assoc();
	
	$subtotal 		= $rsn['subtotal'];
	$in_qty 		= $rsn['qty'];
	
	if($in_qty==0){
	    
	    $avg_price = 0;
	}else{
	$avg_price 		= $subtotal / $in_qty;    
	    
	}
	if(is_nan($avg_price)){$avg_price = 0;}


	$sqlz = "SELECT SUM(log_outqty) as qty FROM invlog  WHERE pm_autoid = '$pm_autoid'
			AND log_outqty > 0  AND month(log_refdate) <= '$month' AND  year(log_refdate) <= '$tahun'";
	$sqlz_con = $conn->query($sqlz) or die($sqlz);
	$rsnz = $sqlz_con->fetch_assoc();
	
	$out_qty 		= $rsnz['qty'];
	
	$stok_akhir = $in_qty - $out_qty;
	$total = $stok_akhir * $avg_price;
	
?> 
	<tr>
    	<td><?php echo $i?></td>
        <td><?php echo $rs['pm_code']?></td>
        <td><?php echo $rs['pm_name']?></td>

        <td class="tr"><?php echo round($avg_price,2)?></td>
        <td class="tr"><?php echo round( $in_qty,2)?></td>
        <td class="tr"><?php echo round($out_qty,2)?></td>
        <td class="tr"><?php echo round($stok_akhir,2)?></td>
        <td class="tr"><?php echo round($total,2)?></td>
	</tr>
<?php 
$i = $i + 1;
$total_in_qty = $total_in_qty + $in_qty;
$total_out_qty = $total_out_qty + $out_qty;
$stotal = $stotal + $total;

} ?>
	<tr class="bold">
    	<td class="tr" colspan="7">TOTAL</td>

	      <td class="tr"><?php echo round($stotal,2)?></td>
    </tr>
</table>
</div>
</body>

</html>