View | Details | Raw Unified | Return to bug 22632
Collapse All | Expand All

(-)a/Koha/Patron.pm (-42 / +44 lines)
Lines 636-694 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 = [ map { { code => $_->code, attribute => $_->attribute } } $attributes->as_list ];
653
                my $new_attributes = [ map { { code => $_->code, attribute => $_->attribute } } $attributes->as_list ];
653
            $attributes
654
                $attributes->delete
654
                ->delete;  # We need to delete before trying to merge them to prevent exception on unique and repeatable
655
                    ;    # We need to delete before trying to merge them to prevent exception on unique and repeatable
655
            for my $attribute (@$new_attributes) {
656
                for my $attribute (@$new_attributes) {
656
                try {
657
                    try {
657
                    $self->add_extended_attribute($attribute);
658
                        $self->add_extended_attribute($attribute);
658
                } catch {
659
                    } catch {
659
660
660
                    # 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.
661
                    unless ( $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) {
662
                        unless ( $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) {
662
                        $_->rethrow;
663
                            $_->rethrow;
663
                    }
664
                        }
664
                };
665
                    };
665
            }
666
                }
666
667
667
            while ( my ( $r, $field ) = each(%$RESULTSET_PATRON_ID_MAPPING) ) {
668
                while ( my ( $r, $field ) = each(%$RESULTSET_PATRON_ID_MAPPING) ) {
668
                my $rs = $schema->resultset($r)->search( { $field => $patron_id } );
669
                    my $rs = $schema->resultset($r)->search( { $field => $patron_id } );
669
                $results->{merged}->{$patron_id}->{updated}->{$r} = $rs->count();
670
                    $results->{merged}->{$patron_id}->{updated}->{$r} = $rs->count();
670
                $rs->update( { $field => $self->id } );
671
                    $rs->update( { $field => $self->id } );
671
                if ( $r eq 'BorrowerDebarment' ) {
672
                    if ( $r eq 'BorrowerDebarment' ) {
672
                    Koha::Patron::Debarments::UpdateBorrowerDebarmentFlags( $self->id );
673
                        Koha::Patron::Debarments::UpdateBorrowerDebarmentFlags( $self->id );
674
                    }
673
                }
675
                }
674
            }
675
676
676
            $patron->move_to_deleted();
677
                $patron->move_to_deleted();
677
            $patron->delete();
678
                $patron->delete();
678
679
679
            if ( C4::Context->preference("BorrowersLog") ) {
680
                if ( C4::Context->preference("BorrowersLog") ) {
680
                my $info =
681
                    my $info =
681
                      $patron->firstname . " "
682
                          $patron->firstname . " "
682
                    . $patron->surname . " ("
683
                        . $patron->surname . " ("
683
                    . $patron->cardnumber . ")"
684
                        . $patron->cardnumber . ")"
684
                    . " has been merged into "
685
                        . " has been merged into "
685
                    . $self->firstname . " "
686
                        . $self->firstname . " "
686
                    . $self->surname . " ("
687
                        . $self->surname . " ("
687
                    . $self->cardnumber . ")";
688
                        . $self->cardnumber . ")";
688
                logaction( "MEMBERS", "PATRON_MERGE", $self->id, $info );
689
                    logaction( "MEMBERS", "PATRON_MERGE", $self->id, $info );
690
                }
689
            }
691
            }
690
        }
692
        }
691
    });
693
    );
692
694
693
    return $results;
695
    return $results;
