<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
require(root."/".adminFolder."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "Reports";
$pageName = "report_deposits";
$pageTitle = "Report Deposits";
$thisUrl = "reports/deposits";
$ajax_page = "ajax_report_deposits.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("report_deposits_main", $ses_kduser)) {
		unAuthorizedAccess("report_deposits_main");
	} else {
		$fil_stgl = trim(cekpost(cleanQuery($_REQUEST["fil_stgl"]), ""));
		$fil_etgl = trim(cekpost(cleanQuery($_REQUEST["fil_etgl"]), ""));
		$fil_username = trim(cekpost(cleanQuery($_REQUEST["fil_username"]), ""));
		$fil_kdbank = trim(cekpost(cleanQuery($_REQUEST["fil_kdbank"]), ""));
		$fil_kdgame = trim(cekpost(cleanQuery($_REQUEST["fil_kdgame"]), ""));
		$fil_norek = trim(cekpost(cleanQuery($_REQUEST["fil_norek"]), ""));
		$fil_namarek = trim(cekpost(cleanQuery($_REQUEST["fil_namarek"]), ""));
		$fil_sort = trim(cekpost(cleanQuery($_REQUEST["fil_sort"]), ""));
		$swhere = '';
		$sorder = '';
		$stgl = cekpost(cleanQuery($_REQUEST["fil_stgl"]), date('d-m-Y', time()));
		$etgl = cekpost(cleanQuery($_REQUEST["fil_etgl"]), date('d-m-Y', time()));
		$q_stgl = strtotime($stgl);
		$q_etgl = strtotime($etgl)+86400;
		$fil_stgl = strtotime($stgl);
		$fil_etgl = strtotime($etgl);
		if ($fil_kdbank!="") { $swhere .= " AND a.kdbank='$fil_kdbank' "; }
		if ($fil_kdgame!="") { $swhere .= " AND a.kdgame='$fil_kdgame' "; }
		if ($fil_username!="") { $swhere .= " AND a.username LIKE '%$fil_username%' "; }
		if ($fil_norek!="") { $swhere .= " AND a.norek LIKE '%$fil_norek%' "; }
		if ($fil_namarek!="") { $swhere .= " AND a.namarek LIKE '%$fil_namarek%' "; }
		if ($fil_sort!="0") {
			if ($fil_sort==1) { $sorder .= " ORDER BY jumlah DESC, tanggal DESC"; }
			if ($fil_sort==2) { $sorder .= " ORDER BY jumlah, tanggal DESC"; }
		} else {
			$sorder .= " ORDER BY tanggal DESC";
		}
		$q = "SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(cleartime)) AS x, 
			UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) AS y,
			UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) AS z,
			a.*, a.username AS username_default, c.username, namabank, inisial, COALESCE(depo.jdepo,0) AS jdepox, e.nama AS nama_game
			FROM deposits a
			LEFT JOIN members b ON b.kdmember=a.kdmember
			LEFT JOIN accounts c ON c.kdaccount=b.kdaccount
			LEFT JOIN banks d ON d.kdbank=a.kdbank
			LEFT JOIN games e ON e.kdgame=a.kdgame
			LEFT JOIN (
				SELECT COUNT(kddeposit) AS jdepo, kdmember FROM deposits WHERE isclear=1
				GROUP BY kdmember
			) AS depo ON depo.kdmember = a.kdmember
			WHERE isclear = 1
			$swhere
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(tanggal))>=UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) 
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(tanggal))< UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) 
			$sorder";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 50;
			$cp = cekpost(cleanQuery($_GET["cp"]), "1");
			$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
			$sr = ($cp-1) * $ld;
			$pagination = '';
			if ($tp!=1) {
				$pagination .= ('<ul class="pagination pagination-sm">');
					$disabled = '';
					$purl = '#';
					$onclick = '';
					if ($cp<=1) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
					for ($c=1;$c<=$tp;$c++) {
						$active = '';
						$purl = '#';
						$disabled = '';
						$onclick = '';
						if ($c==$cp) {
							$active = 'active';
						} else {
							$purl = '#';
							$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
						}
						$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
					}
					$disabled = '';
					$onclick = '';
					if ($cp>=$tp) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
				$pagination .= ('</ul>');
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			
			if ($tp!=1) {
			#	cout($pagination);
			}
			cout('<table class="table table-striped table-bordered">');
				cout('<thead>');
					cout('<tr>');
						cout('<th class="text-center">TR#ID</th>');
						if (authorizedModule("report_deposits_element_games", $ses_kduser)) {
							cout('<th class="text-center">Games</th>');
						}
						if (authorizedModule("report_deposits_element_username", $ses_kduser)) {
							cout('<th class="text-center">UserID</th>');
						}
						if (authorizedModule("report_deposits_element_banks", $ses_kduser)) {
							cout('<th class="text-center">Banks</th>');
						}
						if (authorizedModule("report_deposits_element_bank_account_number", $ses_kduser)) {
							cout('<th class="text-center">Bank Account Number</th>');
						}
						if (authorizedModule("report_deposits_element_bank_account_holder", $ses_kduser)) {
							cout('<th class="text-center">Bank Account Holder</th>');
						}
						if (authorizedModule("report_deposits_element_amount", $ses_kduser)) {
							cout('<th class="text-center" colspan="2">Amount</th>');
						}
						if (authorizedModule("report_deposits_element_status", $ses_kduser)) {
							cout('<th class="text-center">Deposit Time</th>');
						}
						if (authorizedModule("report_deposits_element_status", $ses_kduser)) {
							cout('<th class="text-center">Approve Time</th>');
						}
						if (authorizedModule("report_deposits_element_status", $ses_kduser)) {
							cout('<th class="text-center">Process Time</th>');
						}
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$total = 0;
				for ($i=0;$i<count($rs);$i++) {
					$total = $total + $rs[$i]["jumlah"];
				}
				cout('<tr class="data-ke-'.$kddeposit.'">');
					cout('<td class="text-right" colspan="6"><strong>Total Deposit</strong></td>');
					if (authorizedModule("report_deposits_element_amount", $ses_kduser)) {
						cout('<td class="text-center">Rp.</td>');
						cout('<td class="text-right"><strong>'.number_format($total).'</strong></td>');
					}
					cout('<td class="text-center" colspan="3"></td>');
				cout('</tr>');
				$cnt = 0;
				for ($i=0;$i<count($rs);$i++) {
				#for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$kddeposit = $rs[$i]["kddeposit"];
					$cnt++;
					$status = intval($rs[$i]["isactive"])==0?'Non-Active':'Active';
					$assigned = intval($rs[$i]["kdaccount"])==0?'Not Assigned Yet':'Assigned';
					cout('<tr class="data-ke-'.$kddeposit.'">');
						cout('<td class="text-center">'.$kddeposit.'</td>');
						if (authorizedModule("report_deposits_element_games", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["nama_game"].'</td>');
						}
						if (authorizedModule("report_deposits_element_username", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["username_default"].'</td>');
						}
						if (authorizedModule("report_deposits_element_banks", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["inisial"].'</td>');
						}
						if (authorizedModule("report_deposits_element_bank_account_number", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["norek"].'</td>');
						}
						if (authorizedModule("report_deposits_element_bank_account_holder", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["namarek"].'</td>');
						}
						if (authorizedModule("report_deposits_element_amount", $ses_kduser)) {
							cout('<td class="text-center">Rp.</td>');
							cout('<td class="text-right">'.number_format($rs[$i]["jumlah"]).'</td>');
						}
						if (authorizedModule("report_deposits_element_status", $ses_kduser)) {
							cout('<td class="text-center">'.date('d-M-Y H:i:s', $rs[$i]["tanggal"]).'</td>');
						}
						if (authorizedModule("report_deposits_element_status", $ses_kduser)) {
							cout('<td class="text-center">'.date('d-M-Y H:i:s', $rs[$i]["cleartime"]).'</td>');
						}
						if (authorizedModule("report_deposits_element_status", $ses_kduser)) {
							cout('<td class="text-center">'.convertSecToStr($rs[$i]["cleartime"]-$rs[$i]["tanggal"]).'</td>');
						}
					cout('</tr>');
				}
				cout('<tr class="data-ke-'.$kddeposit.'">');
					cout('<td class="text-right" colspan="6"><strong>Total Deposit</strong></td>');
					if (authorizedModule("report_deposits_element_amount", $ses_kduser)) {
						cout('<td class="text-center">Rp.</td>');
						cout('<td class="text-right"><strong>'.number_format($total).'</strong></td>');
					}
					cout('<td class="text-center" colspan="3"></td>');
				cout('</tr>');
				cout('</tbody>');
			cout('</table>');
			
			if ($tp!=1) {
			#	cout($pagination);
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			$act_ket = "Report Deposits: Main List";
			if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
			updateUserActivities($act_ket);
		} else {
			cout('<div class="alert alert-info" role="alert">');
				cout('<strong>No data available.</strong> No Player do any deposits ;(');
			cout('</div>');
		}
	}
}
?>