Bug 17737: Rename holds_placed_before_today with current_holds
[koha.git] / catalogue / detail.pl
blob63ba1ad3ad73cfe330d28b2d836bfa32b36b428d
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use HTML::Entities;
23 use C4::Acquisition qw( GetHistory );
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Serials; #uses getsubscriptionfrom biblionumber
27 use C4::Output;
28 use C4::Biblio;
29 use C4::Items;
30 use C4::Circulation;
31 use C4::Reserves;
32 use C4::Members; # to use GetMember
33 use C4::Serials;
34 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
35 use C4::External::Amazon;
36 use C4::Search; # enabled_staff_search_views
37 use C4::Tags qw(get_tags);
38 use C4::XSLT;
39 use C4::Images;
40 use Koha::DateUtils;
41 use C4::HTML5Media;
42 use C4::CourseReserves qw(GetItemCourseReservesInfo);
43 use C4::Acquisition qw(GetOrdersByBiblionumber);
44 use Koha::AuthorisedValues;
45 use Koha::Biblios;
46 use Koha::Items;
47 use Koha::Patrons;
48 use Koha::Virtualshelves;
50 my $query = CGI->new();
52 my $analyze = $query->param('analyze');
54 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
56 template_name => 'catalogue/detail.tt',
57 query => $query,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => { catalogue => 1 },
64 my $biblionumber = $query->param('biblionumber');
65 $biblionumber = HTML::Entities::encode($biblionumber);
66 my $record = GetMarcBiblio($biblionumber);
68 if ( not defined $record ) {
69 # biblionumber invalid -> report and exit
70 $template->param( unknownbiblionumber => 1,
71 biblionumber => $biblionumber );
72 output_html_with_http_headers $query, $cookie, $template->output;
73 exit;
76 if($query->cookie("holdfor")){
77 my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
78 $template->param(
79 holdfor => $query->cookie("holdfor"),
80 holdfor_surname => $holdfor_patron->{'surname'},
81 holdfor_firstname => $holdfor_patron->{'firstname'},
82 holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
86 my $fw = GetFrameworkCode($biblionumber);
87 my $showallitems = $query->param('showallitems');
88 my $marcflavour = C4::Context->preference("marcflavour");
90 # XSLT processing of some stuff
91 my $xslfile = C4::Context->preference('XSLTDetailsDisplay');
92 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
93 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
95 if ( $xslfile ) {
96 $template->param(
97 XSLTDetailsDisplay => '1',
98 XSLTBloc => XSLTParse4Display(
99 $biblionumber, $record, "XSLTDetailsDisplay",
100 1, undef, $sysxml, $xslfile, $lang
105 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
106 $template->param( ocoins => GetCOinSBiblio($record) );
108 # some useful variables for enhanced content;
109 # in each case, we're grabbing the first value we find in
110 # the record and normalizing it
111 my $upc = GetNormalizedUPC($record,$marcflavour);
112 my $ean = GetNormalizedEAN($record,$marcflavour);
113 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
114 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
116 $template->param(
117 normalized_upc => $upc,
118 normalized_ean => $ean,
119 normalized_oclc => $oclc,
120 normalized_isbn => $isbn,
123 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
124 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
125 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
126 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
127 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
128 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
129 my $marchostsarray = GetMarcHosts($record,$marcflavour);
130 my $subtitle = GetRecordValue('subtitle', $record, $fw);
132 # Get Branches, Itemtypes and Locations
133 my $itemtypes = GetItemTypes();
134 my $dbh = C4::Context->dbh;
136 my @all_items = GetItemsInfo( $biblionumber );
137 my @items;
138 my $patron = Koha::Patrons->find( $borrowernumber );
139 for my $itm (@all_items) {
140 push @items, $itm unless ( $itm->{itemlost} && $patron->category->hidelostitems && !$showallitems);
143 # flag indicating existence of at least one item linked via a host record
144 my $hostrecords;
145 # adding items linked via host biblios
146 my @hostitems = GetHostItemsInfo($record);
147 if (@hostitems){
148 $hostrecords =1;
149 push (@items,@hostitems);
152 my $dat = &GetBiblioData($biblionumber);
154 #coping with subscriptions
155 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
156 my @subscriptions = SearchSubscriptions({ biblionumber => $biblionumber, orderby => 'title' });
157 my @subs;
159 foreach my $subscription (@subscriptions) {
160 my %cell;
161 my $serials_to_display;
162 $cell{subscriptionid} = $subscription->{subscriptionid};
163 $cell{subscriptionnotes} = $subscription->{internalnotes};
164 $cell{missinglist} = $subscription->{missinglist};
165 $cell{librariannote} = $subscription->{librariannote};
166 $cell{branchcode} = $subscription->{branchcode};
167 $cell{hasalert} = $subscription->{hasalert};
168 $cell{callnumber} = $subscription->{callnumber};
169 $cell{closed} = $subscription->{closed};
170 #get the three latest serials.
171 $serials_to_display = $subscription->{staffdisplaycount};
172 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
173 $cell{staffdisplaycount} = $serials_to_display;
174 $cell{latestserials} =
175 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
176 push @subs, \%cell;
180 # Get acquisition details
181 if ( C4::Context->preference('AcquisitionDetails') ) {
182 my $orders = C4::Acquisition::GetHistory( biblionumber => $biblionumber, get_canceled_order => 1 );
183 $template->param(
184 orders => $orders,
188 if ( defined $dat->{'itemtype'} ) {
189 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
192 $dat->{'count'} = scalar @all_items + @hostitems;
193 $dat->{'showncount'} = scalar @items + @hostitems;
194 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
196 my $shelflocations =
197 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) };
198 my $collections =
199 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
200 my $copynumbers =
201 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
202 my (@itemloop, @otheritemloop, %itemfields);
203 my $norequests = 1;
205 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => { not => undef } });
206 if ( $mss->count ) {
207 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
209 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => { not => undef } });
210 if ( $mss->count ) {
211 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
214 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => { not => undef } });
215 my %materials_map;
216 if ($mss->count) {
217 my $materials_authvals = GetAuthorisedValues($mss->next->authorised_value);
218 if ($materials_authvals) {
219 foreach my $value (@$materials_authvals) {
220 $materials_map{$value->{authorised_value}} = $value->{lib};
225 my $analytics_flag;
226 my $materials_flag; # set this if the items have anything in the materials field
227 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
228 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
229 $template->param(SeparateHoldings => 1);
231 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
232 foreach my $item (@items) {
233 my $itembranchcode = $item->{$separatebranch};
235 # can place holds defaults to yes
236 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
238 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
239 : '';
241 $item->{datedue} = format_sqldatetime($item->{datedue});
243 #get shelf location and collection code description if they are authorised value.
244 # same thing for copy number
245 my $shelfcode = $item->{'location'};
246 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
247 my $ccode = $item->{'ccode'};
248 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
249 my $copynumber = $item->{'copynumber'};
250 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
251 foreach (qw(ccode enumchron copynumber stocknumber itemnotes itemnotes_nonpublic uri)) {
252 $itemfields{$_} = 1 if ( $item->{$_} );
255 if (C4::Context->preference('HidePatronName')){
256 $item->{'hidepatronname'} = 1;
260 # checking for holds
261 my $item_object = Koha::Items->find( $item->{itemnumber} );
262 my $holds = $item_object->current_holds;
263 if ( my $first_hold = $holds->next ) {
264 my $ItemBorrowerReserveInfo = C4::Members::GetMember( borrowernumber => $first_hold->borrowernumber); # FIXME could be improved
265 $item->{backgroundcolor} = 'reserved';
266 $item->{reservedate} = $first_hold->reservedate;
267 $item->{ReservedForBorrowernumber} = $first_hold->borrowernumber;
268 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
269 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
270 $item->{ExpectedAtLibrary} = $first_hold->branchcode;
271 $item->{Reservedcardnumber} = $ItemBorrowerReserveInfo->{'cardnumber'};
272 # Check waiting status
273 $item->{waitingdate} = $first_hold->waitingdate;
277 # Check the transit status
278 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
279 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
280 $item->{transfertwhen} = $transfertwhen;
281 $item->{transfertfrom} = $transfertfrom;
282 $item->{transfertto} = $transfertto;
283 $item->{nocancel} = 1;
286 foreach my $f (qw( itemnotes )) {
287 if ($item->{$f}) {
288 $item->{$f} =~ s|\n|<br />|g;
289 $itemfields{$f} = 1;
293 #item has a host number if its biblio number does not match the current bib
295 if ($item->{biblionumber} ne $biblionumber){
296 $item->{hostbiblionumber} = $item->{biblionumber};
297 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
300 #count if item is used in analytical bibliorecords
301 my $countanalytics= GetAnalyticsCount($item->{itemnumber});
302 if ($countanalytics > 0){
303 $analytics_flag=1;
304 $item->{countanalytics} = $countanalytics;
307 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
308 $materials_flag = 1;
309 if (defined $materials_map{ $item->{materials} }) {
310 $item->{materials} = $materials_map{ $item->{materials} };
314 if ( C4::Context->preference('UseCourseReserves') ) {
315 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
318 if ( C4::Context->preference('IndependentBranches') ) {
319 my $userenv = C4::Context->userenv();
320 if ( not C4::Context->IsSuperLibrarian()
321 and $userenv->{branch} ne $item->{homebranch} ) {
322 $item->{cannot_be_edited} = 1;
326 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
327 and C4::Context->preference('SeparateHoldings')) {
328 if ($itembranchcode and $itembranchcode eq $currentbranch) {
329 push @itemloop, $item;
330 } else {
331 push @otheritemloop, $item;
333 } else {
334 push @itemloop, $item;
338 # Display only one tab if one items list is empty
339 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
340 $template->param(SeparateHoldings => 0);
341 if (scalar(@itemloop) == 0) {
342 @itemloop = @otheritemloop;
346 $template->param( norequests => $norequests );
347 $template->param(
348 MARCNOTES => $marcnotesarray,
349 MARCSUBJCTS => $marcsubjctsarray,
350 MARCAUTHORS => $marcauthorsarray,
351 MARCSERIES => $marcseriesarray,
352 MARCURLS => $marcurlsarray,
353 MARCISBNS => $marcisbnsarray,
354 MARCHOSTS => $marchostsarray,
355 subtitle => $subtitle,
356 itemdata_ccode => $itemfields{ccode},
357 itemdata_enumchron => $itemfields{enumchron},
358 itemdata_uri => $itemfields{uri},
359 itemdata_copynumber => $itemfields{copynumber},
360 itemdata_stocknumber => $itemfields{stocknumber},
361 volinfo => $itemfields{enumchron},
362 itemdata_itemnotes => $itemfields{itemnotes},
363 itemdata_nonpublicnotes => $itemfields{itemnotes_nonpublic},
364 z3950_search_params => C4::Search::z3950_search_args($dat),
365 hostrecords => $hostrecords,
366 analytics_flag => $analytics_flag,
367 C4::Search::enabled_staff_search_views,
368 materials => $materials_flag,
371 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
372 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
373 my $subfields = substr $fieldspec, 3;
374 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
375 my @alternateholdingsinfo = ();
376 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
378 for my $field (@holdingsfields) {
379 my %holding = ( holding => '' );
380 my $havesubfield = 0;
381 for my $subfield ($field->subfields()) {
382 if ((index $subfields, $$subfield[0]) >= 0) {
383 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
384 $holding{'holding'} .= $$subfield[1];
385 $havesubfield++;
388 if ($havesubfield) {
389 push(@alternateholdingsinfo, \%holding);
393 $template->param(
394 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
398 my @results = ( $dat, );
399 foreach ( keys %{$dat} ) {
400 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
403 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
404 # method query not found?!?!
405 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
406 $template->param(
407 itemloop => \@itemloop,
408 otheritemloop => \@otheritemloop,
409 biblionumber => $biblionumber,
410 ($analyze? 'analyze':'detailview') =>1,
411 subscriptions => \@subs,
412 subscriptionsnumber => $subscriptionsnumber,
413 subscriptiontitle => $dat->{title},
414 searchid => scalar $query->param('searchid'),
417 # $debug and $template->param(debug_display => 1);
419 # Lists
421 if (C4::Context->preference("virtualshelves") ) {
422 my $shelves = Koha::Virtualshelves->search(
424 biblionumber => $biblionumber,
425 category => 2,
428 join => 'virtualshelfcontents',
431 $template->param( 'shelves' => $shelves );
434 # XISBN Stuff
435 if (C4::Context->preference("FRBRizeEditions")==1) {
436 eval {
437 $template->param(
438 XISBNS => get_xisbns($isbn)
441 if ($@) { warn "XISBN Failed $@"; }
444 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
445 my @images = ListImagesForBiblio($biblionumber);
446 $template->{VARS}->{localimages} = \@images;
449 # HTML5 Media
450 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
451 $template->param( C4::HTML5Media->gethtml5media($record));
454 # Displaying tags
456 my $tag_quantity;
457 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
458 $template->param(
459 TagsEnabled => 1,
460 TagsShowOnDetail => $tag_quantity
462 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
463 'sort'=>'-weight', limit=>$tag_quantity}));
466 #we only need to pass the number of holds to the template
467 my $biblio = Koha::Biblios->find( $biblionumber );
468 my $holds = $biblio->holds;
469 $template->param( holdcount => $holds->count );
471 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
472 if ($StaffDetailItemSelection) {
473 # Only enable item selection if user can execute at least one action
474 if (
475 $flags->{superlibrarian}
476 || (
477 ref $flags->{tools} eq 'HASH' && (
478 $flags->{tools}->{items_batchmod} # Modify selected items
479 || $flags->{tools}->{items_batchdel} # Delete selected items
482 || ( ref $flags->{tools} eq '' && $flags->{tools} )
485 $template->param(
486 StaffDetailItemSelection => $StaffDetailItemSelection );
490 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
491 my @deletedorders_using_biblio;
492 my @orders_using_biblio;
493 my @baskets_orders;
494 my @baskets_deletedorders;
496 foreach my $myorder (@allorders_using_biblio) {
497 my $basket = $myorder->{'basketno'};
498 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
499 push @deletedorders_using_biblio, $myorder;
500 unless (grep(/^$basket$/, @baskets_deletedorders)){
501 push @baskets_deletedorders,$myorder->{'basketno'};
504 else {
505 push @orders_using_biblio, $myorder;
506 unless (grep(/^$basket$/, @baskets_orders)){
507 push @baskets_orders,$myorder->{'basketno'};
512 my $count_orders_using_biblio = scalar @orders_using_biblio ;
513 $template->param (countorders => $count_orders_using_biblio);
515 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
516 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
518 $template->param (basketsorders => \@baskets_orders);
519 $template->param (basketsdeletedorders => \@baskets_deletedorders);
521 output_html_with_http_headers $query, $cookie, $template->output;