Example #1
0
        public ActionResult SaveAjax(CaseSmartGoalServiceProvider casesmartgoalserviceprovider)
        {
            //id=0 means add operation, update operation otherwise
            bool isNew = casesmartgoalserviceprovider.ID == 0;



            //validate data
            if (ModelState.IsValid)
            {
                if (casesmartgoalserviceprovider.StartDate > casesmartgoalserviceprovider.EndDate)
                {
                    throw new CustomException("Start date can't be greater than end date.");
                }
                try
                {
                    //if (casesmartgoalserviceprovider.IsProposed)
                    //{
                    //    casesmartgoalserviceprovider.IsUsed = false;
                    //}
                    //else
                    //{
                    //    casesmartgoalserviceprovider.IsUsed = true;
                    //}
                    //set the id of the worker who has added/updated this record
                    //if (!isNew)
                    //{
                    //    var primaryWorkerID = GetPrimaryWorkerOfTheCase(casesmartgoalserviceprovider.CaseID);
                    //    if (casesmartgoalserviceprovider.CreatedByWorkerID != CurrentLoggedInWorker.ID && primaryWorkerID != CurrentLoggedInWorker.ID && CurrentLoggedInWorkerRoleIDs.IndexOf(1) == -1 && (CurrentLoggedInWorkerRoleIDs.IndexOf(SiteConfigurationReader.RegionalManagerRoleID) == -1))
                    //    {
                    //        WebHelper.CurrentSession.Content.ErrorMessage = "You are not eligible to do this action";
                    //        return Json(new { success = true, url = Url.Action(Constants.Actions.AccessDenied, Constants.Controllers.Home, new { Area = String.Empty }) });
                    //        //return RedirectToAction(Constants.Actions.AccessDenied, Constants.Controllers.Home, new { Area = String.Empty });
                    //    }
                    //}
                    casesmartgoalserviceprovider.LastUpdatedByWorkerID = CurrentLoggedInWorker.ID;
                    //call repository function to save the data in database
                    casesmartgoalserviceproviderRepository.InsertOrUpdate(casesmartgoalserviceprovider);
                    casesmartgoalserviceproviderRepository.Save();

                    //set status message
                    if (isNew)
                    {
                        casesmartgoalserviceprovider.SuccessMessage = "Service provider has been added successfully";
                    }
                    else
                    {
                        casesmartgoalserviceprovider.SuccessMessage = "Service provider has been updated successfully";
                    }
                }
                catch (DbUpdateException)
                {
                    casesmartgoalserviceprovider.ErrorMessage = "The selected service provider is already added to this goal";
                }
                catch (CustomException ex)
                {
                    casesmartgoalserviceprovider.ErrorMessage = ex.UserDefinedMessage;
                }
                catch (Exception ex)
                {
                    ExceptionManager.Manage(ex);
                    casesmartgoalserviceprovider.ErrorMessage = Constants.Messages.UnhandelledError;
                }
            }
            else
            {
                foreach (var modelStateValue in ViewData.ModelState.Values)
                {
                    foreach (var error in modelStateValue.Errors)
                    {
                        casesmartgoalserviceprovider.ErrorMessage = error.ErrorMessage;
                        break;
                    }
                    if (casesmartgoalserviceprovider.ErrorMessage.IsNotNullOrEmpty())
                    {
                        break;
                    }
                }
            }
            //return the status message in json
            if (casesmartgoalserviceprovider.ErrorMessage.IsNotNullOrEmpty())
            {
                return(Json(new { success = false, data = this.RenderPartialViewToString(Constants.PartialViews.Alert, casesmartgoalserviceprovider) }));
            }
            else
            {
                return(Json(new { success = true, data = this.RenderPartialViewToString(Constants.PartialViews.Alert, casesmartgoalserviceprovider) }));
            }
        }
