Beispiel #1
0
        public string SendVoucher(int bookingId, IEnumerable<string> emails)
        {
            try
            {
                using (var db = new LomsContext())
                {
                    string[] bookingChildren3 = new[]
                    {
                        "Creator",
                        "City", "City.Country", "City.State", "City.State.Country", 
                        "PickUpEndpoint", "PickUpEndpoint.Airport", "PickUpEndpoint.Airline",
                        "PickUpEndpoint.Suburb", "PickUpEndpoint.Suburb.Country", "PickUpEndpoint.Suburb.State", "PickUpEndpoint.Suburb.State.Country",
                        "DropOffEndpoint", "DropOffEndpoint.Airport", "DropOffEndpoint.Airline",
                        "DropOffEndpoint.Suburb", "DropOffEndpoint.Suburb.Country", "DropOffEndpoint.Suburb.State", "DropOffEndpoint.Suburb.State.Country",
                        "Stops",
                        "VehicleType",
                        //"FareInfo", "FareInfo.Currency", "FareInfo.Items",
                        "PrimaryPassenger",
                        "PassengerInfo","PassengerInfo.Adults","PassengerInfo.Childs","PassengerInfo.Infants",
                        "ProvisionInfo",
                         "TransactionCreditCards", "TransactionCreditCards.Address", "TransactionCreditCards.Info"
                     //"PaymentInfo.CreditCard", "PaymentInfo.CreditCard.Info",
                        //"PaymentInfo.BillingAccount", "PaymentInfo.BillingAccount.ChargeBack",  "PaymentInfo.BillingAccount.ChargeBack.Country",
                        //"PaymentInfo.Items"
                    };
                    string[] bookingPaymentInfoChildren = new[]
                    {
                        "CreditCard", "CreditCard.Info",
                        "BillingAccount", "BillingAccount.ChargeBack",  "BillingAccount.ChargeBack.Country",
                        "Items"
                    };
                    var booking = db.Bookings.IncludeAll(bookingChildren3).FirstOrDefault(b => b.Id == bookingId);
                    booking.PaymentInfo = db.BookingPayments.IncludeAll(bookingPaymentInfoChildren).FirstOrDefault(b => b.BookingId == bookingId);

                    var primaryPassenger = booking.PrimaryPassenger != null ? booking.PrimaryPassenger : booking.Creator;

                    booking.ChargeInfo = db.BookingChargeInfoes.Include("Items").SingleOrDefault(b => b.BookingId == bookingId);
                    booking.TransactionBillingAccount = db.BookingTransactionBillingAccounts.Include("ChargeBack").SingleOrDefault(b => b.BillingAccountId == bookingId);

                    booking.TransactionCreditCards.ForEach(i =>
                    {
                        i.Number = AssociationUserCreditCard.ObfuscateCreditCardNumber(i.Info.Number);
                        i.Info = null;
                        i.AcceptChanges();
                    });

                    BookingDispatching dispatch = db.BookingDispatchings.FirstOrDefault(b => b.BookingId == bookingId);
                    if (dispatch == null) dispatch = new BookingDispatching();
                    dispatch.Booking = booking;


                    if (booking.Creator.IsTravelAgency)
                    {
                        booking.Creator.TravelAgencyRole = db.AssociationUserTravelAgencyRoles.IncludeAll("TravelAgency").SingleOrDefault(r => r.UserId == booking.Creator.Id && r.StatusId == (int)TravelAgencyStatus.Accepted);
                        if (booking.Creator.TravelAgencyRole != null)
                        {
                            booking.Creator.TravelAgencyName = booking.Creator.TravelAgencyRole.TravelAgency.Name;
                            booking.Creator.MarkAsUnchanged();
                        }
                    }

                    var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId);
                    if (emailProvider != null)
                    {
                        var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId);

                        var uri = HttpContext.Current.Request.Url;
                        string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                        string contactUsLink = Path.Combine(baseUrl + "/#Contact");

                        EmailTemplate emailTemplate = new EmailTemplate("ConfirmationVoucher");

                        emailTemplate["UserName"] = primaryPassenger.FullName.ToUpper();
                        emailTemplate["AssociationName"] = association.Name.ToUpper();
                        emailTemplate["BookingId"] = booking.ReferenceId;

                        var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html);

                        string email = null;
                        bool first = true;
                        foreach (string item in emails)
                        {
                            if (!first)
                                email += ",";
                            email += item;
                            first = false;
                        }

                        ////generate voucher
                        //var document = BookingVoucher.Generate(booking);
                        ////MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");
                        //PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
                        //renderer.Document = document;
                        //renderer.RenderDocument();

                        //MemoryStream stream = new MemoryStream();
                        //renderer.PdfDocument.Save(stream, false);
                        CommonBookingService commonService = new CommonBookingService();

                        AirportMeetingPoint meetingPoint = commonService.GetMeetingPoint(db, booking);
                        MemoryStream stream = commonService.GeneratePDFVoucher(dispatch, association, meetingPoint);
                        Attachment attach = new Attachment(stream, booking.ReferenceId + ".pdf", "application/pdf");
                        emailProvider.SendMail(email, "Itinerary for " + primaryPassenger.LastName, emailTemplate.Txt, attach, avBody, true);
                    }

                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }

            return null;
        }
        public EntityResponse<AssociationUser> CreateManagedUser(AssociationUser user)
        {
            try
            {
                var email = user.Email.ToLower().Trim();

                using (var scope = new TransactionScope())
                using (var db = new LomsContext())
                {
                    if (!string.IsNullOrWhiteSpace(email))
                    {
                        //check id user with such email existed already
                        var existedUser = (from u in db.AssociationUsers
                                           where u.AssociationId == CurrentAssociationId && u.Email == email
                                           select u).SingleOrDefault();

                        if (existedUser != null)
                            return new EntityResponse<AssociationUser>("User with such email is already registered!");
                        user.Email = email;
                    }
                    else
                        user.Email = null;


                    user.AssociationId = CurrentAssociationId;
                    user.CreatedTime = user.LastUpdatedTime = DateTime.UtcNow;

                    var manager = db.AssociationUsers.FirstOrDefault(u => u.Id == user.ManagerId);
                    if (manager.IsTravelAgency)
                    {
                        user.IsTravelAgency = true;
                        user.IataNumber = manager.IataNumber;
                    }

                    db.AssociationUsers.ApplyChanges(user);
                    db.SaveChanges();

                    AssociationUserActivation activation = new AssociationUserActivation();
                    activation.UserId = user.Id;
                    activation.Guid = Guid.NewGuid();
                    activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0);  //expiry
                    db.AssociationUserActivations.ApplyChanges(activation);
                    db.SaveChanges();

                    var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId);
                    if (emailProvider != null)
                    {
                        var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId);

                        var uri = HttpContext.Current.Request.Url;
                        string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                        string activtionLink = Path.Combine(baseUrl + string.Format("/#Activation/{0}", activation.Guid.ToString("D")));
                        string contactUsLink = Path.Combine(baseUrl + "/#Contact");
                        string termAndConditionsLink = Path.Combine(baseUrl + "/terms");

                        EmailTemplate emailTemplate;
                        if (manager.IsTravelAgency)
                        {
                            emailTemplate = new EmailTemplate("NewProfileByTravelAgent");

                            var travelAgencyName = (from a in db.AssociationUserAddresses
                                                    where a.AssociationUserId == manager.Id && a.TypeId == (int)AddressType.Work
                                                    select a.BusinessName).SingleOrDefault();

                            emailTemplate["TravelAgencyName"] = travelAgencyName.ToUpper();
                        }
                        else
                            emailTemplate = new EmailTemplate("NewProfileByGeneralUser");

                        emailTemplate["UserName"] = user.FullName.ToUpper();
                        emailTemplate["AssociationName"] = association.Name.ToUpper();

                        emailTemplate["ManagerName"] = manager.FullName.ToUpper();
                        emailTemplate["ManagerEmail"] = manager.Email.ToLower();

                        emailTemplate["ActivationLink"] = activtionLink;
                        emailTemplate["ContactUsLink"] = contactUsLink;
                        emailTemplate["TermAndConditionLink"] = termAndConditionsLink;

                        var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html);
                        emailProvider.SendMail(user.Email, association.Name + " Account activation", emailTemplate.Txt, null, avBody, true);
                    }

                    scope.Complete();

                }

                using (var db = new LomsContext())
                {
                    var entity = db.AssociationUsers.IncludeAll("Country").Single(u => u.Id == user.Id);
                    return new EntityResponse<AssociationUser>() { Entity = entity };
                }
            }
            catch (Exception ex)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(ex.Message);
                if (ex.InnerException != null)
                {
                    builder.AppendLine(ex.InnerException.Message);
                    if (ex.InnerException.InnerException != null)
                        builder.AppendLine(ex.InnerException.InnerException.Message);
                }
                return new EntityResponse<AssociationUser>(builder.ToString());
            }
        }