Fixes to OSB to work with KH 3.3

This commit is contained in:
Deon George
2012-11-10 10:13:57 +11:00
parent ea36639638
commit 6db02ae77d
238 changed files with 813 additions and 938 deletions

View File

@@ -0,0 +1,270 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Services
*
* @package OSB
* @subpackage Service
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*
* Fields:
* + queue: PROVISION (to be provisioned)
*/
class Model_Service extends ORM_OSB {
// Relationships
protected $_has_one = array(
'affiliate'=>array('far_key'=>'affiliate_id','foreign_key'=>'id'),
'service_billing'=>array('far_key'=>'account_billing_id','foreign_key'=>'id'),
);
protected $_has_many = array(
'invoice_item'=>array('far_key'=>'id'),
'invoice'=>array('through'=>'invoice_item'),
);
protected $_belongs_to = array(
'product'=>array(),
'account'=>array(),
);
/**
* Filters used to format the display of values into friendlier values
*/
protected $_display_filters = array(
'date_last_invoice'=>array(
array('Config::date',array(':value')),
),
'date_next_invoice'=>array(
array('Config::date',array(':value')),
),
'recur_schedule'=>array(
array('StaticList_RecurSchedule::display',array(':value')),
),
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
);
/**
* Return the object of the product plugin
*/
public function plugin($type='') {
if (! $this->product->prod_plugin_file)
return NULL;
if (! is_numeric($this->product->prod_plugin_data))
throw new Kohana_Exception('Missing plugin_id for :product (:type)',array(':product'=>$this->product->id,':type'=>$this->product->prod_plugin_file));
$o = ORM::factory(sprintf('Service_Plugin_%s',$this->product->prod_plugin_file),array('service_id'=>$this->id));
return $type ? $o->$type : $o;
}
/**
* Display the service number
*/
public function id() {
return sprintf('%05s',$this->id);
}
/**
* Display the service product name
*/
public function name() {
return is_null($plugin=$this->plugin()) ? $this->product->name() : $plugin->name();
}
/**
* Display how much is due on this service
*/
public function due($format=FALSE) {
$total = 0;
foreach ($this->list_invoices(TRUE) as $io)
$total += $io->due();
return $format ? Currency::display($total) : $total;
}
/**
* When does this service expire
*/
public function expire($format=FALSE) {
// For plugins the plugin determins expiry
$expire = (is_null($plugin=$this->plugin()) ? NULL : $plugin->expire());
// If $expire is NULL, we'll use the next invoice date
$expire = is_null($expire) ? $this->date_next_invoice-86400 : $expire;
return $format ? Config::date($expire) : $expire;
}
/**
* Determine if a service expires in the next $days.
*/
public function expiring($days) {
return time()+$days*86400 > $this->expire();
}
/**
* Return the service charge
*/
public function price($tax=FALSE,$format=FALSE) {
$x = $this->product->keyget('price_group',$this->recur_schedule);
// @todo This index shouldnt be hard coded.
$p = $this->price ? $this->price : $x[$this->price_group]['price_base'];
if ($tax)
$p = Tax::add($p);
return $format ? Currency::display($p) : $p;
}
public function service_name() {
return is_null($plugin=$this->plugin()) ? $this->name() : $plugin->service_name();
}
public function service_view() {
return is_null($plugin=$this->plugin()) ? HTML::nbsp('') : $plugin->service_view();
}
/**
* Display the product feature summary
*/
public function product_feature_summary() {
return $this->product->feature_summary();
}
/**
* Render some details for specific calls, eg: invoice
*/
public function details($type) {
switch ($type) {
case 'invoice_detail_items':
if (is_null($plugin = $this->plugin()))
return array();
else
return $plugin->_details($type);
break;
default:
throw new Kohana_Exception('Unkown detail request :type',array(':type'=>$type));
}
}
/**
* Enable the plugin to store data
*/
public function admin_update() {
if (is_null($plugin = $this->plugin()))
return NULL;
else
return $plugin->admin_update();
}
public function transactions() {
return $this->invoice_item->order_by('date_start,date_stop');
}
// @todo To implement
public function charges_new() {
return 0;
}
/** LIST FUNCTIONS **/
/**
* Search for services matching a term
*/
public function list_autocomplete($term,$index='id',array $limit=array()) {
$return = array();
$this->clear();
$this->where_active();
$value = 'service_name()';
// Build our where clause
$this->where_open()
->where('id','like','%'.$term.'%')
->where_close();
foreach ($limit as $w) {
list($k,$s,$v) = $w;
$this->and_where($k,$s,$v);
}
foreach ($this->find_all() as $o)
$return[$o->$index] = array(
'value'=>$o->$index,
'label'=>sprintf('SVC %s: %s',$o->id,Table::resolve($o,$value)),
);
return $return;
}
public function list_bylistgroup($cat) {
$result = array();
$cats = ORM::factory('Product_Category')->list_bylistgroup($cat);
foreach ($this->list_active() as $so) {
if (! $so->product->avail_category OR ! preg_match('/^a:/',$so->product->avail_category))
continue;
$pc = unserialize($so->product->avail_category);
if (array_intersect($pc,array_keys($cats)))
array_push($result,$so);
}
return $result;
}
/**
* List services expiring
*/
public function list_expiring($days=14) {
$result = array();
foreach ($this->list_active() as $so)
if ($so->expiring($days))
array_push($result,$so);
return $result;
}
/**
* List invoices for this service
*/
public function list_invoices($due=FALSE) {
$return = array();
foreach ($this->invoice->find_all() as $io)
if (! $due OR $io->due())
array_push($return,$io);
return $return;
}
/**
* List services that need to be billed.
*
* @param $days int Additional number of days to add to the query, above the module config.
*/
public function list_invoicesoon($days=0) {
return $this->_where_active()
->where_open()->where('suspend_billing','IS',NULL)->or_where('suspend_billing','=','0')->where_close()
->where('date_next_invoice','<',time()+(ORM::factory('Invoice')->config('GEN_DAYS')+$days)*86400)
->find_all();
}
/**
* List services that need to be provisioned
*/
public function list_provision() {
return $this->_where_active()->where('queue','=','PROVISION');
}
}
?>

