public CompanionModel UpdateCompanion(CompanionModel companion)
        {
            var companionToUpdate = _domainObjectRepository.Get <Companion>(c => c.CompanionCID == companion.CompanionCID &&
                                                                            c.PatientCID == companion.PatientCID);
            var newCompanionTypeid =
                _domainObjectRepository.Get <CompanionType>(ct => ct.CompanionType1 == companion.CompanionType)?
                .CompanionTypeID;

            if (companionToUpdate != null)
            {
                companionToUpdate.CompanionCID    = companion.CompanionCID;
                companionToUpdate.BankID          = _domainObjectRepository.Get <Bank>(b => b.BankName == companion.BankName)?.BankID;
                companionToUpdate.CompanionFName  = companion.CompanionFName;
                companionToUpdate.CompanionMName  = companion.CompanionMName;
                companionToUpdate.CompanionLName  = companion.CompanionLName;
                companionToUpdate.CompanionTypeID = newCompanionTypeid;
                companionToUpdate.DateIn          = companion.DateIn;
                companionToUpdate.DateOut         = companion.DateOut;
                companionToUpdate.IsActive        = companion.IsActive;
                companionToUpdate.IBan            = companion.IBan;
                companionToUpdate.BankID          = _domainObjectRepository.Get <Bank>(b => b.BankName == companion.BankName)?.BankID;
                companionToUpdate.IsBeneficiary   = companion.IsBeneficiary;
                companionToUpdate.Notes           = companion.Notes;
                companionToUpdate.PatientCID      = companion.PatientCID;
                companionToUpdate.ModifiedBy      = companion.ModifiedBy;

                if (companion.IsActive == false && companion.DateOut == null)
                {
                    companionToUpdate.DateOut = DateTime.Now;
                }
                else if (companion.IsActive == true)
                {
                    companionToUpdate.DateOut = null;
                }
                // check if the patient is not beneficiary before doing the update to a beneficiary companion
                var patient = _domainObjectRepository.Get <Patient>(p => p.PatientCID == companion.PatientCID);
                CompanionRepository companionRepo = new CompanionRepository();
                var companionList = companionRepo.GetCompanionListByPatientCid(companion.PatientCID);

                // Assert that the user make an update according  to the rules
                CheckBeneficiary(patient, companion, companionList, newCompanionTypeid);
                // no 2 companions should be beneficiary or confelect with the patient
                CheckCompanionType(companionList.Where(c => c.IsActive == true)?.ToList(), newCompanionTypeid); // No 2 companions should have primary as a type
                if (companionToUpdate.IsBeneficiary == true &&
                    (companionToUpdate.Bank == null || String.IsNullOrEmpty(companionToUpdate.IBan)))
                {
                    throw new PatientsMgtException(1, "error", "Update a Companion",
                                                   "You must eneter the bank info! since this companion is beneficiary");
                }
                if (companionToUpdate.IsBeneficiary == true &&
                    companionToUpdate.IsActive == false)
                {
                    throw new PatientsMgtException(1, "error", "Update a Companion",
                                                   "The companion should be active since is beneficiary");
                }
                if (companionToUpdate.IsBeneficiary == true &&
                    companionToUpdate.CompanionTypeID == (int)Enums.CompanionType.Secondary)
                {
                    throw new PatientsMgtException(1, "error", "Update a Companion",
                                                   String.Format("The companion should not be {0} since is beneficiary", companionToUpdate?.CompanionType?.CompanionType1));
                }
                if (companionToUpdate.IsActive == false && companionToUpdate.DateOut == null)
                {
                    throw new PatientsMgtException(1, "error", "Update a Companion",
                                                   String.Format("You have set the companion as inactive, so the date out should be set", companionToUpdate?.CompanionType?.CompanionType1));
                }
                // whene every rule is complied with, we do an update
                _domainObjectRepository.Update <Companion>(companionToUpdate);

                //insert into history when the companion is not active, or date out is set
                if (companionToUpdate.IsActive == false && companionToUpdate.DateOut != null)
                {
                    InsertIntoCompanionHistoryTable(companionToUpdate);
                }
                //Update the beneficiary table
                UpdateBeneficiary(companion, companionToUpdate, patient);
            }

            return(companion);
        }
        public void AddCompanion(CompanionModel companion)
        {
            var existingRecord =
                _domainObjectRepository.Get <Companion>(
                    c => c.CompanionCID == companion.CompanionCID && c.PatientCID == companion.PatientCID);

            if (existingRecord != null)
            {
                throw new PatientsMgtException(1, "error", "Create new Companion", "There is aleady a record with the same companion and patient");
            }
            if (!String.IsNullOrEmpty(companion.PatientCID))
            {
                var newCompanionTypeid = _domainObjectRepository.Get <CompanionType>(ct => ct.CompanionType1 == companion.CompanionType)?
                                         .CompanionTypeID;
                #region Validate companion Type
                //check if already there is a newCompanion associated with the pationcid in table who is primary
                //we should not have two companions with primary type account
                CompanionRepository companionRepo = new CompanionRepository();
                var companionList = companionRepo.GetCompanionListByPatientCid(companion.PatientCID);
                if (companionList != null && companionList.Count > 0)//patient is already in table with other newCompanion
                {
                    //check if this other companion with the same patient that are primary
                    if (newCompanionTypeid == (int)Enums.CompanionType.Primary)
                    {
                        CheckCompanionType(companionList.Where(c => c.IsActive == true)?.ToList(), newCompanionTypeid);
                    }
                }
                //
                #endregion
                var patient = _domainObjectRepository.Get <Patient>(p => p.PatientCID == companion.PatientCID);
                if (patient != null)
                {
                    #region validate beneficiary
                    CheckBeneficiary(patient, companion, companionList, newCompanionTypeid);
                    #endregion

                    Companion newCompanion = new Companion()
                    {
                        CompanionCID    = companion.CompanionCID,
                        CompanionFName  = companion.CompanionFName,
                        CompanionMName  = companion.CompanionMName,
                        CompanionLName  = companion.CompanionLName,
                        CompanionTypeID = newCompanionTypeid,
                        DateIn          = companion.DateIn,
                        DateOut         = companion.DateOut,
                        IsActive        = companion.IsActive, // == "Yes" ? true : false,
                        IBan            = companion.IBan,
                        BankID          = _domainObjectRepository.Get <Bank>(b => b.BankName == companion.BankName)?.BankID,
                        IsBeneficiary   = companion.IsBeneficiary, // == "Yes" ? true : false,
                        Notes           = companion.Notes,
                        PatientCID      = companion.PatientCID,
                        CreatedBy       = companion.CreatedBy
                    };
                    // check if the user entered a bank info when the companion is set as primary and beneficiary

                    _domainObjectRepository.Create <Companion>(newCompanion);
                    // once we created the Companion with a primary type companiontype, we need to call a method that does an insert into Beneficiary table
                    // if the user is not primary then should not be beneficiary
                    if ((newCompanionTypeid == (int)Enums.CompanionType.Primary) &&
                        newCompanion.IsActive == true)
                    {
                        // this Beneficiary can be a ptient himself if he is Beneficiary by default
                        InsertIntoBeneficiaryTable(patient, newCompanion);
                    }
                }
            }
        }