<?php
$total_transaksi = 0;
$produk = array();
//kel_produk_negara($total_transaksi, $produk);
__kel_produk_negara($total_transaksi, $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;
$total_lainnya = 0;

echo '
<p>'.$head_report_negara_kel_produk.'</p>
<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>';?>