<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include('../include/session.php');
include ("../navigation/header.php");
include ("../navigation/production_sidebar.php");

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


	$d="";
	$s = $_GET['s'];
	$p = $_GET['p'];
	if ($_SESSION['p'] >= 4) {$d=1;}
	$error = $_SESSION['error'];
	$_SESSION['p']=$p;
	$_SESSION['s']=$s;
	
	
	if (isset($_GET['prev_button']) )
	   {
		 $_SESSION['p']= $_SESSION['p']-1;
	   }
		 
	if (isset($_GET['next_button']) )
	  {
		$_SESSION['p']=$_SESSION['p']+1;
	  }
	
		
	
if ($s==1) {
		
  $_SESSION['type'] = $_GET['type_text'];	
  $_SESSION['sort'] = $_GET['sort_group'];	
  
  
  if ($_SESSION['type']=="client") {
	
$string = $_GET['comp_name_text2'];
	
	function extract_numbers($string)
	{
	preg_match_all('/([\d]+)/', $string, $match);
 
	return $match[0];
	}

	$numbers_array = extract_numbers($string);
	
	$_SESSION['id']=$numbers_array[0];	
	
	$table_name = $_SESSION['db_prefix']."_customer";
	$cust_query="SELECT account_type, comp_name, contact_1, cell_1, name_2, title FROM $table_name WHERE cust_id = '".$_SESSION['id']."' ";
	$cust_result = mySQL_query($cust_query) or die ("Error in query:   $query . * ". mySQL_error());
	$c0 = mysql_result($cust_result, 0, "account_type");
   
    if ($c0=="corporate") { $_SESSION['company_name'] = mysql_result($cust_result, 0, "comp_name"); }
	 else { 
	 $_SESSION['company_name'] = mysql_result($cust_result, 0, "title")." ".mysql_result($cust_result, 0, "name_2")." ".mysql_result($cust_result, 0, "comp_name"); }
	 
	$_SESSION['comp_contact'] = mysql_result($cust_result, 0, "contact_1");
	$_SESSION['contact_phone'] = mysql_result($cust_result, 0, "cell_1");
	$num = mysql_num_rows($cust_result);
	
	if ($num==0)
	{ 
	$_SESSION['error']=1;
	$_SESSION['p']=1;
	$_SESSION['type'] = "";	
	$_SESSION['sort'] = "";	
	?>
    <script language="javascript" type="text/javascript">
        <!--
		needToConfirm = false;
	  //   window.location="quoteRequestWizbase.php?s=0&p=1";
        -->
     </script>
	<?php
	}
	
		$_SESSION['comp_contact'] = mysql_result($cust_result, 0, "contact_1");
		$_SESSION['contact_phone'] = mysql_result($cust_result, 0, "cell_1");
	
		
		
}

if ($_SESSION['type']=="contact") {
	
	$comp_name = trim($_GET['comp_name_text']);
	$name_2 = $_GET['name_2_text'];
   	$title = $_GET['title_select'];

	$table_name = $_SESSION['db_prefix']."_customer";
	$cust_query="SELECT cust_id FROM $table_name WHERE comp_name = '".$comp_name."' AND name_2 = '".$name_2."' ";

	
    $cust_result = mySQL_query($cust_query) or die ("Error in query:   $query . * ". mySQL_error());
	$num = mysql_num_rows($cust_result);
	$_SESSION['company_name'] = $name_2.' '.$comp_name;

		
	if (($num!=0))
	{  
	$_SESSION['error']=2;
	$_SESSION['p']=1;
	$_SESSION['type'] = "";	
	$_SESSION['sort'] = "";	
	$_SESSION['company_name'] = $title.' '.$name_2.' '.$comp_name;

	
	}
	
	else {
	$status = "new";
	$account_type = $_GET['sort_group'];
	$name = trim($_GET['comp_name_text']);
	$name_2 = trim($_GET['name_2_text']);
	$title = $_GET['title_select'];

	
	$bag_box = trim($_GET['bag_box_select']);
	$box_number = trim($_GET['box_number_text']);
	$post_place = trim($_GET['post_place_select']);
	$phone = trim($_GET['phone_text']);
	$fax = trim($_GET['fax_text']);
	$email = trim($_GET['email_text']);
	$_SESSION['name'] = $name;
	
	$table_name = $_SESSION['db_prefix']."_customer";
	
	
    $contact_query = "INSERT INTO $table_name (`account_type`,`status`, `comp_name`, `name_2`, `title`, `bag_box`, `box_no`, `postal_place`, `phone`, `fax`, `email_1`) VALUES ('".$account_type."', '".$status."',  '".$name."', '".$name_2."','".$title."',  '".$bag_box."', '".$box_number."', '".$post_place."', '".$phone."', '".$fax."', '".$email."') ";

   	execsql($contact_query);
	
	$table_name = $_SESSION['db_prefix']."_customer";
    		$id_query = "SELECT  cust_id  FROM $table_name WHERE comp_name  = '".$name."' AND name_2  = '".$name_2."'  ";

	
	$id_result = mySQL_query($id_query);
    $_SESSION['id'] = mysql_result($id_result, 0, "cust_id");
	
	$table_name = $_SESSION['db_prefix']."_tracking_sales";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '56', '' ,'".$_SESSION['id']."','','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	
}
	}
	
	}
		
	if($s==2) {
		$_SESSION['receive_date']  = $_GET['receive_date'];
		$_SESSION['value'] = $_GET['value_text'];
		$_SESSION['comp_contact']  = $_GET['comp_contact_text'];
		$_SESSION['contact_phone']  = $_GET['contact_phone_text'];
		$_SESSION['project_no']  = $_GET['project_no_text'];
		$_SESSION['project_area']  = $_GET['project_area_select'];
		$_SESSION['project_place']  = $_GET['project_place_select'];
		$_SESSION['description']  = $_GET['description_text'];
		$_SESSION['seller']  = $_GET['seller_select'];
		
		
		$table_name = $_SESSION['db_prefix']."_mworks_order_components_temp";
		$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
		execsql($delete_query);
		
		$line_num2 = $_GET['line_num2'];
		$i=0;
		while ($i<$line_num2) {
		if (isset($_GET['line_checkbox7'.$i])) {
		$item_name = $_GET['item_text7'.$i];

		$quantity2 = $_GET['quantity_text7'.$i];
		$notes2 = $_GET['notes_text7'.$i];

		if(isset($_GET['line_checkbox7'.$i]) ) {
		$table_name = $_SESSION['db_prefix']."_mworks_order_components_temp";
		$components_query = "INSERT INTO $table_name ( component, quantity,  notes, user_name) VALUES ('".$item_name."', '".$quantity2."', '".$notes2."', '".$_SESSION['username']."' ) ";
		execsql($components_query);
		}
		};
		$i++;
		}
	};
	
	
			
	?>

