Ejemplo n.º 1
0
        public async Task <ActionResult <FacturaDto> > CreateEdit([FromBody] FacturaDto factura)
        {
            if (factura.IdFactura == 0)
            {
                await _facturaRepository.CreateFactura(factura);

                if (await _facturaRepository.SaveAllAsync())
                {
                    return(factura);
                }
                else
                {
                    return(BadRequest("Unable to add factura"));
                }
            }

            else
            {
                var facturaFromDB = await _facturaRepository.GetFacturaAsync(factura.IdFactura);

                _mapper.Map(factura, facturaFromDB);

                _facturaRepository.UpdateFactura(facturaFromDB);

                if (await _facturaRepository.SaveAllAsync())
                {
                    return(NoContent());
                }

                else
                {
                    return(BadRequest("Unable to update factura"));
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <Factura> CreateFactura(FacturaDTO factura)
        {
            Producto producto = await productoRepository.GetProducto(factura.ProductoId);

            if (producto == null)
            {
                return(null);
            }

            Preventa preventa = await preventaRepository.GetPreventa(factura.PreventaId);

            if (preventa == null)
            {
                return(null);
            }

            Factura nuevaFactura = new Factura
            {
                Preventa     = preventa,
                Producto     = producto,
                FacturaFecha = DateTime.UtcNow,
                Cantidad     = factura.CantidadProducto,
                Total        = producto.ValorUnitario * factura.CantidadProducto
            };

            return(await facturaRepository.CreateFactura(nuevaFactura));
        }