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

(-)a/Koha/Patron.pm (+29 lines)
Lines 24-29 use Carp; Link Here
24
24
25
use C4::Context;
25
use C4::Context;
26
use C4::Log;
26
use C4::Log;
27
use Koha::Availability::Checks::Patron;
27
use Koha::Checkouts;
28
use Koha::Checkouts;
28
use Koha::Database;
29
use Koha::Database;
29
use Koha::DateUtils;
30
use Koha::DateUtils;
Lines 583-588 sub holds { Link Here
583
    return Koha::Holds->_new_from_dbic($holds_rs);
584
    return Koha::Holds->_new_from_dbic($holds_rs);
584
}
585
}
585
586
587
=head3 status_not_ok
588
589
my $status = $patron->status_not_ok
590
591
Checks patron's status for checkouts and holds.
592
593
Returns an array of Koha::Exception::Patron::* if any restrictions are found.
594
595
=cut
596
597
sub status_not_ok {
598
    my ($self) = @_;
599
600
    my $patron_checks = Koha::Availability::Checks::Patron->new($self);
601
602
    my @problems;
603
    my $ex;
604
    push @problems, $ex if $ex = $patron_checks->debarred;
605
    push @problems, $ex if $ex = $patron_checks->debt_hold;
606
    push @problems, $ex if $ex = $patron_checks->debt_checkout_guarantees;
607
    push @problems, $ex if $ex = $patron_checks->exceeded_maxreserves;
608
    push @problems, $ex if $ex = $patron_checks->expired;
609
    push @problems, $ex if $ex = $patron_checks->gonenoaddress;
610
    push @problems, $ex if $ex = $patron_checks->lost;
611
612
    return @problems;
613
}
614
586
=head3 type
615
=head3 type
587
616
588
=cut
617
=cut
(-)a/Koha/REST/V1/Patron.pm (+31 lines)
Lines 19-26 use Modern::Perl; Link Here
19
19
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Koha::Availability;
22
use Koha::Patrons;
23
use Koha::Patrons;
23
24
25
use Try::Tiny;
26
24
sub list {
27
sub list {
25
    my ($c, $args, $cb) = @_;
28
    my ($c, $args, $cb) = @_;
26
29
Lines 44-47 sub get { Link Here
44
    return $c->$cb($patron->unblessed, 200);
47
    return $c->$cb($patron->unblessed, 200);
45
}
48
}
46
49
50
sub getstatus {
51
    my ($c, $args, $cb) = @_;
52
53
    return try {
54
        my $user = $c->stash('koha.user');
55
56
        my $patron = Koha::Patrons->find($args->{borrowernumber});
57
        unless ($patron) {
58
            return $c->$cb({error => "Patron not found"}, 404);
59
        }
60
61
        my $ret = $patron->unblessed;
62
        my %problems = map { ref($_) => $_ } $patron->status_not_ok;
63
        $ret->{blocks} = Koha::Availability->_swaggerize_exception(\%problems);
64
65
        return $c->$cb($ret, 200);
66
    } catch {
67
        if ( $_->isa('DBIx::Class::Exception') ) {
68
            return $c->$cb( { error => $_->msg }, 500 );
69
        }
70
        else {
71
            return $c->$cb(
72
                { error => "Something went wrong, check the logs." }, 500 );
73
        }
74
    };
75
    
76
}
77
47
1;
78
1;
(-)a/t/db_dependent/Koha/Patrons.t (-1 / +40 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 20;
22
use Test::More tests => 21;
23
use Test::Warn;
23
use Test::Warn;
24
use DateTime;
24
use DateTime;
25
25
Lines 661-666 subtest 'holds' => sub { Link Here
661
    $patron->delete;
661
    $patron->delete;
662
};
662
};
663
663
664
subtest 'status_not_ok' => sub {
665
    plan tests => 5;
666
667
    t::lib::Mocks::mock_preference('maxoutstanding', 5);
668
    my $patron = $builder->build(
669
        {
670
            source => 'Borrower',
671
            value  => { branchcode => $library->{branchcode},
672
                        gonenoaddress => 0,
673
                        lost => 0,
674
                        debarred => undef,
675
                        debarredcomment => undef,
676
                        dateexpiry => '9999-12-12' }
677
        }
678
    );
679
680
    $patron = Koha::Patrons->find($patron->{borrowernumber});
681
    my $line = Koha::Account::Line->new({
682
        borrowernumber => $patron->borrowernumber,
683
        amountoutstanding => 9001,
684
    })->store;
685
    my $outstanding = $patron->account->balance;
686
    my $maxoutstanding = C4::Context->preference('maxoutstanding');
687
    my $expecting = 'Koha::Exceptions::Patron::Debt';
688
    my @problems = $patron->status_not_ok;
689
690
    ok($maxoutstanding, 'When I look at system preferences, I see that maximum '
691
       .'allowed outstanding fines is set.');
692
    ok($maxoutstanding < $outstanding, 'When I check patron\'s balance, I found '
693
       .'out they have more outstanding fines than allowed.');
694
    is(scalar(@problems), 1, 'There is an issue with patron\'s current status');
695
    my $debt = $problems[0];
696
    is($debt->max_outstanding, 0+$maxoutstanding, 'Then I can see the status '
697
       .'showing me how much outstanding total can be at maximum.');
698
    is($debt->current_outstanding, 0+$outstanding, 'Then I can see the status '
699
       .'showing me how much outstanding fines patron has right now.');
700
    $patron->delete;
701
};
702
664
$retrieved_patron_1->delete;
703
$retrieved_patron_1->delete;
665
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
704
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
666
705
(-)a/t/db_dependent/api/v1/patrons.t (-2 / +22 lines)
Lines 17-24 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 20;
20
use Test::More tests => 25;
21
use Test::Mojo;
21
use Test::Mojo;
22
use t::lib::Mocks;
22
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
23
24
24
use C4::Auth;
25
use C4::Auth;
Lines 129-132 $t->request_ok($tx) Link Here
129
  ->json_is('/borrowernumber' => $borrower->{ borrowernumber })
130
  ->json_is('/borrowernumber' => $borrower->{ borrowernumber })
130
  ->json_is('/surname' => $borrower->{ surname });
131
  ->json_is('/surname' => $borrower->{ surname });
131
132
133
# patronstatus
134
my $debt = {
135
    current_outstanding => 9001,
136
    max_outstanding => 5,
137
};
138
t::lib::Mocks::mock_preference('maxoutstanding', $debt->{max_outstanding});
139
my $patron = Koha::Patrons->find($borrower->{borrowernumber});
140
my $line = Koha::Account::Line->new({
141
    borrowernumber => $patron->borrowernumber,
142
    amountoutstanding => $debt->{current_outstanding},
143
})->store;
144
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/".$borrower->{ borrowernumber }
145
                             ."/status");
146
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
147
$t->request_ok($tx)
148
  ->status_is(200)
149
  ->json_is('/borrowernumber' => $borrower->{ borrowernumber })
150
  ->json_is('/surname' => $borrower->{ surname })
151
  ->json_is('/blocks/Patron::Debt' => $debt);
152
132
$dbh->rollback;
153
$dbh->rollback;
133
- 

Return to bug 18103