Beispiel #1
0
        // Evento para realizar venta y pagar factura
        protected void btVentaProductosAceptarPagada_Click(object sender, EventArgs e)
        {
            try
            {
                // Casteo de listados
                List <Cliente>  listadoClientes  = (List <Cliente>)Session["Clientes"];
                List <Producto> listadoProductos = (List <Producto>)Session["Productos"];
                List <Ventas>   listadoVentas    = (List <Ventas>)Session["Ventas"];

                int      numeroVentaAuto  = 0;
                int      documentoCliente = Convert.ToInt32(ddlVentaProductosClientes.SelectedValue);
                long     codigoProducto   = Convert.ToInt64(ddlVentaProductosProducto.SelectedValue);
                DateTime fechaVenta       = DateTime.Now;
                DateTime fechaPago        = DateTime.Now;
                double   descuentoCliente = Convert.ToDouble(lbVentaProductosDescuentoResultado.Text);
                bool     pagarAhora       = true;
                double   totalVenta       = 0;

                // Creacion codigo de factura
                if (listadoVentas.Count == 0)
                {
                    Random rnd = new Random();
                    numeroVentaAuto = rnd.Next(1, 999999999);
                }

                if (listadoVentas.Count != 0)
                {
                    Random rnd = new Random();
                    numeroVentaAuto = rnd.Next(1, 999999999);
                }

                else
                {
                    foreach (var venta in listadoVentas)
                    {
                        while (venta.BuscarExistenciaNumeroVenta(numeroVentaAuto, listadoVentas) == true && numeroVentaAuto == 0)
                        {
                            Random rnd = new Random();
                            numeroVentaAuto = rnd.Next(1, 999999999);
                        }
                    }
                }

                // Asignacion de precio
                foreach (var producto in listadoProductos)
                {
                    if (producto.Código == codigoProducto)
                    {
                        totalVenta = producto.Precio * Convert.ToInt32(cantidadProducto.Text);
                    }
                }

                if (descuentoCliente > 0)
                {
                    totalVenta = (descuentoCliente * totalVenta) / 100;
                }

                Ventas ventaAgregar = new Ventas(numeroVentaAuto, documentoCliente, codigoProducto, fechaVenta, fechaPago, totalVenta, Convert.ToInt32(cantidadProducto.Text), pagarAhora);
                Session["Ventas"] = ventaAgregar.Agregar(listadoVentas, ventaAgregar);

                // Reseteamos sessiones para proximo ingreso
                Session["ClientesListados"]  = null;
                Session["ProductosListados"] = null;

                Response.Redirect("Default.aspx");
            }

            catch (Exception ex)
            {
                lbventaProdError.Visible = true;
                lbventaProdError.Text    = "Ha ocurrido un error." + ex.Message;
            }
        }