694
}
696
}
(-)a/t/db_dependent/Koha/Patron/Log.t (-17 / +30 lines)
Lines 18-23 use Test::More tests => 1; Link Here
18
18
19
use C4::Log;
19
use C4::Log;
20
20
21
use Koha::ActionLogs;
22
21
use t::lib::Mocks;
23
use t::lib::Mocks;
22
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
23
25
Lines 26-32 $schema->storage->txn_begin; Link Here
26
28
27
subtest 'Test Koha::Patrons::merge' => sub {
29
subtest 'Test Koha::Patrons::merge' => sub {
28
30
29
    plan tests => 4;
31
    plan tests => 3;
30
    my $builder = t::lib::TestBuilder->new;
32
    my $builder = t::lib::TestBuilder->new;
31
33
32
    my $keeper = $builder->build_object( { class => 'Koha::Patrons' } );
34
    my $keeper = $builder->build_object( { class => 'Koha::Patrons' } );
Lines 41-47 subtest 'Test Koha::Patrons::merge' => sub { Link Here
41
43
42
    my $results = $keeper->merge_with( [ $borrower1->{borrowernumber}, $borrower2->{borrowernumber} ] );
44
    my $results = $keeper->merge_with( [ $borrower1->{borrowernumber}, $borrower2->{borrowernumber} ] );
43
45
44
    my $log = GetLogs( "", "", "", ["MEMBERS"], ["PATRON_MERGE"], $keeper->id, "" );
46
    # Action logs records the merges under the 'object' of the kept borrowernumber
47
    my $logs = Koha::ActionLogs->search(
48
        {
49
            module => "MEMBERS",
50
            action => "PATRON_MERGE",
51
            object => $keeper->id
52
        }
53
    );
45
54
46
    my $info_borrower1 =
55
    my $info_borrower1 =
47
          $borrower1->{firstname} . " "
56
          $borrower1->{firstname} . " "
Lines 51-61 subtest 'Test Koha::Patrons::merge' => sub { Link Here
51
        . $keeper->firstname . " "
60
        . $keeper->firstname . " "
52
        . $keeper->surname . " ("
61
        . $keeper->surname . " ("
53
        . $keeper->cardnumber . ")";
62
        . $keeper->cardnumber . ")";
54
    my $info_log = $log->[0]{info};
63
    my $info_log = $logs->next->info;
55
64
56
    is(
65
    is(
57
        $info_log, $info_borrower1,
66
        $info_log, $info_borrower1,
58
        "GetLogs returns results in the log viewer for the merge of " . $borrower1->{borrowernumber}
67
        "ActionLogs returns results for the merge of "
68
            . $borrower1->{borrowernumber}
69
            . " into "
70
            . $keeper->borrowernumber
59
    );
71
    );
60
72
61
    my $info_borrower2 =
73
    my $info_borrower2 =
Lines 66-76 subtest 'Test Koha::Patrons::merge' => sub { Link Here
66
        . $keeper->firstname . " "
78
        . $keeper->firstname . " "
67
        . $keeper->surname . " ("
79
        . $keeper->surname . " ("
68
        . $keeper->cardnumber . ")";
80
        . $keeper->cardnumber . ")";
69
    $info_log = $log->[1]{info};
81
    $info_log = $logs->next->info;
70
82
71
    is(
83
    is(
72
        $info_log, $info_borrower2,
84
        $info_log, $info_borrower2,
73
        "GetLogs returns results in the log viewer for the merge of " . $borrower2->{borrowernumber}
85
        "ActionLogs returns results in the log viewer for the merge of "
86
            . $borrower2->{borrowernumber}
87
            . " into "
88
            . $keeper->borrowernumber
74
    );
89
    );
75
90
76
    #test with BorrowersLog off
91
    #test with BorrowersLog off
Lines 80-99 subtest 'Test Koha::Patrons::merge' => sub { Link Here
80
95
81
    $results = $keeper->merge_with( [ $borrower3->{borrowernumber}, $borrower4->{borrowernumber} ] );
96
    $results = $keeper->merge_with( [ $borrower3->{borrowernumber}, $borrower4->{borrowernumber} ] );
82
97
83
    $log      = GetLogs( "", "", "", ["MEMBERS"], ["PATRON_MERGE"], $keeper->id, "" );
98
    $logs = Koha::ActionLogs->search(
84
    $info_log = $log->[0]{info};
99
        {
85
100
            module => "MEMBERS",
86
    is(
101
            action => "PATRON_MERGE",
87
        $info_log, undef,
102
            object => $keeper->id
88
        "GetLogs didn't returns results in the log viewer for the merge of " . $borrower3->{borrowernumber}
103
        }
89
    );
104
    );
90
105
91
    $info_log = $log->[1]{info};
92
93
    is(
106
    is(
94
        $info_log, undef,
107
        $logs->count, 0,
95
        "GetLogs didn't returns results in the log viewer for the merge of " . $borrower4->{borrowernumber}
108
        "Koha::ActionLogs didn't returns results in the log viewer for the merge of " . $borrower3->{borrowernumber}
96
    );
109
    );
110
97
};
111
};
98
112
99
$schema->storage->txn_rollback;
113
$schema->storage->txn_rollback;
100
- 

Return to bug 22632