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

$str=" ";
$error=0;

$edate=date("Y-m-d");
$userid=$_SESSION['rm_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']);
$vat_invoice = isset($_POST['vat_invoice']) ? 1 : 0;
$deliver=trim($_POST['deliver']);
$sup_code= trim($_POST['sup_code']);
$po= trim($_POST['po']);




///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     Validation    !!!!!!!!!!
if (empty($gdate)){
	$gdate=0;
	$error=1;
	$error_des='Date Error';
}
else {

		if (empty($loc_id)){
			$loc_id=0;
			$error=1;
			$error_des='Location Error';
		}
}

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


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

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

else {


mysqli_begin_transaction($con);

/////!!!!!!!!!!!!!!!!!!  sequence   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$querydoc=mysqli_query($con,"select * from esequence 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 einv_temp_header(invd_id,invd_date,cus_id,loc_id,invd_ref,note,discount,inv_type,invtype_id,emp_id,vat_inv,delivery_id,sup_code,po) 
			 VALUES($inv_no,'$gdate',$cus_id,$loc_id,'$invref','$note',0,1,$invtype,$emp_id,$vat_invoice,$deliver,'$sup_code','$po' )  ")or die(mysqli_error($con)))
	        {

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

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

						}  
	        }






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











	

?>