Internal overhaul
This commit is contained in:
@@ -48,12 +48,11 @@ class Model_Product extends ORM_OSB {
|
||||
|
||||
protected $_save_message = TRUE;
|
||||
|
||||
/**
|
||||
* Which categories is this product available in
|
||||
*/
|
||||
public function categories() {
|
||||
return $this->avail_category;
|
||||
}
|
||||
// Our database index for pricing values
|
||||
private $_price_options = array(
|
||||
'base'=>'price_base',
|
||||
'setup'=>'price_setup',
|
||||
);
|
||||
|
||||
public function cost($annual=FALSE) {
|
||||
return $this->plugin() ? $this->plugin()->cost($annual) : 0;
|
||||
@@ -72,7 +71,7 @@ class Model_Product extends ORM_OSB {
|
||||
* This will render the product feature summary information
|
||||
*/
|
||||
public function feature_summary() {
|
||||
return (is_null($plugin = $this->plugin())) ? HTML::nbsp('') : $plugin->feature_summary();
|
||||
return (is_null($x=$this->plugin())) ? HTML::nbsp('') : $x->render_view();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,6 +115,54 @@ class Model_Product extends ORM_OSB {
|
||||
return (is_null($x = $this->plugin())) ? NULL : $x->render_edit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the price for the particular group and price option for the period
|
||||
*/
|
||||
public function price($grp,$period,$price_option,$taxed=FALSE) {
|
||||
if (is_null($option=$this->price_option($price_option)) OR is_null($x=$this->keyget('price_group',$period)))
|
||||
return NULL;
|
||||
|
||||
if (isset($x[$grp][$option]))
|
||||
return $taxed ? Tax::add($x[$grp][$option]) : $x[$grp][$option];
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* For the specific user, get the best price
|
||||
*
|
||||
* @todo change this to be the overall contract price, if there is a contract
|
||||
*/
|
||||
public function price_best($period,$taxed=FALSE) {
|
||||
$result = NULL;
|
||||
|
||||
$x = NULL;
|
||||
foreach (Auth::instance()->get_groups() as $go) {
|
||||
$price = $this->price($go->id,$period,'base',$taxed);
|
||||
|
||||
if ($go->pricing AND ! is_null($price) AND (is_null($result) OR $x > $price)) {
|
||||
$result = $go;
|
||||
$x = $price;
|
||||
}
|
||||
}
|
||||
|
||||
return is_null($result) ? ORM::factory('Group',0) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database index for a price option
|
||||
*/
|
||||
public function price_option($option) {
|
||||
return isset($this->_price_options[$option]) ? $this->_price_options[$option] : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the available pricing options
|
||||
*/
|
||||
public function price_options() {
|
||||
return $this->_price_options;
|
||||
}
|
||||
|
||||
public function save(Validation $validation=NULL) {
|
||||
parent::save($validation);
|
||||
|
||||
@@ -135,13 +182,6 @@ class Model_Product extends ORM_OSB {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* List the services that are linked to this product
|
||||
*/
|
||||
public function services($active=FALSE) {
|
||||
return $active ? $this->service->where_active() : $this->service;
|
||||
}
|
||||
|
||||
public function supplier() {
|
||||
return $this->plugin() ? $this->plugin()->supplier() : 'other';
|
||||
}
|
||||
@@ -158,84 +198,5 @@ class Model_Product extends ORM_OSB {
|
||||
|
||||
return $x->loaded() ? $x->display('name') : 'No Title';
|
||||
}
|
||||
|
||||
public function list_type($type) {
|
||||
return $this->where('prod_plugin_file','=',$type)->find_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the best price to the uesr based on the users group's memberships
|
||||
* @todo This needs to be tested with more than one price group enabled
|
||||
*/
|
||||
public function get_price_array() {
|
||||
// Figure out our eligable groups
|
||||
// @todo Need to work out our default groups elsewhere, not in product
|
||||
// All users are members of the all user group "0"
|
||||
$groups = array(0);
|
||||
if (Auth::instance()->logged_in())
|
||||
foreach (Auth::instance()->get_user()->group->find_all() as $go)
|
||||
array_push($groups,$go->id);
|
||||
|
||||
// Work out the best price for the user
|
||||
$price = array();
|
||||
if (is_array($this->price_group))
|
||||
foreach ($this->price_group as $bill_freq => $pg) {
|
||||
if (isset($pg['show']) AND $pg['show'])
|
||||
foreach ($groups as $gid) {
|
||||
if (! empty($pg[$gid])) {
|
||||
if (empty($price[$bill_freq]['price_base'])
|
||||
OR ($pg[$gid]['price_base'] AND $price[$bill_freq]['price_base'] > $pg[$gid]['price_base'])) {
|
||||
$price[$bill_freq]['price_setup'] = $pg[$gid]['price_setup'];
|
||||
$price[$bill_freq]['price_base'] = $pg[$gid]['price_base'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @todo Ugly hack
|
||||
return $price ? $price : array('0'=>array('price_base'=>0,'price_setup'=>0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured price groups for this product
|
||||
*/
|
||||
public function availPriceGroups() {
|
||||
// @todo This needs to be worked out dynamically
|
||||
return array(0,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the available pricing options
|
||||
*/
|
||||
public function availPriceOptions() {
|
||||
// @todo This needs to be worked out dynamically
|
||||
return array('price_base','price_setup');
|
||||
}
|
||||
|
||||
/**
|
||||
* List the number of services using this product
|
||||
*/
|
||||
public function count_services() {
|
||||
return $this->service->list_count(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* List the number of invoices using this product
|
||||
*/
|
||||
public function count_invoices() {
|
||||
return $this->invoice->list_count(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the price for the particle group and price option for the period
|
||||
*/
|
||||
public function price($grp,$period,$option,$taxed=FALSE) {
|
||||
$x = $this->keyget('price_group',$period);
|
||||
|
||||
if (isset($x[$grp][$option]))
|
||||
return $taxed ? Tax::add($x[$grp][$option]) : $x[$grp][$option];
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Reference in New Issue
Block a user