Internal overhaul

This commit is contained in:
Deon George
2013-11-28 17:41:34 +11:00
parent 0ed5e5163d
commit f8a5b153cf
91 changed files with 1570 additions and 1619 deletions

View File

@@ -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;
}
}
?>

View File

@@ -46,7 +46,7 @@ class Model_Product_Category extends ORM_OSB {
$result = array();
foreach (ORM::factory('Product')->where_active()->find_all() as $po)
if (in_array($this->id,$po->categories()))
if (in_array($this->id,$po->avail_category))
array_push($result,$po);
return $result;

View File

@@ -14,8 +14,8 @@ class Model_Product_Category_Translate extends ORM_OSB {
protected $_created_column = FALSE;
protected $_updated_column = FALSE;
protected $_belongs_to = array(
'product_category'=>array(),
protected $_has_one = array(
'language'=>array('foreign_key'=>'id'),
);
protected $_save_message = TRUE;

View File

@@ -18,12 +18,6 @@ abstract class Model_Product_Plugin extends ORM_OSB {
*/
abstract public function cost($annual=FALSE);
/**
* The feature summary should be implemented in plugins.
* It is displayed on the product overview page, as a summary of the products features.
*/
abstract public function feature_summary();
/**
* Return the name of the plugin
*/
@@ -31,12 +25,38 @@ abstract class Model_Product_Plugin extends ORM_OSB {
return strtolower(preg_replace('/(.*)_([a-zA-Z]+)$/',"$2",get_class($this)));
}
/**
* Get all the products using this plugin
*/
public function products($active=FALSE) {
$x = ORM::factory('Product')
->where('prod_plugin_file','=',$this->plugin())
->and_where('prod_plugin_data','=',$this);
if ($active)
$x->where_active();
return $x->find_all();
}
/**
* Form info for admins to update with plugin data
*/
abstract public function render_edit();
abstract public function render_order();
/**
* Form used during service ordering
*/
public function render_order() {
return View::factory(sprintf('product/plugin/%s/order',$this->plugin()));
}
/**
* View the details of the product
*/
public function render_view() {
return View::factory(sprintf('product/plugin/%s/view',$this->plugin()))->set('o',$this);
}
abstract public function supplier();
}

View File

@@ -17,6 +17,10 @@ class Model_Product_Translate extends ORM_OSB {
'product'=>array(),
);
protected $_has_one = array(
'language'=>array('foreign_key'=>'id'),
);
protected $_save_message = TRUE;
}
?>