Bug 9045 : Adding --where parameter to filter what kind of borrowers to export and...
[koha.git] / misc / export_borrowers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 # Script to export borrowers
21
22 use Modern::Perl;
23 use Text::CSV;
24 use Getopt::Long qw(:config no_ignore_case);
25
26 use C4::Context;
27 use C4::Members;
28
29 use encoding 'utf8';
30
31 sub print_usage {
32     ( my $basename = $0 ) =~ s|.*/||;
33     print <<USAGE;
34
35 $basename
36     Export patron informations in CSV format.
37     It prints to standard output. Use redirection to save CSV in a file.
38
39 Usage:
40 $0 [--field=FIELD [--field=FIELD [...]]] [--separator=CHAR] [--show-header] [--where=CONDITION]
41 $0 -h
42
43     -f, --field=FIELD       Field to export. It is repeatable and has to match
44                             keys returned by the GetMemberDetails function.
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 specifield, a comma will be used.
51     -H, --show-header       Print field names on first row
52     -w, --where=CONDITION   Condition to filter borrowers to export
53                             (SQL where clause)
54     -h, --help              Show this help
55
56 USAGE
57 }
58
59 # Getting parameters
60 my @fields;
61 my $separator;
62 my $show_header;
63 my $where;
64 my $help;
65
66 GetOptions(
67     'field|f=s'     => \@fields,
68     'separator|s=s' => \$separator,
69     'show-header|H' => \$show_header,
70     'where|w=s'       => \$where,
71     'help|h'        => \$help
72 ) or print_usage, exit 1;
73
74 if ($help) {
75     print_usage;
76     exit;
77 }
78
79 # Getting borrowers
80 my $dbh   = C4::Context->dbh;
81 my $query = "SELECT borrowernumber FROM borrowers";
82 $query .= " WHERE $where" if ($where);
83 $query .= " ORDER BY borrowernumber";
84 my $sth   = $dbh->prepare($query);
85 $sth->execute;
86
87 my $csv = Text::CSV->new( { sep_char => $separator, binary => 1 } );
88
89 # If the user did not specify any field to export, we assume he wants them all
90 # We retrieve the first borrower informations to get field names
91 my ($borrowernumber) = $sth->fetchrow_array;
92 my $member = GetMemberDetails($borrowernumber);
93 @fields = keys %$member unless (@fields);
94
95 if ($show_header) {
96     $csv->combine(@fields);
97     print $csv->string . "\n";
98 }
99
100 $csv->combine(
101     map {
102         ( defined $member->{$_} and !ref $member->{$_} )
103           ? $member->{$_}
104           : ''
105       } @fields
106 );
107 die "Invalid character at borrower $borrowernumber: ["
108   . $csv->error_input . "]\n"
109   if ( !defined( $csv->string ) );
110 print $csv->string . "\n";
111
112 while ( my $borrowernumber = $sth->fetchrow_array ) {
113     $member = GetMemberDetails($borrowernumber);
114     $csv->combine(
115         map {
116             ( defined $member->{$_} and !ref $member->{$_} )
117               ? $member->{$_}
118               : ''
119           } @fields
120     );
121     die "Invalid character at borrower $borrowernumber: ["
122       . $csv->error_input . "]\n"
123       if ( !defined( $csv->string ) );
124     print $csv->string . "\n";
125 }