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

(-)a/Koha/Exceptions/Patron/Relationship.pm (-3 / +10 lines)
Lines 32-39 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']
35
        fields      =>  ['relationship','no_relationship','invalid_guarantor']
36
    }
36
    },
37
    'Koha::Exceptions::Patron::Relationship::NoGuarantor' => {
38
        isa         => 'Koha::Exceptions::Patron::Relationship',
39
        description => 'Child patron needs a guarantor',
40
    },
37
);
41
);
38
42
39
sub full_message {
43
sub full_message {
Lines 46-54 sub full_message { Link Here
46
            if ( $self->no_relationship ) {
50
            if ( $self->no_relationship ) {
47
                $msg = sprintf( "No relationship passed." );
51
                $msg = sprintf( "No relationship passed." );
48
            }
52
            }
49
            else {
53
            elsif ( $self->relationship ) {
50
                $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship );
54
                $msg = sprintf("Invalid relationship passed, '%s' is not defined.", $self->relationship );
51
            }
55
            }
56
            elsif ( $self->invalid_guarantor ) {
57
                $msg = sprintf("Child patron cannot be a guarantor.");
58
            }
52
        }
59
        }
53
        elsif ( $self->isa('Koha::Exceptions::Patron::Relationship::DuplicateRelationship') ) {
60
        elsif ( $self->isa('Koha::Exceptions::Patron::Relationship::DuplicateRelationship') ) {
54
            $msg
61
            $msg
(-)a/Koha/Patron.pm (-1 / +18 lines)
Lines 181-187 to db Link Here
181
=cut
181
=cut
182
182
183
sub store {
183
sub store {
184
    my ($self) = @_;
184
    my $self = shift;
185
    my $params = @_ ? shift : {};
186
187
    my $guarantor_ids = $params->{guarantor_ids} // [];
185
188
186
    $self->_result->result_source->schema->txn_do(
189
    $self->_result->result_source->schema->txn_do(
187
        sub {
190
        sub {
Lines 214-219 sub store { Link Here
214
            $self->surname( uc($self->surname) )
217
            $self->surname( uc($self->surname) )
215
                if C4::Context->preference("uppercasesurnames");
218
                if C4::Context->preference("uppercasesurnames");
216
219
220
            if ( C4::Context->preference('ChildNeedsGuarantor') and $self->is_child
221
                and $self->contactname eq "" and !@$guarantor_ids ) {
222
                Koha::Exceptions::Patron::Relationship::NoGuarantor->throw();
223
            }
224
225
            foreach my $guarantor_id (@$guarantor_ids){
226
                my $guarantor = Koha::Patrons->find({ borrowernumber => $guarantor_id });
227
                if($guarantor->is_child){
228
                    Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
229
                        invalid_guarantor => 1
230
                    );
231
                }
232
            }
233
217
            $self->relationship(undef) # We do not want to store an empty string in this field
234
            $self->relationship(undef) # We do not want to store an empty string in this field
218
              if defined $self->relationship
235
              if defined $self->relationship
219
                     and $self->relationship eq "";
236
                     and $self->relationship eq "";
(-)a/members/memberentry.pl (-2 / +2 lines)
Lines 468-474 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){ Link Here
468
	if ($op eq 'insert'){
468
	if ($op eq 'insert'){
469
		# we know it's not a duplicate borrowernumber or there would already be an error
469
		# we know it's not a duplicate borrowernumber or there would already be an error
470
        delete $newdata{password2};
470
        delete $newdata{password2};
471
        $patron = eval { Koha::Patron->new(\%newdata)->store };
471
        $patron = eval { Koha::Patron->new(\%newdata)->store({ guarantor_ids => \@guarantor_ids }) };
472
        if ( $@ ) {
472
        if ( $@ ) {
473
            # FIXME Urgent error handling here, we cannot fail without relevant feedback
473
            # FIXME Urgent error handling here, we cannot fail without relevant feedback
474
            # Lot of code will need to be removed from this script to handle exceptions raised by Koha::Patron->store
474
            # Lot of code will need to be removed from this script to handle exceptions raised by Koha::Patron->store
Lines 554-560 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){ Link Here
554
        delete $newdata{password2};
554
        delete $newdata{password2};
555
555
556
        eval {
556
        eval {
557
            $patron->set(\%newdata)->store if scalar(keys %newdata) > 1; # bug 4508 - avoid crash if we're not
557
            $patron->set(\%newdata)->store({ guarantor_ids => \@guarantor_ids }) if scalar(keys %newdata) > 1; # bug 4508 - avoid crash if we're not
558
                                                                    # updating any columns in the borrowers table,
558
                                                                    # updating any columns in the borrowers table,
559
                                                                    # which can happen if we're only editing the
559
                                                                    # which can happen if we're only editing the
560
                                                                    # patron attributes or messaging preferences sections
560
                                                                    # 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 => 16;
22
use Test::More tests => 17;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 1137-1139 subtest 'recalls() tests' => sub { Link Here
1137
1137
1138
    $schema->storage->txn_rollback;
1138
    $schema->storage->txn_rollback;
1139
};
1139
};
1140
- 
1140
1141
subtest 'guarantor requirements tests' => sub {
1142
1143
    plan tests => 3;
1144
1145
    $schema->storage->txn_begin;
1146
1147
    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
1148
    my $child_category = $builder->build({ source => 'Category', value => { category_type => 'C' }})->{categorycode};
1149
    my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'A' }})->{categorycode};
1150
1151
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 0 );
1152
1153
    my $child = Koha::Patron->new({ branchcode => $branchcode, categorycode => $child_category, contactname => ''});
1154
    $child->store();
1155
1156
    ok(Koha::Patrons->find($child->id), 'Child patron can be stored without guarantor when ChildNeedsGuarantor is off.');
1157
1158
    t::lib::Mocks::mock_preference( 'ChildNeedsGuarantor', 1 );
1159
1160
    my $child2 = Koha::Patron->new({ branchcode => $branchcode, categorycode => $child_category, contactname => ''});
1161
    my $child3 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $child_category }});
1162
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category }});
1163
1164
    throws_ok { $child2->store(); }
1165
    'Koha::Exceptions::Patron::Relationship::NoGuarantor',
1166
    'Exception thrown when guarantor is required but not provided.';
1167
1168
    my @guarantor_ids = ( $patron->id, $child3->id );
1169
    throws_ok { $child2->store({ guarantor_ids => \@guarantor_ids }); }
1170
    'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
1171
    'Exception thrown when child patron is added as guarantor.';
1172
1173
    $schema->storage->txn_rollback;
1174
};

Return to bug 12133