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