Ejemplo n.º 1
0
        /// <summary>
        /// Add or Update caseaction to database
        /// </summary>
        /// <param name="caseaction">data to save</param>
        public void InsertOrUpdate(CaseAction caseaction)
        {
            bool isNew = false;

            if (caseaction.CaseSmartGoalServiceProviderID > 0)
            {
                caseaction.CaseProgressNoteID = null;
                caseaction.CaseSmartGoalID    = null;
            }
            if (caseaction.CaseProgressNoteID == 0)
            {
                caseaction.CaseProgressNoteID = null;
            }
            if (caseaction.CaseSmartGoalID == 0)
            {
                caseaction.CaseSmartGoalID = null;
            }
            caseaction.LastUpdateDate = DateTime.Now;
            if (caseaction.ID == default(int))
            {
                isNew = true;
                //set the date when this record was created
                caseaction.CreateDate = caseaction.LastUpdateDate;
                //set the id of the worker who has created this record
                caseaction.CreatedByWorkerID = caseaction.LastUpdatedByWorkerID;
                //add a new record to database
                context.CaseAction.Add(caseaction);
            }
            else
            {
                //update an existing record to database
                context.Entry(caseaction).State = System.Data.Entity.EntityState.Modified;
            }
            Save();
            if (caseaction.CaseProgressNoteID.HasValue && caseaction.CaseProgressNoteID.Value > 0)
            {
                CaseWorker primaryWorker = caseworkerRepository.FindPrimary(caseaction.CaseID);
                if (primaryWorker != null)
                {
                    string             caseLink           = "/CaseManagement/CaseProgressNote/Edit?noteID=" + caseaction.CaseProgressNoteID.Value + "&CaseID=" + caseaction.CaseID + "&CaseMemberID=" + caseaction.CaseMemberID;
                    WorkerNotification workerNotification = new WorkerNotification()
                    {
                        IsRead                = false,
                        LastUpdateDate        = DateTime.Now,
                        LastUpdatedByWorkerID = caseaction.LastUpdatedByWorkerID,
                        ReferenceLink         = caseLink,
                        WorkerID              = primaryWorker.WorkerID
                    };
                    if (isNew)
                    {
                        workerNotification.Notification = "A new action has been added to a progress note. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the note detail.";
                    }
                    else
                    {
                        workerNotification.Notification = "A progress note action has been updated. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the note detail.";
                    }
                    workernotificationRepository.InsertOrUpdate(workerNotification);
                    workernotificationRepository.Save();
                }
            }
            else if (caseaction.CaseSmartGoalServiceProviderID.HasValue && caseaction.CaseSmartGoalServiceProviderID.Value > 0)
            {
                CaseSmartGoalServiceProvider casesmartgoalserviceprovider = casesmartgoalserviceproviderRepository.Find(caseaction.CaseSmartGoalServiceProviderID.Value);
                if (casesmartgoalserviceprovider != null && casesmartgoalserviceprovider.WorkerID.HasValue && casesmartgoalserviceprovider.WorkerID.Value > 0)
                {
                    string             caseLink           = "/CaseManagement/CaseSmartGoalServiceProvider/Index?casesmartgoalId=" + casesmartgoalserviceprovider.CaseSmartGoalID + "&CaseID=" + caseaction.CaseID + "&CaseMemberID=" + caseaction.CaseMemberID;
                    WorkerNotification workerNotification = new WorkerNotification()
                    {
                        IsRead                = false,
                        LastUpdateDate        = DateTime.Now,
                        LastUpdatedByWorkerID = casesmartgoalserviceprovider.LastUpdatedByWorkerID,
                        ReferenceLink         = caseLink,
                        WorkerID              = casesmartgoalserviceprovider.WorkerID.Value
                    };
                    if (isNew)
                    {
                        workerNotification.Notification = "A new action has been added to a service provider. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the service provider detail.";
                    }
                    else
                    {
                        workerNotification.Notification = "A service provider action has been updated. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the service provider detail.";
                    }
                    workernotificationRepository.InsertOrUpdate(workerNotification);
                    workernotificationRepository.Save();
                }
            }

            if (caseaction.CaseWorkerID.HasValue && caseaction.CaseWorkerID.Value > 0)
            {
                int workerID = 0;
                if (caseaction.CaseSmartGoalServiceProviderID.HasValue && caseaction.CaseSmartGoalServiceProviderID.Value > 0)
                {
                    CaseSmartGoalServiceProvider casesmartgoalserviceprovider = casesmartgoalserviceproviderRepository.Find(caseaction.CaseSmartGoalServiceProviderID.Value);
                    if (casesmartgoalserviceprovider != null && casesmartgoalserviceprovider.WorkerID.HasValue)
                    {
                        workerID = casesmartgoalserviceprovider.WorkerID.Value;
                    }
                }
                if (workerID == 0)
                {
                    CaseWorker caseWorker = caseworkerRepository.Find(caseaction.CaseWorkerID.Value);
                    if (caseWorker != null)
                    {
                        workerID = caseWorker.WorkerID;
                    }
                }
                if (workerID > 0)
                {
                    string     caseLink   = "/CaseManagement/CaseAction/Index?CaseID=" + caseaction.CaseID + "&CaseMemberID=" + caseaction.CaseMemberID;
                    WorkerToDo workerToDo = new WorkerToDo()
                    {
                        LastUpdateDate        = DateTime.Now,
                        LastUpdatedByWorkerID = caseaction.LastUpdatedByWorkerID,
                        ReferenceLink         = caseLink,
                        WorkerID    = workerID,
                        IsCompleted = false,
                    };
                    if (isNew)
                    {
                        workerToDo.Subject = "A new action has been assigned to you. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the detail.";
                    }
                    else if (caseaction.IsCompleted)
                    {
                        workerToDo.Subject = "An action assigned to you has been completed. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the detail.";
                    }
                    else
                    {
                        workerToDo.Subject = "An action assigned to you has been updated. Please <a href='" + caseLink + "' target='_blank'>click here</a> to see the detail.";
                    }
                    workertodoRepository.InsertOrUpdate(workerToDo);
                    workertodoRepository.Save();
                }
            }
        }
