/**
 * Main Output function for the admin area
 **/
function group_pay_output($vars)
{
    //Check for paypal
    if (!gp_CheckForPaypal()) {
        echo "<p><strong>No Paypal Account</strong></p>\n\t\t\t<p>Group Pay has been configured to use a paypal account.</p>\n\t\t\t<p>Please add a paypal gateway.</p>";
    } else {
        //Do we have to save the settings
        $message = "";
        //$validLic = gp_ValidLicense();
        //Load Current Settins
        $settings = gp_LoadSettings();
        $paypalEmail = gp_LoadPayPayEmail();
        // Run a legacy file check
        group_pay_legacy_file_check();
        echo $message != "" ? "<p><b>{$message}</b></p>" : "";
        echo '<table><tr><td>Enabled:</td><td>' . ($settings['Enabled'] == "on" ? '<span style="color: green; font-weight: bold;">Enabled</span>' : '<span style="color: red; font-weight: bold;">Disabled</span>') . '</td></tr>';
        //	echo '<tr><td>License Key:</td><td>'.$settings['LicenseKey'].'</td></tr>';
        //	if($validLic[0]){
        echo '<tr><td>System Name:</td><td>' . $settings['SystemName'] . '</td></tr>';
        echo "<tr><td>PayPal Account:</td><td>{$paypalEmail}&nbsp;(<i>Loaded from PayPal gateway.</i>)</td></tr>";
        echo '<tr><td>Min Deposit:</td><td>$ ' . formatcurrency($settings['MinPayment']) . '</td></tr>';
        echo '<tr><td>Page Icon:</td><td>' . ($settings['PageIcon'] == "" ? "<i>None Configured</i>" : $settings['PageIcon']) . '</td></tr>';
        //	}else {
        //		echo '<tr valign="top"><td style="color:red">License Error:</td><td>'.$validLic[1].'</tr>';
        //	}
        echo '<tr valign="top"><td>Current Version:</td><td>' . $settings['version'] . '</tr>';
        echo '<tr><td>&nbsp;</td><td>&nbsp;</tr>';
        echo '<tr><td>Settings may be altered in Setup->Addon Modules->Group Pay</td><td>&nbsp;</tr>';
        echo '<tr><td>&nbsp;</td><td>&nbsp;</tr></table>';
        echo "<h3>We would like your support!</h3><p>If you like <a target='_blank' href='http://www.whmcs.com/appstore/304/Group-Pay---Its-Clan-Pay-for-your-WHMCS.html'>GroupPay</a> please <b>let people know!</b>" . "<ul>" . "<li><a target='_blank' href='http://www.whmcs.com/appstore/304/Group-Pay---Its-Clan-Pay-for-your-WHMCS.html'>See GroupPay on WHMCS App Store</a></li>" . "<li><a target='_blank' href='http://www.facebook.com/sharer.php?u=http://kadeo.com.au/'>Share Kadeo's Home Page on Facebook</a></li>" . "<li><a target='_blank' href='http://www.facebook.com/sharer.php?u=http://kadeo.com.au/design-and-development/whmcs-dev/whmcs-modules/72-group-pay.html'>Share Kadeo's Group Pay Page on Facebook</a></li>" . "<li><a target='_blank' href='http://twitter.com/share?url=http://kadeo.com.au/&text=I%27m+using+Group+Pay+for+WHMCS+by+%40craftingtheweb'>Tweet About Group Pay</a></li>" . "</ul>" . "Thanks Heaps!</p>";
    }
}
if (!$ipnValid[0]) {
    //Its Invalid
    gp_LogGatewayTrans("PayPal", $ipnValid[1], "Invalid IPN");
    exit;
}
// Check that the payment status is completed
if ($ipnValid[1]['payment_status'] !== "Completed") {
    gp_LogGatewayTrans("PayPal", $ipnValid[1], $ipnValid[1]['payment_status']);
    exit;
}
//Check that we havn't credited for it already
$query = 'SELECT * FROM tblaccounts WHERE transid=\'' . $ipnValid[1]['txn_id'] . '\'';
if (mysql_num_rows(mysql_query($query))) {
    exit;
}
$gpSettings = gp_LoadSettings();
//Begin to credit the client
//TODO: Uncomment
//$clientId = 1;
$clientId = gp_LoadUserFromHash($ipnValid[1]['custom']);
gp_LogGatewayTrans("PayPal", $ipnValid[1], "Successful");
if ($clientId) {
    //Apply the Credit
    // Get the clients currency and apply the rate
    $currency = getCurrency($clientId);
    //Write the Transactions
    mysql_query("INSERT INTO `tblaccounts` (userid,gateway,`date`,description,amountin,fees,transid,invoiceid,`rate`)\n   \t\t\t\t\t\t values ({$clientId},'paypal',now(),'" . $gpSettings['SystemName'] . " Credit'," . $ipnValid[1]['mc_gross'] . "," . $ipnValid[1]['mc_fee'] . ",'" . $ipnValid[1]['txn_id'] . "',0,'" . $currency['rate'] . "')");
    //Increase the credit balance
    mysql_query("UPDATE `tblclients` set credit = credit + " . $ipnValid[1]['mc_gross'] . " where id = {$clientId}");
    //Add it to the credit log
    mysql_query("INSERT INTO `tblcredit` (clientid,date,description,amount)\n\t\t\t\t\t values ({$clientId},now(),'" . $gpSettings['SystemName'] . " Credit " . $ipnValid[1]['payer_email'] . "'," . $ipnValid[1]['mc_gross'] . ")");