예제 #1
0
        public async Task <IActionResult> Details(Guid?id)
        {
            var validateToken = await ValidatedToken(_configuration, _getHelper, "catalogo");

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

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

            if (id == null)
            {
                TempData["toast"] = "Identificacor incorrecto, verifique.";
                return(RedirectToAction(nameof(Index)));
            }

            var producto = await _getHelper.GetProductByIdAsync((Guid)id);

            if (producto == null)
            {
                TempData["toast"] = "Identificacor incorrecto, verifique.";
                return(RedirectToAction(nameof(Index)));
            }

            var productoDetailsViewModel = await _converterHelper.ToProductosDetailsViewModelAsync(producto);

            productoDetailsViewModel.PermisoEscritura = permisosModulo.PermisoEscritura;

            string directorio         = _configuration.GetValue <string>("DirectorioImagenProducto");
            string directorioProducto = Path.Combine(directorio, producto.ProductoID.ToString(), "md");

            if (Directory.Exists(directorioProducto))
            {
                DirectoryInfo directory = new DirectoryInfo(directorioProducto);
                string        _file     = "";
                List <Guid>   images    = new List <Guid>();
                foreach (var file in directory.GetFiles())
                {
                    _file = Path.GetFileNameWithoutExtension(file.ToString());
                    images.Add(Guid.Parse(_file));
                }
                productoDetailsViewModel.ProductoImages = images;
            }

            return(View(productoDetailsViewModel));
        }
예제 #2
0
        /// <summary>
        /// Convertir clase producto a productoViewModel (incluye clase paquete)
        /// </summary>
        /// <param name="producto"></param>
        /// <returns>ProductViewModel(class)</returns>
        public async Task <ProductoViewModel> ToProductosViewModelAsync(Producto producto)
        {
            var productoViewModel = new ProductoViewModel()
            {
                Activo              = producto.Activo,
                Codigo              = producto.Codigo,
                MarcaID             = producto.MarcaID,
                Marcas              = producto.Marcas,
                PrecioCosto         = producto.PrecioCosto,
                PrecioVenta         = producto.PrecioVenta,
                ProductoDescripcion = producto.ProductoDescripcion,
                ProductoID          = producto.ProductoID,
                ProductoNombre      = producto.ProductoNombre,
                TasaID              = producto.TasaID,
                TasasImpuestos      = producto.TasasImpuestos,
                TasasImpuestosDDL   = await _combosHelper.GetComboTasaImpuestosAsync(),
                Unidades            = producto.Unidades,
                UnidadesDDL         = await _combosHelper.GetComboUnidadesAsync(),
                UnidadID            = producto.UnidadID,
                //Paquete
                CantidadProductoxPaquete = 0,
                CodigoPieza = ""
            };

            var paquete = await _context.Paquetes.FindAsync(producto.ProductoID);

            if (paquete != null)
            {
                productoViewModel.CantidadProductoxPaquete = paquete.CantidadProductoxPaquete;

                var pieza = await _getHelper.GetProductByIdAsync(paquete.PiezaProductoID);

                productoViewModel.CodigoPieza = pieza.Codigo;
            }

            return(productoViewModel);
        }
