Bug 31315: Remove GetHostItemsInfo and GetItemsInfo from moredetail
[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->{itemtype} = $itemtypes->{$item->effective_itemtype};
155     $item_info->{'ccode'}              = $ccodes->{ $item->ccode } if $ccodes && $item->ccode && exists $ccodes->{ $item->ccode };
156     if ( defined $item->copynumber ) {
157         $item_info->{'displaycopy'} = 1;
158         if ( defined $copynumbers->{ $item_info->{'copynumber'} } ) {
159             $item_info->{'copyvol'} = $copynumbers->{ $item_info->{'copynumber'} }
160         }
161         else {
162             $item_info->{'copyvol'} = $item_info->{'copynumber'};
163         }
164     }
165
166     # item has a host number if its biblio number does not match the current bib
167     if ($item->biblionumber ne $biblionumber){
168         $item_info->{hostbiblionumber} = $item->{biblionumber};
169         $item_info->{hosttitle} = $item->biblio->title;
170     }
171
172     # FIXME The acquisition code below could be improved using methods from Koha:: objects
173     my $order  = GetOrderFromItemnumber( $item->{'itemnumber'} );
174     $item_info->{'basketno'}                = $order->{'basketno'};
175     $item_info->{'orderdate'}               = $order->{'entrydate'};
176     if ($item_info->{'basketno'}){
177         my $basket = GetBasket($item_info->{'basketno'});
178         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
179         $item_info->{'vendor'} = $bookseller->name;
180     }
181     $item_info->{'invoiceid'}               = $order->{'invoiceid'};
182     if($item_info->{invoiceid}) {
183         my $invoice = GetInvoice($item_info->{invoiceid});
184         $item_info->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
185     }
186     $item_info->{'datereceived'}            = $order->{'datereceived'};
187
188     if (   $item->notforloan
189         || $item->itemtype->notforloan
190         || $item->itemlost
191         || $item->damaged
192         || $item->withdrawn )
193     {
194         $item_info->{status_advisory} = 1;
195     }
196
197     # Add paidfor info
198     if ( $item->itemlost ) {
199         my $accountlines = Koha::Account::Lines->search(
200             {
201                 itemnumber        => $item->itemnumber,
202                 debit_type_code   => 'LOST',
203                 status            => [ undef, { '<>' => 'RETURNED' } ],
204                 amountoutstanding => 0
205             },
206             {
207                 order_by => { '-desc' => 'date' },
208                 rows     => 1
209             }
210         );
211
212         if ( my $accountline = $accountlines->next ) {
213             my $payment_offsets = $accountline->debit_offsets(
214                 {
215                     credit_id => { '!=' => undef }, # it is not the debit itself
216                     'credit.credit_type_code' =>
217                       { '!=' => [ 'Writeoff', 'Forgiven' ] },
218                 },
219                 { join => 'credit', order_by => { '-desc' => 'created_on' } }
220             );
221
222             if ($payment_offsets->count) {
223                 my $patron = $accountline->patron;
224                 my $payment_offset = $payment_offsets->next;
225                 $item_info->{paidfor} = { patron => $patron, created_on => $payment_offset->created_on };
226             }
227         }
228     }
229
230     if (C4::Context->preference("IndependentBranches")) {
231         #verifying rights
232         my $userenv = C4::Context->userenv();
233         unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->homebranch)) {
234                 $item_info->{'nomod'}=1;
235         }
236     }
237     push @item_data, $item_info;
238 }
239
240 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
241 if ( $mss->count ) {
242     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
243 }
244 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
245 if ( $mss->count ) {
246     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
247 }
248 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
249 if ( $mss->count ) {
250     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
251 }
252
253 $template->param(count => $data->{'count'},
254         subscriptionsnumber => $subscriptionsnumber,
255     subscriptiontitle   => $data->{title},
256         C4::Search::enabled_staff_search_views,
257 );
258
259 # get biblionumbers stored in the cart
260 my @cart_list;
261
262 if($query->cookie("intranet_bib_list")){
263     my $cart_list = $query->cookie("intranet_bib_list");
264     @cart_list = split(/\//, $cart_list);
265     if ( grep {$_ eq $biblionumber} @cart_list) {
266         $template->param( incart => 1 );
267     }
268 }
269
270 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
271     {
272         borrowernumber => $loggedinuser,
273         add_allowed    => 1,
274         public         => 0,
275     }
276 );
277 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
278     {
279         borrowernumber => $loggedinuser,
280         add_allowed    => 1,
281         public         => 1,
282     }
283 );
284
285
286 $template->param(
287     add_to_some_private_shelves => $some_private_shelves,
288     add_to_some_public_shelves  => $some_public_shelves,
289 );
290
291 $template->param(
292     ITEM_DATA           => \@item_data,
293     moredetailview      => 1,
294     loggedinuser        => $loggedinuser,
295     biblionumber        => $biblionumber,
296     biblioitemnumber    => $bi,
297     itemnumber          => $itemnumber,
298     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
299     biblio              => $biblio,
300 );
301 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
302 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
303
304 my $holds = $biblio->holds;
305 $template->param( holdcount => $holds->count );
306
307 output_html_with_http_headers $query, $cookie, $template->output;
308