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