Bug 34478: Correct op name in CSV profile deletion confirmation step
[koha.git] / tools / import_borrowers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime
4 # Parts copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 # Script to take some borrowers data in a known format and load it into Koha
22 #
23 # File format
24 #
25 # cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,
26 # address line , address line 2, city, zipcode, contry, email, phone, mobile, fax, work email, work phone,
27 # alternate streetnumber, alternate streettype, alternate address line 1, alternate city,
28 # alternate zipcode, alternate country, alternate email, alternate phone, date of birth, branchcode,
29 # categorycode, enrollment date, expiry date, noaddress, lost, debarred, contact surname,
30 # contact firstname, contact title, borrower notes, contact relationship
31 # gender, username, opac note, contact note, password, sort one, sort two
32 #
33 # any fields except cardnumber can be blank but the number of fields must match
34 # dates should be in the format you have set up Koha to expect
35 # branchcode and categorycode need to be valid
36
37 use Modern::Perl;
38
39 use C4::Auth qw( get_template_and_user );
40 use C4::Output qw( output_and_exit output_html_with_http_headers );
41 use Koha::Database::Columns;
42 use Koha::Patrons;
43 use Koha::DateUtils qw( dt_from_string );
44 use Koha::Token;
45 use Koha::Libraries;
46 use Koha::Patron::Categories;
47 use Koha::Patron::Attribute::Types;
48 use Koha::List::Patron qw( AddPatronList AddPatronsToList );
49
50 use Koha::Patrons::Import;
51 my $Import = Koha::Patrons::Import->new();
52
53 use Text::CSV;
54
55 use CGI qw ( -utf8 );
56
57 my $extended = C4::Context->preference('ExtendedPatronAttributes');
58
59 my @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } Koha::Patrons->columns();
60 push( @columnkeys, 'patron_attributes' ) if $extended;
61 push( @columnkeys, qw( guarantor_relationship guarantor_id ) );
62
63 my $input = CGI->new();
64 my $op    = $input->param('op') // q{};
65
66 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
67     {
68         template_name   => "tools/import_borrowers.tt",
69         query           => $input,
70         type            => "intranet",
71         flagsrequired   => { tools => 'import_patrons' },
72     }
73 );
74
75 # get the patron categories and pass them to the template
76 my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']})->as_list;
77 $template->param( categories => \@patron_categories );
78 $template->param( borrower_fields => Koha::Database::Columns->columns->{borrowers} );
79
80 if ( $input->param('sample') ) {
81     our $csv = Text::CSV->new( { binary => 1 } );    # binary needed for non-ASCII Unicode
82     print $input->header(
83         -type       => 'application/vnd.sun.xml.calc',    # 'application/vnd.ms-excel' ?
84         -attachment => 'patron_import.csv',
85     );
86     $csv->combine(@columnkeys);
87     print $csv->string, "\n";
88     exit 0;
89 }
90
91 my @preserve_fields = $input->multi_param('preserve_existing');
92
93 my $uploadborrowers = $input->param('uploadborrowers');
94 my $matchpoint      = $input->param('matchpoint');
95 my $welcome_new     = $input->param('welcome_new');
96 if ($matchpoint) {
97     $matchpoint =~ s/^patron_attribute_//;
98 }
99
100 #create a patronlist
101 my $createpatronlist = $input->param('createpatronlist') || 0;
102 my $dt = dt_from_string();
103 my $timestamp = $dt->ymd('-').' '.$dt->hms(':');
104 my $patronlistname = $uploadborrowers . ' (' . $timestamp .')';
105
106 $template->param( SCRIPT_NAME => '/cgi-bin/koha/tools/import_borrowers.pl' );
107
108 if ( $op eq 'cud-import' && $uploadborrowers && length($uploadborrowers) > 0 ) {
109
110     my $handle   = $input->upload('uploadborrowers');
111     my %defaults = $input->Vars;
112     my $overwrite_passwords = defined $input->param('overwrite_passwords') ? 1 : 0;
113     my $update_dateexpiry = $input->param('update_dateexpiry');
114     my $return = $Import->import_patrons(
115         {
116             file                         => $handle,
117             defaults                     => \%defaults,
118             matchpoint                   => $matchpoint,
119             overwrite_cardnumber         => scalar $input->param( 'overwrite_cardnumber' ),
120             overwrite_passwords          => $overwrite_passwords,
121             preserve_extended_attributes => scalar $input->param( 'ext_preserve' ) || 0,
122             preserve_fields              => \@preserve_fields,
123             update_dateexpiry            => $update_dateexpiry ? 1 : 0,
124             update_dateexpiry_from_today => $update_dateexpiry eq "now" ? 1 : 0,
125             send_welcome                 => $welcome_new,
126         }
127     );
128
129     my $feedback    = $return->{feedback};
130     my $errors      = $return->{errors};
131     my $imported    = $return->{imported};
132     my $overwritten = $return->{overwritten};
133     my $alreadyindb = $return->{already_in_db};
134     my $invalid     = $return->{invalid};
135     my $imported_borrowers = $return->{imported_borrowers};
136
137     if ( $imported && $createpatronlist ) {
138         my $patronlist = AddPatronList({ name => $patronlistname });
139         AddPatronsToList({ list => $patronlist, borrowernumbers => $imported_borrowers });
140         $template->param('patronlistname' => $patronlistname);
141     }
142
143     my $uploadinfo = $input->uploadInfo($uploadborrowers);
144     foreach ( keys %$uploadinfo ) {
145         push @$feedback, { feedback => 1, name => $_, value => $uploadinfo->{$_}, $_ => $uploadinfo->{$_} };
146     }
147
148     push @$feedback, { feedback => 1, name => 'filename', value => $uploadborrowers, filename => $uploadborrowers };
149
150     $template->param(
151         uploadborrowers => 1,
152         errors          => $errors,
153         feedback        => $feedback,
154         imported        => $imported,
155         overwritten     => $overwritten,
156         alreadyindb     => $alreadyindb,
157         invalid         => $invalid,
158         total           => $imported + $alreadyindb + $invalid + $overwritten,
159     );
160
161 } else {
162     if ($extended) {
163         my @matchpoints = ();
164         my $attribute_types = Koha::Patron::Attribute::Types->search;
165
166         while ( my $attr_type = $attribute_types->next ) {
167             if ( $attr_type->unique_id() ) {
168                 push @matchpoints,
169                   { code => "patron_attribute_" . $attr_type->code(), description => $attr_type->description() };
170             }
171         }
172         $template->param( matchpoints => \@matchpoints );
173     }
174 }
175
176 output_html_with_http_headers $input, $cookie, $template->output;