4 # Copyright 2009 BibLibre
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 #use warnings; FIXME - Bug 2505
31 use C4
::Reserves qw
/MergeHolds/;
32 use C4
::Acquisition qw
/ModOrder GetOrdersByBiblionumber/;
33 use Koha
::MetadataRecord
;
36 my @biblionumber = $input->param('biblionumber');
37 my $merge = $input->param('merge');
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
43 template_name
=> "cataloguing/merge.tt",
47 flagsrequired
=> { editcatalogue
=> 'edit_catalogue' },
51 #------------------------
53 #------------------------
56 my $dbh = C4
::Context
->dbh;
59 # Creating a new record from the html code
60 my $record = TransformHtmlToMarc
( $input );
61 my $tobiblio = $input->param('biblio1');
62 my $frombiblio = $input->param('biblio2');
64 # Rewriting the leader
65 $record->leader(GetMarcBiblio
($tobiblio)->leader());
67 my $frameworkcode = $input->param('frameworkcode');
70 # Modifying the reference record
71 ModBiblio
($record, $tobiblio, $frameworkcode);
73 # Moving items from the other record to the reference record
74 # Also moving orders from the other record to the reference record, only if the order is linked to an item of the other record
75 my $itemnumbers = get_itemnumbers_of
($frombiblio);
76 foreach my $itloop ($itemnumbers->{$frombiblio}) {
77 foreach my $itemnumber (@
$itloop) {
78 my $res = MoveItemFromBiblio
($itemnumber, $frombiblio, $tobiblio);
79 if (not defined $res) {
80 push @notmoveditems, $itemnumber;
84 # If some items could not be moved :
85 if (scalar(@notmoveditems) > 0) {
86 my $itemlist = join(' ',@notmoveditems);
87 push @errors, { code
=> "CANNOT_MOVE", value
=> $itemlist };
90 # Moving subscriptions from the other record to the reference record
91 my $subcount = CountSubscriptionFromBiblionumber
($frombiblio);
93 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
94 $sth->execute($tobiblio, $frombiblio);
96 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
97 $sth->execute($tobiblio, $frombiblio);
102 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
103 $sth->execute($tobiblio, $frombiblio);
105 # TODO : Moving reserves
107 # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
108 my @allorders = GetOrdersByBiblionumber
($frombiblio);
109 my @tobiblioitem = GetBiblioItemByBiblioNumber
($tobiblio);
110 my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber
};
111 foreach my $myorder (@allorders) {
112 $myorder->{'biblionumber'} = $tobiblio;
114 # TODO : add error control (in ModOrder?)
117 # Deleting the other record
118 if (scalar(@errors) == 0) {
120 MergeHolds
($dbh,$tobiblio,$frombiblio);
121 my $error = DelBiblio
($frombiblio);
122 push @errors, $error if ($error);
128 biblio1
=> $input->param('biblio1')
131 #-------------------------
132 # Show records to merge
133 #-------------------------
135 my $mergereference = $input->param('mergereference');
136 my $biblionumber = $input->param('biblionumber');
138 if (scalar(@biblionumber) != 2) {
139 push @errors, { code
=> "WRONG_COUNT", value
=> scalar(@biblionumber) };
142 my $data1 = GetBiblioData
($biblionumber[0]);
143 my $record1 = GetMarcBiblio
($biblionumber[0]);
145 my $data2 = GetBiblioData
($biblionumber[1]);
146 my $record2 = GetMarcBiblio
($biblionumber[1]);
148 # Checks if both records use the same framework
149 my $frameworkcode1 = &GetFrameworkCode
($biblionumber[0]);
150 my $frameworkcode2 = &GetFrameworkCode
($biblionumber[1]);
153 my $subtitle1 = GetRecordValue
('subtitle', $record1, $frameworkcode1);
154 my $subtitle2 = GetRecordValue
('subtitle', $record2, $frameworkcode1);
156 if ($mergereference) {
159 if ($frameworkcode1 ne $frameworkcode2) {
160 $framework = $input->param('frameworkcode')
161 or push @errors, "Famework not selected.";
163 $framework = $frameworkcode1;
166 # Getting MARC Structure
167 my $tagslib = GetMarcStructure
(1, $framework);
169 my $notreference = ($biblionumber[0] == $mergereference) ?
$biblionumber[1] : $biblionumber[0];
171 # Creating a loop for display
173 my $recordObj1 = new Koha
::MetadataRecord
({ 'record' => GetMarcBiblio
($mergereference), 'schema' => lc C4
::Context
->preference('marcflavour') });
174 my $recordObj2 = new Koha
::MetadataRecord
({ 'record' => GetMarcBiblio
($notreference), 'schema' => lc C4
::Context
->preference('marcflavour') });
176 my @record1 = $recordObj1->createMergeHash($tagslib);
177 my @record2 = $recordObj2->createMergeHash($tagslib);
181 biblio1
=> $mergereference,
182 biblio2
=> $notreference,
183 mergereference
=> $mergereference,
186 framework
=> $framework,
191 # Ask the user to choose which record will be the kept
193 choosereference
=> 1,
194 biblio1
=> $biblionumber[0],
195 biblio2
=> $biblionumber[1],
196 title1
=> $data1->{'title'},
197 subtitle1
=> $subtitle1,
198 title2
=> $data2->{'title'},
199 subtitle2
=> $subtitle2
201 if ($frameworkcode1 ne $frameworkcode2) {
202 my $frameworks = getframeworks
;
204 foreach my $thisframeworkcode ( keys %$frameworks ) {
206 value
=> $thisframeworkcode,
207 frameworktext
=> $frameworks->{$thisframeworkcode}->{'frameworktext'},
209 if ($frameworkcode1 eq $thisframeworkcode){
210 $row{'selected'} = 1;
212 push @frameworkselect, \
%row;
215 frameworkselect
=> \
@frameworkselect,
216 frameworkcode1
=> $frameworkcode1,
217 frameworkcode2
=> $frameworkcode2,
226 $template->param( errors
=> \
@errors );
229 output_html_with_http_headers
$input, $cookie, $template->output;
236 # ------------------------
238 # ------------------------