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 205-212 sub store { Link Here
205
    my $self   = shift;
205
    my $self   = shift;
206
    my $params = @_ ? shift : {};
206
    my $params = @_ ? shift : {};
207
207
208
    my $guarantors = $params->{guarantors} // [];
209
210
    $self->_result->result_source->schema->txn_do(
208
    $self->_result->result_source->schema->txn_do(
211
        sub {
209
        sub {
212
            if (
210
            if (
Lines 327-346 sub store { Link Here
327
325
328
                $self->borrowernumber(undef);
326
                $self->borrowernumber(undef);
329
327
330
                if (    C4::Context->preference('ChildNeedsGuarantor')
331
                    and ( $self->is_child or $self->category->can_be_guarantee )
332
                    and $self->contactname eq ""
333
                    and !@$guarantors )
334
                {
335
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
336
                }
337
338
                foreach my $guarantor (@$guarantors) {
339
                    if ( $guarantor->is_child ) {
340
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
341
                    }
342
                }
343
344
                $self = $self->SUPER::store;
328
                $self = $self->SUPER::store;
345
329
346
                $self->add_enrolment_fee_if_needed(0);
330
                $self->add_enrolment_fee_if_needed(0);
Lines 384-406 sub store { Link Here
384
368
385
                }
369
                }
386
370
387
                my @existing_guarantors = $self->guarantor_relationships()->guarantors->as_list;
388
                push @$guarantors, @existing_guarantors;
389
390
                if (    C4::Context->preference('ChildNeedsGuarantor')
391
                    and ( $self->is_child or $self->category->can_be_guarantee )
392
                    and ( !defined $self->contactname || $self->contactname eq "" )
393
                    and !@$guarantors )
394
                {
395
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
396
                }
397
398
                foreach my $guarantor (@$guarantors) {
399
                    if ( $guarantor->is_child ) {
400
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
401
                    }
402
                }
403
404
                # Actionlogs
371
                # Actionlogs
405
                if ( C4::Context->preference("BorrowersLog") || C4::Context->preference("CardnumberLog") ) {
372
                if ( C4::Context->preference("BorrowersLog") || C4::Context->preference("CardnumberLog") ) {
406
                    my $info;
373
                    my $info;
Lines 2741-2746 sub _anonymize_column { Link Here
2741
    $self->$col($val);
2708
    $self->$col($val);
2742
}
2709
}
2743
2710
2711
=head3 validate_guarantor
2712
2713
    Koha::Patron->validate_guarantor(\@guarantors, $contactname, $category );
2714
2715
    Validates guarantor patron.
2716
2717
=cut
2718
2719
sub validate_guarantor {
2720
    my ( $self, $guarantors, $contactname, $category ) = @_;
2721
2722
    my $valid_guarantor = @$guarantors ? @$guarantors : $contactname;
2723
2724
    if (    C4::Context->preference('ChildNeedsGuarantor')
2725
        and ( $category->category_type eq 'C' or $category->can_be_guarantee )
2726
        and !$valid_guarantor )
2727
    {
2728
        Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
2729
    }
2730
2731
    foreach my $guarantor (@$guarantors) {
2732
        if ( $guarantor->is_guarantee ) {
2733
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
2734
        } elsif ( $guarantor->is_child ) {
2735
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( child_guarantor => 1 );
2736
        }
2737
    }
2738
2739
}
2740
2744
=head3 add_guarantor
2741
=head3 add_guarantor
2745
2742
2746
    my $relationship = $patron->add_guarantor(
2743
    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 566-572 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
566
        delete $newdata{guarantor_relationship};
570
        delete $newdata{guarantor_relationship};
567
571
568
        try {
572
        try {
569
            $patron->set( \%newdata )->store( { guarantors => \@guarantors } ) if scalar( keys %newdata ) > 1;
573
            $patron->set( \%newdata )->store() if scalar( keys %newdata ) > 1;
570
574
571
            # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
575
            # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
572
            $success = 1;
576
            $success = 1;
(-)a/t/db_dependent/Koha/Patron.t (-58 / +34 lines)
Lines 2624-2716 subtest 'get_lists_with_patron() tests' => sub { Link Here
2624
    $schema->storage->txn_rollback;
2624
    $schema->storage->txn_rollback;
2625
};
2625
};
2626
2626
2627
subtest 'guarantor requirements tests' => sub {
2627
subtest 'validate_guarantor() tests' => sub {
2628
2628
2629
    plan tests => 6;
2629
    plan tests => 5;
2630
2630
2631
    $schema->storage->txn_begin;
2631
    $schema->storage->txn_begin;
2632
2632
2633
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2633
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2634
    my $child_category =
2634
    my $guarantee_category =
2635
        $builder->build( { source => 'Category', value => { category_type => 'C', can_be_guarantee => 1 } } )
2635
        $builder->build(
2636
        ->{categorycode};
2636
        { source => 'Category', value => { categorycode => "GUARANTEE", category_type => 'C', can_be_guarantee => 1 } }
2637
    my $patron_category =
2637
        );
2638
        $builder->build( { source => 'Category', value => { category_type => 'A', can_be_guarantee => 0 } } )
2638
    my $guarantor_category =
2639
        ->{categorycode};
2639
        $builder->build(
2640
2640
        { source => 'Category', value => { categorycode => "GUARANTOR", category_type => 'A', can_be_guarantee => 0 } }
2641
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2641
        );
2642
2642
2643
    my $child = Koha::Patron->new(
2643
    my $child = Koha::Patron->new(
2644
        {
2644
        {
2645
            branchcode   => $branchcode,
2645
            branchcode   => $branchcode,
2646
            categorycode => $child_category,
2646
            categorycode => $guarantee_category->{categorycode},
2647
            contactname  => ''
2648
        }
2649
    );
2650
    $child->store();
2651
2652
    ok(
2653
        Koha::Patrons->find( $child->id ),
2654
        'Child patron can be stored without guarantor when ChildNeedsGuarantor is off.'
2655
    );
2656
2657
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2658
2659
    my $child2 = Koha::Patron->new(
2660
        {
2661
            branchcode   => $branchcode,
2662
            categorycode => $child_category,
2663
            contactname  => ''
2647
            contactname  => ''
2664
        }
2648
        }
2665
    );
2649
    );
2666
    my $child3 = $builder->build_object(
2650
    my $child2 = $builder->build_object(
2667
        {
2651
        {
2668
            class => 'Koha::Patrons',
2652
            class => 'Koha::Patrons',
2669
            value => { categorycode => $child_category }
2653
            value => { categorycode => $guarantee_category->{categorycode} }
2670
        }
2654
        }
2671
    );
2655
    );
2672
    my $patron = $builder->build_object(
2656
    my $patron = $builder->build_object(
2673
        {
2657
        {
2674
            class => 'Koha::Patrons',
2658
            class => 'Koha::Patrons',
2675
            value => { categorycode => $patron_category }
2659
            value => { categorycode => $guarantor_category->{categorycode} }
2676
        }
2660
        }
2677
    );
2661
    );
2678
2662
2679
    throws_ok { $child2->store(); }
2663
    my @guarantors;
2664
    my $contactname = "";
2665
    my $category    = Koha::Patron::Categories->find( $guarantee_category->{categorycode} );
2666
2667
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2668
2669
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2670
    'Validation passes when ChildNeedsGuarantor syspref is disabled';
2671
2672
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2673
2674
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2680
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2675
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2681
        'Exception thrown when guarantor is required but not provided.';
2676
        'Exception thrown when guarantor is required but not provided.';
2682
2677
2683
    my @guarantors = ( $patron, $child3 );
2678
    @guarantors = ( $patron, $child2 );
2684
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2679
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2685
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2680
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2686
        'Exception thrown when child patron is added as guarantor.';
2681
        'Exception thrown when child patron is added as guarantor.';
2687
2682
2688
    #test ModMember
2689
    @guarantors = ($patron);
2683
    @guarantors = ($patron);
2690
    $child2->store( { guarantors => \@guarantors } )->discard_changes();
2684
    lives_ok { $child->validate_guarantor( \@guarantors, undef, $category ); }
2691
2685
    'Validation passes when valid guarantors are passed in array';
2692
    t::lib::Mocks::mock_preference( 'borrowerRelationship', '' );
2693
2686
2694
    my $relationship = Koha::Patron::Relationship->new(
2687
    @guarantors  = ();
2695
        {
2688
    $contactname = "Guarantor";
2696
            guarantor_id => $patron->borrowernumber,
2689
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2697
            guarantee_id => $child2->borrowernumber,
2690
    'Validation passes when guarantor contanct name is passed';
2698
            relationship => ''
2699
        }
2700
    );
2701
    $relationship->store();
2702
2703
    ok( $child2->store(), 'Child patron can be modified and stored when guarantor is stored' );
2704
2705
    @guarantors = ($child3);
2706
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2707
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2708
        'Exception thrown when child patron is modified and child patron is added as guarantor.';
2709
2710
    $relationship->delete;
2711
    throws_ok { $child2->store(); }
2712
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2713
        'Exception thrown when guarantor is deleted.';
2714
2691
2715
    $schema->storage->txn_rollback;
2692
    $schema->storage->txn_rollback;
2716
};
2693
};
2717
- 

Return to bug 39014