Example #2
0
        /// <summary>
        /// Add or Update casesmartgoal to database
        /// </summary>
        /// <param name="casesmartgoal">data to save</param>
        public void InsertOrUpdate(CaseSmartGoal casesmartgoal, NameValueCollection data, bool endOnly = false)
        {
            bool isNew = false;

            casesmartgoal.LastUpdateDate = DateTime.Now;
            if (casesmartgoal.ID == default(int))
            {
                //set the date when this record was created
                casesmartgoal.CreateDate = casesmartgoal.LastUpdateDate;
                //set the id of the worker who has created this record
                casesmartgoal.CreatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID;
                //add a new record to database
                context.CaseSmartGoal.Add(casesmartgoal);
                isNew = true;
            }
            else
            {
                //update an existing record to database
                context.Entry(casesmartgoal).State = System.Data.Entity.EntityState.Modified;
            }
            Save();
            if (endOnly)
            {
                return;
            }
            if (casesmartgoal.ID > 0)
            {
                List <CaseSmartGoalServiceProvider> existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                if (!isNew)
                {
                    existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                    if (existingServiceProviderList == null)
                    {
                        existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                    }
                }
                else
                {
                    //add progress level outcome
                    CaseSmartGoalProgress newCaseSmartGoalProgress = new CaseSmartGoalProgress()
                    {
                        CaseSmartGoalID       = casesmartgoal.ID,
                        Comment               = "Initial Outcome",
                        CreateDate            = DateTime.Now,
                        CreatedByWorkerID     = casesmartgoal.CreatedByWorkerID,
                        LastUpdateDate        = DateTime.Now,
                        LastUpdatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID,
                        ProgressDate          = DateTime.Now,
                        ServiceLevelOutcomeID = casesmartgoal.ServiceLevelOutcomeID,
                    };
                    context.CaseSmartGoalProgress.Add(newCaseSmartGoalProgress);
                    context.SaveChanges();
                }
                bool   isAdded = false;
                string selectedUsedInternalServiceProvider = casesmartgoal.UsedInternalServiceProviderIDs;
                selectedUsedInternalServiceProvider = selectedUsedInternalServiceProvider.ToString(true).Replace("false", string.Empty);
                string[] arrayselectedUsedInternalServiceProvider = selectedUsedInternalServiceProvider.ToStringArray(',', true);
                if (arrayselectedUsedInternalServiceProvider != null && arrayselectedUsedInternalServiceProvider.Length > 0)
                {
                    foreach (string qolID in arrayselectedUsedInternalServiceProvider)
                    {
                        if (existingServiceProviderList.Where(item => item.ServiceProviderID == qolID.ToInteger(true)).Count() == 0)
                        {
                            CaseSmartGoalServiceProvider newCaseSmartGoalInternalService = new CaseSmartGoalServiceProvider()
                            {
                                ServiceProviderID     = qolID.ToInteger(true),
                                CaseSmartGoalID       = casesmartgoal.ID,
                                LastUpdateDate        = DateTime.Now,
                                LastUpdatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID,
                                IsUsed = true
                            };
                            casesmartgoalserviceproviderRepository.InsertOrUpdate(newCaseSmartGoalInternalService);
                            casesmartgoalserviceproviderRepository.Save();
                            isAdded = true;
                        }
                    }
                    if (isAdded)
                    {
                        existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                        if (existingServiceProviderList == null)
                        {
                            existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                        }
                    }
                }

                bool isDeleted = false;
                foreach (CaseSmartGoalServiceProvider existingUsedInternalServiceProvider in existingServiceProviderList.Where(item => item.IsUsed == true && item.ServiceProvider != null && item.ServiceProvider.IsExternal == false))
                {
                    if (arrayselectedUsedInternalServiceProvider == null || !arrayselectedUsedInternalServiceProvider.Contains(existingUsedInternalServiceProvider.ServiceProviderID.ToString(true)))
                    {
                        string sqlQuery = @"delete from [CaseAction] where CaseSmartGoalServiceProviderID=" + existingUsedInternalServiceProvider.ID + ";";
                        context.Database.ExecuteSqlCommand(sqlQuery);
                        casesmartgoalserviceproviderRepository.Delete(existingUsedInternalServiceProvider);
                        casesmartgoalserviceproviderRepository.Save();
                        isDeleted = true;
                    }
                }

                if (isDeleted)
                {
                    existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                    if (existingServiceProviderList == null)
                    {
                        existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                    }
                }
                isAdded = false;
                string selectedProposedInternalServiceProvider = casesmartgoal.ProposedInternalServiceProviderIDs;
                selectedProposedInternalServiceProvider = selectedProposedInternalServiceProvider.ToString(true).Replace("false", string.Empty);
                string[] arrayselectedProposedInternalServiceProvider = selectedProposedInternalServiceProvider.ToStringArray(',', true);
                if (arrayselectedProposedInternalServiceProvider != null && arrayselectedProposedInternalServiceProvider.Length > 0)
                {
                    foreach (string qolID in arrayselectedProposedInternalServiceProvider)
                    {
                        if (existingServiceProviderList.Where(item => item.ServiceProviderID == qolID.ToInteger(true)).Count() == 0)
                        {
                            CaseSmartGoalServiceProvider newCaseSmartGoalInternalService = new CaseSmartGoalServiceProvider()
                            {
                                ServiceProviderID     = qolID.ToInteger(true),
                                CaseSmartGoalID       = casesmartgoal.ID,
                                LastUpdateDate        = DateTime.Now,
                                LastUpdatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID,
                                IsUsed = false
                            };
                            casesmartgoalserviceproviderRepository.InsertOrUpdate(newCaseSmartGoalInternalService);
                            casesmartgoalserviceproviderRepository.Save();
                            isAdded = true;
                        }
                    }
                    if (isAdded)
                    {
                        existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                        if (existingServiceProviderList == null)
                        {
                            existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                        }
                    }
                }
                isDeleted = false;
                foreach (CaseSmartGoalServiceProvider existingProposedInternalServiceProvider in existingServiceProviderList.Where(item => item.IsUsed == false && item.ServiceProvider != null && item.ServiceProvider.IsExternal == false))
                {
                    if (arrayselectedProposedInternalServiceProvider == null || !arrayselectedProposedInternalServiceProvider.Contains(existingProposedInternalServiceProvider.ServiceProviderID.ToString(true)))
                    {
                        casesmartgoalserviceproviderRepository.Delete(existingProposedInternalServiceProvider);
                        casesmartgoalserviceproviderRepository.Save();
                        isDeleted = true;
                    }
                }
                if (isDeleted)
                {
                    existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                    if (existingServiceProviderList == null)
                    {
                        existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                    }
                }
                isAdded = false;
                string selectedUsedExternalServiceProvider = casesmartgoal.UsedExternalServiceProviderIDs;
                selectedUsedExternalServiceProvider = selectedUsedExternalServiceProvider.ToString(true).Replace("false", string.Empty);
                string[] arrayselectedUsedExternalServiceProvider = selectedUsedExternalServiceProvider.ToStringArray(',', true);
                if (arrayselectedUsedExternalServiceProvider != null && arrayselectedUsedExternalServiceProvider.Length > 0)
                {
                    foreach (string qolID in arrayselectedUsedExternalServiceProvider)
                    {
                        if (existingServiceProviderList.Where(item => item.ServiceProviderID == qolID.ToInteger(true)).Count() == 0)
                        {
                            CaseSmartGoalServiceProvider newCaseSmartGoalExternalService = new CaseSmartGoalServiceProvider()
                            {
                                ServiceProviderID     = qolID.ToInteger(true),
                                CaseSmartGoalID       = casesmartgoal.ID,
                                LastUpdateDate        = DateTime.Now,
                                LastUpdatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID,
                                IsUsed = true
                            };
                            casesmartgoalserviceproviderRepository.InsertOrUpdate(newCaseSmartGoalExternalService);
                            casesmartgoalserviceproviderRepository.Save();
                            isAdded = true;
                        }
                    }
                    if (isAdded)
                    {
                        existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                        if (existingServiceProviderList == null)
                        {
                            existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                        }
                    }
                }
                isDeleted = false;
                foreach (CaseSmartGoalServiceProvider existingUsedExternalServiceProvider in existingServiceProviderList.Where(item => item.IsUsed == true && item.ServiceProvider != null && item.ServiceProvider.IsExternal == true))
                {
                    if (arrayselectedUsedExternalServiceProvider == null || !arrayselectedUsedExternalServiceProvider.Contains(existingUsedExternalServiceProvider.ServiceProviderID.ToString(true)))
                    {
                        casesmartgoalserviceproviderRepository.Delete(existingUsedExternalServiceProvider);
                        casesmartgoalserviceproviderRepository.Save();
                        isDeleted = true;
                    }
                }
                if (isDeleted)
                {
                    existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                    if (existingServiceProviderList == null)
                    {
                        existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                    }
                }
                isAdded = false;
                string selectedProposedExternalServiceProvider = casesmartgoal.ProposedExternalServiceProviderIDs;
                selectedProposedExternalServiceProvider = selectedProposedExternalServiceProvider.ToString(true).Replace("false", string.Empty);
                string[] arrayselectedProposedExternalServiceProvider = selectedProposedExternalServiceProvider.ToStringArray(',', true);
                if (arrayselectedProposedExternalServiceProvider != null && arrayselectedProposedExternalServiceProvider.Length > 0)
                {
                    foreach (string qolID in arrayselectedProposedExternalServiceProvider)
                    {
                        if (existingServiceProviderList.Where(item => item.ServiceProviderID == qolID.ToInteger(true)).Count() == 0)
                        {
                            CaseSmartGoalServiceProvider newCaseSmartGoalExternalService = new CaseSmartGoalServiceProvider()
                            {
                                ServiceProviderID     = qolID.ToInteger(true),
                                CaseSmartGoalID       = casesmartgoal.ID,
                                LastUpdateDate        = DateTime.Now,
                                LastUpdatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID,
                                IsUsed = false
                            };
                            casesmartgoalserviceproviderRepository.InsertOrUpdate(newCaseSmartGoalExternalService);
                            casesmartgoalserviceproviderRepository.Save();
                            isAdded = true;
                        }
                    }
                    if (isAdded)
                    {
                        existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                        if (existingServiceProviderList == null)
                        {
                            existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                        }
                    }
                }
                isDeleted = false;
                foreach (CaseSmartGoalServiceProvider existingProposedExternalServiceProvider in existingServiceProviderList.Where(item => item.IsUsed == false && item.ServiceProvider != null && item.ServiceProvider.IsExternal == true))
                {
                    if (arrayselectedProposedExternalServiceProvider == null || !arrayselectedProposedExternalServiceProvider.Contains(existingProposedExternalServiceProvider.ServiceProviderID.ToString(true)))
                    {
                        casesmartgoalserviceproviderRepository.Delete(existingProposedExternalServiceProvider);
                        casesmartgoalserviceproviderRepository.Save();
                        isDeleted = true;
                    }
                }
                if (isDeleted)
                {
                    existingServiceProviderList = casesmartgoalserviceproviderRepository.AllIncluding(casesmartgoal.ID).ToList();
                    if (existingServiceProviderList == null)
                    {
                        existingServiceProviderList = new List <CaseSmartGoalServiceProvider>();
                    }
                }


                List <CaseSmartGoalAssignment> existingCaseSmartGoalAssignmentList = new List <CaseSmartGoalAssignment>();
                if (!isNew)
                {
                    existingCaseSmartGoalAssignmentList = context.CaseSmartGoalAssignment.Where(item => item.CaseSmartGoalID == casesmartgoal.ID).ToList();
                    if (existingCaseSmartGoalAssignmentList == null)
                    {
                        existingCaseSmartGoalAssignmentList = new List <CaseSmartGoalAssignment>();
                    }
                }

                string selectedCaseSmartGoalAssignment = casesmartgoal.SmartGoalIDs;
                selectedCaseSmartGoalAssignment = selectedCaseSmartGoalAssignment.ToString(true).Replace("false", string.Empty);
                string[] arrayselectedCaseSmartGoalAssignment = selectedCaseSmartGoalAssignment.ToStringArray(',', true);
                if (arrayselectedCaseSmartGoalAssignment != null && arrayselectedCaseSmartGoalAssignment.Length > 0)
                {
                    foreach (string qolID in arrayselectedCaseSmartGoalAssignment)
                    {
                        //if (existingCaseSmartGoalAssignmentList.Where(item => item.SmartGoalID == qolID.ToInteger(true)).Count() == 0)


                        var smartGoalOther = data["SmartGoalOther" + qolID] == null ? "" : data["SmartGoalOther" + qolID].ToString();

                        var qo1IDItem = existingCaseSmartGoalAssignmentList.FirstOrDefault(item => item.SmartGoalID == qolID.ToInteger(true));
                        if (qo1IDItem == null)
                        {
                            CaseSmartGoalAssignment newCaseSmartGoalAssignment = new CaseSmartGoalAssignment()
                            {
                                SmartGoalID           = qolID.ToInteger(true),
                                CaseSmartGoalID       = casesmartgoal.ID,
                                LastUpdateDate        = DateTime.Now,
                                LastUpdatedByWorkerID = casesmartgoal.LastUpdatedByWorkerID,
                                CreateDate            = DateTime.Now,
                                CreatedByWorkerID     = casesmartgoal.LastUpdatedByWorkerID,
                                IsArchived            = false
                            };
                            if (data["SmartGoalStartDate" + qolID] != null && data["SmartGoalStartDate" + qolID].IsNotNullOrEmpty() && data["SmartGoalStartDate" + qolID].ToDateTime().IsValidDate())
                            {
                                newCaseSmartGoalAssignment.StartDate = data["SmartGoalStartDate" + qolID].ToDateTime();
                            }
                            if (data["SmartGoalEndDate" + qolID] != null && data["SmartGoalEndDate" + qolID].IsNotNullOrEmpty() && data["SmartGoalEndDate" + qolID].ToDateTime().IsValidDate())
                            {
                                newCaseSmartGoalAssignment.EndDate = data["SmartGoalEndDate" + qolID].ToDateTime();
                            }
                            if (data["Comment" + qolID] != null && data["Comment" + qolID].IsNotNullOrEmpty())
                            {
                                newCaseSmartGoalAssignment.Comment = data["Comment" + qolID].ToString();
                            }


                            newCaseSmartGoalAssignment.SmartGoalOther = smartGoalOther;

                            context.CaseSmartGoalAssignment.Add(newCaseSmartGoalAssignment);
                        }
                        else if (qo1IDItem.SmartGoalOther != smartGoalOther)
                        {
                            qo1IDItem.SmartGoalOther = smartGoalOther;
                        }
                    }
                }

                foreach (CaseSmartGoalAssignment existingCaseSmartGoalAssignment in existingCaseSmartGoalAssignmentList)
                {
                    if (arrayselectedCaseSmartGoalAssignment == null || !arrayselectedCaseSmartGoalAssignment.Contains(existingCaseSmartGoalAssignment.SmartGoalID.ToString(true)))
                    {
                        context.Entry(existingCaseSmartGoalAssignment).State = System.Data.Entity.EntityState.Deleted;
                        Save();
                    }
                    else
                    {
                        if (data["SmartGoalStartDate" + existingCaseSmartGoalAssignment.SmartGoalID] != null && data["SmartGoalStartDate" + existingCaseSmartGoalAssignment.SmartGoalID].IsNotNullOrEmpty() && data["SmartGoalStartDate" + existingCaseSmartGoalAssignment.SmartGoalID].ToDateTime().IsValidDate())
                        {
                            existingCaseSmartGoalAssignment.StartDate = data["SmartGoalStartDate" + existingCaseSmartGoalAssignment.SmartGoalID].ToDateTime();
                        }
                        if (data["SmartGoalEndDate" + existingCaseSmartGoalAssignment.SmartGoalID] != null && data["SmartGoalEndDate" + existingCaseSmartGoalAssignment.SmartGoalID].IsNotNullOrEmpty() && data["SmartGoalEndDate" + existingCaseSmartGoalAssignment.SmartGoalID].ToDateTime().IsValidDate())
                        {
                            existingCaseSmartGoalAssignment.EndDate = data["SmartGoalEndDate" + existingCaseSmartGoalAssignment.SmartGoalID].ToDateTime();
                        }
                        if (data["Comment" + existingCaseSmartGoalAssignment.SmartGoalID] != null && data["Comment" + existingCaseSmartGoalAssignment.SmartGoalID].IsNotNullOrEmpty())
                        {
                            existingCaseSmartGoalAssignment.Comment = data["Comment" + existingCaseSmartGoalAssignment.SmartGoalID].ToString();
                        }
                        if (data["SmartGoalOther" + existingCaseSmartGoalAssignment.SmartGoalID] != null && data["SmartGoalOther" + existingCaseSmartGoalAssignment.SmartGoalID].IsNotNullOrEmpty())
                        {
                            existingCaseSmartGoalAssignment.Comment = data["SmartGoalOther" + existingCaseSmartGoalAssignment.SmartGoalID].ToString();
                        }
                    }
                }
            }
        }