public PCMChildrensCourtViewModel GetChildrensCourtOutcometById(int CourtId)
        {
            PCMChildrensCourtViewModel vm = new PCMChildrensCourtViewModel();

            using (SDIIS_DatabaseEntities db = new SDIIS_DatabaseEntities())
            {
                try
                {
                    PCM_Childrens_Court_Outcome act = db.PCM_Childrens_Court_Outcome.Find(CourtId);
                    if (act != null)
                    {
                        vm.Outcome_Id             = act.Outcome_Id;
                        vm.Court_Case_Status_Id   = act.Court_Case_Status_Id;
                        vm.Placement_Type_Id      = act.Placement_Type_Id;
                        vm.Recommendation_Type_Id = act.Recommendation_Type_Id;
                        vm.Court_Outcome          = act.Court_Outcome;
                        vm.Intake_Assessment_Id   = act.Intake_Assessment_Id;



                        vm.Next_Court_Date = act.Next_Court_Date;
                        vm.Reason_Remand   = act.Reason_Remand;
                        vm.Remand          = act.Remand;
                        vm.Court_Date      = act.Court_Date;
                        if (act.Recommendation_Type_Id > 0)
                        {
                            vm.descrRecommendation = db.apl_Recommendation_Type.Find(act.Recommendation_Type_Id).Description;
                        }
                        if (act.Placement_Type_Id > 0)
                        {
                            vm.descrPlacement = db.apl_Placement_Type.Find(act.Placement_Type_Id).Description;
                        }
                        if (act.Court_Case_Status_Id > 0)
                        {
                            vm.descrStatusCourt = db.apl_PCM_Court_Case_Status.Find(act.Court_Case_Status_Id).Description;
                        }
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }

                return(vm);
            }
        }
        public void CreatePCMChildrensCourt(PCMChildrensCourtViewModel vm, int RecomendationID, int Intake_Assessment_Id)
        {
            using (SDIIS_DatabaseEntities db = new SDIIS_DatabaseEntities())
            {
                try
                {
                    PCM_Childrens_Court newCC = new PCM_Childrens_Court();
                    newCC.PCM_Recommendation_Id = RecomendationID;
                    newCC.Intake_Assessment_Id  = Intake_Assessment_Id;
                    newCC.Court_Expiry_Date     = vm.Court_Expiry_Date;
                    newCC.Prelim_Enquiry_Date   = vm.Prelim_Enquiry_Date;
                    newCC.Child_Need_Care       = vm.Child_Need_Care;
                    newCC.Substance_Abuse_Treat = vm.Substance_Abuse_Treat;
                    //newCC.Substance_Abuse_Treat = subAbuseTreat;
                    newCC.Referral_Date         = vm.Referral_Date;
                    newCC.Alternative_Placement = vm.Alternative_Placement;
                    newCC.Case_Manager          = vm.Case_Manager;
                    newCC.Service_Provider      = vm.Service_Provider;

                    db.PCM_Childrens_Court.Add(newCC);
                    db.SaveChanges();

                    PCM_Childrens_Court_Outcome newOutcome = new PCM_Childrens_Court_Outcome();
                    newOutcome.Outcome_Id           = vm.Outcome_Id;
                    newOutcome.Intake_Assessment_Id = Intake_Assessment_Id;


                    newOutcome.Children_Court_Id = newCC.Children_Court_Id;

                    db.PCM_Childrens_Court_Outcome.Add(newOutcome);
                    db.SaveChanges();
                }

                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
        }
        public PCMChildrensCourtViewModel GetCCOutComeByID(int Outcome_Id)
        {
            PCMChildrensCourtViewModel vm = new PCMChildrensCourtViewModel();

            using (SDIIS_DatabaseEntities db = new SDIIS_DatabaseEntities())
            {
                try
                {
                    int?id = (from c in db.PCM_Childrens_Court_Outcome
                              where (c.Intake_Assessment_Id == Outcome_Id)
                              select c.Intake_Assessment_Id).FirstOrDefault();

                    PCM_Childrens_Court_Outcome cc = db.PCM_Childrens_Court_Outcome.Find(id);
                    if (cc != null)
                    {
                        vm.Intake_Assessment_Id = db.PCM_Childrens_Court_Outcome.Find(cc.Intake_Assessment_Id).Intake_Assessment_Id;
                        vm.Outcome_Id           = cc.Outcome_Id;
                        vm.Intake_Assessment_Id = cc.Intake_Assessment_Id;
                        //vm.barcode = ass.barcode;
                        //vm.serial = ass.serial;
                        //vm.reason = ass.reason;
                        //vm.official = ass.official;
                        //vm.datereceived = ass.datereceived;
                        //vm.datereturned = ass.datereturned;

                        db.SaveChanges();
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }

                return(vm);
            }
        }
        public void CreatePCMChildrensCourtOutcome(PCMChildrensCourtViewModel vm, int RecomendationID, int Outcome_Id, int assID)
        {
            using (SDIIS_DatabaseEntities db = new SDIIS_DatabaseEntities())
            {
                try
                {
                    PCM_Childrens_Court_Outcome newOutcome = db.PCM_Childrens_Court_Outcome.Find(Outcome_Id);
                    newOutcome.Court_Date             = vm.Court_Date;
                    newOutcome.Remand                 = vm.Remand;
                    newOutcome.Reason_Remand          = vm.Reason_Remand;
                    newOutcome.Next_Court_Date        = vm.Next_Court_Date;
                    newOutcome.Court_Outcome          = vm.Court_Outcome;
                    newOutcome.Placement_Type_Id      = vm.Type_Of_Placement_Id;
                    newOutcome.Recommendation_Type_Id = vm.Recommendation_Type_Id;
                    newOutcome.Court_Case_Status_Id   = vm.Court_Case_Status_Id;


                    db.SaveChanges();



                    PCM_HB_Supervision exist1 = db.PCM_HB_Supervision.OrderByDescending(x => x.HomeBasedSupervision_Id).FirstOrDefault(x => x.Outcome_Id == Outcome_Id);
                    if (newOutcome.Placement_Type_Id == 187 && exist1 == null)

                    {
                        PCM_HB_Supervision newhomebase = new PCM_HB_Supervision();

                        newhomebase.Outcome_Id           = newOutcome.Outcome_Id;
                        newhomebase.Intake_Assessment_Id = assID;
                        newhomebase.Court_Type_Id        = 3;

                        db.PCM_HB_Supervision.Add(newhomebase);
                        db.SaveChanges();
                    }

                    PCM_Diversion exist12 = db.PCM_Diversion.OrderByDescending(x => x.Diversion_Id).FirstOrDefault(x => x.Childrens_Court_Outcome_Id == Outcome_Id);
                    if (newOutcome.Recommendation_Type_Id == 1 && exist12 == null)

                    {
                        PCM_Diversion newhomebase = new PCM_Diversion();

                        newhomebase.Childrens_Court_Outcome_Id = newOutcome.Outcome_Id;
                        newhomebase.Intake_Assessment_Id       = assID;
                        newhomebase.Court_Type_Id = 3;

                        db.PCM_Diversion.Add(newhomebase);
                        db.SaveChanges();
                    }
                }

                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
        }