public async Task <IActionResult> CheckoutAsync(TransactionCheckoutViewModel model) { //get the lease Lease lease = await _leaseRepository.GetLeaseById(model.LeaseId); RentalAsset rentalAsset = await _rentalAssetRepository.GetItemByIdAsync(lease.RentalAssetId); string transactionType; if (lease.UserId == model.ServerId && model.TransactionType != null) { transactionType = model.TransactionType; } else { transactionType = "Cash"; } var totalDays = (lease.leaseTo - lease.leaseFrom).TotalDays; decimal transactionTotal = rentalAsset.Price * (decimal)totalDays; if (ModelState.IsValid) { var transaction = new Transaction { TransactionTotal = transactionTotal, ServerId = model.ServerId, TransactionDate = DateTime.Now, TransactionNotes = model.TransactionNotes, TransactionType = transactionType, LeaseId = lease.LeaseId, VendorUserId = lease.UserId, }; var ActiveLease = new ActiveLease { RentalAssetId = rentalAsset.RentalAssetId, LeaseId = lease.LeaseId }; var result = await _transactionRepository.CreateTransactionAsync(transaction); //success if (result > 0) { await _rentalAssetRepository.BookAsset(lease.leaseTo, rentalAsset.RentalAssetId); await _activeLeaseRepository.AddActiveLeaseAsync(ActiveLease); await _leaseRepository.RemoveUnPaidLeases(); } return(RedirectToAction("CheckoutComplete")); } return(View(model)); }
public async Task <IActionResult> EndBooking(int id) { RentalAsset rentalAsset = await _rentalAssetRepository.GetItemByIdAsync(id); ActiveLease activeLease = _activeLeaseRepository.GetActiveLeaseByAssetId(rentalAsset.RentalAssetId); Lease lease = await _leaseRepository.GetLeaseById(activeLease.LeaseId); ApplicationUser user = await _userManager.FindByIdAsync(lease.UserId); if (rentalAsset == null) { ViewBag.ErrorMessage = $"The rental asset id={id} cannot be found"; return(View("NotFound")); } else { try { if (rentalAsset.BookTillDate < DateTime.Now) { // Calculate amount paid var totalDays = (lease.leaseTo - lease.leaseFrom).TotalDays; decimal AmountPaid = rentalAsset.Price * (decimal)totalDays; //Add invoice var invoice = new Invoice { RentalAssetId = rentalAsset.RentalAssetId, LeaseFrom = lease.leaseFrom, LeaseTo = lease.leaseTo, ApplicationId = user.Id, AmountPaid = AmountPaid }; await _invoiceRepository.AddInvoice(invoice); } await _rentalAssetRepository.EndBooking(id); await _activeLeaseRepository.RemoveLease(activeLease); return(RedirectToAction("BookedList")); } catch (Exception ex) { return(NotFound(ex.Message)); } } }
public async Task RemoveLease(ActiveLease activeLease) { _appDbContext.ActiveLeases.Remove(activeLease); await _appDbContext.SaveChangesAsync(); }
public async Task AddActiveLeaseAsync(ActiveLease activeLease) { await _appDbContext.AddAsync(activeLease); await _appDbContext.SaveChangesAsync(); }