Bug 25758: Return on_reserve over too_soon when not calling from automatic_renewals...
[koha.git] / catalogue / moredetail.pl
blob9bf2f58ae208c83de7fe72c1a723dd890dc0ef36
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 flagsrequired => { catalogue => 1 },
52 $template->param(
53 updated_exclude_from_local_holds_priority => scalar($query->param('updated_exclude_from_local_holds_priority'))
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,
66 if( $query->cookie("searchToOrder") ){
67 my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
68 $template->param(
69 searchtoorder_basketno => $basketno,
70 searchtoorder_vendorid => $vendorid
74 # get variables
76 my $biblionumber=$query->param('biblionumber');
77 $biblionumber = HTML::Entities::encode($biblionumber);
78 my $title=$query->param('title');
79 my $bi=$query->param('bi');
80 $bi = $biblionumber unless $bi;
81 my $itemnumber = $query->param('itemnumber');
82 my $data = &GetBiblioData($biblionumber);
83 my $dewey = $data->{'dewey'};
84 my $showallitems = $query->param('showallitems');
86 #coping with subscriptions
87 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
89 # FIXME Dewey is a string, not a number, & we should use a function
90 # $dewey =~ s/0+$//;
91 # if ($dewey eq "000.") { $dewey = "";};
92 # if ($dewey < 10){$dewey='00'.$dewey;}
93 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
94 # if ($dewey <= 0){
95 # $dewey='';
96 # }
97 # $dewey=~ s/\.$//;
98 # $data->{'dewey'}=$dewey;
100 my $fw = GetFrameworkCode($biblionumber);
101 my @all_items= GetItemsInfo($biblionumber);
102 my @items;
103 my $patron = Koha::Patrons->find( $loggedinuser );
104 for my $itm (@all_items) {
105 push @items, $itm unless ( $itm->{itemlost} &&
106 $patron->category->hidelostitems &&
107 !$showallitems &&
108 ($itemnumber != $itm->{itemnumber}));
111 my $record=GetMarcBiblio({ biblionumber => $biblionumber });
113 output_and_exit( $query, $cookie, $template, 'unknown_biblio')
114 unless $record;
116 my $hostrecords;
117 # adding items linked via host biblios
118 my @hostitems = GetHostItemsInfo($record);
119 if (@hostitems){
120 $hostrecords =1;
121 push (@items,@hostitems);
124 my $biblio = Koha::Biblios->find( $biblionumber );
126 my $totalcount=@all_items;
127 my $showncount=@items;
128 my $hiddencount = $totalcount - $showncount;
129 $data->{'count'}=$totalcount;
130 $data->{'showncount'}=$showncount;
131 $data->{'hiddencount'}=$hiddencount; # can be zero
133 my $ccodes =
134 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
135 my $copynumbers =
136 { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
138 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
140 $data->{'itemtypename'} = $itemtypes->{ $data->{'itemtype'} }->{'translated_description'}
141 if $data->{itemtype} && exists $itemtypes->{ $data->{itemtype} };
142 foreach ( keys %{$data} ) {
143 $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
146 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
147 foreach my $item (@items){
148 $item->{object} = Koha::Items->find( $item->{itemnumber} );
149 $item->{'collection'} = $ccodes->{ $item->{ccode} } if $ccodes && $item->{ccode} && exists $ccodes->{ $item->{ccode} };
150 $item->{'itype'} = $itemtypes->{ $item->{'itype'} }->{'translated_description'} if exists $itemtypes->{ $item->{'itype'} };
151 $item->{'replacementprice'} = $item->{'replacementprice'};
152 if ( defined $item->{'copynumber'} ) {
153 $item->{'displaycopy'} = 1;
154 if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
155 $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
157 else {
158 $item->{'copyvol'} = $item->{'copynumber'};
162 # item has a host number if its biblio number does not match the current bib
163 if ($item->{biblionumber} ne $biblionumber){
164 $item->{hostbiblionumber} = $item->{biblionumber};
165 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
168 my $order = GetOrderFromItemnumber( $item->{'itemnumber'} );
169 $item->{'ordernumber'} = $order->{'ordernumber'};
170 $item->{'basketno'} = $order->{'basketno'};
171 $item->{'orderdate'} = $order->{'entrydate'};
172 if ($item->{'basketno'}){
173 my $basket = GetBasket($item->{'basketno'});
174 my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
175 $item->{'vendor'} = $bookseller->name;
177 $item->{'invoiceid'} = $order->{'invoiceid'};
178 if($item->{invoiceid}) {
179 my $invoice = GetInvoice($item->{invoiceid});
180 $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
182 $item->{'datereceived'} = $order->{'datereceived'};
184 if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
185 $item->{status_advisory} = 1;
188 # Add paidfor info
189 if ( $item->{itemlost} ) {
190 my $accountlines = Koha::Account::Lines->search(
192 itemnumber => $item->{itemnumber},
193 debit_type_code => 'LOST',
194 status => [ undef, { '<>' => 'RETURNED' } ],
195 amountoutstanding => 0
198 order_by => { '-desc' => 'date' },
199 rows => 1
203 if ( my $accountline = $accountlines->next ) {
204 my $payment_offsets = Koha::Account::Offsets->search(
206 debit_id => $accountline->id,
207 credit_id => { '!=' => undef }, # it is not the debit itself
208 type => { '!=' => [ 'Writeoff', 'Forgiven' ] },
209 amount => { '<' => 0 } # credits are negative on the DB
211 { order_by => { '-desc' => 'created_on' } }
214 if ($payment_offsets->count) {
215 my $patron = $accountline->patron;
216 my $payment_offset = $payment_offsets->next;
217 $item->{paidfor} = { patron => $patron, created_on => $payment_offset->created_on };
222 if (C4::Context->preference("IndependentBranches")) {
223 #verifying rights
224 my $userenv = C4::Context->userenv();
225 unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) {
226 $item->{'nomod'}=1;
229 if ($item->{'datedue'}) {
230 $item->{'issue'}= 1;
231 } else {
232 $item->{'issue'}= 0;
235 if ( $item->{'borrowernumber'} ) {
236 my $curr_borrower = Koha::Patrons->find( $item->{borrowernumber} );
237 $item->{patron} = $curr_borrower;
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 ) );
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 ) );
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) );
254 $template->param(count => $data->{'count'},
255 subscriptionsnumber => $subscriptionsnumber,
256 subscriptiontitle => $data->{title},
257 C4::Search::enabled_staff_search_views,
260 $template->param(
261 ITEM_DATA => \@items,
262 moredetailview => 1,
263 loggedinuser => $loggedinuser,
264 biblionumber => $biblionumber,
265 biblioitemnumber => $bi,
266 itemnumber => $itemnumber,
267 z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
268 biblio => $biblio,
270 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
271 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
273 my $holds = $biblio->holds;
274 $template->param( holdcount => $holds->count );
276 output_html_with_http_headers $query, $cookie, $template->output;