3 # Copyright Liblime 2007
4 # Copyright Biblibre 2009
6 # This file is part of Koha.
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.
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.
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>.
24 This script displays lost items.
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_html_with_http_headers );
34 use Text::CSV::Encoded;
35 use Koha::AuthorisedValues;
36 use Koha::CsvProfiles;
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41 template_name => "reports/itemslost.tt",
44 flagsrequired => { reports => '*' },
48 my $params = $query->Vars;
49 my $get_items = $params->{'get_items'};
50 my $op = $query->param('op') || '';
52 if ( $op eq 'cud-export' ) {
53 my @itemnumbers = $query->multi_param('itemnumber');
54 my $csv_profile_id = $query->param('csv_profile_id');
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;
63 my $csv_profile_content = $csv_profile->content;
64 my ( @headers, @fields );
65 while ( $csv_profile_content =~ /
68 ([^\|]*) # fieldname (table.row or row)
72 my $field = ($2 eq '') ? $1 : $2;
74 $header =~ s/^\s+|\s+$//g; # Trim whitespaces
75 push @headers, $header;
77 $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
78 $field =~ s/^\s+|\s+$//g; # Trim whitespaces
81 my $items = Koha::Items->search({ itemnumber => { -in => \@itemnumbers } });
82 while ( my $item = $items->next ) {
84 my $all_fields = $item->unblessed;
85 $all_fields = { %$all_fields, %{$item->biblio->unblessed}, %{$item->biblioitem->unblessed} };
86 for my $field (@fields) {
87 push @row, $all_fields->{$field};
91 my $delimiter = $csv_profile->csv_separator;
92 $delimiter = "\t" if $delimiter eq "\\t";
94 my $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter});
95 $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag();
96 $csv->combine(@headers);
97 my $content .= Encode::decode('UTF-8', $csv->string()) . "\n";
98 for my $row ( @rows ) {
100 $content .= $csv->string . "\n";
102 print $query->header(
104 -attachment => 'lost_items.csv',
109 } elsif ( $get_items ) {
110 my $branchfilter = $params->{'branchfilter'} || undef;
111 my $barcodefilter = $params->{'barcodefilter'} || undef;
112 my $itemtypesfilter = $params->{'itemtypesfilter'} || undef;
113 my $loststatusfilter = $params->{'loststatusfilter'} || undef;
114 my $notforloanfilter = $params->{'notforloanfilter'} || undef;
117 ( $branchfilter ? ( homebranch => $branchfilter ) : () ),
120 ? ( itemlost => $loststatusfilter )
121 : ( itemlost => { '!=' => 0 } )
125 ? ( notforloan => $notforloanfilter )
128 ( $barcodefilter ? ( barcode => { like => "%$barcodefilter%" } ) : () ),
132 if ($itemtypesfilter) {
133 if ( C4::Context->preference('item-level_itypes') ) {
134 $params->{itype} = $itemtypesfilter;
137 # We want a join on biblioitems
138 $attributes = { join => 'biblioitem' };
139 $params->{'biblioitem.itemtype'} = $itemtypesfilter;
143 my $items = Koha::Items->search( $params, $attributes );
147 get_items => $get_items,
151 # getting all itemtypes
152 my $itemtypes = Koha::ItemTypes->search_with_localization;
154 my $csv_profiles = Koha::CsvProfiles->search({ type => 'sql', used_for => 'export_lost_items' });
157 itemtypes => $itemtypes,
158 csv_profiles => $csv_profiles,
161 # writing the template
162 output_html_with_http_headers $query, $cookie, $template->output;