<?php
session_start();
include('config/koneksi2.php');
require_once("function.php"); 
		if(!empty($_POST['order'])){
			if($_SESSION['user_akses'] == 'dealer')
			{
			$pemesan= $_SESSION['nama_toko'];
			$alamat= $_SESSION['alamat_toko'].' '.$_SESSION['kota'].' '.$_SESSION['propinsi'];
			$email= $_SESSION['email'];
			$username= $_SESSION['username'];
			$ketbun=$_POST['ketbun'];
			}
			else
			{
			$pemesan= $_POST['pemesan'];
			$sql=mysql_query("select * from toko where kode=SUBSTRING('$pemesan',-9)");
			$hasil=mysql_fetch_array($sql);
			$alamat= $hasil['alamat'].' '.$hasil['kota'].' '.$hasil['propinsi'];
			$ket=$_POST['ket'];
			$email= $_SESSION['email'];
			$ketbun=$_POST['ketbun'];
			$username= $_SESSION['username'];
			}
			
		$LastID="BS-".FormatNoTrans(getLastTrans()); //Untuk kombinasi dengan huruf 
        foreach($_SESSION as $name => $value){
					if($value > 0){
						if(substr($name, 0, 5) == 'cart_'){
							$id = substr($name, 5, (strlen($name)-5));
							$get = mysql_query('SELECT * FROM bs_stok WHERE id='.mysql_real_escape_string((int)$id));
							$id_order = mysql_real_escape_string((int)$id);
						
							//$sql=mysql_query("INSERT into bs_order(kode_transaksi,nama,deskripsi,stok,retail,dealer,distributor,date,kode_ax,size_ax,colour_ax,config_ax,kategori,brand,tipe) Select distinct nama,deskripsi,stok,retail,dealer,distributor,NOW(),kode_ax,size_ax,colour_ax,config_ax,kategori,brand,tipe FROM bs_stok WHERE id='$id_order' ");
							
							while ($row=mysql_fetch_array($get)){
							if($row[kode_diskon] == '0' or $row[kode_diskon] == '')
							{
							    
							$id_stok = $row['id'];
							$nama = $row['nama'];
							$kode_ax = $row['kode_ax'];
							$config_ax = $row['config_ax'];
							$size_ax = $row['size_ax'];
							$colour_ax = $row['colour_ax'];
							$kategori = $row['kategori'];
							$brand = $row['brand'];
							$tipe = $row['tipe'];
							$retail = $row['retail'];
							$sub_retail = $row['retail'] * $value;
							$region=$_SESSION['region'];
							$merk = $row['merk'];
        
                            $hargato=mysql_query("select harga from harga where kode='$kode_ax' and size='$size_ax' and color='$colour_ax' and config='$config_ax' and region='$region'");
                            $har=mysql_fetch_array($hargato);
                            
                            if($region !="")
                            {
							$dealer = $har['harga'];
                            }
                            else
                            {
                            $dealer = $row['dealer'];
                            }
							
							    if($_SESSION['user_akses'] == 'dealer')
								{
									$userp=$_SESSION['username'];
								}
								else
								{
									$pemesan= $_POST['pemesan'];
									$sql=mysql_query("select * from tab_user where SUBSTRING(user_nama,-9)=SUBSTRING('$pemesan',-9)");
									$hasil=mysql_fetch_array($sql);
									$userp=$hasil['username'];
								}

                            $prom=mysql_query("select * from inthismonth where id_produk='$id' and qty > 0 and  id_produk NOT IN (select id_produk from userp where user='$userp')");
							$cek=mysql_num_rows($prom);
							if($cek > 0)
							{
								$d=mysql_fetch_array($prom);
								$dis1= (($dealer * $d[disc])/100);
								$dis3= $dealer - $dis1;
								$t= number_format($dealer * $value);
								//$sub='<del>'.$t.'</del>';
								$sub_dealer = ($dis3 * 1) + ($dealer * ($value - 1));
								$in=mysql_query("insert into userp(id_produk,user,date) VALUES('$id','$userp',NOW())");
								$up=mysql_query("update inthismonth set qty=qty-1 where id_produk='$id'");
							}
							else
							{
								$sub_dealer = $dealer * $value;
							}							
							$sql=mysql_query("INSERT into bs_order(kode_transaksi,pemesan,alamat,nama,jum_order,total_retail,retail,dealer,after_diskon,total_dealer,date,kode_ax,size_ax,colour_ax,config_ax,kategori,brand,tipe,sales,id_stok,keterangan,ketbun,bundling,mobile,merk)
							VALUES('$LastID','$pemesan','$alamat','$nama','$value','$sub_retail','$retail','$dealer','$dealer','$sub_dealer',NOW(),'$kode_ax','$size_ax','$colour_ax','$config_ax','$kategori','$brand','$tipe','$_SESSION[username]','$id_stok','$ket','$ketbun','$bun','apps','$merk')");
							if($sql)
							{
							 echo'<p>sukses dikirim</p>';
							 unset($_SESSION['cart_'.$id_order]);
							}
							else
							{
							echo'<p>gagal dikirim</p>';
							}
							}
							else
							{
							$kode_diskon=$row[kode_diskon];
							$sql_diskon="select * from diskon where kode_diskon='$kode_diskon'";
							$hasil_diskon=mysql_query($sql_diskon);
							$diskon=mysql_fetch_array($hasil_diskon);
							
							$id_stok = $row['id'];
							$nama = $row['nama'];
							$kode_ax = $row['kode_ax'];
							$config_ax = $row['config_ax'];
							$size_ax = $row['size_ax'];
							$colour_ax = $row['colour_ax'];
							$kategori = $row['kategori'];
							$brand = $row['brand'];
							$tipe = $row['tipe'];
							$retail = $row['retail'];
							$sub_retail = $row['retail'] * $value;
							$merk = $row['merk'];
							$hargato=mysql_query("select harga from harga where kode='$kode_ax' and size='$size_ax' and color='$colour_ax' and config='$config_ax' and region='$region'");
                            $har=mysql_fetch_array($hargato);
                            if($region !="")
                            {
                             $diskon1= (($har[harga] * $diskon[diskon])/100);
							$diskon3= $har[harga] - $diskon1;
							$dealer11= $har['harga'];
							$dealer = $diskon3;
							$sub_dealer = $diskon3 * $value;   
                            }
                            else
                            {
							$diskon1= (($row[dealer] * $diskon[diskon])/100);
							$diskon3= $row[dealer] - $diskon1;
							$dealer11= $row['dealer'];
							$dealer = $diskon3;
							$sub_dealer = $diskon3 * $value;
                            }
							
							$sql=mysql_query("INSERT into bs_order(kode_transaksi,pemesan,alamat,nama,jum_order,total_retail,retail,dealer,after_diskon,total_dealer,date,kode_ax,size_ax,colour_ax,config_ax,kategori,brand,tipe,sales,id_stok,keterangan,ketbun,bundling,mobile,merk)
							VALUES('$LastID','$pemesan','$alamat','$nama','$value','$sub_retail','$retail','$dealer11','$dealer','$sub_dealer',NOW(),'$kode_ax','$size_ax','$colour_ax','$config_ax','$kategori','$brand','$tipe','$_SESSION[username]','$id_stok','$ket','$ketbun','$bun','apps','$merk')");
							if($sql)
							{
							 echo'<p>sukses dikirim</p>';
							 unset($_SESSION['cart_'.$id_order]);
							}
							
							else
							{
							echo'<p>gagal dikirim</p>';
							}
							}
						}
					}
		}
		}
		function get_Last_kode_Trans() 
		{ 
		$querycount="SELECT kode_transaksi AS LastID FROM bs_order where sales='$_SESSION[username]' order by id DESC limit 1"; 
		$result=mysql_query($querycount) or die(mysql_error()); 
		$row=mysql_fetch_array($result, MYSQL_ASSOC); 
		
		return $row['LastID']; 
		}
	$LastID1=get_Last_kode_Trans();
	$id1=$LastID1;
	//$to ="it.agus@bikershop.biz";
	$to ="bsis.order@bikershop.biz";
	//$to ="adm.marketing@indonesiabikeworks.com";
	//$to ="sales@bikershop.biz";
	//$to ="$email";
	$subject = "$id1 - $pemesan - $alamat";
    $myquery="select * from  bs_order where kode_transaksi='$id1'";
    $daftarsiswa=mysql_query($myquery) or die (mysql_error());
    $laporan="<table width=\"100%\" border=\"1\" align=\"center\" cellpadding=\"3\" cellspacing=\"0\">";
    $laporan .="<tr>";
    $laporan .="<td>Kode Transaksi</td><td>Kode AX</td><td>Nama Barang</td><td>Size AX</td><td>Colour AX</td><td>Config AX</td><td>Jumalah Order</td><td>Harga Retail</td><td>Total Retail</td><td>Harga Dealer</td><td>After Diskon</td><td>Total Dealer</td>";
    $laporan .="</tr>";
	
    while($dataku=mysql_fetch_object($daftarsiswa))
    {
        $laporan .="<tr>";
        $laporan .="<td>$dataku->kode_transaksi</td><td>$dataku->kode_ax</td><td>$dataku->nama</td><td>$dataku->size_ax</td><td>$dataku->colour_ax</td><td>$dataku->config_ax</td><td>$dataku->jum_order</td><td>$dataku->retail</td><td>$dataku->total_retail</td><td>$dataku->dealer</td><td>$dataku->after_diskon</td><td>$dataku->total_dealer</td>";
        $laporan .="</tr>";
    }
	$laporan .="</table>";
	
	/*
	$laporan .="<p>";
	$laporan .=" Order By: </br>";
	$laporan .="$username, </br>";
	$laporan .="$pemesan, </br>";
	$laporan .="$alamat, </br>";
	$laporan .="$email </br>";
   	$laporan .="</p>";
	*/
	
	$laporan .="<table>";
	$laporan .="<tr>";
	$laporan .="<td colspan=\"3\">Order by:</td>";
	$laporan .="</tr>";
	$laporan .="<tr>";
	$laporan .="<td>Customer</td><td>:</td><td>$username</td>";
	$laporan .="</tr>";
	$laporan .="<tr>";
	$laporan .="<td>Nama toko</td><td>:</td><td>$pemesan</td>";
	$laporan .="</tr>";
	$laporan .="<tr>";
	$laporan .="<td>Alamat</td><td>:</td><td>$alamat</td>";
	$laporan .="</tr>";
	$laporan .="<tr>";
	$laporan .="<td>Email</td><td>:</td><td>$email</td>";
	$laporan .="</tr>";
	$laporan .="<tr>";
	$laporan .="<td>HP</td><td>:</td><td>$hp</td>";
	$laporan .="</tr>";
	$laporan .="<tr>";
	$laporan .="<td>Keterangan</td><td>:</td><td>$ket</td>";
	$laporan .="</tr>";
	$laporan .="</table>";
	
	// Always set content-type when sending HTML email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
		
		// More headers
		//$headers .= 'From: <staf.it@indonesiabikeworks.com>' . "\r\n";
		$headers .= "From: $email\r\n";
		
		//$headers .= 'Cc: <agus.ibwbs@gmail.com>' . "\r\n";
		$headers .= "Cc: $email\r\n";
		
		/*//mail($to,$subject,$laporan,$headers);
		//echo "<script language='javascript'>alert('Email telah terkirim');</script>";*/
		
		$kirim=@mail($to,$subject,$laporan,$headers);
		/*if ($kirim) 
		{
        $sql_2 =mysql_query("insert into bs_order_report(kode_transaksi,alamat,pemesan,nama,jum_order,total_retail,retail,dealer,total_dealer,distributor,kode_ax,size_ax,colour_ax,config_ax,kategori,brand,tipe,sales,id_stok)
			SELECT kode_transaksi,alamat,pemesan,nama,jum_order,total_retail,retail,dealer,total_dealer,distributor,kode_ax,size_ax,colour_ax,config_ax,kategori,brand,tipe,sales,id_stok FROM bs_order where kode_transaksi='$id'");
			foreach($_SESSION as $name => $value){
					if($value > 0){
						if(substr($name, 0, 5) == 'cart_'){
							$id = substr($name, 5, (strlen($name)-5));
							$get = mysql_query('SELECT * FROM bs_stok WHERE id='.mysql_real_escape_string((int)$id));
							$id_order = mysql_real_escape_string((int)$id);
							unset($_SESSION['cart_'.$id_order]);
						}
					}
		}*/
		}
		?>