<?php
// +----------------------------------------------------------------------+
// | ModernBill [TM] .:. Client Billing System                            |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001-2005 ModernGigabyte, LLC                          |
// +----------------------------------------------------------------------+
// | This source file is subject to the ModernBill End User License       |
// | Agreement (EULA), that is bundled with this package in the file      |
// | LICENSE, and is available at through the world-wide-web at           |
// | http://www.modernbill.com/extranet/LICENSE.txt                       |
// | If you did not receive a copy of the ModernBill license and are      |
// | unable to obtain it through the world-wide-web, please send a note   |
// | to license@modernbill.com so we can email you a copy immediately.    |
// +----------------------------------------------------------------------+
// | Authors: ModernGigabyte, LLC <info@moderngigabyte.com>               |
// | Support: http://www.modernsupport.com/modernbill/                    |
// +----------------------------------------------------------------------+
// | ModernGigabyte and ModernBill are trademarks of ModernGigabyte, LLC. |
// +----------------------------------------------------------------------+


require_once dirname(__FILE__)."/".str_repeat("../",4)."lib-tk/shield.php";

/**
 * This class is a child class of MB_Action. It will display a mini invoice report using the reports wizard.
 *
 * @author  Michael Fountain
 */
class ShowInvoiceStats extends MB_Action
{
	public function execute()
	{
		global $CONF;
		parent::execute();

		loadConfigs("billing");
		$invoiceSettings =& $CONF["billing"]["invoice"];
		$now = time()-(time()%(60*60*24)) + (60*60*24)-1;
		$this->generateOffset = $invoiceSettings["invoice_generate_offset"]*(60*60*24);
		
		$genTime = $now + $this->generateOffset;
		
		$data = dispatchMBAPI(toXML(array(
			"mbapi" => array(
				"command" => "GetInvoiceStats",
			),
		)), 1);

		if($this->addMBAPIErrors($data) === 0) {
					
			$this->smartyVars["invoiceStats"] = collapseArrayElements($data["mbapi"][0]["results"][0]["stats"][0]);
			
			$query = $this->getMBAPIQuery("ProcessInvoices", "", array(
				"getActiveClients" => 1,
				"getActivePackages" => 1,
				"packageDateNextRenewalLessThan" => $genTime,
				"domainDateNextRenewalLessThan" => $genTime,
				"countInvoicesToBeGeneratedOnly" => 1,
			));
			dbg("Action Command", htmlentities($query), __FILE__, __LINE__);
			$data = dispatchMBAPI($query, 1);
			dbg("Action Result", $data, __FILE__, __LINE__);
			$this->smartyVars["numInovicesToBeGenerated"] = $data["mbapi"][0]["header"][0]["numInvoicesGenerated"][0];
			
			require_once("objects/Invoice.php");
			$statusLabels = Invoice::statuses();
			$this->smartyVars["statusLabels"] = array();
			foreach ($statusLabels as $statusID=>$name){
				$this->smartyVars["statusLabels"][getAPIStatusName("invoice", $statusID)] = $name;
			}
			
			$this->addTemplate("invoices/ShowInvoiceStats", $this->smartyVars);
		}

		$this->addErrorTemplates();
		return $this->hasExecutedSuccessfully();

	}
}