예제 #3
0
        public async Task <IActionResult> SetSale(Guid?id, decimal importe)
        {
            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 = "Ingrese al menos un producto a la lista.", Error = true }));
            }

            var ventasNoAplicadasDetalleAgrupada = ventasNoAplicadasDetalle
                                                   .GroupBy(v => new { v.VentaNoAplicadaID, v.ProductoID, v.Cantidad, v.PrecioVenta })
                                                   .Where(v => v.Key.VentaNoAplicadaID == id)
                                                   .Select(v => new
            {
                v.Key.ProductoID,
                Cantidad = v.Sum(c => c.Cantidad),
                v.Key.PrecioVenta,
                Importe = v.Sum(i => i.Cantidad * i.PrecioVenta)
            }).ToList();

            decimal importeTotal = ventasNoAplicadasDetalleAgrupada.Sum(v => v.Importe);

            if (importeTotal > importe)
            {
                return(Json(new { Estatus = "Importe inferior al total.", Error = true }));
            }

            decimal?folio = await _context.Ventas.MaxAsync(v => (decimal?)v.Folio) ?? 0;

            if (folio == null)
            {
                folio = 0;
            }
            folio += 1;

            //ca,biar informaciopn
            Guid     almacenId = Guid.Parse("8706EF28-2EBA-463A-BAB4-62227965F03F");
            DateTime fecha     = DateTime.Now;

            Venta venta = new Venta()
            {
                AlmacenID     = almacenId,
                ClienteID     = Guid.Empty,
                Fecha         = fecha,
                Folio         = (decimal)folio,
                UsuarioID     = token.UsuarioID,
                VentaCierreID = Guid.Empty,
                VentaID       = (Guid)id
            };

            _context.Ventas.Add(venta);

            List <VentaImporte> ventasImporte = new List <VentaImporte>();
            VentaImporte        ventaImporte  = new VentaImporte()
            {
                FormaPagoID    = 1,
                Importe        = importeTotal,
                VentaID        = (Guid)id,
                VentaImporteID = Guid.NewGuid()
            };

            ventasImporte.Add(ventaImporte);
            _context.VentasImportes.Add(ventaImporte);

            List <VentaDetalle> ventasDetalle = new List <VentaDetalle>();

            foreach (var item in ventasNoAplicadasDetalleAgrupada)
            {
                var producto = await _getHelper.GetProductByIdAsync(item.ProductoID);

                if (producto == null)
                {
                    TempData["toast"] = "Producto incorrecto, venta no realizada";
                    return(Json(new { Reiniciar = true, Error = true }));
                }

                Guid    productoId = producto.ProductoID;
                decimal cantidad   = item.Cantidad;

                if (producto.Unidades.Pieza)
                {
                    cantidad = Math.Round(cantidad);
                }

                if (producto.Unidades.Paquete)
                {
                    var productoPieza = await _context.Paquetes
                                        .FirstOrDefaultAsync(p => p.ProductoID == item.ProductoID);

                    if (productoPieza != null)
                    {
                        productoId = productoPieza.PiezaProductoID;
                        cantidad   = cantidad * productoPieza.CantidadProductoxPaquete;
                    }
                }

                var existencia = await _context.Existencias
                                 .FirstOrDefaultAsync(e => e.ProductoID == productoId &&
                                                      e.AlmacenID == almacenId);

                if (existencia == null)
                {
                    _context.Existencias.Add(new Existencia()
                    {
                        AlmacenID           = almacenId,
                        ExistenciaEnAlmacen = cantidad * -1,
                        ExistenciaID        = Guid.NewGuid(),
                        ProductoID          = productoId
                    });
                }
                else
                {
                    existencia.ExistenciaEnAlmacen -= cantidad;
                    _context.Update(existencia);
                }

                var ventaDetalle = new VentaDetalle()
                {
                    Cantidad       = item.Cantidad,
                    PrecioCosto    = (decimal)producto.PrecioCosto,
                    PrecioVenta    = item.PrecioVenta,
                    ProductoID     = item.ProductoID,
                    VentaDetalleID = Guid.NewGuid(),
                    VentaID        = (Guid)id
                };

                ventasDetalle.Add(ventaDetalle);

                _context.VentasDetalle.Add(ventaDetalle);
            }

            VentasViewModel ventaViewModel = new VentasViewModel()
            {
                AlmacenID      = almacenId,
                ClienteID      = Guid.Empty,
                Fecha          = fecha,
                Folio          = (decimal)folio,
                ImporteCobro   = importe,
                ImporteTotal   = importeTotal,
                UsuarioID      = token.UsuarioID,
                VentaCierreID  = Guid.Empty,
                VentaID        = (Guid)id,
                VentasDetalle  = ventasDetalle,
                VentasImportes = ventasImporte
            };

            try
            {
                foreach (var item in ventasNoAplicadasDetalle)
                {
                    _context.VentasNoAplicadasDetalle.Remove(item);
                }
                _context.VentasNoAplicadas.Remove(ventaNoAplicada);

                await _context.SaveChangesAsync();
                await BitacoraAsync("SetSale", ventaViewModel);

                //return Json(new { Error = false });
                return(PartialView(ventaViewModel));
            }
            catch (Exception ex)
            {
                string excepcion = ex.InnerException != null?ex.InnerException.Message.ToString() : ex.ToString();
                await BitacoraAsync("SetSale", ventaViewModel, excepcion);

                return(Json(new { Estatus = "Venta no realizada.", Error = true }));
            }
        }
