<?php

/**
 * This is the model class for table "retenciones.aporte_voluntario_diferido".
 *
 * The followings are the available columns in table 'retenciones.aporte_voluntario_diferido':
 * @property integer $id
 * @property integer $cedula
 * @property string $monto
 * @property integer $area
 * @property string $id_txt
 * @property boolean $blnborrado
 *
 * The followings are the available model relations:
 * @property DatosTxtIntegrado $id
 */
class AporteVoluntarioDiferido extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'retenciones.aporte_voluntario_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('cedula,monto,area','required','on'=>'update'),
			array('cedula, area', 'numerical', 'integerOnly'=>true),
			array('monto', 'length', 'max'=>20),
                        array('monto', 'numerical'),
			array('id_txt, blnborrado,id_estatus_avd', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, cedula, monto, area, id_txt, blnborrado,id_estatus_avd', '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(
			'idTxt' => array(self::BELONGS_TO, 'DatosTxtIntegrado', 'id'),
                        'idEstatusAvd' => array(self::BELONGS_TO, 'EstatusAporteVoluntarioDiferido', 'id_estatus_avd'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cedula' => 'Cédula',
			'monto' => 'Monto',
			'area' => 'Área',
			'id_txt' => 'Id Txt',
			'blnborrado' => 'Eliminado',
			'id_estatus_avd' => 'Estatus',
		);
	}

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

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('monto',$this->monto,true);
		$criteria->compare('area',$this->area);
		$criteria->compare('id_txt',$this->id_txt,true);
                $criteria->compare('aportante',$this->aportante);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('id_estatus_avd',$this->id_estatus_avd);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        
        public function searchD($id)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('cedula::TEXT',$this->cedula,true);
		$criteria->compare('monto::TEXT',$this->monto,true);
		$criteria->compare('area',$this->area);
		$criteria->compare('id_txt',$id);
		$criteria->compare('aportante',$this->aportante);
		$criteria->compare('id_estatus_avd',$this->id_estatus_avd);
		$criteria->addCondition('blnborrado=FALSE');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        
        /**
         * Obtener area del aporte
         * @return string
         */
        public function getArea()
        {
            if($this->area)
            {
                $modelArea=  Unidad::model()->findByPk($this->area);
                if($modelArea) return $modelArea->descripcion;
            }    
          return 'Area Desconocida';      
        }

        /**
	 * 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 AporteVoluntarioDiferido the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    public function detalle($id)
    {
        return Yii::app()->db->createCommand('
            SELECT cedula,
                   monto,
                   area,
                   a.nombre as aportante,
                   eavd.nombre_estatus
            FROM retenciones.aporte_voluntario_diferido avd
            INNER JOIN retenciones.aportante a ON avd.aportante=a.id
            INNER JOIN retenciones.estatus_aporte_voluntario_diferido eavd ON avd.id_estatus_avd=eavd.id
            WHERE id_txt=:id
        ')->bindParam('id', $id)->queryAll();
    }
}