Ejemplo n.º 2
0
        public ActionResult Index(int casesmartgoalId, int caseId, int?caseMemberId, int?casesmartgoalserviceproviderId)
        {
            //<JL:add:06/02/2017>
            var  varCase   = caseRepository.Find(caseId);
            bool hasAccess = workerroleactionpermissionnewRepository.HasPermission(caseId, CurrentLoggedInWorkerRoleIDs, CurrentLoggedInWorker.ID, varCase.ProgramID, varCase.RegionID, varCase.SubProgramID, varCase.JamatkhanaID, Constants.Areas.CaseManagement, Constants.Controllers.CaseSmartGoalServiceProvider, Constants.Actions.Index, true);

            if (!hasAccess)
            {
                WebHelper.CurrentSession.Content.ErrorMessage = "You are not eligible to do this action";
                return(RedirectToAction(Constants.Actions.AccessDenied, Constants.Controllers.Home, new { Area = String.Empty }));
            }
            //<JL:add:06/02/2017>

            //create a new instance of caseSmartGoalServiceProvider
            CaseSmartGoalServiceProvider caseSmartGoalServiceProvider = new CaseSmartGoalServiceProvider();

            if (casesmartgoalserviceproviderId.HasValue)
            {
                caseSmartGoalServiceProvider = casesmartgoalserviceproviderRepository.Find(casesmartgoalserviceproviderId.Value);
                if (caseSmartGoalServiceProvider != null)
                {
                    casesmartgoalId = caseSmartGoalServiceProvider.CaseSmartGoalID;
                }
            }
            caseSmartGoalServiceProvider.CaseID          = caseId;
            caseSmartGoalServiceProvider.CaseSmartGoalID = casesmartgoalId;
            if (caseMemberId.HasValue)
            {
                caseSmartGoalServiceProvider.CaseMemberID = caseMemberId.Value;
            }
            CaseSmartGoal caseSmartGoal = casesmartgoalRepository.Find(casesmartgoalId);

            if (caseSmartGoal != null)
            {
                caseSmartGoalServiceProvider.QualityOfLifeCategoryID = caseSmartGoal.QualityOfLifeCategoryID;
            }
            if (caseSmartGoalServiceProvider.CaseMemberID == 0 && caseSmartGoal != null && caseSmartGoal.CaseGoal != null)
            {
                caseSmartGoalServiceProvider.CaseMemberID = caseSmartGoal.CaseGoal.CaseMemberID;
            }
            if (caseSmartGoal != null)
            {
                List <CaseSmartGoalAssignment> goalAssignmentList = casesmartgoalRepository.FindAllCaseSmartGoalAssignmentByCaseSmargGoalID(caseSmartGoal.ID);
                if (goalAssignmentList != null)
                {
                    foreach (CaseSmartGoalAssignment goalAssignment in goalAssignmentList)
                    {
                        caseSmartGoal.SmartGoalName = caseSmartGoal.SmartGoalName.Concate(",", goalAssignment.SmartGoal.Name);
                    }
                }
            }
            if (caseSmartGoal != null && caseSmartGoal.CaseGoal != null && caseSmartGoal.CaseGoal.CaseMember != null)
            {
                caseSmartGoal.CaseMemberName = caseSmartGoal.CaseGoal.CaseMember.FirstName + " " + caseSmartGoal.CaseGoal.CaseMember.LastName;
            }
            caseSmartGoalServiceProvider.CaseSmartGoal = caseSmartGoal;
            caseSmartGoalServiceProvider.ServiceTypeID = 1;
            caseSmartGoalServiceProvider.CaseSmartGoalServiceLevelOutcome = new CaseSmartGoalServiceLevelOutcome();
            caseSmartGoalServiceProvider.CaseSmartGoalServiceLevelOutcome.CaseSmartGoalID = casesmartgoalId;

            caseSmartGoalServiceProvider.CaseAction = new CaseAction();
            //return view result
            ViewBag.ServiceProviderID = casesmartgoalserviceproviderId.ToString(true);
            if (caseMemberId.HasValue && caseMemberId.Value > 0)
            {
                caseSmartGoalServiceProvider.CaseMemberID = caseMemberId.Value;
                CaseMember caseMember = casememberRepository.Find(caseSmartGoalServiceProvider.CaseMemberID);
                if (caseMember != null)
                {
                    ViewBag.DisplayID = caseMember.DisplayID;
                    if (caseMember.Case != null)
                    {
                        caseSmartGoalServiceProvider.RegionID = caseMember.Case.RegionID;
                    }
                }
            }
            else
            {
                //var varCase = caseRepository.Find(caseId);
                if (varCase != null)
                {
                    ViewBag.DisplayID = varCase.DisplayID;
                    caseSmartGoalServiceProvider.RegionID = varCase.RegionID;
                }
            }
            //if (!caseSmartGoalServiceProvider.IsUsed)
            //{
            //    caseSmartGoalServiceProvider.IsProposed = true;
            //}
            return(View(caseSmartGoalServiceProvider));
        }
