Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)//Agregar Pedido
        {
            if (MessageBox.Show("¿Seguro que desea agregar pedido?", "Importante", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (chContado.Checked)
                {
                    if (txtPrecio.Text != "")
                    {
                        if (dateTimePicker1.Value.Date.CompareTo(DateTime.Now.Date) >= 0)
                        {
                            if (cbBusqueda.SelectedIndex >= 0)
                            {
                                bool _agregado = false;
                                _ped = new Pedido();
                                _ped.Fecha_Entrega = dateTimePicker1.Value;
                                _ped.Precio_Total  = Convert.ToSingle(txtPrecio.Text);
                                _ped.Id_Repartidor = cbBusqueda.Items[cbBusqueda.SelectedIndex].ToString();
                                _ped.Productos     = _prod;
                                _ped.Contado       = true;
                                _ped.Id_Usuario    = _usr.Login;
                                _ped.Id_Cliente    = _cli.Id_Cliente;
                                string _res   = "";
                                int    _pedId = 0;
                                _co.Abrir();

                                _agregado = _co.AgregarPedido(_ped, ref _res, ref _pedId);
                                _co.Cerrar();

                                Conexion _conAux = new Conexion();
                                for (int i = 0; i < _cli.Precios.Count; i++)
                                {
                                    Producto _aux = ((Producto)_cli.Precios[i]);

                                    _conAux.Abrir();
                                    _agregado = _conAux.ModificarProductoCantidad(_aux, ref _res);
                                    _conAux.Cerrar();
                                }
                                if (_agregado)
                                {
                                    Pedido _aux = new Pedido();
                                    _res = "";

                                    _co.Abrir();
                                    _co.AtraparPedido(_pedId, ref _aux, ref _res);
                                    _co.Cerrar();

                                    Documentos.GenerarPedido(_aux, _cli);
                                    MessageBox.Show("Pedido Agregado a Cliente: " + _cli.Nombre + " " + _cli.Apellido, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    _prod = new ArrayList();
                                    MostrarProductos();
                                    txtDescripcion.Clear();
                                    txtNota.Clear();
                                    txtPrecio.Clear();
                                    txtSemanas.Clear();
                                    chContado.Checked = true;
                                }
                            }
                            else
                            {
                                MessageBox.Show("Seleccione a un Repartidor", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Fecha pasada", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ingresar al Menos un Producto al Pedido", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    if (txtPrecio.Text != "")
                    {
                        if (dateTimePicker1.Value.Date.CompareTo(DateTime.Now.Date) >= 0)
                        {
                            if (cbBusqueda.SelectedIndex >= 0 && txtSemanas.Text != "")
                            {
                                _no = new Nota();
                                try
                                {
                                    _no.Descripcion = txtDescripcion.Text;
                                    _no.Semanas     = Convert.ToInt32(txtSemanas.Text);
                                    _no.Pagare      = txtNota.Text;
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message + " Error en los datos", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                                bool _agregado = false;
                                _ped = new Pedido();
                                _ped.Fecha_Entrega = dateTimePicker1.Value;
                                _ped.Precio_Total  = Convert.ToSingle(txtPrecio.Text);
                                _no.Cantidad       = _ped.Precio_Total;
                                _ped.Id_Repartidor = cbBusqueda.Items[cbBusqueda.SelectedIndex].ToString();
                                _ped.Productos     = _prod;
                                _ped.Contado       = false;
                                _ped.Id_Usuario    = _usr.Login;
                                _no.Id_Cliente     = _cli.Id_Cliente;
                                _no.Id_User        = _usr.Login;
                                _ped.Id_Cliente    = _cli.Id_Cliente;
                                string _res   = "";
                                int    _pedId = 0;
                                _co.Abrir();
                                _agregado = _co.AgregarPedido(_ped, ref _res, ref _pedId);
                                _co.Cerrar();
                                _no.Id_Pedido = _pedId;
                                Conexion _conAux = new Conexion();
                                for (int i = 0; i < _cli.Precios.Count; i++)
                                {
                                    Producto _aux = ((Producto)_cli.Precios[i]);

                                    _conAux.Abrir();
                                    _agregado = _conAux.ModificarProductoCantidad(_aux, ref _res);
                                    _conAux.Cerrar();
                                }

                                Conexion _coNota = new Conexion();
                                _coNota.Abrir();
                                _agregado = _coNota.AgregarNota(_no, ref _res);
                                _coNota.Cerrar();

                                if (_agregado)
                                {
                                    Pedido _aux = new Pedido();
                                    _res = "";

                                    _co.Abrir();
                                    _co.AtraparPedido(_pedId, ref _aux, ref _res);
                                    _co.Cerrar();

                                    _co.Abrir();
                                    _co.AtraparNotaPedido(_pedId, ref _no);
                                    _co.Cerrar();

                                    Documentos.GenerarPedido(_aux, _cli);
                                    Documentos.GenerarNota(_no, _cli);

                                    MessageBox.Show("Pedido Agregado a Cliente: " + _cli.Nombre + " " + _cli.Apellido + " Con su respectiva nota", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    _prod = new ArrayList();
                                    MostrarProductos();
                                    txtDescripcion.Clear();
                                    txtNota.Clear();
                                    txtPrecio.Clear();
                                    txtSemanas.Clear();
                                    chContado.Checked = true;
                                }
                            }
                            else
                            {
                                MessageBox.Show("Seleccione a un Repartidor y las semanas", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Fecha pasada", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ingresar al Menos un Producto al Pedido", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }