New tool to import borrowers
[koha.git] / tools / import_borrowers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime Ltd
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # Script to take some borrowers data in a known format and load it into Koha
21 #
22 # File format
23 #
24 # cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,
25 # address line , address line 2, city, zipcode, email, phone, mobile, fax, work email, work phone,
26 # alternate streetnumber, alternate streettype, alternate address line 1, alternate city,
27 # alternate zipcode, alternate email, alternate phone, date of birth, branchcode,
28 # categorycode, enrollment date, expiry date, noaddress, lost, debarred, contact surname,
29 # contact firstname, contact title, borrower notes, contact relationship, ethnicity, ethnicity notes
30 # gender, username, opac note, contact note, password, sort one, sort two
31 #
32 # any fields except cardnumber can be blank but the number of fields must match
33 # dates should be in the format you have set up Koha to expect
34
35 use strict;
36 use C4::Auth;
37 use C4::Output;
38 use C4::Date;
39 use C4::Context;
40
41 use Text::CSV;
42 use CGI;
43
44 my @columnkeys = (
45     'cardnumber',   'surname',          'firstname',    'title',
46     'othernames',   'initials',         'streetnumber', 'streettype',
47     'address',      'address2',         'city',         'email',
48     'phone',        'mobile',           'fax',          'emailpro',
49     'phonepro',     'B_streetnumber',   'B_streettype', 'B_address',
50     'B_city',       'B_zipcode',        'B_email',      'B_phone',
51     'dateofbirth',  'branchcode',       'categorycode', 'dateenrolled',
52     'dateexpiry',   'gonenoaddress',    'lost',         'debarred',
53     'contactname',  'contactfirstname', 'contacttitle', 'borrowernotes',
54     'relationship', 'ethnicity',        'ethnotes',     'sex',
55     'userid',       'opacnote',         'contactnote',  'password',
56     'sort1',        'sort2'
57 );
58
59 my $input = new CGI;
60
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => "tools/import_borrowers.tmpl",
64         query           => $input,
65         type            => "intranet",
66         authnotrequired => 0,
67         flagsrequired   => { tools => 1 },
68         debug           => 1,
69     }
70 );
71
72 my $uploadborrowers      = $input->param('uploadborrowers');
73 my $overwrite_cardnumber = $input->param('overwrite_cardnumber');
74
75 $template->param( SCRIPT_NAME => $ENV{'SCRIPT_NAME'} );
76
77 if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
78     my $csv         = Text::CSV->new();
79     my $imported    = 0;
80     my $alreadyindb = 0;
81     my $overwritten = 0;
82     my $invalid     = 0;
83     while ( my $borrowerline = <$uploadborrowers> ) {
84
85         my $status  = $csv->parse($borrowerline);
86         my @columns = $csv->fields();
87         my %borrower;
88         if ( @columns == @columnkeys ) {
89             @borrower{@columnkeys} = @columns;
90             if ( GetMember( $borrower{'cardnumber'}, 'cardnumber' ) ) {
91
92                 # borrower exists
93                 if ($overwrite_cardnumber) {
94                     ModMember(%borrower);
95                     $overwritten++;
96                 }
97                 else {
98                     $alreadyindb++;
99                 }
100             }
101             else {
102                 AddMember(%borrower);
103                 $imported++;
104             }
105         }
106         else {
107             $invalid++;
108         }
109     }
110     $template->param( 'uploadborrowers' => 1 );
111     $template->param(
112         'uploadborrowers' => 1,
113         'imported'        => $imported,
114         'overwritten'     => $overwritten,
115         'alreadyindb'     => $alreadyindb,
116         'invalid'         => $invalid,
117         'total'           => $imported + $alreadyindb + $invalid + $overwritten,
118     );
119
120 }
121 output_html_with_http_headers $input, $cookie, $template->output;
122