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

/**
 * This class provides Affiliate Service functions
 *
 * @package    Service
 * @category   Controllers/Affiliate
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Affiliate_Service extends Controller_TemplateDefault_Affiliate {
	protected $secure_actions = array(
		'list'=>TRUE,
		'listbycheckout'=>TRUE,
		'listadslservices'=>TRUE,
		'listdomainservices'=>TRUE,
		'listhostservices'=>TRUE,
		'listhspaservices'=>TRUE,
	);

	/**
	 * Show a list of services
	 */
	public function action_list() {
		Block::add(array(
			'title'=>_('Customer Services'),
			'body'=>Table::display(
				$this->filter(ORM::factory('Service')->list_active(),$this->ao->RTM->customers($this->ao->RTM),NULL,'account_id'),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
					'service_name()'=>array('label'=>'Details'),
					'recur_schedule'=>array('label'=>'Billing'),
					'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
					'status'=>array('label'=>'Active'),
					'account->accnum()'=>array('label'=>'Cust ID'),
					'account->name()'=>array('label'=>'Customer'),
				),
				array(
					'page'=>TRUE,
					'type'=>'select',
					'form'=>URL::link('user','service/view'),
				)),
			));
	}

	/**
	 * List all services by their default checkout method
	 */
	public function action_listbycheckout() {
		$svs = array();
		// @todo This needs to be configurable
		$go = ORM::factory('Group',array('name'=>'Personal'));

		foreach (ORM::factory('Account')->list_active() as $ao)
			if ($ao->has_any('group',array($go)))
				foreach ($this->filter($ao->service->list_active(),$this->ao->RTM->customers($this->ao->RTM),NULL,'account_id') as $so)
					if (! $so->service_billing->checkout_plugin_id)
						array_push($svs,$so);

		if ($svs)
			Block::add(array(
				'title'=>'Services that should be auto-billed',
				'body'=>Table::display(
					$svs,
					25,
					array(
						'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
						'service_name()'=>array('label'=>'Details'),
						'recur_schedule'=>array('label'=>'Billing'),
						'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
						'status'=>array('label'=>'Active'),
						'account->accnum()'=>array('label'=>'Cust ID'),
						'account->name()'=>array('label'=>'Customer'),
						'date_next_invoice'=>array('label'=>'Next Invoice'),
						'account->invoices_due_total(NULL,TRUE)'=>array('label'=>'Due Invoices'),
					),
					array(
						'page'=>TRUE,
						'type'=>'select',
						'form'=>URL::link('user','service/view'),
					)),
				));
		else
			Block::add(array(
				'title'=>'Services that should be auto-billed',
				'body'=>_('None found'),
			));

		foreach (ORM::factory('Checkout')->list_active() as $co) {
			$svs = array();

			foreach ($co->account->find_all() as $ao)
				foreach ($this->filter($ao->service->list_active(),$this->ao->RTM->customers($this->ao->RTM),NULL,'account_id') as $so)
					if ($so->service_billing->checkout_plugin_id == $co->id)
						array_push($svs,$so);

			if ($svs)
				Block::add(array(
					'title'=>$co->name,
					'body'=>Table::display(
						$svs,
						25,
						array(
							'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
							'service_name()'=>array('label'=>'Details'),
							'recur_schedule'=>array('label'=>'Billing'),
							'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
							'status'=>array('label'=>'Active'),
							'account->accnum()'=>array('label'=>'Cust ID'),
							'account->name()'=>array('label'=>'Customer'),
							'date_next_invoice'=>array('label'=>'Next Invoice'),
							'account->invoices_due_total(NULL,TRUE)'=>array('label'=>'Due Invoices'),
						),
						array(
							'page'=>TRUE,
							'type'=>'select',
							'form'=>URL::link('user','service/view'),
						)),
					));
		}
	}

	private function consoltraffic($svs,$date) {
		$data = array();

		foreach ($svs as $so) {
			$c = array();
			foreach ($so->plugin()->get_traffic_data_monthly($date) as $metric => $ma) {
				foreach ($ma as $month => $traffic) {
					// Only count the service once, not for each metric.
					if (! isset($c[$month])) {
						if (isset($data['svs'][$month]))
							$data['svs'][$month] += 1;
						else
							$data['svs'][$month] = 1;

						$c[$month] = 1;
					}

					if (isset($data['data'][$metric][$month]))
						$data['data'][$metric][$month] += (int)$traffic;
					else
						$data['data'][$metric][$month] = (int)$traffic;
				}
			}
		}

		ksort($data['svs']);
		foreach ($data['data'] as $metric => $details)
			ksort($data['data'][$metric]);

		return $data;
	}

	public function action_listadslservices() {
		$svs = $this->filter(ORM::factory('Service')->list_bylistgroup('ADSL'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id');
		$data = $this->consoltraffic($svs,time());

		$google = GoogleChart::factory('Legacy')
			->type('vertical_bar')
			->title(sprintf('ADSL traffic as at %s',date('Y-m-d',strtotime('yesterday'))));

		foreach ($data['data'] as $key => $values)
			$google->sdata(array('yl'=>$key),array($key=>$values));

		$google->sdata(array('yr'=>'services'),array('services'=>$data['svs']));

		Block::add(array('body'=>(string)$google));

		Block::add(array(
			'title'=>_('ADSL Services'),
			'body'=>Table::display(
				$svs,
				NULL,
				array(
					'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
					'name()'=>array('label'=>'Service'),
					'plugin()->ipaddress()'=>array('label'=>'IP Address'),
					'product->plugin()->speed'=>array('label'=>'Speed'),
					'product->plugin()->allowance()'=>array('label'=>'Allowance'),
					'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
					'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
					'recur_schedule'=>array('label'=>'Billing'),
					'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
					'account->accnum()'=>array('label'=>'Cust ID'),
					'account->name()'=>array('label'=>'Customer'),
					'date_next_invoice'=>array('label'=>'Next Invoice'),
				),
				array(
					'type'=>'select',
					'form'=>URL::link('user','service/view'),
				)),
			));
	}

	public function action_listhspaservices() {
		$svs = $this->filter(ORM::factory('Service')->list_bylistgroup('HSPA'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id');
		$data = $this->consoltraffic($svs,time());

		$google = GoogleChart::factory('Legacy')
			->type('vertical_bar')
			->title(sprintf('HSPA traffic as at %s',date('Y-m-d',strtotime('yesterday'))));

		foreach ($data['data'] as $key => $values)
			$google->sdata(array('yl'=>$key),array($key=>$values));

		$google->sdata(array('yr'=>'services'),array('services'=>$data['svs']));

		Block::add(array('body'=>(string)$google));

		Block::add(array(
			'title'=>_('HSPA Services'),
			'body'=>Table::display(
				$svs,
				NULL,
				array(
					'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
					'name()'=>array('label'=>'Service'),
					'plugin()->ipaddress()'=>array('label'=>'IP Address'),
					'product->plugin()->speed'=>array('label'=>'Speed'),
					'product->plugin()->allowance()'=>array('label'=>'Allowance'),
					'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
					'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
					'recur_schedule'=>array('label'=>'Billing'),
					'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
					'account->accnum()'=>array('label'=>'Cust ID'),
					'account->name()'=>array('label'=>'Customer'),
					'date_next_invoice'=>array('label'=>'Next Invoice'),
				),
				array(
					'type'=>'select',
					'form'=>URL::link('user','service/view'),
				)),
			));
	}

	public function action_listdomainservices() {
		Block::add(array(
			'title'=>_('Domain Names'),
			'body'=>Table::display(
				$this->filter(ORM::factory('Service')->list_bylistgroup('DOMAIN'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id'),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
					'service_name()'=>array('label'=>'Details'),
					'plugin()->display("domain_expire")'=>array('label'=>'Expire'),
					'recur_schedule'=>array('label'=>'Billing'),
					'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
					'account->accnum()'=>array('label'=>'Cust ID'),
					'account->name()'=>array('label'=>'Customer'),
					'display("date_next_invoice")'=>array('label'=>'Next Invoice'),
				),
				array(
					'page'=>TRUE,
					'type'=>'select',
					'form'=>URL::link('user','service/view'),
				)),
			));
	}

	public function action_listhostservices() {
		Block::add(array(
			'title'=>_('Hosting Services'),
			'body'=>Table::display(
				$this->filter(ORM::factory('Service')->list_bylistgroup('HOST'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id'),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
					'service_name()'=>array('label'=>'Details'),
					'plugin()->display("host_expire")'=>array('label'=>'Expire'),
					'recur_schedule'=>array('label'=>'Billing'),
					'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
					'account->accnum()'=>array('label'=>'Cust ID'),
					'account->name()'=>array('label'=>'Customer'),
					'display("date_next_invoice")'=>array('label'=>'Next Invoice'),
				),
				array(
					'page'=>TRUE,
					'type'=>'select',
					'form'=>URL::link('user','service/view'),
				)),
			));
	}
}
?>