2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports OSB listing products
|
|
|
|
*
|
|
|
|
* @package OSB
|
|
|
|
* @subpackage Product
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2010 Open Source Billing
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
class Model_Product extends ORMOSB {
|
|
|
|
// @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',
|
|
|
|
'sku'=>'asc',
|
|
|
|
);
|
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
protected $_display_filters = array(
|
2011-10-10 23:38:21 +00:00
|
|
|
'active'=>array(
|
|
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
|
|
),
|
2011-10-13 21:41:01 +00:00
|
|
|
'price_base'=>array(
|
|
|
|
array('Tax::add',array(':value')),
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
2011-08-16 02:27:19 +00:00
|
|
|
'price_type'=>array(
|
|
|
|
array('StaticList_PriceType::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
|
|
|
);
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
/**
|
|
|
|
* Return the object of the product plugin
|
|
|
|
*/
|
2011-08-16 02:27:19 +00:00
|
|
|
public function plugin() {
|
2011-07-14 09:09:03 +00:00
|
|
|
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));
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
return ORM::factory(sprintf('product_plugin_%s',$this->prod_plugin_file),$this->prod_plugin_data);
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will render the product feature summary information
|
|
|
|
*/
|
|
|
|
public function feature_summary() {
|
2011-09-28 06:46:22 +00:00
|
|
|
return (is_null($plugin = $this->plugin())) ? HTML::nbsp('') : $plugin->feature_summary();
|
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);
|
|
|
|
$pg = unserialize($this->price_group);
|
|
|
|
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();
|
|
|
|
foreach (unserialize($this->price_group) as $bill_freq => $pg) {
|
|
|
|
if ($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'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $price;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
|
|
|
$thumb = $mediapath->uri(array('file'=>'img/thumbnails/'.$this->thumbnail));
|
|
|
|
|
|
|
|
// @todo Change the ALT to the product name.
|
|
|
|
echo HTML::image($thumb,array('alt'=>_('Thumb Nail')));
|
|
|
|
}
|
2011-09-28 06:46:22 +00:00
|
|
|
|
2011-10-13 21:41:01 +00:00
|
|
|
/**
|
|
|
|
* List the number of services using this product
|
|
|
|
*/
|
|
|
|
public function services_count() {
|
|
|
|
return $this->service->find_all()->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List the number of invoices using this product
|
|
|
|
*/
|
|
|
|
public function invoices_count() {
|
|
|
|
return $this->invoice->find_all()->count();
|
|
|
|
}
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
/**
|
|
|
|
* Return the products for a given category
|
|
|
|
* @todo This shouldnt be here.
|
|
|
|
*/
|
|
|
|
public function list_category($cat) {
|
|
|
|
$results = array();
|
|
|
|
|
|
|
|
foreach ($this->where('active','=',TRUE)->find_all() as $po) {
|
|
|
|
if ($c = unserialize($po->avail_category_id) AND in_array($cat,$c))
|
|
|
|
array_push($results,$po);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
?>
|