public IHttpActionResult Save([FromBody] RequestQuotationViewModel model)
        {
            try
            {
                if (model == null)
                {
                    return(NotFound());
                }
                var quotation = new RequestQuotation
                {
                    Id = model.Id,
                    ServiceRequestId    = model.ServiceRequestId,
                    AgentId             = model.AgentId,
                    QuotationTemplateId = model.QuotationTemplateId,
                    QuotationText       = model.QuotationText,
                    Premimum            = Convert.ToDecimal(model.Premimum.Replace(",", String.Empty)),
                    Status = (int)Constant.QuotationStatus.Initial,
                    Cover  = Convert.ToDecimal(model.Cover.Replace(",", String.Empty))
                };

                using (AppDBContext context = new AppDBContext())
                {
                    long quoteId = new RequestQuotationRepository(context).Save(quotation);
                    new AgentServiceRequestRepository(context).UpdateResponseTime(model.ServiceRequestId, model.AgentId);
                    var userRepo     = new UserRepository(context);
                    var request      = new ServiceRequestRepository(context).GetById(model.ServiceRequestId);
                    var agentProfile = new UserProfileRepository(context).GetByUserId(model.AgentId);
                    var agent        = userRepo.GetByUserId(model.AgentId);
                    var agentName    = agent.Name;
                    var company      = agent.Company.Name;
                    if (agentProfile != null)
                    {
                        agentName = agentProfile.FirstName + " " + agentProfile.LastName;
                    }

                    MessageModel message = new MessageModel
                    {
                        MessageText = "Quotation Sent by: " + agentName + "\n" + company,
                        RequestId   = model.ServiceRequestId,
                        SenderId    = model.AgentId,
                        RecieverId  = request.UserId,
                        QuotationId = quoteId
                    };
                    AddMessage(message, context);
                    new NotificationRepository(context).Add(
                        message.RecieverId,
                        (int)Constant.NotificationType.Quotation,
                        message.QuotationId,
                        ConfigurationHelper.NOTIFICATION_TITLE,
                        Constant.Notification.NEW_QUOTATION_TEXT);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(QuotationController), ex.Message + ex.StackTrace, LogType.ERROR);
                return(InternalServerError());
            }

            return(Ok());
        }
        public IHttpActionResult GetByIdFromBuyer(long Id)
        {
            RequestQuotationViewModel model = null;

            try
            {
                using (AppDBContext context = new AppDBContext())
                {
                    var quote = new RequestQuotationRepository(context).GetById(Id);
                    if (quote != null)
                    {
                        model = new RequestQuotationViewModel
                        {
                            Id = quote.Id,
                            ServiceRequestId    = quote.ServiceRequestId,
                            QuotationTemplateId = quote.QuotationTemplateId,
                            Premimum            = quote.Premimum?.ToString("#,##0.00"),
                            Cover                 = quote.Cover?.ToString("#,##0.00"),
                            AgentId               = quote.AgentId,
                            AgentName             = quote.Agent?.Name,
                            AgentContact          = quote.Agent?.UserName,
                            CompanyId             = quote.Agent?.CompanyId ?? 0,
                            CompanyName           = quote.Agent?.Company?.Name,
                            QuotationTemplateName = quote.QuotationTemplate.Name,
                            QuotationText         = quote.QuotationText,
                            Status                = quote.Status ?? 0,
                            ServiceRequestCode    = quote.ServiceRequest.Code,
                            ClaimType             = quote.ServiceRequest.ClaimType,
                            VehicleNo             = quote.ServiceRequest.VehicleNo,
                            VehicleValue          = quote.ServiceRequest.VehicleValue,
                            IsExpired             = quote.IsExpired ?? false
                        };

                        if (quote.ServiceRequest.Status == (int)Constant.ServiceRequestStatus.Closed ||
                            quote.ServiceRequest.Status == (int)Constant.ServiceRequestStatus.Expired)
                        {
                            model.Status = (int)Constant.QuotationStatus.Closed;
                        }

                        var messageThread = new MessageThreadRepository(context).GetByAgentAndRequest(model.AgentId, model.ServiceRequestId);
                        if (messageThread != null)
                        {
                            model.ThreadId = messageThread.Id;
                        }

                        new RequestQuotationRepository(context).UpdateToChecked(quote.Id);
                    }
                }
                return(Ok(model));
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(QuotationController), ex.Message + ex.StackTrace, LogType.ERROR);
                return(InternalServerError());
            }
        }
        public IHttpActionResult Accept(long QuotationId)
        {
            try
            {
                using (AppDBContext context = new AppDBContext())
                {
                    var repo  = new RequestQuotationRepository(context);
                    var quote = repo.GetById(QuotationId);
                    quote.Status = (int)Constant.QuotationStatus.Accepted;
                    repo.Update(quote);
                    var otherQuotes = repo.GetByRequest(quote.ServiceRequestId)?.Where(q => q.Id != quote.Id);
                    if (otherQuotes != null)
                    {
                        foreach (var q in otherQuotes)
                        {
                            q.Status = (int)Constant.QuotationStatus.Closed;
                            repo.Update(q);
                        }
                    }

                    var reqRepo   = new ServiceRequestRepository(context);
                    var userRepo  = new UserRepository(context);
                    var request   = quote.ServiceRequest;
                    var buyerName = userRepo.GetName(request.UserId);
                    reqRepo.UpdateSRStatus(request.Id, (int)Constant.ServiceRequestStatus.Closed);

                    MessageModel message = new MessageModel
                    {
                        MessageText = "Quotation accepted by: " + buyerName,
                        RequestId   = request.Id,
                        SenderId    = request.UserId,
                        RecieverId  = quote.AgentId,
                        QuotationId = 0
                    };
                    AddMessage(message, context);

                    new NotificationRepository(context).Add(
                        quote.AgentId,
                        (int)Constant.NotificationType.Accept,
                        quote.Id,
                        ConfigurationHelper.NOTIFICATION_TITLE,
                        Constant.Notification.ACCCEPTED_TEXT);
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(QuotationController), ex.Message + ex.StackTrace, LogType.ERROR);
                return(InternalServerError());
            }
        }
        private List <RequestQuotationViewModel> GetServiceQuotations(long SRId)
        {
            List <RequestQuotationViewModel> models = null;
            List <RequestQuotation>          quotes = null;

            using (AppDBContext context = new AppDBContext())
            {
                quotes = new RequestQuotationRepository(context).GetByRequest(SRId);
                var index = quotes.FindIndex(q => (q.Status ?? 0) == (int)Common.Constant.QuotationStatus.Accepted);
                if (index > 0)
                {
                    quotes = quotes.Where(q => (q.Status ?? 0) == (int)Common.Constant.QuotationStatus.Accepted).Concat(
                        quotes.Where(q => (q.Status ?? 0) != (int)Common.Constant.QuotationStatus.Accepted)).ToList();
                }

                if (quotes != null)
                {
                    models = quotes.Select(q => new RequestQuotationViewModel
                    {
                        Id = q.Id,
                        ServiceRequestId    = q.ServiceRequestId,
                        QuotationTemplateId = q.QuotationTemplateId,
                        Premimum            = q.Premimum?.ToString("#,##0.00"),
                        Cover                 = q.Cover?.ToString("#,##0.00"),
                        AgentId               = q.AgentId,
                        AgentName             = q.Agent?.Name,
                        AgentContact          = q.Agent?.UserName,
                        QuotationTemplateName = q.QuotationTemplate.Name,
                        Status                = q.Status ?? 0,
                        CompanyId             = q.Agent.CompanyId ?? 0,
                        CompanyName           = q.Agent.Company?.Name,
                        QuotationText         = q.QuotationText
                    }).ToList();
                }
            }
            return(models);
        }
        public IHttpActionResult GetByQuoteId(long QuotationId, long?UserId)
        {
            ServiceRequest      request   = null;
            ServiceRequestModel model     = null;
            IUser            buyer        = null;
            IUserProfile     buyerProfile = null;
            RequestQuotation quote        = null;

            try
            {
                using (AppDBContext context = new AppDBContext())
                {
                    quote        = new RequestQuotationRepository(context).GetById(QuotationId);
                    request      = quote.ServiceRequest;
                    buyer        = new UserRepository(context).GetByUserId(request.UserId);
                    buyerProfile = new UserProfileRepository(context).GetByUserId(request.UserId);
                    if (UserId != null)
                    {
                        new AgentServiceRequestRepository(context).UpdateToPending(
                            request.Id, UserId ?? 0);
                    }
                }

                if (request != null)
                {
                    model = new ServiceRequestModel
                    {
                        Id                   = request.Id,
                        Code                 = request.Code,
                        InsuranceTypeId      = request.InsuranceTypeId,
                        UserId               = request.UserId,
                        CreatedDate          = request.TimeOccured.GetAdjustedTime().ToString("yyyy-MM-dd"),
                        ClaimType            = request.ClaimType,
                        UsageType            = request.UsageType,
                        RegistrationCategory = request.RegistrationCategory,
                        VehicleNo            = request.VehicleNo,
                        VehicleValue         = request.VehicleValue,
                        VehicleYear          = request.VehicleYear,
                        IsFinanced           = request.IsFinanced,
                        Status               = request.Status,
                        FuelType             = request.FuelType ?? 0,
                        Location             = request.Location == null ? String.Empty : request.Location,
                        ExpiryDate           = request.TimeOccured.GetAdjustedTime().AddDays(ConfigurationHelper.DAYS_TO_EXPIRE_REQUEST).ToString("yyyy-MM-dd"),
                        QuotationList        = GetServiceQuotations(request.Id)
                    };

                    if (buyer != null)
                    {
                        model.BuyerName    = buyer.Name;
                        model.BuyerMobile  = buyer.UserName;
                        model.IsAllowPhone = true;

                        if (buyerProfile != null)
                        {
                            model.BuyerName  = buyerProfile.FirstName + " " + buyerProfile.LastName;
                            model.BuyerPhone = buyerProfile.Phone;
                            if (String.IsNullOrEmpty(model.Location))
                            {
                                model.Location = buyerProfile.City;
                            }
                            if (buyerProfile.ContactMethod == (int)Constant.ContactMethod.Message)
                            {
                                model.IsAllowPhone = false;
                            }
                        }
                    }
                }
                return(Json(model));
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(ServiceRequestController), ex.Message + ex.StackTrace, LogType.ERROR);
                return(InternalServerError());
            }
        }