<?
include "inc/functions.php";

if(!$_SESSION['logged']){
	header("Location: login/");
}
	$meniu='add';
	$step=1;
		if(isset($_GET['step'])){
			$step=$_GET['step'];
		}
		
		if(isset($_POST['step1'])){

		if($_POST['client_name']==''){
			$err['client_name']='error';
		}else{
			$_SESSION['order']['client_name']=$_POST['client_name'];
			$_SESSION['order']['client_email']=$_POST['client_email'];
			$_SESSION['order']['client_phone']=$_POST['client_phone'];
			$_SESSION['order']['client_address']=$_POST['client_address'];
			$_SESSION['order']['client_location']=$_POST['client_location'];
			$_SESSION['order']['payment_method']=$_POST['payment_method'];
			$_SESSION['order']['invoice_date']=($_POST['actualdate']/1000);
			$_SESSION['order']['invoice_no']=$_POST['invoice_no'];
			header("Location: add.php?step=2");
		}
		}		
		
		if(isset($_POST['step2'])){
			mysql_query("insert into invoices(invoice_no, invoice_date, client_name, client_email, client_phone, client_address, client_location, payment_method,vat, total, currency,date) values('".$_SESSION['order']['invoice_no']."','".$_SESSION['order']['invoice_date']."','".$_SESSION['order']['client_name']."','".$_SESSION['order']['client_email']."','".$_SESSION['order']['client_phone']."','".$_SESSION['order']['client_address']."','".$_SESSION['order']['client_location']."','".$_SESSION['order']['payment_method']."','".$cfg['vat']."','".$_SESSION['total_order']."','".$cfg['currency']."','".time()."')")or die(mysql_error()."1");
				$id=mysql_insert_id();
				
			if($_SESSION['order']['invoice_no']==$cfg['invoice_no']){	
				mysql_query("update config set value='".($cfg['invoice_no']+1)."' where name='invoice_no'")or die(mysql_error());
			}
			
			foreach($_SESSION['products'] as $tag=>$val){
				mysql_query("insert into products(title,description,qty,price,taxes,invoice) values('".$val['title']."','".$val['description']."','".$val['qty']."','".$val['price']."','".$val['taxes']."','".$id."')")or die(mysql_error()."2");
			}
			
			unset($_SESSION['products'],$_SESSION['order']);
			$_SESSION['invoice']=$id;
			header("Location: add.php?step=3");
		}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
	<title>New Invoice | Pro Invoice Maker</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script language="javascript" type="text/javascript" src="js/jquery-1.9.1.js"></script>
	<script language="javascript" type="text/javascript" src="js/script.js"></script>
	<script language="javascript" type="text/javascript" src="js/auto.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery-ui-1.10.3.custom.min.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.tipsy.js"></script>	
	<link href="favicon.ico" rel="shortcut icon"/>	
	<link rel="stylesheet" media="screen" href="style/style.css" /> 
	<link rel="stylesheet" media="screen" href="style/tipsy.css" /> 
	<link rel="stylesheet" media="screen" href="style/hot-sneaks/jquery-ui-1.10.3.custom.min.css" /> 
	
	<link href="favicon.ico" rel="shortcut icon"/>		
	
 </head>
 <body>
	<div id='container'>
	<?include "inc/header.php"?>
		<div class='title'>New Invoice</div>
		<div id='step'>
			<table align='center'>
				<tr>
					<td class='step <?if($step==1){?>current<?}elseif($step>1){?>done<?}?>'>Setup Client</td>
					<td class='step <?if($step==2){?>current<?}elseif($step>2){?>done<?}?>'>Add Products</td>
					<td class='step <?if($step==3){?>current<?}elseif($step>3){?>done<?}?>'>Save/Export</td>
				</tr>
			</table>
		</div>
		<div id='content'>
			<?
			if($step==1){include "inc/step1.php";}
			if($step==2){include "inc/step2.php";}
			if($step==3){include "inc/step3.php";}
			?>
		</div>
	</div>
	<?include "inc/footer.php"?>
 </body>
 </html>