Open Source Billing
This commit is contained in:
221
modules/product/classes/Model/Product.php
Normal file
221
modules/product/classes/Model/Product.php
Normal file
@@ -0,0 +1,221 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports OSB listing products
|
||||
*
|
||||
* @package Product
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*
|
||||
* Column Definitions:
|
||||
* + price_type: 0=One Time, 1=Recurring, 2=Trial
|
||||
*/
|
||||
class Model_Product extends ORM_OSB {
|
||||
// @todo this doesnt have our site_id when getting the translation
|
||||
protected $_has_many = array(
|
||||
'product_translate'=>array('far_key'=>'id'),
|
||||
'service'=>array('far_key'=>'id'),
|
||||
'invoice'=>array('through'=>'invoice_item'),
|
||||
);
|
||||
|
||||
protected $_sorting = array(
|
||||
'position'=>'asc',
|
||||
);
|
||||
|
||||
protected $_display_filters = array(
|
||||
'price_type'=>array(
|
||||
array('StaticList_PriceType::display',array(':value')),
|
||||
),
|
||||
'status'=>array(
|
||||
array('StaticList_YesNo::display',array(':value')),
|
||||
),
|
||||
'taxable'=>array(
|
||||
array('StaticList_YesNo::display',array(':value')),
|
||||
),
|
||||
);
|
||||
|
||||
// Our attributes that are arrays, we'll convert/unconvert them
|
||||
protected $_serialize_column = array(
|
||||
'avail_category',
|
||||
'price_group',
|
||||
);
|
||||
|
||||
/**
|
||||
* Which categories is this product available in
|
||||
*/
|
||||
public function categories() {
|
||||
return $this->avail_category;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product name, after translating
|
||||
* @todo This needs to be improved to find the right language item.
|
||||
*/
|
||||
public function name() {
|
||||
return $this->product_translate->find()->display('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the object of the product plugin
|
||||
*/
|
||||
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(Kohana::classname(sprintf('Product_Plugin_%s',$this->prod_plugin_file)),$this->prod_plugin_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
public function show_thumb() {
|
||||
$mediapath = Route::get('default/media');
|
||||
|
||||
$thumbfile = sprintf('prod_thmb_%s',$this->id);
|
||||
|
||||
// @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 '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,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;
|
||||
}
|
||||
}
|
||||
?>
|
55
modules/product/classes/Model/Product/Category.php
Normal file
55
modules/product/classes/Model/Product/Category.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports OSB listing products by category.
|
||||
*
|
||||
* @package Product
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Product_Category extends ORM_OSB {
|
||||
protected $_table_name = 'product_cat';
|
||||
|
||||
protected $_has_many = array(
|
||||
'product_category_translate'=>array('foreign_key'=>'product_cat_id','far_key'=>'id'),
|
||||
);
|
||||
|
||||
protected $_sorting = array(
|
||||
'name'=>'asc',
|
||||
);
|
||||
|
||||
public function description() {
|
||||
// If the user is not logged in, show the site default language
|
||||
// @todo This needs to change to the session language.
|
||||
if (! $ao=Auth::instance()->get_user())
|
||||
$ao=Company::instance()->so();
|
||||
|
||||
return ($x=$this->product_category_translate->where('language_id','=',$ao->language_id)->find()->description) ? $x : _('No Description');
|
||||
}
|
||||
|
||||
/**
|
||||
* List all the products belonging to this cateogry
|
||||
* @todo Consider if we should cache this
|
||||
*/
|
||||
public function products() {
|
||||
$result = array();
|
||||
|
||||
foreach (ORM::factory('Product')->where_active()->find_all() as $po)
|
||||
if (in_array($this->id,$po->categories()))
|
||||
array_push($result,$po);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function list_bylistgroup($cat) {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->where('list_group','=',$cat)->find_all() as $pco)
|
||||
$result[$pco->id] = $pco;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
19
modules/product/classes/Model/Product/Category/Translate.php
Normal file
19
modules/product/classes/Model/Product/Category/Translate.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class product access to product category translation.
|
||||
*
|
||||
* @package Product
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Product_Category_Translate extends ORM_OSB {
|
||||
protected $_table_name = 'product_cat_translate';
|
||||
|
||||
protected $_belongs_to = array(
|
||||
'product_category'=>array(),
|
||||
);
|
||||
}
|
||||
?>
|
28
modules/product/classes/Model/Product/Plugin.php
Normal file
28
modules/product/classes/Model/Product/Plugin.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports Product Plugins.
|
||||
*
|
||||
* @package Product
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
abstract class Model_Product_Plugin extends ORM_OSB {
|
||||
// Reset any sorting that may be defined in our parent
|
||||
protected $_sorting = array();
|
||||
|
||||
/**
|
||||
* The admin_update should be implemented in plugins.
|
||||
* It is used to update the plugin specific product information
|
||||
*/
|
||||
abstract public function admin_update();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
?>
|
19
modules/product/classes/Model/Product/Translate.php
Normal file
19
modules/product/classes/Model/Product/Translate.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class product access to product translation.
|
||||
*
|
||||
* @package Product
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Product_Translate extends ORM_OSB {
|
||||
protected $_updated_column = FALSE;
|
||||
|
||||
protected $_belongs_to = array(
|
||||
'product'=>array(),
|
||||
);
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user