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
77
78
    my $from_vendor = Koha::Acquisition::Booksellers->find( $mergefrom );
79
    my $result = $from_vendor->merge_with($mergeto);
80
81
merge source vendor (-f) into 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 recommanded to run without
107
                        --confirm first to be sure of the changes.
108
109
=item B<--from>
110
111
 --from=<vendorid>  The source vendor identifier that will be merged in
112
                        target vendor.
113
114
=item B<--to>
115
116
 --to=<vendorid>    The target vendor identifier in which merge
117
                        source vendor.
118
119
=back
120
121
=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::Booksellers->find( $booksellerid1 );
51
52
my $result = eval{ $bookseller->merge_with(345876876); };
53
like($@, qr/Unknown target bookseller number: 345876876/, 'Merge with unknown bookseller should die.');
54
55
$result = $bookseller->merge_with($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(Koha::Acquisition::Bookseller::Contacts->find( $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 (-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 756-761 is( $first_contact->phone, Link Here
756
is( $contacts->count,
759
is( $contacts->count,
757
    1, 'Only one contact after modification' );
760
    1, 'Only one contact after modification' );
758
761
762
my $bookseller1   = create('Aqbookseller');
763
my $booksellerid1 = $bookseller1->{id};
764
my $bookseller2   = create('Aqbookseller');
765
my $booksellerid2 = $bookseller2->{id};
766
767
my $bg1 = create( 'Aqbasketgroup', $booksellerid1 );
768
my $bg2 = create( 'Aqbasketgroup', $booksellerid1 );
769
my $bg3 = create( 'Aqbasketgroup', $booksellerid2 );
770
771
my $basket1 = create( 'Aqbasket', $booksellerid1 );
772
my $basket2 = create( 'Aqbasket', $booksellerid1 );
773
774
my $contract = create( 'Aqcontract', $booksellerid1 );
775
776
my $contact = create( 'Aqcontact', $booksellerid1 );
777
778
my $invoice = create( 'Aqinvoice', $booksellerid1 );
779
780
my $deleteditem = create( 'Deleteditem', $booksellerid1 );
781
782
my $item1 = create( 'Item', $booksellerid1 );
783
784
my $subscription = create( 'Subscription', $booksellerid1 );
785
786
$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid1 );
787
788
my $result = eval { $bookseller->merge_with(345876876) };
789
ok(
790
    $@ =~ /^Unknown target bookseller number: 345876876/,
791
    'Merge to unknown bookseller return an error'
792
);
793
794
$result = $bookseller->merge_with($booksellerid2);
795
my $expected_result = {
796
    Aqbasketgroup => [ $bg1->{id},           $bg2->{id} ],
797
    Aqbasket      => [ $basket1->{basketno}, $basket2->{basketno} ],
798
    Aqcontract    => [ $contract->{contractnumber} ],
799
    Aqcontact     => [ $contact->{id} ],
800
    Aqinvoice     => [ $invoice->{invoiceid} ],
801
    Deleteditem   => [ $deleteditem->{itemnumber} ],
802
    Item          => [ $item1->{itemnumber} ],
803
    Subscription  => [ $subscription->{subscriptionid} ]
804
};
805
806
is_deeply( $result, $expected_result, 'Result should be as expected' );
807
808
is( GetBasketgroup( $bg1->{id} )->{'booksellerid'},
809
    $booksellerid2, 'Basket group 1 has bookseller 2' );
810
811
is( GetBasketgroup( $bg2->{id} )->{'booksellerid'},
812
    $booksellerid2, 'Basket group 2 has bookseller 2' );
813
814
is( GetBasketgroup( $bg3->{id} )->{'booksellerid'},
815
    $booksellerid2, 'Basket group 3 keep bookseller 2' );
816
817
is( GetBasket( $basket1->{basketno} )->{'booksellerid'},
818
    $booksellerid2, 'Basket 1 has bookseller 2' );
819
820
is( GetBasket( $basket2->{basketno} )->{'booksellerid'},
821
    $booksellerid2, 'Basket 2 has bookseller 2' );
822
823
is(
824
    GetContract( { contractnumber => $contract->{contractnumber} } )
825
      ->{'booksellerid'},
826
    $booksellerid2,
827
    'Basket 1 has bookseller 2'
828
);
829
830
is( Koha::Acquisition::Bookseller::Contacts->find( $contact->{id} )->booksellerid,
831
    $booksellerid2, 'Contact 1 has booksellerid 2' );
832
833
is( GetInvoice( $invoice->{invoiceid} )->{'booksellerid'},
834
    $booksellerid2, 'Invoice has bookseller 2' );
835
836
my $item = Koha::Database->new()->schema->resultset('Deleteditem')
837
  ->find( { itemnumber => $deleteditem->{itemnumber} } );
838
is( $item->get_column('booksellerid'),
839
    $booksellerid2, 'Deleted item has bookseller 2' );
840
841
is( GetItem( $item1->{itemnumber} )->{'booksellerid'},
842
    $booksellerid2, 'Item has bookseller 2' );
843
844
is( GetSubscription( $subscription->{subscriptionid} )->{'aqbooksellerid'},
845
    $booksellerid2, 'Subscription has bookseller 2' );
846
759
#End transaction
847
#End transaction
760
$schema->storage->txn_rollback();
848
$schema->storage->txn_rollback();
761
849
Lines 779-781 sub field_filter { Link Here
779
    }
867
    }
780
    return $struct;
868
    return $struct;
781
}
869
}
782
- 
870
871
sub create {
872
    my ( $source, $booksellerid ) = @_;
873
874
    my $build = { source => $source };
875
    my $foreign_key =
876
      $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
877
    $build->{value} = { $foreign_key => $booksellerid } if $booksellerid;
878
879
    my $entity = $builder->build($build);
880
881
    return $entity;
882
}

Return to bug 15336