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