Пример #1
0
        public async Task <IActionResult> SetSaveSale(Guid?id)
        {
            var validateToken = await ValidatedToken(_configuration, _getHelper, "movimiento");

            if (validateToken != null)
            {
                return(Json(new { Reiniciar = true, Error = true }));
            }

            if (!await ValidateModulePermissions(_getHelper, moduloId, eTipoPermiso.PermisoEscritura))
            {
                return(Json(new { Reiniciar = true, Error = true }));
            }

            if (id == null || id == Guid.Empty)
            {
                TempData["toast"] = "Identificador de la venta incorrecto.";
                return(Json(new { Reiniciar = true, Error = true }));
            }
            var ventaNoAplicada = await _context.VentasNoAplicadas
                                  .FirstOrDefaultAsync(v => v.VentaNoAplicadaID == id);

            if (ventaNoAplicada == null)
            {
                TempData["toast"] = "Identificador de la venta incorrecto.";
                return(Json(new { Reiniciar = true, Error = true }));
            }

            var ventasNoAplicadasDetalle = await _context.VentasNoAplicadasDetalle
                                           .Where(v => v.VentaNoAplicadaID == id).ToListAsync();

            if (ventasNoAplicadasDetalle == null || ventasNoAplicadasDetalle.Count == 0)
            {
                return(Json(new { Estatus = "No hay registros para almacenar.", Error = true }));
            }

            var ventasConDetalle = await(from v in _context.VentasNoAplicadas
                                         join d in _context.VentasNoAplicadasDetalle
                                         on v.VentaNoAplicadaID equals d.VentaNoAplicadaID
                                         where v.UsuarioID == token.UsuarioID
                                         select v.VentaNoAplicadaID
                                         ).Distinct().ToListAsync();

            if (ventasConDetalle != null)
            {
                ventaNoAplicada = await _context.VentasNoAplicadas
                                  .FirstOrDefaultAsync(v => !ventasConDetalle.Contains(v.VentaNoAplicadaID) &&
                                                       v.UsuarioID == token.UsuarioID);
            }

            if (ventasConDetalle == null || ventaNoAplicada == null)
            {
                ventaNoAplicada = new VentaNoAplicada()
                {
                    Fecha             = DateTime.Now,
                    UsuarioID         = token.UsuarioID,
                    VentaNoAplicadaID = Guid.NewGuid()
                };

                _context.VentasNoAplicadas.Add(ventaNoAplicada);

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (Exception)
                {
                    return(Json(new { Estatus = "La venta no puede ser inicializada.", Error = true }));
                }
            }

            return(Json(new { Error = false }));
        }
Пример #2
0
        public async Task <IActionResult> Index(Guid?id)
        {
            var validateToken = await ValidatedToken(_configuration, _getHelper, "movimiento");

            if (validateToken != null)
            {
                return(validateToken);
            }

            if (!await ValidateModulePermissions(_getHelper, moduloId, eTipoPermiso.PermisoEscritura))
            {
                return(RedirectToAction(nameof(Index)));
            }

            VentaNoAplicada ventaNoAplicada = new VentaNoAplicada();

            if (id == null)
            {
                ventaNoAplicada = await _context.VentasNoAplicadas
                                  .OrderByDescending(v => v.Fecha)
                                  .FirstOrDefaultAsync(v => v.UsuarioID == token.UsuarioID);
            }
            else
            {
                ventaNoAplicada = await _context.VentasNoAplicadas
                                  .OrderByDescending(v => v.Fecha)
                                  .FirstOrDefaultAsync(v => v.VentaNoAplicadaID == id);
            }

            if (ventaNoAplicada == null)
            {
                ventaNoAplicada = new VentaNoAplicada()
                {
                    Fecha             = DateTime.Now,
                    UsuarioID         = token.UsuarioID,
                    VentaNoAplicadaID = Guid.NewGuid()
                };

                _context.VentasNoAplicadas.Add(ventaNoAplicada);
            }

            decimal total        = 0;
            var     ventaDetalle = await _context.VentasNoAplicadasDetalle
                                   .Include(v => v.Productos)
                                   .Where(v => v.VentaNoAplicadaID == ventaNoAplicada.VentaNoAplicadaID)
                                   .ToListAsync();

            if (ventaDetalle != null)
            {
                foreach (var item in ventaDetalle)
                {
                    total += item.Cantidad * item.PrecioVenta;
                }
            }

            VentasNoAplicadasViewModel venta = new VentasNoAplicadasViewModel()
            {
                Fecha                    = ventaNoAplicada.Fecha,
                ImporteTotal             = total,
                UsuarioID                = token.UsuarioID,
                VentaNoAplicadaID        = ventaNoAplicada.VentaNoAplicadaID,
                VentasNoAplicadasDetalle = ventaDetalle
            };

            ViewBag.Id = token.ColaboradorID;

            try
            {
                await _context.SaveChangesAsync();

                return(View(venta));
            }
            catch (Exception)
            {
                TempData["toast"] = "La venta no puede ser inicializada.";
                return(RedirectToAction("Index", "Movimiento"));
            }
        }