Esempio n. 1
0
        public ActionResult Create(CaseWorker caseworker)
        {
            caseworker.LastUpdatedByWorkerID = CurrentLoggedInWorker.ID;
            try
            {
                //validate data
                if (ModelState.IsValid)
                {
                    //if (!string.IsNullOrEmpty(caseworker.CaseWorkerNote.Note) || caseworker.CaseWorkerNote.NoteDate != null ||
                    //    caseworker.CaseWorkerNote.TimeSpentHours != null || caseworker.CaseWorkerNote.TimeSpentMinutes != null
                    //    || (caseworker.CaseWorkerNote.ContactMethodID != null && caseworker.CaseWorkerNote.ContactMethodID > 0))
                    //{
                    //    var isnoteerror = false;

                    //    if (string.IsNullOrEmpty(caseworker.CaseWorkerNote.Note))
                    //    {
                    //        ModelState.AddModelError("", "Please enter work note.");
                    //        isnoteerror = true;
                    //    }

                    //    if (caseworker.CaseWorkerNote.NoteDate == null)
                    //    {
                    //        ModelState.AddModelError("", "Please enter not date");
                    //        isnoteerror = true;
                    //    }

                    //    if ((caseworker.CaseWorkerNote.TimeSpentHours == null || caseworker.CaseWorkerNote.TimeSpentHours == 0) &&
                    //        (caseworker.CaseWorkerNote.TimeSpentMinutes == null || caseworker.CaseWorkerNote.TimeSpentMinutes == 0))
                    //    {
                    //        ModelState.AddModelError("", "Please enter time spent");
                    //        isnoteerror = true;
                    //    }

                    //    if (caseworker.CaseWorkerNote.ContactMethodID == null || caseworker.CaseWorkerNote.ContactMethodID == 0)
                    //    {
                    //        ModelState.AddModelError("", "Please select contact method");
                    //        isnoteerror = true;
                    //    }
                    //    if (isnoteerror)
                    //    {
                    //        Case varCase = caseRepository.Find(caseId);

                    //        List<SelectListItem> workerList = workerRepository.NewFindAllPossible(varCase.ProgramID, varCase.RegionID, varCase.SubProgramID);
                    //        if (workerList == null || (workerList != null && workerList.Count == 0))
                    //        {
                    //            caseworker.ErrorMessage = "There is no worker found for Program:" + varCase.Program.Name + ", Region:" + varCase.Region.Name + ", Sub-Program:" + varCase.SubProgram.Name;
                    //        }
                    //        ViewBag.PossibleWorkerList = workerList;
                    //        return View(caseworker);
                    //    }

                    //}

                    //call the repository function to save in database
                    caseworker.IsActive = true;
                    caseworkerRepository.InsertOrUpdate(caseworker);
                    caseworkerRepository.Save();

                    string selectedCaseMember = Request.Form["SelectedCaseMember"].ToString(true);
                    selectedCaseMember = selectedCaseMember.Replace("false", string.Empty);
                    string[] arraySelectedCaseMember             = selectedCaseMember.ToStringArray(',', true);
                    List <CaseWorkerMemberAssignment> assignment = caseworkermemberassignmentRepository.FindAllByCaseWorkerID(caseworker.ID);
                    if (arraySelectedCaseMember != null && arraySelectedCaseMember.Length > 0)
                    {
                        foreach (string caseMemberID in arraySelectedCaseMember)
                        {
                            if (assignment.Where(item => item.CaseMemberID == caseMemberID.ToInteger(true)).Count() == 0)
                            {
                                CaseWorkerMemberAssignment newCaseWorkerMemberAssignment = new CaseWorkerMemberAssignment()
                                {
                                    CaseMemberID          = caseMemberID.ToInteger(true),
                                    CaseWorkerID          = caseworker.ID,
                                    LastUpdateDate        = DateTime.Now,
                                    LastUpdatedByWorkerID = caseworker.LastUpdatedByWorkerID
                                };
                                caseworkermemberassignmentRepository.InsertOrUpdate(newCaseWorkerMemberAssignment);
                                caseworkermemberassignmentRepository.Save();
                            }
                        }
                    }

                    foreach (CaseWorkerMemberAssignment existingMember in assignment)
                    {
                        if (arraySelectedCaseMember == null || !arraySelectedCaseMember.Contains(existingMember.CaseMemberID.ToString(true)))
                        {
                            caseworkermemberassignmentRepository.Delete(existingMember);
                            caseworkermemberassignmentRepository.Save();
                        }
                    }
                    //if (caseworker.CaseWorkerNote.ContactMethodID > 0)
                    //{
                    //    caseworker.CaseWorkerNote.LastUpdatedByWorkerID = CurrentLoggedInWorker.ID;
                    //    caseworker.CaseWorkerNote.CaseID = caseworker.CaseID;
                    //    //caseMember.CaseWorkerNote.CaseStatusID = varCase.CaseStatusID;
                    //    //caseMember.CaseWorkerNote.ProgramID = varCase.ProgramID;
                    //    caseworker.CaseWorkerNote.IsFamily = true;
                    //    caseworker.CaseWorkerNote.IsFamilyMember = false;
                    //    caseworker.CaseWorkerNote.WorkerNoteActivityTypeID = (int)WorkerNoteActivityType.CaseWorker;
                    //    //varCase.CaseWorkerNote.NoteDate = Convert.ToDateTime(varCase.ContactDate);
                    //    caseWorkerNoteRepository.InsertOrUpdate(caseworker.CaseWorkerNote);
                    //    caseWorkerNoteRepository.Save();
                    //}

                    //redirect to list page after successful operation
                    //return RedirectToAction(Constants.Views.Index, new { caseId = caseId });
                    return(RedirectToAction(Constants.Actions.Index, Constants.Controllers.CaseSummary, new { caseID = caseworker.CaseID }));
                }
                else
                {
                    foreach (var modelStateValue in ViewData.ModelState.Values)
                    {
                        foreach (var error in modelStateValue.Errors)
                        {
                            caseworker.ErrorMessage = error.ErrorMessage;
                            break;
                        }
                        if (caseworker.ErrorMessage.IsNotNullOrEmpty())
                        {
                            break;
                        }
                    }
                }
            }
            catch (CustomException ex)
            {
                caseworker.ErrorMessage = ex.UserDefinedMessage;
            }
            catch (Exception ex)
            {
                ExceptionManager.Manage(ex);
                caseworker.ErrorMessage = Constants.Messages.UnhandelledError;
            }
            ViewBag.PossibleCreatedByWorkers     = workerRepository.All;
            ViewBag.PossibleLastUpdatedByWorkers = workerRepository.All;
            ViewBag.PossibleWorkers = workerRepository.All;
            ViewBag.DisplayID       = caseRepository.Find(caseworker.CaseID).DisplayID;
            ViewBag.CaseID          = caseworker.CaseID;
            return(View(caseworker));
        }
        public ActionResult SaveAjax(CaseWorker caseworker)
        {
            //id=0 means add operation, update operation otherwise
            bool isNew = caseworker.ID == 0;

            //validate data
            if (ModelState.IsValid)
            {
                try
                {
                    //<JL:Comment:No need to check access again on post. On edit we are already checking permission.>
                    //if (!isNew)
                    //{
                    //    var primaryWorkerID = GetPrimaryWorkerOfTheCase(caseworker.CaseID);
                    //    List<CaseWorker> lstcaseworker = caseworkerRepository.FindAllByCaseID(caseworker.CaseID).Where(x => x.WorkerID == CurrentLoggedInWorker.ID).ToList();
                    //    if ((lstcaseworker == null || lstcaseworker.Count() == 0) && caseworker.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 });
                    //    }
                    //}
                    //</JL:Comment:07/08/2017>

                    //set the id of the worker who has added/updated this record
                    caseworker.LastUpdatedByWorkerID = CurrentLoggedInWorker.ID;
                    //call repository function to save the data in database
                    caseworkerRepository.InsertOrUpdate(caseworker);
                    caseworkerRepository.Save();
                    string selectedCaseMember = Request.Form["SelectedCaseMember"].ToString(true);
                    selectedCaseMember = selectedCaseMember.Replace("false", string.Empty);
                    string[] arraySelectedCaseMember             = selectedCaseMember.ToStringArray(',', true);
                    List <CaseWorkerMemberAssignment> assignment = caseworkermemberassignmentRepository.FindAllByCaseWorkerID(caseworker.ID);
                    if (arraySelectedCaseMember != null && arraySelectedCaseMember.Length > 0)
                    {
                        foreach (string caseMemberID in arraySelectedCaseMember)
                        {
                            if (assignment.Where(item => item.CaseMemberID == caseMemberID.ToInteger(true)).Count() == 0)
                            {
                                CaseWorkerMemberAssignment newCaseWorkerMemberAssignment = new CaseWorkerMemberAssignment()
                                {
                                    CaseMemberID          = caseMemberID.ToInteger(true),
                                    CaseWorkerID          = caseworker.ID,
                                    LastUpdateDate        = DateTime.Now,
                                    LastUpdatedByWorkerID = caseworker.LastUpdatedByWorkerID
                                };
                                caseworkermemberassignmentRepository.InsertOrUpdate(newCaseWorkerMemberAssignment);
                                caseworkermemberassignmentRepository.Save();
                            }
                        }
                    }

                    foreach (CaseWorkerMemberAssignment existingMember in assignment)
                    {
                        if (arraySelectedCaseMember == null || !arraySelectedCaseMember.Contains(existingMember.CaseMemberID.ToString(true)))
                        {
                            caseworkermemberassignmentRepository.Delete(existingMember);
                            caseworkermemberassignmentRepository.Save();
                        }
                    }
                    //set status message
                    if (isNew)
                    {
                        caseworker.SuccessMessage = "Case Worker has been added successfully";
                    }
                    else
                    {
                        caseworker.SuccessMessage = "Case Worker has been updated successfully";
                    }
                }
                catch (System.Data.Entity.Infrastructure.DbUpdateException)
                {
                    caseworker.ErrorMessage = "The worker is already assigned to the family or family member";
                }
                catch (CustomException ex)
                {
                    caseworker.ErrorMessage = ex.UserDefinedMessage;
                }
                catch (Exception ex)
                {
                    ExceptionManager.Manage(ex);
                    caseworker.ErrorMessage = Constants.Messages.UnhandelledError;
                }
            }
            else
            {
                foreach (var modelStateValue in ViewData.ModelState.Values)
                {
                    foreach (var error in modelStateValue.Errors)
                    {
                        caseworker.ErrorMessage = error.ErrorMessage;
                        break;
                    }
                    if (caseworker.ErrorMessage.IsNotNullOrEmpty())
                    {
                        break;
                    }
                }
            }
            //return the status message in json
            if (caseworker.ErrorMessage.IsNotNullOrEmpty())
            {
                return(Json(new { success = false, data = this.RenderPartialViewToString(Constants.PartialViews.Alert, caseworker) }));
            }
            else
            {
                return(Json(new { success = true, data = this.RenderPartialViewToString(Constants.PartialViews.Alert, caseworker) }));
            }
        }