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 use Koha::Items;
41
42 my $query=new CGI;
43
44 my ($template, $loggedinuser, $cookie) = get_template_and_user(
45     {
46         template_name   => 'catalogue/moredetail.tt',
47         query           => $query,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { catalogue => 1 },
51     }
52 );
53
54 if($query->cookie("holdfor")){ 
55     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
56     $template->param(
57         holdfor => $query->cookie("holdfor"),
58         holdfor_surname => $holdfor_patron->{'surname'},
59         holdfor_firstname => $holdfor_patron->{'firstname'},
60         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
61     );
62 }
63
64 my $hidepatronname = C4::Context->preference("HidePatronName");
65
66 # get variables
67
68 my $biblionumber=$query->param('biblionumber');
69 my $title=$query->param('title');
70 my $bi=$query->param('bi');
71 $bi = $biblionumber unless $bi;
72 my $itemnumber = $query->param('itemnumber');
73 my $data = &GetBiblioData($biblionumber);
74 my $dewey = $data->{'dewey'};
75 my $showallitems = $query->param('showallitems');
76
77 #coping with subscriptions
78 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
79
80 # FIXME Dewey is a string, not a number, & we should use a function
81 # $dewey =~ s/0+$//;
82 # if ($dewey eq "000.") { $dewey = "";};
83 # if ($dewey < 10){$dewey='00'.$dewey;}
84 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
85 # if ($dewey <= 0){
86 #      $dewey='';
87 # }
88 # $dewey=~ s/\.$//;
89 # $data->{'dewey'}=$dewey;
90
91 my $fw = GetFrameworkCode($biblionumber);
92 my @all_items= GetItemsInfo($biblionumber);
93 my @items;
94 for my $itm (@all_items) {
95     push @items, $itm unless ( $itm->{itemlost} && 
96                                GetHideLostItemsPreference($loggedinuser) &&
97                                !$showallitems && 
98                                ($itemnumber != $itm->{itemnumber}));
99 }
100
101 my $record=GetMarcBiblio($biblionumber);
102
103 my $hostrecords;
104 # adding items linked via host biblios
105 my @hostitems = GetHostItemsInfo($record);
106 if (@hostitems){
107         $hostrecords =1;
108         push (@items,@hostitems);
109 }
110
111 my $subtitle = GetRecordValue('subtitle', $record, $fw);
112
113 my $totalcount=@all_items;
114 my $showncount=@items;
115 my $hiddencount = $totalcount - $showncount;
116 $data->{'count'}=$totalcount;
117 $data->{'showncount'}=$showncount;
118 $data->{'hiddencount'}=$hiddencount;  # can be zero
119
120 my $ccodes= GetKohaAuthorisedValues('items.ccode',$fw);
121 my $copynumbers = GetKohaAuthorisedValues('items.copynumber',$fw);
122 my $itemtypes = GetItemTypes;
123
124 $data->{'itemtypename'} = $itemtypes->{$data->{'itemtype'}}->{'translated_description'};
125 $data->{'rentalcharge'} = sprintf( "%.2f", $data->{'rentalcharge'} );
126 foreach ( keys %{$data} ) {
127     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
128 }
129
130 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
131 foreach my $item (@items){
132     $item->{object} = Koha::Items->find( $item->{itemnumber} );
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
197 if ( my $lost_av = GetAuthValCode('items.itemlost', $fw) ) {
198     $template->param( itemlostloop => GetAuthorisedValues( $lost_av ) );
199 }
200 if ( my $damaged_av = GetAuthValCode('items.damaged', $fw) ) {
201     $template->param( itemdamagedloop => GetAuthorisedValues( $damaged_av ) );
202 }
203 if ( my $withdrawn_av = GetAuthValCode('items.withdrawn', $fw) ) {
204     $template->param( itemwithdrawnloop => GetAuthorisedValues( $withdrawn_av ) );
205 }
206
207 $template->param(count => $data->{'count'},
208         subscriptionsnumber => $subscriptionsnumber,
209     subscriptiontitle   => $data->{title},
210         C4::Search::enabled_staff_search_views,
211 );
212
213 $template->param(
214     ITEM_DATA           => \@items,
215     moredetailview      => 1,
216     loggedinuser        => $loggedinuser,
217     biblionumber        => $biblionumber,
218     biblioitemnumber    => $bi,
219     itemnumber          => $itemnumber,
220     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
221     subtitle            => $subtitle,
222     hidepatronname      => $hidepatronname,
223 );
224 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
225 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
226
227 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
228 my @deletedorders_using_biblio;
229 my @orders_using_biblio;
230 my @baskets_orders;
231 my @baskets_deletedorders;
232
233 foreach my $myorder (@allorders_using_biblio) {
234     my $basket = $myorder->{'basketno'};
235     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
236         push @deletedorders_using_biblio, $myorder;
237         unless (grep(/^$basket$/, @baskets_deletedorders)){
238             push @baskets_deletedorders,$myorder->{'basketno'};
239         }
240     }
241     else {
242         push @orders_using_biblio, $myorder;
243         unless (grep(/^$basket$/, @baskets_orders)){
244             push @baskets_orders,$myorder->{'basketno'};
245             }
246     }
247 }
248
249 my $count_orders_using_biblio = scalar @orders_using_biblio ;
250 $template->param (countorders => $count_orders_using_biblio);
251
252 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
253 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
254
255 my $holds = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
256 my $holdcount = scalar( @$holds );
257 $template->param( holdcount => scalar ( @$holds ) );
258
259 output_html_with_http_headers $query, $cookie, $template->output;
260