<!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />
<link rel="stylesheet" href="../window/dhtmlwindow.css" type="text/css" />
<link rel="stylesheet" href="../window/modal.css" type="text/css" />
<link href="../css/wait.css" rel="stylesheet" type="text/css">

<!--jscript-->
<script type="text/javascript" src="../window/dhtmlwindow.js"></script>
<script type="text/javascript" src="../window/modal.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>    
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>   
<script language="JavaScript" src="../jscript/lower_case.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/upper_case.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/capitalize_textbox.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script> 



<div class="main_frame">

<div class="page_head_frame">
  <label class="label_page_head">M-works order</label>&nbsp;&nbsp;<label class="label_page_head_2"><?php echo $_SESSION['company_name'] ?></label>
  </div>
<br />
     
      <table  align="left">
      <tr>
      <td >      
      <div class='wizard_paging_frame'>
       <table align="left">
      <tr>
      <td>
      <?php if ($_SESSION['p']==1) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;1</div>
      </div>
      <div class="active_step_label">
        Customer details</div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;1</div></div>
      <div class="de_active_step_label">
      Customer details</div>
      <?php } ?>
      </td>
      <td>
      <?php if ($_SESSION['p']==2) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;2</div></div>
      <div class="active_step_label">
      Details</div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;2</div></div>
      <div class="de_active_step_label">
      Details</div>
      <?php } ?>
     </td>
     <td>
      <?php if ($_SESSION['p']==3) { ?>
      <div class="active_step_frame"><div class="active_step_head_label">&nbsp;3</div></div>
      <div class="active_step_label">
      Finish</div>
      <?php } else { ?>
      <div class="de_active_step_frame"><div class="de_active_step_head_label">&nbsp;3</div></div>
      <div class="de_active_step_label">
      Finish</div>
      <?php } ?>
     </td>
        </tr>
      </table>
      </div>
      </td>
      </tr>
      <tr>
      <td><?php 
	if ($_SESSION['p']==1) { include("new_mworks_jobWiz1.php"); }
	if ($_SESSION['p']==2) { include("new_mworks_jobWiz2.php"); }
	if ($_SESSION['p']==3) { include("new_mworks_jobWiz5.php"); }
	
    ?></td>
    </tr>
    </table>
    </div>




<?php include ('../navigation/side_bar.php'); ?>