View File

@@ -0,0 +1,21 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Service Billing.
*
* @package OSB
* @subpackage Product/Service
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Service_Billing extends ORM_OSB {
protected $_table_name = 'account_billing';
// Relationships
protected $_has_one = array(
'checkout'=>array('far_key'=>'checkout_plugin_id','foreign_key'=>'id'),
);
}
?>

View File

@@ -0,0 +1,98 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Service Plugins.
*
* @package OSB
* @subpackage Product/Service
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
abstract class Model_Service_Plugin extends ORM_OSB {
// Reset any sorting that may be defined in our parent
protected $_sorting = array();
/**
* Form info for admins to update
*/
abstract public function admin_update();
/**
* Our service name as defined in the DB
*/
abstract public function name();
/**
* When does our service expire
*/
abstract public function expire();
/**
* Show our service name as defined in the DB with product suffix.
*/
public function service_name() {
return sprintf('%s - %s',$this->service->product->name(),$this->name());
}
/**
* View details of the service
*/
abstract public function service_view();
/**
* The table attributes that provide username/password values
*/
abstract public function username_value();
abstract public function password_value();
public function manage_button() {
if (! $this->service->status)
return FALSE;
static $k = '';
// If $k is already set, we've rendered this JS
if ($k)
return TRUE;
$k = Random::char();
Session::instance()->set('manage_button',$k);
Script::add(array('type'=>'stdin','data'=>'
$(document).ready(function() {
var x=0;
$("button[name=submit]").click(function() {
var t=$(this).val().split(":");
if (x++) { alert("Session expired, please refresh the page!"); return false; }
$.getJSON("'.URL::site('user/service/ajaxmanage/'.$this->service_id).'", { k: "'.$k.'",t: t[1] }, function(data) {
$.each(data, function(key, val) { $("#"+key+"_"+t[0]+"_"+t[1]).val(val); });
}).error(function() { alert("There was a problem with the request"); return false; }).success(
function() { $("form[id=id_"+t[0]+"_"+t[1]+"]").submit(); });
});
});'
));
return TRUE;
}
/**
* Get specific service details for use in other modules
* For Example: Invoice
*
* @todo Make the rendered items configurable
* @todo Change this method name, now that it is public
*/
public function _details($type) {
switch ($type) {
// Nothing to add for invoices
case 'invoice_detail_items':
return array();
default:
throw new Kohana_Exception('Unkown detail request :type',array(':type'=>$type));
}
}
}
?>