private void _RecalculoGrid()
        {
            if (almacaccionid.Length > 0)
            {
                try
                {
                    foreach (DataGridViewRow fila in griddetallemov.Rows)
                    {
                        Decimal precunit = 0, tipcamb = 0;
                        Decimal imporfac = 0;
                        var desct1 = 0;
                        Decimal totimpx = 0;
                        Decimal import = 0;
                        Decimal valor = 0;
                        Decimal xcantidad = 0, xprecio = 0, xstock = 0, xcostopromed = 0;
                        var xproductid = string.Empty;

                        xproductid = Convert.ToString(griddetallemov.Rows[fila.Index].Cells["productid"].Value);
                        xcantidad = Convert.ToDecimal(griddetallemov.Rows[fila.Index].Cells["cantidad"].Value);
                        xprecio = Convert.ToDecimal(griddetallemov.Rows[fila.Index].Cells["precunit"].Value);
                        xstock = Convert.ToDecimal(griddetallemov.Rows[fila.Index].Cells["stock"].Value);
                        xcostopromed = Convert.ToDecimal(griddetallemov.Rows[fila.Index].Cells["costopromed"].Value);

                        if (xcantidad < 0)
                        {
                            MessageBox.Show("Cantidad no puede ser negativo!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            xcantidad = 0;
                            griddetallemov.Rows[fila.Index].Cells["cantidad"].Value = xcantidad;
                            return;
                        }

                        if (xprecio < 0)
                        {
                            MessageBox.Show("Precio no puede ser negativo!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            xprecio = 0;
                            griddetallemov.Rows[fila.Index].Cells["precunit"].Value = xprecio;
                            return;
                        }

                        precunit = Math.Round(Convert.ToDecimal(xprecio), 6);
                        tipcamb = Convert.ToDecimal(tcamb.Text.Trim());

                        if (tipcamb <= 0)
                        {
                            tipcamb = 1;
                        }

                        _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);
                        }

                        if (almacaccionid.Trim().Substring(0, 1) == "2" && almacaccionid.Trim().Substring(0, 1) == "0")
                        {
                            if (xcantidad <= xstock)
                            {
                                valor = Math.Round(Convert.ToDecimal(xcostopromed), 6);
                            }
                            else
                            {
                                MessageBox.Show("Cantidad fuera de rango!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                xcantidad = 0;
                                griddetallemov.Rows[fila.Index].Cells["cantidad"].Value = xcantidad;
                                return;
                            }
                        }
                        else
                        {
                            if (almacaccionid.Trim().Substring(0, 1) == "1" && almacaccionid.Trim().Substring(0, 1) == "0")
                            {
                                valor = precunit;
                            }
                        }

                        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 (moneda.SelectedValue.ToString() == "$")
                        {
                            if (tipref.SelectedIndex != -1)
                            {
                                if (tipref.SelectedValue.ToString() == "OC")
                                {
                                    precunit = xprecio;
                                }
                                else
                                {
                                    precunit = precunit * tipcamb;
                                    xprecio = xprecio / tipcamb;
                                }
                            }
                            else
                            {
                                precunit = precunit * tipcamb;
                                xprecio = xprecio / tipcamb;
                            }
                        }
                        else
                        {
                            if (moneda.Text == "S/.")
                            {
                            }
                        }

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

                        griddetallemov.Rows[fila.Index].Cells["valor"].Value = precunit;
                        griddetallemov.Rows[fila.Index].Cells["importe"].Value = Math.Round(xcantidad * precunit, 6);
                        griddetallemov.Rows[fila.Index].Cells["precunit"].Value = xprecio;
                        griddetallemov.Rows[fila.Index].Cells["dtotimpto"].Value = totimpx;
                        griddetallemov.Rows[fila.Index].Cells["importfac"].Value = Math.Round(imporfac, 2);
                    }
                    calcular_totales();
                }
                catch (Exception ex)
                {
                    var error = string.Empty;
                    error = ex.GetType().ToString();
                    if (error == "System.Data.ConstraintException")
                    {
                        MessageBox.Show("Producto ya existe!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        private void btn_actualizar_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow fila in griddetallemov.Rows)
            {
                var xproductid  = griddetallemov.Rows[fila.Index].Cells["productid"].Value.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[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 ValidaTabladetallemovcopia(String vaproductid)
        {
            var xproductid = string.Empty;
            Decimal xprecio = 0, xprecunit = 0, xcantidad = 0, xcostoprom = 0, tipcamb = 0;
            var desct1 = 0;
            Decimal imporfac = 0;
            Decimal import = 0;
            Decimal totimpx = 0;

            xproductid = vaproductid.Trim();

            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["productname"].Value = string.Empty;
            txt_stock.Text = "0";
            txt_valor.Text = "0";
            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["precunit"].Value = "0";
            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importfac"].Value = "0";

            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)
                {
                    foreach (DataGridViewRow fila in griddetallemov.Rows)
                    {
                        foreach (DataGridViewColumn col in griddetallemov.Columns)
                        {
                            if (fila.Index >= 0)
                            {
                                if (Convert.ToString(griddetallemov.Rows[fila.Index].Cells["productid"].Value) == vaproductid.ToString())
                                {
                                    griddetallemov.Rows[fila.Index].Cells["productid"].Value = DT.Rows[0]["productid"].ToString().Trim();
                                    griddetallemov.Rows[fila.Index].Cells["productname"].Value = DT.Rows[0]["productname"].ToString().Trim();
                                    griddetallemov.Rows[fila.Index].Cells["rollo"].Value = string.Empty;

                                    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[fila.Index].Cells["cantidad"].Value);
                                    mvCantidad = Convert.ToDecimal(griddetallemov.Rows[fila.Index].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[fila.Index].Cells["stock_old"].Value = lsStock;
                                    griddetallemov.Rows[fila.Index].Cells["stock"].Value = dtstock;

                                    griddetallemov.Rows[fila.Index].Cells["dalmacaccionid"].Value = almacaccionid.ToString().Trim();
                                    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[fila.Index].Cells["precventa"].Value = xprecventa;
                                    griddetallemov.Rows[fila.Index].Cells["costoultimo"].Value = xcostoultimo;
                                    griddetallemov.Rows[fila.Index].Cells["costopromed"].Value = xcostoprom;

                                    xcantidad = Convert.ToDecimal(griddetallemov.Rows[fila.Index].Cells["cantidad"].Value);

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

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

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

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

                                    griddetallemov.Rows[fila.Index].Cells["cantidad"].Value = xcantidad;
                                    griddetallemov.Rows[fila.Index].Cells["precunit"].Value = xprecunit;
                                    griddetallemov.Rows[fila.Index].Cells["importfac"].Value = Math.Round(imporfac, 2);
                                    Tabladetallemov.AcceptChanges();
                                    griddetallemov.CurrentCell = griddetallemov.Rows[griddetallemov.RowCount - 1].Cells["cantidad"];
                                }
                            }
                            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 ValidaTabladetallemovmov(String valproductid)
        {
            var xproductid = string.Empty;
            Decimal xprecio = 0, xprecunit = 0, xcantidad = 0, xcostoprom = 0, tipcamb = 0;
            var desct1 = 0;
            Decimal imporfac = 0;
            Decimal import = 0;
            Decimal totimpx = 0;

            xproductid = valproductid.Trim();

            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 = xproductid;
            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);
            //}
            //else
            //{
            #region

            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["productname"].Value = string.Empty;
            txt_stock.Text = "0";
            txt_valor.Text = "0";
            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["precunit"].Value = "0";
            griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importfac"].Value = "0";

            if (xproductid.Trim().Length == 13)
            {
                var BL = new tb_60local_stockBL();
                var BE = new tb_60local_stock();
                var DT = new DataTable();
                BE.moduloid = modulo.ToString();
                BE.local = local.ToString();
                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["unmed"].Value = DT.Rows[0]["unmed"].ToString().Trim();
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["rollo"].Value = string.Empty;

                    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["dalmacaccionid"].Value = almacaccionid.ToString().Trim();
                    txt_stock.Text = Convert.ToString(dtstock);

                    if (almacaccionid.Substring(0, 1) == "2")
                    {
                        xprecventa = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["precventa"]).ToString("###,###,##0.000000"));

                        // LO COMENTAMOS PORQUE AHORA EL PRECIO LO TRAEMOS DEL COSTO PROMEDIO = costopromed
                        //xprecio = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costoultimo"]).ToString("###,###,##0.000000"));
                        xprecio = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costopromed"]).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")
                        {
                            // LO COMENTAMOS PORQUE AHORA EL PRECIO LO TRAEMOS DEL COSTO PROMEDIO = costopromed
                            //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");

                            xcostoultimo = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costopromed"]).ToString("###,###,##0.000000"));
                            xprecio = Convert.ToDecimal(Convert.ToDecimal(DT.Rows[0]["costopromed"]).ToString("###,###,##0.000000"));
                            txt_valor.Text = Convert.ToDecimal(DT.Rows[0]["costopromed"].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;

                    xcantidad = Convert.ToDecimal(griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["cantidad"].Value);

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

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

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

                    _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 = xprecunit;
                    griddetallemov.Rows[griddetallemov.CurrentCell.RowIndex].Cells["importe"].Value = Math.Round(xcantidad * xprecunit, 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 = 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
                {
                    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);
            }
            #endregion
            //}
        }
        private void Tabla_detOC2()
        {
            Decimal xxprecventa = 0, xxcostoultimo = 0, xxstock = 0, xxcostopromed = 0;
            griddetallemov.AutoGenerateColumns = false;

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

            BE.moduloid = "0100";
            BE.local = "001";
            BE.tipodoc = tipref.SelectedValue.ToString();
            BE.serdoc = serref.Text.Trim();
            BE.numdoc = numdococ1.Text.Trim() + numdococ.Text.Trim();

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

            if (dt.Rows.Count > 0)
            {
                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"]);
                            }
                        }
                        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();

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

                    if (tipref.SelectedIndex != -1)
                    {
                        var stock_old = Math.Round(Convert.ToDecimal(fila["cantidad_c"]), 4);
                        var 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);
                    var 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(18) / 100), 6);
                    row["almacaccionid"] = almacaccionid.Trim();
                    Tabladetallemov.Rows.Add(row);
                    griddetallemov.DataSource = Tabladetallemov;
                }
                _RecalculoGrid();
            }
            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 = "2014";
                numdococ.Text = string.Empty;
                return;
            }
        }
        private void DetalleMovimiento(String xtipodocalt, String xnumdocalt)
        {
            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;

                if (modulo == "0500")
                {
                    griddetallemov.Columns[4].Visible = true;
                    griddetallemov.Columns[6].Visible = false;
                    griddetallemov.Width = 1085;
                }
                else
                {
                    if (modulo == "0330")
                    {
                        griddetallemov.Columns[4].Visible = false;
                        griddetallemov.Columns[6].Visible = true;
                    }
                    else
                    {
                        griddetallemov.Columns[4].Visible = false;
                        griddetallemov.Columns[6].Visible = false;
                    }
                }
                BE.local = local;
                BE.tipodoc = xtipodocalt.ToString();
                BE.serdoc = serdoc.Text.Trim().PadLeft(4, '0');
                BE.numdoc = xnumdocalt.ToString();

                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.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 = 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;

                    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);
            }
        }
        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 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 ValidaProducto(String xproductid, Boolean retrn)
        {
            if (xproductid.Trim().Length > 0)
            {
                var BL = new tb_60local_stockBL();
                var BE = new tb_60local_stock();
                var DT = new DataTable();
                BE.moduloid = modulo;
                BE.productid = xproductid.Trim();
                DT = BL.GetAll(EmpresaID, BE).Tables[0];

                if (DT.Rows.Count > 0)
                {
                    productid.Text = DT.Rows[0]["productid"].ToString().Trim();
                    productname.Text = DT.Rows[0]["productname"].ToString().Trim();
                    form_bloqueado(true);
                    productid.Enabled = false;
                    btn_cancelar.Enabled = true;
                    btn_grabar.Enabled = true;
                    btn_detanadir.Enabled = true;
                    btn_log.Enabled = true;
                }
                else
                {
                    if (!retrn)
                    {
                        productid.Text = string.Empty;
                        productname.Text = string.Empty;
                        productid.Focus();
                    }
                }
            }
        }