<?php

/**
 * This is the model class for table "public.beneficiario".
 *
 * The followings are the available columns in table 'public.beneficiario':
 * @property integer $idbeneficiario
 * @property integer $idasociado
 * @property string $nombre
 * @property string $apellido
 * @property integer $parentesco
 * @property string $lugar
 * @property string $fecha
 * @property boolean $blnborrado
 */
class Beneficiario extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Beneficiario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'public.beneficiario';
	}

	/**
	 * @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('idasociado, nombre, apellido, fecha', 'required', 'message'=>'El campo {attribute} no puede estar vacio'),
			array('idasociado, parentesco', 'numerical', 'integerOnly'=>true),
			//array('cedula', 'numerical', 'integerOnly'=>true, 'allowEmpty'=>false, 'message'=>'El campo {attribute} no puede estar vacio'),
			array('cedula', 'length',
							'max'=>9,
							'tooLong'=>'La cédula debe tener máximo {max} dígitos',
							'on'=>'saveBeneficiario'),
			array('nombre, apellido, lugar', 'length', 'max'=>100),
			array('idbeneficiario, fecha, blnborrado', 'safe'),
			array('idbeneficiario', 'safe', 'on' => 'solicitudma'),
			array('idbeneficiario', 'required', 'on' => 'solicitudma'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idbeneficiario, idasociado, nombre, apellido, parentesco, lugar, fecha, blnborrado, cedula', '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(
		'parent' => array(self::BELONGS_TO, 'Parentesco', 'parentesco'),
		'idAsociado' => array(self::BELONGS_TO, 'Asociado', 'idasociado'),
		'idStatus' => array(self::BELONGS_TO, 'EstatusAsociado', 'id_status'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idbeneficiario' => 'Idbeneficiario',
			'idasociado' => 'Idasociado',
			'nombre' => 'Nombre',
			'apellido' => 'Apellido',
			'parentesco' => 'Parentesco',
			'lugar' => 'Lugar',
			'fecha' => 'Fecha de Nacimiento',
			'fecha_fallece' => 'Fecha de Fallecimiento',
			'blnborrado' => 'Blnborrado',
			'cedula' => 'Cédula',
			'id_status' => 'Estado',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->order = "FIELD(idbeneficiario ASC)";

		$criteria->compare('idbeneficiario',$this->idbeneficiario);
		$criteria->compare('idasociado',$this->idasociado);
		$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',$this->fecha,true);
		$criteria->compare('fecha_fallece',$this->fecha_fallece,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('id_status',$this->id_status);

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

	public function searchBenefid($id)
	{

		$criteria = new CDbCriteria;
        if(isset($_GET['cedula'])){//echo $_GET['cedula'];exit;
            if(is_numeric($_GET['cedula'])){                    
                $id= Asociado::model()->find('cedula=:cedula AND blnborrado= FALSE',array(':cedula'=>$_GET['cedula']))->idasociado;
            }
            else {
                $id = 0;
            }
        }

        $criteria->condition = 'idasociado=' . (INT)$id . 'AND "blnborrado"=false AND "id_status"=1';
        /*$criteria = array(
                'condition'=>'"idasociado"='.$modelAsociado->idasociado.' AND "blnborrado"=false',
            );*/
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria->compare('idbeneficiario',$this->idbeneficiario);
		$criteria->compare('idasociado',$this->idasociado);
		$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',$this->fecha,true);
		$criteria->compare('fecha_fallece',$this->fecha_fallece,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('id_status',$this->id_status);

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

    public static function getAll($id)
    {
        return self::model()->with('parent')->findAll([
            'alias' => 'b',
            'condition' => 'b.blnborrado IS FALSE and b.idasociado=:id',
            'params' => array(':id' => $id)
        ]);
    }
}
