<?php
$total_seluruh = $total_transaksi = $transaksi_perjenis = $percent_perjenis = 0;
$trans_rekap = array(); $nama_trans = ''; $abjad = "A"; $total_abjad = "";
$sql =
" SELECT * FROM kontak_dagang_header, kontak_dagang_transaksi, form_element_value ".
" WHERE jn_trans = valueID ".
" AND header_id = to_kontak_dagang_header " .
" AND jn_trans not in (1214, 1215) ".
" AND tgl_transaksi >= '".$date_start."' " .
" AND tgl_transaksi <= '".$date_end."'" . $txt_remark . $txt_pic .
" ORDER BY jn_trans ";
//echo $sql;
if ($hasil = mysqli_query($conn, $sql)) {
	if (mysqli_num_rows($hasil) > 0) {
		while ($data = mysqli_fetch_array($hasil)) {
			if ($nama_trans != $data["valueName"]) {
				if ($nama_trans) {
					$trans_rekap[] = array (
						"nama_trans" => $nama_trans,
						"tot_trans"  => $transaksi_perjenis			
					);
					$transaksi_perjenis = 0;
				}
				$nama_trans = $data["valueName"];
			}
			$transaksi_perjenis = $transaksi_perjenis + $data["estimasi_kontrak"];
			$total_transaksi = $total_transaksi + $data["estimasi_kontrak"];
		}
		if ($nama_trans) {
			$trans_rekap[] = array (
				"nama_trans" => $nama_trans,
				"tot_trans"  => $transaksi_perjenis			
			);
		}
	}
}
//var_dump($trans_rekap);
foreach ($trans_rekap as $key => $row) {
	$s_nama_trans[$key] = $row["nama_trans"];
	$s_tot_trans[$key]  = $row["tot_trans"];
}

if ($total_transaksi > 0) {
	$total_abjad .= $abjad;
	$total_seluruh = $total_seluruh + $total_transaksi;
	array_multisort($s_tot_trans,SORT_DESC, $s_nama_trans,SORT_ASC,  $trans_rekap);
}

echo '
<p>'.$head_report_trans_rekap.'</p>
<p>
	<strong>A. REKAPITULASI TRANSAKSI PRODUK</strong>
	<table width="100%" cellpadding="10" cellspacing="1" bgcolor="#333333" border="0">
		<tr bgcolor="#f1f1f1">
			<td width="35%"><strong>KETERANGAN</strong></td>
			<td width="35%" align="right"><strong>JUMLAH (USD)</strong></td>
			<td width="30%" align="right"><strong>PERSENTASE</strong></td>
		</tr>';
		foreach ($trans_rekap as $value) {
			$percent_perjenis = round(($value["tot_trans"]/$total_transaksi)*100, 2);
			echo '
			<tr bgcolor="#ffffff">
				<td>'.$value["nama_trans"].'</td>
				<td align="right">';
					if ($export == 1) echo $value["tot_trans"];
					else echo number_format($value["tot_trans"], 2);
				echo'</td>
				<td align="right">';
					if ($export == 1) echo $percent_perjenis;
					else echo number_format($percent_perjenis, 2);
				echo
				'</td>
			</tr>';
		}
		echo '
		<tr bgcolor="#ffffff">
			<td><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 100;
				else echo number_format(100, 2);
				echo'</strong>
			</td>
		</tr>
	</table>	
</p>';

unset($trans_rekap, $s_nama_trans, $s_tot_trans);
$total_transaksi = $transaksi_perjenis = $estimasi_kontrak = $percent_perjenis = 0;
$trans_rekap = array(); $nama_trans = ''; 
$sql =
" SELECT * FROM kontak_dagang_header, kontak_dagang_transaksi, form_element_value " .
" WHERE to_negara = valueID " .
" AND header_id = to_kontak_dagang_header " .
" AND jn_trans in (1214) ".
" AND tgl_transaksi >= '".$date_start."' " .
" AND tgl_transaksi <= '".$date_end."'" . $txt_remark . $txt_pic .
" ORDER BY to_negara ";
//echo $sql; 
if ($hasil = mysqli_query($conn, $sql)) {
	if (mysqli_num_rows($hasil) > 0) {
		while ($data = mysqli_fetch_array($hasil)) {
			$estimasi_kontrak = $data["estimasi_kontrak"];
			$trans_rekap[] = array (
				"nama_trans" => $data["valueName"],
				"tot_trans"  => $estimasi_kontrak,
				"bidang"  	 => $data["product_detail"]
			);
			/** 
			echo '<p>
				'.$data["valueName"].'<br>
				'.$estimasi_kontrak.'<br>
				'.$data["product_detail"].'<br>
			</p>';
			**/
			//var_dump($trans_rekap);
			$total_transaksi = $total_transaksi + $data["estimasi_kontrak"];
		}
	}
}

