private void getClientes()
 {
     TrxCLIENTE _TrxCLIENTE = new TrxCLIENTE();
     ddlClientes.Items.Clear();
     ddlClientes.Items.Add(new ListItem("(Todos)", ""));
     ddlClientes.DataSource = _TrxCLIENTE.GetAll();
     ddlClientes.DataBind();
 }
        protected void btnIngreso_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    panelMensaje.CssClass = "OcultarMensaje";

                    if (!string.IsNullOrEmpty(txtRut.Text))
                        if (!ValidaRut(txtRut.Text))
                            throw new Exception("Rut no es valido");

                    string rut = !string.IsNullOrEmpty(txtRut.Text) ? txtRut.Text : string.Empty;
                    string fono = !string.IsNullOrEmpty(txtFono.Text) ? txtFono.Text : string.Empty;
                    string nombre = !string.IsNullOrEmpty(txtNombre.Text) ? txtNombre.Text : string.Empty;
                    string direccion = !string.IsNullOrEmpty(txtDireccion.Text) ? txtDireccion.Text : string.Empty;
                    string giro = !string.IsNullOrEmpty(txtGiro.Text) ? txtGiro.Text : string.Empty;
                    int? descuento = null;
                    if (!string.IsNullOrEmpty(txtDescuento.Text))
                    {
                        descuento = int.Parse(txtDescuento.Text);
                    }
                    int comuna = int.Parse(ddlComuna.SelectedValue);
                    int tipoPrestacion = int.Parse(ddlTipoPrestacion.SelectedValue);
                    int convenio = int.Parse(ddlConvenio.SelectedValue);
                    int tipoFactura = int.Parse(ddlTipoFactura.SelectedValue);

                    var cliente = new TrxCLIENTE();
                    var ingreso = cliente.Add(comuna, convenio, tipoPrestacion, tipoFactura, rut, nombre, descuento, direccion, fono, giro);
                    var ingreso_sinonimo = 0;

                    /*guardar datos del cliente sinonimo desde la grilla*/
                    foreach (var item in _listaClienteSinonimo)
                    {
                        var trxClienteSinonimo = new TrxCLIENTE_SINONIMO();
                        ingreso_sinonimo = trxClienteSinonimo.Add(ingreso, item.NOMBRE);
                    }

                    //se despliega mensaje de exito
                    if (ingreso > 0 && ingreso_sinonimo > 0)
                        Response.Redirect("MensajeExito.aspx?t=Ingreso Clientes&m=Se ha registrado un nuevo cliente", false);
                    else if (ingreso == 0)
                        Response.Redirect("MensajeError.aspx?t=Ingreso Clientes&m=Ha ocurrido un error al ingresar el cliente", false);
                    else if (ingreso_sinonimo == 0)
                        Response.Redirect("MensajeError.aspx?t=Ingreso Cliente_Sinonimo&m=Ha ocurrido un error al ingresar el Sinonimo del cliente", false);
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                panelMensaje.CssClass = "MostrarMensaje";
                lblMensaje.Text = ex.Message;
                return;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                panelMensaje.CssClass = "OcultarMensaje";

                if (Request.QueryString["Id"] == null)
                    throw new Exception("No se ha indicado identificador de la factura");

                int IdFactura = int.Parse(Request.QueryString["Id"].ToString());

                TrxFACTURACION trxFacturacion = new TrxFACTURACION();
                TrxCLIENTE trxCliente = new TrxCLIENTE();
                var detalle = trxFacturacion.GetDetalleFacturaById(IdFactura);
                var cliente = trxCliente.GetByFilter(null,null,null,null,detalle.RUT_CLIENTE.ToString(),"",null,"","","");

                foreach (var cli in cliente)
                {
                    hdIdCliente.Value = cli.ID.ToString();
                }
                lblNombreCliente.Text = detalle.NOMBRE_CLIENTE;
                lblRutCliente.Text = detalle.RUT_CLIENTE;
                lblFechaEmision.Text = detalle.FECHA_EMISION.ToString();
                lblNroFactura.Text = detalle.NUMERO_FACTURA.ToString();
                lblEstadoPago.Text = detalle.PAGADA ? "PAGADA" : "PENDIENTE";

                //carga grilla detalle factura
                grdDetalleFactura.DataSource = detalle.LISTA_PRESTACIONES;
                grdDetalleFactura.DataBind();

                //carga grilla Pagos
                grdPagos.DataSource = detalle.LISTA_PAGOS;
                grdPagos.DataBind();

                //carga grilla Notas de Cobro
                grdNotasCobro.DataSource = detalle.LISTA_COBROS;
                grdNotasCobro.DataBind();

            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                panelMensaje.CssClass = "MostrarMensaje";
                lblMensaje.Text = ex.Message;
                return;
            }
        }
        protected void btnActualizar_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    panelMensaje.CssClass = "OcultarMensaje";

                    if (Request.QueryString["Id"] == null)
                        throw new Exception("No se ha indicado identificador de la cuenta registrada");

                    int Id = int.Parse(Request.QueryString["Id"]);
                    var trxCliente = new TrxCLIENTE();

                    string rut = !string.IsNullOrEmpty(txtRut.Text) ? txtRut.Text : string.Empty;
                    string fono = !string.IsNullOrEmpty(txtFono.Text) ? txtFono.Text : string.Empty;
                    string nombre = !string.IsNullOrEmpty(txtNombre.Text) ? txtNombre.Text : string.Empty;
                    string direccion = !string.IsNullOrEmpty(txtDireccion.Text) ? txtDireccion.Text : string.Empty;
                    string giro = !string.IsNullOrEmpty(txtGiro.Text) ? txtGiro.Text : string.Empty;
                    int? descuento = null;
                    if (!string.IsNullOrEmpty(txtDescuento.Text))
                    {
                        descuento = int.Parse(txtDescuento.Text);
                    }
                    int comuna = int.Parse(ddlComuna.SelectedValue);
                    int tipoPrestacion = int.Parse(ddlTipoPrestacion.SelectedValue);
                    int convenio = int.Parse(ddlConvenio.SelectedValue);
                    int tipoFactura = int.Parse(ddlTipoFactura.SelectedValue);
                    //string razonSocial = !string.IsNullOrEmpty(txtRazonSocial.Text) ? txtRazonSocial.Text : string.Empty;
                    //string direccinEntrega = !string.IsNullOrEmpty(txtDireccionEntrega.Text) ? txtDireccionEntrega.Text : string.Empty;
                    //string ciudad = !string.IsNullOrEmpty(txtCiudad.Text) ? txtCiudad.Text : string.Empty;

                    trxCliente.Update(Id, comuna, convenio, tipoPrestacion, tipoFactura, rut, nombre, descuento, direccion, fono, giro);
                    Response.Redirect("MensajeExito.aspx?t=Actualizar Clientes&m=Se ha modificado la informacion del cliente " + nombre, false);
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                panelMensaje.CssClass = "MostrarMensaje";
                lblMensaje.Text = ex.Message;
                return;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                panelMensaje.CssClass = "OcultarMensaje";
                if (!Page.IsPostBack && !Page.IsCallback)
                {
                    getRegion();
                    getComuna();
                    getTipoPrestacion();
                    getConvenio();
                    getTipoFactura();

                    if (Request.QueryString["Id"] == null)
                        throw new Exception("No se ha indicado identificador de la cuenta registrada");

                    int Id = int.Parse(Request.QueryString["Id"]);
                    var trxCliente = new TrxCLIENTE();
                    var objCliente = trxCliente.GetByIdWithFullReferences(Id);

                    txtRut.Text = objCliente.RUT;
                    txtFono.Text = objCliente.FONO;
                    txtNombre.Text = objCliente.NOMBRE;
                    txtDireccion.Text = objCliente.DIRECCION;
                    txtGiro.Text = objCliente.GIRO;
                    if (objCliente.DESCUENTO.HasValue)
                    {
                        txtDescuento.Text = objCliente.DESCUENTO.ToString();
                    }
                    if (objCliente.COMUNA != null)
                    {
                        if (objCliente.COMUNA.REGION != null)
                        {
                            ddlRegion.SelectedValue = objCliente.COMUNA.REGION.ID.ToString();
                            getComuna();
                        }
                        ddlComuna.SelectedValue = objCliente.COMUNA.ID.ToString();
                    }
                    if (objCliente.TIPO_PRESTACION != null)
                    {
                        ddlTipoPrestacion.SelectedValue = objCliente.TIPO_PRESTACION.ID.ToString();
                        getConvenio();
                    }
                    if (objCliente.CONVENIO != null)
                    {
                        ddlConvenio.SelectedValue = objCliente.CONVENIO.ID.ToString();
                    }
                    if (objCliente.TIPO_FACTURA != null)
                    {
                        ddlTipoFactura.SelectedValue = objCliente.TIPO_FACTURA.ID.ToString();
                    }
                    //txtRazonSocial.Text = ;
                    //txtDireccionEntrega.Text = ;
                    //txtCiudad.Text = ;

                    var _trxClienteSinonimo = new TrxCLIENTE_SINONIMO();
                    grdSinonimoCliente.DataSource = _trxClienteSinonimo.GetByFilterWithReferences(Id, "");
                    grdSinonimoCliente.DataBind();

                    txtSinonimo.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                panelMensaje.CssClass = "MostrarMensaje";
                lblMensaje.Text = ex.Message;
                return;
            }
        }
        private void BuscarCliente()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtRutCliente.Text))
                    if (!ValidaRut(txtRutCliente.Text))
                        throw new Exception("Rut no es valido");

                panelMensaje.CssClass = "OcultarMensaje";

                DTOFindCliente dto = new DTOFindCliente();
                dto.PageIndex = grdClientes.PageIndex;
                dto.PageSize = grdClientes.PageSize;

                dto.RUT = txtRutCliente.Text;
                dto.NOMBRE = txtNombreCliente.Text;
                if (!string.IsNullOrEmpty(ddlRegion.SelectedValue))
                    dto.ID_REGION = int.Parse(ddlRegion.SelectedValue);
                if (!string.IsNullOrEmpty(ddlComuna.SelectedValue))
                    dto.ID_COMUNA = int.Parse(ddlComuna.SelectedValue);
                if (!string.IsNullOrEmpty(ddlTipoPrestacion.SelectedValue))
                    dto.ID_TIPO_PRESTACION = int.Parse(ddlTipoPrestacion.SelectedValue);
                if (!string.IsNullOrEmpty(ddlConvenio.SelectedValue))
                    dto.ID_CONVENIO = int.Parse(ddlConvenio.SelectedValue);

                pnClientes.Visible = true;
                var trx = new TrxCLIENTE();
                //grdClientes.DataSource = trx.GetByFilterWithReferences(Comuna, Convenio, null, null, Rut, Nombre, null, "", "", "");
                //grdClientes.DataBind();
                int Total = trx.GetByFilterWithFullReferencesCount(dto.RUT, dto.NOMBRE, dto.ID_REGION, dto.ID_COMUNA, dto.ID_TIPO_PRESTACION, dto.ID_CONVENIO);
                grdClientes.DataSource = trx.GetByFilterWithFullReferences(dto.RUT, dto.NOMBRE, dto.ID_REGION, dto.ID_COMUNA, dto.ID_TIPO_PRESTACION, dto.ID_CONVENIO, dto.PageIndex, dto.PageSize);
                grdClientes.DataBind();

                Paginador1.TotalPages = Total % grdClientes.PageSize == 0 ? Total / grdClientes.PageSize : Total / grdClientes.PageSize + 1;
                Paginador1.Visible = (Total > 0);
                Paginador1.Inicializar(dto);
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                panelMensaje.CssClass = "MostrarMensaje";
                lblMensaje.Text = ex.Message;
                return;
            }
        }
        protected void imgEliminar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                panelMensaje.CssClass = "OcultarMensaje";
                ImageButton _link = sender as ImageButton;
                int? Id = int.Parse(_link.CommandArgument);
                if (Id.HasValue)
                {
                    TrxCLIENTE trxCliente = new TrxCLIENTE();
                    trxCliente.Delete(Id.Value);

                    BuscarCliente();
                }
            }
            catch (Exception ex)
            {
                ISException.RegisterExcepcion(ex);
                panelMensaje.CssClass = "MostrarMensaje";
                lblMensaje.Text = ex.Message;
                return;
            }
        }