<?php

/**
 * qcat_anl_dia actions.
 *
 * @package    contab2
 * @subpackage qcat_anl_dia
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class qcat_anl_diaActions extends sfQuery {

    protected $table_names = array(
        'CN_MOV_AUX_TER'
    );
    protected $table_join_ids = array(
    );
    protected $fields = array(
        'CN_MOV_AUX_TER' => array(
            'fecha_cad' => 'fecha_cad',
            'id_tipo_comprobante' => 'id_tipo_comprobante',
            'tipo_doc' => 'tipo_doc',
            'doc_soporte' => 'doc_soporte',
            'descr_mto' => 'descr_mto',
            'id_mae_ter' => 'id_mae_ter',
            'nitocc' => 'nitocc',
            'nombre' => 'nombre',
            'debito' => 'debito',
            'credito' => 'credito'
//            ,
//            'saldo' => 'saldo'
        )
    );
    protected $table_field_conditions = array(
        'CN_MOV_AUX_TER' => array(
            'id_cia' => array('=' => array('id_cia')),
            'id_cen_op' => array('=' => array('id_cen_op')),
            //'vig_per' => array('BETWEEN' => array('vig_perIni', 'vig_perFin')),
            'id_auxiliar' => array('=' => array('id_auxiliar')),
            'id_catalogo' => array('=' => array('id_catalogo')),
            'id_cuenta_mayor' => array('=' => array('id_cuenta_mayor')),
            'id_grupo_cuenta' => array('=' => array('id_grupo_cuenta')),
            'id_clase_cuenta' => array('=' => array('id_clase_cuenta')),
            'id_vigencia' => array('=' => array('id_vigencia')),
            'id_periodo_contable' => array('=' => array('id_periodo_contable')),
            'fecha_cad' => array('=' => array('fecha_cad')),
            'rownum,start' => array('>' => array('_startRow')),
            'rownum,end' => array('<=' => array('_endRow'))
        )
    );
    protected $table_field_or_conditions = null;
    protected $query_qualifier = 'DISTINCT';

    /* service sum */
    protected $summatory_data = array(
        'CN_MOV_AUX_TER' =>
        array(
            'fields' => array('debito', 'credito'/*, 'saldo'*/),
            'labels' => array('id_tipo_comprobante,Total'),
        //'condition,cuenta' => array('LENGTH(', 'field', ') = 4')
        )
    );

    /**
     *
     * @param sfWebRequest $request
     */
    public function executeSaldo(sfWebRequest $request) {

        $cia = $request['id_cia'];
        $vigencia = $request['id_vigencia'];
        $periodo = $request['id_periodo_contable'];
        $fecha = $request['fecha_cad'];
        $cen_op = $request['id_cen_op'];
        $auxiliar = $request['id_auxiliar'];
        $catalogo = $request['id_catalogo'];
        $cuenta = $request['id_cuenta_mayor'];
        $grupo = $request['id_grupo_cuenta'];
        $clase = $request['id_clase_cuenta'];

        $sql = 'select
                \'Total\' as fecha_cad,
                bdc.SALDO_ANTERIOR,
                bdc.MVTO_DEBITO as debito,
                bdc.MVTO_CREDITO as credito,
                bdc.NUEVO_SALDO as descr_mto
                from CN_BALANCE_DETALLADO_CATALOGO bdc
                where
                bdc.ID_CIA = ' . $cia . '
                and bdc.ID_CEN_OP = ' . $cen_op . '
                and bdc.ID_VIGENCIA = ' . $vigencia . '
                and bdc.ID_PERIODO_CONTABLE = ' . $periodo . '
                /*and bdc.ID_PERIODO_CONTABLE = ' . $fecha . '*/
                and bdc.ID_CLASE_CUENTA = ' . $clase . '
                and bdc.ID_GRUPO_CUENTA = ' . $grupo . '
                and bdc.ID_CUENTA_MAYOR = ' . $cuenta . '
                and bdc.ID_CATALOGO = ' . $catalogo . '
                and bdc.ID_AUXILIAR = ' . $auxiliar . ' ';

        $records = CN_TABLE::executeSQL($sql);

        $this->list_data = $records;

        $this->logMessage('record_count = ' . count($records));
        $this->logMessage('total_record_count = ' . count($records));
        $this->totalRows = count($records);
        $this->startRow = $request['_startRow'];
        $this->endRow = count($records);
    }

}