Bug 36084: svc - problem_reports
[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
89     my $frameworkcode = $input->param('frameworkcode');
90
91     # Modifying the reference record
92     ModBiblio($record, $ref_biblionumber, $frameworkcode);
93
94     # Moving items and article requests from the other record to the reference record
95     $biblio = $biblio->get_from_storage;
96     foreach my $biblionumber (@biblionumbers) {
97         my $from_biblio = Koha::Biblios->find($biblionumber);
98         $from_biblio->items->move_to_biblio($biblio);
99         $from_biblio->article_requests->update({ biblionumber => $ref_biblionumber }, { no_triggers => 1 });
100     }
101
102     my $sth_subscription = $dbh->prepare("
103         UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
104     ");
105     my $sth_subscriptionhistory = $dbh->prepare("
106         UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
107     ");
108     my $sth_serial = $dbh->prepare("
109         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
110     ");
111     my $sth_suggestions = $dbh->prepare("
112         UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ?
113     ");
114
115     my $report_header = {};
116     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
117         # build report
118         my $biblio = Koha::Biblios->find($biblionumber);
119         my $marcrecord = $biblio->metadata->record;
120         my %report_record = (
121             biblionumber => $biblionumber,
122             fields => {},
123         );
124         foreach my $field (@report_fields) {
125             my @marcfields = $marcrecord->field($field->{tag});
126             foreach my $marcfield (@marcfields) {
127                 my $tag = $marcfield->tag();
128                 if (scalar @{$field->{subfields}}) {
129                     foreach my $subfield (@{$field->{subfields}}) {
130                         my @values = $marcfield->subfield($subfield);
131                         $report_header->{ $tag . $subfield } = 1;
132                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
133                     }
134                 } elsif ($field->{tag} gt '009') {
135                     my @marcsubfields = $marcfield->subfields();
136                     foreach my $marcsubfield (@marcsubfields) {
137                         my ($code, $value) = @$marcsubfield;
138                         $report_header->{ $tag . $code } = 1;
139                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
140                     }
141                 } else {
142                     $report_header->{ $tag . '@' } = 1;
143                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
144                 }
145             }
146         }
147         push @report_records, \%report_record;
148     }
149
150     foreach my $biblionumber (@biblionumbers) {
151         # Moving subscriptions from the other record to the reference record
152         my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
153         if ($subcount > 0) {
154             $sth_subscription->execute($ref_biblionumber, $biblionumber);
155             $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
156         }
157
158     # Moving serials
159     $sth_serial->execute($ref_biblionumber, $biblionumber);
160
161     # Moving suggestions
162     $sth_suggestions->execute($ref_biblionumber, $biblionumber);
163
164     # Moving orders (orders linked to items of frombiblio have already been moved by move_to_biblio)
165     my @allorders = GetOrdersByBiblionumber($biblionumber);
166     foreach my $myorder (@allorders) {
167         $myorder->{'biblionumber'} = $ref_biblionumber;
168         ModOrder ($myorder);
169     # TODO : add error control (in ModOrder?)
170     }
171
172     # Deleting the other records
173     if (scalar(@errors) == 0) {
174         # Move holds
175         MergeHolds($dbh, $ref_biblionumber, $biblionumber);
176         my $error = DelBiblio($biblionumber);
177         push @errors, $error if ($error);
178     }
179 }
180
181     # Parameters
182     $template->param(
183         result => 1,
184         report_records => \@report_records,
185         report_header => $report_header,
186         ref_biblionumber => scalar $input->param('ref_biblionumber')
187     );
188
189 #-------------------------
190 # Show records to merge
191 #-------------------------
192 } else {
193     my $ref_biblionumber = $input->param('ref_biblionumber');
194
195     if ($ref_biblionumber) {
196         my $framework = $input->param('frameworkcode');
197         $framework //= GetFrameworkCode($ref_biblionumber);
198
199         # Getting MARC Structure
200         my $tagslib = GetMarcStructure(1, $framework);
201
202         my $marcflavour = lc(C4::Context->preference('marcflavour'));
203
204         # Creating a loop for display
205         my @records;
206         foreach my $biblionumber (@biblionumbers) {
207             my $biblio = Koha::Biblios->find($biblionumber);
208             my $marcrecord = $biblio->metadata->record;
209             my $frameworkcode = GetFrameworkCode($biblionumber);
210             my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
211             my $record = {
212                 recordid => $biblionumber,
213                 record => $marcrecord,
214                 frameworkcode => $frameworkcode,
215                 display => $recordObj->createMergeHash($tagslib),
216             };
217             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
218                 $record->{reference} = 1;
219                 $template->param(ref_record => $record);
220                 unshift @records, $record;
221             } else {
222                 push @records, $record;
223             }
224         }
225
226         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
227
228         # Parameters
229         $template->param(
230             ref_biblionumber => $ref_biblionumber,
231             records => \@records,
232             ref_record => $records[0],
233             framework => $framework,
234             biblionumbertag => $biblionumbertag,
235             MergeReportFields => C4::Context->preference('MergeReportFields'),
236         );
237     } else {
238         my @records;
239         foreach my $biblionumber (@biblionumbers) {
240             my $frameworkcode = GetFrameworkCode($biblionumber);
241             my $record = {
242                 biblionumber => $biblionumber,
243                 data => GetBiblioData($biblionumber),
244                 frameworkcode => $frameworkcode,
245             };
246             push @records, $record;
247         }
248         # Ask the user to choose which record will be the kept
249         $template->param(
250             choosereference => 1,
251             records => \@records,
252         );
253
254         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
255         $template->param( frameworks => $frameworks );
256     }
257 }
258
259 if (@errors) {
260     # Errors
261     $template->param( errors  => \@errors );
262 }
263
264 output_html_with_http_headers $input, $cookie, $template->output;
265 exit;