<?php
include("../include/session.php");
include("../include/db.php");
	
	
	if (isset($_GET['new_button']) )
	   {
		 $action='orderWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='viewOrders.php?OrderNoText='.$_SESSION['orderNo'] ;
	  }
	
	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	$modDate=date('Y-m-d');
	$active="1";
		
	$complete = "0";
	$orderNo = $_SESSION['orderNo'];
	$CustNo = $_SESSION['CustNo'];
	$client = $_SESSION['CustomerName'];
	$date = $_SESSION['OrderDate'];
	$delDate = $_SESSION['DeliveryDate'];
	$delCol =  $_SESSION['delCol'];
	$value = $_SESSION['RemarksText'];
	
	$deliverTo = $_SESSION['DeliverTo'];
	$address = $_SESSION['AddressText'];
	$place = $_SESSION['PlaceText'];
	$contact = $_SESSION['CellText'];
	
	
	
	$compl = 1;
	$canceled=0;
		
	
	$created_by = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	$i=0;
	$l=$_SESSION['line_no'];
	
	  While ($i < $l ) { 
	  
	if ($_SESSION['QuantityText'.$i]>0) { 
	
	$productId = $_SESSION['ProductSelect'.$i];
	$quantity = $_SESSION['QuantityText'.$i];
	$price = $_SESSION['PriceText'.$i];
	$reserved = $_SESSION['QuantityText'.$i];
		
		
	$custQuery = "INSERT INTO nam_orders (CustNo, client, orderNo, complete, date, delDate,  delCol, value, productId, quantity, price, reserved, canceled  ) VALUES ('".$CustNo."' ,'".$client."','".$orderNo."' , '".$compl."' , '".$date."','".$delDate."', '".$delCol."','".$value."', '".$productId."','".$quantity."' ,'".$price."' , '".$reserved."', '".$canceled."')";
	execsql($custQuery);
	
	}
	$i++;
	}
	
	$pallets = $_SESSION['PalletsText'];
	if ($pallets>0) {
		$quantity = $pallets;
		$productId = '10000';
		$price = $_SESSION['PalletsPriceText'];
		$reserved = $pallets;
		
		$custQuery = "INSERT INTO nam_orders (CustNo, client, orderNo, complete, date, delDate,  delCol, value, productId, quantity, price, reserved, canceled  ) VALUES ('".$CustNo."' ,'".$client."','".$orderNo."' , '".$compl."' , '".$date."','".$delDate."', '".$delCol."','".$value."', '".$productId."','".$quantity."' ,'".$price."' , '".$reserved."', '".$canceled."')";
	execsql($custQuery);
	
	}
	
	$deliveryQuery = "INSERT INTO nam_delivery_address (orderNo, deliverTo, address, place,   contact) VALUES ('" . $orderNo . "','" . $deliverTo . "', '" . $address . "','" . $place . "', '" . $contact . "' ) ";
	
	execsql($deliveryQuery); 
	
	
	$actionQuery = "INSERT INTO nam_action_tracking (entry_date, user, action, details) VALUES ('".$entryDate."' ,'".$created_by."','New order' ,'".$orderNo."' )";
	execsql($actionQuery);
	
?>
<?php
    $_SESSION['orderNo'] ="";
     $_SESSION['CustNo'] = "";
     $_SESSION['CustomerName'] = "";
	 $_SESSION['DeliverTo'] = "";
	 $_SESSION['AddressText'] = "";
	 $_SESSION['PlaceText'] = "";
	 $_SESSION['CellText'] = "";
	 $_SESSION['delCol']="";
	 $_SESSION['OrderDate']= date('Y-m-d');
	 $_SESSION['DelDate'] = "";
	 $_SESSION['DeliveryDate'] = date('Y-m-d');
	 $_SESSION['line_no'] = "0";
	 $_SESSION['ProductSelect'] = "";
	 $_SESSION['ProductText'] = "";
	 $_SESSION['QuantityText'] = "";
	 $_SESSION['PriceText'] = "";
	 $_SESSION['TotalText'] ="";
	 $_SESSION['PalletsText'] = "";
	 $_SESSION['PalletsPriceText'] = "";
	 
	 $_SESSION['RemarksText'] = "";
?>
<script language="javascript" type="text/javascript">
        <!--
		  window.location="<?php echo $action ?> ";
        -->
       </script>

