public RouteTicket GetRoute(int TicketId) { var routeTicket = _routeTicketRepository.Get(x => x.TicketId == TicketId & x.Deleted == false); //var routeTicketRowViewModel = _mapper.Map<RouteTicket, RouteTicketRowViewModel>(routeTicket); return(routeTicket); }
public List <AvailableTicketViewModel> GetTicketAvailableForRouteTicket(int routeTicketId) { var routeTicket = _routeTicketRepository.Get(x => x.Id == routeTicketId && x.Deleted == false ); if (routeTicket == null) { throw new NotFoundException(); } DateTime?departureFromDateUTC = null; DateTime?arrivalToDateUTC = null; int departureCityId = routeTicket.DepartureStation.CityId; int arrivalCityId = routeTicket.ArrivalStation.CityId; var previousRouteTicket = _routeTicketRepository.Get(x => x.RouteId == routeTicket.RouteId && x.Deleted == false && x.Order == routeTicket.Order - 1 ); var nextRouteTicket = _routeTicketRepository.Get(x => x.RouteId == routeTicket.RouteId && x.Deleted == false && x.Order == routeTicket.Order + 1 ); if (previousRouteTicket != null) { //TODO: Add waiting time amount departureFromDateUTC = previousRouteTicket.Ticket.ArrivalDateTimeUTC; } if (nextRouteTicket != null) { //TODO: Add waiting time amount arrivalToDateUTC = nextRouteTicket.Ticket.DepartureDateTimeUTC; } //Get Tickets base on fromDate and toDate var tickets = _ticketRepository.GetAllQueryable() .Where(x => x.Deleted == false && x.SellerId != routeTicket.Ticket.BuyerId && x.Status == Core.Enum.TicketStatus.Valid && ((departureFromDateUTC == null && x.DepartureDateTimeUTC > DateTime.UtcNow) || x.DepartureDateTimeUTC >= departureFromDateUTC) && (arrivalToDateUTC == null || x.ArrivalDateTimeUTC <= arrivalToDateUTC) && x.DepartureStation.CityId == departureCityId && x.ArrivalStation.CityId == arrivalCityId && x.Id != routeTicket.TicketId && x.ExpiredDateTimeUTC > DateTime.UtcNow ); var result = _mapper.Map <List <Ticket>, List <AvailableTicketViewModel> >(tickets.ToList()); return(result); }
//Replacement ticket public void SendEmailReplacementForBuyer(int oldTicketId, int replacementTicketId) { try { var apiKey = SETTING.Value.SendGridKey; var client = new SendGridClient(apiKey); string emailTemplateHtml = _hostingEnvironment.ContentRootPath + "\\EmailTemplate\\EmailTemplateReplacement.html"; string ticketTemplatehtml = _hostingEnvironment.ContentRootPath + "\\EmailTemplate\\TicketsTemplate.html"; string body = string.Empty; string oldTicketRow = string.Empty; string replacementTicketRow = string.Empty; var oldTicket = _ticketRepository.Get(o => o.Id == oldTicketId); var replacementTicket = _ticketRepository.Get(r => r.Id == replacementTicketId); var customer = _customerRepository.Get(c => c.Id == replacementTicket.BuyerId); //using streamreader for reading my htmltemplate using (StreamReader reader = new StreamReader(emailTemplateHtml)) { body = reader.ReadToEnd(); } var customerEmail = customer.Email; var customerName = customer.FullName; var customerPhone = customer.PhoneNumber; var oldTicketCode = oldTicket.TicketCode; var replacementTicketCode = replacementTicket.TicketCode; var routeId = _routeTicketRepository.Get(r => r.TicketId == replacementTicket.Id).RouteId; var routeCode = _routeRepository.Get(r => r.Id == routeId).Code; var oldTotalAmount = oldTicket.SellingPrice; var replacementTotalAmount = replacementTicket.SellingPrice; var totalAmount = oldTotalAmount - replacementTotalAmount; var date = DateTime.UtcNow; var oldTicketRowViewModel = _mapper.Map <Ticket, TicketRowViewModel>(oldTicket); var replacementTicketRowViewModel = _mapper.Map <Ticket, TicketRowViewModel>(replacementTicket); //replacing the required things body = body.Replace("{oldTicketCode}", oldTicketCode); body = body.Replace("{replaceTicketCode}", replacementTicketCode); body = body.Replace("{customerName}", customerName); body = body.Replace("{customerEmail}", customerEmail); body = body.Replace("{customerPhone}", customerPhone); body = body.Replace("{Date}", date.ToString()); body = body.Replace("{oldSubTotal}", oldTotalAmount.ToString()); body = body.Replace("{replaceSubTotal}", replacementTotalAmount.ToString()); body = body.Replace("{Total}", totalAmount.ToString()); body = body.Replace("{Term}", SETTING.Value.Term); body = body.Replace("{Title}", SETTING.Value.ReplacementTitle); body = body.Replace("{fromName}", SETTING.Value.FromName); body = body.Replace("{fromEmail}", SETTING.Value.FromEmail); body = body.Replace("{Street}", SETTING.Value.Street); body = body.Replace("{City}", SETTING.Value.City); body = body.Replace("{addressNumber}", SETTING.Value.AddressNumber); body = body.Replace("{phoneNumber}", SETTING.Value.PhoneNumber); body = body.Replace("{bussinessNumber}", SETTING.Value.BussinessNumber); //Old ticket using (StreamReader reader = new StreamReader(ticketTemplatehtml)) { oldTicketRow = reader.ReadToEnd(); var departureStation = _stationRepository.Get(s => s.Id == oldTicketRowViewModel.DepartureStationId).Name; var arrivalStation = _stationRepository.Get(s => s.Id == oldTicketRowViewModel.ArrivalStationId).Name; oldTicketRow = oldTicketRow.Replace("{ticketCode}", oldTicketRowViewModel.TicketCode); oldTicketRow = oldTicketRow.Replace("{Description}", oldTicketRowViewModel.Description); oldTicketRow = oldTicketRow.Replace("{departureCity}", oldTicketRowViewModel.DepartureCity); oldTicketRow = oldTicketRow.Replace("{departureStation}", departureStation); oldTicketRow = oldTicketRow.Replace("{departureTime}", oldTicketRowViewModel.DepartureDateTime.ToString()); oldTicketRow = oldTicketRow.Replace("{arrivalCity}", oldTicketRowViewModel.ArrivalCity); oldTicketRow = oldTicketRow.Replace("{arrivalStation}", arrivalStation); oldTicketRow = oldTicketRow.Replace("{arrivalTime}", oldTicketRowViewModel.ArrivalDateTime.ToString()); oldTicketRow = oldTicketRow.Replace("{Amount}", oldTicketRowViewModel.SellingPrice.ToString()); } //Replace ticket using (StreamReader reader = new StreamReader(ticketTemplatehtml)) { replacementTicketRow = reader.ReadToEnd(); var departureStation = _stationRepository.Get(s => s.Id == replacementTicketRowViewModel.DepartureStationId).Name; var arrivalStation = _stationRepository.Get(s => s.Id == replacementTicketRowViewModel.ArrivalStationId).Name; replacementTicketRow = replacementTicketRow.Replace("{ticketCode}", replacementTicketRowViewModel.TicketCode); replacementTicketRow = replacementTicketRow.Replace("{Description}", replacementTicketRowViewModel.Description); replacementTicketRow = replacementTicketRow.Replace("{departureCity}", replacementTicketRowViewModel.DepartureCity); replacementTicketRow = replacementTicketRow.Replace("{departureStation}", departureStation); replacementTicketRow = replacementTicketRow.Replace("{departureTime}", replacementTicketRowViewModel.DepartureDateTime.ToString()); replacementTicketRow = replacementTicketRow.Replace("{arrivalCity}", replacementTicketRowViewModel.ArrivalCity); replacementTicketRow = replacementTicketRow.Replace("{arrivalStation}", arrivalStation); replacementTicketRow = replacementTicketRow.Replace("{arrivalTime}", replacementTicketRowViewModel.ArrivalDateTime.ToString()); replacementTicketRow = replacementTicketRow.Replace("{Amount}", replacementTicketRowViewModel.SellingPrice.ToString()); } body = body.Replace("{oldTicket}", oldTicketRow); body = body.Replace("{replaceTicket}", replacementTicketRow); var from = new EmailAddress(SETTING.Value.FromEmail, SETTING.Value.FromName); var subject = routeCode + " - Receipt - Replacement"; var to = new EmailAddress(customerEmail, customerName); var plainTextContent = ""; var htmlContent = body; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); client.SendEmailAsync(msg); } catch (Exception) { } }
public string RefundToTalMoneyToCustomer(int TicketId, ResolveOption resolveOption, string username) { string staffId = _userRepository.Get(x => x.UserName == username).Id; // lấy routeTicket ứng vs ticketId var routeTicket = _routeTicketRepository.Get(x => x.TicketId == TicketId & x.Deleted == false & x.Route.Status == RouteStatus.Bought ); _unitOfWork.StartTransaction(); var route = routeTicket.Route; if (route.Status == RouteStatus.Completed) { throw new InvalidOperationException(); } route.Status = RouteStatus.Completed; route.IsRefundAll = true; //route.ResolveOption = resolveOption; _routeRepository.Update(route); _unitOfWork.CommitChanges(); //lấy Lịch sử chagre tiền var paymentDetail = _paymentRepository.Get(x => x.RouteId == routeTicket.RouteId && x.Route.Deleted == false); var refundFromPayments = _refundRepository.GetAllQueryable().Where(x => x.PaymentId == paymentDetail.Id); decimal totalRefund = 0; foreach (var refundFromPayment in refundFromPayments) { totalRefund = totalRefund + refundFromPayment.Amount; } //refund lại tiền StripeConfiguration.SetApiKey(SETTING.Value.SecretStripe); //cmt để test decimal remainRefund = paymentDetail.Amount - totalRefund; var refundOptions = new RefundCreateOptions() { ChargeId = paymentDetail.StripeChargeId, Amount = Convert.ToInt64(remainRefund * 100) }; var refundService = new Stripe.RefundService(); Stripe.Refund refund = refundService.Create(refundOptions); RefundCreateViewModel refundCreate = new RefundCreateViewModel(); refundCreate.PaymentId = paymentDetail.Id; refundCreate.StripeRefundId = refund.Id; refundCreate.Amount = remainRefund; //refund all refundCreate.Description = "You have a refund for route " + routeTicket.Route.Code + ". We are sorry for the inconvenience."; refundCreate.Status = RefundStatus.Success; var refundAddIntoData = _mapper.Map <RefundCreateViewModel, Core.Models.Refund>(refundCreate); _refundRepository.Add(refundAddIntoData); //refund total amount //save log var routeTickets = route.RouteTickets.Where(x => x.Deleted == false); foreach (var rt in routeTickets) { var resolveOptionLog = _resolveOptionLogRepository.Get(x => x.RouteId == rt.RouteId && x.TicketId == rt.TicketId); if (resolveOptionLog == null || resolveOptionLog.Option == ResolveOption.PAYOUT) { ResolveOptionLog newLog = new ResolveOptionLog() { RouteId = rt.RouteId, TicketId = rt.TicketId, StaffId = staffId, Option = ResolveOption.REFUNDTOTALAMOUNT, Amount = rt.Ticket.SellingPrice }; _resolveOptionLogRepository.Add(newLog); } } _unitOfWork.CommitTransaction(); //save log //push noti to seller bought ticket foreach (var boughtTicket in routeTicket.Route.RouteTickets) { var ticket = boughtTicket.Ticket; if (ticket.Status == TicketStatus.Bought) { ticket.Status = TicketStatus.Valid; //ticket.BuyerId = null; _ticketRepository.Update(ticket); var message = "Buyer has canceled the transaction, ticket " + ticket.TicketCode + " is valid again."; List <string> sellerTicketDeviceIds = GetCustomerDeviceIds(ticket, true); _oneSignalService.PushNotificationCustomer(message, sellerTicketDeviceIds); //Save notification _notificationService.SaveNotification( customerId: ticket.SellerId, type: NotificationType.TicketIsRevalid, message: $"Your Ticket {ticket.TicketCode} is valid again due to buyer has canceled the transaction.", data: new { ticketId = ticket.Id } ); } } //push notification for buyer to notify his payment is refunded var messageRoute = "Route " + route.Code + " has been refunded. " + remainRefund.ToString("N2") + "$ will be refunded within next 5 to 7 working days."; List <string> buyerDeviceIds = GetCustomerDeviceIds(routeTicket.Ticket, false); _oneSignalService.PushNotificationCustomer(messageRoute, buyerDeviceIds); //Save notification for buyer if (routeTicket.Ticket.BuyerId != null) { _notificationService.SaveNotification( customerId: routeTicket.Ticket.BuyerId.Value, type: NotificationType.RouteIsRefunded, message: $"Route {route.Code} has been refunded, {remainRefund.ToString("N2")} will be refunded within next 5 to 7 working days.", data: new { routeId = route.Id }); } _sendGridService.SendEmailRefundForBuyerAllTicket(routeTicket.RouteId, remainRefund); return(""); }