Bug 19889: Add feedback when exclusion is updated on moredetail.pl
[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         authnotrequired => 0,
49         flagsrequired   => { catalogue => 1 },
50     }
51 );
52
53 $template->param(
54     updated_exclude_from_local_holds_priority => $query->param('updated_exclude_from_local_holds_priority')
55 );
56
57 if($query->cookie("holdfor")){ 
58     my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") );
59     $template->param(
60         holdfor => $query->cookie("holdfor"),
61         holdfor_surname => $holdfor_patron->surname,
62         holdfor_firstname => $holdfor_patron->firstname,
63         holdfor_cardnumber => $holdfor_patron->cardnumber,
64     );
65 }
66
67 if( $query->cookie("searchToOrder") ){
68     my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") );
69     $template->param(
70         searchtoorder_basketno => $basketno,
71         searchtoorder_vendorid => $vendorid
72     );
73 }
74
75 # get variables
76
77 my $biblionumber=$query->param('biblionumber');
78 $biblionumber = HTML::Entities::encode($biblionumber);
79 my $title=$query->param('title');
80 my $bi=$query->param('bi');
81 $bi = $biblionumber unless $bi;
82 my $itemnumber = $query->param('itemnumber');
83 my $data = &GetBiblioData($biblionumber);
84 my $dewey = $data->{'dewey'};
85 my $showallitems = $query->param('showallitems');
86
87 #coping with subscriptions
88 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
89
90 # FIXME Dewey is a string, not a number, & we should use a function
91 # $dewey =~ s/0+$//;
92 # if ($dewey eq "000.") { $dewey = "";};
93 # if ($dewey < 10){$dewey='00'.$dewey;}
94 # if ($dewey < 100 && $dewey > 10){$dewey='0'.$dewey;}
95 # if ($dewey <= 0){
96 #      $dewey='';
97 # }
98 # $dewey=~ s/\.$//;
99 # $data->{'dewey'}=$dewey;
100
101 my $fw = GetFrameworkCode($biblionumber);
102 my @all_items= GetItemsInfo($biblionumber);
103 my @items;
104 my $patron = Koha::Patrons->find( $loggedinuser );
105 for my $itm (@all_items) {
106     push @items, $itm unless ( $itm->{itemlost} && 
107                                $patron->category->hidelostitems &&
108                                !$showallitems && 
109                                ($itemnumber != $itm->{itemnumber}));
110 }
111
112 my $record=GetMarcBiblio({ biblionumber => $biblionumber });
113
114 output_and_exit( $query, $cookie, $template, 'unknown_biblio')
115     unless $record;
116
117 my $hostrecords;
118 # adding items linked via host biblios
119 my @hostitems = GetHostItemsInfo($record);
120 if (@hostitems){
121         $hostrecords =1;
122         push (@items,@hostitems);
123 }
124
125 my $biblio = Koha::Biblios->find( $biblionumber );
126
127 my $totalcount=@all_items;
128 my $showncount=@items;
129 my $hiddencount = $totalcount - $showncount;
130 $data->{'count'}=$totalcount;
131 $data->{'showncount'}=$showncount;
132 $data->{'hiddencount'}=$hiddencount;  # can be zero
133
134 my $ccodes =
135   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) };
136 my $copynumbers =
137   { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) };
138
139 my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
140
141 $data->{'itemtypename'} = $itemtypes->{ $data->{'itemtype'} }->{'translated_description'}
142   if $data->{itemtype} && exists $itemtypes->{ $data->{itemtype} };
143 foreach ( keys %{$data} ) {
144     $template->param( "$_" => defined $data->{$_} ? $data->{$_} : '' );
145 }
146
147 ($itemnumber) and @items = (grep {$_->{'itemnumber'} == $itemnumber} @items);
148 foreach my $item (@items){
149     $item->{object} = Koha::Items->find( $item->{itemnumber} );
150     $item->{'collection'}              = $ccodes->{ $item->{ccode} } if $ccodes && $item->{ccode} && exists $ccodes->{ $item->{ccode} };
151     $item->{'itype'}                   = $itemtypes->{ $item->{'itype'} }->{'translated_description'} if exists $itemtypes->{ $item->{'itype'} };
152     $item->{'replacementprice'}        = $item->{'replacementprice'};
153     if ( defined $item->{'copynumber'} ) {
154         $item->{'displaycopy'} = 1;
155         if ( defined $copynumbers->{ $item->{'copynumber'} } ) {
156             $item->{'copyvol'} = $copynumbers->{ $item->{'copynumber'} }
157         }
158         else {
159             $item->{'copyvol'} = $item->{'copynumber'};
160         }
161     }
162
163     # item has a host number if its biblio number does not match the current bib
164     if ($item->{biblionumber} ne $biblionumber){
165         $item->{hostbiblionumber} = $item->{biblionumber};
166         $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
167     }
168
169     my $order  = GetOrderFromItemnumber( $item->{'itemnumber'} );
170     $item->{'ordernumber'}             = $order->{'ordernumber'};
171     $item->{'basketno'}                = $order->{'basketno'};
172     $item->{'orderdate'}               = $order->{'entrydate'};
173     if ($item->{'basketno'}){
174             my $basket = GetBasket($item->{'basketno'});
175         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
176         $item->{'vendor'} = $bookseller->name;
177     }
178     $item->{'invoiceid'}               = $order->{'invoiceid'};
179     if($item->{invoiceid}) {
180         my $invoice = GetInvoice($item->{invoiceid});
181         $item->{invoicenumber} = $invoice->{invoicenumber} if $invoice;
182     }
183     $item->{'datereceived'}            = $order->{'datereceived'};
184
185     if ($item->{notforloantext} or $item->{itemlost} or $item->{damaged} or $item->{withdrawn}) {
186         $item->{status_advisory} = 1;
187     }
188
189     # Add paidfor info
190     if ( $item->{itemlost} ) {
191         my $accountlines = Koha::Account::Lines->search(
192             {
193                 itemnumber        => $item->{itemnumber},
194                 debit_type_code   => 'LOST',
195                 status            => [ undef, { '<>' => 'RETURNED' } ],
196                 amountoutstanding => 0
197             },
198             {
199                 order_by => { '-desc' => 'date' },
200                 rows     => 1
201             }
202         );
203
204         if ( my $accountline = $accountlines->next ) {
205             my $payment_offsets = Koha::Account::Offsets->search(
206                 {
207                     debit_id  => $accountline->id,
208                     credit_id => { '!=' => undef }, # it is not the debit itself
209                     type => { '!=' => [ 'Writeoff', 'Forgiven' ] },
210                     amount => { '<' => 0 }    # credits are negative on the DB
211                 },
212                 { order_by => { '-desc' => 'created_on' } }
213             );
214
215             if ($payment_offsets->count) {
216                 my $patron = $accountline->patron;
217                 my $payment_offset = $payment_offsets->next;
218                 $item->{paidfor} = { patron => $patron, created_on => $payment_offset->created_on };
219             }
220         }
221     }
222
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;
228         }
229     }
230     if ($item->{'datedue'}) {
231         $item->{'issue'}= 1;
232     } else {
233         $item->{'issue'}= 0;
234     }
235
236     if ( $item->{'borrowernumber'} ) {
237         my $curr_borrower = Koha::Patrons->find( $item->{borrowernumber} );
238         $item->{patron} = $curr_borrower;
239     }
240 }
241
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 ) );
245 }
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 ) );
249 }
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) );
253 }
254
255 $template->param(count => $data->{'count'},
256         subscriptionsnumber => $subscriptionsnumber,
257     subscriptiontitle   => $data->{title},
258         C4::Search::enabled_staff_search_views,
259 );
260
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,
270 );
271 $template->param(ONLY_ONE => 1) if ( $itemnumber && $showncount != @items );
272 $template->{'VARS'}->{'searchid'} = $query->param('searchid');
273
274 my $holds = $biblio->holds;
275 $template->param( holdcount => $holds->count );
276
277 output_html_with_http_headers $query, $cookie, $template->output;
278