Bug 11349: Change .tmpl -> .tt in scripts using templates
[koha-equinox.git] / catalogue / moredetail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2003 Katipo Communications
4 # parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use C4::Koha;
25 use CGI;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Branch;
29 use C4::Acquisition;
30 use C4::Bookseller qw(GetBookSellerFromId);
31 use C4::Output;
32 use C4::Auth;
33 use C4::Serials;
34 use C4::Circulation;  # to use itemissues
35 use C4::Members; # to use GetMember
36 use C4::Search;         # enabled_staff_search_views
37 use C4::Members qw/GetHideLostItemsPreference/;
38 use C4::Reserves qw(GetReservesFromBiblionumber);
39 use Koha::DateUtils;
40
41 my $query=new CGI;
42
43 # FIXME  subject is not exported to the template?
44 my $subject=$query->param('subject');
45
46 # if its a subject we need to use the subject.tt
47 my ($template, $loggedinuser, $cookie) = get_template_and_user(
48     {
49         template_name   => ( $subject
50                                 ? 'catalogue/subject.tt'
51                                 : 'catalogue/moredetail.tt'),
52         query           => $query,
53         type            => "intranet",
54         authnotrequired => 0,
55         flagsrequired   => { catalogue => 1 },
56     }
57 );
58
59 if($query->cookie("holdfor")){ 
60     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
61     $template->param(
62         holdfor => $query->cookie("holdfor"),
63         holdfor_surname => $holdfor_patron->{'surname'},
64         holdfor_firstname => $holdfor_patron->{'firstname'},
65         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
66     );
67 }
68
69 my $hidepatronname = C4::Context->preference("HidePatronName");
70
71 # get variables
72
73 my $biblionumber=$query->param('biblionumber');
74 my $title=$query->param('title');
75 my $bi=$query->param('bi');
76 $bi = $biblionumber unless $bi;
77 my $itemnumber = $query->param('itemnumber');
78 my $data = &GetBiblioData($biblionumber);
79 my $dewey = $data->{'dewey'};
80 my $showallitems = $query->param('showallitems');
81
82 #coping with subscriptions
83 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
84
85 # FIXME Dewey is a string, not a number, & we should use a function
86 # $dewey =~ s/0+$//;
87 # if ($dewey eq "000.") { $dewey = "";};
88 # if ($dewey < 10){$dewey='00'.$dewey;}
89 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
90 # if ($dewey <= 0){
91 #      $dewey='';
92 # }
93 # $dewey=~ s/\.$//;
94 # $data->{'dewey'}=$dewey;
95
96 my $fw = GetFrameworkCode($biblionumber);
97 my @all_items= GetItemsInfo($biblionumber);
98 my @items;
99 for my $itm (@all_items) {
100     push @items, $itm unless ( $itm->{itemlost} && 
101                                GetHideLostItemsPreference($loggedinuser) &&
102                                !$showallitems && 
103                                ($itemnumber != $itm->{itemnumber}));
104 }
105
106 my $record=GetMarcBiblio($biblionumber);
107
108 my $hostrecords;
109 # adding items linked via host biblios
110 my @hostitems = GetHostItemsInfo($record);
111 if (@hostitems){
112         $hostrecords =1;
113         push (@items,@hostitems);
114 }
115
116 my $subtitle = GetRecordValue('subtitle', $record, $fw);
117
118 my $totalcount=@all_items;
119 my $showncount=@items;
120 my $hiddencount = $totalcount - $showncount;
121 $data->{'count'}=$totalcount;
122 $data->{'showncount'}=$showncount;
123 $data->{'hiddencount'}=$hiddencount;  # can be zero
124
125 my $ccodes= GetKohaAuthorisedValues('items.ccode',$fw);
126 my $copynumbers = GetKohaAuthorisedValues('items.copynumber',$fw);
127 my $itemtypes = GetItemTypes;
128
129 $data->{'itemtypename'} = $itemtypes->{$data->{'itemtype'}}->{'description'};
130 $data->{'rentalcharge'} = sprintf( "%.2f", $data->{'rentalcharge'} );
131 foreach ( keys %{$data} ) {
132     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
133 }
134
135 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
136 foreach my $item (@items){
137     $item->{itemlostloop}= GetAuthorisedValues(GetAuthValCode('items.itemlost',$fw),$item->{itemlost}) if GetAuthValCode('items.itemlost',$fw);
138     $item->{itemdamagedloop}= GetAuthorisedValues(GetAuthValCode('items.damaged',$fw),$item->{damaged}) if GetAuthValCode('items.damaged',$fw);
139     $item->{'collection'}              = $ccodes->{ $item->{ccode} } if ($ccodes);
140     $item->{'itype'}                   = $itemtypes->{ $item->{'itype'} }->{'description'};
141     $item->{'replacementprice'}        = sprintf( "%.2f", $item->{'replacementprice'} );
142     if ( defined $item->{'copynumber'} ) {
143         $item->{'displaycopy'} = 1;
144         if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
145             $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
146         }
147         else {
148             $item->{'copyvol'} = $item->{'copynumber'};
149         }
150     }
151
152     # item has a host number if its biblio number does not match the current bib
153     if ($item->{biblionumber} ne $biblionumber){
154         $item->{hostbiblionumber} = $item->{biblionumber};
155         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
156     }
157
158     my $order  = GetOrderFromItemnumber( $item->{'itemnumber'} );
159     $item->{'ordernumber'}             = $order->{'ordernumber'};
160     $item->{'basketno'}                = $order->{'basketno'};
161     $item->{'orderdate'}               = $order->{'entrydate'};
162     if ($item->{'basketno'}){
163             my $basket = GetBasket($item->{'basketno'});
164             my $bookseller = GetBookSellerFromId($basket->{'booksellerid'});
165             $item->{'vendor'} = $bookseller->{'name'};
166     }
167     $item->{'invoiceid'}               = $order->{'invoiceid'};
168     if($item->{invoiceid}) {
169         my $invoice = GetInvoice($item->{invoiceid});
170         $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
171     }
172     $item->{'datereceived'}            = $order->{'datereceived'};
173
174     if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
175         $item->{status_advisory} = 1;
176     }
177
178     if (C4::Context->preference("IndependentBranches")) {
179         #verifying rights
180         my $userenv = C4::Context->userenv();
181         unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) {
182                 $item->{'nomod'}=1;
183         }
184     }
185     $item->{'homebranchname'} = GetBranchName($item->{'homebranch'});
186     $item->{'holdingbranchname'} = GetBranchName($item->{'holdingbranch'});
187     if ($item->{'datedue'}) {
188         $item->{'issue'}= 1;
189     } else {
190         $item->{'issue'}= 0;
191     }
192
193     unless ($hidepatronname) {
194         if ( $item->{'borrowernumber'} ) {
195             my $curr_borrower = GetMember('borrowernumber' => $item->{'borrowernumber'} );
196             $item->{borrowerfirstname} = $curr_borrower->{'firstname'};
197             $item->{borrowersurname} = $curr_borrower->{'surname'};
198         }
199     }
200
201 }
202 $template->param(count => $data->{'count'},
203         subscriptionsnumber => $subscriptionsnumber,
204     subscriptiontitle   => $data->{title},
205         C4::Search::enabled_staff_search_views,
206 );
207
208 $template->param(
209     ITEM_DATA           => \@items,
210     moredetailview      => 1,
211     loggedinuser        => $loggedinuser,
212     biblionumber        => $biblionumber,
213     biblioitemnumber    => $bi,
214     itemnumber          => $itemnumber,
215     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
216     subtitle            => $subtitle,
217     hidepatronname      => $hidepatronname,
218 );
219 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
220 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
221
222 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
223 my @deletedorders_using_biblio;
224 my @orders_using_biblio;
225 my @baskets_orders;
226 my @baskets_deletedorders;
227
228 foreach my $myorder (@allorders_using_biblio) {
229     my $basket = $myorder->{'basketno'};
230     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
231         push @deletedorders_using_biblio, $myorder;
232         unless (grep(/^$basket$/, @baskets_deletedorders)){
233             push @baskets_deletedorders,$myorder->{'basketno'};
234         }
235     }
236     else {
237         push @orders_using_biblio, $myorder;
238         unless (grep(/^$basket$/, @baskets_orders)){
239             push @baskets_orders,$myorder->{'basketno'};
240             }
241     }
242 }
243
244 my $count_orders_using_biblio = scalar @orders_using_biblio ;
245 $template->param (countorders => $count_orders_using_biblio);
246
247 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
248 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
249
250 my $holds = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
251 my $holdcount = scalar( @$holds );
252 $template->param( holdcount => scalar ( @$holds ) );
253
254 output_html_with_http_headers $query, $cookie, $template->output;
255