<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides Reseller Account management
 *
 * @package    OSB
 * @category   Controllers/Reseller
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Reseller_Account extends Controller_Account {
	protected $secure_actions = array(
		'ajaxlist'=>TRUE,
		'list'=>TRUE,
		'listlog'=>TRUE,
		'view'=>TRUE,
	);

	/**
	 * Used by AJAX calls to find accounts
	 * @note list_autocomplete() will limit to authorised accounts
	 */
	public function action_ajaxlist() {
		$result = array();

		if (isset($_REQUEST['term']) AND trim($_REQUEST['term']))
			$result += ORM::factory('Account')->list_autocomplete($_REQUEST['term']);

		$this->auto_render = FALSE;
		$this->response->headers('Content-Type','application/json');
		$this->response->body(json_encode(array_values($result)));
	}

	/**
	 * Show a list of accounts
	 */
	public function action_list() {
		Block::factory()
			->title(_('Customer List'))
			->title_icon('icon-th-list')
			->body(Table::factory()
				->data(ORM::factory('Account')->where('id','IN',$this->ao->RTM->customers($this->ao->RTM))->find_all())
				->jssort('customer')
				->columns(array(
					'id'=>'ID',
					'accnum()'=>'Num',
					'name(TRUE)'=>'Account',
					'email'=>'Email',
					'invoices_due_total(NULL,TRUE)'=>'Invoices',
					'services_count(TRUE)'=>'Services',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('reseller','account/view/')),
				))
			);
	}

	/**
	 * Show a list of account logins
	 */
	public function action_listlog() {
		Block::factory()
			->title(_('Customer Login Activity'))
			->title_icon('icon-eye-open')
			->body(Table::factory()
				->data(ORM::factory('Account_Log')->where('account_id','IN',$this->ao->RTM->customers($this->ao->RTM))->find_all())
				->page_items(25)
				->columns(array(
					'id'=>'ID',
					'date_orig'=>'Date',
					'account->name()'=>'Account',
					'ip'=>'IP Address',
					'details'=>'Details',
				))
			);
	}

	public function action_view() {
		$ao = ORM::factory('Account',$this->request->param('id'));
		if (! $ao->loaded() OR ! $ao->status)
			HTTP::redirect(URL::link('reseller','welcome'));

		Block::factory()
			->title(sprintf('Active Service for Account: %s',$ao->accnum()))
			->title_icon('icon-info-sign')
			->span(6)
			->body(Table::factory()
				->data($ao->service->list_active())
				->columns(array(
					'id'=>'ID',
					'service_name()'=>'Service',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('user','service/view/')),
				))
			);

		Block::factory()
			->title(sprintf('Invoices Due Account: %s (%s)',$ao->accnum(),$ao->invoice->list_due_total(TRUE)))
			->title_icon('icon-info-sign')
			->span(6)
			->body(Table::factory()
				->data($ao->invoice->list_due())
				->columns(array(
					'id'=>'ID',
					'due_date'=>'Date Due',
					'total(TRUE)'=>'Invoice Total',
					'due(TRUE)'=>'Amount Due',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('user','invoice/view/')),
				))
			);

		Block::factory()
			->title(sprintf('Services Expiring for Account: %s',$ao->accnum()))
			->title_icon('icon-info-sign')
			->span(6)
			->body(Table::factory()
				->data($ao->service->list_expiring())
				->columns(array(
					'id'=>'ID',
					'service_name()'=>'Service',
					'expire(TRUE)'=>'Date',
				))
				->prepend(array(
					'id'=>array('url'=>URL::link('user','service/view/')),
				))
			);

		$i = Invoice::instance();
		foreach ($ao->service->list_active() as $io)
			if (! $io->suspend_billing)
				$i->add_service($io);

		Block::factory()
			->title(sprintf('Next Invoice Items for Account: %s',$ao->accnum()))
			->title_icon('icon-info-sign')
			->span(6)
			->body($i->render('html','body'));
	}
}
?>