<?php

/**
 * This is the model class for table "montepio.aporte_fondo_diferido".
 *
 * The followings are the available columns in table 'montepio.aporte_fondo_diferido':
 * @property integer $id
 * @property integer $id_txt
 * @property integer $cedula
 * @property integer $unidad
 * @property integer $t_nomina
 * @property string $aporte_asociado
 * @property string $fecha_registro
 * @property string $observaciones
 * @property boolean $blnborrado
 * @property integer $id_status
 *
 * The followings are the available model relations:
 * @property DatosTxtIntegrado $idTxt
 * @property EstatusAporteOrdinarioDiferido $idStatus
 */
class AporteFondoDiferido extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'montepio.aporte_fondo_diferido';
	}

	/**
	 * @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_txt, cedula, unidad, t_nomina, id_status', 'numerical', 'integerOnly'=>true),
			array('aporte_asociado', 'length', 'max'=>20),
			array('fecha_registro, observaciones, blnborrado', 'safe'),

			array('id_status', 'required', 'on'=>'asociado_cmaf'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, id_txt, cedula, unidad, t_nomina, aporte_asociado, fecha_registro, observaciones, blnborrado, id_status', '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(
			'idTxt' => array(self::BELONGS_TO, 'DatosTxtIntegrado', 'id_txt'),
			'idStatus' => array(self::BELONGS_TO, 'EstatusAporteOrdinarioDiferido', 'id_status'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_txt' => 'Txt',
			'cedula' => 'Cedula',
			'unidad' => 'Unidad',
			't_nomina' => 'Tipo de Nomina',
			'aporte_asociado' => 'Aporte Asociado',
			'fecha_registro' => 'Fecha Registro',
			'observaciones' => 'Observaciones',
			'blnborrado' => 'Blnborrado',
			'id_status' => '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($id)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('id_txt',$id);
		$criteria->compare('cedula',$this->cedula);
		$criteria->compare('unidad',$this->unidad);
		$criteria->compare('t_nomina',$this->t_nomina);
		$criteria->compare('aporte_asociado',$this->aporte_asociado,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('observaciones',$this->observaciones,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('id_status',$this->id_status);

		$criteria->addCondition('blnborrado=FALSE');

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

	public function getArea()
        {
            if($this->unidad)
            {
                $modelArea=  Unidad::model()->findByPk($this->unidad);
                if($modelArea) return $modelArea->descripcion;
            }    
          return 'Area Desconocida';      
        }

	protected function beforeSave() {
		
            #Monto en Bs
        if($this->scenario='cm_aportes_fondo_diferidos'){
            $this->aporte_asociado=str_replace(',', '.', $this->aporte_asociado);
            if($this->aporte_asociado=='')$this->aporte_asociado=NULL;                
        }

		return parent::beforeSave();
	}

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