<?php
include("../include/session.php");

	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	
	
	$mm = date("m");
	$yy = date("y");
	
	$user_id = $_SESSION['username'];
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	$read_date = date("Y-m-d");	
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$id_query = "SELECT quote_no  FROM $table_name ORDER BY quote_id DESC LIMIT 1 ";
	$id_result = mySQL_query($id_query);
	$id_num = mysql_num_rows($id_result);
	if ($id_num==0) {
		$quote_no = $yy.$mm.'000';
	}
	else {
		$quote_no = mysql_result($id_result, 0, "quote_no");
	}
	
	
	if ( substr($quote_no, 2, 2)!=$mm ) {
	  $number="000";
	}
	else {
	  $number = substr($quote_no, 4, 3);
	}
	
	$quote_no = $yy.$mm.$number+1;
	


	$sub_total2 = $_GET['sub_total2'];
	$discount = $_GET['discount'];
	$transport = $_GET['transport'];
	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$quotation_query = "INSERT INTO $table_name (`quote_no`, `cust_id`, `type`, `status`, `seller`, `quote_type`, `quotation_date`, `company_contact`, `contact_phone`, `project_no`,  `project_place`, `project_description`, `value`, `quote_discount`, `quote_vat`, `transport`) VALUES ('".$quote_no."', '".trim($_SESSION['id'])."', '".trim($_SESSION['type'])."', 'quoted', '".trim($_SESSION['seller'])."', 'Cash sale', '".trim($_SESSION['quotation_date'])."', '".trim($_SESSION['comp_contact'])."', '".trim($_SESSION['contact_phone'])."',  '".trim($_SESSION['project_no'])."', '".trim($_SESSION['project_place'])."', '".trim($_SESSION['description'])."', '".$sub_total2."', '".$discount."',   '".$_SESSION['vat']."', '".$transport."' )";
	execsql($quotation_query);

	
	$table_name = $_SESSION['db_prefix']."_customer";
$customer_query = "UPDATE $table_name SET contact_1 = '".trim($_SESSION['comp_contact'])."', cell_1 = '".trim($_SESSION['contact_phone'])."' WHERE cust_id = '".trim($_SESSION['id'])."' ";
execsql($customer_query);

	
$table_name3 = $_SESSION['db_prefix']."_cash_quotation_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);

$total_plate_area = 0;
$i=0;
while ($i<$items_num) {
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "price");
   $price_2 = mysql_result($items_result, $i, "price_2");
   $sub_category = mysql_result($items_result, $i, "sub_category");
   
   	$table_name = $_SESSION['db_prefix']."_store_items";
	$item_query = "SELECT item_name, depth, width, class, area, length, price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
	$item_result = mySQL_query($item_query);
	$class = mysql_result($item_result, 0, "class");
   	$area = mysql_result($item_result, 0, "area");
	$length = mysql_result($item_result, 0, "length");
	$item_name = mysql_result($item_result, 0, "item_name"); 
    $price = mysql_result($item_result, 0, "price"); 
	$depth = mysql_result($item_result, 0, "depth");
	$width = mysql_result($item_result, 0, "width");
	
	
 
   $table_name4 = $_SESSION['db_prefix']."_cash_job_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (quote_no, store_item_id, truss_item, quantity, price, price_2) VALUES ('".$quote_no."',  '".$store_item_id."', '".$sub_category."', '".$quantity."', '".$price."', '".$price_2."' ) ";
  execsql($insert_item_query);
  
  if ($sub_category=='Timber') {
	    $total_length = ($length/1000) * $quantity;
		$length_price = $price / ($length/1000);
		$timber_name = $depth.'x'.$width.' ('.$class.')';
	    $table_name = $_SESSION['db_prefix']."_captured_timber";
		$timber_query = "INSERT INTO $table_name (cap_job, cap_timber_group, cap_timber_name, cap_timber, cap_timber_length, prereserved, cap_depth, cap_width, cap_class, cap_timber_price) VALUES ('".$quote_no."', 'Cash sale',  '".$item_name."', '".$timber_name."', '".$total_length."', '".$total_length."', '".$depth."', '".$width."', '".$class."', '".$length_price."' ) ";
		execsql($timber_query);
		
		//$table_name = $_SESSION['db_prefix']."_authorised_timber";
//		$auth_timber_query = "INSERT INTO $table_name (job_no, auth_timber_group, auth_timber_name, store_item_id, de_authorised ) VALUES ('".$quote_no."', 'Cash sale',  '".$item_name."', '".$timber_name."', '".$store_item_id."',  ) ";
//		execsql($auth_timber_query);
  }
  
  
  if  (($sub_category=='Mitek') AND ($class=='Plate')) {
	    $plate_area = $quantity * $area;
		$total_plate_area = $total_plate_area + $plate_area;	    
  }
  
  if ($sub_category=='Mitek') {
	    $table_name = $_SESSION['db_prefix']."_captured_mitek";
		$mitek_query = "INSERT INTO $table_name (cap_job, cap_job_type, cap_item_id, cap_quantity, cap_price) VALUES ('".$quote_no."', 'Cash sale',  '".$store_item_id."', '".$quantity."', '".$price."' ) ";
		execsql($mitek_query);
  }
  
  if ($sub_category=='Sundries') {
	    $table_name = $_SESSION['db_prefix']."_captured_sundries";
		$sundries_query = "INSERT INTO $table_name (cap_job, cap_job_type, cap_item_id, cap_quantity, cap_price) VALUES ('".$quote_no."', 'Cash sale',  '".$store_item_id."', '".$quantity."', '".$price."' ) ";
		execsql($sundries_query);
  }

