created old_issues and old_reserves tables
[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_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 my %locations;
66 # FIXME: move this to a pm, check waiting status for holds
67 my $dbh = C4::Context->dbh;
68 my $lsch = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category = 'LOC'");
69 $lsch->execute();
70 while (my $ldata = $lsch->fetchrow_hashref ) {
71     $locations{ $ldata->{'authorised_value'} } = $ldata->{'lib'};
72 }
73
74 # change back when ive fixed request.pl
75 my @items = &GetItemsInfo( $biblionumber, 'intra' );
76 my $dat = &GetBiblioData($biblionumber);
77
78 if (!$dat) { 
79     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
80         exit;
81 }
82
83 #coping with subscriptions
84 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
85 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
86
87
88
89 my @subs;
90 foreach my $subscription (@subscriptions) {
91     my %cell;
92     $cell{subscriptionid}    = $subscription->{subscriptionid};
93     $cell{subscriptionnotes} = $subscription->{notes};
94
95     #get the three latest serials.
96     $cell{latestserials} =
97       GetLatestSerials( $subscription->{subscriptionid}, 3 );
98     push @subs, \%cell;
99 }
100 $dat->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $dat->{itemtype} }{imageurl};
101 $dat->{'count'} = @items;
102 my @itemloop;
103 my $norequests = 1;
104 foreach my $item (@items) {
105
106     # can place holds defaults to yes
107     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
108
109     # format some item fields for display
110     $item->{ $item->{'publictype'} } = 1;
111     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
112     $item->{datedue} = format_date($item->{datedue});
113     $item->{datelastseen} = format_date($item->{datelastseen});
114     $item->{onloan} = format_date($item->{onloan});
115     $item->{locationname} = $locations{$item->{location}};
116     # item damaged, lost, withdrawn loops
117     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
118     if ($item->{damaged}) {
119         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
120     }
121     #get collection code description, too
122     $item->{'ccode'}  = GetAuthorisedValueDesc('','',   $item->{'ccode'} ,'','','ccode');
123
124     # checking for holds
125     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
126     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
127
128     if ( defined $reservedate ) {
129         $item->{backgroundcolor} = 'reserved';
130         $item->{reservedate}     = format_date($reservedate);
131         $item->{ReservedForBorrowernumber}     = $reservedfor;
132         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
133         $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
134         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
135     }
136
137         # Check the transit status
138     my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($item->{itemnumber});
139     if ( $transfertwhen ne '' ) {
140         $item->{transfertwhen} = format_date($transfertwhen);
141         $item->{transfertfrom} = $branches->{$transfertfrom}{branchname};
142         $item->{transfertto} = $branches->{$transfertto}{branchname};
143         $item->{nocancel} = 1;
144     }
145
146     # FIXME: move this to a pm, check waiting status for holds
147     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
148     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
149     while (my $wait_hashref = $sth2->fetchrow_hashref) {
150         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
151     }
152
153     push @itemloop, $item;
154 }
155
156 $template->param( norequests => $norequests );
157
158     $template->param(
159         MARCNOTES   => $marcnotesarray,
160         MARCSUBJCTS => $marcsubjctsarray,
161         MARCAUTHORS => $marcauthorsarray,
162         MARCSERIES  => $marcseriesarray
163     );
164
165 my @results = ( $dat, );
166 foreach ( keys %{$dat} ) {
167     $template->param( "$_" => $dat->{$_} . "" );
168 }
169
170 $template->param(
171     itemloop        => \@itemloop,
172     biblionumber        => $biblionumber,
173     detailview => 1,
174     subscriptions       => \@subs,
175     subscriptionsnumber => $subscriptionsnumber,
176     subscriptiontitle   => $dat->{title},
177 );
178
179 # XISBN Stuff
180 my $xisbn=$dat->{'isbn'};
181 $xisbn =~ s/(p|-| |:)//g;
182 $template->param(amazonisbn => $xisbn);
183 if (C4::Context->preference("FRBRizeEditions")==1) {
184     eval {
185         $template->param(
186             xisbn => $xisbn,
187             XISBNS => get_xisbns($xisbn)
188         );
189     };
190     if ($@) { warn "XISBN Failed $@"; }
191 }
192 if ( C4::Context->preference("AmazonContent") == 1 ) {
193     my $amazon_details = &get_amazon_details( $xisbn );
194     foreach my $result ( @{ $amazon_details->{Details} } ) {
195         $template->param( item_description => $result->{ProductDescription} );
196         $template->param( image            => $result->{ImageUrlMedium} );
197         $template->param( list_price       => $result->{ListPrice} );
198         $template->param( amazon_url       => $result->{url} );
199     }
200
201     my @products;
202     my @reviews;
203     for my $details ( @{ $amazon_details->{Details} } ) {
204
205         next unless $details->{SimilarProducts};
206         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
207             if (C4::Context->preference("AmazonSimilarItems") ) {
208                 my @xisbns;
209                 if (C4::Context->preference("XISBNAmazonSimilarItems") ) {
210                     @xisbns = @{get_xisbns($product)};
211                 }
212                 else {
213                     push @xisbns, get_biblio_from_xisbn($product);
214                 }
215                 push @products, +{ product => \@xisbns };
216             }
217         }
218         next unless $details->{Reviews};
219         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
220             $template->param( rating => $product * 20 );
221         }
222         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
223             push @reviews,
224               +{
225                 summary => $reviews->{Summary},
226                 comment => $reviews->{Comment},
227               };
228         }
229     }
230     $template->param( SIMILAR_PRODUCTS => \@products );
231     $template->param( AMAZONREVIEWS    => \@reviews );
232 }
233
234 output_html_with_http_headers $query, $cookie, $template->output;