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

(-)a/misc/migration_tools/merge_booksellers.pl (-28 / +77 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
# Script that merge source bookseller (-f) into target bookseller (-t).
3
2
4
use strict;
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;
5
21
6
use Koha::Acquisition::Bookseller;
22
use Koha::Acquisition::Bookseller;
7
use C4::Bookseller qw( DelBookseller );
23
use C4::Bookseller qw( DelBookseller );
Lines 9-27 use C4::Acquisition; Link Here
9
use C4::Contract;
25
use C4::Contract;
10
26
11
use Getopt::Long;
27
use Getopt::Long;
28
use Pod::Usage;
12
29
13
my ($help, $verbose, $mergefrom, $mergeto, $confirm);
30
my ($help, $verbose, $mergefrom, $mergeto, $confirm);
14
GetOptions(
31
GetOptions(
15
    'h' => \$help,
32
    'help|h' => \$help,
16
    'v' => \$verbose,
33
    'verbose|v' => \$verbose,
17
    'f:s' => \$mergefrom,
34
    'from|f:s' => \$mergefrom,
18
    't:s' => \$mergeto,
35
    'to|t:s' => \$mergeto,
19
    'c' => \$confirm,
36
    'confirm|c' => \$confirm,
20
);
37
) || pod2usage(1);
21
38
22
if ($help || !$mergefrom || !$mergeto) {
39
if ($help || !$mergefrom || !$mergeto) {
23
    print_usage();
40
    pod2usage(1);
24
    die;
41
}
42
43
if ($mergefrom eq $mergeto) {
44
    pod2usage('Source and target booksellers should be different.');
25
}
45
}
26
46
27
my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom});
47
my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom});
Lines 31-44 my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto}); Link Here
31
die "Unknown bookseller id ($mergeto)" unless $to_bookseller;
51
die "Unknown bookseller id ($mergeto)" unless $to_bookseller;
32
52
33
my $report;
53
my $report;
34
foreach (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice)) {
54
foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) {
35
    my $rs = Koha::Database->new()->schema->resultset($_);
55
    my $rs = Koha::Database->new()->schema->resultset($entity);
36
    $rs = $rs->search({booksellerid => $mergefrom });
56
    my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid';
57
    $rs = $rs->search({$foreign_key => $mergefrom });
37
    while (my $e = $rs->next) {
58
    while (my $e = $rs->next) {
38
        $e->set_column('booksellerid', $mergeto);
59
        $e->set_column('booksellerid', $mergeto);
39
        print "$_ n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose;
60
        print "$entity n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose;
40
        $e->update() if $confirm;
61
        $e->update() if $confirm;
41
        $report->{$_}++;
62
        $report->{$entity}++;
42
    }
63
    }
43
}
64
}
44
65
Lines 48-65 print "\n" if $verbose; Link Here
48
print_report($report);
69
print_report($report);
49
print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm;
70
print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm;
50
71
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 {
72
sub print_report {
64
    my $report = shift;
73
    my $report = shift;
65
74
Lines 67-69 sub print_report { Link Here
67
        print "$entity merged: $report->{$entity}\n";
76
        print "$entity merged: $report->{$entity}\n";
68
    }
77
    }
69
}
78
}
70
- 
79
80
81
=head1 NAME
82
83
merge source bookseller (-f) into target bookseller (-t)
84
85
=head1 SYNOPSIS
86
87
merge_booksellers.pl [-h|--help] [-v|--verbose] [-c|--confirm] --from=booksellerid --to=booksellerid
88
89
=head1 OPTIONS
90
91
=over
92
93
=item B<-h|--help>
94
95
Print a brief help message.
96
97
=item B<--verbose>
98
99
 --verbose              Show more information on what is done.
100
101
=item B<--confirm>
102
103
 --confirm              Commit the changes in database. Running without this
104
                        parameter is like testing. It is recommanded to run without
105
                        --confirm first to be sure of the changes.
106
107
=item B<--from>
108
109
 --from=<booksellerid>  The source bookseller identifier that will be merged in
110
                        target bookseller.
111
112
=item B<--to>
113
114
 --to=<booksellerid>    The target bookseller identifier in which merge
115
                        source bookseller.
116
117
=back
118
119
=cut

Return to bug 15336