Bug 25873: Ignore malformed data for Elasticsearch integer fields
[koha.git] / catalogue / moredetail.pl
bloba736c30c8c01cdd6f4d9f1e824759ba95b0987d9
1 #!/usr/bin/perl
3 # Copyright 2000-2003 Katipo Communications
4 # parts copyright 2010 BibLibre
6 # This file is part of Koha.
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.
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.
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>.
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
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;
41 my $query=new CGI;
43 my ($template, $loggedinuser, $cookie) = get_template_and_user(
45 template_name => 'catalogue/moredetail.tt',
46 query => $query,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { catalogue => 1 },
53 if($query->cookie("holdfor")){
54 my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
55 $template->param(
56 holdfor => $query->cookie("holdfor"),
57 holdfor_surname => $holdfor_patron->surname,
58 holdfor_firstname => $holdfor_patron->firstname,
59 holdfor_cardnumber => $holdfor_patron->cardnumber,
63 if( $query->cookie("searchToOrder") ){
64 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
65 $template->param(
66 searchtoorder_basketno => $basketno,
67 searchtoorder_vendorid => $vendorid
71 # get variables
73 my $biblionumber=$query->param('biblionumber');
74 $biblionumber = HTML::Entities::encode($biblionumber);
75 my $title=$query->param('title');
76 my $bi=$query->param('bi');
77 $bi = $biblionumber unless $bi;
78 my $itemnumber = $query->param('itemnumber');
79 my $data = &GetBiblioData($biblionumber);
80 my $dewey = $data->{'dewey'};
81 my $showallitems = $query->param('showallitems');
83 #coping with subscriptions
84 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
86 # FIXME Dewey is a string, not a number, & we should use a function
87 # $dewey =~ s/0+$//;
88 # if ($dewey eq "000.") { $dewey = "";};
89 # if ($dewey < 10){$dewey='00'.$dewey;}
90 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
91 # if ($dewey <= 0){
92 # $dewey='';
93 # }
94 # $dewey=~ s/\.$//;
95 # $data->{'dewey'}=$dewey;
97 my $fw = GetFrameworkCode($biblionumber);
98 my @all_items= GetItemsInfo($biblionumber);
99 my @items;
100 my $patron = Koha::Patrons->find( $loggedinuser );
101 for my $itm (@all_items) {
102 push @items, $itm unless ( $itm->{itemlost} &&
103 $patron->category->hidelostitems &&
104 !$showallitems &&
105 ($itemnumber != $itm->{itemnumber}));
108 my $record=GetMarcBiblio({ biblionumber => $biblionumber });
110 output_and_exit( $query, $cookie, $template, 'unknown_biblio')
111 unless $record;
113 my $hostrecords;
114 # adding items linked via host biblios
115 my @hostitems = GetHostItemsInfo($record);
116 if (@hostitems){
117 $hostrecords =1;
118 push (@items,@hostitems);
121 my $biblio = Koha::Biblios->find( $biblionumber );
123 my $totalcount=@all_items;
124 my $showncount=@items;
125 my $hiddencount = $totalcount - $showncount;
126 $data->{'count'}=$totalcount;
127 $data->{'showncount'}=$showncount;
128 $data->{'hiddencount'}=$hiddencount; # can be zero
130 my $ccodes =
131 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
132 my $copynumbers =
133 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
135 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
137 $data->{'itemtypename'} = $itemtypes->{ $data->{'itemtype'} }->{'translated_description'}
138 if $data->{itemtype} && exists $itemtypes->{ $data->{itemtype} };
139 foreach ( keys %{$data} ) {
140 $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
143 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
144 foreach my $item (@items){
145 $item->{object} = Koha::Items->find( $item->{itemnumber} );
146 $item->{'collection'} = $ccodes->{ $item->{ccode} } if $ccodes && $item->{ccode} && exists $ccodes->{ $item->{ccode} };
147 $item->{'itype'} = $itemtypes->{ $item->{'itype'} }->{'translated_description'} if exists $itemtypes->{ $item->{'itype'} };
148 $item->{'replacementprice'} = $item->{'replacementprice'};
149 if ( defined $item->{'copynumber'} ) {
150 $item->{'displaycopy'} = 1;
151 if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
152 $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
154 else {
155 $item->{'copyvol'} = $item->{'copynumber'};
159 # item has a host number if its biblio number does not match the current bib
160 if ($item->{biblionumber} ne $biblionumber){
161 $item->{hostbiblionumber} = $item->{biblionumber};
162 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
165 my $order = GetOrderFromItemnumber( $item->{'itemnumber'} );
166 $item->{'ordernumber'} = $order->{'ordernumber'};
167 $item->{'basketno'} = $order->{'basketno'};
168 $item->{'orderdate'} = $order->{'entrydate'};
169 if ($item->{'basketno'}){
170 my $basket = GetBasket($item->{'basketno'});
171 my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
172 $item->{'vendor'} = $bookseller->name;
174 $item->{'invoiceid'} = $order->{'invoiceid'};
175 if($item->{invoiceid}) {
176 my $invoice = GetInvoice($item->{invoiceid});
177 $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
179 $item->{'datereceived'} = $order->{'datereceived'};
181 if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
182 $item->{status_advisory} = 1;
185 # Add paidfor info
186 if ( $item->{itemlost} ) {
187 my $accountlines = Koha::Account::Lines->search(
189 itemnumber => $item->{itemnumber},
190 debit_type_code => 'LOST',
191 status => [ undef, { '<>' => 'RETURNED' } ],
192 amountoutstanding => 0
195 order_by => { '-desc' => 'date' },
196 rows => 1
200 if ( my $accountline = $accountlines->next ) {
201 my $payment_offsets = Koha::Account::Offsets->search(
203 debit_id => $accountline->id,
204 credit_id => { '!=' => undef }, # it is not the debit itself
205 type => { '!=' => [ 'Writeoff', 'Forgiven' ] },
206 amount => { '<' => 0 } # credits are negative on the DB
208 { order_by => { '-desc' => 'created_on' } }
211 if ($payment_offsets->count) {
212 my $patron = $accountline->patron;
213 my $payment_offset = $payment_offsets->next;
214 $item->{paidfor} =
215 $patron->firstname . " "
216 . $patron->surname . " "
217 . $patron->cardnumber . " "
218 . $payment_offset->created_on;
223 if (C4::Context->preference("IndependentBranches")) {
224 #verifying rights
225 my $userenv = C4::Context->userenv();
226 unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) {
227 $item->{'nomod'}=1;
230 if ($item->{'datedue'}) {
231 $item->{'issue'}= 1;
232 } else {
233 $item->{'issue'}= 0;
236 if ( $item->{'borrowernumber'} ) {
237 my $curr_borrower = Koha::Patrons->find( $item->{borrowernumber} );
238 $item->{patron} = $curr_borrower;
242 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
243 if ( $mss->count ) {
244 $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
246 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
247 if ( $mss->count ) {
248 $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
250 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
251 if ( $mss->count ) {
252 $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
255 $template->param(count => $data->{'count'},
256 subscriptionsnumber => $subscriptionsnumber,
257 subscriptiontitle => $data->{title},
258 C4::Search::enabled_staff_search_views,
261 $template->param(
262 ITEM_DATA => \@items,
263 moredetailview => 1,
264 loggedinuser => $loggedinuser,
265 biblionumber => $biblionumber,
266 biblioitemnumber => $bi,
267 itemnumber => $itemnumber,
268 z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
269 biblio => $biblio->unblessed,
271 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
272 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
274 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
275 my @deletedorders_using_biblio;
276 my @orders_using_biblio;
277 my @baskets_orders;
278 my @baskets_deletedorders;
280 foreach my $myorder (@allorders_using_biblio) {
281 my $basket = $myorder->{'basketno'};
282 if ((defined $myorder->{'datecancellationprinted'}) and ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
283 push @deletedorders_using_biblio, $myorder;
284 unless (grep{ $_ eq $basket } @baskets_deletedorders){
285 push @baskets_deletedorders,$myorder->{'basketno'};
288 else {
289 push @orders_using_biblio, $myorder;
290 unless (grep { $_ eq $basket } @baskets_orders){
291 push @baskets_orders,$myorder->{'basketno'};
296 my $count_orders_using_biblio = scalar @orders_using_biblio ;
297 $template->param (countorders => $count_orders_using_biblio);
299 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
300 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
302 my $holds = $biblio->holds;
303 $template->param( holdcount => $holds->count );
305 output_html_with_http_headers $query, $cookie, $template->output;