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>.
30 use C4
::Reserves qw
/MergeHolds/;
31 use C4
::Acquisition qw
/ModOrder GetOrdersByBiblionumber/;
32 use Koha
::MetadataRecord
;
35 my @biblionumbers = $input->param('biblionumber');
36 my $merge = $input->param('merge');
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
42 template_name
=> "cataloguing/merge.tt",
46 flagsrequired
=> { editcatalogue
=> 'edit_catalogue' },
50 #------------------------
52 #------------------------
55 my $dbh = C4
::Context
->dbh;
57 # Creating a new record from the html code
58 my $record = TransformHtmlToMarc
( $input );
59 my $ref_biblionumber = $input->param('ref_biblionumber');
60 @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
64 my $report_fields_str = $input->param('report_fields');
65 $report_fields_str ||= C4
::Context
->preference('MergeReportFields');
67 foreach my $field_str (split /,/, $report_fields_str) {
68 if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
69 my ($field, $subfields) = ($1, $2);
70 push @report_fields, {
72 subfields
=> [ split //, $subfields ]
77 # Rewriting the leader
78 $record->leader(GetMarcBiblio
($ref_biblionumber)->leader());
80 my $frameworkcode = $input->param('frameworkcode');
83 # Modifying the reference record
84 ModBiblio
($record, $ref_biblionumber, $frameworkcode);
86 # Moving items from the other record to the reference record
87 foreach my $biblionumber (@biblionumbers) {
88 my $itemnumbers = get_itemnumbers_of
($biblionumber);
89 foreach my $itemnumber (@
{ $itemnumbers->{$biblionumber} }) {
90 my $res = MoveItemFromBiblio
($itemnumber, $biblionumber, $ref_biblionumber);
91 if (not defined $res) {
92 push @notmoveditems, $itemnumber;
96 # If some items could not be moved :
97 if (scalar(@notmoveditems) > 0) {
98 my $itemlist = join(' ',@notmoveditems);
99 push @errors, { code
=> "CANNOT_MOVE", value
=> $itemlist };
102 my $sth_subscription = $dbh->prepare("
103 UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
105 my $sth_subscriptionhistory = $dbh->prepare("
106 UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
108 my $sth_serial = $dbh->prepare("
109 UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
112 my $report_header = {};
113 foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
115 my $marcrecord = GetMarcBiblio
($biblionumber);
116 my %report_record = (
117 biblionumber
=> $biblionumber,
120 foreach my $field (@report_fields) {
121 my @marcfields = $marcrecord->field($field->{tag
});
122 foreach my $marcfield (@marcfields) {
123 my $tag = $marcfield->tag();
125 if (scalar @
{$field->{subfields
}}) {
126 foreach my $subfield (@
{$field->{subfields
}}) {
127 my @values = $marcfield->subfield($subfield);
128 $report_header->{ $tag . $subfield } = 1;
129 push @
{ $report_record{fields
}->{$tag . $subfield} }, @values;
131 } elsif ($field->{tag
} gt '009') {
132 my @marcsubfields = $marcfield->subfields();
133 foreach my $marcsubfield (@marcsubfields) {
134 my ($code, $value) = @
$marcsubfield;
135 $report_header->{ $tag . $code } = 1;
136 push @
{ $report_record{fields
}->{ $tag . $code } }, $value;
139 $report_header->{ $tag . '@' } = 1;
140 push @
{ $report_record{fields
}->{ $tag .'@' } }, $marcfield->data();
144 push @report_records, \
%report_record;
147 foreach my $biblionumber (@biblionumbers) {
148 # Moving subscriptions from the other record to the reference record
149 my $subcount = CountSubscriptionFromBiblionumber
($biblionumber);
151 $sth_subscription->execute($ref_biblionumber, $biblionumber);
152 $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
156 $sth_serial->execute($ref_biblionumber, $biblionumber);
158 # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
159 my @allorders = GetOrdersByBiblionumber
($biblionumber);
160 my @tobiblioitem = GetBiblioItemByBiblioNumber
($ref_biblionumber);
161 my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber
};
162 foreach my $myorder (@allorders) {
163 $myorder->{'biblionumber'} = $ref_biblionumber;
165 # TODO : add error control (in ModOrder?)
168 # Deleting the other records
169 if (scalar(@errors) == 0) {
171 MergeHolds
($dbh, $ref_biblionumber, $biblionumber);
172 my $error = DelBiblio
($biblionumber);
173 push @errors, $error if ($error);
180 report_records
=> \
@report_records,
181 report_header
=> $report_header,
182 ref_biblionumber
=> $input->param('ref_biblionumber')
185 #-------------------------
186 # Show records to merge
187 #-------------------------
189 my $ref_biblionumber = $input->param('ref_biblionumber');
191 if ($ref_biblionumber) {
192 my $framework = $input->param('frameworkcode');
193 $framework //= GetFrameworkCode
($ref_biblionumber);
195 # Getting MARC Structure
196 my $tagslib = GetMarcStructure
(1, $framework);
198 my $marcflavour = lc(C4
::Context
->preference('marcflavour'));
200 # Creating a loop for display
202 foreach my $biblionumber (@biblionumbers) {
203 my $marcrecord = GetMarcBiblio
($biblionumber);
204 my $frameworkcode = GetFrameworkCode
($biblionumber);
205 my $recordObj = new Koha
::MetadataRecord
({'record' => $marcrecord, schema
=> $marcflavour});
207 biblionumber
=> $biblionumber,
208 record
=> $marcrecord,
209 frameworkcode
=> $frameworkcode,
210 display
=> $recordObj->createMergeHash($tagslib),
212 if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
213 $record->{reference
} = 1;
214 $template->param(ref_record
=> $record);
215 unshift @records, $record;
217 push @records, $record;
221 my ($biblionumbertag) = GetMarcFromKohaField
('biblio.biblionumber');
225 ref_biblionumber
=> $ref_biblionumber,
226 records
=> \
@records,
227 ref_record
=> $records[0],
228 framework
=> $framework,
229 biblionumbertag
=> $biblionumbertag,
230 MergeReportFields
=> C4
::Context
->preference('MergeReportFields'),
234 foreach my $biblionumber (@biblionumbers) {
235 my $frameworkcode = GetFrameworkCode
($biblionumber);
237 biblionumber
=> $biblionumber,
238 data
=> GetBiblioData
($biblionumber),
239 frameworkcode
=> $frameworkcode,
241 push @records, $record;
243 # Ask the user to choose which record will be the kept
245 choosereference
=> 1,
246 records
=> \
@records,
249 my $frameworks = getframeworks
;
251 foreach my $thisframeworkcode ( keys %$frameworks ) {
253 value
=> $thisframeworkcode,
254 frameworktext
=> $frameworks->{$thisframeworkcode}->{'frameworktext'},
256 push @frameworkselect, \
%row;
259 frameworkselect
=> \
@frameworkselect,
266 $template->param( errors
=> \
@errors );
269 output_html_with_http_headers
$input, $cookie, $template->output;