Bug 29453: Add endpoints for fetching patron credits & debits
[koha.git] / Koha / REST / V1 / Patrons / Account.pm
1 package Koha::REST::V1::Patrons::Account;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Patrons;
23
24 use Try::Tiny qw( catch try );
25
26 =head1 NAME
27
28 Koha::REST::V1::Patrons::Account
29
30 =head1 API
31
32 =head2 Methods
33
34 =head3 get
35
36 Controller function that handles retrieving a patron's account balance
37
38 =cut
39
40 sub get {
41     my $c = shift->openapi->valid_input or return;
42
43     my $patron_id = $c->validation->param('patron_id');
44     my $patron    = Koha::Patrons->find($patron_id);
45
46     unless ($patron) {
47         return $c->render( status => 404, openapi => { error => "Patron not found." } );
48     }
49
50     return try {
51         my $account = $patron->account;
52
53         # get outstanding debits and credits
54         my $debits  = $account->outstanding_debits;
55         my $credits = $account->outstanding_credits;
56
57         return $c->render(
58             status  => 200,
59             openapi => {
60                 balance => $account->balance,
61                 outstanding_debits => {
62                     total => $debits->total_outstanding,
63                     lines => $debits->to_api
64                 },
65                 outstanding_credits => {
66                     total => $credits->total_outstanding,
67                     lines => $credits->to_api
68                 }
69             }
70         );
71     }
72     catch {
73         $c->unhandled_exception($_);
74     };
75 }
76
77 =head3 list_credits
78
79 =cut
80
81 sub list_credits {
82     my $c = shift->openapi->valid_input or return;
83
84     my $patron_id = $c->validation->param('patron_id');
85     my $patron    = Koha::Patrons->find($patron_id);
86
87     unless ($patron) {
88         return $c->render( status => 404, openapi => { error => "Patron not found." } );
89     }
90
91     return try {
92         my $account = $patron->account;
93
94         my $credits_set = $account->credits;
95         my $credits = $c->objects->search( $credits_set );
96         return $c->render( status => 200, openapi => $credits );
97     }
98     catch {
99         $c->unhandled_exception($_);
100     };
101 }
102
103 =head3 add_credit
104
105 Controller function that handles adding a credit to a patron's account
106
107 =cut
108
109 sub add_credit {
110     my $c = shift->openapi->valid_input or return;
111
112     my $patron_id = $c->validation->param('patron_id');
113     my $patron    = Koha::Patrons->find($patron_id);
114     my $user      = $c->stash('koha.user');
115
116
117     unless ($patron) {
118         return $c->render( status => 404, openapi => { error => "Patron not found." } );
119     }
120
121     my $account = $patron->account;
122     my $body    = $c->validation->param('body');
123
124     return try {
125         my $credit_type = $body->{credit_type} || 'PAYMENT';    # default to 'PAYMENT'
126         my $amount = $body->{amount};                           # mandatory, validated by openapi
127
128         unless ( $amount > 0 ) {  # until we support newer JSON::Validator and thus minimumExclusive
129             Koha::Exceptions::BadParameter->throw( { parameter => 'amount' } );
130         }
131
132         # read the rest of the params
133         my $payment_type = $body->{payment_type};
134         my $description  = $body->{description};
135         my $note         = $body->{note};
136         my $library_id   = $body->{library_id};
137
138         my $credit = $account->add_credit(
139             {   amount       => $amount,
140                 type         => $credit_type,
141                 payment_type => $payment_type,
142                 description  => $description,
143                 note         => $note,
144                 user_id      => $user->id,
145                 interface    => 'api',
146                 library_id   => $library_id
147             }
148         );
149         $credit->discard_changes;
150
151         my $date = $body->{date};
152         $credit->date( $date )->store
153             if $date;
154
155         my $debits_ids = $body->{account_lines_ids};
156         my $debits;
157         $debits = Koha::Account::Lines->search({ accountlines_id => { -in => $debits_ids } })
158             if $debits_ids;
159
160         if ($debits) {
161             # pay them!
162             $credit = $credit->apply({ debits => [ $debits->as_list ] });
163         }
164
165         if ($credit->amountoutstanding != 0) {
166             my $outstanding_debits = $account->outstanding_debits;
167             $credit->apply({ debits => [ $outstanding_debits->as_list ] });
168         }
169
170         $credit->discard_changes;
171
172         return $c->render(
173             status  => 201,
174             openapi => $credit->to_api
175         );
176     }
177     catch {
178         $c->unhandled_exception($_);
179     };
180 }
181
182 =head3 list_debits
183
184 =cut
185
186 sub list_debits {
187     my $c = shift->openapi->valid_input or return;
188
189     my $patron_id = $c->validation->param('patron_id');
190     my $patron    = Koha::Patrons->find($patron_id);
191
192     unless ($patron) {
193         return $c->render( status => 404, openapi => { error => "Patron not found." } );
194     }
195
196     return try {
197         my $account = $patron->account;
198
199         my $debits_set = $account->debits;
200         my $debits = $c->objects->search( $debits_set );
201         return $c->render( status => 200, openapi => $debits );
202     }
203     catch {
204         $c->unhandled_exception($_);
205     };
206 }
207
208 1;