<?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'];
//$item_total = $_POST['item_total'];

$item_total  = $item_price * $item_qty;
if ($item_dispr > 0) {
	$item_total  = $item_total - (($item_price * $item_dispr)/100) * $item_qty;
	//$item_disrs = (($item_price * $item_dispr)/100) * $item_qty;
	//$item_total=$item_total-$item_disrs;
}
if ($item_disrs > 0) {
	$item_total  = $item_total - ($item_disrs); // * $item_qty
}

$insert = $db->query("INSERT INTO quotations (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) 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')");

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

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

die();

?>