Ejemplo n.º 3
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) }));
            }
        }
Ejemplo n.º 4
0
        public ActionResult EditorAjax(int id, int caseId, int casememberId)
        {
            var  varCase   = caseRepository.Find(caseId);
            bool hasAccess = workerroleactionpermissionnewRepository.HasPermission(caseId, CurrentLoggedInWorkerRoleIDs, CurrentLoggedInWorker.ID, varCase.ProgramID, varCase.RegionID, varCase.SubProgramID, varCase.JamatkhanaID, Constants.Areas.CaseManagement, Constants.Controllers.CaseSmartGoalServiceProvider, Constants.Actions.Edit, true);

            if (!hasAccess)
            {
                WebHelper.CurrentSession.Content.ErrorMessage = "You are not eligible to do this action";
                return(RedirectToAction(Constants.Actions.AccessDenied, Constants.Controllers.Home, new { Area = String.Empty }));
            }

            CaseSmartGoalServiceProvider casesmartgoalserviceprovider = null;

            if (id > 0)
            {
                //find an existing casesmartgoalserviceprovider from database
                casesmartgoalserviceprovider = casesmartgoalserviceproviderRepository.Find(id);

                if (casesmartgoalserviceprovider == null)
                {
                    //throw an exception if id is provided but data does not exist in database
                    return(new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound, "Measurable goal service provider not found"));
                }

                if (casesmartgoalserviceprovider.ServiceProvider != null && (casesmartgoalserviceprovider.ServiceProvider.IsExternal || casesmartgoalserviceprovider.ServiceProvider.Name == "Other"))
                {
                    casesmartgoalserviceprovider.ServiceTypeID = 2;
                }
                else
                {
                    casesmartgoalserviceprovider.ServiceTypeID = 1;
                }
                if (casesmartgoalserviceprovider.FinancialAssistanceSubCategory != null)
                {
                    casesmartgoalserviceprovider.FinancialAssistanceCategoryID = casesmartgoalserviceprovider.FinancialAssistanceSubCategory.FinancialAssistanceCategoryID;
                }
                //casesmartgoalserviceprovider.CaseActionID = casesmartgoalserviceprovider.ID;
            }
            else
            {
                //create a new instance if id is not provided
                casesmartgoalserviceprovider = new CaseSmartGoalServiceProvider();
            }
            casesmartgoalserviceprovider.CaseID       = caseId;
            casesmartgoalserviceprovider.CaseMemberID = casememberId;
            CaseMember caseMember = casememberRepository.Find(casesmartgoalserviceprovider.CaseMemberID);

            if (caseMember != null)
            {
                ViewBag.DisplayID = caseMember.DisplayID;
                if (caseMember.Case != null)
                {
                    casesmartgoalserviceprovider.RegionID = caseMember.Case.RegionID;
                }
            }
            //if (!casesmartgoalserviceprovider.IsUsed)
            //{
            //    casesmartgoalserviceprovider.IsProposed = true;
            //}
            //return the html of editor to display on popup
            return(Content(this.RenderPartialViewToString(Constants.PartialViews.CreateOrEdit, casesmartgoalserviceprovider)));
        }
