<?php
/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2013 Fillip Hannisdal AKA Revan/NeoRevan/Belazor 	  # ||
|| # All Rights Reserved. 											  # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------------------------------------------------------- # ||
|| # You are not allowed to use this on your server unless the files  # ||
|| # you downloaded were done so with permission.					  # ||
|| # ---------------------------------------------------------------- # ||
|| #################################################################### ||
\*======================================================================*/

// #############################################################################
if ($_REQUEST['action'] == 'transactionlog' OR empty($_REQUEST['action']))
{
	print_cp_header($vbphrase['dbtech_vbshop_transaction_log']);

	print_form_header('vbshop', 'transactionlog');
	construct_hidden_code('action', 'view');
	print_table_header($vbphrase['dbtech_vbshop_transaction_log_viewer']);
	print_input_row($vbphrase['log_entries_to_show_per_page'], 'perpage', 15);
	print_input_row($vbphrase['user_name'], 'username');
	print_time_row($vbphrase['start_date'], 'startdate', 0, 0);
	print_time_row($vbphrase['end_date'], 'enddate', 0, 0);
	print_select_row($vbphrase['dbtech_vbshop_log_type'], 'transaction', array(
		'' 				=> $vbphrase['all_log_entries'],
		'purchase' 		=> $vbphrase['dbtech_vbshop_purchase'],
		'donate' 		=> $vbphrase['dbtech_vbshop_donate'],
		'stealsuccess' 	=> $vbphrase['dbtech_vbshop_stealsuccess'],
		'stealfail' 	=> $vbphrase['dbtech_vbshop_stealfail'],
		'deposit' 		=> $vbphrase['dbtech_vbshop_deposit'],
		'withdraw'		=> $vbphrase['dbtech_vbshop_withdraw'],
		'interest' 		=> $vbphrase['dbtech_vbshop_interest'],
		'discard' 		=> $vbphrase['dbtech_vbshop_discard'],
		'gift' 			=> $vbphrase['dbtech_vbshop_gift'],
		'sale' 			=> $vbphrase['dbtech_vbshop_sale'],
		'sellback' 		=> $vbphrase['dbtech_vbshop_sellback'],
		'perreply' 		=> $vbphrase['dbtech_vbshop_perreply'],
		'perthread' 	=> $vbphrase['dbtech_vbshop_perthread'],
		'lotteryticket' => $vbphrase['dbtech_vbshop_lotteryticket'],
		'lotteryprize' 	=> $vbphrase['dbtech_vbshop_lotteryprize'],
		'pointsadjust' 	=> $vbphrase['dbtech_vbshop_pointsadjust'],
	), '');
	print_select_row($vbphrase['order_by'], 'orderby', array('date' => $vbphrase['date'], 'user' => $vbphrase['username']), 'date');
	print_submit_row($vbphrase['view'], 0);
}

