public IHttpActionResult UpdateWaiveFee(BookingInfo booking)
 {
     var invoice = new InvoiceHandler(Services).UpdateWaiveFee(booking.BookingId, CurrentUser.Id);
     return Json(new { Status = invoice.IsSuccess, Messsage = invoice.Messsage });
 }
        public IHttpActionResult ViewInvoice(BookingInfo booking)
        {
            var invoices = Services.Invoices.GetInvoiceByBookingId(booking.BookingId);

            InvoiceDto customerInvoice = invoices.FirstOrDefault(x => x.User.Id == CurrentUser.Id);

            if (customerInvoice == null)
            {
                Log.Info("IHttpActionResult ViewInvoice(BookingInfo booking)-- "
                    + "Invoice is null");
                return Json(new { Status = false });
            }
            if (!customerInvoice.User.Role.Equals(Role.Customer))
            {
                Log.Info("IHttpActionResult ViewInvoice(BookingInfo booking)-- "
                    + "User is not a Customer");
                return Json(new { Status = false });
            }

            //use invoice waive fee to get invoice with template walve fee
            if (customerInvoice.IsWaiveFee)
            {
                var tempInvoice = invoices.FirstOrDefault(x => x.Type == InvoiceType.WaiveFee);
                if (tempInvoice != null)
                {
                    customerInvoice = tempInvoice;
                }
            }
            string content = Services.Invoices.TranformToInvoiceTemplate(customerInvoice.User, customerInvoice, null, ConstEmailTemplateKey.ConsultationInvoiceTemplate, invoices);
            //        customerInvoice, null, ConstEmailTemplateKey.ConsultationInvoiceTemplate, waiveFee);
            //if (customerInvoice.ConsultationType == ConsultationType.MinimumCharge)
            //{
            //    content = Services.Invoices.TranformToInvoiceTemplate(customerInvoice.User,
            //        customerInvoice, null, ConstEmailTemplateKey.ConsultationInvoiceTemplate, waiveFee);
            //}
            //else
            //{
            //    content = Services.Invoices.TranformToInvoiceTemplate(customerInvoice.User,
            //        customerInvoice, null, ConstEmailTemplateKey.ConsultationInvoiceTemplate, waiveFee);
            //}

            byte[] pdfAsByte = HtmlToPdf.GetPdfData(content, AppSettings.BaseUrl);

            string fileName = Services.Invoices.GenerateInvoiceName(customerInvoice.InvoiceNumber);

            Stream pdfStream = new MemoryStream(pdfAsByte);

            var url = FileHelper.UploadFileToS3(Services, fileName, pdfStream, "Temp_Invoice");

            if (!string.IsNullOrWhiteSpace(url))
            {
                return Json(new { status = true, url = S3ReaderHelper.CombineFileS3Root(url) });
            }
            return Json(new { Status = false });
        }
        public BookingApi GetBookingInfo(BookingInfo model)
        {
            List<InvoiceDto> invoices = Services.Invoices.GetInvoiceByBookingId(model.BookingId);
            InvoiceDto customerInvoice = new InvoiceDto();
            InvoiceDto specialistInvoice = new InvoiceDto();
            BookingDto booking = new BookingDto();

            if (invoices == null || !invoices.Any())
            {
                booking = Services.Booking.GetById(model.BookingId);
            }
            else
            {
                booking = invoices.First(i => i.Booking.Id == model.BookingId).Booking;
                customerInvoice = invoices.First(i => i.User.Id == booking.Customer.Id && i.Type == InvoiceType.Consultation);
                specialistInvoice = invoices.First(i => i.User.Id == booking.Specialist.Id);
            }

            var bookingInfo = Mapper.Map<BookingApi>(booking);

            bookingInfo.IsMinCharge = false;

            if (bookingInfo != null)
            {
                var call = Services.Call.GetByBookingIdAndConference(bookingInfo.Id);

                var specialist = booking.Specialist;
                var customer = booking.Customer;

                var s3Root = BlueChilli.Web.Helpers.S3PathWithoutQuery("").Substring(1);
                if (!string.IsNullOrWhiteSpace(bookingInfo.DocumentPath))
                {
                    bookingInfo.DocumentPath = Path.Combine(s3Root, bookingInfo.DocumentPath);
                }

                bookingInfo.IsFavourite = Services.Users.IsFavourite(specialist.Id, customer.Id);
                bookingInfo.SpecialistStatus = specialist.CurrentAvailabilityStatus;

                //check specilist is calling with customer to change curremt status to "not avaiable"
                if (!AvailabilityStatus.NotAvailable.Equals(bookingInfo.SpecialistStatus)
                    && Services.Call.CheckCallInProgressBySpecialistId(specialist.Id))
                {
                    bookingInfo.SpecialistStatus = AvailabilityStatus.NotAvailable;
                }

                // Convert to client time
                //TRI:TODO
                //bookingInfo.StartTime = Utilities.ConvertToClientTimeById(bookingInfo.CreatedDate, timeZoneId);
                //bookingInfo.EndTime = Utilities.ConvertToClientTimeById(bookingInfo.EndTime, timeZoneId);
                //bookingInfo.CreatedDate = Utilities.ConvertToClientTimeById(bookingInfo.CreatedDate, timeZoneId);

                if (call != null)
                {
                    if (specialist != null && customer != null)
                    {
                        bookingInfo.IsFeedback = Services.Call.CheckExistFeedbackAndRatingByCallId(call.Id);
                        string recordUrl = "";
                        if (!string.IsNullOrWhiteSpace(call.RecordSid))
                        {
                            var twilioServer = System.Configuration.ConfigurationManager.AppSettings["Twilio_ApiServer"];

                            recordUrl = !string.IsNullOrWhiteSpace(call.RecordUrl) && call.RecordUrl.Contains(twilioServer)
                                    ? Utilities.ConvertLinkRecord(call.RecordSid)
                                    : S3ReaderHelper.CombineFileS3Root(call.RecordUrl);
                        }

                        bookingInfo.Rating = (int)Math.Round(Services.Booking.GetRatingByBooking(bookingInfo.Id), 0, MidpointRounding.AwayFromZero);
                        bookingInfo.PastBooking = new PastBooking();
                        bookingInfo.PastBooking.CallId = call.Id;
                        bookingInfo.PastBooking.DurationMinute = call.Duration;
                        bookingInfo.PastBooking.CallStartTime = call.StartTime.HasValue ? (DateTime?)call.StartTime.Value.ConvertToCurrentTimeZone() : null;
                        bookingInfo.PastBooking.CallEndTime = call.EndTime.HasValue ? (DateTime?)call.EndTime.Value.ConvertToCurrentTimeZone() : null;
                        bookingInfo.PastBooking.RecordUrl = recordUrl;
                        bookingInfo.Feedback = call.Feedback;
                    }
                }

                if (customerInvoice != null && specialistInvoice != null)
                {
                    if (bookingInfo.PastBooking == null)
                    {
                        bookingInfo.PastBooking = new PastBooking();
                    }

                    if (ConsultationType.MinimumCharge.Equals(customerInvoice.ConsultationType))
                    {
                        bookingInfo.IsMinCharge = true;
                    }
                    bookingInfo.IsWaiveFee = customerInvoice.IsWaiveFee;
                    bookingInfo.InvoiceCreatedDate = customerInvoice.CreatedDate.ConvertToCurrentTimeZone();
                    bookingInfo.PastBooking.CustomerTotalRate = customerInvoice.Amount;
                    bookingInfo.PastBooking.SpecialistTotalRate = specialistInvoice.Amount;
                }
            }

            return bookingInfo;
        }