IMPORTANT: BUGFIXING STATUSES
[koha_gimpoz] / 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::Branch;
29 use C4::Reserves;
30 use C4::Members;
31 use C4::Serials;
32 use C4::XISBN qw(get_xisbns get_biblio_from_xisbn);
33 use C4::Amazon;
34
35 my $query = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "catalogue/detail.tmpl",
39         query           => $query,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { catalogue => 1 },
43     }
44 );
45
46 my $biblionumber = $query->param('biblionumber');
47 my $fw = GetFrameworkCode($biblionumber);
48
49 # Get Branches, Itemtypes and Locations
50 my $branches = GetBranches();
51 my $itemtypes = GetItemTypes();
52
53 my %locations;
54 # FIXME: move this to a pm, check waiting status for holds
55 my $dbh = C4::Context->dbh;
56 my $lsch = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category = 'SHELF_LOC'");
57 $lsch->execute();
58 while (my $ldata = $lsch->fetchrow_hashref ) {
59     $locations{ $ldata->{'authorised_value'} } = $ldata->{'lib'};
60 }
61
62 # change back when ive fixed request.pl
63 my @items = &GetItemsInfo( $biblionumber, 'intra' );
64 my $dat = &GetBiblioData($biblionumber);
65
66 if (!$dat) { 
67     print $query->redirect("/cgi-bin/koha/koha-tmpl/errors/404.pl");
68 }
69
70 #coping with subscriptions
71 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
72 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
73
74 my @subs;
75 foreach my $subscription (@subscriptions) {
76     my %cell;
77     $cell{subscriptionid}    = $subscription->{subscriptionid};
78     $cell{subscriptionnotes} = $subscription->{notes};
79
80     #get the three latest serials.
81     $cell{latestserials} =
82       GetLatestSerials( $subscription->{subscriptionid}, 3 );
83     push @subs, \%cell;
84 }
85 $dat->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $dat->{itemtype} }{imageurl};
86 $dat->{'count'} = @items;
87 my @itemloop;
88 my $norequests = 1;
89 foreach my $item (@items) {
90
91     # can place holds defaults to yes
92     $norequests = 0 unless ( ( $item->{'notforloan'} > 0 ) || ( $item->{'itemnotforloan'} > 0 ) );
93
94     # format some item fields for display
95     $item->{ $item->{'publictype'} } = 1;
96     $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
97     $item->{datedue} = format_date($item->{datedue});
98     $item->{datelastseen} = format_date($item->{datelastseen});
99     $item->{onloan} = format_date($item->{onloan});
100     $item->{locationname} = $locations{$item->{location}};
101     # item damaged, lost, withdrawn loops
102     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
103     if ($item->{damaged}) {
104         $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
105     }
106
107     # checking for holds
108     my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($item->{itemnumber});
109     my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
110
111     if ( defined $reservedate ) {
112         $item->{backgroundcolor} = 'reserved';
113         $item->{reservedate}     = format_date($reservedate);
114         $item->{ReservedForBorrowernumber}     = $reservedfor;
115         $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
116         $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
117         $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
118     }
119
120     # FIXME: move this to a pm, check waiting status for holds
121     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
122     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
123     while (my $wait_hashref = $sth2->fetchrow_hashref) {
124         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
125     }
126
127     push @itemloop, $item;
128 }
129
130 $template->param( norequests => $norequests );
131
132 ## get notes and subjects from MARC record
133     my $dbh              = C4::Context->dbh;
134     my $marcflavour      = C4::Context->preference("marcflavour");
135     my $record           = GetMarcBiblio($biblionumber);
136     my $marcnotesarray   = GetMarcNotes( $record, $marcflavour );
137     my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
138     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
139
140     $template->param(
141         MARCNOTES   => $marcnotesarray,
142         MARCSUBJCTS => $marcsubjctsarray,
143         MARCAUTHORS => $marcauthorsarray
144     );
145
146 my @results = ( $dat, );
147 foreach ( keys %{$dat} ) {
148     $template->param( "$_" => $dat->{$_} . "" );
149 }
150
151 $template->param(
152     itemloop        => \@itemloop,
153     biblionumber        => $biblionumber,
154     detailview => 1,
155     subscriptions       => \@subs,
156     subscriptionsnumber => $subscriptionsnumber,
157     subscriptiontitle   => $dat->{title},
158 );
159
160 # XISBN Stuff
161 my $xisbn=$dat->{'isbn'};
162 $xisbn =~ s/(p|-| |:)//g;
163 $template->param(amazonisbn => $xisbn);
164 if (C4::Context->preference("FRBRizeEditions")==1) {
165     eval {
166         $template->param(
167             xisbn => $xisbn,
168             XISBNS => get_xisbns($xisbn)
169         );
170     };
171     if ($@) { warn "XISBN Failed $@"; }
172 }
173 if ( C4::Context->preference("AmazonContent") == 1 ) {
174     my $amazon_details = &get_amazon_details( $xisbn );
175     foreach my $result ( @{ $amazon_details->{Details} } ) {
176         $template->param( item_description => $result->{ProductDescription} );
177         $template->param( image            => $result->{ImageUrlMedium} );
178         $template->param( list_price       => $result->{ListPrice} );
179         $template->param( amazon_url       => $result->{url} );
180     }
181
182     my @products;
183     my @reviews;
184     for my $details ( @{ $amazon_details->{Details} } ) {
185
186         next unless $details->{SimilarProducts};
187         for my $product ( @{ $details->{SimilarProducts}->{Product} } ) {
188             if (C4::Context->preference("AmazonSimilarItems") ) {
189                 my @xisbns;
190                 if (C4::Context->preference("XISBNAmazonSimilarItems") ) {
191                     @xisbns = @{get_xisbns($product)};
192                 }
193                 else {
194                     push @xisbns, get_biblio_from_xisbn($product);
195                 }
196                 push @products, +{ product => \@xisbns };
197             }
198         }
199         next unless $details->{Reviews};
200         for my $product ( @{ $details->{Reviews}->{AvgCustomerRating} } ) {
201             $template->param( rating => $product * 20 );
202         }
203         for my $reviews ( @{ $details->{Reviews}->{CustomerReview} } ) {
204             push @reviews,
205               +{
206                 summary => $reviews->{Summary},
207                 comment => $reviews->{Comment},
208               };
209         }
210     }
211     $template->param( SIMILAR_PRODUCTS => \@products );
212     $template->param( AMAZONREVIEWS    => \@reviews );
213 }
214
215 output_html_with_http_headers $query, $cookie, $template->output;