Exemplo n.º 1
0
        private void RefreshSources(List <PublicacionNotCalified> publicationNotCalified)
        {
            ClearDataGridView();
            var publicationNotCalifiedDictionary = new Dictionary <int, PublicacionNotCalified>();

            if (publicationNotCalified == null)
            {
                //The datasource must be all the publications not calified records stored in the database
                _publicationsNotCalified         = CalificacionPersistance.GetAllPubicacionNotCalified(SessionManager.CurrentUser);
                publicationNotCalifiedDictionary = _publicationsNotCalified.ToDictionary(a => a.ID, a => a);;
            }
            else
            {
                //The datasource must be the list of publications not calified received as parameter
                publicationNotCalifiedDictionary = publicationNotCalified.ToDictionary(a => a.ID, a => a);
            }

            var bind = publicationNotCalifiedDictionary.Values.Select(a => new
            {
                Codigo      = a.ID,
                Descripcion = a.Descripcion,
                Precio      = a.Precio
            });

            dgvPublicaciones.DataSource          = bind.ToList();
            dgvPublicaciones.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvPublicaciones.CurrentCell         = null;
            ShowStars();
        }
        private void LblComprar_Click(object sender, EventArgs e)
        {
            try
            {
                if (SessionManager.CurrentRol.Descripcion == "Administrador General")
                {
                    throw new Exception("Siendo un administrador no puede comprar ni ofertar");
                }

                List <PublicacionNotCalified> publications = CalificacionPersistance.GetAllPubicacionNotCalified(SessionManager.CurrentUser);
                if (publications.Count > 5)
                {
                    throw new Exception("Tiene demasiadas compras sin calificar, por favor califíquelas para poder realizar una compra");
                }

                //Valido que ingrese una cantidad válida (mayor a 0 y menor que el stock)
                if (txtCantidad.Text == "" || Int32.Parse(txtCantidad.Text) < 0 || Int32.Parse(txtCantidad.Text) > Int32.Parse(lblStock.Text))
                {
                    throw new Exception("Ingrese una cantidad válida (mayor a 0 y menor que el stock actual)");
                }

                //Creo la nueva compra y la inserto
                Compra newPurchase = new Compra();
                newPurchase.Usuario     = SessionManager.CurrentUser;
                newPurchase.Publicacion = CurrentPublication;
                newPurchase.Fecha       = ConfigurationVariables.FechaSistema;
                newPurchase.Cantidad    = Int32.Parse(txtCantidad.Text);
                newPurchase             = CompraPersistance.Insert(newPurchase, null);

                //Resto el stock de la publicación
                CurrentPublication.Stock = CurrentPublication.Stock - newPurchase.Cantidad;
                if (CurrentPublication.Stock == 0)
                {
                    CurrentPublication.EstadoPublicacion = EstadoPublicacionPersistance.GetById(4); //finalizada
                }
                PublicacionPersistance.Update(CurrentPublication);

                //Le muestro al usuario los datos del vendedor
                var frmDatosVendedor = new FrmDatosVendedor(CurrentPublication.UsuarioCreador);
                frmDatosVendedor.ShowDialog();

                RefreshSources();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void lblOfertar_Click(object sender, EventArgs e)
        {
            try
            {
                if (SessionManager.CurrentRol.Descripcion == "Administrador General")
                {
                    throw new Exception("Siendo un administrador no puede comprar ni ofertar");
                }

                List <PublicacionNotCalified> publications = CalificacionPersistance.GetAllPubicacionNotCalified(SessionManager.CurrentUser);
                if (publications.Count > 5)
                {
                    throw new Exception("Tiene demasiadas compras sin calificar, por favor califíquelas para poder realizar una compra");
                }

                //Valido que la oferta sea mayor a la última
                if (txtMonto.Text == "" || Int32.Parse(txtMonto.Text) <= Int32.Parse(lblPrecio.Text))
                {
                    throw new Exception("El monto a ofertar debe ser mayor que la oferta actual");
                }

                //Creo la nueva oferta y la inserto
                Oferta newOffer = new Oferta();
                newOffer.IdUsuario     = SessionManager.CurrentUser.ID;
                newOffer.IdPublicacion = CurrentPublication.ID;
                newOffer.Fecha         = ConfigurationVariables.FechaSistema;
                newOffer.Monto         = Int32.Parse(txtMonto.Text);
                newOffer = OfertaPersistance.Insert(newOffer, null);
                MessageBox.Show("Se insertó la oferta correctamente");
                RefreshSources();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }