<?php

/**
 * This is the model class for table "ajuste_haberes.ajuste_haberes_diferido".
 *
 * The followings are the available columns in table 'ajuste_haberes.ajuste_haberes_diferido':
 * @property integer $id
 * @property string $id_nomina_ajuste
 * @property integer $cedula
 * @property integer $id_tipo_ajuste
 * @property double $monto_asociado
 * @property double $monto_patrono
 * @property string $observacion
 * @property string $id_estatus_ajuste_diferido
 * @property string $fecha_definicion_estatus
 * @property string $id_usuario_definicion_ajuste
 *
 * The followings are the available model relations:
 * @property DatosNominaAjusteHaberes $idNominaAjuste
 * @property TipoAjuste $idTipoAjuste
 */
class AjusteHaberesDiferido extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ajuste_haberes.ajuste_haberes_diferido';
	}

	/**
	 * @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(
			array('id_estatus_ajuste_diferido', 'required', 'on'=>'DefinicionEstatus'),
			array('cedula, id_tipo_ajuste', 'numerical', 'integerOnly'=>true),
			array('monto_asociado, monto_patrono', 'numerical'),
			array('id_nomina_ajuste, observacion, id_estatus_ajuste_diferido, fecha_definicion_estatus, id_usuario_definicion_ajuste', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, id_nomina_ajuste, cedula, id_tipo_ajuste, monto_asociado, monto_patrono, observacion, id_estatus_ajuste_diferido, fecha_definicion_estatus, id_usuario_definicion_ajuste', '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(
			'idNominaAjuste' => array(self::BELONGS_TO, 'DatosNominaAjusteHaberes', 'id_nomina_ajuste'),
			'idEstatusAjusteHaberes' => array(self::BELONGS_TO, 'EstatusAjusteHaberesDiferido', 'id_estatus_ajuste_diferido'),
			'idTipoAjuste' => array(self::BELONGS_TO, 'TipoAjuste', 'id_tipo_ajuste'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_nomina_ajuste' => 'Id Nomina Ajuste',
			'cedula' => 'Cédula',
			'id_tipo_ajuste' => 'Tipo de ajuste',
			'monto_asociado' => 'Monto asociado',
			'monto_patrono' => 'Monto patrono',
			'observacion' => 'Observación',
			'id_estatus_ajuste_diferido' => 'Estatus',
			'fecha_definicion_estatus' => 'Fecha Definicion Estatus',
			'id_usuario_definicion_ajuste' => 'Id Usuario Definicion Cuota',
		);
	}

	/**
	 * 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 search($id)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		if(!is_numeric($this->cedula))
            $this->cedula = NULL;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_nomina_ajuste',$id);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('id_tipo_ajuste',$this->id_tipo_ajuste);
		$criteria->compare('monto_asociado',$this->monto_asociado);
		$criteria->compare('monto_patrono',$this->monto_patrono);
		$criteria->compare('observacion',$this->observacion,true);
		$criteria->compare('id_estatus_ajuste_diferido',$this->id_estatus_ajuste_diferido);
		$criteria->compare('fecha_definicion_estatus',$this->fecha_definicion_estatus,true);
		$criteria->compare('id_usuario_definicion_ajuste',$this->id_usuario_definicion_ajuste,true);
		$criteria->order ='id_estatus_ajuste_diferido in(1) desc';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function beforeSave() {
        if($this->fecha_definicion_estatus=='')
            $this->fecha_definicion_estatus= date('d-m-Y H:m:s');
        if($this->isNewRecord){
        	if($this->id_usuario_definicion_ajuste =='')
        		$this->id_usuario_definicion_ajuste = Yii::app()->user->id;
        }
        else
        	$this->id_usuario_definicion_ajuste = Yii::app()->user->id;
        return parent::beforeSave();            
    }
        
	/**
	 * 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 AjusteHaberesDiferido the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
