<?php

/**
 * This is the model class for table "sidcai_ciiu".
 *
 * The followings are the available columns in table 'sidcai_ciiu':
 * @property integer $ciiu_codigo_pk
 * @property string $ciiu_habilitado
 * @property string $ciiu_porcentaje
 * @property string $ciiu_descripcion
 * @property string $ciiu_anno_ley
 * @property string $ciiu_porcentaje_2010
 * @property string $ciiu_porcentaje_2010_priv
 * @property integer $sector_codigo_fk
 */
class SidcaiCiiu extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sidcai_ciiu';
	}

	/**
	 * @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('ciiu_codigo_pk, ciiu_descripcion', 'required'),
			array('ciiu_codigo_pk, sector_codigo_fk', 'numerical', 'integerOnly'=>true),
			array('ciiu_habilitado, ciiu_porcentaje, ciiu_anno_ley, ciiu_porcentaje_2010, ciiu_porcentaje_2010_priv', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('ciiu_codigo_pk, ciiu_habilitado, ciiu_porcentaje, ciiu_descripcion, ciiu_anno_ley, ciiu_porcentaje_2010, ciiu_porcentaje_2010_priv, sector_codigo_fk', '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(
		);
	}

	/**
	 * 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('ciiu_codigo_pk',$this->ciiu_codigo_pk);
		$criteria->compare('ciiu_habilitado',$this->ciiu_habilitado,true);
		$criteria->compare('ciiu_porcentaje',$this->ciiu_porcentaje,true);
		$criteria->compare('ciiu_descripcion',$this->ciiu_descripcion,true);
		$criteria->compare('ciiu_anno_ley',$this->ciiu_anno_ley,true);
		$criteria->compare('ciiu_porcentaje_2010',$this->ciiu_porcentaje_2010,true);
		$criteria->compare('ciiu_porcentaje_2010_priv',$this->ciiu_porcentaje_2010_priv,true);
		$criteria->compare('sector_codigo_fk',$this->sector_codigo_fk);

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

	public static function getCiiu(){
		$sql = "SELECT ciiu_codigo_pk, CONCAT (ciiu_codigo_pk,' - ', ciiu_descripcion) as nombre FROM sidcai_ciiu WHERE ciiu_habilitado = TRUE AND ciiu_codigo_pk != 1 ORDER BY ciiu_codigo_pk ASC";
		$ciiu = Yii::app()->db->createCommand($sql)->queryAll();

		return CHtml::listData($ciiu, 'ciiu_codigo_pk', 'nombre');
	}

	public static function getCiiuBuscar(){
		$sql = "SELECT ciiu_codigo_pk, CONCAT ('(', ciiu_codigo_pk, '%) - ', ciiu_descripcion) as nombre FROM sidcai_ciiu WHERE ciiu_habilitado = TRUE AND ciiu_codigo_pk != 1";
		$ciiu = Yii::app()->db->createCommand($sql)->queryAll();

		return CHtml::listData($ciiu, 'ciiu_codigo_pk', 'nombre');
	}
}
