<?php

/**
 * This is the model class for table "contable.det_comprobante".
 *
 * The followings are the available columns in table 'contable.det_comprobante':
 * @property integer $id
 * @property string $descripcion
 * @property string $fecha_referencia
 * @property string $referencia
 * @property integer $clv_auxiliar
 * @property string $c_c
 * @property string $monto_debe
 * @property string $monto_haber
 * @property boolean $blnborrado
 * @property string $fecha_creado
 * @property string $hora_create
 */
class DetalleComprobante extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */


    public $total_apertura = 0;
    public $total_debe;
    public $total_haber;
    public $total_saldo = 0;
    public $cuenta_descripcion;

    public function tableName()
	{
		return 'contable.det_comprobante';
	}

	/**
	 * @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('clv_auxiliar,id_cuenta,estatus_contab', 'numerical', 'integerOnly'=>true),
//			array('descripcion, fecha_referencia, referencia,cuenta_descripcion', 'required'),
			array('descripcion, fecha_referencia, referencia, id_cuenta, clv_comprobante', 'required'),
			array('cuenta_descripcion', 'required','on'=>'create'),
                        array('cuenta_descripcion','verificarcodigocuenta','on'=>array('create','update')),
                        array('monto_debe, monto_haber', 'numerical'),
                        //array('monto_debe', 'verificardebe'),
                        //array('monto_haber', 'verificardebe'),
                        //array('id_cuenta', 'required'),
			array('descripcion,id_cuenta, fecha_referencia, referencia,clv_comprobante, c_c, monto_debe, monto_haber,cuenta_descripcion,fecha_anulacion,observacion_anulacion,usuario_anulacion', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, descripcion, fecha_referencia, referencia, clv_auxiliar, c_c, monto_debe, monto_haber, blnborrado, fecha_creado, hora_create,fecha_anulacion,observacion_anulacion,usuario_anulacion', 'safe', 'on'=>'search'),
		);
	}

public function verificardebe(){
if(!$this->hasErrors('monto_debe'))
    if(($this->monto_debe<=0) && ($this->monto_haber<=0)){
        $this->addError('monto_debe','El monto debe ser mayor a cero (0).');
      $this->addError('monto_haber','El monto haber ser mayor a cero (0).');
                                                        }
    }

public function verificarcodigocuenta(){
if(!$this->hasErrors('cuenta_descripcion')){

    $codigo = explode("-",$this->cuenta_descripcion);

    $cuentas=  CuentasConsolidada::model()->find('codigo_cuenta =\''.$codigo[0].'\'');
//    echo "<pre>"; print_r($cuentas); exit;
    if(!$cuentas){
      $this->addError('cuenta_descripcion','Esta cuenta no esta en base de datos.');
    }
}
}



	/**
	 * @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(
                    'cuenta' => array(self::BELONGS_TO, 'Unidad', 'clv_auxiliar'),
                    'cuentamov' => array(self::BELONGS_TO, 'CuentasConsolidada', 'id_cuenta'),
                    'comprob' => array(self::BELONGS_TO, 'Comprobante', 'clv_comprobante'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'N° asiento',
			'descripcion' => 'Descripción',
			'fecha_referencia' => 'Fecha referencia',
			'referencia' => 'Referencia',
			'clv_auxiliar' => 'Auxiliar',
			'cuenta.cedula' => 'Auxiliar',
			'cuenta.nombres' => 'Nombre aux',
			'c_c' => 'C.C',
			'monto_debe' => 'Debe',
			'monto_haber' => 'Haber',
			'blnborrado' => 'Blnborrado',
			'fecha_creado' => 'Fecha creado',
			'hora_create' => 'Hora creado',
			'cuenta_descripcion' => 'Código cuenta',
		);
	}

	/**
	 * 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('descripcion',$this->descripcion,true);
		$criteria->compare('fecha_referencia',$this->fecha_referencia,true);
		$criteria->compare('referencia',$this->referencia,true);
		$criteria->compare('clv_auxiliar',$this->clv_auxiliar);
		$criteria->compare('c_c',$this->c_c,true);
		$criteria->compare('monto_debe',$this->monto_debe,true);
		$criteria->compare('monto_haber',$this->monto_haber,true);
		$criteria->compare('blnborrado',$this->blnborrado);
		$criteria->compare('fecha_creado',$this->fecha_creado,true);
		$criteria->compare('hora_create',$this->hora_create,true);

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


}
