Bug 18255: Koha::Biblio - Replace GetBiblioItemByBiblioNumber with Koha::Biblio-...
[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;
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/;
32
33 use Koha::BiblioFrameworks;
34 use Koha::Items;
35 use Koha::MetadataRecord;
36
37 my $input = new CGI;
38 my @biblionumbers = $input->multi_param('biblionumber');
39 my $merge = $input->param('merge');
40
41 my @errors;
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "cataloguing/merge.tt",
46         query           => $input,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { editcatalogue => 'edit_catalogue' },
50     }
51 );
52
53 #------------------------
54 # Merging
55 #------------------------
56 if ($merge) {
57
58     my $dbh = C4::Context->dbh;
59
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;
64
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 ]
76             }
77         }
78     }
79
80     # Rewriting the leader
81     $record->leader(GetMarcBiblio({ biblionumber => $ref_biblionumber })->leader());
82
83     my $frameworkcode = $input->param('frameworkcode');
84     my @notmoveditems;
85
86     # Modifying the reference record
87     ModBiblio($record, $ref_biblionumber, $frameworkcode);
88
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;
96             }
97         }
98     }
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 };
103     }
104
105     my $sth_subscription = $dbh->prepare("
106         UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
107     ");
108     my $sth_subscriptionhistory = $dbh->prepare("
109         UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
110     ");
111     my $sth_serial = $dbh->prepare("
112         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
113     ");
114
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 => {},
122         );
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;
132                     }
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;
139                     }
140                 } else {
141                     $report_header->{ $tag . '@' } = 1;
142                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
143                 }
144             }
145         }
146         push @report_records, \%report_record;
147     }
148
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);
155         }
156
157     # Moving serials
158         $sth_serial->execute($ref_biblionumber, $biblionumber);
159
160     # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
161     my @allorders = GetOrdersByBiblionumber($biblionumber);
162     foreach my $myorder (@allorders) {
163         $myorder->{'biblionumber'} = $ref_biblionumber;
164         ModOrder ($myorder);
165     # TODO : add error control (in ModOrder?)
166     }
167
168     # Deleting the other records
169     if (scalar(@errors) == 0) {
170         # Move holds
171         MergeHolds($dbh, $ref_biblionumber, $biblionumber);
172         my $error = DelBiblio($biblionumber);
173         push @errors, $error if ($error);
174     }
175 }
176
177     # Parameters
178     $template->param(
179         result => 1,
180         report_records => \@report_records,
181         report_header => $report_header,
182         ref_biblionumber => scalar $input->param('ref_biblionumber')
183     );
184
185 #-------------------------
186 # Show records to merge
187 #-------------------------
188 } else {
189     my $ref_biblionumber = $input->param('ref_biblionumber');
190
191     if ($ref_biblionumber) {
192         my $framework = $input->param('frameworkcode');
193         $framework //= GetFrameworkCode($ref_biblionumber);
194
195         # Getting MARC Structure
196         my $tagslib = GetMarcStructure(1, $framework);
197
198         my $marcflavour = lc(C4::Context->preference('marcflavour'));
199
200         # Creating a loop for display
201         my @records;
202         foreach my $biblionumber (@biblionumbers) {
203             my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
204             my $frameworkcode = GetFrameworkCode($biblionumber);
205             my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour});
206             my $record = {
207                 recordid => $biblionumber,
208                 record => $marcrecord,
209                 frameworkcode => $frameworkcode,
210                 display => $recordObj->createMergeHash($tagslib),
211             };
212             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
213                 $record->{reference} = 1;
214                 $template->param(ref_record => $record);
215                 unshift @records, $record;
216             } else {
217                 push @records, $record;
218             }
219         }
220
221         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
222
223         # Parameters
224         $template->param(
225             ref_biblionumber => $ref_biblionumber,
226             records => \@records,
227             ref_record => $records[0],
228             framework => $framework,
229             biblionumbertag => $biblionumbertag,
230             MergeReportFields => C4::Context->preference('MergeReportFields'),
231         );
232     } else {
233         my @records;
234         foreach my $biblionumber (@biblionumbers) {
235             my $frameworkcode = GetFrameworkCode($biblionumber);
236             my $record = {
237                 biblionumber => $biblionumber,
238                 data => GetBiblioData($biblionumber),
239                 frameworkcode => $frameworkcode,
240             };
241             push @records, $record;
242         }
243         # Ask the user to choose which record will be the kept
244         $template->param(
245             choosereference => 1,
246             records => \@records,
247         );
248
249         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
250         $template->param( frameworks => $frameworks );
251     }
252 }
253
254 if (@errors) {
255     # Errors
256     $template->param( errors  => \@errors );
257 }
258
259 output_html_with_http_headers $input, $cookie, $template->output;
260 exit;