Bug 28013: (QA follow-up) Remove unused variable
[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=new CGI;
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 if($query->cookie("holdfor")){ 
53     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
54     $template->param(
55         holdfor => $query->cookie("holdfor"),
56         holdfor_surname => $holdfor_patron->surname,
57         holdfor_firstname => $holdfor_patron->firstname,
58         holdfor_cardnumber => $holdfor_patron->cardnumber,
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
72 my $biblionumber=$query->param('biblionumber');
73 $biblionumber = HTML::Entities::encode($biblionumber);
74 my $title=$query->param('title');
75 my $bi=$query->param('bi');
76 $bi = $biblionumber unless $bi;
77 my $itemnumber = $query->param('itemnumber');
78 my $data = &GetBiblioData($biblionumber);
79 my $dewey = $data->{'dewey'};
80 my $showallitems = $query->param('showallitems');
81
82 #coping with subscriptions
83 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
84
85 # FIXME Dewey is a string, not a number, & we should use a function
86 # $dewey =~ s/0+$//;
87 # if ($dewey eq "000.") { $dewey = "";};
88 # if ($dewey < 10){$dewey='00'.$dewey;}
89 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
90 # if ($dewey <= 0){
91 #      $dewey='';
92 # }
93 # $dewey=~ s/\.$//;
94 # $data->{'dewey'}=$dewey;
95
96 my $fw = GetFrameworkCode($biblionumber);
97 my @all_items= GetItemsInfo($biblionumber);
98 my @items;
99 my $patron = Koha::Patrons->find( $loggedinuser );
100 for my $itm (@all_items) {
101     push @items, $itm unless ( $itm->{itemlost} && 
102                                $patron->category->hidelostitems &&
103                                !$showallitems && 
104                                ($itemnumber != $itm->{itemnumber}));
105 }
106
107 my $record=GetMarcBiblio({ biblionumber => $biblionumber });
108
109 output_and_exit( $query, $cookie, $template, 'unknown_biblio')
110     unless $record;
111
112 my $hostrecords;
113 # adding items linked via host biblios
114 my @hostitems = GetHostItemsInfo($record);
115 if (@hostitems){
116         $hostrecords =1;
117         push (@items,@hostitems);
118 }
119
120 my $biblio = Koha::Biblios->find( $biblionumber );
121
122 my $totalcount=@all_items;
123 my $showncount=@items;
124 my $hiddencount = $totalcount - $showncount;
125 $data->{'count'}=$totalcount;
126 $data->{'showncount'}=$showncount;
127 $data->{'hiddencount'}=$hiddencount;  # can be zero
128
129 my $ccodes =
130   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
131 my $copynumbers =
132   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
133
134 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
135
136 $data->{'itemtypename'} = $itemtypes->{ $data->{'itemtype'} }->{'translated_description'}
137   if $data->{itemtype} && exists $itemtypes->{ $data->{itemtype} };
138 foreach ( keys %{$data} ) {
139     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
140 }
141
142 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
143 foreach my $item (@items){
144     $item->{object} = Koha::Items->find( $item->{itemnumber} );
145     $item->{'collection'}              = $ccodes->{ $item->{ccode} } if $ccodes && $item->{ccode} && exists $ccodes->{ $item->{ccode} };
146     $item->{'itype'}                   = $itemtypes->{ $item->{'itype'} }->{'translated_description'} if exists $itemtypes->{ $item->{'itype'} };
147     $item->{'replacementprice'}        = $item->{'replacementprice'};
148     if ( defined $item->{'copynumber'} ) {
149         $item->{'displaycopy'} = 1;
150         if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
151             $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
152         }
153         else {
154             $item->{'copyvol'} = $item->{'copynumber'};
155         }
156     }
157
158     # item has a host number if its biblio number does not match the current bib
159     if ($item->{biblionumber} ne $biblionumber){
160         $item->{hostbiblionumber} = $item->{biblionumber};
161         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
162     }
163
164     my $order  = GetOrderFromItemnumber( $item->{'itemnumber'} );
165     $item->{'ordernumber'}             = $order->{'ordernumber'};
166     $item->{'basketno'}                = $order->{'basketno'};
167     $item->{'orderdate'}               = $order->{'entrydate'};
168     if ($item->{'basketno'}){
169             my $basket = GetBasket($item->{'basketno'});
170         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
171         $item->{'vendor'} = $bookseller->name;
172     }
173     $item->{'invoiceid'}               = $order->{'invoiceid'};
174     if($item->{invoiceid}) {
175         my $invoice = GetInvoice($item->{invoiceid});
176         $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
177     }
178     $item->{'datereceived'}            = $order->{'datereceived'};
179
180     if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
181         $item->{status_advisory} = 1;
182     }
183
184     # Add paidfor info
185     if ( $item->{itemlost} ) {
186         my $accountlines = Koha::Account::Lines->search(
187             {
188                 itemnumber        => $item->{itemnumber},
189                 debit_type_code   => 'LOST',
190                 status            => [ undef, { '<>' => 'RETURNED' } ],
191                 amountoutstanding => 0
192             },
193             {
194                 order_by => { '-desc' => 'date' },
195                 rows     => 1
196             }
197         );
198
199         if ( my $accountline = $accountlines->next ) {
200             my $payment_offsets = Koha::Account::Offsets->search(
201                 {
202                     debit_id  => $accountline->id,
203                     credit_id => { '!=' => undef }, # it is not the debit itself
204                     type => { '!=' => [ 'Writeoff', 'Forgiven' ] },
205                     amount => { '<' => 0 }    # credits are negative on the DB
206                 },
207                 { order_by => { '-desc' => 'created_on' } }
208             );
209
210             if ($payment_offsets->count) {
211                 my $patron = $accountline->patron;
212                 my $payment_offset = $payment_offsets->next;
213                 $item->{paidfor} = { patron => $patron, created_on => $payment_offset->created_on };
214             }
215         }
216     }
217
218     if (C4::Context->preference("IndependentBranches")) {
219         #verifying rights
220         my $userenv = C4::Context->userenv();
221         unless (C4::Context->IsSuperLibrarian() or ($userenv->{'branch'} eq $item->{'homebranch'})) {
222                 $item->{'nomod'}=1;
223         }
224     }
225     if ($item->{'datedue'}) {
226         $item->{'issue'}= 1;
227     } else {
228         $item->{'issue'}= 0;
229     }
230
231     if ( $item->{'borrowernumber'} ) {
232         my $curr_borrower = Koha::Patrons->find( $item->{borrowernumber} );
233         $item->{patron} = $curr_borrower;
234     }
235 }
236
237 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
238 if ( $mss->count ) {
239     $template->param( itemlostloop => GetAuthorisedValues( $mss->next->authorised_value ) );
240 }
241 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.damaged', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
242 if ( $mss->count ) {
243     $template->param( itemdamagedloop => GetAuthorisedValues( $mss->next->authorised_value ) );
244 }
245 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fw, kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
246 if ( $mss->count ) {
247     $template->param( itemwithdrawnloop => GetAuthorisedValues( $mss->next->authorised_value) );
248 }
249
250 $template->param(count => $data->{'count'},
251         subscriptionsnumber => $subscriptionsnumber,
252     subscriptiontitle   => $data->{title},
253         C4::Search::enabled_staff_search_views,
254 );
255
256 $template->param(
257     ITEM_DATA           => \@items,
258     moredetailview      => 1,
259     loggedinuser        => $loggedinuser,
260     biblionumber        => $biblionumber,
261     biblioitemnumber    => $bi,
262     itemnumber          => $itemnumber,
263     z3950_search_params => C4::Search::z3950_search_args(GetBiblioData($biblionumber)),
264     biblio              => $biblio->unblessed,
265 );
266 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
267 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
268
269 my @allorders_using_biblio = GetOrdersByBiblionumber ($biblionumber);
270 my @deletedorders_using_biblio;
271 my @orders_using_biblio;
272 my @baskets_orders;
273 my @baskets_deletedorders;
274
275 foreach my $myorder (@allorders_using_biblio) {
276     my $basket = $myorder->{'basketno'};
277     if ((defined $myorder->{'datecancellationprinted'}) and  ($myorder->{'datecancellationprinted'} ne '0000-00-00') ){
278         push @deletedorders_using_biblio, $myorder;
279         unless (grep{ $_ eq $basket } @baskets_deletedorders){
280             push @baskets_deletedorders,$myorder->{'basketno'};
281         }
282     }
283     else {
284         push @orders_using_biblio, $myorder;
285         unless (grep { $_ eq $basket } @baskets_orders){
286             push @baskets_orders,$myorder->{'basketno'};
287             }
288     }
289 }
290
291 my $count_orders_using_biblio = scalar @orders_using_biblio ;
292 $template->param (countorders => $count_orders_using_biblio);
293
294 my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ;
295 $template->param (countdeletedorders => $count_deletedorders_using_biblio);
296
297 my $holds = $biblio->holds;
298 $template->param( holdcount => $holds->count );
299
300 output_html_with_http_headers $query, $cookie, $template->output;
301