Bug 21978: Use patron-title for holdfor handling in results
[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 Modern::Perl;
23 use C4::Koha qw( GetAuthorisedValues );
24 use CGI qw ( -utf8 );
25 use HTML::Entities;
26 use C4::Biblio qw( GetBiblioData GetFrameworkCode GetMarcBiblio );
27 use C4::Items qw( GetHostItemsInfo GetItemsInfo );
28 use C4::Acquisition qw( GetOrderFromItemnumber GetBasket GetInvoice );
29 use C4::Output qw( output_and_exit output_html_with_http_headers );
30 use C4::Auth qw( get_template_and_user );
31 use C4::Serials qw( CountSubscriptionFromBiblionumber );
32 use C4::Search qw( enabled_staff_search_views z3950_search_args );
33
34 use Koha::Acquisition::Booksellers;
35 use Koha::AuthorisedValues;
36 use Koha::Biblios;
37 use Koha::Items;
38 use Koha::Patrons;
39
40 my $query=CGI->new;
41
42 my ($template, $loggedinuser, $cookie) = get_template_and_user(
43     {
44         template_name   => 'catalogue/moredetail.tt',
45         query           => $query,
46         type            => "intranet",
47         flagsrequired   => { catalogue => 1 },
48     }
49 );
50
51 $template->param(
52     updated_exclude_from_local_holds_priority => scalar($query->param('updated_exclude_from_local_holds_priority'))
53 );
54
55 if($query->cookie("holdfor")){ 
56     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
57     $template->param(
58         holdfor            => $query->cookie("holdfor"),
59         holdfor_patron     => $holdfor_patron,
60         holdfor_cardnumber => $holdfor_patron->cardnumber,
61     );
62 }
63
64 if( $query->cookie("searchToOrder") ){
65     my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
66     $template->param(
67         searchtoorder_basketno => $basketno,
68         searchtoorder_vendorid => $vendorid
69     );
70 }
71
72 # get variables
73 my $biblionumber;
74 my $itemnumber;
75 if( $query->param('itemnumber') && !$query->param('biblionumber') ){
76     $itemnumber = $query->param('itemnumber');
77     my $item = Koha::Items->find( $itemnumber );
78     $biblionumber = $item->biblionumber;
79 } else {
80     $biblionumber = $query->param('biblionumber');
81 }
82
83 $biblionumber = HTML::Entities::encode($biblionumber);
84 my $title=$query->param('title');
85 my $bi=$query->param('bi');
86 $bi = $biblionumber unless $bi;
87 $itemnumber = $query->param('itemnumber');
88 my $data = &GetBiblioData($biblionumber);
89 my $dewey = $data->{'dewey'};
90 my $showallitems = $query->param('showallitems');
91
92 #coping with subscriptions
93 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
94
95 # FIXME Dewey is a string, not a number, & we should use a function
96 # $dewey =~ s/0+$//;
97 # if ($dewey eq "000.") { $dewey = "";};
98 # if ($dewey < 10){$dewey='00'.$dewey;}
99 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
100 # if ($dewey <= 0){
101 #      $dewey='';
102 # }
103 # $dewey=~ s/\.$//;
104 # $data->{'dewey'}=$dewey;
105
106 my $fw = GetFrameworkCode($biblionumber);
107 my @all_items= GetItemsInfo($biblionumber);
108 my @items;
109 my $patron = Koha::Patrons->find( $loggedinuser );
110 for my $itm (@all_items) {
111     push @items, $itm unless ( $itm->{itemlost} && 
112                                $patron->category->hidelostitems &&
113                                !$showallitems && 
114                                ($itemnumber != $itm->{itemnumber}));
115 }
116
117 my $record=GetMarcBiblio({ biblionumber => $biblionumber });
118
119 output_and_exit( $query, $cookie, $template, 'unknown_biblio')
120     unless $record;
121
122 my $hostrecords;
123 # adding items linked via host biblios
124 my @hostitems = GetHostItemsInfo($record);
125 if (@hostitems){
126         $hostrecords =1;
127         push (@items,@hostitems);
128 }
129
130 my $biblio = Koha::Biblios->find( $biblionumber );
131
132 my $totalcount=@all_items;
133 my $showncount=@items;
134 my $hiddencount = $totalcount - $showncount;
135 $data->{'count'}=$totalcount;
136 $data->{'showncount'}=$showncount;
137 $data->{'hiddencount'}=$hiddencount;  # can be zero
138
139 my $ccodes =
140   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
141 my $copynumbers =
142   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
143
144 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
145
146 $data->{'itemtypename'} = $itemtypes->{ $data->{'itemtype'} }->{'translated_description'}
147   if $data->{itemtype} && exists $itemtypes->{ $data->{itemtype} };
148 foreach ( keys %{$data} ) {
149     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
150 }
151
152 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
153 foreach my $item (@items){
154     $item->{object} = Koha::Items->find( $item->{itemnumber} );
155     $item->{'collection'}              = $ccodes->{ $item->{ccode} } if $ccodes && $item->{ccode} && exists $ccodes->{ $item->{ccode} };
156     $item->{'itype'}                   = $itemtypes->{ $item->{'itype'} }->{'translated_description'} if exists $itemtypes->{ $item->{'itype'} };
157     $item->{'replacementprice'}        = $item->{'replacementprice'};
158     if ( defined $item->{'copynumber'} ) {
159         $item->{'displaycopy'} = 1;
160         if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
161             $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
162         }
163         else {
164             $item->{'copyvol'} = $item->{'copynumber'};
165         }
166     }
167
168     # item has a host number if its biblio number does not match the current bib
169     if ($item->{biblionumber} ne $biblionumber){
170         $item->{hostbiblionumber} = $item->{biblionumber};
171         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
172     }
173
174     my $order  = GetOrderFromItemnumber( $item->{'itemnumber'} );
175     $item->{'ordernumber'}             = $order->{'ordernumber'};
176     $item->{'basketno'}                = $order->{'basketno'};
177     $item->{'orderdate'}               = $order->{'entrydate'};
178     if ($item->{'basketno'}){
179             my $basket = GetBasket($item->{'basketno'});
180         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
181         $item->{'vendor'} = $bookseller->name;
182     }
183     $item->{'invoiceid'}               = $order->{'invoiceid'};
184     if($item->{invoiceid}) {
185         my $invoice = GetInvoice($item->{invoiceid});
186         $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
187     }
188     $item->{'datereceived'}            = $order->{'datereceived'};
189
190     if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
191         $item->{status_advisory} = 1;
192     }
193
194     # Add paidfor info
195     if ( $item->{itemlost} ) {
196         my $accountlines = Koha::Account::Lines->search(
197             {
198                 itemnumber        => $item->{itemnumber},
199                 debit_type_code   => 'LOST',
200                 status            => [ undef, { '<>' => 'RETURNED' } ],
201                 amountoutstanding => 0
202             },
203             {
204                 order_by => { '-desc' => 'date' },
205                 rows     => 1
206             }
207         );
208
209         if ( my $accountline = $accountlines->next ) {
210             my $payment_offsets = $accountline->debit_offsets(
211                 {
212                     credit_id => { '!=' => undef }, # it is not the debit itself
213                     'credit.credit_type_code' =>
214                       { '!=' => [ 'Writeoff', 'Forgiven' ] },
215                 },
216                 { join => 'credit', order_by => { '-desc' => 'created_on' } }
217             );
218
219             if ($payment_offsets->count) {
220                 my $patron = $accountline->patron;
221                 my $payment_offset = $payment_offsets->next;
222                 $item->{paidfor} = { patron => $patron, created_on => $payment_offset->created_on };
223             }
224         }
225     }
226
227     if (C4::Context->preference("IndependentBranches")) {
228         #verifying rights
229         my $userenv = C4::Context->userenv();
230         unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) {
231                 $item->{'nomod'}=1;
232         }
233     }
234     if ($item->{'datedue'}) {
235         $item->{'issue'}= 1;
236     } else {
237         $item->{'issue'}= 0;
238     }
239
240     if ( $item->{'borrowernumber'} ) {
241         my $curr_borrower = Koha::Patrons->find( $item->{borrowernumber} );
242         $item->{patron} = $curr_borrower;
243     }
244 }
245
246 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
247 if ( $mss->count ) {
248     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
249 }
250 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
251 if ( $mss->count ) {
252     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
253 }
254 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
255 if ( $mss->count ) {
256     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
257 }
258
259 $template->param(count => $data->{'count'},
260         subscriptionsnumber => $subscriptionsnumber,
261     subscriptiontitle   => $data->{title},
262         C4::Search::enabled_staff_search_views,
263 );
264
265 # get biblionumbers stored in the cart
266 my @cart_list;
267
268 if($query->cookie("intranet_bib_list")){
269     my $cart_list = $query->cookie("intranet_bib_list");
270     @cart_list = split(/\//, $cart_list);
271     if ( grep {$_ eq $biblionumber} @cart_list) {
272         $template->param( incart => 1 );
273     }
274 }
275
276 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
277     {
278         borrowernumber => $loggedinuser,
279         add_allowed    => 1,
280         public         => 0,
281     }
282 );
283 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
284     {
285         borrowernumber => $loggedinuser,
286         add_allowed    => 1,
287         public         => 1,
288     }
289 );
290
291
292 $template->param(
293     add_to_some_private_shelves => $some_private_shelves,
294     add_to_some_public_shelves  => $some_public_shelves,
295 );
296
297 $template->param(
298     ITEM_DATA           => \@items,
299     moredetailview      => 1,
300     loggedinuser        => $loggedinuser,
301     biblionumber        => $biblionumber,
302     biblioitemnumber    => $bi,
303     itemnumber          => $itemnumber,
304     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
305     biblio              => $biblio,
306 );
307 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
308 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
309
310 my $holds = $biblio->holds;
311 $template->param( holdcount => $holds->count );
312
313 output_html_with_http_headers $query, $cookie, $template->output;
314