<?php

if (!defined("WHMCS"))
	die("This file cannot be accessed directly");

$reportdata["title"] = "Server Revenue Forecasts";
$reportdata["description"] = "This report shows income broken down into each payment term period for each of your servers.  It then uses the monthly cost entered for each server to calculate the gross profit.";

$reportdata["tableheadings"] = array("Server Income","Monthly","Quarterly","Semi-Annual","Annual","Biennial","Monthly Costs","Annual Gross Profit");

$query = "SELECT * FROM tblservers ORDER BY name ASC";
$result=mysql_query($query);
while ($data = mysql_fetch_array($result)) {
	$id = $data["id"];
	$name = $data["name"];
	$monthlycost = $data["monthlycost"];
	$monthly = 0;
	$quarterly = 0;
	$semiannually = 0;
	$annually = 0;
	$biennially = 0;
	$query2 = "SELECT * FROM tblhosting WHERE server='$id' AND (domainstatus='Active' OR domainstatus='Suspended') AND billingcycle!='Free Account' AND billingcycle!='One Time'"; 
	$result2=mysql_query($query2);
	while($data = mysql_fetch_array($result2)){
		$amount = $data["amount"];
		$billingcycle = $data["billingcycle"];
		if($billingcycle=="Monthly"){
			$monthly+=$amount;
		}elseif($billingcycle=="Quarterly"){
			$quarterly+=$amount;
		}elseif($billingcycle=="Semi-Annually"){
			$semiannually+=$amount;
		}elseif($billingcycle=="Annually"){
			$annually+=$amount;
		}elseif($billingcycle=="Biennially"){
			$biennially+=$amount;
		}
	}
	$monthly=number_format($monthly,2,".","");
	$quarterly=number_format($quarterly,2,".","");
	$semiannually=number_format($semiannually,2,".","");
	$annually=number_format($annually,2,".","");
	$biennially=number_format($biennially,2,".","");
	$totalserverincome=($monthly*12)+($quarterly*4)+($semiannually*2)+$annually+($biennially/2);
	$totalserverexpenditure=$monthlycost*12;
	$servertotal=number_format(($totalserverincome-$totalserverexpenditure),2,".","");
	$totalincome+=$totalserverincome;
	$totalexpenditure+=$totalserverexpenditure;
	$totalgrossprofit+=$servertotal;
	$reportdata["tablevalues"][] = array("$name",$CONFIG["CurrencySymbol"]."$monthly",$CONFIG["CurrencySymbol"]."$quarterly",$CONFIG["CurrencySymbol"]."$semiannually",$CONFIG["CurrencySymbol"]."$annually",$CONFIG["CurrencySymbol"]."$biennially",$CONFIG["CurrencySymbol"]."$monthlycost",$CONFIG["CurrencySymbol"]."$servertotal");
}
$totalincome=sprintf("%01.2f",$totalincome);
$totalexpenditure=sprintf("%01.2f",$totalexpenditure);
$totalgrossprofit=sprintf("%01.2f",$totalgrossprofit);

$data["footertext"]="<B>Total Income:</B> ".$CONFIG["CurrencySymbol"]."$totalincome<br><B>Total Expenses:</B> ".$CONFIG["CurrencySymbol"]."$totalexpenditure</br><B>Gross Profit:</B> ".$CONFIG["CurrencySymbol"]."$totalgrossprofit";

?>