|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# Script that merge source bookseller (-f) into target bookseller (-t). |
| 3 |
|
| 4 |
use strict; |
| 5 |
|
| 6 |
use Koha::Acquisition::Bookseller; |
| 7 |
use C4::Bookseller qw( DelBookseller ); |
| 8 |
use C4::Acquisition; |
| 9 |
use C4::Contract; |
| 10 |
|
| 11 |
use Getopt::Long; |
| 12 |
|
| 13 |
my ($help, $verbose, $mergefrom, $mergeto, $confirm); |
| 14 |
GetOptions( |
| 15 |
'h' => \$help, |
| 16 |
'v' => \$verbose, |
| 17 |
'f:s' => \$mergefrom, |
| 18 |
't:s' => \$mergeto, |
| 19 |
'c' => \$confirm, |
| 20 |
); |
| 21 |
|
| 22 |
if ($help || !$mergefrom || !$mergeto) { |
| 23 |
print_usage(); |
| 24 |
die; |
| 25 |
} |
| 26 |
|
| 27 |
my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom}); |
| 28 |
die "Unknown bookseller id ($mergefrom)" unless $from_bookseller; |
| 29 |
|
| 30 |
my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto}); |
| 31 |
die "Unknown bookseller id ($mergeto)" unless $to_bookseller; |
| 32 |
|
| 33 |
my $report; |
| 34 |
foreach (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice)) { |
| 35 |
my $rs = Koha::Database->new()->schema->resultset($_); |
| 36 |
$rs = $rs->search({booksellerid => $mergefrom }); |
| 37 |
while (my $e = $rs->next) { |
| 38 |
$e->set_column('booksellerid', $mergeto); |
| 39 |
print "$_ n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose; |
| 40 |
$e->update() if $confirm; |
| 41 |
$report->{$_}++; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
DelBookseller($mergefrom) if $confirm; |
| 46 |
|
| 47 |
print "\n" if $verbose; |
| 48 |
print_report($report); |
| 49 |
print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm; |
| 50 |
|
| 51 |
sub print_usage { |
| 52 |
print <<EOF |
| 53 |
Script to merge a bookselle into another |
| 54 |
parameters : |
| 55 |
\th : this help screen |
| 56 |
\tf : the bookseller id to merge. |
| 57 |
\tt : the bookseller id where to merge |
| 58 |
\tc : Perform changes in database. |
| 59 |
EOF |
| 60 |
; |
| 61 |
} |
| 62 |
|
| 63 |
sub print_report { |
| 64 |
my $report = shift; |
| 65 |
|
| 66 |
foreach my $entity (keys %$report) { |
| 67 |
print "$entity merged: $report->{$entity}\n"; |
| 68 |
} |
| 69 |
} |