/**
  * handles the permanent deletion of a registration.  See comments with _delete_registrations() for details on what models get affected.
  * @param  EE_Registration $REG registration to be deleted permenantly
  * @return boolean              true = successful deletion, false = fail.
  */
 protected function _delete_registration(EE_Registration $REG)
 {
     //first we start with the transaction... ultimately, we WILL not delete permanently if there are any related registrations on the transaction that are NOT trashed.
     $TXN = $REG->get_first_related('Transaction');
     $REGS = $TXN->get_many_related('Registration');
     $all_trashed = TRUE;
     foreach ($REGS as $registration) {
         if (!$registration->get('REG_deleted')) {
             $all_trashed = FALSE;
         }
     }
     if (!$all_trashed) {
         EE_Error::add_error(__('Unable to permanently delete this registration. Before this registration can be permanently deleted, all registrations made in the same transaction must be trashed as well.  These registrations will be permanently deleted in the same action.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
         return false;
     }
     //k made it here so that means we can delete all the related transactions and their answers (but let's do them separately from THIS one).
     foreach ($REGS as $registration) {
         //delete related answers
         $registration->delete_related_permanently('Answer');
         //remove relationship to EE_Attendee (but we ALWAYS leave the contact record intact)
         $attendee = $registration->get_first_related('Attendee');
         if ($attendee instanceof EE_Attendee) {
             $registration->_remove_relation_to($attendee, 'Attendee');
         }
         //now remove relationships to tickets on this registration.
         $registration->_remove_relations('Ticket');
         //now delete permanently the checkins related to this registration.
         $registration->delete_related_permanently('Checkin');
         if ($registration->ID() === $REG->ID()) {
             continue;
         }
         //we don't want to delete permanently the existing registration just yet.
         //remove relation to transaction for these registrations if NOT the existing registrations
         $registration->_remove_relations('Transaction');
         //now delete this registration permanently
         $registration->delete_permanently();
     }
     //now all related registrations on the transaction are handled.  So let's just handle this registration itself (the transaction and line items should be all that's left).
     //delete the line items related to the transaction for this registration.
     $TXN->delete_related_permanently('Line_Item');
     //we need to remove all the relationships on the transaction
     $TXN->delete_related_permanently('Payment');
     $TXN->delete_related_permanently('Extra_Meta');
     //now we can delete this REG permanently (and the transaction of course)
     $REG->delete_related_permanently('Transaction');
     return $REG->delete_permanently();
 }