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