From 150a0af9d41fe1b35ae432a3b847bb68a0f46018 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Fri, 11 Dec 2015 13:59:26 +0100 Subject: [PATCH] Bug 15336 - Fix QA comments --- misc/migration_tools/merge_booksellers.pl | 104 +++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 27 deletions(-) diff --git a/misc/migration_tools/merge_booksellers.pl b/misc/migration_tools/merge_booksellers.pl index a9f6a5d..9ec664e 100644 --- a/misc/migration_tools/merge_booksellers.pl +++ b/misc/migration_tools/merge_booksellers.pl @@ -1,7 +1,23 @@ #!/usr/bin/perl -# Script that merge source bookseller (-f) into target bookseller (-t). -use strict; +# Copyright 2015 BibLibre +# +# 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. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; use Koha::Acquisition::Bookseller; use C4::Bookseller qw( DelBookseller ); @@ -9,19 +25,23 @@ use C4::Acquisition; use C4::Contract; use Getopt::Long; +use Pod::Usage; my ($help, $verbose, $mergefrom, $mergeto, $confirm); GetOptions( - 'h' => \$help, - 'v' => \$verbose, - 'f:s' => \$mergefrom, - 't:s' => \$mergeto, - 'c' => \$confirm, -); + 'help|h' => \$help, + 'verbose|v' => \$verbose, + 'from|f:s' => \$mergefrom, + 'to|t:s' => \$mergeto, + 'confirm|c' => \$confirm, +) || pod2usage(1); if ($help || !$mergefrom || !$mergeto) { - print_usage(); - die; + pod2usage(1); +} + +if ($mergefrom eq $mergeto) { + pod2usage('Source and target booksellers should be different.'); } my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom}); @@ -31,14 +51,15 @@ my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto}); die "Unknown bookseller id ($mergeto)" unless $to_bookseller; my $report; -foreach (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice)) { - my $rs = Koha::Database->new()->schema->resultset($_); - $rs = $rs->search({booksellerid => $mergefrom }); +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 "$_ n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose; + print "$entity n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose; $e->update() if $confirm; - $report->{$_}++; + $report->{$entity}++; } } @@ -48,18 +69,6 @@ print "\n" if $verbose; print_report($report); print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm; -sub print_usage { -print <{$entity}\n"; } } + + +=head1 NAME + +merge source bookseller (-f) into target bookseller (-t) + +=head1 SYNOPSIS + +merge_booksellers.pl [-h|--help] [-v|--verbose] [-c|--confirm] --from=booksellerid --to=booksellerid + +=head1 OPTIONS + +=over + +=item B<-h|--help> + +Print a brief help message. + +=item B<--verbose> + + --verbose Show more information on what is done. + +=item B<--confirm> + + --confirm Commit the changes in database. Running without this + parameter is like testing. It is recommanded to run without + --confirm first to be sure of the changes. + +=item B<--from> + + --from= The source bookseller identifier that will be merged in + target bookseller. + +=item B<--to> + + --to= The target bookseller identifier in which merge + source bookseller. + +=back + +=cut -- 1.7.10.4