private void ValidaTablaDetalle(String valrollo, Decimal xPrecio)
        {
            var xrollo = string.Empty;
            var xproductid = string.Empty;
            Decimal xpreciorollo = 0, xprecunit = 0, xcantidad = 0, xcostoprom = 0, tipcamb = 0;
            Decimal desct1 = 0;
            Decimal imporfac = 0;
            Decimal import = 0;
            Decimal totimpx = 0;

            xrollo = valrollo.Trim();

            var rowrollo = Tabladetallemov.Select("rollo='" + xrollo + "'");
            if (rowrollo.Length > 0)
            {
                MessageBox.Show("Rollo ya existe !!!!!!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["productname"].Value = string.Empty;
            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";

            var BLR = new tb_ta_prodrolloBL();
            var DTR = new DataTable();
            DTR = BLR.GetOne(EmpresaID, xrollo).Tables[0];
            if (DTR.Rows.Count > 0)
            {
                xproductid = DTR.Rows[0]["productid"].ToString();
                if (xproductid.Trim().Length == 13)
                {
                    var BL = new tb_60local_stockBL();
                    var BE = new tb_60local_stock();
                    var DT = new DataTable();
                    BE.moduloid = modulo;
                    BE.productid = xproductid;
                    DT = BL.GetAll(EmpresaID, BE).Tables[0];
                    if (DT.Rows.Count > 0)
                    {
                        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["rollo"].Value = DTR.Rows[0]["rollo"].ToString().Trim();
                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["stock"].Value = Convert.ToDecimal(DTR.Rows[0]["rollostock"].ToString().Trim().PadLeft(1, '0'));
                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["dalmacaccionid"].Value = almacaccionid.ToString().Trim();
                        txt_stock.Text = Math.Round(Convert.ToDecimal(DTR.Rows[0]["rollostock"].ToString().Trim().PadLeft(1, '0')), 2).ToString();

                        if (almacaccionid.Substring(0, 1) == "2")
                        {
                            xprecventa = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["precventa"]).ToString("###,###,##0.000000"));
                            xpreciorollo = 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"));
                                xpreciorollo = 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;

                        if (tipoperacionid.SelectedValue.ToString() == "02")
                        {
                            xcantidad = Convert.ToDecimal(DTR.Rows[0]["rollomedcompra"].ToString().Trim().PadLeft(1, '0'));
                        }
                        else
                        {
                            xcantidad = Convert.ToDecimal(DTR.Rows[0]["rollostock"].ToString().Trim().PadLeft(1, '0'));
                        }

                        tipcamb = Convert.ToDecimal(tcamb.Text.Trim());
                        if (tipcamb <= 0)
                        {
                            tipcamb = 1;
                        }

                        if (moneda.SelectedValue.ToString() == "S")
                        {
                            xprecunit = xpreciorollo;
                        }
                        else
                        {
                            xprecunit = xpreciorollo / tipcamb;
                        }

                        if (tipoperacionid.SelectedValue.ToString() == "02" && Convert.ToDecimal(DTR.Rows[0]["rollovaloractual"].ToString().Trim().PadLeft(1, '0')) > 0)
                        {
                            xprecunit = Convert.ToDecimal(DTR.Rows[0]["rollovaloractual"].ToString().Trim().PadLeft(1, '0'));
                        }

                        imporfac = Math.Round(xcantidad * Convert.ToDecimal(xPrecio), 6);

                        _cal_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 = xpreciorollo;
                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importe"].Value = Math.Round(xcantidad * xPrecio, 6);
                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["dtotimpto"].Value = totimpx;

                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value = xcantidad;
                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["precunit"].Value = xPrecio;
                        griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importfac"].Value = Math.Round(imporfac, 2);
                        Tabladetallemov.AcceptChanges();
                        griddetallemov.CurrentCell = griddetallemov.Rows[griddetallemov.RowCount - 1].Cells["cantidad"];
                    }
                    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);
                }
            }
            else
            {
                MessageBox.Show("Rollo no Existe !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void ValidaTabladetallemovmov(String valrollo)
        {
            var xrollo = string.Empty;
            var xproductid = string.Empty;
            Decimal xpreciorollo = 0, xprecunit = 0, xcantidad = 0, xcostoprom = 0, tipcamb = 0;
            Decimal desct1 = 0;
            Decimal imporfac = 0;
            Decimal import = 0;
            Decimal totimpx = 0;

            xrollo = valrollo.Trim();

            var rowrollo = Tabladetallemov.Select("rollo='" + xrollo + "'");
            if (rowrollo.Length > 0)
            {
                MessageBox.Show("Rollo ya existe !!!!!!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            txt_stock.Text = "0";
            txt_valor.Text = "0";

            var BLR = new tb_ta_prodrolloBL();
            var DTR = new DataTable();
            DTR = BLR.GetOne(EmpresaID, xrollo).Tables[0];
            if (DTR.Rows.Count > 0)
            {
                xproductid = DTR.Rows[0]["productid"].ToString();
                if (xproductid.Trim().Length == 13)
                {
                    var BL = new tb_60local_stockBL();
                    var BE = new tb_60local_stock();
                    var DT = new DataTable();
                    BE.moduloid = modulo;
                    BE.productid = xproductid;
                    DT = BL.GetAll(EmpresaID, BE).Tables[0];
                    var nrow = griddetallemov.CurrentCell.RowIndex;
                    if (DT.Rows.Count > 0)
                    {

                        griddetallemov.Rows[nrow].Cells["productid"].Value = DT.Rows[0]["productid"].ToString().Trim();
                        griddetallemov.Rows[nrow].Cells["productname"].Value = DT.Rows[0]["productname"].ToString().Trim();
                        griddetallemov.Rows[nrow].Cells["rollo"].Value = DTR.Rows[0]["rollo"].ToString().Trim();
                        griddetallemov.Rows[nrow].Cells["stock"].Value = Convert.ToDecimal(DTR.Rows[0]["rollostock"].ToString().Trim().PadLeft(1, '0'));
                        griddetallemov.Rows[nrow].Cells["dalmacaccionid"].Value = almacaccionid.ToString().Trim();
                        txt_stock.Text = Math.Round(Convert.ToDecimal(DTR.Rows[0]["rollostock"].ToString().Trim().PadLeft(1, '0')), 2).ToString();

                        if (almacaccionid.Substring(0, 1) == "2")
                        {
                            xprecventa = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["precventa"]).ToString("###,###,##0.000000"));
                            xpreciorollo = 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"));
                                xpreciorollo = 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[nrow].Cells["precventa"].Value = xprecventa;
                        griddetallemov.Rows[nrow].Cells["costoultimo"].Value = xcostoultimo;
                        griddetallemov.Rows[nrow].Cells["costopromed"].Value = xcostoprom;

                        var BL_ = new tb_60movimientosBL();
                        var BE_ = new tb_60movimientoscab();
                        DataTable dt = new DataTable();
                        BE_.tip_op = "OP";
                        BE_.ser_op = ser_op.Text.Trim().PadLeft(4, '0');
                        BE_.num_op = num_op.Text.Trim().PadLeft(10, '0');
                        BE_.productoid = DT.Rows[0]["productid"].ToString().Trim();
                        BE_.moduloid = modulo.Trim().ToString();
                        BE_.idx = "ONE";

                        dt = BL_.GetvalidaMov(EmpresaID, BE_).Tables[0];
                        string arows = dt.Rows[0].Field<string>("Idx");

                        //if (arows == "0")
                        //{
                        //    MessageBox.Show("El Producto seleccionado no pertenece a la OP N°: " + ser_op.Text + "-" + num_op.Text +
                        //        ", \n Verificar OP ", "Confirmación",
                        //        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        //}

                        if (tipoperacionid.SelectedValue.ToString() == "02")
                        {
                            xcantidad = Convert.ToDecimal(DTR.Rows[0]["rollomedcompra"].ToString().Trim().PadLeft(1, '0'));
                        }
                        else
                        {
                            xcantidad = Convert.ToDecimal(DTR.Rows[0]["rollostock"].ToString().Trim().PadLeft(1, '0'));
                        }

                        tipcamb = Convert.ToDecimal(tcamb.Text.Trim());
                        if (tipcamb <= 0)
                        {
                            tipcamb = 1;
                        }

                        if (moneda.SelectedValue.ToString() == "S")
                        {
                            xprecunit = xpreciorollo;
                        }
                        else
                        {
                            xprecunit = xpreciorollo / tipcamb;
                        }

                        if (tipoperacionid.SelectedValue.ToString() == "02" && Convert.ToDecimal(DTR.Rows[0]["rollovaloractual"].ToString().Trim().PadLeft(1, '0')) > 0)
                        {
                            xprecunit = Convert.ToDecimal(DTR.Rows[0]["rollovaloractual"].ToString().Trim().PadLeft(1, '0'));
                        }

                        imporfac = Math.Round(xcantidad * Convert.ToDecimal(xprecunit), 6);

                        _cal_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[nrow].Cells["valor"].Value = xpreciorollo;
                        griddetallemov.Rows[nrow].Cells["importe"].Value = Math.Round(xcantidad * xpreciorollo, 6);
                        griddetallemov.Rows[nrow].Cells["dtotimpto"].Value = totimpx;

                        griddetallemov.Rows[nrow].Cells["cantidad"].Value = xcantidad;
                        griddetallemov.Rows[nrow].Cells["precunit"].Value = xprecunit;
                        griddetallemov.Rows[nrow].Cells["importfac"].Value = Math.Round(imporfac, 2);
                        Tabladetallemov.AcceptChanges();
                        griddetallemov.CurrentCell = griddetallemov.Rows[nrow].Cells["cantidad"];
                    }
                    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);
                }
            }
            else
            {
                MessageBox.Show("Rollo no Existe !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void data_Tabladetallemovmov()
        {
            try
            {
                Decimal xxprecventa = 0, xxcostoultimo = 0, xxstock = 0, xxcostopromed = 0;
                griddetallemov.AutoGenerateColumns = false;
                var BL = new tb_60movimientosdetBL();
                var BE = new tb_60movimientosdet();
                var 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(10, '0');

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

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

                if (Tabladetallemov != null)
                {
                    Tabladetallemov.Clear();
                }
                foreach (DataRow fila in dt.Rows)
                {
                    var BL2 = new tb_60local_stockBL();
                    var BE2 = new tb_60local_stock();
                    var 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"]);
                            }
                        }
                    }
                    var BLR = new tb_ta_prodrolloBL();
                    var DTR = new DataTable();
                    DTR = BLR.GetOne(EmpresaID, fila["rollo"].ToString()).Tables[0];
                    if (DTR.Rows.Count > 0)
                    {
                        xxstock = Convert.ToDecimal(DTR.Rows[0]["rollostock"]);
                    }
                    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;

                    var cantidad = Math.Round(Convert.ToDecimal(fila["cantidad"]), 4);
                    var 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["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(18) / 100), 6);
                    row["almacaccionid"] = almacaccionid.Trim();

                    row["unmed"] = fila["unmed"].ToString().Trim();
                    row["ubicacion"] = fila["ubicacion"].ToString().Trim();
                    row["nserie"] = fila["nserie"].ToString().Trim();
                    Tabladetallemov.Rows.Add(row);
                }
                griddetallemov.DataSource = Tabladetallemov;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }