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