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

(-)a/Koha/Exceptions/Patron/Relationship.pm (-1 / +3 lines)
Lines 32-38 use Exception::Class ( Link Here
32
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => {
32
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => {
33
        isa         => 'Koha::Exceptions::Patron::Relationship',
33
        isa         => 'Koha::Exceptions::Patron::Relationship',
34
        description => 'The specified relationship is invalid',
34
        description => 'The specified relationship is invalid',
35
        fields      => [ 'relationship', 'no_relationship', 'invalid_guarantor' ]
35
        fields      => [ 'relationship', 'no_relationship', 'invalid_guarantor', 'child_guarantor' ]
36
    },
36
    },
37
    'Koha::Exceptions::Patron::Relationship::NoGuarantor' => {
37
    'Koha::Exceptions::Patron::Relationship::NoGuarantor' => {
38
        isa         => 'Koha::Exceptions::Patron::Relationship',
38
        isa         => 'Koha::Exceptions::Patron::Relationship',
Lines 50-55 sub full_message { Link Here
50
            if ( $self->no_relationship ) {
50
            if ( $self->no_relationship ) {
51
                $msg = sprintf("No relationship passed.");
51
                $msg = sprintf("No relationship passed.");
52
            } elsif ( $self->invalid_guarantor ) {
52
            } elsif ( $self->invalid_guarantor ) {
53
                $msg = sprintf("Guarantee patron cannot be a guarantor.");
54
            } elsif ( $self->child_guarantor ) {
53
                $msg = sprintf("Child patron cannot be a guarantor.");
55
                $msg = sprintf("Child patron cannot be a guarantor.");
54
            } else {
56
            } else {
55
                $msg = sprintf( "Invalid relationship passed, '%s' is not defined.", $self->relationship );
57
                $msg = sprintf( "Invalid relationship passed, '%s' is not defined.", $self->relationship );
(-)a/Koha/Patron.pm (-33 / +30 lines)
Lines 196-203 sub store { Link Here
196
    my $self   = shift;
196
    my $self   = shift;
197
    my $params = @_ ? shift : {};
197
    my $params = @_ ? shift : {};
198
198
199
    my $guarantors = $params->{guarantors} // [];
200
201
    $self->_result->result_source->schema->txn_do(
199
    $self->_result->result_source->schema->txn_do(
202
        sub {
200
        sub {
203
            if (
201
            if (
Lines 318-337 sub store { Link Here
318
316
319
                $self->borrowernumber(undef);
317
                $self->borrowernumber(undef);
320
318
321
                if (    C4::Context->preference('ChildNeedsGuarantor')
322
                    and ( $self->is_child or $self->category->can_be_guarantee )
323
                    and $self->contactname eq ""
324
                    and !@$guarantors )
325
                {
326
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
327
                }
328
329
                foreach my $guarantor (@$guarantors) {
330
                    if ( $guarantor->is_child ) {
331
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
332
                    }
333
                }
334
335
                $self = $self->SUPER::store;
319
                $self = $self->SUPER::store;
336
320
337
                $self->add_enrolment_fee_if_needed(0);
321
                $self->add_enrolment_fee_if_needed(0);
Lines 375-397 sub store { Link Here
375
359
376
                }
360
                }
377
361
378
                my @existing_guarantors = $self->guarantor_relationships()->guarantors->as_list;
379
                push @$guarantors, @existing_guarantors;
380
381
                if (    C4::Context->preference('ChildNeedsGuarantor')
382
                    and ( $self->is_child or $self->category->can_be_guarantee )
383
                    and $self->contactname eq ""
384
                    and !@$guarantors )
385
                {
386
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
387
                }
388
389
                foreach my $guarantor (@$guarantors) {
390
                    if ( $guarantor->is_child ) {
391
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
392
                    }
393
                }
394
395
                # Actionlogs
362
                # Actionlogs
396
                if ( C4::Context->preference("BorrowersLog") ) {
363
                if ( C4::Context->preference("BorrowersLog") ) {
397
                    my $info;
364
                    my $info;
Lines 2393-2398 sub _anonymize_column { Link Here
2393
    $self->$col($val);
2360
    $self->$col($val);
2394
}
2361
}
2395
2362
2363
=head3 validate_guarantor
2364
2365
    Koha::Patron->validate_guarantor(\@guarantors, $contactname, $category );
2366
2367
    Validates guarantor patron.
2368
2369
=cut
2370
2371
sub validate_guarantor {
2372
    my ( $self, $guarantors, $contactname, $category ) = @_;
2373
2374
    my $valid_guarantor = @$guarantors ? @$guarantors : $contactname;
2375
2376
    if (    C4::Context->preference('ChildNeedsGuarantor')
2377
        and ( $category->category_type eq 'C' or $category->can_be_guarantee )
2378
        and !$valid_guarantor )
2379
    {
2380
        Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
2381
    }
2382
2383
    foreach my $guarantor (@$guarantors) {
2384
        if ( $guarantor->is_guarantee ) {
2385
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
2386
        } elsif ( $guarantor->is_child ) {
2387
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( child_guarantor => 1 );
2388
        }
2389
    }
2390
2391
}
2392
2396
=head3 add_guarantor
2393
=head3 add_guarantor
2397
2394
2398
    my $relationship = $patron->add_guarantor(
2395
    my $relationship = $patron->add_guarantor(
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt (-1 / +4 lines)
Lines 195-201 Link Here
195
                                    <li id="ERROR_child_is_guarantor">Child patron cannot be a guarantor.</li>
195
                                    <li id="ERROR_child_is_guarantor">Child patron cannot be a guarantor.</li>
196
                                [% END %]
196
                                [% END %]
197
                                [% IF ( ERROR_guarantor_is_guarantee ) %]
197
                                [% IF ( ERROR_guarantor_is_guarantee ) %]
198
                                    <li id="ERROR_guarantor_is_guarantee">A guarantor cannot be a guarantee.</li>
198
                                    <li id="ERROR_guarantor_is_guarantee">A guarantee cannot be a guarantor.</li>
199
                                [% END %]
200
                                [% IF ( ERROR_guarantee_is_guarantor ) %]
201
                                    <li id="ERROR_guarantee_is_guarantor">A guarantor cannot be a guarantee.</li>
199
                                [% END %]
202
                                [% END %]
200
                                [% IF ( ERROR_cannot_delete_guarantor ) %]
203
                                [% IF ( ERROR_cannot_delete_guarantor ) %]
201
                                    <li id="ERROR_cannot_delete_guarantor">Cannot delete guarantor(s). A child patron needs a guarantor.</li>
204
                                    <li id="ERROR_cannot_delete_guarantor">Cannot delete guarantor(s). A child patron needs a guarantor.</li>
(-)a/members/memberentry.pl (-17 / +20 lines)
Lines 284-304 if (@delete_guarantor) { Link Here
284
}
284
}
285
285
286
#Check if guarantor requirements are met
286
#Check if guarantor requirements are met
287
my $valid_guarantor = @guarantors ? @guarantors : $newdata{'contactname'};
287
if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) ) {
288
if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
288
    unless ( $patron && $patron->is_guarantor ) {
289
    && C4::Context->preference('ChildNeedsGuarantor')
289
        try {
290
    && ( $category->category_type eq 'C' || $category->can_be_guarantee )
290
            Koha::Patron->validate_guarantor( \@guarantors, $newdata{'contactname'}, $category );
291
    && !$valid_guarantor )
291
        } catch {
292
{
292
            if ( ref($_) eq "Koha::Exceptions::Patron::Relationship::NoGuarantor" ) {
293
    push @errors, 'ERROR_child_no_guarantor';
293
                push @errors, "ERROR_child_no_guarantor";
294
}
294
            } elsif ( ref($_) eq "Koha::Exceptions::Patron::Relationship::InvalidRelationship" ) {
295
295
                if ( $_->full_message eq "Guarantee patron cannot be a guarantor." ) {
296
foreach my $guarantor (@guarantors) {
296
                    push @errors, "ERROR_guarantor_is_guarantee";
297
    if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
297
                } elsif ( $_->full_message eq "Child patron cannot be a guarantor." ) {
298
        && ( $guarantor->is_child || $guarantor->is_guarantee || ( $patron && $patron->is_guarantor ) ) )
298
                    push @errors, "ERROR_child_is_guarantor";
299
    {
299
                }
300
        push @errors, 'ERROR_child_is_guarantor'     if ( $guarantor->is_child );
300
            }
301
        push @errors, 'ERROR_guarantor_is_guarantee' if ( !$guarantor->is_child );
301
            return;
302
        };
303
    } else {
304
        push @errors, "ERROR_guarantee_is_guarantor";
302
    }
305
    }
303
}
306
}
304
307
Lines 451-457 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
451
        delete $newdata{password2};
454
        delete $newdata{password2};
452
        $success = 1;
455
        $success = 1;
453
        $patron  = try {
456
        $patron  = try {
454
            Koha::Patron->new( \%newdata )->store( { guarantors => \@guarantors } );
457
            Koha::Patron->new( \%newdata )->store();
455
        } catch {
458
        } catch {
456
            $success = 0;
459
            $success = 0;
457
            $nok     = 1;
460
            $nok     = 1;
Lines 560-566 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
560
        delete $newdata{password2};
563
        delete $newdata{password2};
561
564
562
        try {
565
        try {
563
            $patron->set( \%newdata )->store( { guarantors => \@guarantors } ) if scalar( keys %newdata ) > 1;
566
            $patron->set( \%newdata )->store() if scalar( keys %newdata ) > 1;
564
567
565
            # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
568
            # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
566
            $success = 1;
569
            $success = 1;
(-)a/t/db_dependent/Koha/Patron.t (-58 / +34 lines)
Lines 2425-2517 subtest 'get_lists_with_patron() tests' => sub { Link Here
2425
    $schema->storage->txn_rollback;
2425
    $schema->storage->txn_rollback;
2426
};
2426
};
2427
2427
2428
subtest 'guarantor requirements tests' => sub {
2428
subtest 'validate_guarantor() tests' => sub {
2429
2429
2430
    plan tests => 6;
2430
    plan tests => 5;
2431
2431
2432
    $schema->storage->txn_begin;
2432
    $schema->storage->txn_begin;
2433
2433
2434
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2434
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2435
    my $child_category =
2435
    my $guarantee_category =
2436
        $builder->build( { source => 'Category', value => { category_type => 'C', can_be_guarantee => 1 } } )
2436
        $builder->build(
2437
        ->{categorycode};
2437
        { source => 'Category', value => { categorycode => "GUARANTEE", category_type => 'C', can_be_guarantee => 1 } }
2438
    my $patron_category =
2438
        );
2439
        $builder->build( { source => 'Category', value => { category_type => 'A', can_be_guarantee => 0 } } )
2439
    my $guarantor_category =
2440
        ->{categorycode};
2440
        $builder->build(
2441
2441
        { source => 'Category', value => { categorycode => "GUARANTOR", category_type => 'A', can_be_guarantee => 0 } }
2442
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2442
        );
2443
2443
2444
    my $child = Koha::Patron->new(
2444
    my $child = Koha::Patron->new(
2445
        {
2445
        {
2446
            branchcode   => $branchcode,
2446
            branchcode   => $branchcode,
2447
            categorycode => $child_category,
2447
            categorycode => $guarantee_category->{categorycode},
2448
            contactname  => ''
2449
        }
2450
    );
2451
    $child->store();
2452
2453
    ok(
2454
        Koha::Patrons->find( $child->id ),
2455
        'Child patron can be stored without guarantor when ChildNeedsGuarantor is off.'
2456
    );
2457
2458
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2459
2460
    my $child2 = Koha::Patron->new(
2461
        {
2462
            branchcode   => $branchcode,
2463
            categorycode => $child_category,
2464
            contactname  => ''
2448
            contactname  => ''
2465
        }
2449
        }
2466
    );
2450
    );
2467
    my $child3 = $builder->build_object(
2451
    my $child2 = $builder->build_object(
2468
        {
2452
        {
2469
            class => 'Koha::Patrons',
2453
            class => 'Koha::Patrons',
2470
            value => { categorycode => $child_category }
2454
            value => { categorycode => $guarantee_category->{categorycode} }
2471
        }
2455
        }
2472
    );
2456
    );
2473
    my $patron = $builder->build_object(
2457
    my $patron = $builder->build_object(
2474
        {
2458
        {
2475
            class => 'Koha::Patrons',
2459
            class => 'Koha::Patrons',
2476
            value => { categorycode => $patron_category }
2460
            value => { categorycode => $guarantor_category->{categorycode} }
2477
        }
2461
        }
2478
    );
2462
    );
2479
2463
2480
    throws_ok { $child2->store(); }
2464
    my @guarantors;
2465
    my $contactname = "";
2466
    my $category    = Koha::Patron::Categories->find( $guarantee_category->{categorycode} );
2467
2468
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2469
2470
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2471
    'Validation passes when ChildNeedsGuarantor syspref is disabled';
2472
2473
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2474
2475
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2481
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2476
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2482
        'Exception thrown when guarantor is required but not provided.';
2477
        'Exception thrown when guarantor is required but not provided.';
2483
2478
2484
    my @guarantors = ( $patron, $child3 );
2479
    @guarantors = ( $patron, $child2 );
2485
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2480
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2486
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2481
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2487
        'Exception thrown when child patron is added as guarantor.';
2482
        'Exception thrown when child patron is added as guarantor.';
2488
2483
2489
    #test ModMember
2490
    @guarantors = ($patron);
2484
    @guarantors = ($patron);
2491
    $child2->store( { guarantors => \@guarantors } )->discard_changes();
2485
    lives_ok { $child->validate_guarantor( \@guarantors, undef, $category ); }
2492
2486
    'Validation passes when valid guarantors are passed in array';
2493
    t::lib::Mocks::mock_preference( 'borrowerRelationship', '' );
2494
2487
2495
    my $relationship = Koha::Patron::Relationship->new(
2488
    @guarantors  = ();
2496
        {
2489
    $contactname = "Guarantor";
2497
            guarantor_id => $patron->borrowernumber,
2490
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2498
            guarantee_id => $child2->borrowernumber,
2491
    'Validation passes when guarantor contanct name is passed';
2499
            relationship => ''
2500
        }
2501
    );
2502
    $relationship->store();
2503
2504
    ok( $child2->store(), 'Child patron can be modified and stored when guarantor is stored' );
2505
2506
    @guarantors = ($child3);
2507
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2508
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2509
        'Exception thrown when child patron is modified and child patron is added as guarantor.';
2510
2511
    $relationship->delete;
2512
    throws_ok { $child2->store(); }
2513
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2514
        'Exception thrown when guarantor is deleted.';
2515
2492
2516
    $schema->storage->txn_rollback;
2493
    $schema->storage->txn_rollback;
2517
};
2494
};
2518
- 

Return to bug 39014