private void btn_grabar_Click(object sender, EventArgs e)
        {
            var sw_prosigue = false;
            if (ssModo == "NEW")
            {
                if (XNIVEL == "0" || XNIVEL == "1" || XNIVEL == "2")
                {
                    sw_prosigue = (MessageBox.Show("¿Desea Guardar Documento actual ?", "Mensaje del Sistema", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes);
                }

                string prodid = "";

                if (sw_prosigue)
                {

                    DataTable dt = new DataTable();
                    dt.Columns.Add("productid", typeof(System.String));

                    foreach (DataGridViewRow rowGrid in griddetallemov.Rows)
                    {
                        DataRow row = dt.NewRow();
                        row["productid"] = Convert.ToString(rowGrid.Cells[1].Value);
                        prodid = prodid+row["productid"].ToString();

                        dt.Rows.Add(row);
                    }

                    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 = prodid.ToString();
                    BE_.moduloid = modulo.Trim().ToString();
                    BE_.idx = "ALL";
                    dt_ = BL_.GetvalidaMov(EmpresaID, BE_).Tables[0];
                    string arows = dt_.Rows[0].Field<string>("Idx");

                    //if (arows == "0")
                    //{
                    //    MessageBox.Show("Algunos Productos de la lista no corresponden\n a la OP N°: " + ser_op.Text + "-" + num_op.Text +
                    //        ", Verificar OP ", "Confirmación",
                    //        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //}else
                    //{
                        Insert();
                    //}

                }
            }
            else
            {
                if (XNIVEL == "0" || XNIVEL == "1")
                {
                    sw_prosigue = (MessageBox.Show("¿Desea Modificar Documento actual ?", "Mensaje del Sistema", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes);

                    if (sw_prosigue)
                    {
                        Update();
                    }
                }
            }
        }
        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 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 RecibeRollos(DataTable dtresultado)
        {
            string prodid = "";
            try
            {
                if (dtresultado.Rows.Count > 0)
                {
                    var cont = 0;
                    foreach (DataRow fila in dtresultado.Rows)
                    {
                        cont = cont + 1;
                        if (griddetallemov.Rows.Count > 0)
                        {
                            var nFilaAnt = griddetallemov.RowCount - 1;
                            var xProductid = fila["productid"].ToString();
                            var xRollo = fila["rollo"].ToString();
                            var xProductname = fila["productname"].ToString();
                            var xActivo = Convert.ToBoolean(fila["check"].ToString());
                            Decimal xPrecio = 0;

                            if (xActivo)
                            {
                                xPrecio = Convert.ToDecimal(fila["precio"].ToString());
                            }

                            var xnserie = string.Empty;
                            if (modulo == "0500")
                            {
                                xnserie = fila["nserie"].ToString();
                            }

                            if (cont > 1)
                            {
                                Tabladetallemov.Rows.Add(VariablesPublicas.InsertIntoTable(Tabladetallemov));
                                Tabladetallemov.Rows[Tabladetallemov.Rows.Count - 1]["items"] = VariablesPublicas.CalcMaxField(Tabladetallemov, "items", 5);
                                griddetallemov.Rows[nFilaAnt + 1].Cells["productid"].Value = xProductid;
                                griddetallemov.Rows[nFilaAnt + 1].Cells["productname"].Value = xProductname;
                            }
                            else
                            {
                                griddetallemov.Rows[nFilaAnt].Cells["productid"].Value = xProductid;
                                griddetallemov.Rows[nFilaAnt].Cells["productname"].Value = xProductname;
                                if (modulo == "0500")
                                {
                                    griddetallemov.Rows[nFilaAnt].Cells["nserie"].Value = xnserie;
                                }
                            }
                            prodid = xProductid.ToString();
                            griddetallemov.CurrentCell = griddetallemov.Rows[griddetallemov.RowCount - 1].Cells["cantidad"];
                            griddetallemov.BeginEdit(true);

                            if (xActivo)
                            {
                                ValidaTablaDetalle(xRollo, xPrecio);
                            }
                            else
                            {
                                ValidaTabladetallemovmov(xRollo);
                            }
                        }
                    }
                }
                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 = prodid.ToString();
                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);
                //}

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