public IHttpActionResult AddOpportunity()
        {
            try
            {
                using (MaxMasterDbEntities db = new MaxMasterDbEntities())
                {
                    var form = HttpContext.Current.Request.Form;
                    var opportunityCategories = JsonConvert.DeserializeObject <List <keyValueModel> >(form.Get("Categories"));

                    Opportunity opp = new Opportunity();
                    opp.Client_Id   = form.Get("client");
                    opp.CreatedDate = MasterDataController.GetIndianTime(DateTime.UtcNow);
                    opp.CreatedBy   = User.Identity.GetUserId();

                    if (form.Get("assignTo") != null)
                    {
                        opp.TaskOwner  = form.Get("assignTo");
                        opp.AssignedTo = form.Get("assignTo");
                    }
                    else
                    {
                        opp.TaskOwner  = User.Identity.GetUserId();
                        opp.AssignedTo = User.Identity.GetUserId();
                    }

                    opp.OpportunityName = form.Get("opportunity");
                    opp.RefferedBy      = form.Get("refferedBy");
                    opp.Org_Id          = Convert.ToInt32(form.Get("orgId"));
                    opp.Status          = form.Get("Status");

                    opp.Description = form.Get("comments");
                    opp.Location_Id = Convert.ToInt32(form.Get("Location"));

                    if (opp.Status == "Accepted")
                    {
                        opp.EDOC = DateTime.Parse(form.Get("edoc"));
                        opp.EDOS = DateTime.Parse(form.Get("edos"));
                    }
                    else if (opp.Status == "Completed")
                    {
                        opp.ActualCompletedDate = Convert.ToDateTime(form.Get("CompletedDate"));
                    }
                    db.Opportunities.Add(opp);

                    for (int i = 0; i < opportunityCategories.Count; i++)
                    {
                        OpportunityCategoryMapping oppCat = new OpportunityCategoryMapping();
                        oppCat.Category_Id = Convert.ToInt32(opportunityCategories[i].value);

                        opp.OpportunityCategoryMappings.Add(oppCat);
                    }

                    if (form.Get("contactPersons") != null)
                    {
                        var checkedContacts = JsonConvert.DeserializeObject <List <string> >(form.Get("contactPersons"));
                        for (int j = 0; j < checkedContacts.Count; j++)
                        {
                            OpportunityContactMapping oppContact = new OpportunityContactMapping();
                            oppContact.Contact_Id = Convert.ToInt32(checkedContacts[j]);

                            opp.OpportunityContactMappings.Add(oppContact);
                        }
                    }

                    OpportunitiesLog oppLog = new OpportunitiesLog();

                    oppLog.CreatedBy   = User.Identity.GetUserId();
                    oppLog.CreatedDate = MasterDataController.GetIndianTime(DateTime.UtcNow);
                    oppLog.AssignedTo  = opp.TaskOwner;
                    oppLog.Status      = form.Get("Status");
                    oppLog.Comments    = form.Get("comments");

                    opp.OpportunitiesLogs.Add(oppLog);

                    var files           = HttpContext.Current.Request.Files;
                    var fileAttachments = new List <HttpPostedFile>();

                    for (int i = 0; i < files.Count; i++)
                    {
                        fileAttachments.Add(files[i]);
                    }
                    foreach (var file in fileAttachments)
                    {
                        var fileDirecory = HttpContext.Current.Server.MapPath("~/CommentAttachments");

                        if (!Directory.Exists(fileDirecory))
                        {
                            Directory.CreateDirectory(fileDirecory);
                        }

                        var fileName = file.FileName;
                        var filePath = Path.Combine(fileDirecory, fileName);
                        file.SaveAs(filePath);

                        OpportunityAttachment attachment = new OpportunityAttachment();

                        attachment.FileName       = Path.GetFileNameWithoutExtension(file.FileName);
                        attachment.FileAttachment = Path.Combine(ConfigurationManager.AppSettings["ApiUrl"], "CommentAttachments", fileName);
                        oppLog.OpportunityAttachments.Add(attachment);
                    }
                    db.Opportunities.Add(opp);
                    db.SaveChanges();
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace);
                return(Content(HttpStatusCode.InternalServerError, "An error occured , please try again later"));
            }
        }
        public IHttpActionResult EditOpportunity(int oppId)
        {
            try
            {
                using (MaxMasterDbEntities db = new MaxMasterDbEntities())
                {
                    var         form         = HttpContext.Current.Request.Form;
                    var         assingedTo   = form.Get("assignee");
                    var         status       = form.Get("status");
                    var         followupDate = form.Get("followupon");
                    Opportunity opportunity  = db.Opportunities.Where(x => x.Id == oppId).FirstOrDefault();

                    var userId = User.Identity.GetUserId();
                    //  int UserId = db.Employees.Where(x => x.AspNetUserId == userId).FirstOrDefault().Id;
                    opportunity.Status        = status;
                    opportunity.LastUpdated   = MasterDataController.GetIndianTime(DateTime.Now);
                    opportunity.LastUpdatedBy = User.Identity.GetUserId();
                    if (followupDate != "")
                    {
                        opportunity.FollowUpOn = Convert.ToDateTime(form.Get("followupon"));
                    }
                    OpportunitiesLog oppLog = new OpportunitiesLog();

                    if (assingedTo != null)
                    {
                        oppLog.AssignedTo     = (assingedTo);
                        opportunity.TaskOwner = (assingedTo);
                    }
                    else
                    {
                        oppLog.AssignedTo = opportunity.TaskOwner;
                    }

                    oppLog.CreatedDate   = MasterDataController.GetIndianTime(DateTime.UtcNow);
                    oppLog.OpportunityId = oppId;
                    oppLog.Status        = form.Get("status");
                    oppLog.CreatedBy     = User.Identity.GetUserId();
                    oppLog.Comments      = form.Get("Comments");

                    if (oppLog.Status == "Accepted")
                    {
                        opportunity.EDOC = DateTime.Parse(form.Get("edoc"));
                        opportunity.EDOS = DateTime.Parse(form.Get("edos"));
                    }
                    else if (oppLog.Status == "Completed")
                    {
                        opportunity.ActualCompletedDate = Convert.ToDateTime(form.Get("completedDate"));
                    }

                    var files           = HttpContext.Current.Request.Files;
                    var fileAttachments = new List <HttpPostedFile>();

                    if (files.Count > 0)
                    {
                        for (int i = 0; i < files.Count; i++)
                        {
                            fileAttachments.Add(files[i]);
                        }
                        foreach (var file in fileAttachments)
                        {
                            var fileDirecory = HttpContext.Current.Server.MapPath("~/CommentAttachments");

                            if (!Directory.Exists(fileDirecory))
                            {
                                Directory.CreateDirectory(fileDirecory);
                            }

                            var fileName = file.FileName;
                            var filePath = Path.Combine(fileDirecory, fileName);
                            file.SaveAs(filePath);

                            OpportunityAttachment attachment = new OpportunityAttachment();

                            attachment.FileName       = Path.GetFileNameWithoutExtension(file.FileName);
                            attachment.FileAttachment = Path.Combine(ConfigurationManager.AppSettings["ApiUrl"], "CommentAttachments", fileName);
                            oppLog.OpportunityAttachments.Add(attachment);
                        }
                    }

                    // opportunity.OpportunitiesLogs.Add(oppLog);
                    db.OpportunitiesLogs.Add(oppLog);
                    db.Entry(opportunity).State = System.Data.Entity.EntityState.Modified;

                    db.SaveChanges();
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace);
                return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later"));
            }
        }