<?php

/**
* @package Reports
*/
class Upcoming_Transactions extends Report
{

    function process()
    {
        /*
        ************************************************
        *   1.2 Only include for active customers.  - Alberto Vasquezs             *
        ************************************************
        */
                
        if (!get_magic_quotes_gpc()) {
            $sortbydate = addslashes(@$_GET['sortbydate']);
        } else {
            $sortbydate = @$_GET['sortbydate'];
        }
        
        require_once dirname(__FILE__).'/../../modules/admin/models/Package.php';
        require_once 'classes/domain.php';
        
        require_once 'classes/Currency.php';
        $currency = new Currency($this->user);

        $this->SetDescription($this->user->lang('Displays upcoming transactions'));
        
        $reportSQL = "SELECT r.customerid, r.amount, r.nextbilldate, r.appliestoid, r.disablegenerate, r.billingtypeid, r.taxable "
                    ."FROM recurringfee r, users u "
                    ."WHERE r.nextbilldate<>'NULL' AND r.customerid=u.id AND u.status=1 "
                    ."ORDER BY r.nextbilldate ASC";
        $result = $this->db->query($reportSQL);
        
        $domainids = array();
        $masterGroup = array();
        while (list($customerid, $amount, $nextbilldate, $appliestoid, $disablegenerate, $billingtype, $taxable) = $result->fetch()) {
            $date = $this->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);
            $taxrate = $user->GetTaxRate();
            if ($taxrate > 0) {
                // Check to see if the package is taxable
                if ($appliestoid > 0 && $billingtype == -1) {
                    $domain = new Domain($appliestoid, $this->user);
                    $planid = $domain->getPlanID();
                    $paymentterm = $domain->getPaymentTermEnum();
                    $taxable = $domain->getTaxable()? true : false;
                }
                if ($taxable) {
                    $tax =  $currency->format($user->getCurrency(), $amount * $taxrate / 100);
                }
            }
            $reportSQL = "SELECT paymenttype FROM users WHERE id=?";
            $paymentresult = $this->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 = $this->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);
             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=clients&view=ShowCustomerData\">".$username[0]." ".@$username[1]."</a>", $date, "<a href=\"#\" title=\"".strtoupper($paymenttype)." - ".$domains[$i]."\">".$currency->format($tUser->getCurrency(), $amount, true, false)."</a>" . $disablegenerate, $currency->format($tUser->getCurrency(), $taxes, true, false), $currency->format($tUser->getCurrency(), $total, true, false));
            } else {
                $subGroup[$paymenttype][] = array("<a href=\"index.php?frmClientID=".$customerid."&fuse=clients&view=ShowCustomerData\" $title>".$username[0]." ".@$username[1]."</a>", $date, "<a href=\"#\" title=\"".$domains[$i]."\">".$currency->format($tUser->getCurrency(), $amount, true, false)."</a>" . $disablegenerate, $currency->format($tUser->getCurrency(), $taxes, true, false), $currency->format($tUser->getCurrency(), $total, true, false));
            }
            unset($username);
            $i++;
        }
        
        $total = count($paymenttypes);
        if ($sortbydate) {
            $this->Add($subGroup, "Transactions", array($this->user->lang('Client'), $this->user->lang('Next Invoice Date'), $this->user->lang('Subtotal'), $this->user->lang('Tax'), $this->user->lang('Total')));
        } else {
          for ($i=0; $i<$total; $i++) {
            $this->Add($subGroup[$paymenttypes[$i]], $paymenttypes[$i], array($this->user->lang('Client'), $this->user->lang('Next Invoice Date'), $this->user->lang('Subtotal'), $this->user->lang('Tax'), $this->user->lang('Total')));
          }
        }
        
        if ($sortbydate) {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?&fuse=reports&amp;report=Upcoming+Transactions&amp;type=Transactions&amp;view=ViewReport&sortbydate=0\">".$this->user->lang('Sort By Payment Type')."</a><br><br>\n\n";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?&fuse=reports&amp;report=Upcoming+Transactions&amp;type=Transactions&amp;view=ViewReport&sortbydate=1\">".$this->user->lang('Sort By Date')."</a><br><br>\n\n";
        }
        $this->Show();
    }
    
    function convertDate($date) {
        $date = date("F j, Y", strtotime($date));
        return $date;
    }
}
?>
