private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            if (VerificaIngreso())
            {
                Entities.Tables.COMORDENCOMPRA _OCCabecera = new Entities.Tables.COMORDENCOMPRA();
                BLL.Tables.COMORDENCOMPRA      _OCCab      = new BLL.Tables.COMORDENCOMPRA();
                //Entities.Tables.COMORDENCOMPRADETALLE _OCDetalle = new Entities.Tables.COMORDENCOMPRADETALLE();
                List <Entities.Tables.COMORDENCOMPRADETALLE> _listDetalle = new List <Entities.Tables.COMORDENCOMPRADETALLE>();

                CultureInfo culture = new CultureInfo("en-US");
                _OCCabecera.ORDENCOMPRA_ID   = _OrdenCompraModifica;
                _OCCabecera.FECHA            = Convert.ToDateTime(DateTime.Now.ToShortDateString()); //FECHA
                _OCCabecera.PROVEED_ID       = this.comboBoxProveed.SelectedValue.ToString();        // PROVEEDOR
                _OCCabecera.ESTADO_ID        = 1;                                                    //Ojo ver que valor se envia despues  //ESTADO
                _OCCabecera.TIPOOC_ID        = 4;                                                    //ORDEN DE COMPRA DE SERVICIO, SIN STOCK
                _OCCabecera.MODIFICACOSTO    = 0;                                                    //MODIFICA COSTO
                _OCCabecera.CODEMP           = this.comboBoxcodEmp.SelectedValue.ToString();         //EMPRESA PARA LA CUAL SE HACE LA OC
                _OCCabecera.CONDICIONPAGO_ID = "";                                                   // CONDICIOND DE PAGO NO ENVIO NADA
                //ya la dejo autorizada
                //_OCCabecera.FECHAAUTORIZACION = Convert.ToDateTime(DateTime.Now.ToShortDateString()); //FECHA
                //_OCCabecera.USUARIOAUTORIZA = Clases.Usuario.UsuarioLogeado.usuario_Logeado;
                _OCCabecera.OBS = this.textBoxObs.Text.Trim();

                _OCCabecera.USUARIO      = Clases.Usuario.UsuarioLogeado.usuario_Logeado;
                _OCCabecera.PEDIDODIARIO = false;
                //PRORRATEO EMPRESA- CUOTAS
                if (!checkBoxCuotas.Checked && !checkBoxEmpresa.Checked)
                {
                    _OCCabecera.TIPOPRORRATEO = 1; //SIN PRORRATEO
                }
                if (checkBoxEmpresa.Checked && !checkBoxCuotas.Checked)
                {
                    _OCCabecera.TIPOPRORRATEO = 2; //SOLO EMPRESA
                }
                if (checkBoxCuotas.Checked && !checkBoxEmpresa.Checked)
                {
                    _OCCabecera.TIPOPRORRATEO = 3; //SOLO CUOTAS
                }

                if (checkBoxCuotas.Checked && checkBoxEmpresa.Checked)
                {
                    _OCCabecera.TIPOPRORRATEO = 4; //AMBOS
                }

                //FIN PRORRATEO
                if (this.comboBoxLugarEntrega.SelectedIndex > 0)
                {
                    _OCCabecera.SECTORENTREGA_ID = Convert.ToInt32(this.comboBoxLugarEntrega.SelectedValue);//SECTOR DE ENTREGA
                }

                bool _hayItems = false;
                //AHORA LOS OBJETOS DE LA TABLA COMORDENCOMPRADETALLE
                for (int i = 0; i < this.dataGridViewDetalleOC.Rows.Count - 1; i++)
                {
                    if (dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.CANTIDAD].Value != string.Empty && dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.CANTIDAD].Value != null && dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.PRECIO].Value != string.Empty && dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.PRECIO].Value != null && dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.DETALLE].Value != null && dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.DETALLE].Value != string.Empty)
                    {
                        Entities.Tables.COMORDENCOMPRADETALLE _OCDetalle = new Entities.Tables.COMORDENCOMPRADETALLE();
                        _OCDetalle.CODEMP         = this.comboBoxcodEmp.SelectedValue.ToString();                                                               //EMPRESA PARA LA CUAL SE HACE LA OC
                        _OCDetalle.ORDENCOMPRA_ID = _OrdenCompraModifica;
                        _OCDetalle.DETALLE        = dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.DETALLE].Value.ToString();                           //this.textBoxDetalle.Text.Trim();
                        _OCDetalle.NIVELAUTO_ID   = 1;                                                                                                          //LA AUTORIZA SERGIO !!!!
                        _OCDetalle.IMPORTE        = Convert.ToDouble(dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.PRECIO].Value.ToString(), culture); //Convert.ToDouble(this.textBoxImporte.Text.ToString(), culture);
                        _OCDetalle.CANTIDAD       = Convert.ToDecimal(dataGridViewDetalleOC.Rows[i].Cells[(int)Col_OCDetalle.CANTIDAD].Value.ToString(), culture);
                        _OCDetalle.NROITEM        = i + 1;

                        _listDetalle.Add(_OCDetalle);

                        _hayItems = true;
                    }
                }

                if (_hayItems)
                {
                    if (_OCCab.ModificaOCSinStock(_OCCabecera, _listDetalle))
                    {
                        MessageBox.Show("La orden de compra se modificó correctamente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        this.textBoxObs.Text = "";

                        this.textBoxImporte.Text                = string.Empty;
                        this.comboBoxProveed.SelectedIndex      = 0;
                        this.comboBoxLugarEntrega.SelectedIndex = 0;
                        dataGridViewDetalleOC.Rows.Clear();
                    }
                    else
                    {
                        MessageBox.Show("Error al modificar la Orden de Compra", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("No se puede modificar la orden de compra porque no tiene detalle", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridViewOCItemSinStock.Rows.Count > 0)
                {
                    if (checkBoxAutoriza.Checked || checkBoxNoAutoriza.Checked)
                    {
                        Entities.Tables.COMORDENCOMPRADETALLE        _itemOC = new Entities.Tables.COMORDENCOMPRADETALLE();
                        List <Entities.Tables.COMORDENCOMPRADETALLE> _items  = new List <Entities.Tables.COMORDENCOMPRADETALLE>();
                        Entities.Tables.COMORDENCOMPRA _itemCab = new Entities.Tables.COMORDENCOMPRA();
                        BLL.Tables.COMORDENCOMPRA      _auto    = new BLL.Tables.COMORDENCOMPRA();

                        for (int i = 0; i < this.dataGridViewOCItemSinStock.Rows.Count; i++)
                        {
                            _itemOC                = new Entities.Tables.COMORDENCOMPRADETALLE();
                            _itemOC.CODEMP         = dataGridViewOCItemSinStock.Rows[i].Cells[(int)Col_OCItem.CODEMP].Value.ToString();
                            _itemOC.ORDENCOMPRA_ID = Convert.ToInt32(dataGridViewOCItemSinStock.Rows[i].Cells[(int)Col_OCItem.NRO_OC].Value);
                            _itemOC.IMPORTE        = Convert.ToDouble(dataGridViewOCItemSinStock.Rows[i].Cells[(int)Col_OCItem.PRECIO].Value.ToString(), culture);
                            _itemOC.CANTIDAD       = Convert.ToDecimal(dataGridViewOCItemSinStock.Rows[i].Cells[(int)Col_OCItem.CANTIDAD].Value.ToString(), culture);
                            _itemOC.NROITEM        = Convert.ToInt32(dataGridViewOCItemSinStock.Rows[i].Cells[(int)Col_OCItem.ITEM].Value);
                            _itemOC.DETALLE        = dataGridViewOCItemSinStock.Rows[i].Cells[(int)Col_OCItem.DETALLE].Value.ToString();

                            //VERIFICA SI ESTA AUTORIZADO
                            if (checkBoxAutoriza.Checked)
                            {
                                _itemOC.NIVELAUTO_ID = 2;
                                _items.Add(_itemOC); //VOY AGREGANDO A LA COLECCION
                            }
                            //VERIFICA SI LO ESTA RECHAZANDO
                            if (checkBoxNoAutoriza.Checked)
                            {
                                _itemOC.NIVELAUTO_ID = 3;
                                _items.Add(_itemOC); //VOY AGREGANDO A LA COLECCION
                            }
                        }
                        //DATOS PARA LA CABECERA
                        _itemCab.CODEMP         = dataGridViewOCCabSinStock.Rows[dataGridViewOCCabSinStock.CurrentRow.Index].Cells[(int)Col_OCCab.CODEMP].Value.ToString();
                        _itemCab.ORDENCOMPRA_ID = Convert.ToInt32(dataGridViewOCCabSinStock.Rows[dataGridViewOCCabSinStock.CurrentRow.Index].Cells[(int)Col_OCCab.NRO_OC].Value);
                        //PASO EL USUARIO LOGEADO
                        _itemCab.USUARIOAUTORIZA = Clases.Usuario.UsuarioLogeado.usuario_Logeado.ToString();

                        //ACTUALIZO CABECERA E ITEMS
                        _auto.AutorizaOrdenCompra(_items, _itemCab);


                        MessageBox.Show("La autorización se guardó correctamente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //DialogResult _result = MessageBox.Show("Desea enviar al Proveedor la Orden de Compra por mail?", "Mensaje", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        //if (_result == System.Windows.Forms.DialogResult.OK)
                        //{

                        //    //ENVIO POR MAIL, SOLO LOS QUE SELECCIONO Y SI ESTE PROVEEDOR TIENE UN MAIL VÁLIDO
                        //    if (dataGridViewOCCabSinStock.Rows[dataGridViewOCCabSinStock.CurrentRow.Index].Cells[(int)Col_OCCab.EMAIL].Value != null && dataGridViewOCCabSinStock.Rows[dataGridViewOCCabSinStock.CurrentRow.Index].Cells[(int)Col_OCCab.EMAIL].Value != string.Empty)
                        //    {
                        //        //ExportToMSMail(_itemCab.CODEMP, _itemCab.ORDENCOMPRA_ID);
                        //        if (GenerarPDFOC(_itemCab.CODEMP, _itemCab.ORDENCOMPRA_ID, dataGridViewOCCabSinStock.Rows[dataGridViewOCCabSinStock.CurrentRow.Index].Cells[(int)Col_OCCab.EMAIL].Value.ToString()))
                        //        {
                        //            MessageBox.Show("Mail enviado correctamente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //        }
                        //        else
                        //        {
                        //            MessageBox.Show("No se pudo enviar el mail al proveedor", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        //        }
                        //    }



                        //}


                        TraeOCPendientes();
                        dataGridViewOCItemSinStock.Rows.Clear();
                    }

                    else
                    {
                        MessageBox.Show("Debe seleccionar al menos una opción de Autorización", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            catch (Exception ex)
            {
            }
        }