Bug 36018: Prevent random failure from api/v1/acquisitions_orders.t
[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 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46     {
47         template_name   => "cataloguing/merge.tt",
48         query           => $input,
49         type            => "intranet",
50         flagsrequired   => { editcatalogue => 'edit_catalogue' },
51     }
52 );
53
54 my @biblionumbers = $input->multi_param('biblionumber');
55 my $op = $input->param('op') || q{};
56
57 my @errors;
58 #------------------------
59 # Merging
60 #------------------------
61 if ($op eq 'cud-merge') {
62
63     my $dbh = C4::Context->dbh;
64
65     # Creating a new record from the html code
66     my $record       = TransformHtmlToMarc( $input, 1 );
67     my $ref_biblionumber = $input->param('ref_biblionumber');
68     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
69
70     # prepare report
71     my @report_records;
72     my $report_fields_str = $input->param('report_fields');
73     $report_fields_str ||= C4::Context->preference('MergeReportFields');
74     my @report_fields;
75     foreach my $field_str (split /,/, $report_fields_str) {
76         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
77             my ($field, $subfields) = ($1, $2);
78             push @report_fields, {
79                 tag => $field,
80                 subfields => [ split //, $subfields ]
81             }
82         }
83     }
84
85     # Rewriting the leader
86     my $biblio = Koha::Biblios->find($ref_biblionumber);
87     $record->leader($biblio->metadata->record->leader());
88     #Take new framework code
89     my $frameworkcode = $input->param('frameworkcode');
90
91     # Modifying the reference record
92     ModBiblio($record, $ref_biblionumber, $frameworkcode);
93
94     my $report_header = {};
95     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
96         # build report
97         my $biblio = Koha::Biblios->find($biblionumber);
98         my $marcrecord = $biblio->metadata->record;
99         my %report_record = (
100             biblionumber => $biblionumber,
101             fields => {},
102         );
103         foreach my $field (@report_fields) {
104             my @marcfields = $marcrecord->field($field->{tag});
105             foreach my $marcfield (@marcfields) {
106                 my $tag = $marcfield->tag();
107                 if (scalar @{$field->{subfields}}) {
108                     foreach my $subfield (@{$field->{subfields}}) {
109                         my @values = $marcfield->subfield($subfield);
110                         $report_header->{ $tag . $subfield } = 1;
111                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
112                     }
113                 } elsif ($field->{tag} gt '009') {
114                     my @marcsubfields = $marcfield->subfields();
115                     foreach my $marcsubfield (@marcsubfields) {
116                         my ($code, $value) = @$marcsubfield;
117                         $report_header->{ $tag . $code } = 1;
118                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
119                     }
120                 } else {
121                     $report_header->{ $tag . '@' } = 1;
122                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
123                 }
124             }
125         }
126         push @report_records, \%report_record;
127     }
128
129     my $rmerge;
130     eval {
131         my $newbiblio = Koha::Biblios->find($ref_biblionumber);
132         $rmerge = $newbiblio->merge_with( \@biblionumbers );
133     };
134     if ($@) {
135         push @errors, $@;
136     }
137
138     # Parameters
139     $template->param(
140         result           => 1,
141         report_records   => \@report_records,
142         report_header    => $report_header,
143         ref_biblionumber => scalar $input->param('ref_biblionumber')
144     );
145
146 #-------------------------
147 # Show records to merge
148 #-------------------------
149 } else {
150     my $ref_biblionumber = $input->param('ref_biblionumber');
151
152     if ($ref_biblionumber) {
153         my $framework = $input->param('frameworkcode');
154         $framework //= GetFrameworkCode($ref_biblionumber);
155
156         # Getting MARC Structure
157         my $tagslib = GetMarcStructure(1, $framework);
158
159         my $marcflavour = lc(C4::Context->preference('marcflavour'));
160
161         # Creating a loop for display
162         my @records;
163         foreach my $biblionumber (@biblionumbers) {
164             my $biblio = Koha::Biblios->find($biblionumber);
165             my $marcrecord = $biblio->metadata->record;
166             my $frameworkcode = GetFrameworkCode($biblionumber);
167             my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
168             my $record = {
169                 recordid => $biblionumber,
170                 record => $marcrecord,
171                 frameworkcode => $frameworkcode,
172                 display => $recordObj->createMergeHash($tagslib),
173             };
174             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
175                 $record->{reference} = 1;
176                 $template->param(ref_record => $record);
177                 unshift @records, $record;
178             } else {
179                 push @records, $record;
180             }
181         }
182
183         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
184
185         # Parameters
186         $template->param(
187             ref_biblionumber => $ref_biblionumber,
188             records => \@records,
189             ref_record => $records[0],
190             framework => $framework,
191             biblionumbertag => $biblionumbertag,
192             MergeReportFields => C4::Context->preference('MergeReportFields'),
193         );
194     } else {
195         my @records;
196         foreach my $biblionumber (@biblionumbers) {
197             my $frameworkcode = GetFrameworkCode($biblionumber);
198             my $record = {
199                 biblionumber => $biblionumber,
200                 data => GetBiblioData($biblionumber),
201                 frameworkcode => $frameworkcode,
202             };
203             push @records, $record;
204         }
205         # Ask the user to choose which record will be the kept
206         $template->param(
207             choosereference => 1,
208             records => \@records,
209         );
210
211         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
212         $template->param( frameworks => $frameworks );
213     }
214 }
215
216 if (@errors) {
217     # Errors
218     $template->param( errors  => \@errors );
219 }
220
221 output_html_with_http_headers $input, $cookie, $template->output;
222 exit;