public IHttpActionResult GetSplitTimes(int Id) { var splitTimes = _context.SplitTimes.Where(p => p.Rental.RentalId == Id) .Include(p => p.Rental) .Include(p => p.Rental.Ps) .ToList(); var SplitTimeDtos = new List <SplitTimeDto>(); double rentalFee = 0; var rental = _context.Rentals.Include(r => r.Ps).Single(r => r.RentalId == Id); //whole rental time, and check if this rental is checkout or not to show modal as view mode or checkout mode TimeSpan rentalSpan = (rental.To == null) ? DateTime.Now.Subtract(rental.From) : Convert.ToDateTime(rental.To).Subtract(rental.From);; TimeSpan splitSpan; // if there is no splitTimes if (splitTimes.Count() == 0) { rentalFee = CalculateMethods.RentalFeeCalculate(rentalSpan.TotalMinutes, rental.Ps.Price); SplitTimeDtos.Add(new SplitTimeDto { RentalId = Id, //SplitSpans in this case is whole RentalSpan SplitSpan = string.Format("{0}:{1}", decimal.Truncate(Convert.ToDecimal(rentalSpan.TotalHours)), rentalSpan.Minutes), SplitFee = Convert.ToInt32(rentalFee), SplitSpanRatio = 100 }); //return whole rental span and rental fee return(Ok(SplitTimeDtos)); } for (int i = 0; i <= splitTimes.Count(); i++) { double splitSpanRatio = 0; if (i < splitTimes.Count()) { var from = (i == 0) ? rental.From : splitTimes[i - 1].TimeSplit; splitSpan = splitTimes[i].TimeSplit.Subtract(from); } else { splitSpan = (rental.To == null) ? DateTime.Now.Subtract(splitTimes[i - 1].TimeSplit) : Convert.ToDateTime(rental.To).Subtract(splitTimes[i - 1].TimeSplit); } rentalFee = CalculateMethods.RentalFeeCalculate(splitSpan.TotalMinutes, rental.Ps.Price); //calculate split span ratio to show on view splitSpanRatio = splitSpan.TotalMinutes * 100 / rentalSpan.TotalMinutes; SplitTimeDtos.Add(new SplitTimeDto { RentalId = Id, SplitSpan = string.Format("{0}:{1}", decimal.Truncate(Convert.ToDecimal(splitSpan.TotalHours)), splitSpan.Minutes), SplitFee = Convert.ToInt32(rentalFee), SplitSpanRatio = splitSpanRatio }); } return(Ok(SplitTimeDtos)); }
public IHttpActionResult GetRental(int Id) { var rental = _context.Rentals.Include(p => p.Ps) .SingleOrDefault(r => r.RentalId == Id); if (rental == null) { return(BadRequest("Invalid rental")); } var serviceFee = (from s in _context.ServiceRentals where s.RentalId == Id group s by 1 into g select new { svFee = g.Sum(x => (x.Service.Price * x.Quantity)) }).SingleOrDefault(); double rentalFee = 0; double rentalTimeSpan = (rental.To == null) ? DateTime.Now.Subtract(rental.From).TotalMinutes : Convert.ToDateTime(rental.To).Subtract(rental.From).TotalMinutes; int price = rental.Ps.Price; rentalFee = CalculateMethods.RentalFeeCalculate(rentalTimeSpan, price); var rentalDto = new RentalDto { RentalId = rental.RentalId, From = rental.From.ToString("d MMM H:m"), To = (rental.To == null) ? DateTime.Now.ToString("d MMM H:m") : Convert.ToDateTime(rental.To).ToString("d MMM H:m"), RentalFee = Convert.ToInt32(rentalFee), ServiceFee = serviceFee == null ? 0 : serviceFee.svFee }; rental.RentalFee = Convert.ToInt32(rentalFee); rental.ServiceFee = serviceFee == null ? 0 : serviceFee.svFee; _context.SaveChanges(); return(Ok(rentalDto)); }