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

(-)a/Koha/Exceptions/Patron/Relationship.pm (-3 / +10 lines)
Lines 30-37 use Exception::Class ( Link Here
30
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => {
30
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship' => {
31
        isa         => 'Koha::Exceptions::Patron::Relationship',
31
        isa         => 'Koha::Exceptions::Patron::Relationship',
32
        description => 'The specified relationship is invalid',
32
        description => 'The specified relationship is invalid',
33
        fields      =>  ['relationship','no_relationship']
33
        fields      =>  ['relationship','no_relationship','invalid_guarantor']
34
    }
34
    },
35
    'Koha::Exceptions::Patron::Relationship::NoGuarantor' => {
36
        isa         => 'Koha::Exceptions::Patron::Relationship',
37
        description => 'Child patron needs a guarantor',
38
    },
35
);
39
);
36
40
37
sub full_message {
41
sub full_message {
Lines 44-52 sub full_message { Link Here
44
            if ( $self->no_relationship ) {
48
            if ( $self->no_relationship ) {
45
                $msg = sprintf( "No relationship passed." );
49
                $msg = sprintf( "No relationship passed." );
46
            }
50
            }
47
            else {
51
            elsif ( $self->relationship ) {
48
                $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship );
52
                $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship );
49
            }
53
            }
54
            elsif ( $self->invalid_guarantor ) {
55
                $msg = sprintf("Child patron cannot be a guarantor.");
56
            }
50
        }
57
        }
51
        elsif ( $self->isa('Koha::Exceptions::Patron::Relationship::DuplicateRelationship') ) {
58
        elsif ( $self->isa('Koha::Exceptions::Patron::Relationship::DuplicateRelationship') ) {
52
            $msg
59
            $msg
(-)a/Koha/Patron.pm (-1 / +18 lines)
Lines 172-178 to db Link Here
172
=cut
172
=cut
173
173
174
sub store {
174
sub store {
175
    my ($self) = @_;
175
    my $self = shift;
176
    my $params = @_ ? shift : {};
177
178
    my $guarantor_ids = $params->{guarantor_ids} // [];
176
179
177
    $self->_result->result_source->schema->txn_do(
180
    $self->_result->result_source->schema->txn_do(
178
        sub {
181
        sub {
Lines 201-206 sub store { Link Here
201
            $self->surname( uc($self->surname) )
204
            $self->surname( uc($self->surname) )
202
                if C4::Context->preference("uppercasesurnames");
205
                if C4::Context->preference("uppercasesurnames");
203
206
207
            if ( C4::Context->preference('ChildNeedsGuarantor') and $self->is_child
208
                and $self->contactname eq "" and !@$guarantor_ids ) {
209
                Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
210
            }
211
212
            foreach my $guarantor_id (@$guarantor_ids){
213
                my $guarantor = Koha::Patrons->find({ borrowernumber => $guarantor_id });
214
                if($guarantor->is_child){
215
                    Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
216
                        invalid_guarantor => 1
217
                    );
218
                }
219
            }
220
204
            $self->relationship(undef) # We do not want to store an empty string in this field
221
            $self->relationship(undef) # We do not want to store an empty string in this field
205
              if defined $self->relationship
222
              if defined $self->relationship
206
                     and $self->relationship eq "";
223
                     and $self->relationship eq "";
(-)a/members/memberentry.pl (-2 / +2 lines)
Lines 474-480 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){ Link Here
474
	if ($op eq 'insert'){
474
	if ($op eq 'insert'){
475
		# we know it's not a duplicate borrowernumber or there would already be an error
475
		# we know it's not a duplicate borrowernumber or there would already be an error
476
        delete $newdata{password2};
476
        delete $newdata{password2};
477
        $patron = eval { Koha::Patron->new(\%newdata)->store };
477
        $patron = eval { Koha::Patron->new(\%newdata)->store({ guarantor_ids => \@guarantor_ids }) };
478
        if ( $@ ) {
478
        if ( $@ ) {
479
            # FIXME Urgent error handling here, we cannot fail without relevant feedback
479
            # FIXME Urgent error handling here, we cannot fail without relevant feedback
480
            # Lot of code will need to be removed from this script to handle exceptions raised by Koha::Patron->store
480
            # Lot of code will need to be removed from this script to handle exceptions raised by Koha::Patron->store
Lines 560-566 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){ Link Here
560
        delete $newdata{password2};
560
        delete $newdata{password2};
561
561
562
        eval {
562
        eval {
563
            $patron->set(\%newdata)->store if scalar(keys %newdata) > 1; # bug 4508 - avoid crash if we're not
563
            $patron->set(\%newdata)->store({ guarantor_ids => \@guarantor_ids }) if scalar(keys %newdata) > 1; # bug 4508 - avoid crash if we're not
564
                                                                    # updating any columns in the borrowers table,
564
                                                                    # updating any columns in the borrowers table,
565
                                                                    # which can happen if we're only editing the
565
                                                                    # which can happen if we're only editing the
566
                                                                    # patron attributes or messaging preferences sections
566
                                                                    # patron attributes or messaging preferences sections
(-)a/t/db_dependent/Koha/Patron.t (-2 / +36 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use Koha::Database;
25
use Koha::Database;
Lines 365-367 subtest 'is_superlibrarian() tests' => sub { Link Here
365
365
366
    $schema->storage->txn_rollback;
366
    $schema->storage->txn_rollback;
367
};
367
};
368
- 
368
369
subtest 'guarantor requirements tests' => sub {
370
371
    plan tests => 3;
372
373
    $schema->storage->txn_begin;
374
375
    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
376
    my $child_category = $builder->build({ source => 'Category', value => { category_type => 'C' }})->{categorycode};
377
    my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'A' }})->{categorycode};
378
379
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
380
381
    my $child = Koha::Patron->new({ branchcode => $branchcode, categorycode => $child_category, contactname => ''});
382
    $child->store();
383
384
    ok(Koha::Patrons->find($child->id), 'Child patron can be stored without guarantor when ChildNeedsGuarantor is off.');
385
386
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
387
388
    my $child2 = Koha::Patron->new({ branchcode => $branchcode, categorycode => $child_category, contactname => ''});
389
    my $child3 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $child_category }});
390
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category }});
391
392
    throws_ok { $child2->store(); }
393
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
394
    'Exception thrown when guarantor is required but not provided.';
395
396
    my @guarantor_ids = ( $patron->id, $child3->id );
397
    throws_ok { $child2->store({ guarantor_ids => \@guarantor_ids }); }
398
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
399
    'Exception thrown when child patron is added as guarantor.';
400
401
    $schema->storage->txn_rollback;
402
};

Return to bug 12133