2013-10-10 02:44:53 +00:00
|
|
|
<?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
|
|
|
|
*/
|
2013-12-19 23:00:32 +00:00
|
|
|
class Model_Product extends ORM_OSB implements Invoicable {
|
2016-08-03 04:00:51 +00:00
|
|
|
private $_pto = NULL;
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
protected $_has_many = array(
|
|
|
|
'invoice'=>array('through'=>'invoice_item'),
|
2013-11-22 04:36:50 +00:00
|
|
|
'service'=>array('far_key'=>'id'),
|
|
|
|
'translate'=>array('model'=>'Product_Translate','far_key'=>'id'),
|
2013-10-10 02:44:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
protected $_sorting = array(
|
|
|
|
'position'=>'asc',
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $_display_filters = array(
|
|
|
|
'price_type'=>array(
|
2013-04-26 01:42:09 +00:00
|
|
|
array('StaticList_PriceType::get',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
'status'=>array(
|
2013-11-14 11:50:35 +00:00
|
|
|
array('StaticList_YesNo::get',array(':value',TRUE)),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
'taxable'=>array(
|
2013-11-14 11:50:35 +00:00
|
|
|
array('StaticList_YesNo::get',array(':value',TRUE)),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2013-11-27 00:22:20 +00:00
|
|
|
protected $_form = array('id'=>'id','value'=>'description()');
|
|
|
|
|
2014-08-25 04:41:07 +00:00
|
|
|
protected $_compress_column = array(
|
|
|
|
'avail_category',
|
|
|
|
'group_avail',
|
|
|
|
'price_group',
|
|
|
|
'prod_plugin_data',
|
|
|
|
);
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
protected $_nullifempty = array(
|
|
|
|
'price_group',
|
|
|
|
);
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
// Our attributes that are arrays, we'll convert/unconvert them
|
|
|
|
protected $_serialize_column = array(
|
|
|
|
'price_group',
|
|
|
|
);
|
|
|
|
|
2013-11-22 04:36:50 +00:00
|
|
|
protected $_save_message = TRUE;
|
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
/** REQUIRED ABSTRACT METHODS **/
|
|
|
|
|
|
|
|
public function name($variable=NULL) {
|
|
|
|
if (! $variable instanceof Model_Language)
|
|
|
|
throw HTTP_Exception::factory(500,'Call to :method incorrect',array(':method'=>__METHOD__));
|
|
|
|
|
|
|
|
$pto = $this->_pto($variable);
|
|
|
|
|
|
|
|
return (! $pto->loaded() OR ! $pto->name) ? sprintf('Unknown: [%s]',$this->id) : $pto->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function namesub($variable=NULL) {
|
|
|
|
if (! $variable instanceof Model_Language)
|
|
|
|
throw HTTP::Exception(500,'Call to :method incorrect',array(':method'=>__METHOD__));
|
|
|
|
|
|
|
|
$pto = $this->_pto($variable);
|
|
|
|
|
|
|
|
return (! $pto->loaded() OR ! $pto->description_short) ? sprintf('Unknown: [%s]',$this->id) : $pto->description_short;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refnum($short=FALSE) {
|
|
|
|
return ($short ? '' : sprintf('%02s-',Site::id())).sprintf('%04s',$this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** REQUIRED INTERFACE METHODS **/
|
2013-12-19 23:00:32 +00:00
|
|
|
|
|
|
|
public function invoice_item($item_type) {
|
|
|
|
switch ($item_type) {
|
|
|
|
case 0:
|
|
|
|
case 7:
|
|
|
|
return 'Service';
|
2014-05-20 11:49:52 +00:00
|
|
|
case 6:
|
|
|
|
return 'Service Cancellation';
|
2013-12-19 23:00:32 +00:00
|
|
|
default:
|
|
|
|
return 'Product Charge';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invoice_title() {
|
|
|
|
return $this->title();
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
/** LOCAL METHODS **/
|
2013-12-19 23:00:32 +00:00
|
|
|
|
2013-11-28 06:41:34 +00:00
|
|
|
// Our database index for pricing values
|
|
|
|
private $_price_options = array(
|
|
|
|
'base'=>'price_base',
|
|
|
|
'setup'=>'price_setup',
|
|
|
|
);
|
2013-06-17 08:01:47 +00:00
|
|
|
|
2016-08-03 04:00:51 +00:00
|
|
|
/**
|
|
|
|
* Return the translated langauge object
|
|
|
|
*/
|
|
|
|
private function _pto(Model_Language $lo) {
|
|
|
|
if (! $this->_pto) {
|
|
|
|
|
|
|
|
// First try the called langauge.
|
|
|
|
$pto = $this->translate->where('language_id','=',$lo->id)->find();
|
|
|
|
|
|
|
|
// Second try the called langauge.
|
|
|
|
if (! $pto->loaded())
|
|
|
|
$pto = $this->translate->where('language_id','=',Company::instance()->language()->id)->find();
|
|
|
|
|
|
|
|
$this->_pto = $pto;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_pto;
|
|
|
|
}
|
|
|
|
|
2013-10-28 23:36:57 +00:00
|
|
|
public function cost($annual=FALSE) {
|
|
|
|
return $this->plugin() ? $this->plugin()->cost($annual) : 0;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
2013-04-26 01:42:09 +00:00
|
|
|
* Return the translated description for a category.
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-04-26 01:42:09 +00:00
|
|
|
public function description($full=FALSE) {
|
|
|
|
$x = $this->translate();
|
|
|
|
|
|
|
|
return $x->loaded() ? $x->display($full ? 'description_full' : 'description_short') : 'No Description';
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 13:43:31 +00:00
|
|
|
/**
|
|
|
|
* This will render the product feature summary information
|
|
|
|
*/
|
|
|
|
public function feature_summary() {
|
2013-11-28 06:41:34 +00:00
|
|
|
return (is_null($x=$this->plugin())) ? HTML::nbsp('') : $x->render_view();
|
2013-06-01 13:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is price shown for a specific period
|
|
|
|
*
|
|
|
|
* @param $p int recurring schedule period
|
|
|
|
*/
|
|
|
|
public function is_price_shown($p) {
|
|
|
|
$x = $this->keyget('price_group',$p);
|
|
|
|
|
|
|
|
return (isset($x['show']) AND $x['show']) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the product is a TRIAL product
|
|
|
|
* (price_type == 2)
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function is_trial() {
|
|
|
|
return ($this->price_type == 2) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
2013-04-26 01:42:09 +00:00
|
|
|
* Return the object of the product plugin
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-04-26 01:42:09 +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(Kohana::classname(sprintf('Product_Plugin_%s',$this->prod_plugin_file)),$this->prod_plugin_data);
|
|
|
|
}
|
|
|
|
|
2013-11-27 00:22:20 +00:00
|
|
|
/**
|
|
|
|
* Enable the plugin to store data during an update
|
|
|
|
*/
|
|
|
|
public function plugin_edit() {
|
|
|
|
return (is_null($x = $this->plugin())) ? NULL : $x->render_edit();
|
|
|
|
}
|
|
|
|
|
2013-11-28 06:41:34 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
public function save(Validation $validation=NULL) {
|
2013-11-22 04:36:50 +00:00
|
|
|
parent::save($validation);
|
2013-04-26 01:42:09 +00:00
|
|
|
|
|
|
|
// Save our Translated Message
|
2016-08-03 06:20:25 +00:00
|
|
|
if ($x = array_diff_key($_POST,$this->_object) AND Arr::get($_POST,'language_id') AND is_array(Arr::get($_POST,'translate'))) {
|
|
|
|
$to = $this->translate->where('language_id','=',Arr::get($_POST,'language_id'))->find();
|
2013-04-26 01:42:09 +00:00
|
|
|
|
2013-07-05 06:11:37 +00:00
|
|
|
// For a new entry, we need to set the product_id
|
2013-11-22 04:36:50 +00:00
|
|
|
if (! $to->loaded()) {
|
|
|
|
$to->product_id = $this->id;
|
2016-08-03 06:20:25 +00:00
|
|
|
$to->language_id = Arr::get($_POST,'language_id');
|
2013-04-26 01:42:09 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 04:36:50 +00:00
|
|
|
$to->values($x['translate'])->save();
|
2013-04-26 01:42:09 +00:00
|
|
|
}
|
2013-11-22 04:36:50 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:36:57 +00:00
|
|
|
public function supplier() {
|
|
|
|
return $this->plugin() ? $this->plugin()->supplier() : 'other';
|
|
|
|
}
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
private function translate() {
|
2014-08-25 04:41:07 +00:00
|
|
|
return $this->translate->where('language_id','=',Company::instance()->language())->find();
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-26 01:42:09 +00:00
|
|
|
* Return the translated title for a category
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-04-26 01:42:09 +00:00
|
|
|
public function title() {
|
|
|
|
$x = $this->translate();
|
|
|
|
|
|
|
|
return $x->loaded() ? $x->display('name') : 'No Title';
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|