<?php

/**
 * This is the model class for table "porcentaje_descuento_asociado_patrono".
 *
 * The followings are the available columns in table 'porcentaje_descuento_asociado_patrono':
 * @property string $idasociado
 * @property integer $id_porcentaje_asociado
 * @property integer $id_porcentaje_patrono
 * @property boolean $blnborrado
 * @property string $fecharegistro
 * @property integer $id
 *
 * The followings are the available model relations:
 * @property Asociado $idasociado0
 * @property ConfPorcentaje $idPorcentajeAsociado
 * @property ConfPorcentaje $idPorcentajePatrono
 */
class PorcentajeDescuentoAsociadoPatrono extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'porcentaje_descuento_asociado_patrono';
	}

	/**
	 * @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('id_porcentaje_asociado', 'required'),
			array('id_porcentaje_asociado, id_porcentaje_patrono', 'numerical', 'integerOnly'=>true),
			array('idasociado, id_porcentaje_asociado,blnborrado, fecharegistro', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idasociado, id_porcentaje_asociado, id_porcentaje_patrono, blnborrado, fecharegistro, id', '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(
			'idAsociado' => array(self::BELONGS_TO, 'Asociado', 'idasociado'),
			'idPorcentajeAsociado' => array(self::BELONGS_TO, 'Porcentaje', 'id_porcentaje_asociado'),
			'idPorcentajePatrono' => array(self::BELONGS_TO, 'Porcentaje', 'id_porcentaje_patrono'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idasociado' => 'Idasociado',
			'id_porcentaje_asociado' => 'Porcentaje',
			'id_porcentaje_patrono' => 'Id Porcentaje Patrono',
			'blnborrado' => 'Blnborrado',
			'fecharegistro' => 'Fecharegistro',
			'id' => 'ID',
		);
	}

        
        public function getIdPorcentajePatrono(){
            
            $id=NULL;
            //buscara si el porcentaje del patrono es fijo                    
            $modelPorcentajePatrono = ConfPorcentaje::model()->find('blnborrado= false AND trabajador = false AND tipo_aporte= 1');
            if($modelPorcentajePatrono){
                $id = $modelPorcentajePatrono->idporcentaje;
            }
            else{
                //buscara si el porcentaje del patrono es equivalente al del asociado     
                $modelPorcentajePatrono = ConfPorcentaje::model()->find('blnborrado= false AND trabajador = false AND tipo_aporte= 2');
                if($modelPorcentajePatrono){
                    $id = $modelPorcentajePatrono->idporcentaje;
                }
                else{
                     $modelAsociado =$this->idAsociado;
                    $modelPorcentajePatrono = ConfPorcentaje::model()->find('idunidad=:idunidad AND idtipotrabajador=:idtipotrabajador AND blnborrado= false AND trabajador = false AND tipo_aporte= 3',
                                                                                array(':idunidad'=>$modelAsociado->idunidad,':idtipotrabajador'=>$modelAsociado->idtipotrabajador));
                    if($modelPorcentajePatrono){
                       $id = $modelPorcentajePatrono->idporcentaje;                     
                    } 
                    else{
                        $modelPorcentajePatrono = ConfPorcentaje::model()->find('idunidad=:idunidad  AND blnborrado= false AND trabajador = false AND tipo_aporte= 3',
                                                                                array(':idunidad'=>$modelAsociado->idunidad));
                        if($modelPorcentajePatrono){
                            $id = $modelPorcentajePatrono->idporcentaje;     
                        }
                        else{
                            $modelPorcentajePatrono = ConfPorcentaje::model()->find('idtipotrabajador=:idtipotrabajador AND blnborrado= false AND trabajador = false AND tipo_aporte= 3',
                                                                                array(':idtipotrabajador'=>$modelAsociado->idtipotrabajador));
                            if($modelPorcentajePatrono){
                               $id = $modelPorcentajePatrono->idporcentaje;                     
                            } 
                        }
                    }
                }
            }
            
            return $id;
            
        }
        
	/**
	 * 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('idasociado',$this->idasociado,true);
		$criteria->compare('id_porcentaje_asociado',$this->id_porcentaje_asociado);
		$criteria->compare('id_porcentaje_patrono',$this->id_porcentaje_patrono);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('fecharegistro',$this->fecharegistro,true);
		$criteria->compare('id',$this->id);

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