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