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