@@ -, +, @@ Koha::Acquisition::Bookseller.pm --- Koha/Acquisition/Bookseller.pm | 26 +++++++ misc/migration_tools/merge_booksellers.pl | 38 +++++----- t/Bookseller.t | 113 ++++++++++++++++++++++++++++- 3 files changed, 156 insertions(+), 21 deletions(-) --- a/Koha/Acquisition/Bookseller.pm +++ a/Koha/Acquisition/Bookseller.pm @@ -111,6 +111,32 @@ sub insert { return $self; } +sub merge_to { + my ($self, $mergeto) = @_; + + my $error = ''; + my $result = {}; + + unless (my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto})) { + $error = "Unknown target bookseller number: $mergeto"; + return ($error, {}); + } + + foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) { + my $rs = Koha::Database->new()->schema->resultset($entity); + + my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; + $rs = $rs->search({$foreign_key => $self->{id} }); + + while (my $e = $rs->next) { + $e->set_column($foreign_key, $mergeto); + $e->update(); + push @{ $result->{$entity} }, $e->id(); + } + } + return ($error, $result); +} + # TODO Move code from ModBookseller sub update { die "not implemented yet"; --- a/misc/migration_tools/merge_booksellers.pl +++ a/misc/migration_tools/merge_booksellers.pl @@ -44,36 +44,40 @@ if ($mergefrom eq $mergeto) { pod2usage('Source and target booksellers should be different.'); } +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; + my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom}); die "Unknown bookseller id ($mergefrom)" unless $from_bookseller; -my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto}); -die "Unknown bookseller id ($mergeto)" unless $to_bookseller; - -my $report; -foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) { - my $rs = Koha::Database->new()->schema->resultset($entity); - my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; - $rs = $rs->search({$foreign_key => $mergefrom }); - while (my $e = $rs->next) { - $e->set_column('booksellerid', $mergeto); - print "$entity n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose; - $e->update() if $confirm; - $report->{$entity}++; - } +my ($error, $report) = $from_bookseller->merge_to($mergeto); + +if ($error) { + die $error; +} + +if ($confirm) { + $schema->storage->txn_commit; +} +else { + $schema->storage->txn_rollback; } DelBookseller($mergefrom) if $confirm; -print "\n" if $verbose; -print_report($report); +if ($verbose) { + print_report($report); +} + print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm; sub print_report { my $report = shift; foreach my $entity (keys %$report) { - print "$entity merged: $report->{$entity}\n"; + my $count = scalar @{ $report->{$entity} }; + my $ids = join(', ', @{ $report->{$entity} }); + print "$entity merged: $count (numbers: $ids)\n"; } } --- a/t/Bookseller.t +++ a/t/Bookseller.t @@ -1,14 +1,119 @@ #!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# This Koha test module is a stub! -# Add more tests here!!! +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use strict; use warnings; -use Test::More tests => 1; +use C4::Acquisition qw( GetBasketgroup GetBasket GetInvoice ); +use C4::Contract qw( GetContract ); +use C4::Items qw ( GetItem ); +use C4::Serials qw( GetSubscription ); +use t::lib::TestBuilder; + +use Test::More tests => 14; BEGIN { - use_ok('C4::Bookseller'); + use_ok('Koha::Acquisition::Bookseller'); +} + +my $schema = Koha::Database->schema; +$schema->storage->txn_begin; + +our $builder = t::lib::TestBuilder->new; + +my $bookseller1 = create('Aqbookseller'); +my $booksellerid1 = $bookseller1->{id}; +my $bookseller2 = create('Aqbookseller'); +my $booksellerid2 = $bookseller2->{id}; + +my $bg1 = create('Aqbasketgroup', $booksellerid1); +my $bg2 = create('Aqbasketgroup', $booksellerid1); +my $bg3 = create('Aqbasketgroup', $booksellerid2); + +my $basket1 = create('Aqbasket', $booksellerid1); +my $basket2 = create('Aqbasket', $booksellerid1); + +my $contract = create('Aqcontract', $booksellerid1); + +my $contact = create('Aqcontact', $booksellerid1); + +my $invoice = create('Aqinvoice', $booksellerid1); + +my $deleteditem = create('Deleteditem', $booksellerid1); + +my $item1 = create('Item', $booksellerid1); + +my $subscription = create('Subscription', $booksellerid1); + +my $bookseller = Koha::Acquisition::Bookseller->fetch({id => $booksellerid1}); + +my ($error, $result) = $bookseller->merge_to(345876876); +is($error, "Unknown target bookseller number: 345876876", 'Merge to unknown bookseller return an error' ); + +($error, $result) = $bookseller->merge_to($booksellerid2); +my $expected_result = { + Aqbasketgroup => [$bg1->{id}, $bg2->{id}], + Aqbasket => [$basket1->{basketno}, $basket2->{basketno}], + Aqcontract => [$contract->{contractnumber}], + Aqcontact => [$contact->{id}], + Aqinvoice => [$invoice->{invoiceid}], + Deleteditem => [$deleteditem->{itemnumber}], + Item => [$item1->{itemnumber}], + Subscription => [$subscription->{subscriptionid}] +}; + +is_deeply($result, $expected_result, 'Result should be as expected'); + +is(GetBasketgroup($bg1->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 1 has bookseller 2'); + +is(GetBasketgroup($bg2->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 2 has bookseller 2'); + +is(GetBasketgroup($bg3->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 3 keep bookseller 2'); + +is(GetBasket($basket1->{basketno})->{'booksellerid'}, $booksellerid2, 'Basket 1 has bookseller 2'); + +is(GetBasket($basket2->{basketno})->{'booksellerid'}, $booksellerid2, 'Basket 2 has bookseller 2'); + +is(GetContract({contractnumber => $contract->{contractnumber}})->{'booksellerid'}, $booksellerid2, 'Basket 1 has bookseller 2'); + +is(C4::Bookseller::Contact->fetch($contact->{id})->{'booksellerid'}, $booksellerid2, 'Contact 1 has booksellerid 2'); + +is(GetInvoice($invoice->{invoiceid})->{'booksellerid'}, $booksellerid2, 'Invoice has bookseller 2'); + +my $item = Koha::Database->new()->schema->resultset('Deleteditem') + ->find({itemnumber => $deleteditem->{itemnumber}}); +is($item->get_column('booksellerid'), $booksellerid2, 'Deleted item has bookseller 2'); + +is(GetItem($item1->{itemnumber})->{'booksellerid'}, $booksellerid2, 'Item has bookseller 2'); + +is(GetSubscription($subscription->{subscriptionid})->{'aqbooksellerid'}, $booksellerid2, 'Subscription has bookseller 2'); + +$schema->storage->txn_rollback; + +sub create { + my ($source, $booksellerid) = @_; + + my $build = { source => $source}; + my $foreign_key = $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; + $build->{value} = {$foreign_key => $booksellerid} if $booksellerid; + + my $entity = $builder->build($build); + + return $entity; } +1; --