Bug 16534: Block AddIssue from issuing if the return is not possible
[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 use Koha::Items;
42
43 my $query=new CGI;
44
45 # FIXME  subject is not exported to the template?
46 my $subject=$query->param('subject');
47
48 # if its a subject we need to use the subject.tt
49 my ($template, $loggedinuser, $cookie) = get_template_and_user(
50     {
51         template_name   => ( $subject
52                                 ? 'catalogue/subject.tt'
53                                 : 'catalogue/moredetail.tt'),
54         query           => $query,
55         type            => "intranet",
56         authnotrequired => 0,
57         flagsrequired   => { catalogue => 1 },
58     }
59 );
60
61 if($query->cookie("holdfor")){ 
62     my $holdfor_patron = GetMember('borrowernumber' => $query->cookie("holdfor"));
63     $template->param(
64         holdfor => $query->cookie("holdfor"),
65         holdfor_surname => $holdfor_patron->{'surname'},
66         holdfor_firstname => $holdfor_patron->{'firstname'},
67         holdfor_cardnumber => $holdfor_patron->{'cardnumber'},
68     );
69 }
70
71 my $hidepatronname = C4::Context->preference("HidePatronName");
72
73 # get variables
74
75 my $biblionumber=$query->param('biblionumber');
76 my $title=$query->param('title');
77 my $bi=$query->param('bi');
78 $bi = $biblionumber unless $bi;
79 my $itemnumber = $query->param('itemnumber');
80 my $data = &GetBiblioData($biblionumber);
81 my $dewey = $data->{'dewey'};
82 my $showallitems = $query->param('showallitems');
83
84 #coping with subscriptions
85 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
86
87 # FIXME Dewey is a string, not a number, & we should use a function
88 # $dewey =~ s/0+$//;
89 # if ($dewey eq "000.") { $dewey = "";};
90 # if ($dewey < 10){$dewey='00'.$dewey;}
91 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
92 # if ($dewey <= 0){
93 #      $dewey='';
94 # }
95 # $dewey=~ s/\.$//;
96 # $data->{'dewey'}=$dewey;
97
98 my $fw = GetFrameworkCode($biblionumber);
99 my @all_items= GetItemsInfo($biblionumber);
100 my @items;
101 for my $itm (@all_items) {
102     push @items, $itm unless ( $itm->{itemlost} && 
103                                GetHideLostItemsPreference($loggedinuser) &&
104                                !$showallitems && 
105                                ($itemnumber != $itm->{itemnumber}));
106 }
107
108 my $record=GetMarcBiblio($biblionumber);
109
110 my $hostrecords;
111 # adding items linked via host biblios
112 my @hostitems = GetHostItemsInfo($record);
113 if (@hostitems){
114         $hostrecords =1;
115         push (@items,@hostitems);
116 }
117
118 my $subtitle = GetRecordValue('subtitle', $record, $fw);
119
120 my $totalcount=@all_items;
121 my $showncount=@items;
122 my $hiddencount = $totalcount - $showncount;
123 $data->{'count'}=$totalcount;
124 $data->{'showncount'}=$showncount;
125 $data->{'hiddencount'}=$hiddencount;  # can be zero
126
127 my $ccodes= GetKohaAuthorisedValues('items.ccode',$fw);
128 my $copynumbers = GetKohaAuthorisedValues('items.copynumber',$fw);
129 my $itemtypes = GetItemTypes;
130
131 $data->{'itemtypename'} = $itemtypes->{$data->{'itemtype'}}->{'translated_description'};
132 $data->{'rentalcharge'} = sprintf( "%.2f", $data->{'rentalcharge'} );
133 foreach ( keys %{$data} ) {
134     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
135 }
136
137 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
138 foreach my $item (@items){
139     $item->{object} = Koha::Items->find( $item->{itemnumber} );
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
204 if ( my $lost_av = GetAuthValCode('items.itemlost', $fw) ) {
205     $template->param( itemlostloop => GetAuthorisedValues( $lost_av ) );
206 }
207 if ( my $damaged_av = GetAuthValCode('items.damaged', $fw) ) {
208     $template->param( itemdamagedloop => GetAuthorisedValues( $damaged_av ) );
209 }
210 if ( my $withdrawn_av = GetAuthValCode('items.withdrawn', $fw) ) {
211     $template->param( itemwithdrawnloop => GetAuthorisedValues( $withdrawn_av ) );
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 $holds = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
263 my $holdcount = scalar( @$holds );
264 $template->param( holdcount => scalar ( @$holds ) );
265
266 output_html_with_http_headers $query, $cookie, $template->output;
267