	<?php
	include ("component.php");
	
	$total_transaksi = 0;
	$produk = array();
	$sql3 = 
	" SELECT DISTINCT product_category ".
	" FROM kontak_dagang_header, kontak_dagang_transaksi ".
	" WHERE header_id = to_kontak_dagang_header ".
	" AND tgl_transaksi >= '".$date_start."' " .
	" AND tgl_transaksi <= '".$date_end."' " . $txt_remark . $txt_pic .
	" ORDER BY product_category ";
	if ($hasil3 = mysql_query($sql3)) {
		while ($data3 = mysql_fetch_array ($hasil3)) {
			$sql4 = 
			" SELECT DISTINCT to_negara ".
			" FROM kontak_dagang_header, kontak_dagang_transaksi, form_element_sub ".
			" WHERE header_id = to_kontak_dagang_header " .
			" AND product_category = elementSubID ".
			" AND product_category = ".$data3["product_category"].
			" AND tgl_transaksi >= '".$date_start."' " .
			" AND tgl_transaksi <= '".$date_end."'" . $txt_remark . $txt_pic .
			" ORDER BY to_negara ";
			//echo $sql4;
			if ($hasil4 = mysql_query($sql4)) {
				$total_produk = 0;
				$negara = array();
				$namaProduk = '';
				while ($data4 = mysql_fetch_array ($hasil4)) {
					$sql5 = 
					" SELECT * From kontak_dagang_header, kontak_dagang_transaksi, form_element_sub, form_element_value ".
					" WHERE header_id = to_kontak_dagang_header " .
					" AND valueID = to_negara AND product_category = elementSubID ".
					" AND valueID = ".$data4["to_negara"].
					" AND product_category = ".$data3["product_category"].
					" AND tgl_transaksi >= '".$date_start."' " .
					" AND tgl_transaksi <= '".$date_end."'" . $txt_remark . $txt_pic . $jn_trans .
					" ORDER BY to_negara ";
					if ($hasil5 = mysql_query($sql5)) {
						$total_negara = 0;$namaNegara = '';
						while ($data5 = mysql_fetch_array ($hasil5)) {
							$namaProduk   = $data5["elementSubDescription"];
							$namaNegara   = $data5["valueName"];
							$total_negara = $total_negara + $data5["estimasi_kontrak"];
						}
						if ($total_negara) {
							$total_produk = $total_produk + $total_negara; 
							$negara[] = array(
								"namaNegara" 	=> $namaNegara,	
								"total_negara" 	=> $total_negara
							);
						}
					}
				}
				if ($total_produk) {	
					foreach ($negara as $key => $value) {
						$produk[] = array(
							"nama_produk"  => $namaProduk,
							"total_produk" => $total_produk,
							"nama_negara"  => $value["namaNegara"],
							"total_negara" => $value["total_negara"]
						);
					}

					$total_transaksi = $total_transaksi + $total_produk;
				}
			}
		}
	}

	foreach ($produk as $key => $row) {
		$s_produk[$key] 	  = $row["nama_produk"];
		$s_total_produk[$key] = $row["total_produk"];
		$s_nama_negara[$key]  = $row["nama_negara"];
		$s_total_negara[$key] = $row["total_negara"];
	}
	
	if ($total_transaksi > 0)
	array_multisort($s_total_produk,SORT_DESC, $s_total_negara,SORT_DESC,  $produk);
	
	$total_persen = 100;
	$counter = 0;


	echo '
	<br>
	<div align="center">
		<strong>TRANSAKSI TEI 2018 BERDASARKAN KELOMPOK PRODUK DAN NEGARA TERBESAR</strong> <br>
		Periode: '.indonesian_date($date_start).' sampai dengan '.indonesian_date($date_end);
		if ($jn_trans) echo '<p><strong>Filter by Jenis Transaksi</strong><br> '.$jn_trans_name.'</p>';
		echo '
	</div> <br>
	<table width="100%" cellpadding="10" cellspacing="0" border="1" bgcolor="#FFFFFF">	
		<tr bgcolor="#DDDDDD">
			<td width="10%" align="center"><strong>No</strong></td>
			<td width="45%"><strong>Produk / Negara</strong></td>
			<td width="25%" align="right"><strong>Nilai (US $)</strong></td>
			<td align="right"><strong>Persentase (%)</strong></td>
		</tr>
		<tr bgcolor="#FFFFFF">
			<td colspan="2" align="right"><strong>Total</strong></td>
			<td align="right"><strong>';
				if ($export == 1) echo $total_transaksi; 
				else echo number_format($total_transaksi, 2);
			echo
			'</strong></td>
			<td align="right"><strong>';
				if ($export == 1) echo $total_persen;
				else echo number_format($total_persen, 2);
			echo
			'</strong></td>
		</tr>';
		$cek = '';
		$counter2 = 0;
		foreach ($produk as $key => $value) {
			if ($cek != $value["nama_produk"]) {
				$cek = $value["nama_produk"];
				$counter++;
				$counter2 = 0;
				if (isset($total_lainnya) && $total_lainnya > 0) {
					echo '
					<tr bgcolor="#FFFFFF">
						<td align="center">&nbsp;</td>
						<td><strong>Lainnya</strong></td>
						<td align="right"><strong>';
							if ($export == 1) echo $total_lainnya;
							else echo number_format($total_lainnya, 2);
							echo
						'</strong></td>
						<td align="right">&nbsp;</td>
					</tr>';
				}
				$total_lainnya = 0;
				echo '
				<tr bgcolor="#FFFFFF">
					<td align="center"><strong>'.$counter.'</strong></td>
					<td><strong>'.$value["nama_produk"].'</strong></td>
					<td align="right"><strong>';
						if ($export == 1) echo $value["total_produk"];
						else echo number_format($value["total_produk"], 2);
						echo '
					</strong></td>
					<td align="right">';
						$percenPernegara = round(($value["total_produk"]/$total_transaksi)*100, 2);
						//echo floor((250 / 434) * 100);  //Round down
						echo '<strong>';
							if ($export == 1) echo $percenPernegara;
							echo sprintf ("%.2f", $percenPernegara);
							echo 
						'</strong>';
						echo '	
					</td>
				</tr>';
				$counter2++;
				if ($counter2 <= 5) {
					echo '
					<tr bgcolor="#FFFFFF">
						<td align="center">&nbsp;</td>
						<td>'.$value["nama_negara"].'</td>
						<td align="right">';
							if ($export == 1) echo $value["total_negara"];
							else echo number_format($value["total_negara"], 2);  
							echo '
						</td>
						<td align="right">&nbsp;</td>
					</tr>';
				} else {
					$total_lainnya = $total_lainnya + $value["total_negara"];
				}
			} else {
				$counter2++;
				if ($counter2 <= 5) {
					echo '
					<tr bgcolor="#FFFFFF">
						<td align="center">&nbsp;</td>
						<td>'.$value["nama_negara"].'</td>
						<td align="right">';
							if ($export == 1) echo $value["total_negara"];
							else echo number_format($value["total_negara"], 2);
							echo '
						</td>
						<td align="right">&nbsp;</td>
					</tr>';
				} else {
					$total_lainnya = $total_lainnya + $value["total_negara"];
				}
			}
		}
		echo '
	</table>';
	?>