Fixing error trap routine in picture-upload.pl
[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 # branchcode and categorycode need to be valid
35
36 use strict;
37 use C4::Auth;
38 use C4::Output;
39 use C4::Dates qw(format_date_in_iso);
40 use C4::Context;
41 use C4::Members;
42
43 use Text::CSV;
44 use CGI;
45
46 my @columnkeys = (
47     'cardnumber',    'surname',      'firstname',        'title',
48     'othernames',    'initials',     'streetnumber',     'streettype',
49     'address',       'address2',     'city',             'zipcode',
50     'email',         'phone',        'mobile',           'fax',
51     'emailpro',      'phonepro',     'B_streetnumber',   'B_streettype',
52     'B_address',     'B_city',       'B_zipcode',        'B_email',
53     'B_phone',       'dateofbirth',  'branchcode',       'categorycode',
54     'dateenrolled',  'dateexpiry',   'gonenoaddress',    'lost',
55     'debarred',      'contactname',  'contactfirstname', 'contacttitle',
56     'borrowernotes', 'relationship', 'ethnicity',        'ethnotes',
57     'sex',           'userid',       'opacnote',         'contactnote',
58     'password',      'sort1',        'sort2'
59 );
60
61 my $input = new CGI;
62
63 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
64     {
65         template_name   => "tools/import_borrowers.tmpl",
66         query           => $input,
67         type            => "intranet",
68         authnotrequired => 0,
69         flagsrequired   => { tools => 1 },
70         debug           => 1,
71     }
72 );
73
74 my $uploadborrowers      = $input->param('uploadborrowers');
75 my $overwrite_cardnumber = $input->param('overwrite_cardnumber');
76
77 $template->param( SCRIPT_NAME => $ENV{'SCRIPT_NAME'} );
78
79 if ( $uploadborrowers && length($uploadborrowers) > 0 ) {
80     my $csv         = Text::CSV->new();
81     my $imported    = 0;
82     my $alreadyindb = 0;
83     my $overwritten = 0;
84     my $invalid     = 0;
85     while ( my $borrowerline = <$uploadborrowers> ) {
86         my $status  = $csv->parse($borrowerline);
87         my @columns = $csv->fields();
88         my %borrower;
89         if ( @columns == @columnkeys ) {
90             @borrower{@columnkeys} = @columns;
91                         foreach (qw(dateofbirth dateenrolled dateexpiry)) {
92                                 my $tempdate = $borrower{$_} or next;
93                                 $borrower{$_} = format_date_in_iso($tempdate) || '';
94                         }
95             if ( my $member =
96                 GetMember( $borrower{'cardnumber'}, 'cardnumber' ) )
97             {
98                 # borrower exists
99                 if ($overwrite_cardnumber) {
100                     $borrower{'borrowernumber'} = $member->{'borrowernumber'};
101                     ModMember(%borrower);
102                     $overwritten++;
103                 } else {
104                     $alreadyindb++;
105                 }
106             }
107             else {
108                 if (AddMember(%borrower)) {
109                     $imported++;
110                 } else {
111                     $invalid++;         # was just "$invalid", I assume incrementing was the point --atz
112                 }
113             }
114         } else {
115             $invalid++;
116         }
117     }
118     $template->param( 'uploadborrowers' => 1 );
119     $template->param(
120         'uploadborrowers' => 1,
121         'imported'        => $imported,
122         'overwritten'     => $overwritten,
123         'alreadyindb'     => $alreadyindb,
124         'invalid'         => $invalid,
125         'total'           => $imported + $alreadyindb + $invalid + $overwritten,
126     );
127
128 }
129 output_html_with_http_headers $input, $cookie, $template->output;
130