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

(-)a/Koha/Acquisition/Bookseller.pm (+29 lines)
Lines 26-31 sub subscriptions { Link Here
26
    return Koha::Subscriptions->search( { aqbooksellerid => $self->id } );
26
    return Koha::Subscriptions->search( { aqbooksellerid => $self->id } );
27
}
27
}
28
28
29
sub merge_with {
30
    my ($self, $booksellerid) = @_;
31
32
    my $result = {};
33
34
    unless ( Koha::Acquisition::Booksellers->find( $booksellerid ) ) {
35
        die("Unknown target bookseller number: $booksellerid");
36
    }
37
38
    foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) {
39
        my $rs = Koha::Database->new()->schema->resultset($entity);
40
41
        my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
42
        $rs = $rs->search({$foreign_key => $self->id });
43
44
        while (my $e = $rs->next) {
45
            $e->set_column($foreign_key, $booksellerid);
46
            $e->update();
47
            push @{ $result->{$entity} }, $e->id();
48
        }
49
    }
50
    return $result;
51
}
52
53
# TODO Move code from ModBookseller
54
sub update {
55
    die "not implemented yet";
56
}
57
29
sub _type {
58
sub _type {
30
    return 'Aqbookseller';
59
    return 'Aqbookseller';
31
}
60
}
(-)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 11-19 use Koha::DateUtils; Link Here
11
use DateTime::Duration;
11
use DateTime::Duration;
12
use t::lib::Mocks;
12
use t::lib::Mocks;
13
use C4::Acquisition;
13
use C4::Acquisition;
14
use C4::Contract qw( GetContract );
15
use C4::Items qw ( GetItem );
14
use C4::Serials;
16
use C4::Serials;
15
use C4::Budgets;
17
use C4::Budgets;
16
use C4::Biblio;
18
use C4::Biblio;
19
use t::lib::TestBuilder;
17
20
18
use Koha::Acquisition::Booksellers;
21
use Koha::Acquisition::Booksellers;
19
use Koha::Acquisition::Order;
22
use Koha::Acquisition::Order;
Lines 38-43 $schema->storage->txn_begin(); Link Here
38
41
39
$dbh->{RaiseError} = 1;
42
$dbh->{RaiseError} = 1;
40
43
44
our $builder = t::lib::TestBuilder->new;
45
41
#Start tests
46
#Start tests
42
$dbh->do(q|DELETE FROM aqorders|);
47
$dbh->do(q|DELETE FROM aqorders|);
43
$dbh->do(q|DELETE FROM aqbasket|);
48
$dbh->do(q|DELETE FROM aqbasket|);
Lines 751-756 is( $first_contact->phone, Link Here
751
is( $contacts->count,
756
is( $contacts->count,
752
    1, 'Only one contact after modification' );
757
    1, 'Only one contact after modification' );
753
758
759
my $bookseller1   = create('Aqbookseller');
760
my $booksellerid1 = $bookseller1->{id};
761
my $bookseller2   = create('Aqbookseller');
762
my $booksellerid2 = $bookseller2->{id};
763
764
my $bg1 = create( 'Aqbasketgroup', $booksellerid1 );
765
my $bg2 = create( 'Aqbasketgroup', $booksellerid1 );
766
my $bg3 = create( 'Aqbasketgroup', $booksellerid2 );
767
768
my $basket1 = create( 'Aqbasket', $booksellerid1 );
769
my $basket2 = create( 'Aqbasket', $booksellerid1 );
770
771
my $contract = create( 'Aqcontract', $booksellerid1 );
772
773
my $contact = create( 'Aqcontact', $booksellerid1 );
774
775
my $invoice = create( 'Aqinvoice', $booksellerid1 );
776
777
my $deleteditem = create( 'Deleteditem', $booksellerid1 );
778
779
my $item1 = create( 'Item', $booksellerid1 );
780
781
my $subscription = create( 'Subscription', $booksellerid1 );
782
783
$bookseller = Koha::Acquisition::Bookseller->fetch( { id => $booksellerid1 } );
784
785
my $result = eval { $bookseller->merge_with(345876876) };
786
ok(
787
    $@ =~ /^Unknown target bookseller number: 345876876/,
788
    'Merge to unknown bookseller return an error'
789
);
790
791
$result = $bookseller->merge_with($booksellerid2);
792
my $expected_result = {
793
    Aqbasketgroup => [ $bg1->{id},           $bg2->{id} ],
794
    Aqbasket      => [ $basket1->{basketno}, $basket2->{basketno} ],
795
    Aqcontract    => [ $contract->{contractnumber} ],
796
    Aqcontact     => [ $contact->{id} ],
797
    Aqinvoice     => [ $invoice->{invoiceid} ],
798
    Deleteditem   => [ $deleteditem->{itemnumber} ],
799
    Item          => [ $item1->{itemnumber} ],
800
    Subscription  => [ $subscription->{subscriptionid} ]
801
};
802
803
is_deeply( $result, $expected_result, 'Result should be as expected' );
804
805
is( GetBasketgroup( $bg1->{id} )->{'booksellerid'},
806
    $booksellerid2, 'Basket group 1 has bookseller 2' );
807
808
is( GetBasketgroup( $bg2->{id} )->{'booksellerid'},
809
    $booksellerid2, 'Basket group 2 has bookseller 2' );
810
811
is( GetBasketgroup( $bg3->{id} )->{'booksellerid'},
812
    $booksellerid2, 'Basket group 3 keep bookseller 2' );
813
814
is( GetBasket( $basket1->{basketno} )->{'booksellerid'},
815
    $booksellerid2, 'Basket 1 has bookseller 2' );
816
817
is( GetBasket( $basket2->{basketno} )->{'booksellerid'},
818
    $booksellerid2, 'Basket 2 has bookseller 2' );
819
820
is(
821
    GetContract( { contractnumber => $contract->{contractnumber} } )
822
      ->{'booksellerid'},
823
    $booksellerid2,
824
    'Basket 1 has bookseller 2'
825
);
826
827
is( C4::Bookseller::Contact->fetch( $contact->{id} )->{'booksellerid'},
828
    $booksellerid2, 'Contact 1 has booksellerid 2' );
829
830
is( GetInvoice( $invoice->{invoiceid} )->{'booksellerid'},
831
    $booksellerid2, 'Invoice has bookseller 2' );
832
833
my $item = Koha::Database->new()->schema->resultset('Deleteditem')
834
  ->find( { itemnumber => $deleteditem->{itemnumber} } );
835
is( $item->get_column('booksellerid'),
836
    $booksellerid2, 'Deleted item has bookseller 2' );
837
838
is( GetItem( $item1->{itemnumber} )->{'booksellerid'},
839
    $booksellerid2, 'Item has bookseller 2' );
840
841
is( GetSubscription( $subscription->{subscriptionid} )->{'aqbooksellerid'},
842
    $booksellerid2, 'Subscription has bookseller 2' );
843
754
#End transaction
844
#End transaction
755
$schema->storage->txn_rollback();
845
$schema->storage->txn_rollback();
756
846
Lines 774-776 sub field_filter { Link Here
774
    }
864
    }
775
    return $struct;
865
    return $struct;
776
}
866
}
777
- 
867
868
sub create {
869
    my ( $source, $booksellerid ) = @_;
870
871
    my $build = { source => $source };
872
    my $foreign_key =
873
      $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
874
    $build->{value} = { $foreign_key => $booksellerid } if $booksellerid;
875
876
    my $entity = $builder->build($build);
877
878
    return $entity;
879
}

Return to bug 15336