<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:550px;overflow-y:scroll">
<table class="table table-striped">
<?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 b.pm_name ASC";
	$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'];
?>
<tr>
	<td style="width:50%"><?php echo $pm_name?> <br>
    Rp. <?php echo number_format($posd_price)?>
    </td>
    <td style="width:30%">
<div class="input-group">
          <span class="input-group-btn">
              <button type="button" class="btn btn-light btn-number" data-type="minus" onclick="add_item('<?php echo $_pm_autoid?>','minus')" data-field="quant[1]">
                 -
              </button>
          </span>
          <input readonly name="quant[1]" id="row<?php echo $i?>" class="form-control input-number tc input-sm" value="<?php echo $posd_qty?>" min="0" max="10">
          <span class="input-group-btn">
              <button type="button" class="btn btn-light btn-number" data-type="plus"  onclick="add_item('<?php echo $_pm_autoid?>','plus')" data-field="quant[1]">
                  +
              </button>
          </span>
      </div>
    </td>
    <td style="width:20%" class="tr"><?php echo number_format($subtotal)?></td>
</tr>
<?php 
$gtotal = $gtotal + $subtotal;
$i = $i + 1;
} ?>
</table>
</div>
<div class="tr py-2"><h3>Total : Rp. <?php echo number_format($gtotal)?></h3></div>
<input type="hidden" name="gtotal" id="gtotal" value="<?php echo $gtotal?>">
<script>
$('#row'+<?php echo $i?>).focus();
</script>

