<?php 

include 'inc/conx.php';
$id =  $_POST['id'];
$item_code = $_POST['item_code'];
$item_unit = $_POST['item_unit'];
$item_name = $_POST['item_name'];
$item_price = $_POST['item_price'];
$item_cost = $_POST['item_cost'];
$item_qty = $_POST['item_qty'];
$item_dispr = $_POST['item_dispr'];
$item_disrs = $_POST['item_disrs'];
$item_kg = $_POST['item_kg'];
$totalqty = $item_kg * $item_qty;
if(isset($_POST['kaat'])){
	$kaat = $_POST['kaat'];
	$totalqty = ($item_qty * $item_kg) - $kaat;

}


$item_total  = $item_price * $item_qty;
if ($item_dispr > 0) {
	$item_total  = $item_total - (($item_price * $item_dispr)/100) * $item_qty;
}
if ($item_disrs > 0) {
	$item_total  = $item_total - ($item_disrs * $item_qty);
}
$update = $db->query("UPDATE customerinvoice SET item_code = '$item_code', item_name ='$item_name', qty='$item_qty', purchase = '$item_cost', sell = '$item_price', discount_pr = '$item_dispr', discount_rs = '$item_disrs', total = '$item_total', totalqty = '$totalqty' WHERE id = '$id' ");
if(isset($_POST['kaat'])){
	$update = $db->query("UPDATE customerinvoice SET kaat = '$kaat' WHERE id = '$id' ");
	echo '<td>'.$item_code.'</td>
			<td>'.$item_name.'</td>
			<td>'.$item_price.'</td>
			<td>'.$item_qty.'</td>
			<td>'.$item_kg.'</td>
			<td>'.$kaat.'</td>
			<td>'.$item_disrs.'</td>
			<td>'.$totalqty.'</td>
			<td>'.$item_total.'</td>
			<td style="vertical-align: top; text-align: center;"> <button type="button" class="btn btn-info editItem" value="'.$id.'">Edit</button> | <button type="button" class="btn btn-danger deleteItem" value="'.$id.'">Delete</button></td>';
} else{
			echo '<td>'.$item_code.'</td>
			<td>'.$item_name.'</td>
			<td>'.$item_price.'</td>
			<td>'.$item_qty.'</td>
			<td>'.$item_kg.'</td>
			<td>'.$item_disrs.'</td>
			<td>'.$item_total.'</td>
			<td style="vertical-align: top; text-align: center;"> <button type="button" class="btn btn-info editItem" value="'.$id.'">Edit</button> | <button type="button" class="btn btn-danger deleteItem" value="'.$id.'">Delete</button></td>';
}
//$item_kit = 0;
$countQuery = $db->query("SELECT count(*) totalCount FROM itemsetup WHERE uei = '".$item_code."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
	if ($totalCount < 1) {
		//$item_kit = 2;
		$select = $db->query("SELECT * FROM kitsetup WHERE kit_code = '$item_code'");
		while($row  = $select->fetchArray()){

			$sel = $db->query("SELECT * FROM itemsetup WHERE item_name = '".$row['kit_items']."'");
			$fsel = $sel->fetchArray();
			$update = $db->query("UPDATE stock SET itemDescription = '".$row['kit_items']."', qty='".$row['kit_qty']*$item_qty."', rate = '".$fsel['purchase_price']."', last_up = NULL WHERE unique_id = '$id' ");

			
		}
	}else{
		$q = $db->query("SELECT * FROM itemsetup WHERE item_name = '".$item_name."'");
		$fetchRm = $q->fetchArray();
		$new_item_qty = $item_qty;
		if($item_unit == 'ctn'){
			$new_item_qty = $fetchRm['ctn'] * $item_qty;
		}elseif($item_unit == 'dzn'){
			$new_item_qty = $fetchRm['dzn'] * $item_qty;
		}elseif($item_unit == 'pcs'){
			$new_item_qty = $fetchRm['pcs'] * $item_qty;
		}
		//$item_kit = 1;
		$update = $db->query("UPDATE stock SET itemDescription = '".$item_name."', unit ='$item_unit', qty= '$totalqty', rate = '".$item_price."', last_up = NULL WHERE unique_id = '$id' ");

	}

//$update = $db->query("UPDATE customerinvoice SET item_kit = '$item_kit' WHERE id = '".$frow['id']."'");
?>