// #############################################################################
if ($_REQUEST['action'] == 'view')
{
	print_cp_header($vbphrase['dbtech_vbshop_transaction_log']);

	$vbulletin->input->clean_array_gpc('r', array(
		'perpage'    	=> TYPE_UINT,
		'pagenumber' 	=> TYPE_UINT,
		'username' 		=> TYPE_STR,
		'transaction' 	=> TYPE_STR,
		'orderby'    	=> TYPE_NOHTML,
		'product'    	=> TYPE_STR,
		'startdate'  	=> TYPE_UNIXTIME,
		'enddate'    	=> TYPE_UNIXTIME,
	));

	$sqlconds = array();
	$hook_query_fields = $hook_query_joins = '';

	if ($vbulletin->GPC['perpage'] < 1)
	{
		$vbulletin->GPC['perpage'] = 15;
	}

	if ($vbulletin->GPC['username'])
	{
		$sqlconds[] = "(
			user.username LIKE '%" . $db->escape_string_like(htmlspecialchars_uni($vbulletin->GPC['username'])) . "%'
			OR recipient.username LIKE '%" . $db->escape_string_like(htmlspecialchars_uni($vbulletin->GPC['username'])) . "%'
		)";
	}

	if ($vbulletin->GPC['transaction'])
	{
		$sqlconds[] = "transactionlog.action = " . $db->sql_prepare($vbulletin->GPC['transaction']);
	}

	if ($vbulletin->GPC['startdate'])
	{
		$sqlconds[] = "transactionlog.dateline >= " . $vbulletin->GPC['startdate'];
	}

	if ($vbulletin->GPC['enddate'])
	{
		$sqlconds[] = "transactionlog.dateline <= " . $vbulletin->GPC['enddate'];
	}

	//($hook = vBulletinHook::fetch_hook('admin_modlogviewer_query')) ? eval($hook) : false;

	$counter = $db->query_first_slave("
		SELECT COUNT(*) AS total
		FROM " . TABLE_PREFIX . "dbtech_vbshop_transactionlog AS transactionlog
		LEFT JOIN " . TABLE_PREFIX . "user AS user ON (user.userid = transactionlog.userid)
		LEFT JOIN " . TABLE_PREFIX . "user AS recipient ON (recipient.userid = transactionlog.recipient)
		" . (!empty($sqlconds) ? "WHERE " . implode("\r\n\tAND ", $sqlconds) : "") . "
	");
	$totalpages = ceil($counter['total'] / $vbulletin->GPC['perpage']);

	if ($vbulletin->GPC['pagenumber'] < 1)
	{
		$vbulletin->GPC['pagenumber'] = 1;
	}
	$startat = ($vbulletin->GPC['pagenumber'] - 1) * $vbulletin->GPC['perpage'];

	switch($vbulletin->GPC['orderby'])
	{
		case 'user':
			$order = 'username ASC, dateline DESC';
			break;

		case 'recipient':
			$order = 'recipient ASC, dateline DESC';
			break;

		case 'transaction':
			$order = 'action ASC, dateline DESC';
			break;

		case 'date':
		default:
			$order = 'dateline DESC';
	}

	$logs = $db->query_read_slave("
		SELECT transactionlog.*, recipient.username AS recipient, recipient.userid AS recipientuserid, user.username
			$hook_query_fields
		FROM " . TABLE_PREFIX . "dbtech_vbshop_transactionlog AS transactionlog
		LEFT JOIN " . TABLE_PREFIX . "user AS user ON (user.userid = transactionlog.userid)
		LEFT JOIN " . TABLE_PREFIX . "user AS recipient ON (recipient.userid = transactionlog.recipient)
		$hook_join_fields
		" . (!empty($sqlconds) ? "WHERE " . implode("\r\n\tAND ", $sqlconds) : "") . "
		ORDER BY $order
		LIMIT $startat, " . $vbulletin->GPC['perpage'] . "
	");

	if ($db->num_rows($logs))
	{
		$vbulletin->GPC['transaction'] = htmlspecialchars_uni($vbulletin->GPC['transaction']);

		if ($vbulletin->GPC['pagenumber'] != 1)
		{
			$prv = $vbulletin->GPC['pagenumber'] - 1;
			$firstpage = "<input type=\"button\" class=\"button\" value=\"&laquo; " . $vbphrase['first_page'] . "\" tabindex=\"1\" onclick=\"window.location='vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=" . $vbulletin->GPC['orderby'] . "&page=1'\">";
			$prevpage = "<input type=\"button\" class=\"button\" value=\"&lt; " . $vbphrase['prev_page'] . "\" tabindex=\"1\" onclick=\"window.location='vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=" . $vbulletin->GPC['orderby'] . "&page=$prv'\">";
		}

		if ($vbulletin->GPC['pagenumber'] != $totalpages)
		{
			$nxt = $vbulletin->GPC['pagenumber'] + 1;
			$nextpage = "<input type=\"button\" class=\"button\" value=\"" . $vbphrase['next_page'] . " &gt;\" tabindex=\"1\" onclick=\"window.location='vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=" . $vbulletin->GPC['orderby'] . "&page=$nxt'\">";
			$lastpage = "<input type=\"button\" class=\"button\" value=\"" . $vbphrase['last_page'] . " &raquo;\" tabindex=\"1\" onclick=\"window.location='vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=" . $vbulletin->GPC['orderby'] . "&page=$totalpages'\">";
		}

		$headings = array();
		$headings[] = $vbphrase['id'];
		$headings[] = "<a href=\"vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=user&page=" . $vbulletin->GPC['pagenumber'] . "\">" . str_replace(' ', '&nbsp;', $vbphrase['username']) . "</a>";
		$headings[] = "<a href=\"vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=recipient&page=" . $vbulletin->GPC['pagenumber'] . "\">" . str_replace(' ', '&nbsp;', $vbphrase['dbtech_vbshop_recipient_short']) . "</a>";
		$headings[] = "<a href=\"vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=transaction&page=" . $vbulletin->GPC['pagenumber'] . "\">" . $vbphrase['dbtech_vbshop_log_type'] . "</a>";
		$headings[] = "<a href=\"vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog&action=view&u=" . $vbulletin->GPC['userid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=date&page=" . $vbulletin->GPC['pagenumber'] . "\">" . $vbphrase['date'] . "</a>";
		$headings[] = $vbphrase['info'];
		$headings[] = str_replace(' ', '&nbsp;', $vbphrase['ip_address']);

		print_form_header('', '');
		print_description_row(construct_link_code($vbphrase['restart'], "vbshop.php?" . $vbulletin->session->vars['sessionurl'] . "do=transactionlog"), false, count($headings), 'thead', 'right');
		print_table_header(construct_phrase($vbphrase['dbtech_vbshop_transaction_log_viewer_page_x_y_there_are_z_total_log_entries'], vb_number_format($vbulletin->GPC['pagenumber']), vb_number_format($totalpages), vb_number_format($counter['total'])), count($headings));
		print_cells_row($headings, 1);
		while ($log = $db->fetch_array($logs))
		{
			// prepare some required info
			$log['info'] = @unserialize($log['info']);
			$info = '';

			switch ($log['action'])
			{
				case 'purchase':
					$item = (VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] ? VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] : 'N/A');
					$info = '<strong>' . $vbphrase['dbtech_vbshop_item'] . ':</strong> ' . $item . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_price'] . ':</strong> ' . $log['info']['price'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'] . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_shop'] . ':</strong> ' . VBSHOP::$cache['shop']["{$log[info][shopid]}"]['title'] . '<br />
					';
					break;

				case 'donate':
					$info =  '<strong>' . $vbphrase['dbtech_vbshop_amount'] . ':</strong> ' . $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;

				case 'stealsuccess':
					$info = vb_number_format($entries_r['info']['gain']['total'], VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['rounding']) . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;

				case 'stealfail':
					$info = vb_number_format($entries_r['info']['loss']['total'], VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['rounding']) . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;

				case 'deposit':
					$info = $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;

				case 'withdraw':
					$info = $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;

				case 'interest':
					$info = $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;

				case 'discard':
					$item = (VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] ? VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] : 'N/A');
					$info = '<strong>' . $vbphrase['dbtech_vbshop_item'] . ':</strong> ' . $item . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_shop'] . ':</strong> ' . VBSHOP::$cache['shop']["{$log[info][shopid]}"]['title'] . '<br />
					';
					break;

				case 'gift':
					$item = (VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] ? VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] : 'N/A');
					$info = '<strong>' . $vbphrase['dbtech_vbshop_item'] . ':</strong> ' . $item . '<br />
						<strong>' . $vbphrase['message'] . ':</strong> ' . $log['info']['message'] . '<br />
					';
					break;

				case 'sale':
					$item = (VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] ? VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] : 'N/A');
					$info = '<strong>' . $vbphrase['dbtech_vbshop_item'] . ':</strong> ' . $item . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_price'] . ':</strong> ' . $log['info']['price'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'] . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_shop'] . ':</strong> ' . VBSHOP::$cache['shop']["{$log[info][shopid]}"]['title'] . '<br />
					';
					break;

				case 'sellback':
					$item = (VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] ? VBSHOP::$cache["{$log[info][feature]}"]["{$log[info][featureid]}"]['title'] : 'N/A');
					$info = '<strong>' . $vbphrase['dbtech_vbshop_item'] . ':</strong> ' . $item . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_price'] . ':</strong> ' . $log['info']['price'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'] . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_shop'] . ':</strong> ' . VBSHOP::$cache['shop']["{$log[info][shopid]}"]['title'] . '
					';
					break;

				case 'perreply':
					$info = $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'] . '<br />
						<strong>' . $vbphrase['post'] . ':</strong> <a href="../showpost.php?' . $vbulletin->session->vars['sessionurl'] . 'p=' . $log['info']['postid'] . '">' . $log['info']['postid'] . '</a>
					';
					break;

				case 'perthread':
					$info = $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'] . '<br />
						<strong>' . $vbphrase['thread'] . ':</strong> <a href="../showthread.php?' . $vbulletin->session->vars['sessionurl'] . 't=' . $log['info']['threadid'] . '">' . $log['info']['threadid'] . '</a>
					';

				case 'lotteryticket':
					$info = $log['info']['price'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'] . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_lottery'] . ':</strong> <a href="../vbshop.php?' . $vbulletin->session->vars['sessionurl'] . 'do=lottery&amp;action=view&amp;lotteryid=' . $log['info']['lotteryid'] . '">' . VBSHOP::$cache['lottery'][$log['info']['lotteryid']]['title'] . '</a>
					';

				case 'lotteryprize':
					$info = $log['info']['winnings'] . ' ' . VBSHOP::$cache['currency'][$log['info']['currencyid']]['title'] . '<br />
						<strong>' . $vbphrase['dbtech_vbshop_lottery'] . ':</strong> <a href="../vbshop.php?' . $vbulletin->session->vars['sessionurl'] . 'do=lottery&amp;action=view&amp;lotteryid=' . $log['info']['lotteryid'] . '">' . VBSHOP::$cache['lottery'][$log['info']['lotteryid']]['title'] . '</a><br />
						<strong>' . $vbphrase['dbtech_vbshop_lotteryprize'] . ':</strong> ' . VBSHOP::$cache['lotteryprize'][$log['info']['prizeid']]['title'] . '
					';
					break;

				case 'pointsadjust':
					$info = $log['info']['amount'] . ' ' . VBSHOP::$cache['currency']["{$log[info][currencyid]}"]['title'];
					break;
			}

			$cell = array();
			$cell[] = $log['transactionlogid'];
			$cell[] = ($log['username'] ? "<a href=\"user.php?" . $vbulletin->session->vars['sessionurl'] . "do=edit&u=$log[userid]\"><b>$log[username]</b></a>" : 'N/A');
			$cell[] = ($log['recipient'] ? "<a href=\"user.php?" . $vbulletin->session->vars['sessionurl'] . "do=edit&u=$log[recipientuserid]\"><b>$log[recipient]</b></a>" : 'N/A');
			$cell[] = $vbphrase['dbtech_vbshop_' . $log['action']];
			$cell[] = '<span class="smallfont">' . vbdate($vbulletin->options['logdateformat'], $log['dateline']) . '</span>';
			$cell[] = '<div align="left">' . $info . '</div>';
			$cell[] = '<span class="smallfont">' . iif($log['ipaddress'], "<a href=\"usertools.php?" . $vbulletin->session->vars['sessionurl'] . "do=gethost&ip=$log[ipaddress]\">$log[ipaddress]</a>", '&nbsp;') . '</span>';

			print_cells_row($cell, 0, 0, -4);
		}

		print_table_footer(count($headings), "$firstpage $prevpage &nbsp; $nextpage $lastpage");
	}
	else
	{
		print_stop_message('no_results_matched_your_query');
	}
}

print_cp_footer();