Bug 11772: (follow-up) make warning about no active currency a link to currency setup...
[koha.git] / catalogue / detail.pl
blob3f71a875f755fe3923bc3eeb7dc46b7db2d395f0
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
19 use strict;
20 use warnings;
22 use CGI;
23 use C4::Acquisition qw( GetHistory );
24 use C4::Auth;
25 use C4::Dates qw/format_date/;
26 use C4::Koha;
27 use C4::Serials; #uses getsubscriptionfrom biblionumber
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Circulation;
32 use C4::Branch;
33 use C4::Reserves;
34 use C4::Members; # to use GetMember
35 use C4::Serials;
36 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
37 use C4::External::Amazon;
38 use C4::Search; # enabled_staff_search_views
39 use C4::Tags qw(get_tags);
40 use C4::VirtualShelves;
41 use C4::XSLT;
42 use C4::Images;
43 use Koha::DateUtils;
44 use C4::HTML5Media;
45 use C4::CourseReserves qw(GetItemCourseReservesInfo);
46 use C4::Acquisition qw(GetOrdersByBiblionumber);
48 my $query = CGI->new();
50 my $analyze = $query->param('analyze');
52 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
54 template_name => 'catalogue/detail.tmpl',
55 query => $query,
56 type => "intranet",
57 authnotrequired => 0,
58 flagsrequired => { catalogue => 1 },
62 my $biblionumber = $query->param('biblionumber');
63 my $record = GetMarcBiblio($biblionumber);
65 if ( not defined $record ) {
66 # biblionumber invalid -> report and exit
67 $template->param( unknownbiblionumber => 1,
68 biblionumber => $biblionumber );
69 output_html_with_http_headers $query, $cookie, $template->output;
70 exit;
73 if($query->cookie("holdfor")){
74 my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
75 $template->param(
76 holdfor => $query->cookie("holdfor"),
77 holdfor_surname => $holdfor_patron->{'surname'},
78 holdfor_firstname => $holdfor_patron->{'firstname'},
79 holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
83 my $fw = GetFrameworkCode($biblionumber);
84 my $showallitems = $query->param('showallitems');
85 my $marcflavour = C4::Context->preference("marcflavour");
87 # XSLT processing of some stuff
88 if (C4::Context->preference("XSLTDetailsDisplay") ) {
89 $template->param('XSLTDetailsDisplay' =>'1',
90 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "XSLTDetailsDisplay") );
93 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
94 $template->param( ocoins => GetCOinSBiblio($record) );
96 # some useful variables for enhanced content;
97 # in each case, we're grabbing the first value we find in
98 # the record and normalizing it
99 my $upc = GetNormalizedUPC($record,$marcflavour);
100 my $ean = GetNormalizedEAN($record,$marcflavour);
101 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
102 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
104 $template->param(
105 normalized_upc => $upc,
106 normalized_ean => $ean,
107 normalized_oclc => $oclc,
108 normalized_isbn => $isbn,
111 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
112 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
113 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
114 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
115 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
116 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
117 my $marchostsarray = GetMarcHosts($record,$marcflavour);
118 my $subtitle = GetRecordValue('subtitle', $record, $fw);
120 # Get Branches, Itemtypes and Locations
121 my $branches = GetBranches();
122 my $itemtypes = GetItemTypes();
123 my $dbh = C4::Context->dbh;
125 my @all_items = GetItemsInfo( $biblionumber );
126 my @items;
127 for my $itm (@all_items) {
128 push @items, $itm unless ( $itm->{itemlost} && GetHideLostItemsPreference($borrowernumber) && !$showallitems);
131 # flag indicating existence of at least one item linked via a host record
132 my $hostrecords;
133 # adding items linked via host biblios
134 my @hostitems = GetHostItemsInfo($record);
135 if (@hostitems){
136 $hostrecords =1;
137 push (@items,@hostitems);
140 my $dat = &GetBiblioData($biblionumber);
142 #coping with subscriptions
143 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
144 my @subscriptions = GetSubscriptions( $dat->{title}, $dat->{issn}, undef, $biblionumber );
145 my @subs;
147 foreach my $subscription (@subscriptions) {
148 my %cell;
149 my $serials_to_display;
150 $cell{subscriptionid} = $subscription->{subscriptionid};
151 $cell{subscriptionnotes} = $subscription->{internalnotes};
152 $cell{missinglist} = $subscription->{missinglist};
153 $cell{librariannote} = $subscription->{librariannote};
154 $cell{branchcode} = $subscription->{branchcode};
155 $cell{branchname} = GetBranchName($subscription->{branchcode});
156 $cell{hasalert} = $subscription->{hasalert};
157 $cell{callnumber} = $subscription->{callnumber};
158 $cell{closed} = $subscription->{closed};
159 #get the three latest serials.
160 $serials_to_display = $subscription->{staffdisplaycount};
161 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
162 $cell{staffdisplaycount} = $serials_to_display;
163 $cell{latestserials} =
164 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
165 push @subs, \%cell;
169 # Get acquisition details
170 if ( C4::Context->preference('AcquisitionDetails') ) {
171 my ( $orders, $qty, $price, $received ) = C4::Acquisition::GetHistory( biblionumber => $biblionumber, get_canceled_order => 1 );
172 $template->param(
173 orders => $orders,
177 if ( defined $dat->{'itemtype'} ) {
178 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
181 $dat->{'count'} = scalar @all_items + @hostitems;
182 $dat->{'showncount'} = scalar @items + @hostitems;
183 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
185 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
186 my $collections = GetKohaAuthorisedValues('items.ccode' , $fw);
187 my $copynumbers = GetKohaAuthorisedValues('items.copynumber', $fw);
188 my (@itemloop, @otheritemloop, %itemfields);
189 my $norequests = 1;
190 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
191 my $authvalcode_items_damaged = GetAuthValCode('items.damaged', $fw);
193 my $analytics_flag;
194 my $materials_flag; # set this if the items have anything in the materials field
195 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
196 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
197 $template->param(SeparateHoldings => 1);
199 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
200 foreach my $item (@items) {
201 my $itembranchcode = $item->{$separatebranch};
202 $item->{homebranch} = GetBranchName($item->{homebranch});
204 # can place holds defaults to yes
205 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
207 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
208 : '';
210 foreach (qw(datelastseen onloan)) {
211 $item->{$_} = format_date($item->{$_});
213 $item->{datedue} = format_sqldatetime($item->{datedue});
214 # item damaged, lost, withdrawn loops
215 $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
216 if ($item->{damaged}) {
217 $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
219 #get shelf location and collection code description if they are authorised value.
220 # same thing for copy number
221 my $shelfcode = $item->{'location'};
222 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
223 my $ccode = $item->{'ccode'};
224 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
225 my $copynumber = $item->{'copynumber'};
226 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
227 foreach (qw(ccode enumchron copynumber itemnotes uri)) {
228 $itemfields{$_} = 1 if ( $item->{$_} );
231 # checking for holds
232 my ($reservedate,$reservedfor,$expectedAt,undef,$wait) = GetReservesFromItemnumber($item->{itemnumber});
233 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
235 if (C4::Context->preference('HidePatronName')){
236 $item->{'hidepatronname'} = 1;
239 if ( defined $reservedate ) {
240 $item->{backgroundcolor} = 'reserved';
241 $item->{reservedate} = format_date($reservedate);
242 $item->{ReservedForBorrowernumber} = $reservedfor;
243 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
244 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
245 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
246 $item->{Reservedcardnumber} = $ItemBorrowerReserveInfo->{'cardnumber'};
247 # Check waiting status
248 $item->{waitingdate} = $wait;
252 # Check the transit status
253 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
254 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
255 $item->{transfertwhen} = format_date($transfertwhen);
256 $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
257 $item->{transfertto} = $branches->{$transfertto}{branchname};
258 $item->{nocancel} = 1;
261 # item has a host number if its biblio number does not match the current bib
262 if ($item->{biblionumber} ne $biblionumber){
263 $item->{hostbiblionumber} = $item->{biblionumber};
264 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
267 #count if item is used in analytical bibliorecords
268 my $countanalytics= GetAnalyticsCount($item->{itemnumber});
269 if ($countanalytics > 0){
270 $analytics_flag=1;
271 $item->{countanalytics} = $countanalytics;
274 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
275 $materials_flag = 1;
278 if ( C4::Context->preference('UseCourseReserves') ) {
279 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
282 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
283 and C4::Context->preference('SeparateHoldings')) {
284 if ($itembranchcode and $itembranchcode eq $currentbranch) {
285 push @itemloop, $item;
286 } else {
287 push @otheritemloop, $item;
289 } else {
290 push @itemloop, $item;
294 # Display only one tab if one items list is empty
295 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
296 $template->param(SeparateHoldings => 0);
297 if (scalar(@itemloop) == 0) {
298 @itemloop = @otheritemloop;
302 $template->param( norequests => $norequests );
303 $template->param(
304 MARCNOTES => $marcnotesarray,
305 MARCSUBJCTS => $marcsubjctsarray,
306 MARCAUTHORS => $marcauthorsarray,
307 MARCSERIES => $marcseriesarray,
308 MARCURLS => $marcurlsarray,
309 MARCISBNS => $marcisbnsarray,
310 MARCHOSTS => $marchostsarray,
311 subtitle => $subtitle,
312 itemdata_ccode => $itemfields{ccode},
313 itemdata_enumchron => $itemfields{enumchron},
314 itemdata_uri => $itemfields{uri},
315 itemdata_copynumber => $itemfields{copynumber},
316 volinfo => $itemfields{enumchron},
317 itemdata_itemnotes => $itemfields{itemnotes},
318 z3950_search_params => C4::Search::z3950_search_args($dat),
319 hostrecords => $hostrecords,
320 analytics_flag => $analytics_flag,
321 C4::Search::enabled_staff_search_views,
322 materials => $materials_flag,
325 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
326 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
327 my $subfields = substr $fieldspec, 3;
328 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
329 my @alternateholdingsinfo = ();
330 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
332 for my $field (@holdingsfields) {
333 my %holding = ( holding => '' );
334 my $havesubfield = 0;
335 for my $subfield ($field->subfields()) {
336 if ((index $subfields, $$subfield[0]) >= 0) {
337 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
338 $holding{'holding'} .= $$subfield[1];
339 $havesubfield++;
342 if ($havesubfield) {
343 push(@alternateholdingsinfo, \%holding);
347 $template->param(
348 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
352 my @results = ( $dat, );
353 foreach ( keys %{$dat} ) {
354 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
357 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
358 # method query not found?!?!
359 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
360 $template->param(
361 itemloop => \@itemloop,
362 otheritemloop => \@otheritemloop,
363 biblionumber => $biblionumber,
364 ($analyze? 'analyze':'detailview') =>1,
365 subscriptions => \@subs,
366 subscriptionsnumber => $subscriptionsnumber,
367 subscriptiontitle => $dat->{title},
368 searchid => $query->param('searchid'),
371 # $debug and $template->param(debug_display => 1);
373 # Lists
375 if (C4::Context->preference("virtualshelves") ) {
376 $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
379 # XISBN Stuff
380 if (C4::Context->preference("FRBRizeEditions")==1) {
381 eval {
382 $template->param(
383 XISBNS => get_xisbns($isbn)
386 if ($@) { warn "XISBN Failed $@"; }
389 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
390 my @images = ListImagesForBiblio($biblionumber);
391 $template->{VARS}->{localimages} = \@images;
394 # HTML5 Media
395 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
396 $template->param( C4::HTML5Media->gethtml5media($record));
400 # Get OPAC URL
401 if (C4::Context->preference('OPACBaseURL')){
402 $template->param( OpacUrl => C4::Context->preference('OPACBaseURL') );
405 # Displaying tags
407 my $tag_quantity;
408 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
409 $template->param(
410 TagsEnabled => 1,
411 TagsShowOnDetail => $tag_quantity
413 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
414 'sort'=>'-weight', limit=>$tag_quantity}));
417 #we only need to pass the number of holds to the template
418 my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
419 $template->param( holdcount => scalar ( @$holds ) );
421 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
422 if ($StaffDetailItemSelection) {
423 # Only enable item selection if user can execute at least one action
424 if (
425 $flags->{superlibrarian}
426 || (
427 ref $flags->{tools} eq 'HASH' && (
428 $flags->{tools}->{items_batchmod} # Modify selected items
429 || $flags->{tools}->{items_batchdel} # Delete selected items
432 || ( ref $flags->{tools} eq '' && $flags->{tools} )
435 $template->param(
436 StaffDetailItemSelection => $StaffDetailItemSelection );
440 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
441 my @deletedorders_using_biblio;
442 my @orders_using_biblio;
443 my @baskets_orders;
444 my @baskets_deletedorders;
446 foreach my $myorder (@allorders_using_biblio) {
447 my $basket = $myorder->{'basketno'};
448 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
449 push @deletedorders_using_biblio, $myorder;
450 unless (grep(/^$basket$/, @baskets_deletedorders)){
451 push @baskets_deletedorders,$myorder->{'basketno'};
454 else {
455 push @orders_using_biblio, $myorder;
456 unless (grep(/^$basket$/, @baskets_orders)){
457 push @baskets_orders,$myorder->{'basketno'};
462 my $count_orders_using_biblio = scalar @orders_using_biblio ;
463 $template->param (countorders => $count_orders_using_biblio);
465 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
466 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
468 $template->param (basketsorders => \@baskets_orders);
469 $template->param (basketsdeletedorders => \@baskets_deletedorders);
471 output_html_with_http_headers $query, $cookie, $template->output;