<?php

/**
 * This is the model class for table "configuracion.banco_tipo_servicio".
 *
 * The followings are the available columns in table 'configuracion.banco_tipo_servicio':
 * @property integer $id
 * @property string $idbanco
 * @property string $codigo_servicio
 * @property integer $tipo_descarga
 * @property string $id_tipo_servicio
 *
 * The followings are the available model relations:
 * @property EstructuraArchivoBanco[] $estructuraArchivoBancos
 * @property TipoServicio $idTipoServicio
 * @property Banco $idbanco
 */
class BancoTipoServicio extends CActiveRecord
{
    public $nombre;
    public static $TipoDescarga = ['1'=>'Cuenta del mismo banco','2'=>'Cuentas de otros banco','3'=>'Mixto'];

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'configuracion.banco_tipo_servicio';
	}

	/**
	 * @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('idbanco, codigo_servicio, tipo_descarga, id_tipo_servicio', 'required'),
			array('tipo_descarga', 'numerical', 'integerOnly'=>true),
			array('idbanco, codigo_servicio, id_tipo_servicio', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, idbanco, codigo_servicio, tipo_descarga, id_tipo_servicio', '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(
			'estructuraArchivoBancos' => array(self::HAS_MANY, 'EstructuraArchivoBanco', 'id_banco_tipo_servicio'),
			'idTipoServicio' => array(self::BELONGS_TO, 'TipoServicio', 'id_tipo_servicio'),
			'idBanco' => array(self::BELONGS_TO, 'Banco', 'idbanco'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'idbanco' => 'Banco',
			'codigo_servicio' => 'Código del servicio',
			'tipo_descarga' => 'Tipo de descarga',
			'id_tipo_servicio' => 'Tipo de servicio',
		);
	}

	/**
	 * 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('idbanco',$this->idbanco);
		$criteria->compare('codigo_servicio',$this->codigo_servicio,true);
		$criteria->compare('tipo_descarga',$this->tipo_descarga);
		$criteria->compare('id_tipo_servicio',$this->id_tipo_servicio);

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

	public function getTipoDescarga($id = NULL){
		if($id!==null){
			return self::$TipoDescarga[$id];
		}//
			
		return self::$TipoDescarga;
	}

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

    public static function tipos($id)
    {
        return self::model()->findAll(array(
            'select' => 'bts.id, codigo_servicio, concat(ts.nombre, \' - \', td.nombre) as nombre',
            'alias' => 'bts',
            'join' => 'inner join configuracion.tipo_servicio ts on bts.id_tipo_servicio=ts.id '.
                      'inner join configuracion.tipo_descarga td on bts.tipo_descarga=td.id',
            'condition' => 'bts.idbanco=:id_banco',
            'params' => array(
                'id_banco' => $id,
            ),
        ));
    }
}
