Bug 17835: Replace GetItemTypes with Koha::ItemTypes
[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 HTML::Entities;
27 use C4::Biblio;
28 use C4::Items;
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
36 use Koha::Acquisition::Booksellers;
37 use Koha::AuthorisedValues;
38 use Koha::Biblios;
39 use Koha::DateUtils;
40 use Koha::Items;
41 use Koha::Patrons;
42
43 my $query=new CGI;
44
45 my ($template, $loggedinuser, $cookie) = get_template_and_user(
46     {
47         template_name   => 'catalogue/moredetail.tt',
48         query           => $query,
49         type            => "intranet",
50         authnotrequired => 0,
51         flagsrequired   => { catalogue => 1 },
52     }
53 );
54
55 if($query->cookie("holdfor")){ 
56     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
57     $template->param(
58         holdfor => $query->cookie("holdfor"),
59         holdfor_surname => $holdfor_patron->{'surname'},
60         holdfor_firstname => $holdfor_patron->{'firstname'},
61         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
62     );
63 }
64
65 my $hidepatronname = C4::Context->preference("HidePatronName");
66
67 # get variables
68
69 my $biblionumber=$query->param('biblionumber');
70 $biblionumber = HTML::Entities::encode($biblionumber);
71 my $title=$query->param('title');
72 my $bi=$query->param('bi');
73 $bi = $biblionumber unless $bi;
74 my $itemnumber = $query->param('itemnumber');
75 my $data = &GetBiblioData($biblionumber);
76 my $dewey = $data->{'dewey'};
77 my $showallitems = $query->param('showallitems');
78
79 #coping with subscriptions
80 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
81
82 # FIXME Dewey is a string, not a number, & we should use a function
83 # $dewey =~ s/0+$//;
84 # if ($dewey eq "000.") { $dewey = "";};
85 # if ($dewey < 10){$dewey='00'.$dewey;}
86 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
87 # if ($dewey <= 0){
88 #      $dewey='';
89 # }
90 # $dewey=~ s/\.$//;
91 # $data->{'dewey'}=$dewey;
92
93 my $fw = GetFrameworkCode($biblionumber);
94 my @all_items= GetItemsInfo($biblionumber);
95 my @items;
96 my $patron = Koha::Patrons->find( $loggedinuser );
97 for my $itm (@all_items) {
98     push @items, $itm unless ( $itm->{itemlost} && 
99                                $patron->category->hidelostitems &&
100                                !$showallitems && 
101                                ($itemnumber != $itm->{itemnumber}));
102 }
103
104 my $record=GetMarcBiblio($biblionumber);
105
106 my $hostrecords;
107 # adding items linked via host biblios
108 my @hostitems = GetHostItemsInfo($record);
109 if (@hostitems){
110         $hostrecords =1;
111         push (@items,@hostitems);
112 }
113
114 my $subtitle = GetRecordValue('subtitle', $record, $fw);
115
116 my $totalcount=@all_items;
117 my $showncount=@items;
118 my $hiddencount = $totalcount - $showncount;
119 $data->{'count'}=$totalcount;
120 $data->{'showncount'}=$showncount;
121 $data->{'hiddencount'}=$hiddencount;  # can be zero
122
123 my $ccodes =
124   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
125 my $copynumbers =
126   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
127
128 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
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->{object} = Koha::Items->find( $item->{itemnumber} );
139     $item->{'collection'}              = $ccodes->{ $item->{ccode} } if ($ccodes);
140     $item->{'itype'}                   = $itemtypes->{ $item->{'itype'} }->{'translated_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 = Koha::Acquisition::Booksellers->find( $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     if ($item->{'datedue'}) {
186         $item->{'issue'}= 1;
187     } else {
188         $item->{'issue'}= 0;
189     }
190
191     unless ($hidepatronname) {
192         if ( $item->{'borrowernumber'} ) {
193             my $curr_borrower = GetMember('borrowernumber' => $item->{'borrowernumber'} );
194             $item->{borrowerfirstname} = $curr_borrower->{'firstname'};
195             $item->{borrowersurname} = $curr_borrower->{'surname'};
196         }
197     }
198
199 }
200
201 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => { not => undef } });
202 if ( $mss->count ) {
203     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
204 }
205 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => { not => undef } });
206 if ( $mss->count ) {
207     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
208 }
209 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => { not => undef } });
210 if ( $mss->count ) {
211     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
212 }
213
214 $template->param(count => $data->{'count'},
215         subscriptionsnumber => $subscriptionsnumber,
216     subscriptiontitle   => $data->{title},
217         C4::Search::enabled_staff_search_views,
218 );
219
220 $template->param(
221     ITEM_DATA           => \@items,
222     moredetailview      => 1,
223     loggedinuser        => $loggedinuser,
224     biblionumber        => $biblionumber,
225     biblioitemnumber    => $bi,
226     itemnumber          => $itemnumber,
227     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
228     subtitle            => $subtitle,
229     hidepatronname      => $hidepatronname,
230 );
231 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
232 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
233
234 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
235 my @deletedorders_using_biblio;
236 my @orders_using_biblio;
237 my @baskets_orders;
238 my @baskets_deletedorders;
239
240 foreach my $myorder (@allorders_using_biblio) {
241     my $basket = $myorder->{'basketno'};
242     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
243         push @deletedorders_using_biblio, $myorder;
244         unless (grep(/^$basket$/, @baskets_deletedorders)){
245             push @baskets_deletedorders,$myorder->{'basketno'};
246         }
247     }
248     else {
249         push @orders_using_biblio, $myorder;
250         unless (grep(/^$basket$/, @baskets_orders)){
251             push @baskets_orders,$myorder->{'basketno'};
252             }
253     }
254 }
255
256 my $count_orders_using_biblio = scalar @orders_using_biblio ;
257 $template->param (countorders => $count_orders_using_biblio);
258
259 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
260 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
261
262 my $biblio = Koha::Biblios->find( $biblionumber );
263 my $holds = $biblio->holds;
264 $template->param( holdcount => $holds->count );
265
266 output_html_with_http_headers $query, $cookie, $template->output;
267