<?php
	include '../../inc/inc.koneksi.php';
	include '../../inc/fungsi_tanggal.php';

	$kode = $_GET['kode'];

	$text = "SELECT * FROM kunci";
	$sql  = mysql_query($text);
	$r	  = mysql_fetch_assoc($sql);
	$keyw = $r[keyword];
	
	$key1 = substr($keyw,0,1);
	$key2 = substr($keyw,1,1);
	$key3 = substr($keyw,2,1);
	$key4 = substr($keyw,3,1);
	$key5 = substr($keyw,4,1);
	$key6 = substr($keyw,5,1);
	$key7 = substr($keyw,6,1);
	$key8 = substr($keyw,7,1);
	$key9 = substr($keyw,8,1);
	$key0 = substr($keyw,9,1);
	
	$sql = "SELECT * FROM faktur_pb WHERE kode_pb='$kode' ORDER BY no_urut";
	$query = mysql_query($sql);
	$content = "<table width='410'>";
	
	while($r_data=mysql_fetch_array($query)){
		$unit  	= $r_data[jml_unit];
		$kodest	= $r_data[kode_st];
		$hbeli	= $r_data[hg_beli];
		
// --> buat konversi harga beli
		$nhb1	= substr($hbeli,0,1);
		$nhb2	= substr($hbeli,1,1);
		$nhb3	= substr($hbeli,2,1);
		$nhb4	= substr($hbeli,3,1);
		
		if($nhb1==1) $thb1=$key1; elseif($nhb1==2) $thb1=$key2;	elseif($nhb1==3) $thb1=$key3; elseif($nhb1==4) $thb1=$key4;
		elseif($nhb1==5) $thb1=$key5; elseif($nhb1==6) $thb1=$key6; elseif($nhb1==7) $thb1=$key7; elseif($nhb1==8) $thb1=$key8;
		elseif($nhb1==9) $thb1=$key9; else $thb1=$key0;
		
		if($nhb2==1) $thb2=$key1; elseif($nhb2==2) $thb2=$key2;	elseif($nhb2==3) $thb2=$key3; elseif($nhb2==4) $thb2=$key4;
		elseif($nhb2==5) $thb2=$key5; elseif($nhb2==6) $thb2=$key6; elseif($nhb2==7) $thb2=$key7; elseif($nhb2==8) $thb2=$key8;
		elseif($nhb2==9) $thb2=$key9; else $thb2=$key0;
		
		if($nhb3==1) $thb3=$key1; elseif($nhb3==2) $thb3=$key2;	elseif($nhb3==3) $thb3=$key3; elseif($nhb3==4) $thb3=$key4;
		elseif($nhb3==5) $thb3=$key5; elseif($nhb3==6) $thb3=$key6;	elseif($nhb3==7) $thb3=$key7; elseif($nhb3==8) $thb3=$key8;
		elseif($nhb3==9) $thb3=$key9; else $thb3=$key0;
		
		if($nhb4==1) $thb4=$key1; elseif($nhb4==2) $thb4=$key2;	elseif($nhb4==3) $thb4=$key3; elseif($nhb4==4) $thb4=$key4;
		elseif($nhb4==5) $thb4=$key5; elseif($nhb4==6) $thb4=$key6;	elseif($nhb4==7) $thb4=$key7; elseif($nhb4==8) $thb4=$key8;
		elseif($nhb4==9) $thb4=$key9; else $thb4=$key0;

		$sql1  = "SELECT * FROM stock WHERE kode_st='$kodest'";
		$query1= mysql_query($sql1);
		$data  = mysql_fetch_assoc($query1);
		
		$hjual	= $data[hg_jual];
		$hjual1 = $data[hg_jual1];
		$hjual2 = $data[hg_jual2];
		
		// --> buat konversi harga jual
		$nhj1	= substr($hjual,0,1);
		$nhj2	= substr($hjual,1,1);
		$nhj3	= substr($hjual,2,1);
		$nhj4	= substr($hjual,3,1);
		
		if($nhj1==1) $thj1=$key1; elseif($nhj1==2) $thj1=$key2;	elseif($nhj1==3) $thj1=$key3; elseif($nhj1==4) $thj1=$key4;
		elseif($nhj1==5) $thj1=$key5; elseif($nhj1==6) $thj1=$key6; elseif($nhj1==7) $thj1=$key7; elseif($nhj1==8) $thj1=$key8;
		elseif($nhj1==9) $thj1=$key9; else $thj1=$key0; 
		
		if($nhj2==1) $thj2=$key1; elseif($nhj2==2) $thj2=$key2;	elseif($nhj2==3) $thj2=$key3; elseif($nhj2==4) $thj2=$key4;
		elseif($nhj2==5) $thj2=$key5; elseif($nhj2==6) $thj2=$key6; elseif($nhj2==7) $thj2=$key7; elseif($nhj2==8) $thj2=$key8;
		elseif($nhj2==9) $thj2=$key9; else $thj2=$key0;
		
		if($nhj3==1) $thj3=$key1; elseif($nhj3==2) $thj3=$key2;	elseif($nhj3==3) $thj3=$key3; elseif($nhj3==4) $thj3=$key4;
		elseif($nhj3==5) $thj3=$key5; elseif($nhj3==6) $thj3=$key6;	elseif($nhj3==7) $thj3=$key7; elseif($nhj3==8) $thj3=$key8;
		elseif($nhj3==9) $thj3=$key9; else $thj3=$key0;
		
		if($nhj4==1) $thj4=$key1; elseif($nhj4==2) $thj4=$key2;	elseif($nhj4==3) $thj4=$key3; elseif($nhj4==4) $thj4=$key4;
		elseif($nhj4==5) $thj4=$key5; elseif($nhj4==6) $thj4=$key6;	elseif($nhj4==7) $thj4=$key7; elseif($nhj4==8) $thj4=$key8;
		elseif($nhj4==9) $thj4=$key9;	else $thj4=$key0;
		
// --> buat konversi harga jual1
		$nhj11	= substr($hjual1,0,1);
		$nhj12	= substr($hjual1,1,1);
		$nhj13	= substr($hjual1,2,1);
		$nhj14	= substr($hjual1,3,1);
		
		if($nhj11==1) $thj11=$key1; elseif($nhj11==2) $thj11=$key2;	elseif($nhj11==3) $thj11=$key3; elseif($nhj11==4) $thj11=$key4;
		elseif($nhj11==5) $thj11=$key5; elseif($nhj11==6) $thj11=$key6; elseif($nhj11==7) $thj11=$key7; elseif($nhj11==8) $thj11=$key8;
		elseif($nhj11==9) $thj11=$key9; else $thj11=$key0; 
		
		if($nhj12==1) $thj12=$key1; elseif($nhj12==2) $thj12=$key2;	elseif($nhj12==3) $thj12=$key3; elseif($nhj12==4) $thj12=$key4;
		elseif($nhj12==5) $thj12=$key5; elseif($nhj12==6) $thj12=$key6; elseif($nhj12==7) $thj12=$key7; elseif($nhj12==8) $thj12=$key8;
		elseif($nhj12==9) $thj12=$key9; else $thj12=$key0;
		
		if($nhj13==1) $thj13=$key1; elseif($nhj13==2) $thj13=$key2;	elseif($nhj13==3) $thj13=$key3; elseif($nhj13==4) $thj13=$key4;
		elseif($nhj13==5) $thj13=$key5; elseif($nhj13==6) $thj13=$key6;	elseif($nhj13==7) $thj13=$key7; elseif($nhj13==8) $thj13=$key8;
		elseif($nhj13==9) $thj13=$key9; else $thj13=$key0;
		
		if($nhj14==1) $thj14=$key1; elseif($nhj14==2) $thj14=$key2;	elseif($nhj14==3) $thj14=$key3; elseif($nhj14==4) $thj14=$key4;
		elseif($nhj14==5) $thj14=$key5; elseif($nhj14==6) $thj14=$key6;	elseif($nhj14==7) $thj14=$key7; elseif($nhj14==8) $thj14=$key8;
		elseif($nhj14==9) $thj14=$key9; else $thj14=$key0;

// --> buat konversi harga jual2
		$nhj21	= substr($hjual2,0,1);
		$nhj22	= substr($hjual2,1,1);
		$nhj23	= substr($hjual2,2,1);
		$nhj24	= substr($hjual2,3,1);
		
		if($nhj21==1) $thj21=$key1; elseif($nhj21==2) $thj21=$key2;	elseif($nhj21==3) $thj21=$key3; elseif($nhj21==4) $thj21=$key4;
		elseif($nhj21==5) $thj21=$key5; elseif($nhj21==6) $thj21=$key6; elseif($nhj21==7) $thj21=$key7; elseif($nhj21==8) $thj21=$key8;
		elseif($nhj21==9) $thj21=$key9; else $thj21=$key0; 
		
		if($nhj22==1) $thj22=$key1; elseif($nhj22==2) $thj22=$key2;	elseif($nhj22==3) $thj22=$key3; elseif($nhj22==4) $thj22=$key4;
		elseif($nhj22==5) $thj22=$key5; elseif($nhj22==6) $thj22=$key6; elseif($nhj22==7) $thj22=$key7; elseif($nhj22==8) $thj22=$key8;
		elseif($nhj22==9) $thj22=$key9; else $thj22=$key0;
		
		if($nhj23==1) $thj23=$key1; elseif($nhj23==2) $thj23=$key2;	elseif($nhj23==3) $thj23=$key3; elseif($nhj23==4) $thj23=$key4;
		elseif($nhj23==5) $thj23=$key5; elseif($nhj23==6) $thj23=$key6;	elseif($nhj23==7) $thj23=$key7; elseif($nhj23==8) $thj23=$key8;
		elseif($nhj23==9) $thj23=$key9; else $thj23=$key0;
		
		if($nhj24==1) $thj24=$key1; elseif($nhj24==2) $thj24=$key2;	elseif($nhj24==3) $thj24=$key3; elseif($nhj24==4) $thj24=$key4;
		elseif($nhj24==5) $thj24=$key5; elseif($nhj24==6) $thj24=$key6;	elseif($nhj24==7) $thj24=$key7; elseif($nhj24==8) $thj24=$key8;
		elseif($nhj24==9) $thj24=$key9; else $thj24=$key0;
		
		if($thb4=='.') $thb4='';
		if($thb3=='.') $thb3='';
		
		if($thj4=='.') $thj4='';
		if($thj3=='.') $thj3='';
		
		if($thj14=='.') $thj14='';
		if($thj13=='.') $thj13='';
		
		if($thj24=='.') $thj24='';
		if($thj23=='.') $thj23='';
		
		$keyhbeli = $thb1.$thb2.$thb3.$thb4;
		$keyhjual = $thj1.$thj2.$thj3.$thj4;
		$keyhjual1= $thj11.$thj12.$thj13.$thj14;
		$keyhjual2= $thj21.$thj22.$thj23.$thj24;

		if($unit>3){
			$content .="<tr>
					<td width='100' style='font:10px arial'>$r_data[nama_st]</td>
					<td width='30'style='font:10px arial'>$keyhbeli</td>
					<td width='10'></td>
					<td width='100' style='font:10px arial'>$r_data[nama_st]</td>
					<td width='30' style='font:10px arial'>$keyhbeli</td>
					<td width='10'></td>
					<td width='100' style='font:10px arial'>$r_data[nama_st]</td>
					<td width='30' style='font:10px arial'>$keyhbeli</td>
				</tr>
				<tr>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
					<td></td>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
					<td></td>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
				</tr>
				<tr>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
				</tr>
				<tr>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
				</tr>
				<br>
				
				<tr>
					<td style='font:10px arial'>$r_data[nama_st]</td>
					<td style='font:10px arial'>$keyhbeli</td>
					<td></td>
					<td style='font:10px arial'>$r_data[nama_st]</td>
					<td style='font:10px arial'>$keyhbeli</td>
					<td></td>
					<td style='font:10px arial'>$r_data[nama_st]</td>
					<td style='font:10px arial'>$keyhbeli</td>
				</tr>
				<tr>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
					<td></td>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
					<td></td>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
				</tr>
				<tr>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
				</tr>
				<tr>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
				</tr>
				<br>";
		}else{
			$content .= "<tr>
					<td style='font:10px arial'>$r_data[nama_st]</td>
					<td style='font:10px arial'>$keyhbeli</td>
					<td></td>
					<td style='font:10px arial'>$r_data[nama_st]</td>
					<td style='font:10px arial'>$keyhbeli</td>
					<td></td>
					<td style='font:10px arial'>$r_data[nama_st]</td>
					<td style='font:10px arial'>$keyhbeli</td>
				</tr>
				<tr>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
					<td></td>
					<td align='left' style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
					<td></td>
					<td style='font:24px barcod39'>*$r_data[barcode]*</td>
					<td style='font:10px arial'>$keyhjual</td>
				</tr>
				<tr>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_sup] - ".jin_date_str($r_data[tanggal])."</td>
					<td style='font:10px arial'>$keyhjual1</td>
				</tr>
				<tr>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
					<td></td>
					<td style='font:10px arial'>$r_data[kode_st] - $r_data[part_num]</td>
					<td style='font:10px arial'>$keyhjual2</td>
				</tr>
				<br>";
		}
	}
	$content .= "</table>";
	
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=label_harga.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $content;
?>