Bug 29407: Make the pickup locations dropdown JS reusable
[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     GetMarcBiblio
31     GetMarcFromKohaField
32     GetMarcStructure
33     ModBiblio
34     TransformHtmlToMarc
35 );
36 use C4::Serials qw( CountSubscriptionFromBiblionumber );
37 use C4::Reserves qw( MergeHolds );
38 use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
39
40 use Koha::BiblioFrameworks;
41 use Koha::Biblios;
42 use Koha::Items;
43 use Koha::MetadataRecord;
44
45 my $input = CGI->new;
46 my @biblionumbers = $input->multi_param('biblionumber');
47 my $merge = $input->param('merge');
48
49 my @errors;
50
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {
53         template_name   => "cataloguing/merge.tt",
54         query           => $input,
55         type            => "intranet",
56         flagsrequired   => { editcatalogue => 'edit_catalogue' },
57     }
58 );
59
60 #------------------------
61 # Merging
62 #------------------------
63 if ($merge) {
64
65     my $dbh = C4::Context->dbh;
66
67     # Creating a new record from the html code
68     my $record       = TransformHtmlToMarc( $input, 1 );
69     my $ref_biblionumber = $input->param('ref_biblionumber');
70     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
71
72     # prepare report
73     my @report_records;
74     my $report_fields_str = $input->param('report_fields');
75     $report_fields_str ||= C4::Context->preference('MergeReportFields');
76     my @report_fields;
77     foreach my $field_str (split /,/, $report_fields_str) {
78         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
79             my ($field, $subfields) = ($1, $2);
80             push @report_fields, {
81                 tag => $field,
82                 subfields => [ split //, $subfields ]
83             }
84         }
85     }
86
87     # Rewriting the leader
88     $record->leader(GetMarcBiblio({ biblionumber => $ref_biblionumber })->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     my $biblio = Koha::Biblios->find($ref_biblionumber);
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 $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
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 $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
208             my $frameworkcode = GetFrameworkCode($biblionumber);
209             my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
210             my $record = {
211                 recordid => $biblionumber,
212                 record => $marcrecord,
213                 frameworkcode => $frameworkcode,
214                 display => $recordObj->createMergeHash($tagslib),
215             };
216             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
217                 $record->{reference} = 1;
218                 $template->param(ref_record => $record);
219                 unshift @records, $record;
220             } else {
221                 push @records, $record;
222             }
223         }
224
225         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
226
227         # Parameters
228         $template->param(
229             ref_biblionumber => $ref_biblionumber,
230             records => \@records,
231             ref_record => $records[0],
232             framework => $framework,
233             biblionumbertag => $biblionumbertag,
234             MergeReportFields => C4::Context->preference('MergeReportFields'),
235         );
236     } else {
237         my @records;
238         foreach my $biblionumber (@biblionumbers) {
239             my $frameworkcode = GetFrameworkCode($biblionumber);
240             my $record = {
241                 biblionumber => $biblionumber,
242                 data => GetBiblioData($biblionumber),
243                 frameworkcode => $frameworkcode,
244             };
245             push @records, $record;
246         }
247         # Ask the user to choose which record will be the kept
248         $template->param(
249             choosereference => 1,
250             records => \@records,
251         );
252
253         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
254         $template->param( frameworks => $frameworks );
255     }
256 }
257
258 if (@errors) {
259     # Errors
260     $template->param( errors  => \@errors );
261 }
262
263 output_html_with_http_headers $input, $cookie, $template->output;
264 exit;