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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #use warnings; FIXME - Bug 2505
31 use C4
::Reserves qw
/MergeHolds/;
32 use C4
::Acquisition qw
/ModOrder GetOrdersByBiblionumber/;
35 my @biblionumber = $input->param('biblionumber');
36 my $merge = $input->param('merge');
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
42 template_name
=> "cataloguing/merge.tmpl",
46 flagsrequired
=> { editcatalogue
=> 'edit_catalogue' },
50 #------------------------
52 #------------------------
55 my $dbh = C4
::Context
->dbh;
58 # Creating a new record from the html code
59 my $record = TransformHtmlToMarc
( $input );
60 my $tobiblio = $input->param('biblio1');
61 my $frombiblio = $input->param('biblio2');
63 # Rewriting the leader
64 $record->leader(GetMarcBiblio
($tobiblio)->leader());
66 my $frameworkcode = $input->param('frameworkcode');
69 # Modifying the reference record
70 ModBiblio
($record, $tobiblio, $frameworkcode);
72 # Moving items from the other record to the reference record
73 # Also moving orders from the other record to the reference record, only if the order is linked to an item of the other record
74 my $itemnumbers = get_itemnumbers_of
($frombiblio);
75 foreach my $itloop ($itemnumbers->{$frombiblio}) {
76 foreach my $itemnumber (@
$itloop) {
77 my $res = MoveItemFromBiblio
($itemnumber, $frombiblio, $tobiblio);
78 if (not defined $res) {
79 push @notmoveditems, $itemnumber;
83 # If some items could not be moved :
84 if (scalar(@notmoveditems) > 0) {
85 my $itemlist = join(' ',@notmoveditems);
86 push @errors, { code
=> "CANNOT_MOVE", value
=> $itemlist };
89 # Moving subscriptions from the other record to the reference record
90 my $subcount = CountSubscriptionFromBiblionumber
($frombiblio);
92 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
93 $sth->execute($tobiblio, $frombiblio);
95 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
96 $sth->execute($tobiblio, $frombiblio);
101 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
102 $sth->execute($tobiblio, $frombiblio);
104 # TODO : Moving reserves
106 # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
107 my @allorders = GetOrdersByBiblionumber
($frombiblio);
108 my @tobiblioitem = GetBiblioItemByBiblioNumber
($tobiblio);
109 my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber
};
110 foreach my $myorder (@allorders) {
111 $myorder->{'biblionumber'} = $tobiblio;
113 # TODO : add error control (in ModOrder?)
116 # Deleting the other record
117 if (scalar(@errors) == 0) {
119 MergeHolds
($dbh,$tobiblio,$frombiblio);
120 my $error = DelBiblio
($frombiblio);
121 push @errors, $error if ($error);
127 biblio1
=> $input->param('biblio1')
130 #-------------------------
131 # Show records to merge
132 #-------------------------
134 my $mergereference = $input->param('mergereference');
135 my $biblionumber = $input->param('biblionumber');
137 if (scalar(@biblionumber) != 2) {
138 push @errors, { code
=> "WRONG_COUNT", value
=> scalar(@biblionumber) };
141 my $data1 = GetBiblioData
($biblionumber[0]);
142 my $record1 = GetMarcBiblio
($biblionumber[0]);
144 my $data2 = GetBiblioData
($biblionumber[1]);
145 my $record2 = GetMarcBiblio
($biblionumber[1]);
147 # Checks if both records use the same framework
148 my $frameworkcode1 = &GetFrameworkCode
($biblionumber[0]);
149 my $frameworkcode2 = &GetFrameworkCode
($biblionumber[1]);
152 my $subtitle1 = GetRecordValue
('subtitle', $record1, $frameworkcode1);
153 my $subtitle2 = GetRecordValue
('subtitle', $record2, $frameworkcode1);
155 if ($mergereference) {
158 if ($frameworkcode1 ne $frameworkcode2) {
159 $framework = $input->param('frameworkcode')
160 or push @errors, "Famework not selected.";
162 $framework = $frameworkcode1;
165 # Getting MARC Structure
166 my $tagslib = GetMarcStructure
(1, $framework);
168 my $notreference = ($biblionumber[0] == $mergereference) ?
$biblionumber[1] : $biblionumber[0];
170 # Creating a loop for display
171 my @record1 = _createMarcHash
(GetMarcBiblio
($mergereference), $tagslib);
172 my @record2 = _createMarcHash
(GetMarcBiblio
($notreference), $tagslib);
176 biblio1
=> $mergereference,
177 biblio2
=> $notreference,
178 mergereference
=> $mergereference,
181 framework
=> $framework,
186 # Ask the user to choose which record will be the kept
188 choosereference
=> 1,
189 biblio1
=> $biblionumber[0],
190 biblio2
=> $biblionumber[1],
191 title1
=> $data1->{'title'},
192 subtitle1
=> $subtitle1,
193 title2
=> $data2->{'title'},
194 subtitle2
=> $subtitle2
196 if ($frameworkcode1 ne $frameworkcode2) {
197 my $frameworks = getframeworks
;
199 foreach my $thisframeworkcode ( keys %$frameworks ) {
201 value
=> $thisframeworkcode,
202 frameworktext
=> $frameworks->{$thisframeworkcode}->{'frameworktext'},
204 if ($frameworkcode1 eq $thisframeworkcode){
205 $row{'selected'} = 1;
207 push @frameworkselect, \
%row;
210 frameworkselect
=> \
@frameworkselect,
211 frameworkcode1
=> $frameworkcode1,
212 frameworkcode2
=> $frameworkcode2,
221 $template->param( errors
=> \
@errors );
224 output_html_with_http_headers
$input, $cookie, $template->output;
231 # ------------------------
233 # ------------------------
234 sub _createMarcHash
{
238 my @fields = $record->fields();
241 foreach my $field (@fields) {
242 my $fieldtag = $field->tag();
243 if ($fieldtag < 10) {
244 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
250 value
=> $field->data(),
256 my @subfields = $field->subfields();
258 foreach my $subfield (@subfields) {
259 if ($tagslib->{$fieldtag}->{@
$subfield[0]}->{'tab'} >= 0) {
260 push @subfield_array, {
261 subtag
=> @
$subfield[0],
262 subkey
=> createKey
(),
263 value
=> @
$subfield[1],
269 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
275 indicator1
=> $field->indicator(1),
276 indicator2
=> $field->indicator(2),
277 subfield
=> [@subfield_array],
291 Create a random value to set it into the input name
296 return int(rand(1000000));