Esempio n. 1
0
        /// <summary>
        /// Add or Update casemember to database
        /// </summary>
        /// <param name="casemember">data to save</param>
        public void InsertOrUpdate(CaseMember casemember)
        {
            //set a member as primary if there is no primary member has been set
            if (!casemember.IsPrimary)
            {
                int count = context.CaseMember.Where(item => item.CaseID == casemember.CaseID && item.IsPrimary == true).Count();
                if (count == 0)
                {
                    casemember.IsPrimary = true;
                }
            }
            string caseDisplayID = string.Empty;

            //when a member is set as primary, release others from primary is there is any
            if (casemember.IsPrimary && casemember.CaseID > 0)
            {
                string sqlQuery = "UPDATE CaseMember SET IsPrimary=0 WHERE CaseID=" + casemember.CaseID;
                context.Database.ExecuteSqlCommand(sqlQuery);
                context.SaveChanges();
                //Case varCase = context.Case.SingleOrDefault(item => item.ID == casemember.CaseID);
                //if (varCase != null)
                //{
                //    string regionCode = varCase.Region.Code;
                //    if (regionCode.IsNullOrEmpty())
                //    {
                //        if (varCase.Region.Name.IsNotNullOrEmpty())
                //        {
                //            regionCode = varCase.Region.Name.Substring(0, 2).ToUpper();
                //        }
                //    }
                //    if (varCase.DisplayID.IsNotNullOrEmpty() && varCase.DisplayID.Contains("NA"))
                //    {
                //        varCase.DisplayID = MiscUtility.GetCasePersonalizedId(regionCode, casemember.FirstName, casemember.LastName, casemember.CaseID, varCase.DisplayID);
                //    }
                //    context.Entry(varCase).State = System.Data.Entity.EntityState.Modified;
                //    context.SaveChanges();
                //    caseDisplayID = varCase.DisplayID;
                //}
            }
            //if (casemember.DisplayID.IsNullOrEmpty())
            //{
            //    if (caseDisplayID.IsNullOrEmpty())
            //    {
            //        Case varCase = context.Case.SingleOrDefault(item => item.ID == casemember.CaseID);
            //        if (varCase != null)
            //        {
            //            caseDisplayID = varCase.DisplayID;
            //        }
            //        if (caseDisplayID.IsNullOrEmpty())
            //        {
            //            CaseMember originalCaller = GetOriginalCaller(casemember.CaseID);
            //            if (originalCaller == null)
            //            {
            //                originalCaller = context.CaseMember.OrderBy(item => item.ID).FirstOrDefault(item=>item.CaseID==casemember.CaseID);
            //                if (originalCaller != null)
            //                {
            //                    originalCaller.IsPrimary = true;
            //                    context.Entry(originalCaller).State = System.Data.Entity.EntityState.Modified;
            //                    context.SaveChanges();
            //                }
            //            }
            //            string regionCode = varCase.Region.Code;
            //            if (regionCode.IsNullOrEmpty())
            //            {
            //                if (varCase.Region.Name.IsNotNullOrEmpty())
            //                {
            //                    regionCode = varCase.Region.Name.Substring(0, 2).ToUpper();
            //                }
            //            }
            //            if (varCase.DisplayID.IsNotNullOrEmpty() && varCase.DisplayID.Contains("NA"))
            //            {
            //                varCase.DisplayID = MiscUtility.GetCasePersonalizedId(regionCode, casemember.FirstName, casemember.LastName, casemember.CaseID, varCase.DisplayID);
            //            }
            //            context.Entry(varCase).State = System.Data.Entity.EntityState.Modified;
            //            context.SaveChanges();
            //            caseDisplayID = varCase.DisplayID;
            //        }
            //    }
            //}
            int sequence = 1;

            //Update case display id
            List <CaseMember> allCaseMember = context.CaseMember.Where(item => item.CaseID == casemember.CaseID && item.ID != casemember.ID).OrderBy(item => item.ID).ToList();

            if (allCaseMember != null)
            {
                foreach (CaseMember existingCaseMember in allCaseMember)
                {
                    casemember.Sequence  = sequence;
                    casemember.DisplayID = caseDisplayID + "-" + casemember.Sequence.ToString().PadLeft(3, '0');
                    sequence++;
                }
                Save();
            }
            casemember.Sequence       = sequence;
            casemember.DisplayID      = caseDisplayID + "-" + casemember.Sequence.ToString().PadLeft(3, '0');
            casemember.LastUpdateDate = DateTime.Now;
            if (casemember.ID == default(int))
            {
                //set the date when this record was created
                casemember.CreateDate = casemember.LastUpdateDate;
                //set the id of the worker who has created this record
                casemember.CreatedByWorkerID = casemember.LastUpdatedByWorkerID;
                //add a new record to database
                context.CaseMember.Add(casemember);
                Save();
                CaseWorker primaryWorker = caseworkerRepository.FindPrimary(casemember.CaseID);
                if (primaryWorker != null)
                {
                    CaseWorkerMemberAssignment newCaseWorkerMemberAssignment = new CaseWorkerMemberAssignment()
                    {
                        CaseMemberID          = casemember.ID,
                        CaseWorkerID          = primaryWorker.ID,
                        CreateDate            = DateTime.Now,
                        CreatedByWorkerID     = casemember.CreatedByWorkerID,
                        LastUpdateDate        = DateTime.Now,
                        LastUpdatedByWorkerID = casemember.CreatedByWorkerID
                    };
                    caseworkermemberassignmentRepository.InsertOrUpdate(newCaseWorkerMemberAssignment);
                    caseworkermemberassignmentRepository.Save();
                }
            }
            else
            {
                //update an existing record to database
                context.Entry(casemember).State = System.Data.Entity.EntityState.Modified;
            }
        }
Esempio n. 2
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) }));
            }
        }