private DataTable Lista_tomainventario()
        {
            var TablaProductostock = new DataTable("Productostock");
            try
            {
                var BL = new tb_me_local_stockBL();
                var BE = new tb_me_local_stock();

                BE.moduloid = moduloid.Trim();
                BE.local = local.Trim();
                BE.lineaid = lineaid;
                BE.status = status;

                TablaProductostock = BL.GetAll_productostock(VariablesPublicas.EmpresaID.ToString(), BE).Tables[0];
                if (TablaProductostock != null)
                {
                    return TablaProductostock;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        private DataTable Movimiento_productostock(bool porlocal)
        {
            var TablaProductostock = new DataTable("Productostock");
            try
            {
                var BL = new tb_me_local_stockBL();
                var BE = new tb_me_local_stock();

                BE.moduloid = moduloid.Trim();
                BE.local = local.Trim();
                BE.lineaid = lineaid;
                BE.grupoid = grupoid;
                BE.subgrupoid = subgrupoid;
                BE.productid = productid;
                BE.colorid = colorid;
                BE.status = status;
                BE.productidold = productidold;
                if (porlocal)
                {
                    TablaProductostock = BL.GetAll_productostockxlocal(VariablesPublicas.EmpresaID.ToString(), BE).Tables[0];
                }
                else
                {
                    TablaProductostock = BL.GetAll_productostock(VariablesPublicas.EmpresaID.ToString(), BE).Tables[0];
                }

                if (TablaProductostock != null)
                {
                    return TablaProductostock;
                }
                else
                {
                    return TablaProductostock;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public bool Update(string empresaid, tb_me_local_stock BE)
 {
     return tablaDA.Update(empresaid, BE);
 }
 private void btn_process_Click(object sender, EventArgs e)
 {
     fech_ini.Text = DateTime.Now.ToLongTimeString();
     var BL = new tb_me_local_stockBL();
     var BE = new tb_me_local_stock();
     BE.moduloid = modulo;
     BE.local = local;
     BE.perianio = perianio;
     BE.perimes = perimes;
     BE.grabacp = "S";
     try
     {
         if (BL.ReorgAnioMes(VariablesPublicas.EmpresaID.ToString(), BE))
         {
             fech_fin.Text = DateTime.Now.ToLongTimeString();
             MessageBox.Show("Proceso terminado Correctamente", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             MessageBox.Show("Contactese con Sistema !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
 public bool Insert(string empresaid, tb_me_local_stock BE)
 {
     return tablaDA.Insert(empresaid, BE);
 }
 public bool ReorgAnioMes(string empresaid, tb_me_local_stock BE)
 {
     return tablaDA.ReorgAnioMes(empresaid, BE);
 }
 public bool CierreDePeriodo(string empresaid, tb_me_local_stock BE)
 {
     return tablaDA.CierreDePeriodo(empresaid, BE);
 }
 public DataSet GetAll_productostockxlocal(string empresaid, tb_me_local_stock BE)
 {
     return tablaDA.GetAll_productostockxlocal(empresaid, BE);
 }
        private void btn_busqueda_Click(object sender, EventArgs e)
        {
            DataTable TablaProductostock = new DataTable("Productostock");
            try
            {
                tb_me_local_stockBL BL = new tb_me_local_stockBL();
                tb_me_local_stock BE = new tb_me_local_stock();

                BE.moduloid = modulo.Trim();
                BE.local = local.Trim();
                BE.lineaid = lineaid.Text.Trim();
                BE.grupoid = grupoid.Text.Trim();
                BE.subgrupoid = subgrupoid.Text.Trim();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btn_busqueda_Click(object sender, EventArgs e)
        {
            var TablaProductostock = new DataTable("Productostock");
            try
            {
                var BL = new tb_me_local_stockBL();
                var BE = new tb_me_local_stock();

                BE.moduloid = modulo.Trim();
                BE.local = local.Trim();
                BE.lineaid = lineaid.Text.Trim();
                BE.grupoid = grupoid.Text.Trim();
                BE.subgrupoid = subgrupoid.Text.Trim();
                BE.productid = productid.Text.Trim();
                BE.colorid = colorid.Text.Trim();
                BE.status = chkTodos.Checked ? "1" : "0";

                TablaProductostock = BL.GetAll_productostock(VariablesPublicas.EmpresaID.ToString(), BE).Tables[0];
                if (TablaProductostock != null)
                {
                    Examinar3.DataSource = TablaProductostock;
                    Examinar3.RefreshDataSource();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btn_actualizar_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow fila in griddetallemov.Rows)
            {
                String xproductid = griddetallemov.Rows[fila.Index].Cells["productid"].Value.ToString();
                if (xproductid.Trim().Length == 13)
                {
                    tb_me_local_stockBL BL = new tb_me_local_stockBL();
                    tb_me_local_stock BE = new tb_me_local_stock();
                    DataTable DT = new DataTable();

                    BE.moduloid = modulo;
                    BE.productid = xproductid;

                    DT = BL.GetAll(EmpresaID, BE).Tables[0];

                    if (DT.Rows.Count > 0)
                    {
                        griddetallemov.Rows[fila.Index].Cells["productname"].Value = DT.Rows[0]["productname"].ToString().Trim();
                    }
                    else
                    {
                        MessageBox.Show("Producto no existe !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Producto no existe !!! ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void Valida_DetDoc()
        {
            _cal_Igv();

            Decimal xxprecventa = 0, xxcostoultimo = 0, xxstock = 0, xxcostopromed = 0;
            griddetallemov.AutoGenerateColumns = false;

            tb_me_movimientosdetBL BL = new tb_me_movimientosdetBL();
            tb_me_movimientosdet BE = new tb_me_movimientosdet();

            DataTable dt = new DataTable();

            BE.moduloid = modulo;
            BE.local = local;
            BE.tipodoc = tipfac.Text.ToString();
            BE.serdoc = serfac.Text.Trim();
            BE.numdoc = numfac.Text.ToString();

            dt = BL.GetAll(EmpresaID, BE).Tables[0];
            try
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow fila in dt.Rows)
                    {
                        tb_me_local_stockBL BL2 = new tb_me_local_stockBL();
                        tb_me_local_stock BE2 = new tb_me_local_stock();
                        DataTable dt2 = new DataTable();

                        BE2.moduloid = modulo;
                        BE2.local = local;
                        BE2.productid = fila["productid"].ToString();

                        dt2 = BL2.GetAll(EmpresaID, BE2).Tables[0];

                        if (dt2.Rows.Count > 0)
                        {
                            if (almacaccionid.Trim() == "20" || almacaccionid.Trim() == "21")
                            {
                                lbl_valor.Text = "Cost.Prom";
                                xxprecventa = Convert.ToDecimal(dt2.Rows[0]["precventa"]);
                                xxcostopromed = Convert.ToDecimal(dt2.Rows[0]["costopromed"]);

                            }
                            else if (almacaccionid.Trim() == "10" || almacaccionid.Trim() == "11")
                            {
                                lbl_valor.Text = "Cost.Ultm";
                                xxcostoultimo = Convert.ToDecimal(dt2.Rows[0]["costoultimo"]);
                            }

                            xxstock = Convert.ToDecimal(dt2.Rows[0]["stock"]);
                            if (xxstock == 0)
                            { xxstock = Convert.ToDecimal(dt2.Rows[0]["stockini"]); }
                        }

                        row = Tabladetallemov.NewRow();
                        row["itemref"] = fila["itemref"].ToString();
                        row["items"] = fila["items"].ToString();
                        row["productid"] = fila["productid"].ToString().Trim();
                        row["productname"] = fila["productname"].ToString().Trim();

                        Decimal cantidad = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                        row["stock"] = xxstock;
                        row["precventa"] = xxprecventa;
                        row["costoultimo"] = xxcostoultimo;
                        row["costopromed"] = xxcostopromed;

                        row["cantidad"] = cantidad;
                        row["cantidad_old"] = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                        row["precunit"] = Math.Round(Convert.ToDecimal(Convert.ToDecimal(fila["precunit"]).ToString("###,###,##0.000000")), 6);
                        Decimal precunit = Math.Round(Convert.ToDecimal(Convert.ToDecimal(fila["precunit"]).ToString("###,###,##0.000000")), 6);
                        Decimal importe;
                        importe = cantidad * precunit;

                        row["importfac"] = importe;
                        row["valor"] = Math.Round(Convert.ToDecimal(fila["valor"]), 6);
                        row["importe"] = Math.Round(Convert.ToDecimal(fila["importe"]), 6);
                        row["totimpto"] = Math.Round(Math.Round(Convert.ToDecimal(fila["importe"]), 6) * (Convert.ToDecimal(igv) / 100), 6);
                        row["almacaccionid"] = almacaccionid.Trim();

                        Tabladetallemov.Rows.Add(row);
                        griddetallemov.DataSource = Tabladetallemov;
                    }
                    _RecalculoGrid();
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void ValidaTabladetallemovmov(String valproductid)
        {
            String xproductid = "";
            //xprecio == valor
            Decimal xprecio = 0, xprecunit = 0, xcantidad = 0, xcostoprom = 0, tipcamb = 0,
                desct1 = 0, imporfac = 0, import = 0, totimpx = 0;

            xproductid = valproductid.Trim();

            //DataRow[] rowproductid = Tabladetallemov.Select("productid='" + xproductid + "'");
            //if (rowproductid.Length > 0)
            //{
            //    MessageBox.Show("Producto ya existe !!!!!!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["productname"].Value = "";
            txt_stock.Text = "0";
            txt_valor.Text = "0";
            //griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value = "0";
            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["precunit"].Value = "0";
            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importfac"].Value = "0";
            // griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["nostock"].Value = 0;

            if (xproductid.Trim().Length == 13)
            {
                tb_me_local_stockBL BL = new tb_me_local_stockBL();
                tb_me_local_stock BE = new tb_me_local_stock();
                DataTable DT = new DataTable();
                BE.moduloid = modulo;
                BE.local = local;
                BE.productid = xproductid;

                DT = BL.GetAll(EmpresaID, BE).Tables[0];

                if (DT.Rows.Count > 0)
                {
                    //foreach (DataGridViewRow fila in griddetallemov.Rows)
                    //{
                    //foreach (DataGridViewColumn col in griddetallemov.Columns)
                    //{
                    //    if (griddetallemov.Rows[col.Index].Cells["productid"].Value.ToString().ToUpper() == valproductid.ToUpper())
                    //    {
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["productid"].Value = DT.Rows[0]["productid"].ToString().Trim();
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["productname"].Value = DT.Rows[0]["productname"].ToString().Trim();
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["unmed"].Value = DT.Rows[0]["unmed"].ToString().Trim();
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["rollo"].Value = "";

                    Decimal lsStock = 0, dtCantidad = 0, mvCantidad = 0, dtstock = 0;

                    lsStock = Convert.ToDecimal(DT.Rows[0]["stock"].ToString().Trim().PadLeft(1, '0'));
                    dtCantidad = Convert.ToDecimal(griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value);
                    mvCantidad = Convert.ToDecimal(griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad_old"].Value);

                    if (almacaccionid.Substring(0, 1) == "1")
                    {
                        dtstock = lsStock + dtCantidad - mvCantidad;
                    }
                    else if (almacaccionid.Substring(0, 1) == "2")
                    {
                        dtstock = lsStock - dtCantidad + mvCantidad;
                    }

                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["stock_old"].Value = lsStock;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["stock"].Value = dtstock;

                    //griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["stock"].Value = Convert.ToDecimal(DT.Rows[0]["stock"].ToString().Trim().PadLeft(1, '0'));

                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["dalmacaccionid"].Value = almacaccionid.ToString().Trim();
                    //txt_stock.Text = Math.Round(Convert.ToDecimal(DT.Rows[0]["stock"].ToString().Trim().PadLeft(1, '0')), 2).ToString();
                    txt_stock.Text = Convert.ToString(dtstock);

                    if (almacaccionid.Substring(0, 1) == "2")
                    {
                        xprecventa = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["precventa"]).ToString("###,###,##0.000000"));
                        xprecio = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costoultimo"]).ToString("###,###,##0.000000"));
                        txt_valor.Text = Convert.ToDecimal(DT.Rows[0]["costopromed"].ToString().Trim().PadLeft(1, '0')).ToString("###,###,##0.0000");
                        xcostoprom = Convert.ToDecimal(DT.Rows[0]["costopromed"].ToString().Trim().PadLeft(1, '0'));
                    }
                    else if (almacaccionid.Substring(0, 1) == "1")
                    {
                        xcostoultimo = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costoultimo"]).ToString("###,###,##0.000000"));
                        xprecio = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costoultimo"]).ToString("###,###,##0.000000"));
                        txt_valor.Text = Convert.ToDecimal(DT.Rows[0]["costoultimo"].ToString().Trim().PadLeft(1, '0')).ToString("###,###,##0.0000");
                    }

                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["precventa"].Value = xprecventa;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["costoultimo"].Value = xcostoultimo;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["costopromed"].Value = xcostoprom;

                    // valida si el precio de producto es ingresado o biene de local_stock
                    //xcantidad = Convert.ToDecimal(DT.Rows[0]["stock"].ToString().Trim().PadLeft(1, '0'));
                    xcantidad = Convert.ToDecimal(griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value);

                    //*** CALCULA EN IMPORTE FACTURADO
                    imporfac = Math.Round(xcantidad * Convert.ToDecimal(xprecio), 6);

                    //*** EVALUAR SI TIPO DE CAMBIO ES MENOR A 0
                    tipcamb = Convert.ToDecimal(tcamb.Text.Trim());
                    if (tipcamb <= 0)
                    {
                        tipcamb = 1;
                    }

                    //muestra el precion segun moneda para producto
                    if (moneda.SelectedValue.ToString() == "S")
                    {
                        xprecunit = xprecio;

                    }
                    else
                    {
                        xprecunit = xprecio / tipcamb;
                    }

                    _cal_Igv();

                    //*** EVALUAR SI ES INCLUIDO O NO IGV
                    desct1 = 0;
                    import = imporfac * (1 - (desct1 / 100));
                    if (incprec.Trim() == "S")
                    {
                        totimpx = Math.Round(import * (Convert.ToDecimal(igv) / (100 + Convert.ToDecimal(igv))), 6);
                    }
                    else
                    {
                        totimpx = Math.Round(import * (Convert.ToDecimal(igv) / 100), 6);
                    }

                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["valor"].Value = xprecunit;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importe"].Value = Math.Round(xcantidad * xprecunit, 6);
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["dtotimpto"].Value = totimpx;

                    //if (Convert.ToBoolean(griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["nostock"].Value))
                    //{
                    //    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value = "1";
                    //    griddetallemov.Columns["cantidad"].ReadOnly = true;
                    //}
                    //else
                    //{
                    //    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value = xcantidad;
                    //    griddetallemov.Columns["cantidad"].ReadOnly = false;
                    //}

                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value = xcantidad;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["precunit"].Value = xprecunit;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importfac"].Value = Math.Round(imporfac, 2);
                    Tabladetallemov.AcceptChanges();
                    griddetallemov.CurrentCell = griddetallemov.Rows[griddetallemov.RowCount - 1].Cells["cantidad"];
                    //    }else
                    //    break;
                    //}
                    //    break;
                    //}
                }
                else
                {
                    MessageBox.Show("Producto no existe en tabla LOCAL_STOCK !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Producto no existe !!! ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Tabla_detOC()
        {
            _cal_Igv();

            Decimal xxprecventa = 0, xxcostoultimo = 0, xxstock = 0, xxcostopromed = 0;
            griddetallemov.AutoGenerateColumns = false;

            tb_cm_ordendecompradetBL BL = new tb_cm_ordendecompradetBL();
            tb_cm_ordendecompradet BE = new tb_cm_ordendecompradet();
            DataTable dt = new DataTable();

            BE.moduloid = "0100";
            BE.local = "001";
            BE.tipodoc = tipref.SelectedValue.ToString();
            BE.serdoc = serref.Text.Trim();   // Vendria Ser el Modulo de Cada Almacen
            BE.numdoc = numdococ1.Text.Trim() + numdococ.Text.Trim();  // Correlativo de Cada Orden de Compra

            dt = BL.GetAll(EmpresaID, BE).Tables[0];

            if (dt.Rows.Count > 0)
            {

                if (local == dt.Rows[0]["localdes"].ToString().Trim())
                {

                    Decimal cantidadcta_cc = (Decimal)(dt.Rows[0]["cantidadcta_c"]);
                    Decimal cantidad_cc = (Decimal)(dt.Rows[0]["cantidad_c"]);

                    foreach (DataRow fila in dt.Rows)
                    {
                        //if (cantidadcta_cc < cantidad_cc)
                        //{
                        tb_me_local_stockBL BL2 = new tb_me_local_stockBL();
                        tb_me_local_stock BE2 = new tb_me_local_stock();
                        DataTable dt2 = new DataTable();

                        BE2.moduloid = modulo;
                        BE2.productid = fila["productid"].ToString();

                        dt2 = BL2.GetAll(EmpresaID, BE2).Tables[0];

                        if (dt2.Rows.Count > 0)
                        {
                            if (almacaccionid.Trim() == "20" || almacaccionid.Trim() == "21")
                            {
                                lbl_valor.Text = "Cost.Prom";
                                xxprecventa = Convert.ToDecimal(dt2.Rows[0]["precventa"]);
                                xxcostopromed = Convert.ToDecimal(dt2.Rows[0]["costopromed"]);

                            }
                            else if (almacaccionid.Trim() == "10" || almacaccionid.Trim() == "11")
                            {
                                lbl_valor.Text = "Cost.Ultm";
                                xxcostoultimo = Convert.ToDecimal(dt2.Rows[0]["costoultimo"]);
                            }

                            xxstock = Convert.ToDecimal(dt2.Rows[0]["stock"]);
                        }

                        tipoperacionid.Text = "COMPRA";
                        row = Tabladetallemov.NewRow();
                        row["itemref"] = fila["itemref"].ToString();
                        row["items"] = fila["items"].ToString();
                        row["productid"] = fila["productid"].ToString().Trim();
                        row["productname"] = fila["productname"].ToString().Trim();
                        //row["rollo"] = fila["rollo"].ToString();

                        Decimal cantidad_c = Math.Round(Convert.ToDecimal(fila["cantidad_c"]), 4);
                        Decimal cantidadcta_c = Math.Round(Convert.ToDecimal(fila["cantidadcta_c"]), 4);

                        if (tipref.SelectedIndex != -1)
                        {
                            Decimal stock_old = Math.Round(Convert.ToDecimal(fila["cantidad_c"]), 4);
                            Decimal total = cantidad_c - cantidadcta_c;
                            row["stock_old"] = total;
                            row["stock"] = stock_old - total;
                        }
                        else
                        {
                            row["stock"] = xxstock;
                            row["stock_old"] = xxstock;
                        }

                        row["precventa"] = xxprecventa;
                        row["costoultimo"] = xxcostoultimo;
                        row["costopromed"] = xxcostopromed;

                        Decimal saldo;
                        saldo = cantidad_c - cantidadcta_c;
                        row["cantidad"] = saldo;
                        row["cantidad_old"] = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                        row["precunit"] = Math.Round(Convert.ToDecimal(Convert.ToDecimal(fila["precunit_c"]).ToString("###,###,##0.000000")), 6);
                        Decimal precunit = Math.Round(Convert.ToDecimal(Convert.ToDecimal(fila["precunit_c"]).ToString("###,###,##0.000000")), 6);
                        Decimal importe;
                        importe = cantidad_c * precunit;

                        row["importfac"] = importe;
                        row["valor"] = Math.Round(Convert.ToDecimal(fila["valor"]), 6);
                        row["importe"] = Math.Round(Convert.ToDecimal(fila["importe"]), 6);
                        row["totimpto"] = Math.Round(Math.Round(Convert.ToDecimal(fila["importe"]), 6) * (Convert.ToDecimal(igv) / 100), 6);
                        row["almacaccionid"] = almacaccionid.Trim();
                        //row["ubicacion"] = fila["ubicacion"].ToString().Trim();
                        Tabladetallemov.Rows.Add(row);
                        // }
                        griddetallemov.DataSource = Tabladetallemov;
                    }

                    _RecalculoGrid();
                }
                else
                {
                    tipref.Enabled = true;
                    tipref.SelectedIndex = -1;
                    tipref.Enabled = true;
                    numdococ1.Text = "";
                    numdococ.Text = "";
                    return;
                }
            }
            else
            {
                MessageBox.Show("    !!!... Informacion de la Orden ...!!! \n" +
                                "\n » La Orden Todabia no ha Sido Generada " +
                                "\n » O la Orden esta Generada en Otra Guia " +
                                "\n\n »» Verifica Tus Ordenes Pendientes  ", "Localizando Orden", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                tipref.Enabled = true;
                tipref.SelectedIndex = -1;
                tipref.Enabled = true;
                numdococ1.Text = "";
                numdococ.Text = "";
                return;
            }
        }
        private void data_Tabladetallemovmov()
        {
            _cal_Igv();
            try
            {
                Decimal xxprecventa = 0, xxcostoultimo = 0, xxstock = 0, xxcostopromed = 0;
                griddetallemov.AutoGenerateColumns = false;

                tb_me_movimientosdetBL BL = new tb_me_movimientosdetBL();
                tb_me_movimientosdet BE = new tb_me_movimientosdet();
                DataTable dt = new DataTable();

                BE.moduloid = modulo;
                BE.local = local;
                BE.tipodoc = tipodoc.SelectedValue.ToString().Trim();
                BE.serdoc = serdoc.Text.Trim().PadLeft(4, '0');
                BE.numdoc = numdoc.Text.Trim().PadLeft(6, '0');

                dt = BL.GetAll_datosdetalle(EmpresaID, BE).Tables[0];

                //datos incluido IGV
                if (dt.Rows.Count > 0)
                {
                    statcostopromed = dt.Rows[0]["statcostopromed"].ToString();
                    tiptransac = dt.Rows[0]["tiptransac"].ToString();
                    incprec = dt.Rows[0]["incprec"].ToString().Trim();
                    //tipoperef = dt.Rows[0]["tipoperef"].ToString();
                }

                if (Tabladetallemov != null)
                {
                    Tabladetallemov.Clear();
                }

                foreach (DataRow fila in dt.Rows)
                {
                    tb_me_local_stockBL BL2 = new tb_me_local_stockBL();
                    tb_me_local_stock BE2 = new tb_me_local_stock();
                    DataTable dt2 = new DataTable();
                    BE2.moduloid = modulo;
                    BE2.local = local;
                    BE2.productid = fila["productid"].ToString();
                    dt2.Clear();
                    dt2 = BL2.GetAll(EmpresaID, BE2).Tables[0];

                    if (dt2.Rows.Count > 0)
                    {
                        if (almacaccionid.Trim() == "20" || almacaccionid.Trim() == "21")
                        {
                            lbl_valor.Text = "Cost.Prom";
                            xxprecventa = 0;// Convert.ToDecimal(dt2.Rows[0]["precventa"]);
                            xxcostopromed = Convert.ToDecimal(dt2.Rows[0]["costopromed"]);

                        }
                        else if (almacaccionid.Trim() == "10" || almacaccionid.Trim() == "11")
                        {
                            lbl_valor.Text = "Cost.Ultm";
                            xxcostoultimo = Convert.ToDecimal(dt2.Rows[0]["costoultimo"]);
                        }

                        xxstock = Convert.ToDecimal(dt2.Rows[0]["stock"]);
                    }

                    row = Tabladetallemov.NewRow();
                    row["itemref"] = fila["itemref"].ToString();
                    row["items"] = fila["items"].ToString();
                    row["productid"] = fila["productid"].ToString().Trim();
                    row["productname"] = fila["productname"].ToString().Trim();
                    row["rollo"] = fila["rollo"].ToString();
                    row["stock"] = xxstock;
                    row["stock_old"] = xxstock;
                    row["precventa"] = xxprecventa;
                    row["costoultimo"] = xxcostoultimo;
                    row["costopromed"] = xxcostopromed;
                    //row["cantidad"] = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                    //row["cantidad_c"] = Math.Round(Convert.ToDecimal(fila["cantidad_c"]), 4);

                    Decimal cantidad = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                    Decimal cantidadcta = Math.Round(Convert.ToDecimal(fila["cantidadcta"]), 4);
                    Decimal total;
                    total = cantidad - cantidadcta;

                    row["cantidad"] = total;

                    //row["cantidad_old"] = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                    row["cantidad_old"] = Convert.ToDecimal(fila["cantidad"]);

                    row["precunit"] = Math.Round(Convert.ToDecimal(Convert.ToDecimal(fila["precunit"]).ToString("###,###,##0.000000")), 6);

                    row["importfac"] = Math.Round(Convert.ToDecimal(fila["importfac"]), 2);

                    row["valor"] = Math.Round(Convert.ToDecimal(fila["valor"]), 6);
                    row["importe"] = Math.Round(Convert.ToDecimal(fila["importe"]), 6);
                    row["totimpto"] = Math.Round(Math.Round(Convert.ToDecimal(fila["importe"]), 6) * (Convert.ToDecimal(igv) / 100), 6);

                    row["almacaccionid"] = almacaccionid.Trim();
                    row["unmed"] = fila["unmed"].ToString().Trim();
                    row["nserie"] = fila["nserie"].ToString().Trim();
                    row["ubicacion"] = fila["ubicacion"].ToString().Trim();
                    row["nostock"] = xnostock;
                    Tabladetallemov.Rows.Add(row);

                }

                griddetallemov.DataSource = Tabladetallemov;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }