public async Task <IActionResult> AddSale([FromBody] SaleHeader Sale) { try { var sale_detail = Sale.SaleDetails.ToList(); // var totalLine = sale_detail.Sum(x=>x.TotalAmount);//from s in sale_detail.Select(x=>x.TotalAmount).Sum(); // Sale.TotalLine = totalLine == 0 ? 0: totalLine; foreach (var sd in sale_detail) { var p = _ProductService.Find(sd.ProductId); p.Inventory -= sd.Quantity; sd.WarrantyStart = DateTime.Now; sd.WarrantyEnd = DateTime.Now.AddMonths(p.Warranty); } _SaleHeaderService.Add(Sale); int res = await _unitOfWork.SaveChangesAsync(); if (res > 0) { operationResult.Success = true; operationResult.Message = "Added new record"; operationResult.Caption = "Add complete"; } } catch (System.Exception ex) { operationResult.Success = false; operationResult.Message = ex.ToString(); operationResult.Caption = "Add failed!"; } return(Ok(operationResult)); }
public async Task Validate(SaleHeader saleHeader) { if (saleHeader.SaleLines.Count == 0) { throw new Exception("Add at least one document line"); } if (saleHeader.SaleLines .AsEnumerable() .GroupBy(x => x.BeerId) .Where(g => g.Count() > 1) .Any() ) { throw new Exception("Duplicate beers are not allowed"); } List <WholesalerBeer> wholesalerBeers = (await _wholesalerBeerRepo.ListByWholesalerIdAsync(saleHeader.WholesalerId)).ToList(); List <int> beersNotAvailable = saleHeader.SaleLines .AsEnumerable() .Where(sl => wholesalerBeers .All(wb => wb.BeerId != sl.BeerId || wb.Stock < sl.Quantity) ) .Select(x => x.BeerId) .ToList(); if (beersNotAvailable.Count > 0) { throw new Exception("Following beer ids are not available at this wholesaler or are out of stock: " + String.Join(", ", beersNotAvailable)); } }
public async Task <ActionResult <SaleHeaderDto> > PostSale(PostSaleHeaderDto postSaleHeaderDto) { SaleHeader result = _mapper.Map <SaleHeader>(postSaleHeaderDto); _saleService.Create(result); return(CreatedAtAction(nameof(GetSale), new { id = result.Id }, _mapper.Map <SaleHeaderDto>(result))); }
public async Task <ActionResult <SaleHeaderDto> > GetSale(int id) { SaleHeader saleHeader = await _saleService.GetAsync(id); SaleHeaderDto result = _mapper.Map <SaleHeaderDto>(saleHeader); return(Ok(result)); }
public async Task <SaleHeader> GetAsync(int id) { SaleHeader saleHeader = await _context.Sales.FindAsync(id); if (saleHeader == null) { throw new RestException(HttpStatusCode.NotFound, new { sale = "Not found" }); } return(saleHeader); }
public async Task Compute(SaleHeader saleHeader) { List <Beer> beers = (await _beerRepository.PricesListAsync(saleHeader.SaleLines.AsEnumerable().Select(x => x.BeerId).ToList())).ToList(); foreach (SaleLine saleLine in saleHeader.SaleLines) { saleLine.UnitPrice = beers.AsEnumerable().Where(x => x.Id == saleLine.BeerId).First().Price; } saleHeader.Compute(); }
public async Task <ActionResult <SaleHeaderDto> > PostSale(PostSaleHeaderDto postSaleHeaderDto) { Wholesaler wholesaler = await _wholesalerService.GetAsync(postSaleHeaderDto.WholeSalerId); if (wholesaler == null) { return(NotFound()); } SaleHeader result = _mapper.Map <SaleHeader>(postSaleHeaderDto); await _saleHeaderService.Validate(result); await _saleHeaderService.Compute(result); _saleHeaderService.Add(result); await _saleHeaderService.SaveAsync(); return(CreatedAtAction(nameof(GetSale), new { id = result.Id }, _mapper.Map <SaleHeaderDto>(result))); }
public async Task <IActionResult> UpdateSale([FromBody] SaleHeader Sale) { try { _SaleHeaderService.Update(Sale); int res = await _unitOfWork.SaveChangesAsync(); if (res > 0) { operationResult.Success = true; operationResult.Message = "Update success"; operationResult.Caption = "Update complete"; } } catch (System.Exception ex) { operationResult.Success = false; operationResult.Message = ex.ToString(); operationResult.Caption = "Update failed!"; } return(Ok(operationResult)); }
public void Create(SaleHeader saleHeader) { saleHeader.Compute(); _context.Sales.Add(saleHeader); }
public void Update(SaleHeader entity) { _saleHeaderRepo.Update(entity); }
public void Add(SaleHeader entity) { _saleHeaderRepo.Add(entity); }