<?php

/**
 * This is the model class for table "ajuste_haberes.datos_nomina_ajuste_haberes".
 *
 * The followings are the available columns in table 'ajuste_haberes.datos_nomina_ajuste_haberes':
 * @property integer $id
 * @property integer $total_asociado
 * @property double $total_ajuste
 * @property string $fecha_ajuste
 * @property string $nombre_archivo
 * @property string $num_comprobante
 * @property string $descripcion
 * @property integer $id_concepto_ajuste
 *
 * The followings are the available model relations:
 * @property AjusteHaberes[] $ajusteHaberes
 * @property DatosNominaHaberesSeguimiento[] $datosNominaHaberesSeguimientos
 * @property ConceptoAjuste $idConceptoAjuste
 */
class DatosNominaAjusteHaberes extends CActiveRecord
{
	public $fecha_operacion_range = array();
	public $fecha_estatus_range = array();
	public $fecha_estatus,$id_estatus,$fecha_registro,$nombre_estatus;
	public $nombre_archivo_carga;
    public $fecha_comprobante;
    public $fecha_referencia;
	public $referencia;

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

	/**
	 * @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('nombre_archivo,descripcion, id_concepto_ajuste, id_tipo_ajuste','required','on'=>array('RegistroMasivo')),
			array('nombre_archivo', 'match',
		          'pattern' => '/^(AH)\d{8}.xlsx|xls$/',
		          'message' => 'El nombre del archivo debe tener un formato AHDDMMAAAA.xls o AHDDMMAAAA.xlsx',
		          'on'=>'RegistroMasivo',
		      ),
			array('nombre_archivo,total_asociado,total_ajuste','required','on'=>'carga_ajuste'),
			array('nombre_archivo','ValidarNombreArchivo','on'=>'carga_ajuste'),
            array('fecha_ajuste','date', 'format'=>'dd-MM-yyyy', 'message' => 'El formato de Fecha del ajuste no coincide con el formado DD-MM-AAAA.', 'on'=>'carga_ajuste'),
            array('total_asociado', 'numerical', 'integerOnly'=>true,'on'=>'carga_ajuste'),
            array('total_ajuste', 'length', 'max'=>20),
        //     array('total_ajuste', 'match',
		      //     'pattern' => '/^[0-9]\d{0,2}(\.[0-9]\d{2,2})*(\,\d{1,2})?$/',
		      //     'message' => 'El valor del campo debe tener un formato 1.000,00',
		      //     'on'=>'carga_ajuste',
	      	// ),

			array('total_asociado, id_concepto_ajuste', 'numerical', 'integerOnly'=>true),
			array('fecha_ajuste, nombre_archivo, num_comprobante, descripcion,nombre_archivo_carga, fecha_comprobante', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, total_asociado, total_ajuste, fecha_ajuste, nombre_archivo, num_comprobante, descripcion, id_concepto_ajuste', '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(
			'ajusteHaberes' => array(self::HAS_MANY, 'AjusteHaberes', 'id_nomina_ajuste_haberes'),
			'datosNominaHaberesSeguimientos' => array(self::HAS_MANY, 'DatosNominaHaberesSeguimiento', 'id_datos_nomina_ajuste_haberes'),
			'idConceptoAjuste' => array(self::BELONGS_TO, 'ConceptoAjuste', 'id_concepto_ajuste'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'total_asociado' => 'Total asociado',
			'total_ajuste' => 'Total ajuste',
			'fecha_ajuste' => 'Fecha del ajuste',
			'nombre_archivo' => 'Nombre del archivo',
			'num_comprobante' => 'Num Comprobante',
			'descripcion' => 'Descripción',
			'id_concepto_ajuste' => 'Concepto del ajuste',
			'id_tipo_ajuste' => 'Tipo de ajuste',
			'fecha_comprobante' => 'Fecha de comprobante',
		);
	}

	/**
	 * 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 ValidarNombreArchivo(){
		if(!$this->hasErrors('nombre_archivo')){
			$model = DatosNominaHaberesSeguimiento::model()->with('idDatosNominaAjusteHaberes')->find('nombre_archivo=:nombre_archivo AND actual = TRUE AND id_estatus_ajuste_haberes !=3',
																										[':nombre_archivo'=>$this->nombre_archivo]	);
			if($model){
				$this->addError('nombre_archivo','El nombre del archivo ya se encuentra en uso');
			}
		}
	}
	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('total_asociado',$this->total_asociado);
		$criteria->compare('total_ajuste',$this->total_ajuste);
		$criteria->compare('fecha_ajuste',$this->fecha_ajuste,true);
		$criteria->compare('nombre_archivo',$this->nombre_archivo,true);
		$criteria->compare('num_comprobante',$this->num_comprobante,true);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('id_concepto_ajuste',$this->id_concepto_ajuste);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function afterFind(){
        $model = DatosNominaHaberesSeguimiento::model()->find('id_datos_nomina_ajuste_haberes=:id AND actual =  TRUE',array(':id'=>  $this->id));
        if($model){
            $this->nombre_estatus = $model->idEstatusAjusteHaberes->descripcion;
            $this->id_estatus = $model->id_estatus_ajuste_haberes;
            $this->fecha_estatus = date('d-m-Y',strtotime($model->fecha_registro));
        }
		$model = DatosNominaHaberesSeguimiento::model()->find('id_datos_nomina_ajuste_haberes=:id ORDER BY id ASC',array(':id'=>  $this->id));
        if($model){
            $this->fecha_registro = date('d-m-Y',strtotime($model->fecha_registro));
        }

        return parent::afterFind();
    }

    public function getReferencia()
    {
        return $this->referencia;
    }

    public function setReferencia($referencia)
    {
        $this->referencia = $referencia;
    }

    public function getFechaReferencia()
    {
        return $this->fecha_referencia;
    }

    public function setFechaReferencia($fecha_referencia)
    {
        $this->fecha_referencia = $fecha_referencia;
    }

    public function getFechaComprobante()
    {
        return $this->fecha_comprobante;
    }

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