<script>

</script>
<?php
include('config.php');


$pos_autoid = $_POST['pos_autoid'];
$pm_autoid = $_POST['pm_autoid'];
$act = $_POST['act'];

if($act=="add"){

$sqlchk = "SELECT COUNT(*) as num FROM posdetail WHERE pos_autoid = '$pos_autoid' AND pm_autoid = '$pm_autoid'";
$conn_chk = $conn->query($sqlchk) or die($sqlchk);
$rschk = $conn_chk->fetch_assoc();
$chk_pm = $rschk['num'];



$sql_pm = "SELECT * FROM mst_product WHERE  pm_autoid = '$pm_autoid'";
$conn_pm = $conn->query($sql_pm) or die($sql_pm);
$rspm = $conn_pm->fetch_assoc();
$_pm_name 	= $conn->real_escape_string($rspm['pm_name']);
$_pm_sprice = $rspm['pm_sprice'];

if($chk_pm > 0){
	$sql_in = "UPDATE posdetail set posd_qty = posd_qty + 1 WHERE pos_autoid = '$pos_autoid' AND pm_autoid = '$pm_autoid'";
	}else{
	$sql_in = "INSERT INTO posdetail (pos_autoid,pm_autoid,pm_name,posd_price,posd_qty) 
		values ('$pos_autoid','$pm_autoid','$_pm_name','$_pm_sprice','1')";
	}
	$conn->query($sql_in) or die($sql_in);		
	
}

if($act=="plus"){
	$sql_in = "UPDATE posdetail set posd_qty = posd_qty + 1 WHERE pos_autoid = '$pos_autoid' AND pm_autoid = '$pm_autoid'";
	$conn->query($sql_in) or die($sql_in);		
	}
if($act=="minus"){
	$sql_in = "UPDATE posdetail set posd_qty = posd_qty - 1 WHERE pos_autoid = '$pos_autoid' AND pm_autoid = '$pm_autoid'";
	$conn->query($sql_in) or die($sql_in);		
	
	$conn->query("DELETE FROM posdetail where posd_qty = 0 AND pos_autoid = '$pos_autoid'");	
}
?>
<div style="height:400px;overflow-y:scroll;margin-left:-20px" class="pl-0">
<table class="table table-striped table-borderless text-light">
<?php	
	$gtotal = 0;
	$i = 0;
	$sql = "select * from posdetail a, mst_product b  WHERE a.pm_autoid = b.pm_autoid AND a.pos_autoid = '$pos_autoid' ORDER BY a.posd_autoid desc";
	$dcon 	= $conn->query($sql) or die($sql);
	while($rs 	= $dcon->fetch_array()){
		$pm_name = $rs['pm_name'];
		$posd_qty = $rs['posd_qty'];
		$posd_price = $rs['posd_price'];
		$subtotal = $posd_price * $posd_qty;
		$_pm_autoid = $rs['pm_autoid'];
		$posd_autoid = $rs['posd_autoid'];
?>
<tr id="span_item<?php echo $posd_autoid?>" onclick="change_qty('<?php echo $posd_autoid?>')">
	<td style="width:50%">

		<span id="item_name<?php echo $posd_autoid?>"><?php echo $pm_name?> </span>
        <br>
	    Rp. <?php echo number_format($posd_price)?>

    </td>
    <td class="tc" style="width:30%;">
    <span id="span_qty<?php echo $posd_autoid?>"><?php echo $posd_qty?></span>
	<input type="hidden" class="input tc" style="width:100%;" id="item_qty<?php echo $posd_autoid?>" value="<?php echo $posd_qty?>">
    </td>
    <td style="width:20%" class="tr">
        <span id="span_subtotal<?php echo $posd_autoid?>"><?php echo number_format($subtotal)?></span>
	</td>
</tr>
<?php 
$gtotal = $gtotal + $subtotal;
$i = $i + 1;
} ?>
</table>
</div>
<div class="tr py-2">
<h3>Total : Rp. <span id="span_total"><?php echo number_format($gtotal)?></span></h3></div>
<input type="hidden" name="gtotal" id="gtotal" value="<?php echo $gtotal?>">


