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 197-204 sub store { Link Here
197
    my $self   = shift;
197
    my $self   = shift;
198
    my $params = @_ ? shift : {};
198
    my $params = @_ ? shift : {};
199
199
200
    my $guarantors = $params->{guarantors} // [];
201
202
    $self->_result->result_source->schema->txn_do(
200
    $self->_result->result_source->schema->txn_do(
203
        sub {
201
        sub {
204
            if (
202
            if (
Lines 319-338 sub store { Link Here
319
317
320
                $self->borrowernumber(undef);
318
                $self->borrowernumber(undef);
321
319
322
                if (    C4::Context->preference('ChildNeedsGuarantor')
323
                    and ( $self->is_child or $self->category->can_be_guarantee )
324
                    and $self->contactname eq ""
325
                    and !@$guarantors )
326
                {
327
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
328
                }
329
330
                foreach my $guarantor (@$guarantors) {
331
                    if ( $guarantor->is_child ) {
332
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
333
                    }
334
                }
335
336
                $self = $self->SUPER::store;
320
                $self = $self->SUPER::store;
337
321
338
                $self->add_enrolment_fee_if_needed(0);
322
                $self->add_enrolment_fee_if_needed(0);
Lines 376-398 sub store { Link Here
376
360
377
                }
361
                }
378
362
379
                my @existing_guarantors = $self->guarantor_relationships()->guarantors->as_list;
380
                push @$guarantors, @existing_guarantors;
381
382
                if (    C4::Context->preference('ChildNeedsGuarantor')
383
                    and ( $self->is_child or $self->category->can_be_guarantee )
384
                    and $self->contactname eq ""
385
                    and !@$guarantors )
386
                {
387
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
388
                }
389
390
                foreach my $guarantor (@$guarantors) {
391
                    if ( $guarantor->is_child ) {
392
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
393
                    }
394
                }
395
396
                # Actionlogs
363
                # Actionlogs
397
                if ( C4::Context->preference("BorrowersLog") ) {
364
                if ( C4::Context->preference("BorrowersLog") ) {
398
                    my $info;
365
                    my $info;
Lines 2498-2503 sub _anonymize_column { Link Here
2498
    $self->$col($val);
2465
    $self->$col($val);
2499
}
2466
}
2500
2467
2468
=head3 validate_guarantor
2469
2470
    Koha::Patron->validate_guarantor(\@guarantors, $contactname, $category );
2471
2472
    Validates guarantor patron.
2473
2474
=cut
2475
2476
sub validate_guarantor {
2477
    my ( $self, $guarantors, $contactname, $category ) = @_;
2478
2479
    my $valid_guarantor = @$guarantors ? @$guarantors : $contactname;
2480
2481
    if (    C4::Context->preference('ChildNeedsGuarantor')
2482
        and ( $category->category_type eq 'C' or $category->can_be_guarantee )
2483
        and !$valid_guarantor )
2484
    {
2485
        Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
2486
    }
2487
2488
    foreach my $guarantor (@$guarantors) {
2489
        if ( $guarantor->is_guarantee ) {
2490
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
2491
        } elsif ( $guarantor->is_child ) {
2492
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( child_guarantor => 1 );
2493
        }
2494
    }
2495
2496
}
2497
2501
=head3 add_guarantor
2498
=head3 add_guarantor
2502
2499
2503
    my $relationship = $patron->add_guarantor(
2500
    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 (-16 / +20 lines)
Lines 286-305 if (@delete_guarantor) { Link Here
286
}
286
}
287
287
288
#Check if guarantor requirements are met
288
#Check if guarantor requirements are met
289
my $valid_guarantor = @guarantors ? @guarantors : $newdata{'contactname'};
289
if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) ) {
290
if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
290
    unless ( $patron && $patron->is_guarantor ) {
291
    && C4::Context->preference('ChildNeedsGuarantor')
291
        try {
292
    && ( $category->category_type eq 'C' || $category->can_be_guarantee )
292
            Koha::Patron->validate_guarantor( \@guarantors, $newdata{'contactname'}, $category );
293
    && !$valid_guarantor )
293
        } catch {
294
{
294
            if ( ref($_) eq "Koha::Exceptions::Patron::Relationship::NoGuarantor" ) {
295
    push @errors, 'ERROR_child_no_guarantor';
295
                push @errors, "ERROR_child_no_guarantor";
296
}
296
            } elsif ( ref($_) eq "Koha::Exceptions::Patron::Relationship::InvalidRelationship" ) {
297
297
                if ( $_->full_message eq "Guarantee patron cannot be a guarantor." ) {
298
foreach my $guarantor (@guarantors) {
298
                    push @errors, "ERROR_guarantor_is_guarantee";
299
    if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
299
                } elsif ( $_->full_message eq "Child patron cannot be a guarantor." ) {
300
        && ( $guarantor->is_child || $guarantor->is_guarantee || ( $patron && $patron->is_guarantor ) ) )
300
                    push @errors, "ERROR_child_is_guarantor";
301
    {
301
                }
302
        push @errors, 'ERROR_guarantor_is_guarantee';
302
            }
303
            return;
304
        };
305
    } else {
306
        push @errors, "ERROR_guarantee_is_guarantor";
303
    }
307
    }
304
}
308
}
305
309
Lines 452-458 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
452
        delete $newdata{password2};
456
        delete $newdata{password2};
453
        $success = 1;
457
        $success = 1;
454
        $patron  = try {
458
        $patron  = try {
455
            Koha::Patron->new( \%newdata )->store( { guarantors => \@guarantors } );
459
            Koha::Patron->new( \%newdata )->store();
456
        } catch {
460
        } catch {
457
            $success = 0;
461
            $success = 0;
458
            $nok     = 1;
462
            $nok     = 1;
Lines 561-567 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
561
        delete $newdata{password2};
565
        delete $newdata{password2};
562
566
563
        try {
567
        try {
564
            $patron->set( \%newdata )->store( { guarantors => \@guarantors } ) if scalar( keys %newdata ) > 1;
568
            $patron->set( \%newdata )->store() if scalar( keys %newdata ) > 1;
565
569
566
            # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
570
            # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
567
            $success = 1;
571
            $success = 1;
(-)a/t/db_dependent/Koha/Patron.t (-58 / +34 lines)
Lines 2429-2521 subtest 'get_lists_with_patron() tests' => sub { Link Here
2429
    $schema->storage->txn_rollback;
2429
    $schema->storage->txn_rollback;
2430
};
2430
};
2431
2431
2432
subtest 'guarantor requirements tests' => sub {
2432
subtest 'validate_guarantor() tests' => sub {
2433
2433
2434
    plan tests => 6;
2434
    plan tests => 5;
2435
2435
2436
    $schema->storage->txn_begin;
2436
    $schema->storage->txn_begin;
2437
2437
2438
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2438
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2439
    my $child_category =
2439
    my $guarantee_category =
2440
        $builder->build( { source => 'Category', value => { category_type => 'C', can_be_guarantee => 1 } } )
2440
        $builder->build(
2441
        ->{categorycode};
2441
        { source => 'Category', value => { categorycode => "GUARANTEE", category_type => 'C', can_be_guarantee => 1 } }
2442
    my $patron_category =
2442
        );
2443
        $builder->build( { source => 'Category', value => { category_type => 'A', can_be_guarantee => 0 } } )
2443
    my $guarantor_category =
2444
        ->{categorycode};
2444
        $builder->build(
2445
2445
        { source => 'Category', value => { categorycode => "GUARANTOR", category_type => 'A', can_be_guarantee => 0 } }
2446
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2446
        );
2447
2447
2448
    my $child = Koha::Patron->new(
2448
    my $child = Koha::Patron->new(
2449
        {
2449
        {
2450
            branchcode   => $branchcode,
2450
            branchcode   => $branchcode,
2451
            categorycode => $child_category,
2451
            categorycode => $guarantee_category->{categorycode},
2452
            contactname  => ''
2453
        }
2454
    );
2455
    $child->store();
2456
2457
    ok(
2458
        Koha::Patrons->find( $child->id ),
2459
        'Child patron can be stored without guarantor when ChildNeedsGuarantor is off.'
2460
    );
2461
2462
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2463
2464
    my $child2 = Koha::Patron->new(
2465
        {
2466
            branchcode   => $branchcode,
2467
            categorycode => $child_category,
2468
            contactname  => ''
2452
            contactname  => ''
2469
        }
2453
        }
2470
    );
2454
    );
2471
    my $child3 = $builder->build_object(
2455
    my $child2 = $builder->build_object(
2472
        {
2456
        {
2473
            class => 'Koha::Patrons',
2457
            class => 'Koha::Patrons',
2474
            value => { categorycode => $child_category }
2458
            value => { categorycode => $guarantee_category->{categorycode} }
2475
        }
2459
        }
2476
    );
2460
    );
2477
    my $patron = $builder->build_object(
2461
    my $patron = $builder->build_object(
2478
        {
2462
        {
2479
            class => 'Koha::Patrons',
2463
            class => 'Koha::Patrons',
2480
            value => { categorycode => $patron_category }
2464
            value => { categorycode => $guarantor_category->{categorycode} }
2481
        }
2465
        }
2482
    );
2466
    );
2483
2467
2484
    throws_ok { $child2->store(); }
2468
    my @guarantors;
2469
    my $contactname = "";
2470
    my $category    = Koha::Patron::Categories->find( $guarantee_category->{categorycode} );
2471
2472
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2473
2474
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2475
    'Validation passes when ChildNeedsGuarantor syspref is disabled';
2476
2477
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2478
2479
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2485
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2480
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2486
        'Exception thrown when guarantor is required but not provided.';
2481
        'Exception thrown when guarantor is required but not provided.';
2487
2482
2488
    my @guarantors = ( $patron, $child3 );
2483
    @guarantors = ( $patron, $child2 );
2489
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2484
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2490
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2485
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2491
        'Exception thrown when child patron is added as guarantor.';
2486
        'Exception thrown when child patron is added as guarantor.';
2492
2487
2493
    #test ModMember
2494
    @guarantors = ($patron);
2488
    @guarantors = ($patron);
2495
    $child2->store( { guarantors => \@guarantors } )->discard_changes();
2489
    lives_ok { $child->validate_guarantor( \@guarantors, undef, $category ); }
2496
2490
    'Validation passes when valid guarantors are passed in array';
2497
    t::lib::Mocks::mock_preference( 'borrowerRelationship', '' );
2498
2491
2499
    my $relationship = Koha::Patron::Relationship->new(
2492
    @guarantors  = ();
2500
        {
2493
    $contactname = "Guarantor";
2501
            guarantor_id => $patron->borrowernumber,
2494
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2502
            guarantee_id => $child2->borrowernumber,
2495
    'Validation passes when guarantor contanct name is passed';
2503
            relationship => ''
2504
        }
2505
    );
2506
    $relationship->store();
2507
2508
    ok( $child2->store(), 'Child patron can be modified and stored when guarantor is stored' );
2509
2510
    @guarantors = ($child3);
2511
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2512
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2513
        'Exception thrown when child patron is modified and child patron is added as guarantor.';
2514
2515
    $relationship->delete;
2516
    throws_ok { $child2->store(); }
2517
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2518
        'Exception thrown when guarantor is deleted.';
2519
2496
2520
    $schema->storage->txn_rollback;
2497
    $schema->storage->txn_rollback;
2521
};
2498
};
2522
- 

Return to bug 39014