Bug 9780: restrict the ability to delete a bib record associated with an order
[koha.git] / catalogue / detail.pl
blob955dbf5e1b54ae49b882074683a00523cf3dfe48
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::Auth;
24 use C4::Dates qw/format_date/;
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::Branch;
32 use C4::Reserves;
33 use C4::Members; # to use GetMember
34 use C4::Serials;
35 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
36 use C4::External::Amazon;
37 use C4::Search; # enabled_staff_search_views
38 use C4::Tags qw(get_tags);
39 use C4::VirtualShelves;
40 use C4::XSLT;
41 use C4::Images;
42 use Koha::DateUtils;
43 use C4::HTML5Media;
44 use C4::CourseReserves qw(GetItemCourseReservesInfo);
45 use C4::Acquisition qw(GetOrdersByBiblionumber);
47 # use Smart::Comments;
49 my $query = CGI->new();
51 my $analyze = $query->param('analyze');
53 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
55 template_name => 'catalogue/detail.tmpl',
56 query => $query,
57 type => "intranet",
58 authnotrequired => 0,
59 flagsrequired => { catalogue => 1 },
63 my $biblionumber = $query->param('biblionumber');
64 my $record = GetMarcBiblio($biblionumber);
66 if ( not defined $record ) {
67 # biblionumber invalid -> report and exit
68 $template->param( unknownbiblionumber => 1,
69 biblionumber => $biblionumber );
70 output_html_with_http_headers $query, $cookie, $template->output;
71 exit;
74 if($query->cookie("holdfor")){
75 my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
76 $template->param(
77 holdfor => $query->cookie("holdfor"),
78 holdfor_surname => $holdfor_patron->{'surname'},
79 holdfor_firstname => $holdfor_patron->{'firstname'},
80 holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
84 my $fw = GetFrameworkCode($biblionumber);
85 my $showallitems = $query->param('showallitems');
86 my $marcflavour = C4::Context->preference("marcflavour");
88 # XSLT processing of some stuff
89 if (C4::Context->preference("XSLTDetailsDisplay") ) {
90 $template->param('XSLTDetailsDisplay' =>'1',
91 'XSLTBloc' => XSLTParse4Display($biblionumber, $record, "XSLTDetailsDisplay") );
94 $template->param( 'SpineLabelShowPrintOnBibDetails' => C4::Context->preference("SpineLabelShowPrintOnBibDetails") );
95 $template->param( ocoins => GetCOinSBiblio($record) );
97 # some useful variables for enhanced content;
98 # in each case, we're grabbing the first value we find in
99 # the record and normalizing it
100 my $upc = GetNormalizedUPC($record,$marcflavour);
101 my $ean = GetNormalizedEAN($record,$marcflavour);
102 my $oclc = GetNormalizedOCLCNumber($record,$marcflavour);
103 my $isbn = GetNormalizedISBN(undef,$record,$marcflavour);
105 $template->param(
106 normalized_upc => $upc,
107 normalized_ean => $ean,
108 normalized_oclc => $oclc,
109 normalized_isbn => $isbn,
112 my $marcnotesarray = GetMarcNotes( $record, $marcflavour );
113 my $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
114 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
115 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
116 my $marcseriesarray = GetMarcSeries($record,$marcflavour);
117 my $marcurlsarray = GetMarcUrls ($record,$marcflavour);
118 my $marchostsarray = GetMarcHosts($record,$marcflavour);
119 my $subtitle = GetRecordValue('subtitle', $record, $fw);
121 # Get Branches, Itemtypes and Locations
122 my $branches = GetBranches();
123 my $itemtypes = GetItemTypes();
124 my $dbh = C4::Context->dbh;
126 my @all_items = GetItemsInfo( $biblionumber );
127 my @items;
128 for my $itm (@all_items) {
129 push @items, $itm unless ( $itm->{itemlost} && GetHideLostItemsPreference($borrowernumber) && !$showallitems);
132 # flag indicating existence of at least one item linked via a host record
133 my $hostrecords;
134 # adding items linked via host biblios
135 my @hostitems = GetHostItemsInfo($record);
136 if (@hostitems){
137 $hostrecords =1;
138 push (@items,@hostitems);
141 my $dat = &GetBiblioData($biblionumber);
143 #coping with subscriptions
144 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
145 my @subscriptions = GetSubscriptions( $dat->{title}, $dat->{issn}, undef, $biblionumber );
146 my @subs;
148 foreach my $subscription (@subscriptions) {
149 my %cell;
150 my $serials_to_display;
151 $cell{subscriptionid} = $subscription->{subscriptionid};
152 $cell{subscriptionnotes} = $subscription->{internalnotes};
153 $cell{missinglist} = $subscription->{missinglist};
154 $cell{librariannote} = $subscription->{librariannote};
155 $cell{branchcode} = $subscription->{branchcode};
156 $cell{branchname} = GetBranchName($subscription->{branchcode});
157 $cell{hasalert} = $subscription->{hasalert};
158 $cell{callnumber} = $subscription->{callnumber};
159 $cell{closed} = $subscription->{closed};
160 #get the three latest serials.
161 $serials_to_display = $subscription->{staffdisplaycount};
162 $serials_to_display = C4::Context->preference('StaffSerialIssueDisplayCount') unless $serials_to_display;
163 $cell{staffdisplaycount} = $serials_to_display;
164 $cell{latestserials} =
165 GetLatestSerials( $subscription->{subscriptionid}, $serials_to_display );
166 push @subs, \%cell;
169 if ( defined $dat->{'itemtype'} ) {
170 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
173 $dat->{'count'} = scalar @all_items + @hostitems;
174 $dat->{'showncount'} = scalar @items + @hostitems;
175 $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items;
177 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
178 my $collections = GetKohaAuthorisedValues('items.ccode' , $fw);
179 my $copynumbers = GetKohaAuthorisedValues('items.copynumber', $fw);
180 my (@itemloop, @otheritemloop, %itemfields);
181 my $norequests = 1;
182 my $authvalcode_items_itemlost = GetAuthValCode('items.itemlost',$fw);
183 my $authvalcode_items_damaged = GetAuthValCode('items.damaged', $fw);
185 my $analytics_flag;
186 my $materials_flag; # set this if the items have anything in the materials field
187 my $currentbranch = C4::Context->userenv ? C4::Context->userenv->{branch} : undef;
188 if ($currentbranch and C4::Context->preference('SeparateHoldings')) {
189 $template->param(SeparateHoldings => 1);
191 my $separatebranch = C4::Context->preference('SeparateHoldingsBranch') || 'homebranch';
192 foreach my $item (@items) {
193 my $itembranchcode = $item->{$separatebranch};
194 $item->{homebranch} = GetBranchName($item->{homebranch});
196 # can place holds defaults to yes
197 $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
199 $item->{imageurl} = defined $item->{itype} ? getitemtypeimagelocation('intranet', $itemtypes->{ $item->{itype} }{imageurl})
200 : '';
202 foreach (qw(datelastseen onloan)) {
203 $item->{$_} = format_date($item->{$_});
205 $item->{datedue} = format_sqldatetime($item->{datedue});
206 # item damaged, lost, withdrawn loops
207 $item->{itemlostloop} = GetAuthorisedValues($authvalcode_items_itemlost, $item->{itemlost}) if $authvalcode_items_itemlost;
208 if ($item->{damaged}) {
209 $item->{itemdamagedloop} = GetAuthorisedValues($authvalcode_items_damaged, $item->{damaged}) if $authvalcode_items_damaged;
211 #get shelf location and collection code description if they are authorised value.
212 # same thing for copy number
213 my $shelfcode = $item->{'location'};
214 $item->{'location'} = $shelflocations->{$shelfcode} if ( defined( $shelfcode ) && defined($shelflocations) && exists( $shelflocations->{$shelfcode} ) );
215 my $ccode = $item->{'ccode'};
216 $item->{'ccode'} = $collections->{$ccode} if ( defined( $ccode ) && defined($collections) && exists( $collections->{$ccode} ) );
217 my $copynumber = $item->{'copynumber'};
218 $item->{'copynumber'} = $copynumbers->{$copynumber} if ( defined($copynumber) && defined($copynumbers) && exists( $copynumbers->{$copynumber} ) );
219 foreach (qw(ccode enumchron copynumber itemnotes uri)) {
220 $itemfields{$_} = 1 if ( $item->{$_} );
223 # checking for holds
224 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
225 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
227 if (C4::Context->preference('HidePatronName')){
228 $item->{'hidepatronname'} = 1;
231 if ( defined $reservedate ) {
232 $item->{backgroundcolor} = 'reserved';
233 $item->{reservedate} = format_date($reservedate);
234 $item->{ReservedForBorrowernumber} = $reservedfor;
235 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
236 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
237 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
238 $item->{Reservedcardnumber} = $ItemBorrowerReserveInfo->{'cardnumber'};
241 # Check the transit status
242 my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
243 if ( defined( $transfertwhen ) && ( $transfertwhen ne '' ) ) {
244 $item->{transfertwhen} = format_date($transfertwhen);
245 $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
246 $item->{transfertto} = $branches->{$transfertto}{branchname};
247 $item->{nocancel} = 1;
250 # FIXME: move this to a pm, check waiting status for holds
251 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
252 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
253 while (my $wait_hashref = $sth2->fetchrow_hashref) {
254 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
257 # item has a host number if its biblio number does not match the current bib
258 if ($item->{biblionumber} ne $biblionumber){
259 $item->{hostbiblionumber} = $item->{biblionumber};
260 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
263 #count if item is used in analytical bibliorecords
264 my $countanalytics= GetAnalyticsCount($item->{itemnumber});
265 if ($countanalytics > 0){
266 $analytics_flag=1;
267 $item->{countanalytics} = $countanalytics;
270 if (defined($item->{'materials'}) && $item->{'materials'} =~ /\S/){
271 $materials_flag = 1;
274 if ( C4::Context->preference('UseCourseReserves') ) {
275 $item->{'course_reserves'} = GetItemCourseReservesInfo( itemnumber => $item->{'itemnumber'} );
278 if ($currentbranch and $currentbranch ne "NO_LIBRARY_SET"
279 and C4::Context->preference('SeparateHoldings')) {
280 if ($itembranchcode and $itembranchcode eq $currentbranch) {
281 push @itemloop, $item;
282 } else {
283 push @otheritemloop, $item;
285 } else {
286 push @itemloop, $item;
290 # Display only one tab if one items list is empty
291 if (scalar(@itemloop) == 0 || scalar(@otheritemloop) == 0) {
292 $template->param(SeparateHoldings => 0);
293 if (scalar(@itemloop) == 0) {
294 @itemloop = @otheritemloop;
298 $template->param( norequests => $norequests );
299 $template->param(
300 MARCNOTES => $marcnotesarray,
301 MARCSUBJCTS => $marcsubjctsarray,
302 MARCAUTHORS => $marcauthorsarray,
303 MARCSERIES => $marcseriesarray,
304 MARCURLS => $marcurlsarray,
305 MARCISBNS => $marcisbnsarray,
306 MARCHOSTS => $marchostsarray,
307 subtitle => $subtitle,
308 itemdata_ccode => $itemfields{ccode},
309 itemdata_enumchron => $itemfields{enumchron},
310 itemdata_uri => $itemfields{uri},
311 itemdata_copynumber => $itemfields{copynumber},
312 volinfo => $itemfields{enumchron},
313 itemdata_itemnotes => $itemfields{itemnotes},
314 z3950_search_params => C4::Search::z3950_search_args($dat),
315 hostrecords => $hostrecords,
316 analytics_flag => $analytics_flag,
317 C4::Search::enabled_staff_search_views,
318 materials => $materials_flag,
321 if (C4::Context->preference("AlternateHoldingsField") && scalar @items == 0) {
322 my $fieldspec = C4::Context->preference("AlternateHoldingsField");
323 my $subfields = substr $fieldspec, 3;
324 my $holdingsep = C4::Context->preference("AlternateHoldingsSeparator") || ' ';
325 my @alternateholdingsinfo = ();
326 my @holdingsfields = $record->field(substr $fieldspec, 0, 3);
328 for my $field (@holdingsfields) {
329 my %holding = ( holding => '' );
330 my $havesubfield = 0;
331 for my $subfield ($field->subfields()) {
332 if ((index $subfields, $$subfield[0]) >= 0) {
333 $holding{'holding'} .= $holdingsep if (length $holding{'holding'} > 0);
334 $holding{'holding'} .= $$subfield[1];
335 $havesubfield++;
338 if ($havesubfield) {
339 push(@alternateholdingsinfo, \%holding);
343 $template->param(
344 ALTERNATEHOLDINGS => \@alternateholdingsinfo,
348 my @results = ( $dat, );
349 foreach ( keys %{$dat} ) {
350 $template->param( "$_" => defined $dat->{$_} ? $dat->{$_} : '' );
353 # does not work: my %views_enabled = map { $_ => 1 } $template->query(loop => 'EnableViews');
354 # method query not found?!?!
355 $template->param( AmazonTld => get_amazon_tld() ) if ( C4::Context->preference("AmazonCoverImages"));
356 $template->param(
357 itemloop => \@itemloop,
358 otheritemloop => \@otheritemloop,
359 biblionumber => $biblionumber,
360 ($analyze? 'analyze':'detailview') =>1,
361 subscriptions => \@subs,
362 subscriptionsnumber => $subscriptionsnumber,
363 subscriptiontitle => $dat->{title},
364 searchid => $query->param('searchid'),
367 # $debug and $template->param(debug_display => 1);
369 # Lists
371 if (C4::Context->preference("virtualshelves") ) {
372 $template->param( 'GetShelves' => GetBibliosShelves( $biblionumber ) );
375 # XISBN Stuff
376 if (C4::Context->preference("FRBRizeEditions")==1) {
377 eval {
378 $template->param(
379 XISBNS => get_xisbns($isbn)
382 if ($@) { warn "XISBN Failed $@"; }
385 if ( C4::Context->preference("LocalCoverImages") == 1 ) {
386 my @images = ListImagesForBiblio($biblionumber);
387 $template->{VARS}->{localimages} = \@images;
390 # HTML5 Media
391 if ( (C4::Context->preference("HTML5MediaEnabled") eq 'both') or (C4::Context->preference("HTML5MediaEnabled") eq 'staff') ) {
392 $template->param( C4::HTML5Media->gethtml5media($record));
396 # Get OPAC URL
397 if (C4::Context->preference('OPACBaseURL')){
398 $template->param( OpacUrl => C4::Context->preference('OPACBaseURL') );
401 # Displaying tags
403 my $tag_quantity;
404 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
405 $template->param(
406 TagsEnabled => 1,
407 TagsShowOnDetail => $tag_quantity
409 $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
410 'sort'=>'-weight', limit=>$tag_quantity}));
413 my ( $holdcount, $holds ) = C4::Reserves::GetReservesFromBiblionumber($biblionumber,1);
414 $template->param( holdcount => $holdcount, holds => $holds );
415 my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection');
416 if ($StaffDetailItemSelection) {
417 # Only enable item selection if user can execute at least one action
418 if (
419 $flags->{superlibrarian}
420 || (
421 ref $flags->{tools} eq 'HASH' && (
422 $flags->{tools}->{items_batchmod} # Modify selected items
423 || $flags->{tools}->{items_batchdel} # Delete selected items
426 || ( ref $flags->{tools} eq '' && $flags->{tools} )
429 $template->param(
430 StaffDetailItemSelection => $StaffDetailItemSelection );
434 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
435 my @deletedorders_using_biblio;
436 my @orders_using_biblio;
437 my @baskets_orders;
438 my @baskets_deletedorders;
440 foreach my $myorder (@allorders_using_biblio) {
441 my $basket = $myorder->{'basketno'};
442 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
443 push @deletedorders_using_biblio, $myorder;
444 unless (grep(/^$basket$/, @baskets_deletedorders)){
445 push @baskets_deletedorders,$myorder->{'basketno'};
448 else {
449 push @orders_using_biblio, $myorder;
450 unless (grep(/^$basket$/, @baskets_orders)){
451 push @baskets_orders,$myorder->{'basketno'};
456 my $count_orders_using_biblio = scalar @orders_using_biblio ;
457 $template->param (countorders => $count_orders_using_biblio);
459 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
460 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
462 $template->param (basketsorders => \@baskets_orders);
463 $template->param (basketsdeletedorders => \@baskets_deletedorders);
465 output_html_with_http_headers $query, $cookie, $template->output;