<?php

/**
 * This is the model class for table "configuracion.conf_haberes_asociado".
 *
 * The followings are the available columns in table 'configuracion.conf_haberes_asociado':
 * @property integer $id
 * @property double $monto_minimo_haberes
 * @property string $porcentaje_minimo_haberes
 */
class ConfHaberesAsociado extends CActiveRecord
{
	public $bloqueo = 1;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'configuracion.conf_haberes_asociado';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			['monto_minimo_haberes','required','on'=>'monto'],
			['porcentaje_minimo_haberes','required','on'=>'porcentaje'],
			['bloqueo','ValidarSeleccion',],
			array('monto_minimo_haberes', 'numerical'),
			array('porcentaje_minimo_haberes', 'length', 'max'=>6),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, monto_minimo_haberes, porcentaje_minimo_haberes', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'monto_minimo_haberes' => 'Monto Minimo Haberes',
			'porcentaje_minimo_haberes' => 'Porcentaje Minimo Haberes',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */

	public function ValidarSeleccion(){
		if($this->bloqueo==1){
			if($this->porcentaje_minimo_haberes > 100)
				$this->addError('porcentaje_minimo_haberes', 'porcentaje invalido');

			$this->monto_minimo_haberes = null;
		}
		else{
			$this->porcentaje_minimo_haberes = null;
		}
	}
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('monto_minimo_haberes',$this->monto_minimo_haberes);
		$criteria->compare('porcentaje_minimo_haberes',$this->porcentaje_minimo_haberes,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return ConfHaberesAsociado the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave(){
		
        $this->monto_minimo_haberes =  str_replace('.', '', $this->monto_minimo_haberes);
        $this->monto_minimo_haberes=str_replace(',', '.', $this->monto_minimo_haberes);

        $this->porcentaje_minimo_haberes=str_replace(',', '.', $this->porcentaje_minimo_haberes);

        if($this->monto_minimo_haberes=='')
            $this->monto_minimo_haberes=NULL;
        
        if($this->porcentaje_minimo_haberes=='')
            $this->porcentaje_minimo_haberes=NULL;
        
        return parent::beforeSave();
    }
}
