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