예제 #4
0
        public async Task <IActionResult> AddDetails(SalidaDetalle salidaDetalle)
        {
            var validateToken = await ValidatedToken(_configuration, _getHelper, "movimiento");

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

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

            if (salidaDetalle == null)
            {
                TempData["toast"] = "Identificador incorrecto.";
                return(RedirectToAction(nameof(Index)));
            }

            if (SalidaAplicada(salidaDetalle.SalidaID))
            {
                TempData["toast"] = "Salida aplicada no se permiten cambios.";
                return(RedirectToAction(nameof(Details), new { id = salidaDetalle.SalidaID }));
            }

            if (salidaDetalle.AlmacenID == null)
            {
                TempData["toast"] = "El campo almacén es requerido.";
                ModelState.AddModelError("AlmacenID", "El campo almacén es requerido.");
                return(View(salidaDetalle));
            }

            if (salidaDetalle.ProductoID == null)
            {
                TempData["toast"] = "El campo producto es requerido.";
                ModelState.AddModelError("ProductoID", "El campo producto es requerido.");
                return(View(salidaDetalle));
            }

            var almacen = await _getHelper.GetAlmacenByIdAsync((Guid)salidaDetalle.AlmacenID);

            var producto = await _getHelper.GetProductByIdAsync((Guid)salidaDetalle.ProductoID);

            TempData["toast"] = "Falta información en algún campo.";
            if (ModelState.IsValid)
            {
                if (almacen == null)
                {
                    TempData["toast"] = "El campo almacén es requerido.";
                    ModelState.AddModelError("AlmacenID", "El campo almacén es requerido.");
                    return(View(salidaDetalle));
                }

                if (producto == null)
                {
                    TempData["toast"] = "El campo producto es requerido.";
                    ModelState.AddModelError("ProductoID", "El campo producto es requerido.");
                    return(View(salidaDetalle));
                }

                var existencia = await _context.Existencias
                                 .FirstOrDefaultAsync(e => e.ProductoID == producto.ProductoID &&
                                                      e.AlmacenID == almacen.AlmacenID);

                salidaDetalle.Almacenes = almacen;
                salidaDetalle.Productos = producto;

                var cantidadTotalPorProducto = await _context.SalidasDetalle
                                               .Where(s => s.SalidaID == salidaDetalle.SalidaID &&
                                                      s.ProductoID == salidaDetalle.ProductoID &&
                                                      s.AlmacenID == salidaDetalle.AlmacenID)
                                               .SumAsync(s => s.Cantidad);

                if (existencia == null ||
                    (existencia.ExistenciaEnAlmacen - (cantidadTotalPorProducto + salidaDetalle.Cantidad)) < 0)
                {
                    ModelState.AddModelError("Cantidad", "La cantidad excede a la cantidad en inventario.");
                    return(View(salidaDetalle));
                }

                try
                {
                    salidaDetalle.SalidaDetalleID = Guid.NewGuid();

                    if (producto.Unidades.Pieza)
                    {
                        salidaDetalle.Cantidad = (int)salidaDetalle.Cantidad;
                    }

                    salidaDetalle.PrecioCosto = producto.PrecioCosto;

                    _context.Add(salidaDetalle);

                    await _context.SaveChangesAsync();

                    TempData["toast"] = "Los datos del producto fueron almacenados correctamente.";
                    await BitacoraAsync("Alta", salidaDetalle, salidaDetalle.SalidaID);

                    return(View(new SalidaDetalle()
                    {
                        AlmacenID = salidaDetalle.AlmacenID,
                        Almacenes = almacen,
                        SalidaID = salidaDetalle.SalidaID,
                        Cantidad = 0,
                        PrecioCosto = 0,
                    }));
                }
                catch (Exception ex)
                {
                    string excepcion = ex.InnerException != null?ex.InnerException.Message.ToString() : ex.ToString();

                    TempData["toast"] = "[Error] Los datos del producto no fueron almacenados.";
                    ModelState.AddModelError(string.Empty, "Error al guardar registro");
                    await BitacoraAsync("Alta", salidaDetalle, salidaDetalle.SalidaID, excepcion);
                }
            }

            salidaDetalle.Almacenes = almacen;
            salidaDetalle.Productos = producto;
            return(View(salidaDetalle));
        }
