<?php

/**
 * This is the model class for table "retenciones.estatus_aporte_voluntario".
 *
 * The followings are the available columns in table 'retenciones.estatus_aporte_voluntario':
 * @property integer $id
 * @property integer $id_status_av
 * @property string $id_aporte_voluntario
 * @property string $fecha_registro
 * @property boolean $actual
 * @property string $id_user
 *
 * The followings are the available model relations:
 * @property AporteVoluntario $idAporteVoluntario
 * @property EstatusAporte $idStatusAv
 */
class EstatusAporteVoluntario extends CActiveRecord
{
	public $monto_aporte_anterior;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'retenciones.estatus_aporte_voluntario';
	}

	/**
	 * @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('fecha_registro, observaciones', 'required', 'message'=>'El campo {attribute} no puede estar vacio', 'on'=>'aprobarAporte'),
			array('fecha_registro', 'required', 'message'=>'El campo {attribute} no puede estar vacio', 'on'=>'aprobarRetiro'),
			array('id_status_av', 'numerical', 'integerOnly'=>true),
			array('id_aporte_voluntario, id_user, observaciones', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, id_status_av, id_aporte_voluntario, fecha_registro, actual, id_user, observaciones, monto_aporte_anterior', '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(
			'idAporteVoluntario' => array(self::BELONGS_TO, 'AporteVoluntario', 'id_aporte_voluntario'),
			'idStatusAv' => array(self::BELONGS_TO, 'EstatusAporte', 'id_status_av'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_status_av' => 'Estatus del aporte',
			'id_aporte_voluntario' => 'Aporte Voluntario',
			'fecha_registro' => 'Fecha estatus',
			'actual' => 'Actual',
			'id_user' => 'Usuario',
			'observaciones' => 'Observaciones',
		);
	}

	/**
	 * 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('id_status_av',$this->id_status_av);
		$criteria->compare('id_aporte_voluntario',$this->id_aporte_voluntario,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('actual',$this->actual);
		$criteria->compare('id_user',$this->id_user,true);
		$criteria->compare('observaciones',$this->observaciones,true);

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

	public function validacionTiempoSolicitud(){
        $tiempo = 0;
        $fechainicial = new DateTime($this->fecha_registro);
        $fechafinal = new DateTime(date('Y-m-d'));

        $diferencia = $fechainicial->diff($fechafinal);
        $tiempo = ( $diferencia->y * 12 ) + $diferencia->m;
        return $tiempo;
    }

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

    public static function seguimiento($idAporte, $idEstatus)
    {
        self::model()->updateAll(array('actual' => false), 'id_aporte_voluntario=:id', array(':id' => $idAporte));

        $model = new self();
        $model->id_status_av = $idEstatus;
        $model->id_aporte_voluntario = $idAporte;
        $model->id_user = Yii::app()->user->id;
        if (! $model->save()) {
            throw new Exception('Error al hacer seguimiento del aporte');
        }
    }

    public static function create($attributes)
    {
        self::model()->updateAll(['actual' => false], 'id_aporte_voluntario=:id', [
            'id' => $attributes['id_aporte_voluntario'],
        ]);

        $model = new self();
        $model->setAttributes(array_merge([
            'id_user' => Yii::app()->user->id,
        ], $attributes));

        if (! $model->save()) {
            return false;
        }

        return true;
    }
}
