- [% IF ( uploadborrowers ) %]
+ [% IF op == 'enqueued' %]
-
Import results
-
- -
- [% tpx("Patron import", "{count_of_imported_records} imported records", {count_of_imported_records = imported}) | html %]
- [% IF ( lastimported ) %]
- [% tpx("Patron import", "(last was {last_imported_record})", {last_imported_record = lastimported}) | html %]
- [% END %]
-
- [% IF imported and patronlistname %]
- - Patron list with imported patrons: [% patronlistname | html %]
- [% END %]
- -
- [% tpx("Patron import", "{count_of_overwritten_records} overwritten records", {count_of_overwritten_records = overwritten}) | html %]
- [% IF ( lastoverwritten ) %]
- [% tpx("Patron import", "(last was {last_overwritten_record})", {last_overwritten_record = lastoverwritten}) | html %]
- [% END %]
-
- -
- [% tpx("Patron import", "{count_of_existing_records} not imported because they are already in borrowers table and overwrite is disabled ", {count_of_existing_records = alreadyindb}) | html %]
- [% IF ( lastalreadyindb ) %]
- [% tpx("Patron import", "(last was {last_existing_record})", {last_existing_record = lastalreadyindb}) | html %]
- [% END %]
-
- -
- [% tpx("Patron import", "{count_of_invalid_records} not imported because they are not in the expected format", {count_of_invalid_records = invalid}) | html %]
- [% IF ( lastinvalid ) %]
- [% tpx("Patron import", "(last was {last_invalid_record})", {last_invalid_record = lastinvalid}) | html %]
- [% END %]
- [% tpx("Patron import", "{total} records parsed", {total = total}) | html %]
- - Back to Tools
-
-
- [% IF ( feedback ) %]
-
-
-
-
Feedback
-
- [% FOREACH f IN feedback %]
- -
- [% IF ( f.filename ) %]
- Parsing upload file [% f.filename | html %]
- [% ELSIF ( f.backend ) %]
- Upload parsed using [% f.backend | html %]
- [% ELSIF ( f.headerrow ) %]
- These fields found: [% f.value | html %]
- [% ELSIF ( f.already_in_db ) %]
- Patron already in database: [% f.value | html %]
- [% ELSE %]
- [% f.name | html %]
- : [% f.value | html %]
- [% END %]
-
- [% END %]
-
-
- [% END %]
-
- [% IF ( errors ) %]
-
-
-
-
Error analysis
-
- [% FOREACH e IN errors %]
- [% IF ( e.badheader ) %]- Header row could not be parsed
[% END %]
-
- [% FOREACH missing_critical IN e.missing_criticals %]
- -
- Line [% missing_critical.line | html %]
-
- [% IF ( missing_critical.badparse ) %]
- could not be parsed!
- [% ELSIF ( missing_critical.bad_date ) %]
- has "[% missing_critical.key | html %]" in unrecognized format: "[% missing_critical.value | html %]"
- [% ELSE %]
- Critical field "[% missing_critical.key | html %]"
-
- [% IF ( missing_critical.branch_map ) %]
- has unrecognized value "[% missing_critical.value | html %]"
- [% ELSIF ( missing_critical.category_map ) %]
- has unrecognized value "[% missing_critical.value | html %]"
- [% ELSE %]
- missing
- [% END %]
- (borrowernumber: [% missing_critical.borrowernumber | html %]; surname: [% missing_critical.surname | html %]).
- [% END %]
-
-
- [% missing_critical.lineraw | html %]
-
- [% END %]
-
- [% IF e.invalid_cardnumber %]
- - Card number [% e.cardnumber | html %] is not a valid card number [% IF e.borrowernumber %](for patron with borrowernumber [% e.borrowernumber | html %])[% END %]
- [% END %]
- [% IF e.duplicate_userid %]
- - Userid [% e.userid | html %] is already used by another patron.
- [% END %]
- [% IF e.passwd_too_short %]
- - Password is too short for patron with borrowernumber [% e.borrowernumber | html %]. Minimum length is [% e.min_length | html %], length is [% e.length | html %]
- [% END %]
- [% IF e.passwd_whitespace %]
- - Password contains whitespace for patron with borrowernumber [% e.borrowernumber | html %].
- [% END %]
- [% IF e.passwd_too_weak %]
- - Password is too weak for patron with borrowernumber [% e.borrowernumber | html %].
- [% END %]
- [% IF e.passwd_plugin_err %]
- - Password plugin error for patron with borrowernumber [% e.borrowernumber | html %].
- [% END %]
- [% IF e.passwd_unknown_err %]
- - Password error for patron with borrowernumber [% e.borrowernumber | html %].
- [% END %]
- [% IF e.patron_attribute_unique_id_constraint %]
- -
- [% IF e.borrowernumber %]
- Patron attribute [% e.attribute.code | html %] must be unique for patron with borrowernumber [% e.borrowernumber | html %].
- [% ELSE %]
- Patron attribute [% e.attribute.code | html %] must be unique for patron [% e.patron_id | html %].
- [% END %]
-
- [% END %]
- [% IF e.patron_attribute_invalid_type %]
- -
- [% IF e.borrowernumber %]
- Patron attribute [% e.attribute_type_code | html %] is invalid for patron with borrowernumber [% e.borrowernumber | html %].
- [% ELSE %]
- Patron attribute [% e.attribute_type_code | html %] is invalid for patron [% e.patron_id | html %].
- [% END %]
-
- [% END %]
- [% IF e.patron_attribute_non_repeatable %]
- -
- [% IF e.borrowernumber %]
- Patron attribute [% e.attribute.code | html %] is non-repeatable for patron with borrowernumber [% e.borrowernumber | html %].
- [% ELSE %]
- Patron attribute [% e.attribute.code | html %] is non-repeatable for patron [% e.patron_id | html %].
- [% END %]
-
- [% END %]
- [% END %]
-
+ [% IF error == 'cannot_enqueue_job' %]
+
The import job could not be enqueued. Please try again.
+ [% ELSE %]
+
[% END %]
diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl
index eed7265758..92c729757b 100755
--- a/tools/import_borrowers.pl
+++ b/tools/import_borrowers.pl
@@ -36,19 +36,16 @@
use Modern::Perl;
+use Try::Tiny qw( catch try );
+
use C4::Auth qw( get_template_and_user );
use C4::Output qw( output_and_exit output_html_with_http_headers );
+use Koha::BackgroundJob::PatronImport;
use Koha::Database::Columns;
-use Koha::Patrons;
use Koha::DateUtils qw( dt_from_string );
-use Koha::Token;
-use Koha::Libraries;
-use Koha::Patron::Categories;
use Koha::Patron::Attribute::Types;
-use Koha::List::Patron qw( AddPatronList AddPatronsToList );
-
-use Koha::Patrons::Import;
-my $Import = Koha::Patrons::Import->new();
+use Koha::Patron::Categories;
+use Koha::Patrons;
use Text::CSV;
@@ -105,58 +102,48 @@ my $timestamp = $dt->ymd('-') . ' ' . $dt->hms(':');
if ( $op eq 'cud-import' && $uploadborrowers && length($uploadborrowers) > 0 ) {
- my $patronlistname = $uploadborrowers . ' (' . $timestamp . ')';
- my $handle = $input->upload('uploadborrowers');
- my %defaults = $input->Vars;
+ my $handle = $input->upload('uploadborrowers');
+ my $file_content = do { local $/; <$handle> };
+ my $patronlistname = $uploadborrowers . ' (' . $timestamp . ')';
+
+ my %defaults;
+ for my $k (@columnkeys) {
+ my $v = scalar $input->param($k);
+ $defaults{$k} = $v if defined $v && length $v;
+ }
+
my $overwrite_passwords = defined $input->param('overwrite_passwords') ? 1 : 0;
my $update_dateexpiry = $input->param('update_dateexpiry') // "";
- my $return = $Import->import_patrons(
- {
- file => $handle,
- defaults => \%defaults,
- matchpoint => $matchpoint,
- overwrite_cardnumber => scalar $input->param('overwrite_cardnumber'),
- overwrite_passwords => $overwrite_passwords,
- preserve_extended_attributes => scalar $input->param('ext_preserve') || 0,
- preserve_fields => \@preserve_fields,
- update_dateexpiry => $update_dateexpiry ? 1 : 0,
- update_dateexpiry_from_today => $update_dateexpiry eq "now" ? 1 : 0,
- update_dateexpiry_from_existing => $update_dateexpiry eq "dateexpiry" ? 1 : 0,
- send_welcome => $welcome_new,
- }
- );
- my $feedback = $return->{feedback};
- my $errors = $return->{errors};
- my $imported = $return->{imported};
- my $overwritten = $return->{overwritten};
- my $alreadyindb = $return->{already_in_db};
- my $invalid = $return->{invalid};
- my $imported_borrowers = $return->{imported_borrowers};
+ try {
+ my $job_id = Koha::BackgroundJob::PatronImport->new->enqueue(
+ {
+ file_content => $file_content,
+ defaults => \%defaults,
+ matchpoint => $matchpoint,
+ overwrite_cardnumber => scalar $input->param('overwrite_cardnumber'),
+ overwrite_passwords => $overwrite_passwords,
+ preserve_extended_attributes => scalar $input->param('ext_preserve') || 0,
+ preserve_fields => \@preserve_fields,
+ update_dateexpiry => $update_dateexpiry ? 1 : 0,
+ update_dateexpiry_from_today => $update_dateexpiry eq "now" ? 1 : 0,
+ update_dateexpiry_from_existing => $update_dateexpiry eq "dateexpiry" ? 1 : 0,
+ send_welcome => $welcome_new,
+ createpatronlist => $createpatronlist,
+ patronlistname => $patronlistname,
+ }
+ );
- if ( $imported && $createpatronlist ) {
- my $patronlist = AddPatronList( { name => $patronlistname } );
- AddPatronsToList( { list => $patronlist, borrowernumbers => $imported_borrowers } );
- $template->param( 'patronlistname' => $patronlistname );
- }
-
- my $uploadinfo = $input->uploadInfo($uploadborrowers);
- foreach ( keys %$uploadinfo ) {
- push @$feedback, { feedback => 1, name => $_, value => $uploadinfo->{$_}, $_ => $uploadinfo->{$_} };
- }
-
- push @$feedback, { feedback => 1, name => 'filename', value => $uploadborrowers, filename => $uploadborrowers };
-
- $template->param(
- uploadborrowers => 1,
- errors => $errors,
- feedback => $feedback,
- imported => $imported,
- overwritten => $overwritten,
- alreadyindb => $alreadyindb,
- invalid => $invalid,
- total => $imported + $alreadyindb + $invalid + $overwritten,
- );
+ $template->param(
+ op => 'enqueued',
+ job_id => $job_id,
+ );
+ } catch {
+ $template->param(
+ op => 'enqueued',
+ error => 'cannot_enqueue_job',
+ );
+ };
} else {
if ($extended) {