2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports OSB listing products
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Product
|
2010-11-29 22:41:08 +00:00
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2010-11-29 22:41:08 +00:00
|
|
|
* @license http://dev.osbill.net/license.html
|
2011-10-14 05:44:12 +00:00
|
|
|
*
|
|
|
|
* Column Definitions:
|
|
|
|
* + price_type: 0=One Time, 1=Recurring, 2=Trial
|
2010-11-29 22:41:08 +00:00
|
|
|
*/
|
2012-12-10 21:48:30 +00:00
|
|
|
class Model_Product extends ORM_OSB {
|
2010-11-29 22:41:08 +00:00
|
|
|
// @todo this doesnt have our site_id when getting the translation
|
|
|
|
protected $_has_many = array(
|
2011-07-13 22:59:32 +00:00
|
|
|
'product_translate'=>array('far_key'=>'id'),
|
2011-09-28 06:46:22 +00:00
|
|
|
'service'=>array('far_key'=>'id'),
|
2011-10-13 21:41:01 +00:00
|
|
|
'invoice'=>array('through'=>'invoice_item'),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
protected $_sorting = array(
|
|
|
|
'position'=>'asc',
|
|
|
|
);
|
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
protected $_display_filters = array(
|
|
|
|
'price_type'=>array(
|
|
|
|
array('StaticList_PriceType::display',array(':value')),
|
|
|
|
),
|
2012-08-01 12:43:33 +00:00
|
|
|
'status'=>array(
|
|
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
|
|
),
|
2011-10-13 21:41:01 +00:00
|
|
|
'taxable'=>array(
|
|
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
|
|
),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
// Our attributes that are arrays, we'll convert/unconvert them
|
|
|
|
protected $_serialize_column = array(
|
2013-01-11 02:33:57 +00:00
|
|
|
'avail_category',
|
2012-03-30 04:13:01 +00:00
|
|
|
'price_group',
|
|
|
|
);
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
2013-01-11 02:33:57 +00:00
|
|
|
* Which categories is this product available in
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
2013-01-11 02:33:57 +00:00
|
|
|
public function categories() {
|
|
|
|
return $this->avail_category;
|
|
|
|
}
|
2011-07-14 09:09:03 +00:00
|
|
|
|
2013-01-11 02:33:57 +00:00
|
|
|
/**
|
|
|
|
* Get the product description, after translating
|
|
|
|
* @todo This needs to be improved to find the right language item.
|
|
|
|
*/
|
|
|
|
public function description_long() {
|
|
|
|
return $this->product_translate->find()->display('description_full');
|
|
|
|
}
|
2011-07-14 09:09:03 +00:00
|
|
|
|
2013-01-11 02:33:57 +00:00
|
|
|
/**
|
|
|
|
* Get the product description, after translating
|
|
|
|
* @todo This needs to be improved to find the right language item.
|
|
|
|
*/
|
|
|
|
public function description_short() {
|
|
|
|
return $this->product_translate->find()->display('description_short');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will render the product feature summary information
|
|
|
|
*/
|
|
|
|
public function feature_summary() {
|
|
|
|
return (is_null($plugin = $this->plugin())) ? HTML::nbsp('') : $plugin->feature_summary();
|
2011-07-14 09:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the product name, after translating
|
2011-10-10 23:38:21 +00:00
|
|
|
* @todo This needs to be improved to find the right language item.
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
|
|
|
public function name() {
|
|
|
|
return $this->product_translate->find()->display('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-11 02:33:57 +00:00
|
|
|
* Return the object of the product plugin
|
2011-07-14 09:09:03 +00:00
|
|
|
*/
|
2013-01-11 02:33:57 +00:00
|
|
|
public function plugin() {
|
|
|
|
if (! $this->prod_plugin_file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (! is_numeric($this->prod_plugin_data))
|
|
|
|
throw new Kohana_Exception('Missing plugin_id for :product (:type)',array(':product'=>$this->id,':type'=>$this->prod_plugin_file));
|
|
|
|
|
|
|
|
return ORM::factory(sprintf('Product_Plugin_%s',$this->prod_plugin_file),$this->prod_plugin_data);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
if (! $this->loaded())
|
|
|
|
throw new Kohana_Exception('Call to :method where no object loaded?',array(':method'=>__METHOD__));
|
|
|
|
|
|
|
|
// 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();
|
2012-03-30 04:13:01 +00:00
|
|
|
if (is_array($this->price_group))
|
|
|
|
foreach ($this->price_group as $bill_freq => $pg) {
|
2011-12-29 23:54:54 +00:00
|
|
|
if (isset($pg['show']) AND $pg['show'])
|
2010-11-29 22:41:08 +00:00
|
|
|
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'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-29 23:54:54 +00:00
|
|
|
// @todo Ugly hack
|
|
|
|
return $price ? $price : array('0'=>array('price_base'=>0,'price_setup'=>0));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the product is a TRIAL product
|
|
|
|
* (price_type == 2)
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function is_trial() {
|
|
|
|
if ($this->price_type == 2)
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-22 01:04:20 +00:00
|
|
|
|
|
|
|
public function show_thumb() {
|
|
|
|
$mediapath = Route::get('default/media');
|
|
|
|
|
2011-12-27 01:06:04 +00:00
|
|
|
$thumbfile = sprintf('prod_thmb_%s',$this->id);
|
2011-07-22 01:04:20 +00:00
|
|
|
|
2011-12-27 01:06:04 +00:00
|
|
|
// @todo This needs to be optimised. By nice if find_files could return the HTML path too.
|
|
|
|
if (Kohana::find_file('media/img/thumbnails',$thumbfile,'png')) {
|
|
|
|
$thumb = $mediapath->uri(array('file'=>'img/thumbnails/'.$thumbfile.'.png'));
|
|
|
|
|
|
|
|
// @todo Change the ALT to the product name.
|
|
|
|
echo HTML::image($thumb,array('alt'=>_('Thumb Nail')));
|
|
|
|
} else
|
|
|
|
echo '';
|
2011-07-22 01:04:20 +00:00
|
|
|
}
|
2011-09-28 06:46:22 +00:00
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
/**
|
|
|
|
* Enable the plugin to store data
|
|
|
|
*/
|
|
|
|
public function admin_update() {
|
|
|
|
if (is_null($plugin = $this->plugin()))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return $plugin->admin_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is price shown for a specific period
|
|
|
|
*/
|
|
|
|
public function isPriceShown($p) {
|
|
|
|
$x = $this->keyget('price_group',$p);
|
|
|
|
|
|
|
|
return (isset($x['show']) AND $x['show']) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the configured price groups for this product
|
|
|
|
*/
|
|
|
|
public function availPriceGroups() {
|
|
|
|
// @todo This needs to be worked out dynamically
|
|
|
|
return array(0,2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the available pricing options
|
|
|
|
*/
|
|
|
|
public function availPriceOptions() {
|
|
|
|
// @todo This needs to be worked out dynamically
|
|
|
|
return array('price_base','price_setup');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-01 12:43:33 +00:00
|
|
|
* List the number of services using this product
|
2012-03-30 04:13:01 +00:00
|
|
|
*/
|
2012-08-01 12:43:33 +00:00
|
|
|
public function count_services() {
|
|
|
|
return $this->service->list_count(TRUE);
|
2012-03-30 04:13:01 +00:00
|
|
|
}
|
|
|
|
|
2011-10-13 21:41:01 +00:00
|
|
|
/**
|
2012-08-01 12:43:33 +00:00
|
|
|
* List the number of invoices using this product
|
2011-10-13 21:41:01 +00:00
|
|
|
*/
|
2012-08-01 12:43:33 +00:00
|
|
|
public function count_invoices() {
|
|
|
|
return $this->invoice->list_count(TRUE);
|
2011-10-13 21:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-01 12:43:33 +00:00
|
|
|
* Return the price for the particle group and price option for the period
|
2011-10-13 21:41:01 +00:00
|
|
|
*/
|
2013-01-11 02:33:57 +00:00
|
|
|
public function price($grp,$period,$option,$taxed=FALSE) {
|
2012-08-01 12:43:33 +00:00
|
|
|
$x = $this->keyget('price_group',$period);
|
|
|
|
|
2013-01-11 02:33:57 +00:00
|
|
|
if (isset($x[$grp][$option]))
|
|
|
|
return $taxed ? Tax::add($x[$grp][$option]) : $x[$grp][$option];
|
|
|
|
else
|
|
|
|
return NULL;
|
2011-09-28 06:46:22 +00:00
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|