3 # Copyright 2009 BibLibre
4 # Parts Copyright Catalyst IT 2011
6 # This file is part of Koha.
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.
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.
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>.
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
36 use C4::Serials qw( CountSubscriptionFromBiblionumber );
37 use C4::Reserves qw( MergeHolds );
38 use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
40 use Koha::BiblioFrameworks;
43 use Koha::MetadataRecord;
46 my @biblionumbers = $input->multi_param('biblionumber');
47 my $merge = $input->param('merge');
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 template_name => "cataloguing/merge.tt",
56 flagsrequired => { editcatalogue => 'edit_catalogue' },
60 #------------------------
62 #------------------------
65 my $dbh = C4::Context->dbh;
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;
74 my $report_fields_str = $input->param('report_fields');
75 $report_fields_str ||= C4::Context->preference('MergeReportFields');
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, {
82 subfields => [ split //, $subfields ]
87 # Rewriting the leader
88 $record->leader(GetMarcBiblio({ biblionumber => $ref_biblionumber })->leader());
90 my $frameworkcode = $input->param('frameworkcode');
92 # Modifying the reference record
93 ModBiblio($record, $ref_biblionumber, $frameworkcode);
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 });
103 my $sth_subscription = $dbh->prepare("
104 UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
106 my $sth_subscriptionhistory = $dbh->prepare("
107 UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
109 my $sth_serial = $dbh->prepare("
110 UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
112 my $sth_suggestions = $dbh->prepare("
113 UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ?
116 my $report_header = {};
117 foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
119 my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
120 my %report_record = (
121 biblionumber => $biblionumber,
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;
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;
142 $report_header->{ $tag . '@' } = 1;
143 push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
147 push @report_records, \%report_record;
150 foreach my $biblionumber (@biblionumbers) {
151 # Moving subscriptions from the other record to the reference record
152 my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
154 $sth_subscription->execute($ref_biblionumber, $biblionumber);
155 $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
159 $sth_serial->execute($ref_biblionumber, $biblionumber);
162 $sth_suggestions->execute($ref_biblionumber, $biblionumber);
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;
169 # TODO : add error control (in ModOrder?)
172 # Deleting the other records
173 if (scalar(@errors) == 0) {
175 MergeHolds($dbh, $ref_biblionumber, $biblionumber);
176 my $error = DelBiblio($biblionumber);
177 push @errors, $error if ($error);
184 report_records => \@report_records,
185 report_header => $report_header,
186 ref_biblionumber => scalar $input->param('ref_biblionumber')
189 #-------------------------
190 # Show records to merge
191 #-------------------------
193 my $ref_biblionumber = $input->param('ref_biblionumber');
195 if ($ref_biblionumber) {
196 my $framework = $input->param('frameworkcode');
197 $framework //= GetFrameworkCode($ref_biblionumber);
199 # Getting MARC Structure
200 my $tagslib = GetMarcStructure(1, $framework);
202 my $marcflavour = lc(C4::Context->preference('marcflavour'));
204 # Creating a loop for display
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});
211 recordid => $biblionumber,
212 record => $marcrecord,
213 frameworkcode => $frameworkcode,
214 display => $recordObj->createMergeHash($tagslib),
216 if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
217 $record->{reference} = 1;
218 $template->param(ref_record => $record);
219 unshift @records, $record;
221 push @records, $record;
225 my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
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'),
238 foreach my $biblionumber (@biblionumbers) {
239 my $frameworkcode = GetFrameworkCode($biblionumber);
241 biblionumber => $biblionumber,
242 data => GetBiblioData($biblionumber),
243 frameworkcode => $frameworkcode,
245 push @records, $record;
247 # Ask the user to choose which record will be the kept
249 choosereference => 1,
250 records => \@records,
253 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
254 $template->param( frameworks => $frameworks );
260 $template->param( errors => \@errors );
263 output_html_with_http_headers $input, $cookie, $template->output;