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

/**
 * This class provides User Service functions
 *
 * @package    OSB
 * @subpackage Service
 * @category   Controllers/User
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_User_Service extends Controller_TemplateDefault {
	public $secure_actions = array(
		'list'=>TRUE,
		'view'=>TRUE,
	);
	// Our acccount object
	private $ao;

	public function before() {
		parent::before();

		$this->ao = ORM::factory('account',Auth::instance()->get_user()->id);
		if (! $this->ao->loaded())
			throw new Kohana_Exception('Account doesnt exist :account ?',array(':account'=>Auth::instance()->get_user()->id));
	}

	/**
	 * Show a product
	 */
	public function action_list() {
		Block::add(array(
			'title'=>sprintf('%s: %s - %s',_('Services For'),$this->ao->accnum(),$this->ao->name(TRUE)),
			'body'=>View::factory('service/list')
				->set('services',$this->ao->service->find_all()),
			));

		$this->template->content = Block::factory();
	}

	public function action_view($id) {
		$so = ORM::factory('service',$id);

        if (! $so->loaded() OR ! Auth::instance()->authorised($so->account_id)) {
            $this->template->content = 'Unauthorised or doesnt exist?';
            return FALSE;
        }

		$graph = $graph_data = $product_info = $product_detail = '';

		// @todo Work this out dynamically
		if ($so->product->prod_plugin AND in_array($so->product->prod_plugin_file,array('ADSL'))) {

			$google = GoogleChart::factory('vertical_bar');

			// If we came in via a post to show a particular month, then show that, otherwise show the yearly result
			if ($_POST AND isset($_POST['month'])) {
				$google->title = sprintf('DSL traffic usage for %s',$_POST['month']);
				$traffic_data = $so->service_adsl->get_traffic_data_daily(strtotime($_POST['month'].'-01'));

				foreach ($traffic_data as $k => $details)
					$google->series(array(
						'title'=>array((isset($friendly[$k]) ? $friendly[$k] : $k)),
						'axis'=>'x',
						'data'=>array((isset($friendly[$k]) ? $friendly[$k] : $k)=>$traffic_data[$k])));

				foreach ($traffic_data as $k => $details)
					$google->series(array(
						'title'=>array((isset($friendly['cumulative'.$k]) ? $friendly['cumulative'.$k] : 'cumulative'.$k)),
						'axis'=>'r',
						'data'=>array((isset($friendly['cumulative'.$k]) ? $friendly['cumulative'.$k] : 'cumulative'.$k)=>$so->service_adsl->cumulative($traffic_data[$k]))));

				$graph_data = View::factory('service/view_detail_adsl_traffic')
					->set('traffic',$so->service_adsl->traffic_month(strtotime($_POST['month'].'-01'),FALSE));

			} else {
				// @todo Change the date to the last record date
				$google->title = sprintf('Monthly DSL traffic usage as at %s',Config::date(strtotime('yesterday')));
				$traffic_data = $so->service_adsl->get_traffic_data_monthly();

				foreach ($traffic_data as $k => $details)
					$google->series(array(
						'title'=>array((isset($friendly[$k]) ? $friendly[$k] : $k)),
						'axis'=>'x',
						'data'=>array((isset($friendly[$k]) ? $friendly[$k] : $k)=>$traffic_data[$k])));
			}

			$graph = (string)$google;

			$product_info = ADSL::instance()->product_view($so->service_adsl->adsl_plan_id,$so->price,$so->product->price_setup);
			$product_detail = View::factory('service/view_detail_adsl')
				->set('graph',$graph)
				->set('graph_data',$graph_data)
				->set('service',$so);
		}

		// @todo need to get the monthly price
		Block::add(array(
			'title'=>sprintf('%06s: %s',$so->id,$so->product->product_translate->find()->name),
			'body'=>View::factory('service/view')
				->set('service',$so)
				->set('product_info',$product_info)
				->set('product_detail',$product_detail),
			));

		$this->template->content = Block::factory();
	}
}
?>