<?php
$total_transaksi = 0;
$negara = array();
__negara_kel_produk($total_transaksi, $negara);

foreach ($negara as $key => $row) {
	$s_negara[$key]  	  = $row["nama_negara"];
	$s_total_negara[$key] = $row["total_negara"];
	$s_nama_produk[$key]  = $row["nama_produk"];
	$s_total_produk[$key] = $row["total_produk"];
}

if ($total_transaksi > 0)
array_multisort($s_total_negara,SORT_DESC, $s_total_produk,SORT_DESC,  $negara);
$total_persen = 100;
$counter = 0;

echo '
<p>'.$head_report_negara_kel_produk.'</p>
<table width="100%" cellpadding="10" cellspacing="0" border="1">	
	<tr bgcolor="#DDDDDD">
		<td width="10%" align="center"><strong>No</strong></td>
		<td width="45%"><strong>Negara / Produk</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 = '';
	foreach ($negara as $key => $value) {
		if ($cek != $value["nama_negara"]) {
			$cek = $value["nama_negara"];
			$counter++;
			echo '
			<tr bgcolor="#FFFFFF">
				<td align="center"><strong>'.$counter.'</strong></td>
				<td><strong>'.$value["nama_negara"].'</strong></td>
				<td align="right"><strong>';
					if ($export == 1) echo $value["total_negara"];
					else echo number_format($value["total_negara"], 2);
					echo '</strong>
				</td>
				<td align="right"><strong>';
					$percenPernegara = round(($value["total_negara"]/$total_transaksi)*100, 2);
					echo floor(($value["total_negara"]/$total_transaksi)*100);  //Round down
					if ($export == 1) echo $percenPernegara;
					else echo sprintf ("%.2f", $percenPernegara);
					echo '</strong>	
				</td>
			</tr>';
		}
		echo '
		<tr bgcolor="#FFFFFF">
			<td align="center">&nbsp;</td>
			<td>'.$value["nama_produk"].'</td>
			<td align="right">';
				if ($export == 1) echo $value["total_produk"];
				else echo number_format($value["total_produk"], 2);
				echo 
			'</td>
			<td align="right">&nbsp;</td>
		</tr>';
	}
	echo '
</table>';
/** 
$total_transaksi = 0;
$negara = array();
$sql3 = 
" SELECT DISTINCT to_negara ". 
" 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 to_negara ";
if ($hasil3 = mysql_query($sql3)) {
	while ($data3 = mysql_fetch_array ($hasil3)) {
		$sql4 = 
		" SELECT DISTINCT product_category ".
		" FROM kontak_dagang_header, kontak_dagang_transaksi, form_element_value " .
		" WHERE header_id = to_kontak_dagang_header " .
		" AND to_negara = valueID ".
		" AND to_negara = ".$data3["to_negara"].
		" AND tgl_transaksi >= '".$date_start."' " .
		" AND tgl_transaksi <= '".$date_end."'". $txt_remark . $txt_pic .
		" ORDER BY product_category ";
		if ($hasil4 = mysql_query($sql4)) {
			$total_negara = 0;
			$produk = array();
			$namaNegara = '';
			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 = ".$data3["to_negara"].
				" AND product_category = ".$data4["product_category"].
				" AND tgl_transaksi >= '".$date_start."' " .
				" AND tgl_transaksi <= '".$date_end."'". $txt_remark . $txt_pic . $jn_trans .
				" ORDER BY product_category ";
				if ($hasil5 = mysql_query($sql5)) {
					$total_produk = 0; $namaProduk = '';
					while ($data5 = mysql_fetch_array ($hasil5)) {
						$namaNegara   = $data5["valueName"];
						$namaProduk   = $data5["elementSubDescription"];
						$total_produk = $total_produk + $data5["estimasi_kontrak"];
						
					}
					if ($total_produk) {
						$total_negara = $total_negara + $total_produk; 
						$produk[] = array(
							"namaProduk" 	=> $namaProduk,	
							"total_produk" 	=> $total_produk
						);
					}
				}
			}
			if ($total_negara) {
				foreach ($produk as $key => $value) {
					$negara[] = array(
						"nama_negara"  => $namaNegara,
						"total_negara" => $total_negara,
						"nama_produk"  => $value["namaProduk"],
						"total_produk" => $value["total_produk"]
					);
				}
				$total_transaksi = $total_transaksi + $total_negara;
			}
		}
		
	}
}
**/?>