/// <summary>
 /// Recibe ClienteProd (id_emp,id_cd,id_cte,id_prd) y regresa el precio
 /// especial(CatClienteProdDet.Id_Precio=2). Regresa -1 en caso de que no exista.
 /// </summary>
 /// <param name="clienteprod"></param>
 /// <param name="Conexion"></param>
 /// <param name="precio"></param>
 public void ConsultaClienteProdPrecioEspecial(ClienteProd clienteprod, string Conexion, ref double precio)
 {//rm
     try
     {
         new CD_CatClienteProd().ConsultaClienteProdPrecioEspecial(clienteprod, Conexion, ref precio);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public void ModificarClienteProd(ClienteProd clienteprod, string Conexion, ref int verificador)
 {
     try
     {
         CD_CatClienteProd claseCapaDatos = new CD_CatClienteProd();
         claseCapaDatos.ModificarClienteProd(clienteprod, Conexion, ref verificador);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public void ClienteProductoPrecioPublico_Consultar(ref ClienteProd clienteprod, string Conexion, ref float precioPublico)
 {
     try
     {
         CD_CatClienteProd claseCapaDatos = new CD_CatClienteProd();
         claseCapaDatos.ClienteProductoPrecioPublico_Consultar(ref clienteprod, Conexion, ref precioPublico);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public void InsertarClienteProdDet(ClienteProd clienteprod, DataTable dt, string Conexion, ref int verificador)
 {
     try
     {
         CD_CatClienteProd claseCapaDatos = new CD_CatClienteProd();
         claseCapaDatos.InsertarClienteProdDet(clienteprod, dt, Conexion);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public void ConsultaClienteProd(ClienteProd clienteprod, string Conexion, ref List <CapaEntidad.ClienteProd> List)
 {
     try
     {
         CD_CatClienteProd claseCapaDatos = new CD_CatClienteProd();
         claseCapaDatos.ConsultaClienteProd(clienteprod, Conexion, ref List);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public void ConsultaClienteProdDet(ClienteProd clientedet, string Conexion, ref System.Data.DataTable dt)
 {
     try
     {
         CD_CatClienteProd claseCapaDatos = new CD_CatClienteProd();
         claseCapaDatos.ConsultarClienteDet(clientedet, Conexion, ref dt);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        private void GetListDet()
        {
            try
            {
                dt = new DataTable();
                DataColumn dc = new DataColumn();
                dt.Columns.Add("Id_ClpDet", System.Type.GetType("System.Int32"));
                dt.Columns.Add("TPrecio", System.Type.GetType("System.Int32"));
                dt.Columns.Add("TPrecioStr", System.Type.GetType("System.String"));
                dt.Columns.Add("Precio", System.Type.GetType("System.String"));
                dt.Columns.Add("Pesos", System.Type.GetType("System.Double"));

                txtClave.Text       = "";
                txtDescripcion.Text = "";
                chkActivo.Checked   = true;

                if (txtClienteID.Text != "" && txtProductoID.Text != "")
                {
                    if (Convert.ToInt32(txtClienteID.Text) > 0 && Convert.ToInt32(txtProductoID.Text) > 0)
                    {
                        CN_CatClienteProd clsCatCliente = new CN_CatClienteProd();
                        Sesion            session2      = new Sesion();
                        session2 = (Sesion)Session["Sesion" + Session.SessionID];
                        ClienteProd ClienteProddet = new ClienteProd();
                        ClienteProddet.Id_Emp = session2.Id_Emp;
                        ClienteProddet.Id_Cd  = session2.Id_Cd_Ver;
                        ClienteProddet.Id_Cte = Convert.ToInt32(txtClienteID.Text);
                        ClienteProddet.Id_Prd = Convert.ToInt32(txtProductoID.Text);
                        DataTable dt2 = dt;
                        clsCatCliente.ConsultaClienteProdDet(ClienteProddet, session2.Emp_Cnx, ref dt2);
                        dt                  = dt2;
                        txtClave.Text       = ClienteProddet.Id_Clp;// == 0 ? (int?)null : ClienteProddet.Id_Clp;
                        txtDescripcion.Text = ClienteProddet.Clp_descripcion == null ? "" : ClienteProddet.Clp_descripcion;
                        chkActivo.Checked   = string.IsNullOrEmpty(ClienteProddet.Id_Clp) ? true : ClienteProddet.Estatus;
                        if (!string.IsNullOrEmpty(txtClave.Text))
                        {
                            HF_ID.Value = txtClave.Text;
                        }

                        txtUnidades.Text           = ClienteProddet.Unidades;
                        txtPresentacion.Text       = ClienteProddet.Clp_Presentacion;
                        txtCantFact.Value          = ClienteProddet.CantFact;
                        dpUltimaVta.DbSelectedDate = ClienteProddet.Clp_FecUltVta;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void PartidasCalcularPrecioLista(ref DataTable tabla)
        {
            foreach (DataRow row in tabla.Rows)
            {
                double precioProductoAceptado = 0;
                int    Id_Emp = Convert.ToInt32(row[0]);
                int    Id_Cd  = Convert.ToInt32(row[1]);
                int    Id_Cte = Convert.ToInt32(row[2]);
                int    Id_Prd = Convert.ToInt32(row[3]);

                //obtener precio especial del producto
                //para el cliente actual de la factura
                //desde la CAPTURA de SOLICITUDES DE PRECIOS ESPECIALES
                VentanaPrecioEspecialPro precioEspecialPro = null;
                new CN_PrecioEspecial().PrecioEspecialProductoCliente_Consulta(ref precioEspecialPro, this.ReportParameters["@Conexion"].Value.ToString()
                                                                               , Id_Emp, Id_Cd, Id_Cte, Id_Prd /* , Convert.ToInt32(cmbMoneda.SelectedValue) */);

                if (precioEspecialPro != null && precioEspecialPro.Ape_PreEsp > 0)
                {
                    /*
                     * NOTA: si el precio está en dólares u otro tipo de moneda,
                     * se hace la conversión al tipo de moneda de la Valuacion de proyectos
                     */
                    if (precioEspecialPro.Id_Mon != 1) // MONEDA = PESO (1) siempre en captura de valuacion proyectos
                    {
                        //Consultar tipo de cambio
                        double tipoCambioFactura        = 1; // MONEDA = PESO (1) siempre en captura de valuacion proyectos
                        double tipoCambioPrecioEspecial = 0;
                        foreach (TipoMoneda tm in this.ListaTipoMoneda)
                        {
                            if (tm.Id_Mon == precioEspecialPro.Id_Mon)
                            {
                                tipoCambioPrecioEspecial = tm.Mon_TipCambio;
                            }
                        }
                        precioProductoAceptado = (precioEspecialPro.Ape_PreEsp * tipoCambioPrecioEspecial) / tipoCambioFactura;
                    }
                    else
                    {
                        precioProductoAceptado = precioEspecialPro.Ape_PreEsp;
                    }
                }
                else
                {
                    //Si no hay un precio especial en SOLICITUD DE PRECIOS ESPECIALES
                    //va por el precio del catalogo CLIENTE-PRODUCTO, si no hay toma el precio AAA normal del producto

                    //obtener precio AAA
                    float precioAAA = 0;
                    new CN_ProductoPrecios().ConsultaListaProductoPrecioAAA(ref precioAAA, Id_Emp, Id_Cd, Id_Prd, this.ReportParameters["@Conexion"].Value.ToString());

                    //obtener precio especial de producto
                    //desde el catálogo CAT_CLIENTEPRODUCTO
                    float       precioPublicoCAT_CLIENTEPRODUCTO = 0;
                    ClienteProd clienteProd = new ClienteProd();
                    clienteProd.Id_Emp = Id_Emp;
                    clienteProd.Id_Cd  = Id_Cd;
                    clienteProd.Id_Cte = Id_Cte;
                    clienteProd.Id_Prd = Id_Prd;
                    new CN_CatClienteProd().ClienteProductoPrecioPublico_Consultar(ref clienteProd, this.ReportParameters["@Conexion"].Value.ToString(), ref precioPublicoCAT_CLIENTEPRODUCTO);

                    precioProductoAceptado = precioPublicoCAT_CLIENTEPRODUCTO > 0 ? precioPublicoCAT_CLIENTEPRODUCTO : precioAAA;
                }

                row[8] = precioProductoAceptado;
            }
        }
        private void Guardar()
        {
            try
            {
                Sesion session = new Sesion();
                session = (Sesion)Session["Sesion" + Session.SessionID];

                ClienteProd clienteprod = new ClienteProd();
                clienteprod.Id_Emp           = session.Id_Emp;
                clienteprod.Id_Cd            = session.Id_Cd_Ver;
                clienteprod.Id_Cte           = !string.IsNullOrEmpty(txtClienteID.Text) ? Convert.ToInt32(txtClienteID.Text) : 0;
                clienteprod.Id_Prd           = !string.IsNullOrEmpty(txtProductoID.Text) ? Convert.ToInt32(txtProductoID.Text) : 0;
                clienteprod.Clp_descripcion  = txtDescripcion.Text;
                clienteprod.Estatus          = chkActivo.Checked;
                clienteprod.Id_Clp           = !string.IsNullOrEmpty(txtClave.Text) ? txtClave.Text : "";
                clienteprod.Clp_Presentacion = txtPresentacion.Text;
                clienteprod.Unidades         = txtUnidades.Text;
                clienteprod.CantFact         = !string.IsNullOrEmpty(txtCantFact.Text) ? Convert.ToInt32(txtCantFact.Text) : 0;

                CN_CatClienteProd clsCatClienteProd = new CN_CatClienteProd();
                int verificador = -1;
                if (clienteprod.Id_Cte == 0)
                {
                    Alerta("Agregue un cliente");
                    return;
                }
                if (clienteprod.Id_Prd == 0)
                {
                    Alerta("Agregue un producto");
                    return;
                }
                if (HF_ID.Value == "")
                {
                    if (!_PermisoGuardar)
                    {
                        Alerta("No tiene permisos para grabar");
                        return;
                    }

                    clsCatClienteProd.InsertarClienteProd(clienteprod, session.Emp_Cnx, ref verificador);

                    if (verificador == 1)
                    {
                        clsCatClienteProd.InsertarClienteProdDet(clienteprod, dt, session.Emp_Cnx, ref verificador);
                        Nuevo();
                        RadTabStrip1.Tabs[0].Selected       = true;
                        RadMultiPage1.PageViews[0].Selected = true;
                        Alerta("Los datos se guardaron correctamente");
                    }
                    else
                    {
                        Alerta("La clave ya existe");
                    }
                }
                else
                {
                    if (!_PermisoModificar)
                    {
                        Alerta("No tiene permisos para modificar");
                        return;
                    }

                    clsCatClienteProd.ModificarClienteProd(clienteprod, session.Emp_Cnx, ref verificador);

                    if (verificador == 1)
                    {
                        clsCatClienteProd.ModificarClienteProdDet(clienteprod, dt, session.Emp_Cnx, ref verificador);
                        Nuevo();
                        RadTabStrip1.Tabs[0].Selected       = true;
                        RadMultiPage1.PageViews[0].Selected = true;
                        Alerta("Los datos se modificaron correctamente");
                    }
                    else
                    {
                        Alerta("Ocurrió un error al intentar modificar los datos");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }