Lines 627-633
sub merge_with {
Link Here
|
627 |
my $anonymous_patron = C4::Context->preference("AnonymousPatron"); |
627 |
my $anonymous_patron = C4::Context->preference("AnonymousPatron"); |
628 |
return if $anonymous_patron && $self->id eq $anonymous_patron; |
628 |
return if $anonymous_patron && $self->id eq $anonymous_patron; |
629 |
|
629 |
|
630 |
my @patron_ids = @{ $patron_ids }; |
630 |
my @patron_ids = @{$patron_ids}; |
631 |
|
631 |
|
632 |
# Ensure the keeper isn't in the list of patrons to merge |
632 |
# Ensure the keeper isn't in the list of patrons to merge |
633 |
@patron_ids = grep { $_ ne $self->id } @patron_ids; |
633 |
@patron_ids = grep { $_ ne $self->id } @patron_ids; |
Lines 636-695
sub merge_with {
Link Here
|
636 |
|
636 |
|
637 |
my $results; |
637 |
my $results; |
638 |
|
638 |
|
639 |
$self->_result->result_source->schema->txn_do( sub { |
639 |
$self->_result->result_source->schema->txn_do( |
640 |
foreach my $patron_id (@patron_ids) { |
640 |
sub { |
|
|
641 |
foreach my $patron_id (@patron_ids) { |
641 |
|
642 |
|
642 |
next if $patron_id eq $anonymous_patron; |
643 |
next if $patron_id eq $anonymous_patron; |
643 |
|
644 |
|
644 |
my $patron = Koha::Patrons->find( $patron_id ); |
645 |
my $patron = Koha::Patrons->find($patron_id); |
645 |
|
646 |
|
646 |
next unless $patron; |
647 |
next unless $patron; |
647 |
|
648 |
|
648 |
# Unbless for safety, the patron will end up being deleted |
649 |
# Unbless for safety, the patron will end up being deleted |
649 |
$results->{merged}->{$patron_id}->{patron} = $patron->unblessed; |
650 |
$results->{merged}->{$patron_id}->{patron} = $patron->unblessed; |
650 |
|
651 |
|
651 |
my $attributes = $patron->extended_attributes; |
652 |
my $attributes = $patron->extended_attributes; |
652 |
my $new_attributes = [ |
653 |
my $new_attributes = [ map { { code => $_->code, attribute => $_->attribute } } $attributes->as_list ]; |
653 |
map { { code => $_->code, attribute => $_->attribute } } |
654 |
$attributes->delete |
654 |
$attributes->as_list |
655 |
; # We need to delete before trying to merge them to prevent exception on unique and repeatable |
655 |
]; |
656 |
for my $attribute (@$new_attributes) { |
656 |
$attributes->delete; # We need to delete before trying to merge them to prevent exception on unique and repeatable |
657 |
try { |
657 |
for my $attribute ( @$new_attributes ) { |
658 |
$self->add_extended_attribute($attribute); |
658 |
try { |
659 |
} catch { |
659 |
$self->add_extended_attribute($attribute); |
660 |
|
660 |
} catch { |
661 |
# Don't block the merge if there is a non-repeatable attribute that cannot be added to the current patron. |
661 |
# Don't block the merge if there is a non-repeatable attribute that cannot be added to the current patron. |
662 |
unless ( $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) { |
662 |
unless ( $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) { |
663 |
$_->rethrow; |
663 |
$_->rethrow; |
664 |
} |
|
|
665 |
}; |
666 |
} |
667 |
|
668 |
while ( my ( $r, $field ) = each(%$RESULTSET_PATRON_ID_MAPPING) ) { |
669 |
my $rs = $schema->resultset($r)->search( { $field => $patron_id } ); |
670 |
$results->{merged}->{$patron_id}->{updated}->{$r} = $rs->count(); |
671 |
$rs->update( { $field => $self->id } ); |
672 |
if ( $r eq 'BorrowerDebarment' ) { |
673 |
Koha::Patron::Debarments::UpdateBorrowerDebarmentFlags( $self->id ); |
664 |
} |
674 |
} |
665 |
}; |
675 |
} |
666 |
} |
|
|
667 |
|
676 |
|
668 |
while (my ($r, $field) = each(%$RESULTSET_PATRON_ID_MAPPING)) { |
677 |
$patron->move_to_deleted(); |
669 |
my $rs = $schema->resultset($r)->search({ $field => $patron_id }); |
678 |
$patron->delete(); |
670 |
$results->{merged}->{ $patron_id }->{updated}->{$r} = $rs->count(); |
679 |
|
671 |
$rs->update({ $field => $self->id }); |
680 |
if ( C4::Context->preference("BorrowersLog") ) { |
672 |
if ( $r eq 'BorrowerDebarment' ) { |
681 |
my $info = |
673 |
Koha::Patron::Debarments::UpdateBorrowerDebarmentFlags($self->id); |
682 |
$patron->firstname . " " |
|
|
683 |
. $patron->surname . " (" |
684 |
. $patron->cardnumber . ")" |
685 |
. " has been merged into " |
686 |
. $self->firstname . " " |
687 |
. $self->surname . " (" |
688 |
. $self->cardnumber . ")"; |
689 |
logaction( "MEMBERS", "PATRON_MERGE", $self->id, $info ); |
674 |
} |
690 |
} |
675 |
} |
691 |
} |
676 |
|
692 |
|
677 |
$patron->move_to_deleted(); |
|
|
678 |
$patron->delete(); |
679 |
|
680 |
if ( C4::Context->preference("BorrowersLog") ) { |
681 |
my $info = |
682 |
$patron->firstname . " " |
683 |
. $patron->surname . " (" |
684 |
. $patron->cardnumber . ")" |
685 |
. " has been merged into " |
686 |
. $self->firstname . " " |
687 |
. $self->surname . " (" |
688 |
. $self->cardnumber . ")"; |
689 |
logaction( "MEMBERS", "PATRON_MERGE", $self->id, $info ); |
690 |
} |
691 |
} |
693 |
} |
692 |
}); |
694 |
); |
693 |
|
695 |
|
694 |
return $results; |
696 |
return $results; |
695 |
} |
697 |
} |