Пример #1
0
 /**
  * This will marge the specified household into this household.  The passed in household will be DELETED.
  * All members of either household will be part of the merged household.
  * 
  * The New HeadPerson must be a member of either household, or else an exception will be thrown
  * 
  * All "Roles" will be reset by calling RefreshRole() on each HouseholdParticipation record.
  * @param Household $objHousehold
  * @param Person $objNewHeadPerson
  * @param boolean $blnTransaction whether or not to use a local transaction
  * @return void
  */
 public function MergeHousehold(Household $objHousehold, Person $objNewHeadPerson, $blnTransaction = true)
 {
     // Ensure the specified HeadPerson is part of either household
     if (!HouseholdParticipation::LoadByPersonIdHouseholdId($objNewHeadPerson->Id, $this->Id) && !HouseholdParticipation::LoadByPersonIdHouseholdId($objNewHeadPerson->Id, $objHousehold->Id)) {
         throw new QCallerException('Specified HeadPerson of this newly merged Household not a member of either household');
     }
     if ($blnTransaction) {
         self::GetDatabase()->TransactionBegin();
     }
     try {
         // Get all the members of the Merging Hosuehold
         $objParticipationArray = $objHousehold->GetHouseholdParticipationArray();
         // Each Home Address in the Merging Household will be set as a Prior Home address for each member
         $objAddressArray = $objHousehold->GetAddressArray();
         foreach ($objParticipationArray as $objParticipation) {
             foreach ($objAddressArray as $objAddress) {
                 $objAddress->CopyForPerson($objParticipation->Person, AddressType::Home, false);
             }
         }
         // Combine any household Splits
         foreach ($objHousehold->GetHouseholdSplitArray() as $objSplit) {
             if ($objSplit->SplitHouseholdId == $this->Id) {
                 $objSplit->Delete();
             } else {
                 $objSplit->Household = $this;
                 $objSplit->Save();
             }
         }
         foreach ($objHousehold->GetHouseholdSplitAsSplitArray() as $objSplit) {
             if ($objSplit->HouseholdId == $this->Id) {
                 $objSplit->Delete();
             } else {
                 $objSplit->SplitHousehold = $this;
                 $objSplit->Save();
             }
         }
         // Delete the merging household object, itself
         $objHousehold->Delete();
         // Each Person in the Merging Household will be now be associated with This Household
         foreach ($objParticipationArray as $objParticipation) {
             $this->AssociatePerson(Person::Load($objParticipation->PersonId), null);
         }
         // Setup the New Head and Refresh Data
         $this->HeadPerson = $objNewHeadPerson;
         $this->RefreshMembers(false);
         $this->RefreshName(false);
         $this->Save();
         // Refresh Roles of All Members
         foreach ($this->GetHouseholdParticipationArray() as $objParticipation) {
             $objParticipation->RefreshRole();
             $objParticipation->Person->RefreshPrimaryContactInfo();
         }
     } catch (Exception $objExc) {
         self::GetDatabase()->TransactionRollBack();
         throw $objExc;
     }
     if ($blnTransaction) {
         self::GetDatabase()->TransactionCommit();
     }
 }