Bug 4078: Pass with_symbol to module
[koha.git] / misc / export_borrowers.pl
blob5f5c7f74eddb50249f6fd6df36bbb479063a5882
1 #!/usr/bin/perl
3 # Copyright 2011 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 # Script to export borrowers
22 use Modern::Perl;
23 use Text::CSV;
24 use Getopt::Long qw(:config no_ignore_case);
26 use C4::Context;
27 use Koha::Patrons;
29 binmode STDOUT, ":encoding(UTF-8)";
31 sub print_usage {
32 ( my $basename = $0 ) =~ s|.*/||;
33 print <<USAGE;
35 $basename
36 Export patron informations in CSV format.
37 It prints to standard output. Use redirection to save CSV in a file.
39 Usage:
40 $0 [--field=FIELD [--field=FIELD [...]]] [--separator=CHAR] [--show-header] [--where=CONDITION]
41 $0 -h
43 -f, --field=FIELD Field to export. It is repeatable and has to match
44 column names of the borrower table (also as 'description' and 'category_type'
45 If no field is specified, then all fields will be
46 exported.
47 -s, --separator=CHAR This character will be used to separate fields.
48 Some characters like | or ; will need to be escaped
49 in the parameter setting, like -s=\\| or -s=\\;
50 If no separator is specified, the delimiter pref
51 will be used (or a comma, if the pref is empty)
52 -H, --show-header Print field names on first row
53 -w, --where=CONDITION Condition to filter borrowers to export
54 (SQL where clause).
55 CONDITION must be enclosed by double quotes
56 You can use single quotes around a field value
57 within the condition like:
58 --where "surname='De Lattre'"
59 -h, --help Show this help
61 USAGE
64 # Getting parameters
65 my @fields;
66 my $separator;
67 my $show_header;
68 my $where;
69 my $help;
71 GetOptions(
72 'field|f=s' => \@fields,
73 'separator|s=s' => \$separator,
74 'show-header|H' => \$show_header,
75 'where|w=s' => \$where,
76 'help|h' => \$help
77 ) or print_usage, exit 1;
79 if ($help) {
80 print_usage;
81 exit;
84 # Getting borrowers
85 my $dbh = C4::Context->dbh;
86 my $query = "SELECT borrowernumber FROM borrowers";
87 $query .= " WHERE $where" if ($where);
88 $query .= " ORDER BY borrowernumber";
89 my $sth = $dbh->prepare($query);
90 $sth->execute;
92 unless ( $separator ) {
93 $separator = C4::Context->preference('delimiter') || ',';
94 $separator = "\t" if ($separator eq 'tabulation');
97 my $csv = Text::CSV->new( { sep_char => $separator, binary => 1 } );
99 # If the user did not specify any field to export, we assume they want them all
100 # We retrieve the first borrower informations to get field names
101 my ($borrowernumber) = $sth->fetchrow_array or die "No borrower to export";
102 my $patron = Koha::Patrons->find( $borrowernumber ); # FIXME Now is_expired is no longer available
103 # We will have to use Koha::Patron and allow method calls
104 my $category = $patron->category;
105 my $member = $patron->unblessed;
106 $member->{description} = $category->description;
107 $member->{category_type} = $category->category_type;
109 @fields = keys %$member unless (@fields);
111 if ($show_header) {
112 $csv->combine(@fields);
113 print $csv->string . "\n";
116 $csv->combine(
117 map {
118 ( defined $member->{$_} and !ref $member->{$_} )
119 ? $member->{$_}
120 : ''
121 } @fields
123 die "Invalid character at borrower $borrowernumber: ["
124 . $csv->error_input . "]\n"
125 if ( !defined( $csv->string ) );
126 print $csv->string . "\n";
128 while ( my $borrowernumber = $sth->fetchrow_array ) {
129 my $patron = Koha::Patrons->find( $borrowernumber );
130 my $category = $patron->category;
131 my $member = $patron->unblessed;
132 $member->{description} = $category->description;
133 $member->{category_type} = $category->category_type;
134 $csv->combine(
135 map {
136 ( defined $member->{$_} and !ref $member->{$_} )
137 ? $member->{$_}
138 : ''
139 } @fields
141 die "Invalid character at borrower $borrowernumber: ["
142 . $csv->error_input . "]\n"
143 if ( !defined( $csv->string ) );
144 print $csv->string . "\n";