More cleanup of location & ccode authval handling.
[koha.git] / catalogue / detail.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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
17
18
19 use strict;
20 require Exporter;
21 use CGI;
22 use C4::Auth;
23 use C4::Date qw/format_date/;
24 use C4::Koha;
25 use C4::Serials;    #uses getsubscriptionfrom biblionumber
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Circulation;
30 use C4::Branch;
31 use C4::Reserves;
32 use C4::Members;
33 use C4::Serials;
34 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn);
35 use C4::Amazon;
36
37 # use Smart::Comments;
38
39 my $query = new CGI;
40 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41     {
42         template_name   => "catalogue/detail.tmpl",
43         query           => $query,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { catalogue => 1 },
47     }
48 );
49
50 my $biblionumber = $query->param('biblionumber');
51 my $fw = GetFrameworkCode($biblionumber);
52
53 ## get notes and subjects from MARC record
54 my $marcflavour      = C4::Context->preference("marcflavour");
55 my $record           = GetMarcBiblio($biblionumber);
56 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
57 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
58 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
59 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
60
61 # Get Branches, Itemtypes and Locations
62 my $branches = GetBranches();
63 my $itemtypes = GetItemTypes();
64
65 # FIXME: move this to a pm, check waiting status for holds
66 my $dbh = C4::Context->dbh;
67
68 # change back when ive fixed request.pl
69 my @items = &GetItemsInfo( $biblionumber, 'intra' );
70 my $dat = &GetBiblioData($biblionumber);
71
72 if (!$dat) { 
73     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
74         exit;
75 }
76
77 #coping with subscriptions
78 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
79 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
80
81
82
83 my @subs;
84 foreach my $subscription (@subscriptions) {
85     my %cell;
86     $cell{subscriptionid}    = $subscription->{subscriptionid};
87     $cell{subscriptionnotes} = $subscription->{notes};
88
89     #get the three latest serials.
90     $cell{latestserials} =
91       GetLatestSerials( $subscription->{subscriptionid}, 3 );
92     push @subs, \%cell;
93 }
94 $dat->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $dat->{itemtype} }{imageurl};
95 $dat->{'count'} = @items;
96 my @itemloop;
97 my $norequests = 1;
98 foreach my $item (@items) {
99
100     # can place holds defaults to yes
101     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
102
103     # format some item fields for display
104     $item->{ $item->{'publictype'} } = 1;
105     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
106     $item->{datedue} = format_date($item->{datedue});
107     $item->{datelastseen} = format_date($item->{datelastseen});
108     $item->{onloan} = format_date($item->{onloan});
109     # item damaged, lost, withdrawn loops
110     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
111     if ($item->{damaged}) {
112         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
113     }
114     #get shelf location and collection code description if they are authorised value.
115         my $shelflocations = GetKohaAuthorisedValues('items.location',$fw );
116         $item->{'location'} = $shelflocations->{$item->{'location'}} if($shelflocations->{$item->{'location'}}); 
117     my $collections =  GetKohaAuthorisedValues('items.ccode',$fw );
118         $item->{'ccode'} = $collections->{$item->{'ccode'}} if($collections->{$item->{'ccode'}}); 
119
120     # checking for holds
121     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
122     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
123
124     if ( defined $reservedate ) {
125         $item->{backgroundcolor} = 'reserved';
126         $item->{reservedate}     = format_date($reservedate);
127         $item->{ReservedForBorrowernumber}     = $reservedfor;
128         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
129         $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
130         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
131     }
132
133         # Check the transit status
134     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
135     if ( $transfertwhen ne '' ) {
136         $item->{transfertwhen} = format_date($transfertwhen);
137         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
138         $item->{transfertto} = $branches->{$transfertto}{branchname};
139         $item->{nocancel} = 1;
140     }
141
142     # FIXME: move this to a pm, check waiting status for holds
143     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
144     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
145     while (my $wait_hashref = $sth2->fetchrow_hashref) {
146         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
147     }
148
149     push @itemloop, $item;
150 }
151
152 $template->param( norequests => $norequests );
153
154     $template->param(
155         MARCNOTES   => $marcnotesarray,
156         MARCSUBJCTS => $marcsubjctsarray,
157         MARCAUTHORS => $marcauthorsarray,
158         MARCSERIES  => $marcseriesarray
159     );
160
161 my @results = ( $dat, );
162 foreach ( keys %{$dat} ) {
163     $template->param( "$_" => $dat->{$_} . "" );
164 }
165
166 $template->param(
167     itemloop        => \@itemloop,
168     biblionumber        => $biblionumber,
169     detailview => 1,
170     subscriptions       => \@subs,
171     subscriptionsnumber => $subscriptionsnumber,
172     subscriptiontitle   => $dat->{title},
173 );
174
175 # XISBN Stuff
176 my $xisbn=$dat->{'isbn'};
177 $xisbn =~ s/(p|-| |:)//g;
178 $template->param(amazonisbn => $xisbn);
179 if (C4::Context->preference("FRBRizeEditions")==1) {
180     eval {
181         $template->param(
182             xisbn => $xisbn,
183             XISBNS => get_xisbns($xisbn)
184         );
185     };
186     if ($@) { warn "XISBN Failed $@"; }
187 }
188 if ( C4::Context->preference("AmazonContent") == 1 ) {
189     my $similar_products_exist;
190     my $amazon_details = &get_amazon_details( $xisbn );
191     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
192     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
193     my @similar_products;
194     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
195         # do we have any of these isbns in our collection?
196         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
197         # verify that there is at least one similar item
198         $similar_products_exist++ if ${@$similar_biblionumbers}[0];
199         push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
200     }
201     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
202     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
203     $template->param( AmazonSimilarItems => $similar_products_exist );
204     $template->param( amazon_average_rating => $average_rating * 20);
205     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
206     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
207     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
208 }
209 output_html_with_http_headers $query, $cookie, $template->output;