//var_dump($trans_rekap);
$abjad++; $total_abjad .= " + ".$abjad;
$total_seluruh = $total_seluruh + $total_transaksi;	
if (count($trans_rekap) > 1) {
	foreach ($trans_rekap as $key => $row) {
		$s_nama_trans[$key] = $row["nama_trans"];
		$s_tot_trans[$key]  = $row["tot_trans"];
		$s_bidang[$key]		= $row["bidang"];
	}
	//var_dump($s_bidang);
	array_multisort($s_tot_trans,SORT_DESC, $s_nama_trans,SORT_ASC,  $trans_rekap);
}

echo '
<p>
	<br>
	<strong>B. REKAPITULASI INVESTASI</strong>
	<table width="100%" cellpadding="10" cellspacing="1" bgcolor="#333333" border="0">
		<tr bgcolor="#f1f1f1">
			<td width="35%"><strong>NEGARA</strong></td>
			<td width="35%" align="right"><strong>NILAI INVESTASI (USD)</strong></td>
			<td width="30%"><strong>BIDANG</strong></td>
		</tr>';
		foreach ($trans_rekap as $value) {
			echo '
			<tr bgcolor="#ffffff">
				<td>'.$value["nama_trans"].'</td>
				<td align="right">';
					if ($export == 1) echo $value["tot_trans"];
					else echo number_format($value["tot_trans"], 2);
				echo'</td>
				<td>
					'.$value["bidang"].'
				</td>
			</tr>';
		}
		echo '
		<tr bgcolor="#ffffff">
			<td><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">&nbsp;</td>
		</tr>
	</table>
</p>';

unset($trans_rekap, $s_nama_trans, $s_tot_trans, $s_bidang);
$total_transaksi = $transaksi_perjenis = $percent_perjenis = 0;
$trans_rekap = array(); $nama_trans = ''; 
$sql =
" SELECT companyName, estimasi_kontrak FROM kontak_dagang_header, kontak_dagang_transaksi, ".
" exhibitor_list, form_element_value " .
" WHERE to_negara = valueID " .
" AND header_id = to_kontak_dagang_header " .
" AND jn_trans in (1215) ".
" AND tgl_transaksi >= '".$date_start."' " .
" AND tgl_transaksi <= '".$date_end."'" . $txt_remark . $txt_pic .
" AND to_exhibitor_id = exhibitorID ".
" ORDER BY companyName ";
//echo $sql; 
if ($hasil = mysqli_query($conn, $sql)) {
	if (mysqli_num_rows($hasil) > 0) {
		while ($data = mysqli_fetch_array($hasil)) {
			if ($nama_trans != $data["companyName"]) {
				if ($nama_trans) {
					$trans_rekap[] = array (
						"nama_trans" => $nama_trans,
						"tot_trans"  => $transaksi_perjenis			
					);
					$transaksi_perjenis = 0;
				}
				$nama_trans = $data["companyName"];
			}
			$transaksi_perjenis = $transaksi_perjenis + $data["estimasi_kontrak"];
			$total_transaksi = $total_transaksi + $data["estimasi_kontrak"];
		}
		if ($nama_trans) {
			$trans_rekap[] = array (
				"nama_trans" => $nama_trans,
				"tot_trans"  => $transaksi_perjenis			
			);
		}
	}
}
//var_dump($trans_rekap);

$abjad++; $total_abjad .= " + ".$abjad;
$total_seluruh = $total_seluruh + $total_transaksi;	

if (count($trans_rekap) > 1) {
	foreach ($trans_rekap as $key => $row) {
		$s_nama_trans[$key] = $row["nama_trans"];
		$s_tot_trans[$key]  = $row["tot_trans"];
	}
	array_multisort($s_tot_trans,SORT_DESC, $s_nama_trans,SORT_ASC,  $trans_rekap);
}

echo '
<p>
	<br>
	<strong>C. REKAPITULASI JASA</strong>
	<table width="100%" cellpadding="10" cellspacing="1" bgcolor="#333333" border="0">
		<tr bgcolor="#f1f1f1">
			<td width="35%"><strong>KETERANGAN</strong></td>
			<td width="35%" align="right"><strong>POTENSI REMITANSI (USD)</strong></td>
			<td width="30%">&nbsp;</td>
		</tr>';
		foreach ($trans_rekap as $value) {
			echo '
			<tr bgcolor="#ffffff">
				<td>'.$value["nama_trans"].'</td>
				<td align="right">';
					if ($export == 1) echo $value["tot_trans"];
					else echo number_format($value["tot_trans"], 2);
				echo'</td>
				<td>
					&nbsp;
				</td>
			</tr>';
		}
		echo '
		<tr bgcolor="#ffffff">
			<td><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">&nbsp;</td>
		</tr>
	</table>	
</p>
<p>
	<br>
	<table width="100%" cellpadding="10" cellspacing="1" border="0">
		<tr>
			<td style="padding:0;" width="35%"><h4 class="h4-teaser">TOTAL '.$total_abjad.'</h4></td>
			<td width="35%" align="right"><strong>';
				if ($export == 1) echo $total_seluruh;
				else echo number_format($total_seluruh, 2);
				echo '</strong>
			</td>
			<td width="35%">&nbsp;</td>
		</tr>
	</table>	
</p>';unset($trans_rekap, $s_nama_trans, $s_tot_trans);;?>