1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12: 
 13: class QuoteProduct extends CActiveRecord {
 14: 
 15:     private $_isPercentAdjustment;
 16:     private $_isTotalAdjustment;
 17: 
 18:     public function resolveAdjustmentType() {
 19:         if(empty($this->adjustmentType))
 20:             $this->adjustmentType = 'linear';
 21:         return $this->adjustmentType;
 22:     }
 23: 
 24:     public function getIsPercentAdjustment() {
 25:         if (!isset($this->_isPercentAdjustment)) {
 26:             $adjType = $this->resolveAdjustmentType();
 27:             $this->_isPercentAdjustment = $adjType == 'percent' || $adjType == 'totalPercent';
 28:         }
 29:         return $this->_isPercentAdjustment;
 30:     }
 31: 
 32: 
 33:      34:  35:  36: 
 37:     public function getIsTotalAdjustment() {
 38:         if(!isset($this->_isTotalAdjustment)) {
 39:             $adjType = $this->resolveAdjustmentType();
 40:             $this->_isTotalAdjustment = strpos($adjType,'total') === 0;
 41:         }
 42:         return $this->_isTotalAdjustment;
 43:     }
 44: 
 45: 
 46:     public static function model($className = __CLASS__) {
 47:         return parent::model($className);
 48:     }
 49: 
 50: 
 51:     public function tableName() {
 52:         return 'x2_quotes_products';
 53:     }
 54: 
 55: 
 56: 
 57:     public function rules() {
 58: 
 59: 
 60:         return array(
 61:             array('lineNumber,productId', 'numerical', 'integerOnly' => true),
 62:             array('quantity','numerical'),
 63:             array('price,adjustment,total','numerical','allowEmpty'=>true),
 64:             array('name','required'),
 65:             array('name,type,currency', 'length', 'max' => 100),
 66:             array('description', 'safe', 'safe' => true),
 67:             array('adjustmentType', 'in', 'range' => array('percent', 'linear', 'totalPercent', 'totalLinear')),
 68:         );
 69:     }
 70: 
 71:     public function relations() {
 72:         return array(
 73:             'product' => array(self::BELONGS_TO, 'Product', 'productId'),
 74:             'quote' => array(self::BELONGS_TO, 'Quote', 'quoteId'),
 75:         );
 76:     }
 77: 
 78:      79:  80:  81: 
 82:     public function formatAttribute($attr,$value=null) {
 83:         $percentage =  $this->isPercentAdjustment;
 84:         if(empty($this->currency) && !empty($this->quoteId))
 85:             $this->currency = $this->quote->currency;
 86:         if($value===null)
 87:             $value = $this->$attr;
 88:         if($attr == 'adjustment' && $percentage) {
 89:             return $value.'%';
 90:         } else if($attr == 'price' || $attr == 'adjustment' || $attr == 'total') {
 91:             return $this->formatCurrency ($value);
 92:         } else {
 93:             return $value;
 94:         }
 95:     }
 96: 
 97:      98:  99: 100: 101: 102: 
103:     public function renderAttribute($attr,$value=null) {
104:         return CHtml::encode($this->formatAttribute($attr,$value));
105:     }
106: 
107:     108: 109: 
110:     private function formatCurrency ($value) {
111:         return Yii::app ()->locale->numberFormatter->formatCurrency (
112:             $value, $this->currency, '-ยค');
113: 
114:     }
115: 
116: }
117: