public async Task <IActionResult> OnPost(int[] IdProducto, int[] CantProducto, int[] IdServicio, int[] CantServicio) { Venta venta = new Venta(); int IdUsuario = HttpContext.Session.GetInt32("IdUsuario").Value; venta.IdUsuario = IdUsuario; venta.Fecha = DateTime.Now; await this._context.Ventas.AddAsync(venta); await this._context.SaveChangesAsync(); //En el siguiente for se van a registrar los detalles de venta (Productos) int i; int len = IdProducto.Length; Precioventa precioventa; Detalleventa dtv = null; Detalleservicio dts = null; for (i = 0; i < len; i++) { dtv = new Detalleventa(); dtv.IdVenta = venta.Id; dtv.Cantidad = CantProducto[i]; precioventa = this._context.Precioventa.Where(p => p.IdProducto == IdProducto[i]).OrderByDescending(p => p.Fecha).FirstOrDefault(); dtv.IdPrecioVenta = precioventa.Id; dtv.IdPrecioVentaNavigation = precioventa;; await this._context.Detalleventa.AddAsync(dtv); await this._context.SaveChangesAsync(); int Existencia = 0; //traer la existencia del ultimo kardex IList <ConsultaKardex> ck = _context.ConsultaKardex.FromSql(ConsultaKardex.queryOne(), precioventa.IdProducto, precioventa.IdProducto).ToList(); if (ck != null && ck.Count > 0) { Existencia = ck.Last().Existencia; } if (Existencia <= 0) { //no hay productos } Existencia -= dtv.Cantidad; Kardex kardex = new Kardex { Existencia = Existencia, IdDetalleVenta = dtv.Id, IdProducto = precioventa.IdProducto, Fecha = venta.Fecha }; _context.Kardex.Add(kardex); await _context.SaveChangesAsync(); } len = IdServicio.Length; for (i = 0; i < len; i++) { dts = new Detalleservicio(); dts.IdVenta = venta.Id; dts.Cantidad = CantServicio[i]; dts.IdTipoServicio = IdServicio[i]; await this._context.Detalleservicio.AddAsync(dts); await this._context.SaveChangesAsync(); } Accion Accion = new Accion(); Accion.IdBitacora = HttpContext.Session.GetInt32("IdBitacora").Value; Accion.Hora = DateTime.Now; Accion.Descripcion = "registró una venta"; this._context.Add(Accion); this._context.SaveChanges(); return(RedirectToPage("/Ventas/ListaVenta")); }
public async Task <JsonResult> OnPost(Compra Compra, Detallecompra[] Detalles, decimal[] PrecioVenta, IFormFile Comprobante) { /* * Cambiar aqui * * * */ Compra.IdUsuario = HttpContext.Session.GetInt32("IdUsuario").Value; _context.Compras.Add(Compra); await _context.SaveChangesAsync(); string Mensaje = ""; Documento documento = new Documento(); bool error = false; string Ruta = "Comprobantes"; //envio imagen if (Comprobante != null) { //directorio de destino var filepath = "wwwroot/" + Ruta + "/"; var filename = Comprobante.FileName; //validar antes de subir var isValidName = ValidFileName(filepath, filename); //nombre valido y el archivo no existe if (isValidName && !FileExists(filepath, filename)) { await UploadFile(filepath, filename, Comprobante); documento.Nombre = filename; documento.IdCompra = Compra.Id; documento.IdRuta = (await _context.Rutas.Where(r => EF.Functions.Like(r.Nombre, $"%{Ruta}%")).FirstOrDefaultAsync()).Id; _context.Documentos.Add(documento); await _context.SaveChangesAsync(); Mensaje = "Se agrego correctamente"; error = false; // // // } else if (FileExists(filepath, filename)) { //el ya archivo existe Mensaje = "El documento: " + filename + " ya existe. por favor cambie el nombre del archivo que quiere subir e intentelo de nuevo"; error = true; } else if (!isValidName) { Mensaje = "El nombre de archivo: " + filename + " es incorrecto"; error = true; } }//envio comprobante //registrar los detalles for (var i = 0; i < Detalles.Length; i++) { Detallecompra det; det = Detalles[i]; det.IdProductoNavigation = null; det.Kardex = null; det.IdCompra = Compra.Id; _context.Precioventa.Add( new Precioventa { Fecha = Compra.Fecha, IdProducto = det.IdProducto, Valor = PrecioVenta[i], } ); _context.Detallecompra.Add( det ); //necesario para que el detalle tenga su id correspondiente await _context.SaveChangesAsync(); int Existencia = 0; //traer la existencia del ultimo kardex IList <ConsultaKardex> ck = _context.ConsultaKardex.FromSql(ConsultaKardex.queryOne(), det.IdProducto, det.IdProducto).ToList(); if (ck != null && ck.Count > 0) { Existencia = ck.Last().Existencia; } Existencia += det.Cantidad; Kardex kardex = new Kardex { Existencia = Existencia, IdDetalleCompra = det.Id, IdProducto = det.IdProducto, Fecha = Compra.Fecha }; _context.Kardex.Add(kardex); await _context.SaveChangesAsync(); } Accion Accion = new Accion(); Accion.IdBitacora = HttpContext.Session.GetInt32("IdBitacora").Value; Accion.Hora = DateTime.Now; Accion.Descripcion = "registró una compra"; this._context.Add(Accion); this._context.SaveChanges(); return(new JsonResult( new { Mensaje = "Mensaje de Prueba" } )); }//fin OnPost