<?php
include("config/connection.php");
include("config/session.php");

$jumlahtiket = $_POST['jumlahtiket'];
$id_operator = $_SESSION['id'];
$act = $_POST['act'];
$id_jadwal = $_POST['id_jadwal'];
$nama = $_POST['nama'];
$nama = str_replace("'", "", $nama);
$no_identitas = $_POST['no_identitas'];
$jenisangkutan = $_POST['jenisangkutan'];
$penumpang = $_POST['penumpang'];
$kendaraan = $_POST['kendaraan'];
	if($jenisangkutan == 1){
		$harga = $penumpang;	
	}else{
		$harga = $kendaraan;	
	}
		$sql ="select * from tb_detil_angkutan_def where harga = '".$harga."'";
		$hasil = mysql_query($sql) or die("Kesalahan Pada SQL");
		mysql_close($test);
		
		$row = mysql_fetch_object($hasil);
			$id_angkutan = $row->id_angkutan;

//echo $id_angkutan;	
//BUAT NO TIKET
for ($i = 1; $i <= $jumlahtiket; $i++) {
	include("config/connection.php");
	$tgl = date("Y-m-d");
	$sql1 = "select count(id) from tb_tiket where date(tanggal) = '".$tgl."'";
	$query1 =mysql_query($sql1);
	$row = mysql_fetch_row($query1);
	$jumlah = $row[0];
	$bk=$jumlah + 1;
	$dp = date("dmy");
	$notiket = $dp.$bk;
	$namact = '';
	
	if($jumlahtiket > 1){
		$namact = $nama." Tiket ke -".$i;
	}else{
		$namact = $nama;
	}

//SIMPAN DATA
include("config/connection.php");
if ($act == 1){
	
	$sql="insert into tb_tiket (id_jadwal, id_angkutan, harga, nama, no_identitas, tanggal, no_tiket, id_operator) values ('".$id_jadwal."', '".$id_angkutan."', '".$harga."', '".$namact."', '".$no_identitas."', now(), '".$notiket."', '".$id_operator."')";
	if (!mysql_query($sql,$test)){
  	die('Error: ' . mysql_error());
	}
	
include("config/connection.php");
	
	$sql="update tb_jadwal set Sisa = Sisa -1 where Id_Jadwal = '".$id_jadwal."'";
	if (!mysql_query($sql,$test)){
  	die('Error: ' . mysql_error());
	}
}
?>
<script>window.location = "<?php echo $base?>tiket.php?id=<?php echo $notiket?>&jumlahtiket=<?php echo $jumlahtiket?>", "_blank"</script>

<?php	

}
