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 207-214 sub store { Link Here
207
    my $self   = shift;
207
    my $self   = shift;
208
    my $params = @_ ? shift : {};
208
    my $params = @_ ? shift : {};
209
209
210
    my $guarantors = $params->{guarantors} // [];
211
212
    $self->_result->result_source->schema->txn_do(
210
    $self->_result->result_source->schema->txn_do(
213
        sub {
211
        sub {
214
            if (
212
            if (
Lines 329-348 sub store { Link Here
329
327
330
                $self->borrowernumber(undef);
328
                $self->borrowernumber(undef);
331
329
332
                if (    C4::Context->preference('ChildNeedsGuarantor')
333
                    and ( $self->is_child or $self->category->can_be_guarantee )
334
                    and $self->contactname eq ""
335
                    and !@$guarantors )
336
                {
337
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
338
                }
339
340
                foreach my $guarantor (@$guarantors) {
341
                    if ( $guarantor->is_child ) {
342
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
343
                    }
344
                }
345
346
                $self = $self->SUPER::store;
330
                $self = $self->SUPER::store;
347
331
348
                $self->add_enrolment_fee_if_needed(0);
332
                $self->add_enrolment_fee_if_needed(0);
Lines 386-408 sub store { Link Here
386
370
387
                }
371
                }
388
372
389
                my @existing_guarantors = $self->guarantor_relationships()->guarantors->as_list;
390
                push @$guarantors, @existing_guarantors;
391
392
                if (    C4::Context->preference('ChildNeedsGuarantor')
393
                    and ( $self->is_child or $self->category->can_be_guarantee )
394
                    and ( !defined $self->contactname || $self->contactname eq "" )
395
                    and !@$guarantors )
396
                {
397
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
398
                }
399
400
                foreach my $guarantor (@$guarantors) {
401
                    if ( $guarantor->is_child ) {
402
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
403
                    }
404
                }
405
406
                # Actionlogs
373
                # Actionlogs
407
                if ( C4::Context->preference("BorrowersLog") || C4::Context->preference("CardnumberLog") ) {
374
                if ( C4::Context->preference("BorrowersLog") || C4::Context->preference("CardnumberLog") ) {
408
                    my $info;
375
                    my $info;
Lines 2835-2840 sub _anonymize_column { Link Here
2835
    $self->$col($val);
2802
    $self->$col($val);
2836
}
2803
}
2837
2804
2805
=head3 validate_guarantor
2806
2807
    Koha::Patron->validate_guarantor(\@guarantors, $contactname, $category );
2808
2809
    Validates guarantor patron.
2810
2811
=cut
2812
2813
sub validate_guarantor {
2814
    my ( $self, $guarantors, $contactname, $category ) = @_;
2815
2816
    my $valid_guarantor = @$guarantors ? @$guarantors : $contactname;
2817
2818
    if (    C4::Context->preference('ChildNeedsGuarantor')
2819
        and ( $category->category_type eq 'C' or $category->can_be_guarantee )
2820
        and !$valid_guarantor )
2821
    {
2822
        Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
2823
    }
2824
2825
    foreach my $guarantor (@$guarantors) {
2826
        if ( $guarantor->is_guarantee ) {
2827
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
2828
        } elsif ( $guarantor->is_child ) {
2829
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( child_guarantor => 1 );
2830
        }
2831
    }
2832
2833
}
2834
2838
=head3 add_guarantor
2835
=head3 add_guarantor
2839
2836
2840
    my $relationship = $patron->add_guarantor(
2837
    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 281-300 if (@delete_guarantor) { Link Here
281
}
281
}
282
282
283
#Check if guarantor requirements are met
283
#Check if guarantor requirements are met
284
my $valid_guarantor = @guarantors ? @guarantors : $newdata{'contactname'};
284
if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) ) {
285
if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
285
    unless ( $patron && $patron->is_guarantor ) {
286
    && C4::Context->preference('ChildNeedsGuarantor')
286
        try {
287
    && ( $category->category_type eq 'C' || $category->can_be_guarantee )
287
            Koha::Patron->validate_guarantor( \@guarantors, $newdata{'contactname'}, $category );
288
    && !$valid_guarantor )
288
        } catch {
289
{
289
            if ( ref($_) eq "Koha::Exceptions::Patron::Relationship::NoGuarantor" ) {
290
    push @errors, 'ERROR_child_no_guarantor';
290
                push @errors, "ERROR_child_no_guarantor";
291
}
291
            } elsif ( ref($_) eq "Koha::Exceptions::Patron::Relationship::InvalidRelationship" ) {
292
292
                if ( $_->full_message eq "Guarantee patron cannot be a guarantor." ) {
293
foreach my $guarantor (@guarantors) {
293
                    push @errors, "ERROR_guarantor_is_guarantee";
294
    if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
294
                } elsif ( $_->full_message eq "Child patron cannot be a guarantor." ) {
295
        && ( $guarantor->is_child || $guarantor->is_guarantee || ( $patron && $patron->is_guarantor ) ) )
295
                    push @errors, "ERROR_child_is_guarantor";
296
    {
296
                }
297
        push @errors, 'ERROR_guarantor_is_guarantee';
297
            }
298
            return;
299
        };
300
    } else {
301
        push @errors, "ERROR_guarantee_is_guarantor";
298
    }
302
    }
299
}
303
}
300
304
Lines 447-453 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
447
        delete $newdata{password2};
451
        delete $newdata{password2};
448
        $success = 1;
452
        $success = 1;
449
        $patron  = try {
453
        $patron  = try {
450
            Koha::Patron->new( \%newdata )->store( { guarantors => \@guarantors } );
454
            Koha::Patron->new( \%newdata )->store();
451
        } catch {
455
        } catch {
452
            $success = 0;
456
            $success = 0;
453
            $nok     = 1;
457
            $nok     = 1;
Lines 561-567 if ( ( !$nok ) and $nodouble and ( $op eq 'cud-insert' or $op eq 'cud-save' ) ) Link Here
561
        delete $newdata{guarantor_relationship};
565
        delete $newdata{guarantor_relationship};
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 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