View | Details | Raw Unified | Return to bug 28220
Collapse All | Expand All

(-)a/Koha/Patrons/Import.pm (-40 / +44 lines)
Lines 331-337 sub import_patrons { Link Here
331
            }
331
            }
332
            if ($extended) {
332
            if ($extended) {
333
                if ($ext_preserve) {
333
                if ($ext_preserve) {
334
                    $patron_attributes = $patron->extended_attributes->merge_with( $patron_attributes );
334
                    $patron_attributes = $patron->extended_attributes->merge_and_replace_with( $patron_attributes );
335
                }
335
                }
336
                eval {
336
                eval {
337
                    # We do not want to filter by branch, maybe we should?
337
                    # We do not want to filter by branch, maybe we should?
Lines 354-404 sub import_patrons { Link Here
354
            );
354
            );
355
        }
355
        }
356
        else {
356
        else {
357
            my $patron = eval {
357
            try {
358
                Koha::Patron->new(\%borrower)->store;
358
                $schema->storage->txn_do(sub {
359
            };
359
                    my $patron = Koha::Patron->new(\%borrower)->store;
360
            unless ( $@ ) {
360
                    $borrowernumber = $patron->id;
361
                $borrowernumber = $patron->id;
362
361
363
                if ( $patron->is_debarred ) {
362
                    if ( $patron->is_debarred ) {
364
                    AddDebarment(
363
                        AddDebarment(
365
                        {
364
                            {
366
                            borrowernumber => $patron->borrowernumber,
365
                                borrowernumber => $patron->borrowernumber,
367
                            expiration     => $patron->debarred,
366
                                expiration     => $patron->debarred,
368
                            comment        => $patron->debarredcomment,
367
                                comment        => $patron->debarredcomment,
369
                        }
368
                            }
370
                    );
369
                        );
371
                }
370
                    }
372
371
373
                if ($extended) {
372
                    if ($extended) {
374
                    # FIXME Hum, we did not filter earlier and now we do?
373
                        # FIXME Hum, we did not filter earlier and now we do?
375
                    $patron->extended_attributes->filter_by_branch_limitations->delete;
374
                        $patron->extended_attributes->filter_by_branch_limitations->delete;
376
                    $patron->extended_attributes($patron_attributes);
375
                        $patron->extended_attributes($patron_attributes);
377
                }
376
                    }
378
377
379
                if ($set_messaging_prefs) {
378
                    if ($set_messaging_prefs) {
380
                    C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
379
                        C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
380
                            {
381
                                borrowernumber => $patron->borrowernumber,
382
                                categorycode   => $patron->categorycode,
383
                            }
384
                        );
385
                    }
386
387
                    $imported++;
388
                    push @imported_borrowers, $patron->borrowernumber; #for patronlist
389
                    push(
390
                        @feedback,
381
                        {
391
                        {
382
                            borrowernumber => $patron->borrowernumber,
392
                            feedback => 1,
383
                            categorycode   => $patron->categorycode,
393
                            name     => 'lastimported',
394
                            value    => $patron->surname . ' / ' . $patron->borrowernumber,
384
                        }
395
                        }
385
                    );
396
                    );
386
                }
397
                });
387
398
            } catch {
388
                $imported++;
389
                push @imported_borrowers, $patron->borrowernumber; #for patronlist
390
                push(
391
                    @feedback,
392
                    {
393
                        feedback => 1,
394
                        name     => 'lastimported',
395
                        value    => $patron->surname . ' / ' . $patron->borrowernumber,
396
                    }
397
                );
398
            }
399
            else {
400
                $invalid++;
399
                $invalid++;
401
                push @errors, { unknown_error => 1 };
400
                if ( $_->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint') ) {
401
                    my $patron_id = defined $matchpoint ? $borrower{$matchpoint} : $matchpoint_attr_type;
402
                    push @errors, { patron_attribute_unique_id_constraint => 1, patron_id => $patron_id, attribute => $_->attribute };
403
                } else {
404
                    push @errors, { unknown_error => 1 };
405
                }
402
                push(
406
                push(
403
                    @errors,
407
                    @errors,
404
                    {
408
                    {
Lines 406-412 sub import_patrons { Link Here
406
                        value => $borrower{'surname'} . ' / Create patron',
410
                        value => $borrower{'surname'} . ' / Create patron',
407
                    }
411
                    }
408
                );
412
                );
409
            }
413
            };
410
        }
414
        }
