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

/**
 * This class provides charge capabilities.
 *
 * @package    OSB
 * @subpackage Charge
 * @category   Controllers/Admin
 * @author     Deon George
 * @copyright  (c) 2010 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Controller_Admin_Charge extends Controller_TemplateDefault_Admin {
	protected $secure_actions = array(
		'add'=>TRUE,
		'list'=>TRUE,
		'auditinvoiceitems'=>TRUE,
	);

	/**
	 * Show a list of invoices
	 */
	public function action_list() {
		Block::add(array(
			'title'=>_('Customer Charges'),
			'body'=>Table::display(
				ORM::factory('charge')->where('sweep_type','>=',0)->order_by('date_orig DESC')->find_all(),
				25,
				array(
					'id'=>array('label'=>'ID','url'=>'user/charge/view/'),
					'date_orig'=>array('label'=>'Date'),
					'sweep_type'=>array('label'=>'Sweep'),
					'status'=>array('label'=>'Status'),
					'quantity'=>array('label'=>'Quantity','class'=>'right'),
					'amount'=>array('label'=>'Total','class'=>'right'),
					'description'=>array('label'=>'Description'),
					'service_id'=>array('label'=>'Service'),
					'account->accnum()'=>array('label'=>'Cust ID'),
					'account->name()'=>array('label'=>'Customer'),
					'attributes'=>array('label'=>'Attributes'),
				),
				array(
					'page'=>TRUE,
					'type'=>'select',
					'form'=>'user/charge/view',
				)),
			));
	}

	public function action_add() {
		$output = '';

		$co = ORM::factory('charge');

		if ($_POST) {
			// Trim down our attributes
			if (is_array($_POST['attributes']))
				foreach ($_POST['attributes'] as $k=>$v)
					if (! trim($v))
						unset($_POST['attributes'][$k]);

			if ($co->values($_POST)->check()) {
				$co->status=0;

				// Entry updated
				if (! $co->save())
					throw new Kohana_Exception('Unable to save charge');
			}
		}

		$output .= Form::open();
		$output .= View::factory($this->viewpath());
		$output .= Form::submit('submit','submit');
		$output .= Form::close();

		Style::add(array(
			'type'=>'stdin',
			'data'=>'.ui-autocomplete-loading { background: white url("'.URL::site('media/img/ui-anim_basic_16x16.gif').'") right center no-repeat; }'
		));

		Style::add(array(
			'type'=>'file',
			'data'=>'js/jquery.ui/css/smoothness/jquery-ui-1.8.16.custom.css',
		));

		Script::add(array(
			'type'=>'file',
			'data'=>'js/jquery-ui-1.8.16.custom.min.js',
		));

		Script::add(array('type'=>'stdin','data'=>'
			$(document).ready(function() {
				$("input[name=account_id]").autocomplete({
					source: "'.URL::site('admin/account/ajaxlist').'",
					minLength: 2,
					change: function(event,ui) {
						// Send the request and update sub category dropdown
						$.ajax({
							type: "GET",
							data: "aid="+$(this).val(),
							dataType: "json",
							cache: false,
							url: "'.URL::site('admin/service/ajaxlist').'",
							timeout: 2000,
							error: function() {
								alert("Failed to submit");
							},
							success: function(data) {
								// Clear all options from sub category select
								$("select[name=service_id] option").remove();

								// Prepopulate a blank
								var row = "<option value=\"\">&nbsp;</option>";
								$(row).appendTo("select[name=service_id]");

								// Fill sub category select
								$.each(data, function(i, j){
									var row = "<option value=\"" + j.value + "\">" + j.label + "</option>";
									$(row).appendTo("select[name=service_id]");
								});
							}
						});
					}
				});
			});'
		));

		Block::add(array(
			'title'=>_('Add Customer Charges'),
			'body'=>$output,
		));
	}
}
?>
