<?php

/**
 * This is the model class for table "contable.cierre_trimestral".
 *
 * The followings are the available columns in table 'contable.cierre_trimestral':
 * @property integer $id
 * @property string $trimestre
 * @property string $meses
 * @property boolean $estatus
 */
class CierreTrimestral extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contable.cierre_trimestral';
	}
         public $nombre_periodo;

	/**
	 * @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('trimestre, meses, estatus', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, trimestre, meses, estatus,id_periodo', '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(
                     'periodo' => array(self::BELONGS_TO, 'EjerciciosContables', 'id_periodo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'trimestre' => 'Trimestre',
			'meses' => 'Meses',
			'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()
    {
        $cuentaperiodos = CierreTrimestral::model()->findAllBySql('select distinct (id_periodo) from contable.cierre_trimestral;');
        $totalperiodo = count($cuentaperiodos);

        $criteria = new CDbCriteria;
        if ($totalperiodo >= 2) {
            $criteria->condition = 'id_periodo=(select id from contable.ejercicios where activo=1)';
        }

        $criteria->compare('id', $this->id);
        $criteria->compare('LOWER(trimestre)', strtolower($this->trimestre), true);
        $criteria->compare('LOWER(meses)', strtolower($this->meses), true);
        $criteria->compare('estatus', $this->estatus);
        $criteria->compare('id_periodo', $this->id_periodo);
        $criteria->order = 'id ASC';

        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 CierreTrimestral the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function estatus($campo){
            if ($campo==true){
                $campo='CERRADO';
            }
            else{
                 $campo='ABIERTO';
            }
            return $campo;
        }
        
        public function getColor() {
        
        $statuscolor='white';
        switch ($this->estatus) {
            case true:
                $statuscolor='rojo';
//                $statuscolor='negrita';
                break;
             
             default :
                $statuscolor='white';
                break;       
        }
        return $statuscolor;
        
    }
}
