<?php

/**
 * This is the model class for table "montepio.recaudos_mpma".
 *
 * The followings are the available columns in table 'montepio.recaudos_mpma':
 * @property integer $id
 * @property integer $id_solicitud
 * @property integer $id_tipodoc
 * @property string $nombre_archivo
 * @property string $fecha_registro
 * @property integer $estado_documento
 * @property boolean $blnborrado
 * @property boolean $nombre_hash
 *
 * The followings are the available model relations:
 * @property Solicitud $idSolicitud
 * @property Documentos $idTipodoc
 */
class RecaudosMpma extends CActiveRecord
{
	public $ci_solicitante, $ci_fallecido, $acta_defuncion, $herederos;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'montepio.recaudos_mpma';
	}

	/**
	 * @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_solicitud, id_tipodoc, estado_documento', 'numerical', 'integerOnly'=>true),
			array('nombre_archivo, blnborrado', 'safe'),

			array('ci_solicitante', 'validarExtension', 'on'=>'loadFiles'),
			array('ci_fallecido, acta_defuncion, herederos', 'file', 
															'allowEmpty'=>true,
															'types'=>'jpg, jpeg, png, pdf',
															'wrongType'=>'La extensión del archivo {attribute} debe ser jpg, png ó pdf',
															'message'=>'Archivo no admitido {attribute}',
															'safe' => false,
				),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, id_solicitud, id_tipodoc, nombre_archivo, fecha_registro, estado_documento, blnborrado', '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(
			'idSolicitud' => array(self::BELONGS_TO, 'Solicitud', 'id_solicitud'),
			'idTipodoc' => array(self::BELONGS_TO, 'Documentos', 'id_tipodoc'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'id_solicitud' => 'Nro. de solicitud',
			'id_tipodoc' => 'Tipo de documento',
			'nombre_archivo' => 'Nombre del archivo',
			'fecha_registro' => 'Fecha Registro',
			'estado_documento' => 'Estado del documento',
			'blnborrado' => 'Blnborrado',
			'nombre_hash' => 'Nombre',
		);
	}

	/**
	 * 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('id_solicitud',$this->id_solicitud);
		$criteria->compare('id_tipodoc',$this->id_tipodoc);
		$criteria->compare('nombre_archivo',$this->nombre_archivo,true);
		$criteria->compare('fecha_registro',$this->fecha_registro,true);
		$criteria->compare('estado_documento',$this->estado_documento);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('nombre_hash',$this->nombre_hash);

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

	public function validarExtension(){

		$ext_permitidas = array('jpeg', 'jpg', 'png', 'pdf');
		
		if (!empty($this->ci_solicitante)) {
			
			if (is_array($this->ci_solicitante)) {
				
				foreach ($this->ci_solicitante as $k => $v) {
					
					$ext = explode('.', $v->name);

					if (count($ext) >= 2) {

						$a = array_search($ext[count($ext)-1], $ext_permitidas);

						if ($a==="") {
						
							$this->addError('ci_solicitante', 'La extensión del archivo '.$v->name.' debe ser jpg, png ó pdf ');
							return false;
						}

					}else{

						$a = array_search($ext[1], $ext_permitidas);

						if ($a==="") {
							
							$this->addError('ci_solicitante', 'La extensión del archivo '.$v->name.' debe ser jpg, png ó pdf ');
							return false;
						}
					}
				}
			
			}else{ //Si no es array

				$ext = explode('.', $this->ci_solicitante->name);
				$a = array_search($ext, $ext_permitidas);

				if (count($ext) >= 2) {

					$a = array_search($ext[count($ext)-1], $ext_permitidas);

					if ($a==="") {
						
						$this->addError('ci_solicitante', 'La extensión del archivo '.$this->ci_solicitante->name.' debe ser jpg, png ó pdf ');
						return false;
					}

				}else{

					$a = array_search($ext[1], $ext_permitidas);

					if ($a==="") {
							
						$this->addError('ci_solicitante', 'La extensión del archivo '.$this->ci_solicitante->name.' debe ser jpg, png ó pdf ');
						return false;
					}
				}
			}
		}
	}

	public static function extension($file_name){

		$array_name = explode('.', $file_name);
		$a = '';

		if (!empty($file_name)) {

			if (count($array_name) > 2) {

				$a = $array_name[count($array_name)-1];
				return $a;

			}else{

				$a = $array_name[1];
				return $a;
			}

		}else{

			return $a;
		}
	}

	public static function Check($id_benef_socio){

		$resul = self::model()->find('id_socio_benef=:id and blnborrado=false', array(':id'=>$id_benef_socio));

		if (empty($resul)) {

			return $id_benef_socio;

		}else{

			return empty($resul);
		}
	}

	public function noRepetidos($nombres, $valor){

		if (is_array($nombres) && !empty($nombres)) {
			$coincidencia = 0;
			foreach ($nombres as $key => $value) {
				
				if ($valor == $value) {
					
					$coincidencia++;
				}

				if ($coincidencia == 2) {
			//echo "<pre>norepetir";print_r($nombres);
			//echo "<pre>";print_r($valor);
			//exit;
					
					return 'repetido';
				}
			}
		}
	}

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