bug 2606: reduce size of offline circ patron database
[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
21 use CGI;
22 use C4::Auth;
23 use C4::Dates 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
57 unless (defined($record)) {
58     print $query->redirect("/cgi-bin/koha/errors/404.pl");
59         exit;
60 }
61
62 my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
63 my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
64 my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
65 my $marcseriesarray  = GetMarcSeries($record,$marcflavour);
66 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
67 my $subtitle         = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
68
69 # Get Branches, Itemtypes and Locations
70 my $branches = GetBranches();
71 my $itemtypes = GetItemTypes();
72
73 # FIXME: move this to a pm, check waiting status for holds
74 my $dbh = C4::Context->dbh;
75
76 # change back when ive fixed request.pl
77 my @items = &GetItemsInfo( $biblionumber, 'intra' );
78 my $dat = &GetBiblioData($biblionumber);
79
80 #coping with subscriptions
81 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
82 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
83 my @subs;
84 $dat->{'serial'}=1 if $subscriptionsnumber;
85 foreach my $subscription (@subscriptions) {
86     my %cell;
87     $cell{subscriptionid}    = $subscription->{subscriptionid};
88     $cell{subscriptionnotes} = $subscription->{notes};
89         $cell{branchcode}        = $subscription->{branchcode};
90         $cell{hasalert}          = $subscription->{hasalert};
91     #get the three latest serials.
92     $cell{latestserials} =
93       GetLatestSerials( $subscription->{subscriptionid}, 3 );
94     push @subs, \%cell;
95 }
96 $dat->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $dat->{itemtype} }{imageurl} );
97 $dat->{'count'} = scalar @items;
98 my $shelflocations = GetKohaAuthorisedValues('items.location', $fw);
99 my $collections    = GetKohaAuthorisedValues('items.ccode'   , $fw);
100 my (@itemloop, %itemfields);
101 my $norequests = 1;
102 foreach my $item (@items) {
103
104     # can place holds defaults to yes
105     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
106
107     # format some item fields for display
108     $item->{ $item->{'publictype'} } = 1;
109     $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
110         foreach (qw(datedue datelastseen onloan)) {
111                 $item->{$_} = format_date($item->{$_});
112         }
113     # item damaged, lost, withdrawn loops
114     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
115     if ($item->{damaged}) {
116         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
117     }
118     #get shelf location and collection code description if they are authorised value.
119         my $shelfcode= $item->{'location'};
120         $item->{'location'} = $shelflocations->{$shelfcode} if(defined($shelflocations) && exists($shelflocations->{$shelfcode})); 
121         my $ccode= $item->{'ccode'};
122         $item->{'ccode'} = $collections->{$ccode} if(defined($collections) && exists($collections->{$ccode})); 
123         foreach (qw(ccode enumchron copynumber)) {
124                 $itemfields{$_} = 1 if($item->{$_});
125         }
126
127     # checking for holds
128     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
129     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
130
131     if ( defined $reservedate ) {
132         $item->{backgroundcolor} = 'reserved';
133         $item->{reservedate}     = format_date($reservedate);
134         $item->{ReservedForBorrowernumber}     = $reservedfor;
135         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
136         $item->{ReservedForFirstname}   = $ItemBorrowerReserveInfo->{'firstname'};
137         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
138     }
139
140         # Check the transit status
141     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
142     if ( $transfertwhen ne '' ) {
143         $item->{transfertwhen} = format_date($transfertwhen);
144         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
145         $item->{transfertto}   = $branches->{$transfertto}{branchname};
146         $item->{nocancel} = 1;
147     }
148
149     # FIXME: move this to a pm, check waiting status for holds
150     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
151     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
152     while (my $wait_hashref = $sth2->fetchrow_hashref) {
153         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
154     }
155
156     push @itemloop, $item;
157 }
158
159 $template->param( norequests => $norequests );
160 $template->param(
161         MARCNOTES   => $marcnotesarray,
162         MARCSUBJCTS => $marcsubjctsarray,
163         MARCAUTHORS => $marcauthorsarray,
164         MARCSERIES  => $marcseriesarray,
165         MARCURLS => $marcurlsarray,
166         subtitle    => $subtitle,
167         itemdata_ccode      => $itemfields{ccode},
168         itemdata_enumchron  => $itemfields{enumchron},
169         itemdata_copynumber => $itemfields{copynumber},
170         volinfo                         => $itemfields{enumchron} || $dat->{'serial'} ,
171 );
172
173 my @results = ( $dat, );
174 foreach ( keys %{$dat} ) {
175     $template->param( "$_" => $dat->{$_} . "" );
176 }
177
178 $template->param(
179     itemloop        => \@itemloop,
180     biblionumber        => $biblionumber,
181     detailview => 1,
182     subscriptions       => \@subs,
183     subscriptionsnumber => $subscriptionsnumber,
184     subscriptiontitle   => $dat->{title},
185 );
186
187 # $debug and $template->param(debug_display => 1);
188
189 # XISBN Stuff
190 my $xisbn=$dat->{'isbn'};
191 $xisbn =~ /(\d*[X]*)/;
192 $template->param(amazonisbn => $1);             # FIXME: so it is OK if the ISBN = 'XXXXX' ?
193 if (C4::Context->preference("FRBRizeEditions")==1) {
194     eval {
195         $template->param(
196             xisbn => $xisbn,
197             XISBNS => get_xisbns($xisbn)
198         );
199     };
200     if ($@) { warn "XISBN Failed $@"; }
201 }
202 if ( C4::Context->preference("AmazonContent") == 1 ) {
203     my $similar_products_exist;
204     my $amazon_details = &get_amazon_details( $xisbn );
205     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
206     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
207     my @similar_products;
208     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
209         # do we have any of these isbns in our collection?
210         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
211         # verify that there is at least one similar item
212                 if (scalar(@$similar_biblionumbers)){            
213                         $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
214             push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
215         }
216     }
217     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
218     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
219     $template->param( AmazonSimilarItems => $similar_products_exist );
220     $template->param( amazon_average_rating => $average_rating * 20);
221     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
222     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
223     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
224 }
225 output_html_with_http_headers $query, $cookie, $template->output;