411
415
412
        # Add a guarantor if we are given a relationship
416
        # Add a guarantor if we are given a relationship
(-)a/t/db_dependent/Koha/Patrons/Import.t (-9 / +11 lines)
Lines 845-851 subtest 'test_format_dates' => sub { Link Here
845
845
846
subtest 'patron_attributes' => sub {
846
subtest 'patron_attributes' => sub {
847
847
848
    plan tests => 4;
848
    plan tests => 6;
849
849
850
    t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
850
    t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
851
851
Lines 875-880 subtest 'patron_attributes' => sub { Link Here
875
    my $non_existent_attribute_type_code = $non_existent_attribute_type->code;
875
    my $non_existent_attribute_type_code = $non_existent_attribute_type->code;
876
    $non_existent_attribute_type->delete;
876
    $non_existent_attribute_type->delete;
877
877
878
    our $cardnumber = "1042";
879
878
    # attributes is { code => \@attributes }
880
    # attributes is { code => \@attributes }
879
    sub build_csv {
881
    sub build_csv {
880
        my ($attributes) = @_;
882
        my ($attributes) = @_;
Lines 882-888 subtest 'patron_attributes' => sub { Link Here
882
        my $csv_headers = 'cardnumber,surname,firstname,branchcode,categorycode,patron_attributes';
884
        my $csv_headers = 'cardnumber,surname,firstname,branchcode,categorycode,patron_attributes';
883
        my @attributes_str = map { my $code = $_; map {  sprintf "%s:%s", $code, $_ } @{ $attributes->{$code} } } keys %$attributes;
885
        my @attributes_str = map { my $code = $_; map {  sprintf "%s:%s", $code, $_ } @{ $attributes->{$code} } } keys %$attributes;
884
        my $attributes_str = join ',', @attributes_str;
886
        my $attributes_str = join ',', @attributes_str;
885
        my $csv_line = sprintf '1042,John,D,MPL,PT,"%s"', $attributes_str;
887
        my $csv_line = sprintf '%s,John,D,MPL,PT,"%s"', $cardnumber, $attributes_str;
886
        my $filename = make_csv( $temp_dir, $csv_headers, $csv_line );
888
        my $filename = make_csv( $temp_dir, $csv_headers, $csv_line );
887
        open( my $fh, "<:encoding(utf8)", $filename ) or die "cannot open $filename: $!";
889
        open( my $fh, "<:encoding(utf8)", $filename ) or die "cannot open $filename: $!";
888
        return $fh;
890
        return $fh;
Lines 899-905 subtest 'patron_attributes' => sub { Link Here
899
901
900
        is( $result->{imported}, 1 );
902
        is( $result->{imported}, 1 );
901
903
902
        my $patron = Koha::Patrons->find({cardnumber => "1042"});
904
        my $patron = Koha::Patrons->find({cardnumber => $cardnumber});
903
        compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes } );
905
        compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes } );
904
        $patron->delete;
906
        $patron->delete;
905
    }
907
    }
Lines 918-929 subtest 'patron_attributes' => sub { Link Here
918
        };
920
        };
919
        my $fh = build_csv({ %$attributes });
921
        my $fh = build_csv({ %$attributes });
920
922
921
        throws_ok {
923
        my $result = $patrons_import->import_patrons({file => $fh, matchpoint => 'cardnumber'});
922
            $patrons_import->import_patrons({file => $fh});
924
        my $error = $result->{errors}->[0];
923
        }
925
        is( $error->{patron_attribute_unique_id_constraint}, 1 );
924
        'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint';
926
        is( $error->{patron_id}, $cardnumber );
927
        is( $error->{attribute}->code, $unique_attribute_type->code );
925
928
926
        my $patron = Koha::Patrons->find({cardnumber => "1042"});
929
        my $patron = Koha::Patrons->find({cardnumber => $cardnumber});
927
        is( $patron, undef );
930
        is( $patron, undef );
928
931
929
    }
932
    }
930
- 

Return to bug 28220