<?php

/**
 * This is the model class for table "contable.vsw_detalle_analitico".
 *
 * The followings are the available columns in table 'contable.vsw_detalle_analitico':
 * @property integer $id_cuenta
 * @property string $descripcion
 * @property boolean $valido
 * @property string $cuenta
 * @property string $codigo_cuenta
 * @property string $naturaleza
 * @property integer $titulo_mov
 * @property integer $nivel
 * @property integer $grupo_cuenta
 * @property string $referencia
 * @property integer $id_referencia
 * @property integer $digitos_padre
 * @property integer $digitos
 * @property integer $id_cmp
 * @property integer $clv_comprobante
 * @property string $clv_auxiliar
 * @property string $c_c
 * @property string $monto_debe
 * @property string $monto_haber
 * @property string $fecha_creado
 * @property string $hora_create
 * @property integer $estatus_contab
 */
class DetalleAnalitico extends CActiveRecord
{
    
        public $total_apertura = 0;
        public $total_debe;
        public $saldo_cuenta;
        public $total_haber;
        public $total_saldo = 0; 
        public $total_monto_actual = 0;
        public $total_monto_anterior = 0;
    
    
        /*
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contable.vsw_detalle_analitico';
	}

	/**
	 * @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_cuenta, titulo_mov, nivel, grupo_cuenta, id_referencia, digitos_padre, digitos, id_cmp, clv_comprobante, estatus_contab', 'numerical', 'integerOnly'=>true),
			array('naturaleza', 'length', 'max'=>10),
			array('clv_auxiliar', 'length', 'max'=>12),
			array('fecha_comprobante,descripcion, valido, cuenta, codigo_cuenta, referencia, c_c, monto_debe, monto_haber, fecha_creado, hora_create', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('fecha_comprobante,id_cuenta, descripcion, valido, cuenta, codigo_cuenta, naturaleza, titulo_mov, nivel, grupo_cuenta, referencia, id_referencia, digitos_padre, digitos, id_cmp, clv_comprobante, clv_auxiliar, c_c, monto_debe, monto_haber, fecha_creado, hora_create, estatus_contab', '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(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_cuenta' => 'Id Cuenta',
			'descripcion' => 'Descripcion',
			'valido' => 'Valido',
			'cuenta' => 'Cuenta',
			'codigo_cuenta' => 'Codigo Cuenta',
			'naturaleza' => 'Naturaleza',
			'titulo_mov' => 'Titulo Mov',
			'nivel' => 'Nivel',
			'grupo_cuenta' => 'Grupo Cuenta',
			'referencia' => 'Referencia',
			'id_referencia' => 'Id Referencia',
			'digitos_padre' => 'Digitos Padre',
			'digitos' => 'Digitos',
			'id_cmp' => 'Id Cmp',
			'clv_comprobante' => 'Clv Comprobante',
			'clv_auxiliar' => 'Clv Auxiliar',
			'c_c' => 'C C',
			'monto_debe' => 'Monto Debe',
			'monto_haber' => 'Monto Haber',
			'fecha_creado' => 'Fecha Creado',
			'hora_create' => 'Hora Create',
			'estatus_contab' => 'Estatus Contab',
		);
	}

	/**
	 * 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_cuenta',$this->id_cuenta);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('valido',$this->valido);
		$criteria->compare('cuenta',$this->cuenta,true);
		$criteria->compare('codigo_cuenta',$this->codigo_cuenta,true);
		$criteria->compare('naturaleza',$this->naturaleza,true);
		$criteria->compare('titulo_mov',$this->titulo_mov);
		$criteria->compare('nivel',$this->nivel);
		$criteria->compare('grupo_cuenta',$this->grupo_cuenta);
		$criteria->compare('referencia',$this->referencia,true);
		$criteria->compare('id_referencia',$this->id_referencia);
		$criteria->compare('digitos_padre',$this->digitos_padre);
		$criteria->compare('digitos',$this->digitos);
		$criteria->compare('id_cmp',$this->id_cmp);
		$criteria->compare('clv_comprobante',$this->clv_comprobante);
		$criteria->compare('clv_auxiliar',$this->clv_auxiliar,true);
		$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('fecha_creado',$this->fecha_creado,true);
		$criteria->compare('hora_create',$this->hora_create,true);
		$criteria->compare('estatus_contab',$this->estatus_contab);

		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 DetalleAnalitico the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public static function totalDebe($condicion=''){ //La condicion es para obtener el total de un rango de cuentas
            $model_total = DetalleAnalitico::model()->find(array(
                    'select'=>'sum(monto_debe) as total_debe ',
                    'condition'=>" blnborrado = false and activo=1 and estatus_contab=1".$condicion,
                    //'order'=>'id',
                    //'params'=>array(':idcuenta'=>$cuenta->id,)
               ));
            
            if ($model_total){
                $total = $model_total->total_debe;
            }else{
                $total = 0;
            }
            
            return $total;
        }
        
        public static function totalHaber($condicion=''){
            $model_total = DetalleAnalitico::model()->find(array(
                    'select'=>'sum(monto_haber) as total_haber ',
                    'condition'=>" blnborrado = false and activo=1 and estatus_contab=1".$condicion,
                    //'order'=>'id',
                    //'params'=>array(':idcuenta'=>$cuenta->id,)
               ));
            
            if ($model_total){
                $total = $model_total->total_haber;
            }else{
                $total = 0;
            }
            
            return $total;
        }
        
        public static function monto_inicial_cta($id){
            
            $sql = "SELECT ( SUM(d.monto_debe) - SUM(d.monto_haber)) as total_apertura  FROM contable.det_comprobante d INNER JOIN contable.comprobante cmp ON d.clv_comprobante = cmp.id WHERE d.blnborrado = FALSE AND d.id_cuenta = $id GROUP BY d.id_cuenta ";
            $model_total = DetalleComprobante::model()->findBySql($sql);
            
                if ($model_total):
                    $total = $model_total->total_apertura;
                else:
                    $total = 0;
                endif;
            return $total;
        }
}
