<?php 
session_start();
include('../includes/dbcon.php');

$str=" ";

$error=0;

$edate=date("Y-m-d");
$userid=$_SESSION['hrm_id'];

$loc_id= trim($_POST['loc_id']);
$gdate= trim($_POST['gdate']);
$invref= trim($_POST['invref']);
$cus_id= trim($_POST['cus_id']);
$note= trim($_POST['note']);
$invtype= trim($_POST['invtype']);
$emp_id= trim($_POST['emp_id']);
$r_id= trim($_POST['r_id']);
$t_id= trim($_POST['t_id']);

///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     Validation    !!!!!!!!!!
if (empty($gdate)){
	$gdate=0;
	$error=1;
	$error_des='Date Error';
	 echo "<script type='text/javascript'>alert('date');</script>";
}
else {

		if (empty($loc_id)){
			$loc_id=0;
			$error=1;
			$error_des='Location Error';
			 echo "<script type='text/javascript'>alert('location');</script>";
		}
}

	if (empty($cus_id)){   $cus_id=0;   $error=1;       }   
	if (empty($emp_id)){   $emp_id=0;   $error=1;       } 


///~~~~~~~~~~~~~~~~~~~~~~~~~~End of Validation ~~~~~~~~~~~~~~~~~~~

if ($error==1)  {  
	 echo "<script type='text/javascript'>alert('Please select The Item !');</script>";
	 echo "<script>document.location='inv_new_free.php'</script>";
}

else {


mysqli_begin_transaction($con);

/////!!!!!!!!!!!!!!!!!!  sequence   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$querydoc=mysqli_query($con,"select * from sequence where seq_id=5 ")or die(mysqli_error());
while($row=mysqli_fetch_array($querydoc)){  
$inv_no=$row['cno'];
}
///~~~~~~~~~~~~~~~~~~~~~~~~~~~   End of sequence ~~~~~~~~~~~~~~~~~~



		if ( mysqli_query($con,"INSERT INTO inv_temp_header(invd_id,invd_date,cus_id,loc_id,invd_ref,note,discount,inv_type,invtype_id,emp_id,r_id,t_id,bill_type) 
			 VALUES($inv_no,'$gdate',$cus_id,$loc_id,'$invref','$note',0,3,$invtype,$emp_id,$r_id,$t_id ,2)  ")or die(mysqli_error($con)))
	        {

	        	
	        			if ( mysqli_query($con,"update sequence set cno =cno + 1  where seq_id=5  ")or die(mysqli_error($con)))			
						{   

							mysqli_commit($con);
							echo "<script>document.location='inv_new1_free.php?inv_no=$inv_no '</script>"; 

						}  
	        }






}  ///// end of else  ( $error==1)  { 











	

?>