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

$outlet 	= $_POST['outlet'];
$pm_autoid 		= $_POST['pm_autoid'];

$pagename = "Lap Stok";

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


$sqlc = "SELECT SUM(a.log_inqty) as inqty, SUM(a.log_outqty) as outqty, b.pm_name, a.pm_autoid, a.outlet_id FROM invlog a, mst_product b 
						WHERE a.pm_autoid = b.pm_autoid AND (a.log_refdate) < '$fdate' AND a.outlet_id = '$outlet' GROUP BY a.pm_autoid ORDER BY b.pm_name DESC";
		$sqlcon = $conn->query($sqlc) or die($sqlc);
		while($rs = $sqlcon->fetch_assoc()){
			$_pm_autoid = $rs['pm_autoid'];
			$_stok_awal[$_pm_autoid] = ($rs['inqty'] - $rs['outqty']);
		}

$sqlc = "SELECT SUM(a.log_inqty) as inqty, SUM(a.log_outqty) as outqty, b.pm_name, a.pm_autoid, a.outlet_id FROM invlog a, mst_product b 
						WHERE a.pm_autoid = b.pm_autoid AND (a.log_refdate between '$fdate' AND '$tdate') AND a.outlet_id = '$outlet' GROUP BY a.pm_autoid ORDER BY b.pm_name DESC";
$sqlcon = $conn->query($sqlc) or die($sqlc);
while($rs = $sqlcon->fetch_assoc()){
			$_pm_autoid = $rs['pm_autoid'];
			$_stok_keluar[$_pm_autoid] = $rs['outqty'];
			$_stok_masuk[$_pm_autoid] = $rs['inqty'];
	}
	$url = "index-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?> - <?php echo $_outlet_name[$outlet]?></title>
    <?php include('../../common/script_css.php');?>

</head>
<body class="print_body">
<?php include('../../sys/navprint.php')?>
<div class="noprint">
<div class="tbprint">
        <input type="button" value="Print" class="btn btn-primary btn-sm" onclick="window.print()"/>
        <input type="button" value="Back" class="btn btn-danger btn-sm" onclick="window.location.href='index-2'"/>
</div>
<br>
<br>
<br>
</div>

<div>
    <table class="table" width="1600" border="1" bordercolor="#CCCCCC" style="font-size:12px">
    <thead>
    	<tr class="bold">
        	<td colspan="8">
            Laporan Stok - <?php echo $_outlet_name[$outlet]?> - -
            <br>
            <?php echo iddate2($fdate)?> -  <?php echo iddate($tdate)?> 
            </td>
        </tr>
                <tr class="bold">
                	<td class="tl">Nama Item</td>
                    <td class="tc">Kategori</td>
                    <td class="tr">Stok Awal</td>     
                    <td class="tr">Stok Masuk</td>                
                    <td class="tr">Stok Keluar</td>                
                    <td class="tr">Sisa Stok</td>                

                </tr>
                </thead>
                <?php
				$sqlkat = "SELECT * FROM mst_partclass WHERE pt_autoid = 1  ORDER BY pc_autoid ASC";
				$dcon_kat = $conn->query($sqlkat) or die($sqlkat);
				while($rsk = $dcon_kat->fetch_assoc()){				
				$pc_autoid = $rsk['pc_autoid'];
				$kategori  = $rsk['pc_name'];
				$z = 0;
				$t_stok_awal = 0;
				$t_stok_masuk = 0;
				$t_stok_keluar = 0;
				$t_stok_akhir = 0;
			   	$sqlc = "SELECT * FROM mst_product WHERE pc_autoid = '$pc_autoid' ORDER BY pc_autoid, pm_name";
				$sqlcon = $conn->query($sqlc) or die($sqlc);
				while($rs = $sqlcon->fetch_assoc()){
					$pm_autoid = $rs['pm_autoid'];
					$stok_masuk = $_stok_masuk[$pm_autoid];
					$stok_keluar = $_stok_keluar[$pm_autoid]; 

					$stok_awal = $_stok_awal[$pm_autoid];
					
					if(!$stok_awal){$stok_awal = 0;}
					if(!$stok_masuk){$stok_masuk = 0;}
					if(!$stok_keluar){$stok_keluar = 0;}		
					$sisa_stok = ($stok_awal + $stok_masuk) - $stok_keluar;
					
				?>
                <tr>
                	<td class="tl"><?php echo $rs['pm_name']?></td>
                    <td class="tc"><?php echo $kategori?></td>
                    <td class="tr"><?php echo number_format($stok_awal,2)?></td>
                    <td class="tr"><?php echo number_format($stok_masuk,2)?></td>
                    <td class="tr"><?php echo number_format($stok_keluar,2)?></td>
                    <td class="tr"><?php echo number_format($sisa_stok,2)?></td>
                </tr>
                <?php 
				$z = $z+1;
				$t_stok_masuk = $t_stok_masuk + $stok_masuk;
				$t_stok_keluar = $t_stok_keluar + $stok_keluar;
				$t_stok_akhir = $t_stok_akhir + $sisa_stok;
				$t_stok_awal = $t_stok_awal + $stok_awal;
				}?>
                <?php ?>
				<tr class="bold" style="background-color:#BBFDC7">
                	<td class="tr" colspan="2">TOTAL <?php echo $rsk['pc_name']?></td>
                    <td class="tr"><?php echo number_format($t_stok_awal,2)?></td>
                    <td class="tr"><?php echo number_format($t_stok_masuk,2)?></td>
                    <td class="tr"><?php echo number_format($t_stok_keluar,2)?></td>
                    <td class="tr"><?php echo number_format($t_stok_akhir,2)?></td>
                </tr>				
				<?php } ?>

                </table>
    
</div>
</body>

</html>