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