<?php 

include 'inc/conx.php';
$saleType = $_POST['saleType'];
$paymentType = $_POST['paymentType'];
$invoice = $_POST['invoice'];
$date = $_POST['date'];

$change_date = explode("/", $date);
$show_date = $change_date[1]."/".$change_date[0]."/".$change_date[2];
$php_date = $change_date[2]."/".$change_date[0]."/".$change_date[1];

$name = $_POST['name'];
$ID = 0;
	$countQuery = $db->query("SELECT count(*) totalCount FROM customer_setup WHERE customer_name = '".$name."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
	if ($totalCount < 1) {
	
	$countQuery = $db->query("SELECT count(*) totalCount FROM supplier_setup WHERE supplier_name = '".$name."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
		if ($totalCount < 1) {
			$ID = 0;
	 	}else{
	 		$supplier = $db->query("SELECT * FROM supplier_setup WHERE supplier_name = '".$name."'");
	 		$fetchdate = $supplier->fetchArray();
	 		$ID = "s".$fetchdate['id'];
	 	}
	 }else{
	 	$customer = $db->query("SELECT * FROM customer_setup WHERE customer_name = '".$name."'");
 		$fetchdate = $customer->fetchArray();
 		$ID = "c".$fetchdate['id'];
	 }

$bilty = $_POST['bilty'];
$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'];
$kaat = $_POST['kaat'];
$kg = $_POST['kg'];
//$totalqty = ($item_qty * $kg) - $kaat;
$totalqty = $item_qty;
$item_total = $totalqty * $item_price;
// $totalweight = ($item_qty * $totalqty) - $kaat;
// $item_total  = $item_price * $totalweight;


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);
}




$insert = $db->query("INSERT INTO customerinvoice (unit, bilty, payment_type, sale_type, user_name, invoiceNo, name, name_word, item_code, item_name, qty, purchase, sell, discount_pr, discount_rs, total, date, php_date, show_date,kaat,totalqty,kg) VALUES ('$item_unit', '$bilty', '$paymentType', '$saleType', '".$_SESSION['name']."', '$invoice', '$ID', '$name', '$item_code', '$item_name', '$item_qty', '$item_cost', '$item_price', '$item_dispr', '$item_disrs', '$item_total', '$date', '$php_date', '$show_date', '$kaat', '$totalqty', '$kg')");

$select = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '$invoice' ORDER BY id DESC LIMIT 1");
$frow  = $select->fetchArray();

echo '<tr>
			<td>'.$frow['item_code'].'</td>
			<td>'.$frow['item_name'].'</td>
			<td>'.$frow['sell'].'</td>
			<td>'.$frow['qty'].'</td>
			<td>'.$frow['discount_pr'].'</td>
			<td>'.$frow['total'].'</td>
			<td style="vertical-align: top; text-align: center;"><button type="button" class="btn btn-danger deleteItem" value="'.$frow['id'].'">Delete</button></td>
		  </tr>';

$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();
			$insert = $db->query("INSERT INTO stock (date, php_date, show_date, invoiceNo, type, supplierName, itemDescription, qty, rate, unique_id, item_kit) VALUES ('$date', '$php_date', '$show_date', '$invoice', '$saleType', '$ID', '".$row['kit_items']."', '".$row['kit_qty']*$item_qty."', '".$fsel['purchase_price']."', '".$frow['id']."', '$item_kit')");
		}
	}else{
		$q = $db->query("SELECT * FROM itemsetup WHERE item_name = '".$item_name."'");
		$fetchRm = $q->fetchArray();
		
		$item_kit = 1;
		$insert = $db->query("INSERT INTO stock (date, php_date, show_date, invoiceNo, type, supplierName, itemDescription,  unit, qty, rate, unique_id, item_kit) VALUES ('$date', '$php_date', '$show_date', '$invoice', '$saleType', '$ID', '$item_name', '$item_unit',  '$totalqty', '".$frow['sell']."', '".$frow['id']."', '$item_kit')");

		$newStock = $fetchRm['stock'] + $totalqty;
		
		$previousPricenQty = $fetchRm['purchase_price'] * $fetchRm['stock'];
		$currentPricenQty = $item_price * $totalqty;
		$newPrice = ($previousPricenQty + $currentPricenQty) / ($fetchRm['stock'] + $totalqty);

		$update = $db->query("UPDATE itemsetup SET stock = '$newStock', purchase_price = '$newPrice' WHERE item_name = '$item_name'");
	}

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

die();

?>