Bug 28787: (QA follow-up) Remove unused variable
[koha.git] / cataloguing / merge.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
4 # Parts Copyright Catalyst IT 2011
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Biblio qw(
27     DelBiblio
28     GetBiblioData
29     GetFrameworkCode
30     GetMarcFromKohaField
31     GetMarcStructure
32     ModBiblio
33     TransformHtmlToMarc
34 );
35 use C4::Serials qw( CountSubscriptionFromBiblionumber );
36 use C4::Reserves qw( MergeHolds );
37 use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
38
39 use Koha::BiblioFrameworks;
40 use Koha::Biblios;
41 use Koha::Items;
42 use Koha::MetadataRecord;
43
44 my $input = CGI->new;
45 my @biblionumbers = $input->multi_param('biblionumber');
46 my $merge = $input->param('merge');
47
48 my @errors;
49
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51     {
52         template_name   => "cataloguing/merge.tt",
53         query           => $input,
54         type            => "intranet",
55         flagsrequired   => { editcatalogue => 'edit_catalogue' },
56     }
57 );
58
59 #------------------------
60 # Merging
61 #------------------------
62 if ($merge) {
63
64     my $dbh = C4::Context->dbh;
65
66     # Creating a new record from the html code
67     my $record       = TransformHtmlToMarc( $input, 1 );
68     my $ref_biblionumber = $input->param('ref_biblionumber');
69     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
70
71     # prepare report
72     my @report_records;
73     my $report_fields_str = $input->param('report_fields');
74     $report_fields_str ||= C4::Context->preference('MergeReportFields');
75     my @report_fields;
76     foreach my $field_str (split /,/, $report_fields_str) {
77         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
78             my ($field, $subfields) = ($1, $2);
79             push @report_fields, {
80                 tag => $field,
81                 subfields => [ split //, $subfields ]
82             }
83         }
84     }
85
86     # Rewriting the leader
87     my $biblio = Koha::Biblios->find($ref_biblionumber);
88     $record->leader($biblio->metadata->record->leader());
89
90     my $frameworkcode = $input->param('frameworkcode');
91
92     # Modifying the reference record
93     ModBiblio($record, $ref_biblionumber, $frameworkcode);
94
95     # Moving items and article requests from the other record to the reference record
96     $biblio = $biblio->get_from_storage;
97     foreach my $biblionumber (@biblionumbers) {
98         my $from_biblio = Koha::Biblios->find($biblionumber);
99         $from_biblio->items->move_to_biblio($biblio);
100         $from_biblio->article_requests->update({ biblionumber => $ref_biblionumber }, { no_triggers => 1 });
101     }
102
103     my $sth_subscription = $dbh->prepare("
104         UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
105     ");
106     my $sth_subscriptionhistory = $dbh->prepare("
107         UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
108     ");
109     my $sth_serial = $dbh->prepare("
110         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
111     ");
112     my $sth_suggestions = $dbh->prepare("
113         UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ?
114     ");
115
116     my $report_header = {};
117     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
118         # build report
119         my $biblio = Koha::Biblios->find($biblionumber);
120         my $marcrecord = $biblio->metadata->record;
121         my %report_record = (
122             biblionumber => $biblionumber,
123             fields => {},
124         );
125         foreach my $field (@report_fields) {
126             my @marcfields = $marcrecord->field($field->{tag});
127             foreach my $marcfield (@marcfields) {
128                 my $tag = $marcfield->tag();
129                 if (scalar @{$field->{subfields}}) {
130                     foreach my $subfield (@{$field->{subfields}}) {
131                         my @values = $marcfield->subfield($subfield);
132                         $report_header->{ $tag . $subfield } = 1;
133                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
134                     }
135                 } elsif ($field->{tag} gt '009') {
136                     my @marcsubfields = $marcfield->subfields();
137                     foreach my $marcsubfield (@marcsubfields) {
138                         my ($code, $value) = @$marcsubfield;
139                         $report_header->{ $tag . $code } = 1;
140                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
141                     }
142                 } else {
143                     $report_header->{ $tag . '@' } = 1;
144                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
145                 }
146             }
147         }
148         push @report_records, \%report_record;
149     }
150
151     foreach my $biblionumber (@biblionumbers) {
152         # Moving subscriptions from the other record to the reference record
153         my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
154         if ($subcount > 0) {
155             $sth_subscription->execute($ref_biblionumber, $biblionumber);
156             $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
157         }
158
159     # Moving serials
160     $sth_serial->execute($ref_biblionumber, $biblionumber);
161
162     # Moving suggestions
163     $sth_suggestions->execute($ref_biblionumber, $biblionumber);
164
165     # Moving orders (orders linked to items of frombiblio have already been moved by move_to_biblio)
166     my @allorders = GetOrdersByBiblionumber($biblionumber);
167     foreach my $myorder (@allorders) {
168         $myorder->{'biblionumber'} = $ref_biblionumber;
169         ModOrder ($myorder);
170     # TODO : add error control (in ModOrder?)
171     }
172
173     # Deleting the other records
174     if (scalar(@errors) == 0) {
175         # Move holds
176         MergeHolds($dbh, $ref_biblionumber, $biblionumber);
177         my $error = DelBiblio($biblionumber);
178         push @errors, $error if ($error);
179     }
180 }
181
182     # Parameters
183     $template->param(
184         result => 1,
185         report_records => \@report_records,
186         report_header => $report_header,
187         ref_biblionumber => scalar $input->param('ref_biblionumber')
188     );
189
190 #-------------------------
191 # Show records to merge
192 #-------------------------
193 } else {
194     my $ref_biblionumber = $input->param('ref_biblionumber');
195
196     if ($ref_biblionumber) {
197         my $framework = $input->param('frameworkcode');
198         $framework //= GetFrameworkCode($ref_biblionumber);
199
200         # Getting MARC Structure
201         my $tagslib = GetMarcStructure(1, $framework);
202
203         my $marcflavour = lc(C4::Context->preference('marcflavour'));
204
205         # Creating a loop for display
206         my @records;
207         foreach my $biblionumber (@biblionumbers) {
208             my $biblio = Koha::Biblios->find($biblionumber);
209             my $marcrecord = $biblio->metadata->record;
210             my $frameworkcode = GetFrameworkCode($biblionumber);
211             my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
212             my $record = {
213                 recordid => $biblionumber,
214                 record => $marcrecord,
215                 frameworkcode => $frameworkcode,
216                 display => $recordObj->createMergeHash($tagslib),
217             };
218             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
219                 $record->{reference} = 1;
220                 $template->param(ref_record => $record);
221                 unshift @records, $record;
222             } else {
223                 push @records, $record;
224             }
225         }
226
227         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
228
229         # Parameters
230         $template->param(
231             ref_biblionumber => $ref_biblionumber,
232             records => \@records,
233             ref_record => $records[0],
234             framework => $framework,
235             biblionumbertag => $biblionumbertag,
236             MergeReportFields => C4::Context->preference('MergeReportFields'),
237         );
238     } else {
239         my @records;
240         foreach my $biblionumber (@biblionumbers) {
241             my $frameworkcode = GetFrameworkCode($biblionumber);
242             my $record = {
243                 biblionumber => $biblionumber,
244                 data => GetBiblioData($biblionumber),
245                 frameworkcode => $frameworkcode,
246             };
247             push @records, $record;
248         }
249         # Ask the user to choose which record will be the kept
250         $template->param(
251             choosereference => 1,
252             records => \@records,
253         );
254
255         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
256         $template->param( frameworks => $frameworks );
257     }
258 }
259
260 if (@errors) {
261     # Errors
262     $template->param( errors  => \@errors );
263 }
264
265 output_html_with_http_headers $input, $cookie, $template->output;
266 exit;