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