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 );
26 use C4::Items qw( MoveItemFromBiblio );
37 use C4::Serials qw( CountSubscriptionFromBiblionumber );
38 use C4::Reserves qw( MergeHolds );
39 use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
41 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');
93 # Modifying the reference record
94 ModBiblio($record, $ref_biblionumber, $frameworkcode);
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;
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 };
112 my $sth_subscription = $dbh->prepare("
113 UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
115 my $sth_subscriptionhistory = $dbh->prepare("
116 UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
118 my $sth_serial = $dbh->prepare("
119 UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
121 my $sth_suggestions = $dbh->prepare("
122 UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ?
125 my $report_header = {};
126 foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
128 my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
129 my %report_record = (
130 biblionumber => $biblionumber,
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;
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;
151 $report_header->{ $tag . '@' } = 1;
152 push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
156 push @report_records, \%report_record;
159 foreach my $biblionumber (@biblionumbers) {
160 # Moving subscriptions from the other record to the reference record
161 my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
163 $sth_subscription->execute($ref_biblionumber, $biblionumber);
164 $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
168 $sth_serial->execute($ref_biblionumber, $biblionumber);
171 $sth_suggestions->execute($ref_biblionumber, $biblionumber);
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;
178 # TODO : add error control (in ModOrder?)
181 # Deleting the other records
182 if (scalar(@errors) == 0) {
184 MergeHolds($dbh, $ref_biblionumber, $biblionumber);
185 my $error = DelBiblio($biblionumber);
186 push @errors, $error if ($error);
193 report_records => \@report_records,
194 report_header => $report_header,
195 ref_biblionumber => scalar $input->param('ref_biblionumber')
198 #-------------------------
199 # Show records to merge
200 #-------------------------
202 my $ref_biblionumber = $input->param('ref_biblionumber');
204 if ($ref_biblionumber) {
205 my $framework = $input->param('frameworkcode');
206 $framework //= GetFrameworkCode($ref_biblionumber);
208 # Getting MARC Structure
209 my $tagslib = GetMarcStructure(1, $framework);
211 my $marcflavour = lc(C4::Context->preference('marcflavour'));
213 # Creating a loop for display
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});
220 recordid => $biblionumber,
221 record => $marcrecord,
222 frameworkcode => $frameworkcode,
223 display => $recordObj->createMergeHash($tagslib),
225 if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
226 $record->{reference} = 1;
227 $template->param(ref_record => $record);
228 unshift @records, $record;
230 push @records, $record;
234 my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
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'),
247 foreach my $biblionumber (@biblionumbers) {
248 my $frameworkcode = GetFrameworkCode($biblionumber);
250 biblionumber => $biblionumber,
251 data => GetBiblioData($biblionumber),
252 frameworkcode => $frameworkcode,
254 push @records, $record;
256 # Ask the user to choose which record will be the kept
258 choosereference => 1,
259 records => \@records,
262 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
263 $template->param( frameworks => $frameworks );
269 $template->param( errors => \@errors );
272 output_html_with_http_headers $input, $cookie, $template->output;