예제 #5
0
        public async Task <IActionResult> AddDetails(EntradaDetalle entradaDetalle)
        {
            var validateToken = await ValidatedToken(_configuration, _getHelper, "movimiento");

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

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

            if (entradaDetalle == null)
            {
                TempData["toast"] = "Identificador incorrecto.";
                return(RedirectToAction(nameof(Index)));
            }

            if (EntradaAplicada(entradaDetalle.EntradaID))
            {
                TempData["toast"] = "Entrada aplicada no se permiten cambios.";
                return(RedirectToAction(nameof(Details), new { id = entradaDetalle.EntradaID }));
            }

            if (entradaDetalle.AlmacenID == null)
            {
                TempData["toast"] = "El campo almacén es requerido.";
                ModelState.AddModelError("AlmacenID", "El campo almacén es requerido.");
                return(View(entradaDetalle));
            }

            if (entradaDetalle.ProductoID == null)
            {
                TempData["toast"] = "El campo producto es requerido.";
                ModelState.AddModelError("ProductoID", "El campo producto es requerido.");
                return(View(entradaDetalle));
            }

            var almacen = await _getHelper.GetAlmacenByIdAsync((Guid)entradaDetalle.AlmacenID);

            var producto = await _getHelper.GetProductByIdAsync((Guid)entradaDetalle.ProductoID);

            TempData["toast"] = "Falta información en algún campo.";

            ValidarDatosDelProducto(entradaDetalle);

            if (ModelState.IsValid)
            {
                if (almacen == null)
                {
                    TempData["toast"] = "El campo almacén es requerido.";
                    ModelState.AddModelError("AlmacenID", "El campo almacén es requerido.");
                    return(View(entradaDetalle));
                }

                if (producto == null)
                {
                    TempData["toast"] = "El campo producto es requerido.";
                    ModelState.AddModelError("ProductoID", "El campo producto es requerido.");
                    return(View(entradaDetalle));
                }

                try
                {
                    entradaDetalle.EntradaDetalleID = Guid.NewGuid();

                    if (producto.Unidades.Pieza)
                    {
                        entradaDetalle.Cantidad = (int)entradaDetalle.Cantidad;
                    }

                    _context.Add(entradaDetalle);

                    await _context.SaveChangesAsync();

                    TempData["toast"] = "Los datos del producto fueron almacenados correctamente.";
                    await BitacoraAsync("Alta", entradaDetalle, entradaDetalle.EntradaID);

                    return(View(new EntradaDetalle()
                    {
                        AlmacenID = entradaDetalle.AlmacenID,
                        Almacenes = almacen,
                        EntradaID = entradaDetalle.EntradaID,
                        Cantidad = 0,
                        PrecioCosto = 0,
                        PrecioVenta = 0
                    }));
                }
                catch (Exception ex)
                {
                    string excepcion = ex.InnerException != null?ex.InnerException.Message.ToString() : ex.ToString();

                    TempData["toast"] = "[Error] Los datos del producto no fueron almacenados.";
                    ModelState.AddModelError(string.Empty, "Error al guardar registro");
                    await BitacoraAsync("Alta", entradaDetalle, entradaDetalle.EntradaID, excepcion);
                }
            }

            entradaDetalle.Almacenes = almacen;
            entradaDetalle.Productos = producto;
            return(View(entradaDetalle));
        }