<?php

/**
 * This is the model class for table "asociado_dividendos".
 *
 * The followings are the available columns in table 'asociado_dividendos':
 * @property integer $id
 * @property integer $idasociado
 * @property integer $ano
 * @property double $dividendo
 * @property boolean $activo
 * @property string $fecha_registro
 * @property integer $cedula
 */
class AsociadoDividendos extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'asociado_dividendos';
	}

	/**
	 * @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('ano, dividendo', 'required'),
			array('idasociado, ano, cedula', 'numerical', 'integerOnly'=>true),
			array('dividendo', 'numerical'),
			array('activo, fecha_registro', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, idasociado, ano, dividendo, activo, fecha_registro, 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(
                   'asociado' => array(self::BELONGS_TO, 'Asociado', 'idasociado'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'idasociado' => 'Idasociado',
			'ano' => 'Año',
			'dividendo' => 'Dividendo',
			'activo' => 'Estatus',
			'fecha_registro' => 'Fecha Registro',
			'cedula' => 'Cedula',
		);
	}

	/**
	 * 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('idasociado',$this->idasociado);
		$criteria->compare('ano',$this->ano);
		$criteria->compare('dividendo',$this->dividendo);
		$criteria->compare('activo',$this->activo);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('cedula',$this->cedula);

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

    public function criteriaSearchDividendos($id)
    {
        $criteria=new CDbCriteria;
        if(isset($_GET['cedula'])){
            if(is_numeric($_GET['cedula'])){
                $id = 0;
                $model= Asociado::model()->find('cedula=:cedula AND blnborrado= FALSE AND id_estatus in(1,4) ORDER BY idasociado desc',array(':cedula'=>$_GET['cedula']));
                if($model)
                  $id = $model->idasociado;
            }
            else {
                $id = 0;
            }
        }
        $criteria->compare('id',$this->id);
        $criteria->compare('idasociado',$id);
        $criteria->compare('ano',$this->ano);
        $criteria->compare('dividendo',$this->dividendo);
        $criteria->compare('activo',$this->activo);
        $criteria->compare('fecha_registro',$this->fecha_registro,true);
        $criteria->compare('cedula',$this->cedula);

        return $criteria;
    }

	public function searchDividendosSocio($id)
	{
        $criteria = $this->criteriaSearchDividendos($id);

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

    public function searchDividendosSocioFichaLiquidacion($id, $params)
    {
        $criteria = $this->criteriaSearchDividendos($id);
        $criteria->addColumnCondition([
            'activo' => true
        ]);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => [
                'pageSize' => 10,
                'params'  => $params,
            ],
        ));
    }

    public function dividendosSocioFichaLiquidacion($id)
    {
        $criteria = $this->criteriaSearchDividendos($id);
        $criteria->addColumnCondition([
            'activo' => true
        ]);

        $datos = self::model()->findAll($criteria);

        return (new Warp($datos))->map(function ($dividendo) {
            return [
                'id' => $dividendo->id,
                'ano' => $dividendo->ano,
                'dividendo' => $dividendo->dividendo,
                'fecha_registro' => ! empty($dividendo->fecha_registro) ? $dividendo->fecha_registro : "",
                'activo' => $dividendo->activo == 1 ? 'Aprobado' : 'Cargado',
            ];
        });
    }

	/**
	 * 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 AsociadoDividendos 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',
	    );
	}
}
