Bug 15172: (follow-up)Serial enumchron/sequence not visible when returning/checking...
[koha.git] / catalogue / MARCdetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 =head1 NAME
22
23 MARCdetail.pl : script to show a biblio in MARC format
24
25 =head1 SYNOPSIS
26
27 =cut
28
29 =head1 DESCRIPTION
30
31 This script needs a biblionumber as parameter
32
33 It shows the biblio in a (nice) MARC format depending on MARC
34 parameters tables.
35
36 The template is in <templates_dir>/catalogue/MARCdetail.tt.
37 this template must be divided into 11 "tabs".
38
39 The first 10 tabs present the biblio, the 11th one presents
40 the items attached to the biblio
41
42 =head1 FUNCTIONS
43
44 =cut
45
46 use strict;
47 #use warnings; FIXME - Bug 2505
48
49 use C4::Auth;
50 use C4::Context;
51 use C4::Output;
52 use CGI qw ( -utf8 );
53 use C4::Koha;
54 use MARC::Record;
55 use C4::Biblio;
56 use C4::Items;
57 use C4::Acquisition;
58 use C4::Members; # to use GetMember
59 use C4::Serials;    #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber
60 use C4::Search;         # enabled_staff_search_views
61
62 use List::MoreUtils qw( uniq );
63
64 my $query        = new CGI;
65 my $dbh          = C4::Context->dbh;
66 my $biblionumber = $query->param('biblionumber');
67 my $frameworkcode = $query->param('frameworkcode');
68 $frameworkcode = GetFrameworkCode( $biblionumber ) unless ($frameworkcode);
69 my $popup        =
70   $query->param('popup')
71   ;    # if set to 1, then don't insert links, it's just to show the biblio
72 my $subscriptionid = $query->param('subscriptionid');
73
74 # open template
75 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
76     {
77         template_name   => "catalogue/MARCdetail.tt",
78         query           => $query,
79         type            => "intranet",
80         authnotrequired => 0,
81         flagsrequired   => { catalogue => 1 },
82         debug           => 1,
83     }
84 );
85
86 my $record = GetMarcBiblio($biblionumber, 1);
87 $template->param( ocoins => GetCOinSBiblio($record) );
88
89 if ( not defined $record ) {
90     # biblionumber invalid -> report and exit
91     $template->param( unknownbiblionumber => 1,
92                 biblionumber => $biblionumber
93     );
94     output_html_with_http_headers $query, $cookie, $template->output;
95     exit;
96 }
97
98 my $tagslib = &GetMarcStructure(1,$frameworkcode);
99 my $biblio = GetBiblioData($biblionumber);
100
101 if($query->cookie("holdfor")){ 
102     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
103     $template->param(
104         holdfor => $query->cookie("holdfor"),
105         holdfor_surname => $holdfor_patron->{'surname'},
106         holdfor_firstname => $holdfor_patron->{'firstname'},
107         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
108     );
109 }
110
111 #count of item linked
112 my $itemcount = GetItemsCount($biblionumber);
113 $template->param( count => $itemcount,
114                                         bibliotitle => $biblio->{title}, );
115
116 # Getting the list of all frameworks
117 # get framework list
118 my $frameworks = getframeworks;
119 my @frameworkcodeloop;
120 foreach my $thisframeworkcode ( keys %$frameworks ) {
121     my %row = (
122         value         => $thisframeworkcode,
123         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
124     );
125     if ($frameworkcode eq $thisframeworkcode){
126         $row{'selected'}= 1;
127         }
128     push @frameworkcodeloop, \%row;
129 }
130 $template->param( frameworkcodeloop => \@frameworkcodeloop, );
131 # fill arrays
132 my @loop_data = ();
133
134 # loop through each tab 0 through 9
135 for ( my $tabloop = 0 ; $tabloop <= 10 ; $tabloop++ ) {
136
137     # loop through each tag
138     my @fields    = $record->fields();
139     my @loop_data = ();
140     my @subfields_data;
141
142     # deal with leader
143     unless ( $tagslib->{'000'}->{'@'}->{tab} ne $tabloop )
144     {    #  or ($tagslib->{'000'}->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/ )) {
145         my %subfield_data;
146         $subfield_data{marc_lib}      = $tagslib->{'000'}->{'@'}->{lib};
147         $subfield_data{marc_value}    = $record->leader();
148         $subfield_data{marc_subfield} = '@';
149         $subfield_data{marc_tag}      = '000';
150         push( @subfields_data, \%subfield_data );
151         my %tag_data;
152         $tag_data{tag} = '000';
153         $tag_data{tag_desc} = $tagslib->{'000'}->{lib};
154         my @tmp = @subfields_data;
155         $tag_data{subfield} = \@tmp;
156         push( @loop_data, \%tag_data );
157         undef @subfields_data;
158     }
159     @fields = $record->fields();
160     for ( my $x_i = 0 ; $x_i <= $#fields ; $x_i++ ) {
161
162         # if tag <10, there's no subfield, use the "@" trick
163         if ( $fields[$x_i]->tag() < 10 ) {
164             next
165               if (
166                 $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{tab} ne $tabloop );
167             next if ( $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
168             my %subfield_data;
169             $subfield_data{marc_lib} =
170               $tagslib->{ $fields[$x_i]->tag() }->{'@'}->{lib};
171             $subfield_data{marc_value}    = $fields[$x_i]->data();
172             $subfield_data{marc_subfield} = '@';
173             $subfield_data{marc_tag}      = $fields[$x_i]->tag();
174             push( @subfields_data, \%subfield_data );
175         }
176         else {
177             my @subf = $fields[$x_i]->subfields;
178
179             # loop through each subfield
180             for my $i ( 0 .. $#subf ) {
181                 $subf[$i][0] = "@" unless defined $subf[$i][0];
182                 next
183                   if (
184                     $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{tab}
185                     ne $tabloop );
186                 next
187                   if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
188                     ->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
189                 my %subfield_data;
190                 $subfield_data{short_desc} = $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
191                 $subfield_data{long_desc} =
192                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{lib};
193                 $subfield_data{link} =
194                   $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }->{link};
195
196 #                 warn "tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}."lien koha? : "$subfield_data{link};
197                 if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
198                     ->{isurl} )
199                 {
200                     $subfield_data{marc_value} = $subf[$i][1];
201                                         $subfield_data{is_url} = 1;
202                 }
203                 elsif ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
204                     ->{kohafield} eq "biblioitems.isbn" )
205                 {
206
207 #                    warn " tag : ".$tagslib->{$fields[$x_i]->tag()}." subfield :".$tagslib->{$fields[$x_i]->tag()}->{$subf[$i][0]}. "ISBN : ".$subf[$i][1]."PosttraitementISBN :".DisplayISBN($subf[$i][1]);
208                     $subfield_data{marc_value} = $subf[$i][1];
209                 }
210                 else {
211                     if ( $tagslib->{ $fields[$x_i]->tag() }->{ $subf[$i][0] }
212                         ->{authtypecode} )
213                     {
214                         $subfield_data{authority} = $fields[$x_i]->subfield(9);
215                     }
216                     $subfield_data{marc_value} =
217                       GetAuthorisedValueDesc( $fields[$x_i]->tag(),
218                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
219
220                 }
221                 $subfield_data{marc_subfield} = $subf[$i][0];
222                 $subfield_data{marc_tag}      = $fields[$x_i]->tag();
223                 push( @subfields_data, \%subfield_data );
224             }
225         }
226         if ( $#subfields_data == 0 ) {
227             $subfields_data[0]->{marc_lib}      = '';
228 #            $subfields_data[0]->{marc_subfield} = '';
229         }
230         if ( $#subfields_data >= 0) {
231             my %tag_data;
232             if ( $fields[$x_i]->tag() eq $fields[ $x_i - 1 ]->tag() && (C4::Context->preference('LabelMARCView') eq 'economical')) {
233                 $tag_data{tag} = "";
234             }
235             else {
236                 if ( C4::Context->preference('hide_marc') ) {
237                     $tag_data{tag} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
238                 }
239                 else {
240                     $tag_data{tag} = $fields[$x_i]->tag();
241             $tag_data{tag_ind} = C4::Koha::display_marc_indicators($fields[$x_i]);
242             $tag_data{tag_desc} = $tagslib->{ $fields[$x_i]->tag() }->{lib};
243                 }
244             }
245             my @tmp = @subfields_data;
246             $tag_data{subfield} = \@tmp;
247             push( @loop_data, \%tag_data );
248             undef @subfields_data;
249         }
250     }
251     $template->param( "tab" . $tabloop . "XX" => \@loop_data );
252 }
253
254 # now, build item tab !
255 # the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
256 # loop through each tag
257 # warning : we may have differents number of columns in each row. Thus, we first build a hash, complete it if necessary
258 # then construct template.
259 my @fields = $record->fields();
260 my %witness
261   ; #---- stores the list of subfields used at least once, with the "meaning" of the code
262 my @item_subfield_codes;
263 my @item_loop;
264 my $norequests = 1;
265 foreach my $field (@fields) {
266     next if ( $field->tag() < 10 );
267     my @subf = $field->subfields;
268     my $item;
269
270     # loop through each subfield
271     for my $i ( 0 .. $#subf ) {
272         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{tab} ne 10 );
273         next if ( $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{hidden} =~ /-7|-4|-3|-2|2|3|5|8/);
274         push @item_subfield_codes, $subf[$i][0];
275         $witness{ $subf[$i][0] } =
276         $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{lib};
277         $item->{ $subf[$i][0] } = GetAuthorisedValueDesc( $field->tag(),
278                         $subf[$i][0], $subf[$i][1], '', $tagslib) || $subf[$i][1];
279         $norequests = 0 if $subf[$i][1] ==0 and $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield} eq 'items.notforloan';
280     }
281     push @item_loop, $item if $item;
282 }
283
284 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
285 @item_loop = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @item_loop;
286
287 @item_subfield_codes = uniq @item_subfield_codes;
288 # fill item info
289 my @item_header_loop;
290 for my $subfield_code ( @item_subfield_codes ) {
291     push @item_header_loop, $witness{$subfield_code};
292     for my $item_data ( @item_loop ) {
293         $item_data->{$subfield_code} ||= "&nbsp;"
294     }
295 }
296
297 my $subscriptionscount = CountSubscriptionFromBiblionumber($biblionumber);
298
299 if ($subscriptionscount) {
300     my $subscriptions = GetSubscriptionsFromBiblionumber($biblionumber);
301     my $subscriptiontitle = $subscriptions->[0]{'bibliotitle'};
302     $template->param(
303         subscriptiontitle   => $subscriptiontitle,
304         subscriptionsnumber => $subscriptionscount,
305     );
306 }
307
308 $template->param (
309     norequests              => $norequests,
310     item_loop               => \@item_loop,
311     item_header_loop        => \@item_header_loop,
312     item_subfield_codes     => \@item_subfield_codes,
313     biblionumber            => $biblionumber,
314     popup                   => $popup,
315     hide_marc               => C4::Context->preference('hide_marc'),
316         marcview => 1,
317         z3950_search_params             => C4::Search::z3950_search_args($biblio),
318         C4::Search::enabled_staff_search_views,
319     searchid            => scalar $query->param('searchid'),
320 );
321
322 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
323 my @deletedorders_using_biblio;
324 my @orders_using_biblio;
325 my @baskets_orders;
326 my @baskets_deletedorders;
327
328 foreach my $myorder (@allorders_using_biblio) {
329     my $basket = $myorder->{'basketno'};
330     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
331         push @deletedorders_using_biblio, $myorder;
332         unless (grep(/^$basket$/, @baskets_deletedorders)){
333             push @baskets_deletedorders,$myorder->{'basketno'};
334         }
335     }
336     else {
337         push @orders_using_biblio, $myorder;
338         unless (grep(/^$basket$/, @baskets_orders)){
339             push @baskets_orders,$myorder->{'basketno'};
340             }
341     }
342 }
343
344 my $count_orders_using_biblio = scalar @orders_using_biblio ;
345 $template->param (countorders => $count_orders_using_biblio);
346
347 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
348 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
349
350 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
351 my $holdcount = scalar( @$holds );
352 $template->param( holdcount => scalar ( @$holds ) );
353
354 output_html_with_http_headers $query, $cookie, $template->output;