<?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/Khartoum');
    $entry_date = date("Y-m-d H:i:s",time()-3600); 
	$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;
	
//$_SESSION['pallets'] = 0;
if ($_SESSION['quote_type']=='Tiles & accessories') {
	$sub_total = $_GET['sub_total'];
	$discount = $_GET['discount'];
	$transport = $_GET['transport'];
	$pallets_price = $_GET['pallets_price'];
	$value = $sub_total;	
	$table_name = $_SESSION['db_prefix']."_quotations";
	$quotation_query = "INSERT INTO $table_name (`quote_no`, `cust_id`, `cust_type`, `status`, `seller`, `quote_type`, `quotation_date`, `check_date`, `company_contact`, `contact_phone`, `project_no`,  `project_place`, `project_description`, `quote_value`, `quote_discount`, `quote_vat`, `pallets`, `pallets_price`, `transport`, `deliver_date`, `branch`) VALUES ('$quote_no', '".trim($_SESSION['id'])."', '".trim($_SESSION['type'])."', 'quoted', '".trim($_SESSION['seller'])."', 'tiles', '".trim($_SESSION['receive_date'])."', '".trim($_SESSION['check_date'])."', '".trim($_SESSION['comp_contact'])."', '".trim($_SESSION['contact_phone'])."', '".trim($_SESSION['project_no'])."', '".trim($_SESSION['project_place'])."', '".trim($_SESSION['description'])."', '".$sub_total."', '".$discount."',   '".$_SESSION['vat']."', '".$_SESSION['pallets']."', '".$pallets_price."', '".trim($transport)."', '".trim($_SESSION['deliver_date'])."', '".$_SESSION['userbranch']."' )";
	execsql($quotation_query);
}

if ($_SESSION['quote_type']=='Truss installation') { 
	$discount = $_GET['discount'];
	$transport = $_GET['transport'];
    $table_name = $_SESSION['db_prefix']."_quotations"; 
    $quotation_query = "INSERT INTO $table_name (`quote_no`, `cust_id`, `cust_type`, `status`, `seller`, `quote_type`, `cplx`, `storey`, `quotation_date`, `check_date`, `company_contact`, `contact_phone`, `project_no`,  `project_place`, `project_description`, `quote_value`, `quote_discount`, `quote_vat`, `transport`, `deliver_date`, `branch`) VALUES ('$quote_no', '".trim($_SESSION['id'])."', '".trim($_SESSION['type'])."', 'quoted', '".trim($_SESSION['seller'])."', 'trusses', '".$_SESSION['cplx']."', '".$_SESSION['double']."', '".trim($_SESSION['receive_date'])."', '".trim($_SESSION['check_date'])."', '".trim($_SESSION['comp_contact'])."', '".trim($_SESSION['contact_phone'])."', '".trim($_SESSION['project_no'])."', '".trim($_SESSION['project_place'])."', '".trim($_SESSION['description'])."', '".trim($_SESSION['sub_total2'])."', '".$discount."',   '".$_SESSION['vat']."', '".$transport."', '".trim($_SESSION['deliver_date'])."', '".$_SESSION['userbranch']."' )";
	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);


if ($_SESSION['quote_type']=='Tiles & accessories') {		
$table_name3 = $_SESSION['db_prefix']."_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);

$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");
   $purchase_price = mysql_result($items_result, $i, "purchase_price");
   $sub_category = mysql_result($items_result, $i, "sub_category");

 
   $table_name4 = $_SESSION['db_prefix']."_quotation_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (quote_no, store_item_id, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  '".$store_item_id."', '".$sub_category."', '".$quantity."', '".$price."', '".$purchase_price."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$i++;
}	
}

if ($_SESSION['quote_type']=='Truss installation') { 
$value = $_SESSION['sub_total2'];
$table_name4 = $_SESSION['db_prefix']."_quotation_lines";
 
$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'truss_M2', '".$_SESSION['quantity_1']."', '".$_SESSION['unit_1']."', '".$_SESSION['total_1']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'truss_M1', '".$_SESSION['quantity_2']."', '".$_SESSION['unit_2']."', '".$_SESSION['total_2']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'truss_No', '".$_SESSION['quantity_3']."', '".$_SESSION['unit_3']."', '".$_SESSION['total_3']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'tiles_M2', '".$_SESSION['quantity_4']."', '".$_SESSION['unit_4']."', '".$_SESSION['total_4']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'tiles_M1', '".$_SESSION['quantity_5']."', '".$_SESSION['unit_5']."', '".$_SESSION['total_5']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'tiles_M3', '".$_SESSION['quantity_6']."', '".$_SESSION['unit_6']."', '".$_SESSION['total_6']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'inspection', '".$_SESSION['quantity_7']."', '".$_SESSION['unit_7']."', '".$_SESSION['total_7']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, additional_text, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'add_1', '".$_SESSION['description_8']."', '".$_SESSION['quantity_8']."', '".$_SESSION['unit_8']."', '".$_SESSION['total_8']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_query);

$insert_item_query = "INSERT INTO $table_name4 (quote_no, truss_item, additional_text, quantity, price, purchase_price, branch ) VALUES ('".$quote_no."',  'add_2', '".$_SESSION['description_9']."', '".$_SESSION['quantity_9']."', '".$_SESSION['unit_9']."', '".$_SESSION['total_9']."', '".$_SESSION['userbranch']."' ) ";
execsql($insert_item_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']."', '40', '".$quote_no."' ,'".$_SESSION['id']."','', '".$_SESSION['userbranch']."' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	include('../include/quotation_alert.php');
	
	$table_name = $_SESSION['db_prefix']."_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');
	 
	 $_SESSION['cplx'] = '1';
	 $_SESSION['double'] = 'no';
	 
	 $_SESSION['quantity_1'] = '';
	 $_SESSION['unit_1'] = '';
	 $_SESSION['total_1'] = '';
	 $_SESSION['quantity_2'] = '';
	 $_SESSION['unit_2'] = '';
	 $_SESSION['total_2'] = '';
	 $_SESSION['quantity_3'] = '';
	 $_SESSION['unit_3'] = '';
	 $_SESSION['total_3'] = '';
	 $_SESSION['quantity_4'] = '';
	 $_SESSION['unit_4'] = '';
	 $_SESSION['total_4'] = '';
	 $_SESSION['quantity_5'] = '';
	 $_SESSION['unit_5'] = '';
	 $_SESSION['total_5'] = '';
	 $_SESSION['quantity_6'] = '';
	 $_SESSION['unit_6'] = '';
	 $_SESSION['total_6'] = '';
	 $_SESSION['quantity_7'] = '';
	 $_SESSION['unit_7'] = '';
	 $_SESSION['total_7'] = '';
	 $_SESSION['description_8'] = '';
	 $_SESSION['quantity_8'] = '';
	 $_SESSION['unit_8'] = '';
	 $_SESSION['total_8'] = '';
	 $_SESSION['description_9'] = ''; 
	 $_SESSION['quantity_9'] = '';
	 $_SESSION['unit_9'] = '';
	 $_SESSION['total_9'] = '';
	 
	 
	 	
    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>



