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