<?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;
$total_lainnya = 0;
//echo $total_transaksi;

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>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 = '';
	$counter2 = 0;
	foreach ($negara as $key => $value) {
		if ($cek != $value["nama_negara"]) {
			$cek = $value["nama_negara"];
			$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_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">';
					$percenPernegara =  round(($value["total_negara"]/$total_transaksi)*100, 2);
					echo 
					'<strong>'
						.($export==1 ? $percenPernegara : number_format($percenPernegara,2,".",",")).
					'</strong>';
					echo '	
				</td>
			</tr>';
			$counter2++;
			if ($counter2 <= 5) {
				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>';
			} else {
				$total_lainnya = $total_lainnya + $value["total_produk"];
			}
		} else {
			$counter2++;
			if ($counter2 <= 5) {
				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>';
			} else {
				$total_lainnya = $total_lainnya + $value["total_produk"];
			}
		}
	}
	echo '
</table>';
/** 
if (!$remark || $remark == "1") {
	$remark = " AND remark = '0' ";
} else if ($remark == "2") {
	$remark = " AND remark = 'on' ";
} else if ($remark == "3") {
	$remark = " ";
}

if (!$pic || $pic == "1") {
	$pic = " AND pic = '0' ";
} else if ($pic == "2") {
	$pic = " AND pic = 'on' ";
} else if ($pic == "3") {
	$pic = " ";
}
**/
//var_dump($negara);
/** 
$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 ";
//echo "<p>".$sql3."</p>";
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 ";
				//echo '<p>sql5: '.$sql5.'</p>';
				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;
			}
		}
	}
}
**/
//var_dump($negara);
//echo '<p>ini dia: &nbsp;</p>';
//var_dump($negara);
?>