Bug 7317: Add missing 'use Encode' statement
[koha.git] / cataloguing / merge.pl
blob45946716b9a14280a57f1b2fe474a76527948a08
1 #!/usr/bin/perl
3 # Copyright 2009 BibLibre
4 # Parts Copyright Catalyst IT 2011
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
24 use C4::Output;
25 use C4::Auth;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Serials;
29 use C4::Koha;
30 use C4::Reserves qw/MergeHolds/;
31 use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
33 use Koha::BiblioFrameworks;
34 use Koha::Items;
35 use Koha::MetadataRecord;
37 my $input = new CGI;
38 my @biblionumbers = $input->multi_param('biblionumber');
39 my $merge = $input->param('merge');
41 my @errors;
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "cataloguing/merge.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { editcatalogue => 'edit_catalogue' },
53 #------------------------
54 # Merging
55 #------------------------
56 if ($merge) {
58 my $dbh = C4::Context->dbh;
60 # Creating a new record from the html code
61 my $record = TransformHtmlToMarc( $input, 1 );
62 my $ref_biblionumber = $input->param('ref_biblionumber');
63 @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
65 # prepare report
66 my @report_records;
67 my $report_fields_str = $input->param('report_fields');
68 $report_fields_str ||= C4::Context->preference('MergeReportFields');
69 my @report_fields;
70 foreach my $field_str (split /,/, $report_fields_str) {
71 if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
72 my ($field, $subfields) = ($1, $2);
73 push @report_fields, {
74 tag => $field,
75 subfields => [ split //, $subfields ]
80 # Rewriting the leader
81 $record->leader(GetMarcBiblio({ biblionumber => $ref_biblionumber })->leader());
83 my $frameworkcode = $input->param('frameworkcode');
84 my @notmoveditems;
86 # Modifying the reference record
87 ModBiblio($record, $ref_biblionumber, $frameworkcode);
89 # Moving items from the other record to the reference record
90 foreach my $biblionumber (@biblionumbers) {
91 my $items = Koha::Items->search({ biblionumber => $biblionumber });
92 while ( my $item = $items->next) {
93 my $res = MoveItemFromBiblio( $item->itemnumber, $biblionumber, $ref_biblionumber );
94 if ( not defined $res ) {
95 push @notmoveditems, $item->itemnumber;
99 # If some items could not be moved :
100 if (scalar(@notmoveditems) > 0) {
101 my $itemlist = join(' ',@notmoveditems);
102 push @errors, { code => "CANNOT_MOVE", value => $itemlist };
105 my $sth_subscription = $dbh->prepare("
106 UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
108 my $sth_subscriptionhistory = $dbh->prepare("
109 UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
111 my $sth_serial = $dbh->prepare("
112 UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
115 my $report_header = {};
116 foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
117 # build report
118 my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
119 my %report_record = (
120 biblionumber => $biblionumber,
121 fields => {},
123 foreach my $field (@report_fields) {
124 my @marcfields = $marcrecord->field($field->{tag});
125 foreach my $marcfield (@marcfields) {
126 my $tag = $marcfield->tag();
127 if (scalar @{$field->{subfields}}) {
128 foreach my $subfield (@{$field->{subfields}}) {
129 my @values = $marcfield->subfield($subfield);
130 $report_header->{ $tag . $subfield } = 1;
131 push @{ $report_record{fields}->{$tag . $subfield} }, @values;
133 } elsif ($field->{tag} gt '009') {
134 my @marcsubfields = $marcfield->subfields();
135 foreach my $marcsubfield (@marcsubfields) {
136 my ($code, $value) = @$marcsubfield;
137 $report_header->{ $tag . $code } = 1;
138 push @{ $report_record{fields}->{ $tag . $code } }, $value;
140 } else {
141 $report_header->{ $tag . '@' } = 1;
142 push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
146 push @report_records, \%report_record;
149 foreach my $biblionumber (@biblionumbers) {
150 # Moving subscriptions from the other record to the reference record
151 my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
152 if ($subcount > 0) {
153 $sth_subscription->execute($ref_biblionumber, $biblionumber);
154 $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
157 # Moving serials
158 $sth_serial->execute($ref_biblionumber, $biblionumber);
160 # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
161 my @allorders = GetOrdersByBiblionumber($biblionumber);
162 my @tobiblioitem = GetBiblioItemByBiblioNumber ($ref_biblionumber);
163 my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
164 foreach my $myorder (@allorders) {
165 $myorder->{'biblionumber'} = $ref_biblionumber;
166 ModOrder ($myorder);
167 # TODO : add error control (in ModOrder?)
170 # Deleting the other records
171 if (scalar(@errors) == 0) {
172 # Move holds
173 MergeHolds($dbh, $ref_biblionumber, $biblionumber);
174 my $error = DelBiblio($biblionumber);
175 push @errors, $error if ($error);
179 # Parameters
180 $template->param(
181 result => 1,
182 report_records => \@report_records,
183 report_header => $report_header,
184 ref_biblionumber => scalar $input->param('ref_biblionumber')
187 #-------------------------
188 # Show records to merge
189 #-------------------------
190 } else {
191 my $ref_biblionumber = $input->param('ref_biblionumber');
193 if ($ref_biblionumber) {
194 my $framework = $input->param('frameworkcode');
195 $framework //= GetFrameworkCode($ref_biblionumber);
197 # Getting MARC Structure
198 my $tagslib = GetMarcStructure(1, $framework);
200 my $marcflavour = lc(C4::Context->preference('marcflavour'));
202 # Creating a loop for display
203 my @records;
204 foreach my $biblionumber (@biblionumbers) {
205 my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
206 my $frameworkcode = GetFrameworkCode($biblionumber);
207 my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour});
208 my $record = {
209 recordid => $biblionumber,
210 record => $marcrecord,
211 frameworkcode => $frameworkcode,
212 display => $recordObj->createMergeHash($tagslib),
214 if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
215 $record->{reference} = 1;
216 $template->param(ref_record => $record);
217 unshift @records, $record;
218 } else {
219 push @records, $record;
223 my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
225 # Parameters
226 $template->param(
227 ref_biblionumber => $ref_biblionumber,
228 records => \@records,
229 ref_record => $records[0],
230 framework => $framework,
231 biblionumbertag => $biblionumbertag,
232 MergeReportFields => C4::Context->preference('MergeReportFields'),
234 } else {
235 my @records;
236 foreach my $biblionumber (@biblionumbers) {
237 my $frameworkcode = GetFrameworkCode($biblionumber);
238 my $record = {
239 biblionumber => $biblionumber,
240 data => GetBiblioData($biblionumber),
241 frameworkcode => $frameworkcode,
243 push @records, $record;
245 # Ask the user to choose which record will be the kept
246 $template->param(
247 choosereference => 1,
248 records => \@records,
251 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
252 $template->param( frameworks => $frameworks );
256 if (@errors) {
257 # Errors
258 $template->param( errors => \@errors );
261 output_html_with_http_headers $input, $cookie, $template->output;
262 exit;