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

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

Return to bug 15336