<?php

/**
 * This is the model class for table "monte_pio".
 *
 * The followings are the available columns in table 'monte_pio':
 * @property integer $idsolicitud
 * @property integer $idtrabajador
 * @property integer $cedula
 * @property string $nombre
 * @property string $apellido
 * @property integer $parentesco
 * @property string $lugar
 * @property string $fecha_siniestro
 * @property string $fecha
 * @property boolean $blnborrado
 */
class MontePio extends CActiveRecord
{
     public $unidad;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'monte_pio';
	}

	/**
	 * @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('idtrabajador,cedula,nombre, apellido, parentesco,fecha_siniestro', 'required'),
			array('idtrabajador, cedula, parentesco', 'numerical', 'integerOnly'=>true),
			array('nombre, apellido, lugar', 'length', 'max'=>100),
			array('fecha_siniestro,idtrabajador, cedula, parentesco ', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idsolicitud, idtrabajador, cedula, nombre, apellido, parentesco, lugar, fecha_siniestro, fecharegistro, blnborrado', '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(
		'trabajador' => array(self::BELONGS_TO, 'Asociado', 'idtrabajador'),
                 'estatus' => array(self::BELONGS_TO, 'Estatus', 'estatus'),
                 'parent' => array(self::BELONGS_TO, 'Parentesco', 'parentesco'),
                );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idsolicitud' => 'Idsolicitud',
			'idtrabajador' => 'Codigo de Trabajador',
			'cedula' => 'Cedula del difunto',
			'nombre' => 'Nombre del difunto',
			'apellido' => 'Apellido del difunto ',
			'parentesco' => 'Parentesco del difunto',
			'lugar' => 'Lugar del suceso',
			'fecha_siniestro' => 'Fecha suceso',
			'fecharegistro' => 'Fecha registro',
			'blnborrado' => 'Blnborrado',
                            'estatus' => '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('idsolicitud',$this->idsolicitud);
		$criteria->compare('idtrabajador',$this->idtrabajador);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('parentesco',$this->parentesco);
		$criteria->compare('lugar',$this->lugar,true);
		$criteria->compare('fecha_siniestro',$this->fecha_siniestro,true);
		$criteria->compare('fecharegistro',$this->fecharegistro,true);
		$criteria->compare('blnborrado',$this->blnborrado);
                $criteria->with = 'trabajador';
                $criteria->addSearchCondition('trabajador.nombre || trabajador.apellidos || trabajador.cedula',  strtoupper($this->idtrabajador),true);
                $criteria->addSearchCondition('trabajador.idunidad::text',$this->unidad,false);
	$_SESSION['datos_filtrados'] = new CActiveDataProvider($this, array(
                      'criteria'=>$criteria,
                      'pagination'=>false,
              ));
                
                return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
 
	}
	public function searchAnalista()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
                $uni=Yii::app()->user->name;
                /*$user=Usuario::model()->findByPk(Yii::app()->user->name);		
		$idunidad=$user['username'];
		$uni=Unidad::model()->findAll("codigo='$idunidad' and blnborrado=false");*/		
                //$filtro="idunidad=" . $uni[0]['idunidad'] ." and t.blnborrado=false ";		
		$criteria->select="t.*";
		//$criteria->condition="t.idtrabajador=(select max(b.idasociado) from asociado b where b.idasociado = t.idtrabajador and b.idunidad=" . $uni .") ";         
		$criteria->condition="t.idtrabajador=(select max(b.idasociado) from asociado b where b.idasociado = t.idtrabajador) ";         
		$sort=new CSort();
		$criteria->compare('idsolicitud',$this->idsolicitud);
		$criteria->compare('idtrabajador',$this->idtrabajador);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('parentesco',$this->parentesco);
		$criteria->compare('lugar',$this->lugar,true);
		$criteria->compare('fecha_siniestro',$this->fecha_siniestro,true);
		$criteria->compare('fecharegistro',$this->fecharegistro,true);
		$criteria->compare('blnborrado',$this->blnborrado);
                $criteria->with = 'trabajador';
                $criteria->addSearchCondition('trabajador.nombre || trabajador.apellidos || trabajador.cedula',  strtoupper($this->idtrabajador),true);
                $criteria->addSearchCondition('trabajador.idunidad::text',$this->unidad,false);
	$_SESSION['datos_filtrados'] = new CActiveDataProvider($this, array(
                      'criteria'=>$criteria,
                      'pagination'=>false,
              ));
                
                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 MontePio the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

       public function behaviors()
	{
	    return array(
		    // Classname => path to Class
		    'ActiveRecordLogableBehavior'=>
		    	'application.behaviors.ActiveRecordLogableBehavior',
	    );
	}
}
