1 package Koha::REST::V1::Patrons::Account;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
24 use Scalar::Util qw(blessed);
29 Koha::REST::V1::Patrons::Account
37 Controller function that handles retrieving a patron's account balance
42 my $c = shift->openapi->valid_input or return;
44 my $patron_id = $c->validation->param('patron_id');
45 my $patron = Koha::Patrons->find($patron_id);
48 return $c->render( status => 404, openapi => { error => "Patron not found." } );
51 my $account = $patron->account;
54 $balance->{balance} = $account->balance;
56 # get outstanding debits and credits
57 my $debits = $account->outstanding_debits;
58 my $credits = $account->outstanding_credits;
60 my @debit_lines = map { _to_api( $_->TO_JSON ) } @{ $debits->as_list };
61 $balance->{outstanding_debits} = {
62 total => $debits->total_outstanding,
63 lines => \@debit_lines
66 my @credit_lines = map { _to_api( $_->TO_JSON ) } @{ $credits->as_list };
67 $balance->{outstanding_credits} = {
68 total => $credits->total_outstanding,
69 lines => \@credit_lines
72 return $c->render( status => 200, openapi => $balance );
77 Controller function that handles adding a credit to a patron's account
82 my $c = shift->openapi->valid_input or return;
84 my $patron_id = $c->validation->param('patron_id');
85 my $patron = Koha::Patrons->find($patron_id);
86 my $user = $c->stash('koha.user');
90 return $c->render( status => 404, openapi => { error => "Patron not found." } );
93 my $account = $patron->account;
94 my $body = $c->validation->param('body');
97 my $credit_type = $body->{credit_type} || 'payment'; # default to 'payment'
98 my $amount = $body->{amount}; # mandatory, validated by openapi
100 unless ( $amount > 0 ) { # until we support newer JSON::Validator and thus minimumExclusive
101 Koha::Exceptions::BadParameter->throw( { parameter => 'amount' } );
104 # read the rest of the params
105 my $payment_type = $body->{payment_type};
106 my $description = $body->{description};
107 my $note = $body->{note};
108 my $library_id = $body->{library_id};
110 my $credit = $account->add_credit(
112 credit_type => $credit_type,
113 payment_type => $payment_type,
114 description => $description,
116 user_id => $user->id,
118 library_id => $library_id
121 $credit->discard_changes;
123 my $date = $body->{date};
124 $credit->date( $date )->store
127 my $debits_ids = $body->{account_lines_ids};
129 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => $debits_ids } })
132 my $outstanding_credit = $credit->amountoutstanding;
135 $outstanding_credit = $credit->apply({ debits => $debits, offset_type => 'payment' });
138 if ($outstanding_credit) {
139 my $outstanding_debits = $account->outstanding_debits;
140 $credit->apply({ debits => $outstanding_debits, offset_type => 'payment' });
143 return $c->render( status => 200, openapi => { account_line_id => $credit->id } );
146 if ( blessed $_ && $_->can('rethrow') ) {
149 openapi => { error => "$_" }
153 # Exception, rely on the stringified exception
156 openapi => { error => "Something went wrong, check the logs" }
165 Helper function that maps unblessed Koha::Account::Line objects
166 into REST API attribute names.
171 my $account_line = shift;
174 foreach my $column ( keys %{ $Koha::REST::V1::Patrons::Account::to_api_mapping } ) {
175 my $mapped_column = $Koha::REST::V1::Patrons::Account::to_api_mapping->{$column};
176 if ( exists $account_line->{ $column }
177 && defined $mapped_column )
180 $account_line->{ $mapped_column } = delete $account_line->{ $column };
182 elsif ( exists $account_line->{ $column }
183 && !defined $mapped_column )
186 delete $account_line->{ $column };
190 return $account_line;
195 Helper function that maps REST API objects into Koha::Account::Line
201 my $account_line = shift;
203 foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::Account::to_model_mapping } ) {
204 my $mapped_attribute = $Koha::REST::V1::Patrons::Account::to_model_mapping->{$attribute};
205 if ( exists $account_line->{ $attribute }
206 && defined $mapped_attribute )
209 $account_line->{ $mapped_attribute } = delete $account_line->{ $attribute };
211 elsif ( exists $account_line->{ $attribute }
212 && !defined $mapped_attribute )
214 # key => undef / to be deleted
215 delete $account_line->{ $attribute };
219 return $account_line;
222 =head2 Global variables
224 =head3 $to_api_mapping
228 our $to_api_mapping = {
229 accountlines_id => 'account_line_id',
230 accounttype => 'account_type',
231 amountoutstanding => 'amount_outstanding',
232 borrowernumber => 'patron_id',
233 branchcode => 'library_id',
234 issue_id => 'checkout_id',
235 itemnumber => 'item_id',
236 manager_id => 'user_id',
237 note => 'internal_note',
240 =head3 $to_model_mapping
244 our $to_model_mapping = {
245 account_line_id => 'accountlines_id',
246 account_type => 'accounttype',
247 amount_outstanding => 'amountoutstanding',
248 checkout_id => 'issue_id',
249 internal_note => 'note',
250 item_id => 'itemnumber',
251 library_id => 'branchcode',
252 patron_id => 'borrowernumber',
253 user_id => 'manager_id'