Ejemplo n.º 5
0
        public ActionResult DeleteAjax(int id)
        {
            //find the caseSmartGoalServiceProvider in database
            //find the varCase in database
            BaseModel statusModel = new BaseModel();

            if (!workerroleactionpermissionnewRepository.HasPermission(CurrentLoggedInWorkerRoleIDs, Constants.Areas.CaseManagement, Constants.Controllers.CaseSmartGoalServiceProvider, Constants.Actions.Delete, true))
            {
                statusModel.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 }) }));
            }

            CaseSmartGoalServiceProvider caseSmartGoalServiceProvider = casesmartgoalserviceproviderRepository.Find(id);

            if (caseSmartGoalServiceProvider == null)
            {
                //set error message if it does not exist in database
                caseSmartGoalServiceProvider = new CaseSmartGoalServiceProvider();
                caseSmartGoalServiceProvider.ErrorMessage = "CaseSmartGoalServiceProvider not found";
            }
            else
            {
                try
                {
                    var primaryWorkerID = GetPrimaryWorkerOfTheCase(caseSmartGoalServiceProvider.CaseSmartGoal.CaseGoal.CaseMember.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 });
                    }
                    //delete caseSmartGoalServiceProvider from database
                    casesmartgoalserviceproviderRepository.Delete(caseSmartGoalServiceProvider);
                    casesmartgoalserviceproviderRepository.Save();
                    //set success message
                    caseSmartGoalServiceProvider.SuccessMessage = "Case Smart Goal Service Provider has been deleted successfully";
                }
                catch (CustomException ex)
                {
                    caseSmartGoalServiceProvider.ErrorMessage = ex.UserDefinedMessage;
                }
                catch (Exception ex)
                {
                    if (ex.Message == "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.")
                    {
                        caseSmartGoalServiceProvider.SuccessMessage = "Case Smart Goal Service Provider has been deleted successfully";
                    }
                    else
                    {
                        ExceptionManager.Manage(ex);
                        caseSmartGoalServiceProvider.ErrorMessage = Constants.Messages.UnhandelledError;
                    }
                }
            }
            //return action status in json to display on a message bar
            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) }));
            }
        }
Ejemplo n.º 6
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();
                        }
                    }
                }
            }
        }