<?php
/*
************************************************
*   1.2 Only include for active customers.  - Alberto Vasquezs             *
************************************************
*/

// don't show if not logged in
if (!isset($_SESSION['userId'])) die('Log in first');

if (!get_magic_quotes_gpc()) {
    $sortbydate = addslashes(@$_GET['sortbydate']);
} else {
    $sortbydate = @$_GET['sortbydate'];
}

require_once dirname(__FILE__).'/../../classes/package.php';

$report = new Report();
$report->SetDescription(lang('Displays upcoming transactions'));

$reportSQL = "SELECT r.customerid,r.amount,r.nextbilldate,r.appliestoid, r.disablegenerate, r.billingtypeid 
              FROM recurringfee r, users u
              WHERE r.nextbilldate<>'NULL' AND r.customerid=u.id AND u.status=1
              ORDER BY r.nextbilldate ASC";
$result = $GLOBALS['db']->query($reportSQL);

$domainids = array();
$masterGroup = array();
while (list($customerid, $amount, $nextbilldate, $appliestoid, $disablegenerate, $billingtype) = $result->fetch()) {
    $date = convertDate($nextbilldate);
    $domainids[] = $appliestoid;
    if ($disablegenerate == '2') {
        $disablegenerate = "<a href=\"#\" title=\"Paypal subscription set, invoice will not be created when running Generate invoices\">*</a>";
    }
    else {
        $disablegenerate = "";
    }
    // Check for taxes
    $tax = 0;
    $user =& new User($customerid);
    $user->populateCustomFields();
    $taxrate = $user->GetTaxRate();
    if ($taxrate > 0) {
        // Check to see if the package is taxable
        $domain = new Domain($appliestoid);
        $planid = $domain->getPlanID();
        $paymentterm = $domain->getPaymentTermEnum();
        $tax =  $amount * $taxrate / 100;
    }
    $reportSQL = "SELECT paymenttype FROM users WHERE id=?";
    $paymentresult = $GLOBALS['db']->query($reportSQL, $customerid);
    $paymenttype = $paymentresult->fetch();
    $masterGroup[] = array($customerid, $date, $amount, $disablegenerate, $paymenttype[0],$tax, $amount+$tax);
}

$tmpUID = array();
$domains = array();
$total = count($masterGroup);
for ($i=0; $i<$total; $i++) {
    $reportSQL = "SELECT DomainName FROM domains WHERE id=$domainids[$i]";
    //$result = $GLOBALS['db']->query($reportSQL, $domainids[$i]);
    $result = mysql_query($reportSQL);
    $tmpDomains = mysql_fetch_array($result);
    $domains[$i] = $tmpDomains[0];
    if (in_array($masterGroup[$i][0], $tmpUID)) {
        $position = array_search($masterGroup[$i][0], $tmpUID);
        if ($masterGroup[$position][1] == $masterGroup[$i][1]) {
            $newAmount = $masterGroup[$position][2] + $masterGroup[$i][2];
            $masterGroup[$position][2] = $newAmount;
            
            $newTax = $masterGroup[$position][5] + $masterGroup[$i][5];
            $masterGroup[$position][5] = $newTax;
            
            $newTotal = $masterGroup[$position][6] + $masterGroup[$i][6];
            $masterGroup[$position][6] = $newTotal;
            
            $domains[$position] .= ", ".$domains[$i];
            unset($domains[$i]);
            unset($masterGroup[$i]);
        }
    }
    $tmpUID[] = @$masterGroup[$i][0];
    unset($tmpDomains);
}

$paymenttypes = array();
$subGroup = array();
$domains = array_values($domains);
$i = 0;
while (list($info) = each($masterGroup)) {
    $customerid = $masterGroup[$info][0];
    $date = $masterGroup[$info][1];
    $amount = $masterGroup[$info][2];
    $disablegenerate = $masterGroup[$info][3];
    $paymenttype = $masterGroup[$info][4];
    $taxes = $masterGroup[$info][5];
    $total = $masterGroup[$info][6];
    if (!in_array($paymenttype, $paymenttypes)) {
        $paymenttypes[] = $paymenttype;
    }



    $userid = $customerid;
    $tUser =& new User($userid);
    $tUser->PopulateCustomFields();
     if ($tUser->IsOrganization() == true) {
        $dusername = $tUser->getOrganization();
        if (strlen($dusername) > 20) {
            $title = "title=\"$dusername\"";
            $dusername = substr($dusername, 0,20) . "...";
            $username[] =  $dusername;
        } else {
            $title = "";
            $username[] = $dusername;
        }
    } else {
        $username[] = $tUser->getFullName();
        $title = "";
    }

    if ($sortbydate) {
        $subGroup[] = array("<a href=\"index.php?frmClientID=".$customerid."&fuse=3&action=0\">".$username[0]." ".@$username[1]."</a>", $date, "<a href=\"#\" title=\"".strtoupper($paymenttype)." - ".$domains[$i]."\">$".$amount."</a>" . $disablegenerate, "$".$taxes, "$".sprintf("%.2f",$total));
    } else {
        $subGroup[$paymenttype][] = array("<a href=\"index.php?frmClientID=".$customerid."&fuse=3&action=0\" $title>".$username[0]." ".@$username[1]."</a>", $date, "<a href=\"#\" title=\"".$domains[$i]."\">$".$amount."</a>" . $disablegenerate, "$".$taxes, "$".sprintf("%.2f",$total));
    }
    unset($username);
    $i++;
}

$total = count($paymenttypes);
if ($sortbydate) {
    $report->Add($subGroup, "Transactions", array(lang('Client'), lang('Next Invoice Date'), lang('Subtotal'), lang('Tax'), lang('Total')));
} else {
  for ($i=0; $i<$total; $i++) {
    $report->Add($subGroup[$paymenttypes[$i]], $paymenttypes[$i], array(lang('Client'), lang('Next Invoice Date'), lang('Subtotal'), lang('Tax'), lang('Total')));
  }
}

if ($sortbydate) {
    echo "<a href=\"".$_SERVER['PHP_SELF']."?&fuse=7&report=Upcoming+Transactions&type=Transactions&action=0&sortbydate=0\">".lang('Sort By Payment Type')."</a><br><br>\n\n";
} else {
    echo "<a href=\"".$_SERVER['PHP_SELF']."?&fuse=7&report=Upcoming+Transactions&type=Transactions&action=0&sortbydate=1\">".lang('Sort By Date')."</a><br><br>\n\n";
}
$report->Show();

function convertDate($date) {
    $date = date("F j, Y", strtotime($date));
    return $date;
}
?>
