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

(-)a/Koha/Patron.pm (-25 / +28 lines)
Lines 317-336 sub store { Link Here
317
317
318
                $self->borrowernumber(undef);
318
                $self->borrowernumber(undef);
319
319
320
                if (    C4::Context->preference('ChildNeedsGuarantor')
321
                    and ( $self->is_child or $self->category->can_be_guarantee )
322
                    and $self->contactname eq ""
323
                    and !@$guarantors )
324
                {
325
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
326
                }
327
328
                foreach my $guarantor (@$guarantors) {
329
                    if ( $guarantor->is_child ) {
330
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
331
                    }
332
                }
333
334
                $self = $self->SUPER::store;
320
                $self = $self->SUPER::store;
335
321
336
                $self->add_enrolment_fee_if_needed(0);
322
                $self->add_enrolment_fee_if_needed(0);
Lines 374-390 sub store { Link Here
374
360
375
                }
361
                }
376
362
377
                my @existing_guarantors = $self->guarantor_relationships()->guarantors->as_list;
378
                push @$guarantors, @existing_guarantors;
379
380
                if (    C4::Context->preference('ChildNeedsGuarantor')
381
                    and ( $self->is_child or $self->category->can_be_guarantee )
382
                    and $self->contactname eq ""
383
                    and !@$guarantors )
384
                {
385
                    Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
386
                }
387
388
                foreach my $guarantor (@$guarantors) {
363
                foreach my $guarantor (@$guarantors) {
389
                    if ( $guarantor->is_child ) {
364
                    if ( $guarantor->is_child ) {
390
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
365
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
Lines 2373-2378 sub _anonymize_column { Link Here
2373
    $self->$col($val);
2348
    $self->$col($val);
2374
}
2349
}
2375
2350
2351
=head3 validate_guarantor
2352
2353
    Koha::Patron->validate_guarantor(\@guarantors, $contactname, $category );
2354
2355
    Validates guarantor patron.
2356
2357
=cut
2358
2359
sub validate_guarantor {
2360
    my ( $self, $guarantors, $contactname, $category ) = @_;
2361
2362
    my $valid_guarantor = @$guarantors ? @$guarantors : $contactname;
2363
2364
    if (    C4::Context->preference('ChildNeedsGuarantor')
2365
        and ( $category->category_type eq 'C' or $category->can_be_guarantee )
2366
        and !$valid_guarantor )
2367
    {
2368
        Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
2369
    }
2370
2371
    foreach my $guarantor (@$guarantors) {
2372
        if ( $guarantor->is_child || $guarantor->is_guarantee ) {
2373
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
2374
        }
2375
    }
2376
2377
}
2378
2376
=head3 add_guarantor
2379
=head3 add_guarantor
2377
2380
2378
    my $relationship = $patron->add_guarantor(
2381
    my $relationship = $patron->add_guarantor(
(-)a/members/memberentry.pl (-15 / +11 lines)
Lines 275-295 if (@delete_guarantor) { Link Here
275
}
275
}
276
276
277
#Check if guarantor requirements are met
277
#Check if guarantor requirements are met
278
my $valid_guarantor = @guarantors ? @guarantors : $newdata{'contactname'};
278
if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) ) {
279
if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
279
    try {
280
    && C4::Context->preference('ChildNeedsGuarantor')
280
        Koha::Patron->validate_guarantor( \@guarantors, $newdata{'contactname'}, $category );
281
    && ( $category->category_type eq 'C' || $category->can_be_guarantee )
281
    } catch {
282
    && !$valid_guarantor )
282
        if ( ref($_) eq 'Koha::Exceptions::Patron::Relationship::NoGuarantor' ) {
283
{
283
            push @errors, "ERROR_child_no_guarantor";
284
    push @errors, 'ERROR_child_no_guarantor';
284
        } elsif ( ref($_) eq 'Koha::Exceptions::Patron::Relationship::InvalidRelationship' ) {
285
}
285
            push @errors, "ERROR_guarantor_is_guarantee";
286
286
        }
287
foreach my $guarantor (@guarantors) {
287
        return;
288
    if (   ( $op eq 'cud-save' || $op eq 'cud-insert' )
288
    };
289
        && ( $guarantor->is_child || $guarantor->is_guarantee || ( $patron && $patron->is_guarantor ) ) )
290
    {
291
        push @errors, 'ERROR_guarantor_is_guarantee';
292
    }
293
}
289
}
294
290
295
my @valid_relationships = split( /\|/, C4::Context->preference('borrowerRelationship'), -1 );
291
my @valid_relationships = split( /\|/, C4::Context->preference('borrowerRelationship'), -1 );
(-)a/t/db_dependent/Koha/Patron.t (-58 / +34 lines)
Lines 2351-2443 subtest 'get_lists_with_patron() tests' => sub { Link Here
2351
    $schema->storage->txn_rollback;
2351
    $schema->storage->txn_rollback;
2352
};
2352
};
2353
2353
2354
subtest 'guarantor requirements tests' => sub {
2354
subtest 'validate_guarantor() tests' => sub {
2355
2355
2356
    plan tests => 6;
2356
    plan tests => 5;
2357
2357
2358
    $schema->storage->txn_begin;
2358
    $schema->storage->txn_begin;
2359
2359
2360
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2360
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
2361
    my $child_category =
2361
    my $guarantee_category =
2362
        $builder->build( { source => 'Category', value => { category_type => 'C', can_be_guarantee => 1 } } )
2362
        $builder->build(
2363
        ->{categorycode};
2363
        { source => 'Category', value => { categorycode => "GUARANTEE", category_type => 'C', can_be_guarantee => 1 } }
2364
    my $patron_category =
2364
        );
2365
        $builder->build( { source => 'Category', value => { category_type => 'A', can_be_guarantee => 0 } } )
2365
    my $guarantor_category =
2366
        ->{categorycode};
2366
        $builder->build(
2367
2367
        { source => 'Category', value => { categorycode => "GUARANTOR", category_type => 'A', can_be_guarantee => 0 } }
2368
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2368
        );
2369
2369
2370
    my $child = Koha::Patron->new(
2370
    my $child = Koha::Patron->new(
2371
        {
2371
        {
2372
            branchcode   => $branchcode,
2372
            branchcode   => $branchcode,
2373
            categorycode => $child_category,
2373
            categorycode => $guarantee_category->{categorycode},
2374
            contactname  => ''
2375
        }
2376
    );
2377
    $child->store();
2378
2379
    ok(
2380
        Koha::Patrons->find( $child->id ),
2381
        'Child patron can be stored without guarantor when ChildNeedsGuarantor is off.'
2382
    );
2383
2384
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2385
2386
    my $child2 = Koha::Patron->new(
2387
        {
2388
            branchcode   => $branchcode,
2389
            categorycode => $child_category,
2390
            contactname  => ''
2374
            contactname  => ''
2391
        }
2375
        }
2392
    );
2376
    );
2393
    my $child3 = $builder->build_object(
2377
    my $child2 = $builder->build_object(
2394
        {
2378
        {
2395
            class => 'Koha::Patrons',
2379
            class => 'Koha::Patrons',
2396
            value => { categorycode => $child_category }
2380
            value => { categorycode => $guarantee_category->{categorycode} }
2397
        }
2381
        }
2398
    );
2382
    );
2399
    my $patron = $builder->build_object(
2383
    my $patron = $builder->build_object(
2400
        {
2384
        {
2401
            class => 'Koha::Patrons',
2385
            class => 'Koha::Patrons',
2402
            value => { categorycode => $patron_category }
2386
            value => { categorycode => $guarantor_category->{categorycode} }
2403
        }
2387
        }
2404
    );
2388
    );
2405
2389
2406
    throws_ok { $child2->store(); }
2390
    my @guarantors;
2391
    my $contactname = "";
2392
    my $category    = Koha::Patron::Categories->find( $guarantee_category->{categorycode} );
2393
2394
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
2395
2396
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2397
    'Validation passes when ChildNeedsGuarantor syspref is disabled';
2398
2399
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
2400
2401
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2407
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2402
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2408
        'Exception thrown when guarantor is required but not provided.';
2403
        'Exception thrown when guarantor is required but not provided.';
2409
2404
2410
    my @guarantors = ( $patron, $child3 );
2405
    @guarantors = ( $patron, $child2 );
2411
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2406
    throws_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2412
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2407
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2413
        'Exception thrown when child patron is added as guarantor.';
2408
        'Exception thrown when child patron is added as guarantor.';
2414
2409
2415
    #test ModMember
2416
    @guarantors = ($patron);
2410
    @guarantors = ($patron);
2417
    $child2->store( { guarantors => \@guarantors } )->discard_changes();
2411
    lives_ok { $child->validate_guarantor( \@guarantors, undef, $category ); }
2418
2412
    'Validation passes when valid guarantors are passed in array';
2419
    t::lib::Mocks::mock_preference( 'borrowerRelationship', '' );
2420
2413
2421
    my $relationship = Koha::Patron::Relationship->new(
2414
    @guarantors = ();
2422
        {
2415
    $contactname = "Guarantor";
2423
            guarantor_id => $patron->borrowernumber,
2416
    lives_ok { $child->validate_guarantor( \@guarantors, $contactname, $category ); }
2424
            guarantee_id => $child2->borrowernumber,
2417
    'Validation passes when guarantor contanct name is passed';
2425
            relationship => ''
2426
        }
2427
    );
2428
    $relationship->store();
2429
2430
    ok( $child2->store(), 'Child patron can be modified and stored when guarantor is stored' );
2431
2432
    @guarantors = ($child3);
2433
    throws_ok { $child2->store( { guarantors => \@guarantors } ); }
2434
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
2435
        'Exception thrown when child patron is modified and child patron is added as guarantor.';
2436
2437
    $relationship->delete;
2438
    throws_ok { $child2->store(); }
2439
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
2440
        'Exception thrown when guarantor is deleted.';
2441
2418
2442
    $schema->storage->txn_rollback;
2419
    $schema->storage->txn_rollback;
2443
};
2420
};
2444
- 

Return to bug 39014