<?php

/**
 * This is the model class for table "contable.param_unidad".
 *
 * The followings are the available columns in table 'contable.param_unidad':
 * @property integer $id
 * @property integer $clvcuenta
 * @property integer $clvunidad
 * @property boolean $blnborrado
 */
class ParametrosUnidades extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contable.param_unidad';
	}

        public $cuenta_descripcion;
        public $codigo_cuenta;
	/**
	 * @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('clvcuenta, clvunidad', 'numerical', 'integerOnly'=>true),
			array('clvcuenta, clvunidad,id_categoria, id_clave','required'),
			array('clvcuenta, clvunidad,id_categoria,cuenta_descripcion', 'required','on'=>'crear'),
			array('blnborrado', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, clvcuenta, clvunidad, blnborrado,id_categoria', '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(
                'cuenta' => array(self::BELONGS_TO, 'CuentasConsolidada', 'clvcuenta'),
                'unid' => array(self::BELONGS_TO, 'Unidad', 'clvunidad'),
//                'categoria' => array(self::BELONGS_TO, 'ParamCategorias', 'id_categoria'),
                'Escenario' => array(self::BELONGS_TO, 'ParamEscenarios', 'id_categoria'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'clvcuenta' => 'Cuenta contable',
			'clvunidad' => 'Unidad',
			'id_categoria' => 'Categoria',
            'blnborrado' => 'Blnborrado',
			'id_clave' => 'Clave',
		);
	}

	/**
	 * 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->condition="t.blnborrado=false";
        $criteria->compare('id',$this->id);
        $criteria->compare('blnborrado',$this->blnborrado);
        $criteria->compare('t.id_categoria',$this->id_categoria);
        $criteria->compare('clvunidad',$this->clvunidad);
                $criteria->with =array('cuenta','unid','Escenario') ;
                $criteria->addSearchCondition('cuenta.codigo_cuenta || LOWER(cuenta.descripcion)',  strtolower($this->clvcuenta),true);
//                $criteria->addSearchCondition('LOWER(unid.descripcion)',  strtolower($this->clvunidad),true);
//                $criteria->addSearchCondition('LOWER(categoria.denominacion)',  strtolower($this->id_categoria),true);

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

    public function searchPorEscenario($id, $clave)
	{
		$criteria = new CDbCriteria;
        $criteria->with = array('cuenta', 'unid', 'Escenario') ;
        $criteria->compare('id', $this->id);
        $criteria->compare('blnborrado', $this->blnborrado);
        $criteria->compare('clvunidad', $this->clvunidad);
        $criteria->condition = "t.blnborrado=false";
        $criteria->addCondition("t.id_categoria={$id} AND t.id_clave={$clave}");
        $criteria->addSearchCondition('cuenta.codigo_cuenta || LOWER(cuenta.descripcion)', strtolower($this->clvcuenta), true);

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

        public function concatenar($campo1,$campo2){

            $nombrec=$campo1.'-'.$campo2;

            return $nombrec;
        }
}
