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

(-)a/Koha/Acquisition/Bookseller.pm (+38 lines)
Lines 73-84 sub subscriptions { Link Here
73
    return Koha::Subscriptions->search( { aqbooksellerid => $self->id } );
73
    return Koha::Subscriptions->search( { aqbooksellerid => $self->id } );
74
}
74
}
75
75
76
=head3 merge_with
77
78
Merges the records related to the given vendor with this vendor
79
80
=cut
81
82
sub merge_with {
83
    my ($self, $booksellerid) = @_;
84
85
    my $result = {};
86
87
    unless ( Koha::Acquisition::Booksellers->find( $booksellerid ) ) {
88
        die("Unknown target bookseller number: $booksellerid");
89
    }
90
91
    foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) {
92
        my $rs = Koha::Database->new()->schema->resultset($entity);
93
94
        my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
95
        $rs = $rs->search({$foreign_key => $self->id });
96
97
        while (my $e = $rs->next) {
98
            $e->set_column($foreign_key, $booksellerid);
99
            $e->update();
100
            push @{ $result->{$entity} }, $e->id();
101
        }
102
    }
103
    return $result;
104
}
105
76
=head2 Internal methods
106
=head2 Internal methods
77
107
108
=cut
109
110
78
=head3 _type
111
=head3 _type
79
112
80
=cut
113
=cut
81
114
115
# TODO Move code from ModBookseller
116
sub update {
117
    die "not implemented yet";
118
}
119
82
sub _type {
120
sub _type {
83
    return 'Aqbookseller';
121
    return 'Aqbookseller';
84
}
122
}
(-)a/misc/migration_tools/merge_vendors.pl (+120 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2015 BibLibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Koha::Acquisition::Bookseller;
23
use C4::Acquisition;
24
use C4::Contract;
25
26
use Getopt::Long;
27
use Pod::Usage;
28
29
my ($help, $verbose, $mergefrom, $mergeto, $confirm);
30
GetOptions(
31
    'help|h' => \$help,
32
    'verbose|v' => \$verbose,
33
    'from|f:s' => \$mergefrom,
34
    'to|t:s' => \$mergeto,
35
    'confirm|c' => \$confirm,
36
) || pod2usage(1);
37
38
if ($help || !$mergefrom || !$mergeto) {
39
    pod2usage(1);
40
}
41
42
if ($mergefrom eq $mergeto) {
43
    pod2usage('Source and target vendors should be different.');
44
}
45
46
my $schema = Koha::Database->schema;
47
$schema->storage->txn_begin;
48
49
my $from_vendor = Koha::Acquisition::Booksellers->find( $mergefrom );
50
die "Unknown vendor id ($mergefrom)" unless $from_vendor;
51
52
my $report = $from_vendor->merge_with($mergeto);
53
54
if ($confirm) {
55
    $schema->storage->txn_commit;
56
}
57
else {
58
    $schema->storage->txn_rollback;
59
}
60
61
Koha::Acquisition::Booksellers->find($mergefrom)->delete if $confirm;
62
63
if ($verbose) {
64
    print_report($report);
65
}
66
67
print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm;
68
69
sub print_report {
70
    my $report = shift;
71
72
    foreach my $entity (keys %$report) {
73
        my $count = scalar @{ $report->{$entity} };
74
        my $ids = join(', ', @{ $report->{$entity} });
75
        print "$entity merged: $count (numbers: $ids)\n";
76
    }
77
}
78
79
80
=head1 NAME
81
82
merge source vendor (-f) into target vendor (-t)
83
84
=head1 SYNOPSIS
85
86
** WARNING ** This script can delete data. You must only use it if you know what your are doing
87
88
merge_vendors.pl [-h|--help] [-v|--verbose] [-c|--confirm] --from=vendorid --to=vendorid
89
90
=head1 OPTIONS
91
92
=over
93
94
=item B<-h|--help>
95
96
Print a brief help message.
97
98
=item B<--verbose>
99
100
 --verbose              Show more information on what is done.
101
102
=item B<--confirm>
103
104
 --confirm              Commit the changes in database. Running without this
105
                        parameter is like testing. It is recommanded to run without
106
                        --confirm first to be sure of the changes.
107
108
=item B<--from>
109
110
 --from=<vendorid>  The source vendor identifier that will be merged in
111
                        target vendor.
112
113
=item B<--to>
114
115
 --to=<vendorid>    The target vendor identifier in which merge
116
                        source vendor.
117
118
=back
119
120
=cut
(-)a/t/Bookseller.t (-3 / +96 lines)
Lines 1-14 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
#
2
#
3
# This Koha test module is a stub!  
3
# This Koha test module is a stub!
4
# Add more tests here!!!
4
# Add more tests here!!!
5
5
6
use strict;
6
use strict;
7
use warnings;
7
use warnings;
8
8
9
use Test::More tests => 1;
9
use C4::Acquisition qw( GetBasketgroup GetBasket GetInvoice );
10
use C4::Contract qw( GetContract );
11
use C4::Items qw ( GetItem );
12
use C4::Serials qw( GetSubscription );
13
use t::lib::TestBuilder;
14
15
use Test::More tests => 14;
10
16
11
BEGIN {
17
BEGIN {
12
        use_ok('C4::Bookseller');
18
        use_ok('Koha::Acquisition::Bookseller');
19
}
20
21
my $schema = Koha::Database->schema;
22
$schema->storage->txn_begin;
23
24
our $builder = t::lib::TestBuilder->new;
25
26
my $bookseller1 = create('Aqbookseller');
27
my $booksellerid1 = $bookseller1->{id};
28
my $bookseller2 = create('Aqbookseller');
29
my $booksellerid2 = $bookseller2->{id};
30
31
my $bg1 = create('Aqbasketgroup', $booksellerid1);
32
my $bg2 = create('Aqbasketgroup', $booksellerid1);
33
my $bg3 = create('Aqbasketgroup', $booksellerid2);
34
35
my $basket1 = create('Aqbasket', $booksellerid1);
36
my $basket2 = create('Aqbasket', $booksellerid1);
37
38
my $contract = create('Aqcontract', $booksellerid1);
39
40
my $contact = create('Aqcontact', $booksellerid1);
41
42
my $invoice = create('Aqinvoice', $booksellerid1);
43
44
my $deleteditem = create('Deleteditem', $booksellerid1);
45
46
my $item1 = create('Item', $booksellerid1);
47
48
my $subscription = create('Subscription', $booksellerid1);
49
50
my $bookseller = Koha::Acquisition::Bookseller->fetch({id => $booksellerid1});
51
52
my ($error, $result) = $bookseller->merge_to(345876876);
53
is($error, "Unknown target bookseller number: 345876876", 'Merge to unknown bookseller return an error' );
54
55
($error, $result) = $bookseller->merge_to($booksellerid2);
56
my $expected_result = {
57
    Aqbasketgroup => [$bg1->{id}, $bg2->{id}],
58
    Aqbasket => [$basket1->{basketno}, $basket2->{basketno}],
59
    Aqcontract => [$contract->{contractnumber}],
60
    Aqcontact => [$contact->{id}],
61
    Aqinvoice => [$invoice->{invoiceid}],
62
    Deleteditem => [$deleteditem->{itemnumber}],
63
    Item => [$item1->{itemnumber}],
64
    Subscription => [$subscription->{subscriptionid}]
65
};
66
67
is_deeply($result, $expected_result, 'Result should be as expected');
68
69
is(GetBasketgroup($bg1->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 1 has bookseller 2');
70
71
is(GetBasketgroup($bg2->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 2 has bookseller 2');
72
73
is(GetBasketgroup($bg3->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 3 keep bookseller 2');
74
75
is(GetBasket($basket1->{basketno})->{'booksellerid'}, $booksellerid2, 'Basket 1 has bookseller 2');
76
77
is(GetBasket($basket2->{basketno})->{'booksellerid'}, $booksellerid2, 'Basket 2 has bookseller 2');
78
79
is(GetContract({contractnumber => $contract->{contractnumber}})->{'booksellerid'}, $booksellerid2, 'Basket 1 has bookseller 2');
80
81
is(C4::Bookseller::Contact->fetch($contact->{id})->{'booksellerid'}, $booksellerid2, 'Contact 1 has booksellerid 2');
82
83
is(GetInvoice($invoice->{invoiceid})->{'booksellerid'}, $booksellerid2, 'Invoice has bookseller 2');
84
85
my $item = Koha::Database->new()->schema->resultset('Deleteditem')
86
    ->find({itemnumber => $deleteditem->{itemnumber}});
87
is($item->get_column('booksellerid'), $booksellerid2, 'Deleted item has bookseller 2');
88
89
is(GetItem($item1->{itemnumber})->{'booksellerid'}, $booksellerid2, 'Item has bookseller 2');
90
91
is(GetSubscription($subscription->{subscriptionid})->{'aqbooksellerid'}, $booksellerid2, 'Subscription has bookseller 2');
92
93
$schema->storage->txn_rollback;
94
95
sub create {
96
    my ($source, $booksellerid) = @_;
97
98
    my $build = { source => $source};
99
    my $foreign_key = $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
100
    $build->{value} = {$foreign_key => $booksellerid} if $booksellerid;
101
102
    my $entity = $builder->build($build);
103
104
    return $entity;
13
}
105
}
14
106
107
1;
(-)a/t/db_dependent/Bookseller.t (-2 / +104 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
4
5
use Test::More tests => 86;
5
use Test::More tests => 99;
6
use Test::MockModule;
6
use Test::MockModule;
7
use Test::Warn;
7
use Test::Warn;
8
8
Lines 13-21 use Koha::DateUtils; Link Here
13
use DateTime::Duration;
13
use DateTime::Duration;
14
use t::lib::Mocks;
14
use t::lib::Mocks;
15
use C4::Acquisition;
15
use C4::Acquisition;
16
use C4::Contract qw( GetContract );
17
use C4::Items qw ( GetItem );
16
use C4::Serials;
18
use C4::Serials;
17
use C4::Budgets;
19
use C4::Budgets;
18
use C4::Biblio;
20
use C4::Biblio;
21
use t::lib::TestBuilder;
19
22
20
use Koha::Acquisition::Booksellers;
23
use Koha::Acquisition::Booksellers;
21
use Koha::Acquisition::Order;
24
use Koha::Acquisition::Order;
Lines 40-45 $schema->storage->txn_begin(); Link Here
40
$dbh->{RaiseError} = 1;
43
$dbh->{RaiseError} = 1;
41
my $builder = t::lib::TestBuilder->new;
44
my $builder = t::lib::TestBuilder->new;
42
45
46
our $builder = t::lib::TestBuilder->new;
47
43
#Start tests
48
#Start tests
44
$dbh->do(q|DELETE FROM aqorders|);
49
$dbh->do(q|DELETE FROM aqorders|);
45
$dbh->do(q|DELETE FROM aqbasket|);
50
$dbh->do(q|DELETE FROM aqbasket|);
Lines 756-761 is( $first_contact->phone, Link Here
756
is( $contacts->count,
761
is( $contacts->count,
757
    1, 'Only one contact after modification' );
762
    1, 'Only one contact after modification' );
758
763
764
my $bookseller1   = create('Aqbookseller');
765
my $booksellerid1 = $bookseller1->{id};
766
my $bookseller2   = create('Aqbookseller');
767
my $booksellerid2 = $bookseller2->{id};
768
769
my $bg1 = create( 'Aqbasketgroup', $booksellerid1 );
770
my $bg2 = create( 'Aqbasketgroup', $booksellerid1 );
771
my $bg3 = create( 'Aqbasketgroup', $booksellerid2 );
772
773
my $basket1 = create( 'Aqbasket', $booksellerid1 );
774
my $basket2 = create( 'Aqbasket', $booksellerid1 );
775
776
my $contract = create( 'Aqcontract', $booksellerid1 );
777
778
my $contact = create( 'Aqcontact', $booksellerid1 );
779
780
my $invoice = create( 'Aqinvoice', $booksellerid1 );
781
782
my $deleteditem = create( 'Deleteditem', $booksellerid1 );
783
784
my $item1 = create( 'Item', $booksellerid1 );
785
786
my $subscription = create( 'Subscription', $booksellerid1 );
787
788
$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid1 );
789
790
my $result = eval { $bookseller->merge_with(345876876) };
791
ok(
792
    $@ =~ /^Unknown target bookseller number: 345876876/,
793
    'Merge to unknown bookseller return an error'
794
);
795
796
$result = $bookseller->merge_with($booksellerid2);
797
my $expected_result = {
798
    Aqbasketgroup => [ $bg1->{id},           $bg2->{id} ],
799
    Aqbasket      => [ $basket1->{basketno}, $basket2->{basketno} ],
800
    Aqcontract    => [ $contract->{contractnumber} ],
801
    Aqcontact     => [ $contact->{id} ],
802
    Aqinvoice     => [ $invoice->{invoiceid} ],
803
    Deleteditem   => [ $deleteditem->{itemnumber} ],
804
    Item          => [ $item1->{itemnumber} ],
805
    Subscription  => [ $subscription->{subscriptionid} ]
806
};
807
808
is_deeply( $result, $expected_result, 'Result should be as expected' );
809
810
is( GetBasketgroup( $bg1->{id} )->{'booksellerid'},
811
    $booksellerid2, 'Basket group 1 has bookseller 2' );
812
813
is( GetBasketgroup( $bg2->{id} )->{'booksellerid'},
814
    $booksellerid2, 'Basket group 2 has bookseller 2' );
815
816
is( GetBasketgroup( $bg3->{id} )->{'booksellerid'},
817
    $booksellerid2, 'Basket group 3 keep bookseller 2' );
818
819
is( GetBasket( $basket1->{basketno} )->{'booksellerid'},
820
    $booksellerid2, 'Basket 1 has bookseller 2' );
821
822
is( GetBasket( $basket2->{basketno} )->{'booksellerid'},
823
    $booksellerid2, 'Basket 2 has bookseller 2' );
824
825
is(
826
    GetContract( { contractnumber => $contract->{contractnumber} } )
827
      ->{'booksellerid'},
828
    $booksellerid2,
829
    'Basket 1 has bookseller 2'
830
);
831
832
is( Koha::Acquisition::Bookseller::Contacts->find( $contact->{id} )->booksellerid,
833
    $booksellerid2, 'Contact 1 has booksellerid 2' );
834
835
is( GetInvoice( $invoice->{invoiceid} )->{'booksellerid'},
836
    $booksellerid2, 'Invoice has bookseller 2' );
837
838
my $item = Koha::Database->new()->schema->resultset('Deleteditem')
839
  ->find( { itemnumber => $deleteditem->{itemnumber} } );
840
is( $item->get_column('booksellerid'),
841
    $booksellerid2, 'Deleted item has bookseller 2' );
842
843
is( GetItem( $item1->{itemnumber} )->{'booksellerid'},
844
    $booksellerid2, 'Item has bookseller 2' );
845
846
is( GetSubscription( $subscription->{subscriptionid} )->{'aqbooksellerid'},
847
    $booksellerid2, 'Subscription has bookseller 2' );
848
759
#End transaction
849
#End transaction
760
$schema->storage->txn_rollback();
850
$schema->storage->txn_rollback();
761
851
Lines 779-781 sub field_filter { Link Here
779
    }
869
    }
780
    return $struct;
870
    return $struct;
781
}
871
}
782
- 
872
873
sub create {
874
    my ( $source, $booksellerid ) = @_;
875
876
    my $build = { source => $source };
877
    my $foreign_key =
878
      $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
879
    $build->{value} = { $foreign_key => $booksellerid } if $booksellerid;
880
881
    my $entity = $builder->build($build);
882
883
    return $entity;
884
}

Return to bug 15336