Bug 20434: Update UNIMARC framework - auth (PUB)
[koha.git] / reports / itemslost.pl
1 #!/usr/bin/perl
2
3 # Copyright Liblime 2007
4 # Copyright Biblibre 2009
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 =head1 itemslost
23
24 This script displays lost items.
25
26 =cut
27
28 use Modern::Perl;
29
30 use CGI qw ( -utf8 );
31 use Text::CSV_XS;
32 use C4::Auth;
33 use C4::Output;
34 use C4::Biblio;
35 use C4::Items;
36
37 use Koha::AuthorisedValues;
38 use Koha::CsvProfiles;
39 use Koha::DateUtils;
40
41 my $query = new CGI;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43     {
44         template_name   => "reports/itemslost.tt",
45         query           => $query,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { reports => '*' },
49         debug           => 1,
50     }
51 );
52
53 my $params = $query->Vars;
54 my $get_items = $params->{'get_items'};
55 my $op = $query->param('op') || '';
56
57 if ( $op eq 'export' ) {
58     my @itemnumbers = $query->multi_param('itemnumber');
59     my $csv_profile_id = $query->param('csv_profile_id');
60     my @rows;
61     if ($csv_profile_id) {
62         # FIXME This following code has the same logic as GetBasketAsCSV
63         # We should refactor all the CSV export code
64         # Note: For MARC it is already done in Koha::Exporter::Record but not for SQL CSV profiles type
65         my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
66         die "There is no valid csv profile given" unless $csv_profile;
67
68         my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$csv_profile->csv_separator,'binary'=>1});
69         my $csv_profile_content = $csv_profile->content;
70         my ( @headers, @fields );
71         while ( $csv_profile_content =~ /
72             ([^=\|]+) # header
73             =?
74             ([^\|]*) # fieldname (table.row or row)
75             \|? /gxms
76         ) {
77             my $header = $1;
78             my $field = ($2 eq '') ? $1 : $2;
79
80             $header =~ s/^\s+|\s+$//g; # Trim whitespaces
81             push @headers, $header;
82
83             $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
84             $field =~ s/^\s+|\s+$//g; # Trim whitespaces
85             push @fields, $field;
86         }
87         my $items = Koha::Items->search({ itemnumber => { -in => \@itemnumbers } });
88         while ( my $item = $items->next ) {
89             my @row;
90             my $all_fields = $item->unblessed;
91             $all_fields = { %$all_fields, %{$item->biblio->unblessed}, %{$item->biblioitem->unblessed} };
92             for my $field (@fields) {
93                 push @row, $all_fields->{$field};
94             }
95             push @rows, \@row;
96         }
97         my $content = join( $csv_profile->csv_separator, @headers ) . "\n";
98         for my $row ( @rows ) {
99             $csv->combine(@$row);
100             my $string = $csv->string;
101             $content .= $string . "\n";
102         }
103         print $query->header(
104             -type       => 'text/csv',
105             -attachment => 'lost_items.csv',
106         );
107         print $content;
108         exit;
109     }
110 } elsif ( $get_items ) {
111     my $branchfilter     = $params->{'branchfilter'}     || undef;
112     my $barcodefilter    = $params->{'barcodefilter'}    || undef;
113     my $itemtypesfilter  = $params->{'itemtypesfilter'}  || undef;
114     my $loststatusfilter = $params->{'loststatusfilter'} || undef;
115     my $notforloanfilter = $params->{'notforloanfilter'} || undef;
116
117     my $params = {
118         ( $branchfilter ? ( homebranch => $branchfilter ) : () ),
119         (
120             $loststatusfilter
121             ? ( itemlost => $loststatusfilter )
122             : ( itemlost => { '!=' => 0 } )
123         ),
124         (
125             $notforloanfilter
126             ? ( notforloan => $notforloanfilter )
127             : ()
128         ),
129         ( $barcodefilter ? ( barcode => { like => "%$barcodefilter%" } ) : () ),
130     };
131
132     my $attributes;
133     if ($itemtypesfilter) {
134         if ( C4::Context->preference('item-level_itypes') ) {
135             $params->{itype} = $itemtypesfilter;
136         }
137         else {
138             # We want a join on biblioitems
139             $attributes = { join => 'biblioitem' };
140             $params->{'biblioitem.itemtype'} = $itemtypesfilter;
141         }
142     }
143
144     my $items = Koha::Items->search( $params, $attributes );
145
146     $template->param(
147         items     => $items,
148         get_items => $get_items,
149     );
150 }
151
152 # getting all itemtypes
153 my $itemtypes = Koha::ItemTypes->search_with_localization;
154
155 my $csv_profiles = Koha::CsvProfiles->search({ type => 'sql', used_for => 'export_lost_items' });
156
157 $template->param(
158     itemtypes => $itemtypes,
159     csv_profiles => $csv_profiles,
160 );
161
162 # writing the template
163 output_html_with_http_headers $query, $cookie, $template->output;