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