public static VentaEntidad Map(CrearVentaDTO crearEntidadDTO) { VentaEntidad ventaEntidad = new VentaEntidad(); List <DetalleVentaEntidad> lstDetalleVenta = new List <DetalleVentaEntidad>(); if (crearEntidadDTO != null) { ventaEntidad.PersonaId = crearEntidadDTO.PersonaId; ventaEntidad.TipoComprobante = crearEntidadDTO.TipoComprobante; ventaEntidad.SerieComprobante = crearEntidadDTO.SerieComprobante; ventaEntidad.Impuesto = crearEntidadDTO.Impuesto; ventaEntidad.Total = crearEntidadDTO.Total; ventaEntidad.Estatus = "Act"; if (crearEntidadDTO.DetalleVentas != null) { foreach (var elem in crearEntidadDTO.DetalleVentas) { lstDetalleVenta.Add(new DetalleVentaEntidad { ProductoId = elem.ProductoId, Cantidad = elem.Cantidad, Descuento = elem.Descuento, Precio = elem.Precio, Estatus = "Act" }); } } ventaEntidad.DetalleVentas = lstDetalleVenta; } return(ventaEntidad); }
public async Task <ActionResult> CrearVentaAsync([FromBody] CrearVentaDTO crearVentaDTO) { var cliente = await _clientesServicios.ObtenerClienteIdAsync(crearVentaDTO.PersonaId); if (cliente == null) { return(BadRequest("Cliente no encontrado")); } if (crearVentaDTO.Total <= 0) { return(BadRequest("El total de ventas no puede ser cero, verifique la venta")); } if (crearVentaDTO.Impuesto <= 0) { return(BadRequest("El impuesto no puede ser cero, verifique el impuesto")); } var ventaEntidad = VentaMapper.Map(crearVentaDTO); foreach (var detalle in ventaEntidad.DetalleVentas) { if (await _productosServicios.ObtenerProductoPorIdAsync(detalle.ProductoId) == null) { return(BadRequest("Uno o más productos no estan registrados en la base de datos")); } if (detalle.Precio <= 0) { return(BadRequest("El precio del producto no puede ser cero, verifique el precio porfavor")); } if (detalle.Cantidad <= 0) { return(BadRequest("La cantidad del producto no puede ser cero, verifiquelo porfavor")); } } var respuesta = await _ventasServicios.CrearVentaAsync(ventaEntidad); if (!respuesta) { return(BadRequest("La venta no pudo realizarse correctamente")); } return(Ok()); }