$i++;
}

$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$cash_sale_query = "INSERT INTO $table_name (job_no, cust_id, date, project_no, project_place, project_description, plate_area,  value, sales_discount, sales_vat, transport, captured,  job_type, sales_rep) VALUES ('".$quote_no."', '".trim($_SESSION['id'])."', '".trim($_SESSION['quotation_date'])."', '".trim($_SESSION['project_no'])."', '".trim($_SESSION['project_place'])."', '".trim($_SESSION['description'])."', '".$total_plate_area."', '".$sub_total2."', '".$discount."',   '".$_SESSION['vat']."', '".$transport."', 'yes', 'Cash sale', '".$_SESSION['seller']."') ";
execsql($cash_sale_query);	


	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '52', '".$quote_no."' ,'".$_SESSION['id']."','', '' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	//include('../include/quotation_alert.php');
	
	$table_name = $_SESSION['db_prefix']."_cash_quotation_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."'   "; 
	execsql($delete_query);
	
	 $_SESSION['company_name'] ="";
	 $_SESSION['name'] ="";
	 $_SESSION['id'] = "";
     $_SESSION['type'] ="";
	 $_SESSION['sort'] ="";
     $_SESSION['contact'] = "";
	 $_SESSION['cell'] = "";
	 $_SESSION['bag_box'] = "";
	 $_SESSION['box_number'] = "";
	 $_SESSION['post_place'] = "";
	 $_SESSION['phone'] = "";
	 $_SESSION['fax'] = "";
	 $_SESSION['email'] = "";
	 $_SESSION['reference'] = "";
	 $_SESSION['comp_contact'] = "";
	 $_SESSION['contact_phone'] = "";
	 $_SESSION['project_no'] = "";
	 $_SESSION['project_place'] = "";
	 $_SESSION['description'] = "";
	 $_SESSION['seller'] = "";
	 $_SESSION['quote_type']  = "";
	 
	 $_SESSION['quote_date'] = date('Y-m-d');
	 $_SESSION['deliver_date'] = date('Y-m-d');
	 
	
	 
	 	
    if (isset($_GET['new_button']) )
	   {
		 $action='newQuoteWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		 $action='quotation.php?authorise=0&quote_no='.$quote_no ;
	  }
	 
?>

<script language="javascript" type="text/javascript">
        
		 window.location="<?php echo $action ?> ";
        
       </script>



