private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                string tag = ((TextBox)sender).Tag.ToString();

                if (tag == "inmae_ref")
                {
                    if (e.Key == System.Windows.Input.Key.Enter & string.IsNullOrEmpty(TextBoxRef.Text.Trim()))
                    {
                        dynamic ww = SiaWin.WindowExt(9326, "InBuscarReferencia");
                        ww.Height                = 400;
                        ww.Conexion              = SiaWin.Func.DatosEmp(idemp);
                        ww.idEmp                 = idemp;
                        ww.idBod                 = idBod;
                        ww.ShowInTaskbar         = false;
                        ww.Owner                 = Application.Current.MainWindow;
                        ww.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                        ww.ShowDialog();

                        if (!string.IsNullOrEmpty(ww.Codigo))
                        {
                            TextBoxRef.Text = ww.Codigo;
                            TXNomRef.Text   = ww.Nombre;
                            TextBoxbod.Focus();
                        }
                        ww        = null;
                        e.Handled = true;
                    }
                }
                if (tag == "inmae_bod")
                {
                    if (e.Key == System.Windows.Input.Key.Enter & string.IsNullOrEmpty(TextBoxbod.Text.Trim()))
                    {
                        string cmptabla = tag; string cmpcodigo = "cod_bod"; string cmpnombre = "nom_bod"; string cmporden = "cod_bod"; string cmpidrow = "idrow"; string cmptitulo = "Maestra de bodegas"; string cmpconexion = cnEmp; Boolean mostrartodo = true; string cmpwhere = "";
                        int    idr = 0; string code = ""; string nom = "";

                        dynamic winb = SiaWin.WindowBuscar(cmptabla, cmpcodigo, cmpnombre, cmporden, cmpidrow, cmptitulo, SiaWin.Func.DatosEmp(idemp), mostrartodo, cmpwhere, idEmp: idemp);
                        winb.ShowInTaskbar = false;
                        winb.Owner         = Application.Current.MainWindow;
                        winb.ShowDialog();
                        idr  = winb.IdRowReturn;
                        code = winb.Codigo;
                        nom  = winb.Nombre;
                        winb = null;

                        if (idr > 0)
                        {
                            TextBoxbod.Text = code;
                            TxNomBod.Text   = nom;

                            //var uiElement = e.OriginalSource as UIElement;
                            //uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                            BtnConsultar.Focus();
                        }
                        e.Handled = true;
                    }
                }
                if (e.Key == Key.Enter & !string.IsNullOrEmpty(((TextBox)sender).Text.Trim()))
                {
                    var uiElement = e.OriginalSource as UIElement;
                    uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ResetValue();
                if (FecIni.Text.Length <= 0)
                {
                    MessageBox.Show("debe de ingresar la fecha de corte");
                    FecIni.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(TextBoxRef.Text.Trim()))
                {
                    MessageBox.Show("debe de ingresar una referencia");
                    TextBoxRef.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(TextBoxbod.Text.Trim()))
                {
                    MessageBox.Show("debe de ingresar una bodega");
                    TextBoxbod.Focus();
                    return;
                }


                SqlConnection  con = new SqlConnection(SiaWin._cn);
                SqlCommand     cmd = new SqlCommand();
                SqlDataAdapter da  = new SqlDataAdapter();
                DataSet        ds  = new DataSet();
                cmd             = new SqlCommand("_EmpInventarioKardes", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Fecha", FecIni.Text);
                cmd.Parameters.AddWithValue("@Ref", TextBoxRef.Text);
                cmd.Parameters.AddWithValue("@Bods", TextBoxbod.Text);
                cmd.Parameters.AddWithValue("@codemp", codemp);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
                GridKardex.ItemsSource = ds.Tables[0];
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GridKardex.Focus();
                    GridKardex.SelectedIndex = 0;

                    decimal CantEnt = Convert.ToDecimal(ds.Tables[0].Compute("Sum(ent_uni)", "").ToString());
                    decimal TotEnt  = Convert.ToDecimal(ds.Tables[0].Compute("Sum(ent_ctotal)", "").ToString());
                    TxtTotalUnEnt.Text     = CantEnt.ToString("N2");
                    TxtTotalUncostEnt.Text = TotEnt.ToString("N2");

                    int promedioEntrada = 0;
                    if (TotEnt > 0 & CantEnt > 0)
                    {
                        TxtTotalUncosEnt.Text = (TotEnt / CantEnt).ToString("N2");
                        promedioEntrada       = Convert.ToInt32(TotEnt / CantEnt);
                    }
                    else
                    {
                        TxtTotalUncosEnt.Text = "0";
                    }

                    ProEnt.Text = promedioEntrada.ToString();
                    decimal CantSal = Convert.ToDecimal(ds.Tables[0].Compute("Sum(sal_uni)", "").ToString());
                    decimal TotSal  = Convert.ToDecimal(ds.Tables[0].Compute("Sum(sal_ctotal)", "").ToString());
                    TxtTotalUnSal.Text     = CantSal.ToString("N2");
                    TxtTotalUncostSal.Text = TotSal.ToString("N2");

                    int promedioSalida = 0;
                    if (TotSal > 0 & CantSal > 0)
                    {
                        TxtTotalUncosSal.Text = (TotSal / CantSal).ToString("N2");
                        promedioSalida        = Convert.ToInt32(TotSal / CantSal);
                    }
                    else
                    {
                        TxtTotalUncosSal.Text = "0";
                    }

                    ProSal.Text = promedioSalida.ToString();

                    decimal CantSaldo = Convert.ToDecimal(ds.Tables[0].Compute("Sum(saldo_uni)", "").ToString());
                    decimal TotSaldo  = Convert.ToDecimal(ds.Tables[0].Compute("Sum(saldo_ctotal)", "").ToString());

                    TxtTotalUnSaldo.Text     = CantSaldo.ToString("N2");
                    TxtTotalUncostSaldo.Text = TotSaldo.ToString("N2");

                    int promedioSaldo = 0;

                    if (TotSaldo > 0 & CantSaldo > 0)
                    {
                        promedioSaldo           = Convert.ToInt32(TotSaldo / CantSaldo);
                        TxtTotalUncosSaldo.Text = (TotSaldo / CantSaldo).ToString("N2");
                    }
                    else
                    {
                        TxtTotalUncosSaldo.Text = "0";
                    }


                    ProSaldo.Text = promedioSaldo.ToString();
                }


                Total.Text = ds.Tables[0].Rows.Count.ToString();
            }
            catch (Exception w)
            {
                MessageBox.Show("error al cargar la consulta programada:" + w.ToString());
            }
        }