Bug 15801: Koha::BiblioFrameworks - Remove C4::Koha::getframeworks
[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::MetadataRecord;
35
36 my $input = new CGI;
37 my @biblionumbers = $input->multi_param('biblionumber');
38 my $merge = $input->param('merge');
39
40 my @errors;
41
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "cataloguing/merge.tt",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { editcatalogue => 'edit_catalogue' },
49     }
50 );
51
52 #------------------------
53 # Merging
54 #------------------------
55 if ($merge) {
56
57     my $dbh = C4::Context->dbh;
58
59     # Creating a new record from the html code
60     my $record       = TransformHtmlToMarc( $input, 1 );
61     my $ref_biblionumber = $input->param('ref_biblionumber');
62     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
63
64     # prepare report
65     my @report_records;
66     my $report_fields_str = $input->param('report_fields');
67     $report_fields_str ||= C4::Context->preference('MergeReportFields');
68     my @report_fields;
69     foreach my $field_str (split /,/, $report_fields_str) {
70         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
71             my ($field, $subfields) = ($1, $2);
72             push @report_fields, {
73                 tag => $field,
74                 subfields => [ split //, $subfields ]
75             }
76         }
77     }
78
79     # Rewriting the leader
80     $record->leader(GetMarcBiblio($ref_biblionumber)->leader());
81
82     my $frameworkcode = $input->param('frameworkcode');
83     my @notmoveditems;
84
85     # Modifying the reference record
86     ModBiblio($record, $ref_biblionumber, $frameworkcode);
87
88     # Moving items from the other record to the reference record
89     foreach my $biblionumber (@biblionumbers) {
90         my $itemnumbers = get_itemnumbers_of($biblionumber);
91         foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) {
92         my $res = MoveItemFromBiblio($itemnumber, $biblionumber, $ref_biblionumber);
93         if (not defined $res) {
94             push @notmoveditems, $itemnumber;
95         }
96     }
97     }
98     # If some items could not be moved :
99     if (scalar(@notmoveditems) > 0) {
100         my $itemlist = join(' ',@notmoveditems);
101         push @errors, { code => "CANNOT_MOVE", value => $itemlist };
102     }
103
104     my $sth_subscription = $dbh->prepare("
105         UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
106     ");
107     my $sth_subscriptionhistory = $dbh->prepare("
108         UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
109     ");
110     my $sth_serial = $dbh->prepare("
111         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
112     ");
113
114     my $report_header = {};
115     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
116         # build report
117         my $marcrecord = GetMarcBiblio($biblionumber);
118         my %report_record = (
119             biblionumber => $biblionumber,
120             fields => {},
121         );
122         foreach my $field (@report_fields) {
123             my @marcfields = $marcrecord->field($field->{tag});
124             foreach my $marcfield (@marcfields) {
125                 my $tag = $marcfield->tag();
126                 if (scalar @{$field->{subfields}}) {
127                     foreach my $subfield (@{$field->{subfields}}) {
128                         my @values = $marcfield->subfield($subfield);
129                         $report_header->{ $tag . $subfield } = 1;
130                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
131                     }
132                 } elsif ($field->{tag} gt '009') {
133                     my @marcsubfields = $marcfield->subfields();
134                     foreach my $marcsubfield (@marcsubfields) {
135                         my ($code, $value) = @$marcsubfield;
136                         $report_header->{ $tag . $code } = 1;
137                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
138                     }
139                 } else {
140                     $report_header->{ $tag . '@' } = 1;
141                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
142                 }
143             }
144         }
145         push @report_records, \%report_record;
146     }
147
148     foreach my $biblionumber (@biblionumbers) {
149         # Moving subscriptions from the other record to the reference record
150         my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
151         if ($subcount > 0) {
152             $sth_subscription->execute($ref_biblionumber, $biblionumber);
153             $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
154         }
155
156     # Moving serials
157         $sth_serial->execute($ref_biblionumber, $biblionumber);
158
159     # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
160     my @allorders = GetOrdersByBiblionumber($biblionumber);
161     my @tobiblioitem = GetBiblioItemByBiblioNumber ($ref_biblionumber);
162     my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
163     foreach my $myorder (@allorders) {
164         $myorder->{'biblionumber'} = $ref_biblionumber;
165         ModOrder ($myorder);
166     # TODO : add error control (in ModOrder?)
167     }
168
169     # Deleting the other records
170     if (scalar(@errors) == 0) {
171         # Move holds
172         MergeHolds($dbh, $ref_biblionumber, $biblionumber);
173         my $error = DelBiblio($biblionumber);
174         push @errors, $error if ($error);
175     }
176 }
177
178     # Parameters
179     $template->param(
180         result => 1,
181         report_records => \@report_records,
182         report_header => $report_header,
183         ref_biblionumber => scalar $input->param('ref_biblionumber')
184     );
185
186 #-------------------------
187 # Show records to merge
188 #-------------------------
189 } else {
190     my $ref_biblionumber = $input->param('ref_biblionumber');
191
192     if ($ref_biblionumber) {
193         my $framework = $input->param('frameworkcode');
194         $framework //= GetFrameworkCode($ref_biblionumber);
195
196         # Getting MARC Structure
197         my $tagslib = GetMarcStructure(1, $framework);
198
199         my $marcflavour = lc(C4::Context->preference('marcflavour'));
200
201         # Creating a loop for display
202         my @records;
203         foreach my $biblionumber (@biblionumbers) {
204             my $marcrecord = GetMarcBiblio($biblionumber);
205             my $frameworkcode = GetFrameworkCode($biblionumber);
206             my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour});
207             my $record = {
208                 recordid => $biblionumber,
209                 record => $marcrecord,
210                 frameworkcode => $frameworkcode,
211                 display => $recordObj->createMergeHash($tagslib),
212             };
213             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
214                 $record->{reference} = 1;
215                 $template->param(ref_record => $record);
216                 unshift @records, $record;
217             } else {
218                 push @records, $record;
219             }
220         }
221
222         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
223
224         # Parameters
225         $template->param(
226             ref_biblionumber => $ref_biblionumber,
227             records => \@records,
228             ref_record => $records[0],
229             framework => $framework,
230             biblionumbertag => $biblionumbertag,
231             MergeReportFields => C4::Context->preference('MergeReportFields'),
232         );
233     } else {
234         my @records;
235         foreach my $biblionumber (@biblionumbers) {
236             my $frameworkcode = GetFrameworkCode($biblionumber);
237             my $record = {
238                 biblionumber => $biblionumber,
239                 data => GetBiblioData($biblionumber),
240                 frameworkcode => $frameworkcode,
241             };
242             push @records, $record;
243         }
244         # Ask the user to choose which record will be the kept
245         $template->param(
246             choosereference => 1,
247             records => \@records,
248         );
249
250         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
251         $template->param( frameworks => $frameworks );
252     }
253 }
254
255 if (@errors) {
256     # Errors
257     $template->param( errors  => \@errors );
258 }
259
260 output_html_with_http_headers $input, $cookie, $template->output;
261 exit;