Bug 27920: Add ability to update patron expiration dates when importing patrons
[koha.git] / opac / opac-basket.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21
22 use C4::Koha;
23 use C4::Biblio qw(
24     GetFrameworkCode
25     GetMarcSeries
26     GetMarcSubjects
27     GetMarcUrls
28 );
29 use C4::Auth qw( get_template_and_user );
30 use C4::Output qw( output_html_with_http_headers );
31 use Koha::RecordProcessor;
32 use Koha::CsvProfiles;
33 use Koha::AuthorisedValues;
34 use Koha::Biblios;
35 use Koha::Items;
36
37 my $query = CGI->new;
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
40     {
41         template_name   => "opac-basket.tt",
42         query           => $query,
43         type            => "opac",
44         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
45     }
46 );
47
48 my $bib_list     = $query->param('bib_list');
49 my $verbose      = $query->param('verbose');
50
51 if ($verbose)      { $template->param( verbose      => 1 ); }
52
53 my @bibs = split( /\//, $bib_list );
54 my @results;
55
56 my $num = 1;
57 my $marcflavour = C4::Context->preference('marcflavour');
58 if (C4::Context->preference('TagsEnabled')) {
59         $template->param(TagsEnabled => 1);
60         foreach (qw(TagsShowOnList TagsInputOnList)) {
61                 C4::Context->preference($_) and $template->param($_ => 1);
62         }
63 }
64
65 my $logged_in_user = Koha::Patrons->find($borrowernumber);
66
67 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
68 my $rules = C4::Context->yaml_preference('OpacHiddenItems');
69
70 foreach my $biblionumber ( @bibs ) {
71     $template->param( biblionumber => $biblionumber );
72
73     my $biblio           = Koha::Biblios->find( $biblionumber ) or next;
74     my $dat              = $biblio->unblessed;
75
76     # No filtering on the item records needed for the record itself
77     # since the only reason item information is grabbed is because of branchcodes.
78     my $record = $biblio->metadata->record;
79     my $framework = &GetFrameworkCode( $biblionumber );
80     $record_processor->options({
81         interface => 'opac',
82         frameworkcode => $framework
83     });
84     $record_processor->process($record);
85     next unless $record;
86     my $marcnotesarray   = $biblio->get_marc_notes({ opac => 1 });
87     my $marcauthorsarray = $biblio->get_marc_contributors;
88     my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
89     my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
90     my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
91
92     # If every item is hidden, then the biblio should be hidden too.
93     next
94       if $biblio->hidden_in_opac({ rules => $rules });
95
96     # grab all the items...
97     my $items        = $biblio->items->filter_by_visible_in_opac({ patron => $logged_in_user })->unblessed;
98     foreach my $item (@$items) {
99         my $reserve_status = C4::Reserves::GetReserveStatus($item->{itemnumber});
100         if( $reserve_status eq "Waiting"){ $item->{'waiting'} = 1; }
101         if( $reserve_status eq "Processing"){ $item->{'processing'} = 1; }
102     }
103
104     my $hasauthors = 0;
105     if($dat->{'author'} || @$marcauthorsarray) {
106       $hasauthors = 1;
107     }
108     my $collections =
109       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) };
110     my $shelflocations =
111       { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) };
112
113         # COinS format FIXME: for books Only
114         my $fmt = substr $record->leader(), 6,2;
115         my $fmts;
116         $fmts->{'am'} = 'book';
117         $dat->{ocoins_format} = $fmts->{$fmt};
118
119     if ( $num % 2 == 1 ) {
120         $dat->{'even'} = 1;
121     }
122
123     for my $itm (@$items) {
124         if ($itm->{'location'}){
125             $itm->{'location_opac'} = $shelflocations->{$itm->{'location'} };
126         }
127         my $item_object = Koha::Items->find($itm->{itemnumber});
128         my $transfer = $item_object->get_transfer;
129         if ( $transfer && $transfer->in_transit ) {
130             $itm->{transfertwhen} = $transfer->datesent;
131             $itm->{transfertfrom} = $transfer->frombranch;
132             $itm->{transfertto} = $transfer->tobranch;
133         }
134     }
135     $num++;
136     $dat->{biblionumber} = $biblionumber;
137     $dat->{ITEM_RESULTS}   = $items;
138     $dat->{MARCNOTES}      = $marcnotesarray;
139     $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
140     $dat->{MARCAUTHORS}    = $marcauthorsarray;
141     $dat->{MARCSERIES}  = $marcseriesarray;
142     $dat->{MARCURLS}    = $marcurlsarray;
143     $dat->{HASAUTHORS}  = $hasauthors;
144
145     push( @results, $dat );
146 }
147
148 my $resultsarray = \@results;
149
150 # my $itemsarray=\@items;
151
152 $template->param(
153     csv_profiles => Koha::CsvProfiles->search(
154         { type => 'marc', used_for => 'export_records', staff_only => 0 } ),
155     bib_list => $bib_list,
156     BIBLIO_RESULTS => $resultsarray,
157 );
158
159 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };