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

(-)a/Koha/Patron.pm (-33 / +28 lines)
Lines 195-202 sub store { Link Here
195
    my $self   = shift;
195
    my $self   = shift;
196
    my $params = @_ ? shift : {};
196
    my $params = @_ ? shift : {};
197
197
198
    my $guarantors = $params->{guarantors} // [];
199
200
    $self->_result->result_source->schema->txn_do(
198
    $self->_result->result_source->schema->txn_do(
201
        sub {
199
        sub {
202
            if (
200
            if (
Lines 317-336 sub store { Link Here
317
315
318
                $self->borrowernumber(undef);
316
                $self->borrowernumber(undef);
319
317
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;
318
                $self = $self->SUPER::store;
335
319
336
                $self->add_enrolment_fee_if_needed(0);
320
                $self->add_enrolment_fee_if_needed(0);
Lines 374-396 sub store { Link Here
374
358
375
                }
359
                }
376
360
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) {
389
                    if ( $guarantor->is_child ) {
390
                        Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
391
                    }
392
                }
393
394
                # Actionlogs
361
                # Actionlogs
395
                if ( C4::Context->preference("BorrowersLog") ) {
362
                if ( C4::Context->preference("BorrowersLog") ) {
396
                    my $info;
363
                    my $info;
Lines 2373-2378 sub _anonymize_column { Link Here
2373
    $self->$col($val);
2340
    $self->$col($val);
2374
}
2341
}
2375
2342
2343
=head3 validate_guarantor
2344
2345
    Koha::Patron->validate_guarantor(\@guarantors, $contactname, $category );
2346
2347
    Validates guarantor patron.
2348
2349
=cut
2350
2351
sub validate_guarantor {
2352
    my ( $self, $guarantors, $contactname, $category ) = @_;
2353
2354
    my $valid_guarantor = @$guarantors ? @$guarantors : $contactname;
2355
2356
    if (    C4::Context->preference('ChildNeedsGuarantor')
2357
        and ( $category->category_type eq 'C' or $category->can_be_guarantee )
2358
        and !$valid_guarantor )
2359
    {
2360
        Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
2361
    }
2362
2363
    foreach my $guarantor (@$guarantors) {
2364
        if ( $guarantor->is_child || $guarantor->is_guarantee ) {
2365
            Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 );
2366
        }
2367
    }
2368
2369
}
2370
2376
=head3 add_guarantor
2371
=head3 add_guarantor
2377
2372
2378
    my $relationship = $patron->add_guarantor(
2373
    my $relationship = $patron->add_guarantor(
(-)a/members/memberentry.pl (-17 / +13 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 );
Lines 440-446 if ((!$nok) and $nodouble and ($op eq 'cud-insert' or $op eq 'cud-save')){ Link Here
440
        delete $newdata{password2};
436
        delete $newdata{password2};
441
        $success = 1;
437
        $success = 1;
442
        $patron = try {
438
        $patron = try {
443
            Koha::Patron->new( \%newdata )->store( { guarantors => \@guarantors } );
439
            Koha::Patron->new( \%newdata )->store();
444
        } catch {
440
        } catch {
445
            $success = 0;
441
            $success = 0;
446
            $nok = 1;
442
            $nok = 1;
Lines 537-543 if ((!$nok) and $nodouble and ($op eq 'cud-insert' or $op eq 'cud-save')){ Link Here
537
        delete $newdata{password2};
533
        delete $newdata{password2};
538
534
539
        try {
535
        try {
540
            $patron->set( \%newdata )->store( { guarantors => \@guarantors } ) if scalar( keys %newdata ) > 1;
536
            $patron->set( \%newdata )->store() if scalar( keys %newdata ) > 1;
541
                # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
537
                # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
542
            $success = 1;
538
            $success = 1;
543
        } catch {
539
        } catch {
(-)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