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
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
24 use Try::Tiny qw( catch try );
28 Koha::REST::V1::Patrons::Account
36 Controller function that handles retrieving a patron's account balance
41 my $c = shift->openapi->valid_input or return;
43 my $patron_id = $c->validation->param('patron_id');
44 my $patron = Koha::Patrons->find($patron_id);
47 return $c->render( status => 404, openapi => { error => "Patron not found." } );
51 my $account = $patron->account;
53 # get outstanding debits and credits
54 my $debits = $account->outstanding_debits;
55 my $credits = $account->outstanding_credits;
60 balance => $account->balance,
61 outstanding_debits => {
62 total => $debits->total_outstanding,
63 lines => $debits->to_api
65 outstanding_credits => {
66 total => $credits->total_outstanding,
67 lines => $credits->to_api
73 $c->unhandled_exception($_);
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);
88 return $c->render( status => 404, openapi => { error => "Patron not found." } );
92 my $account = $patron->account;
94 my $credits_set = $account->credits;
95 my $credits = $c->objects->search( $credits_set );
96 return $c->render( status => 200, openapi => $credits );
99 $c->unhandled_exception($_);
105 Controller function that handles adding a credit to a patron's account
110 my $c = shift->openapi->valid_input or return;
112 my $patron_id = $c->validation->param('patron_id');
113 my $patron = Koha::Patrons->find($patron_id);
114 my $user = $c->stash('koha.user');
118 return $c->render( status => 404, openapi => { error => "Patron not found." } );
121 my $account = $patron->account;
122 my $body = $c->validation->param('body');
125 my $credit_type = $body->{credit_type} || 'PAYMENT'; # default to 'PAYMENT'
126 my $amount = $body->{amount}; # mandatory, validated by openapi
128 unless ( $amount > 0 ) { # until we support newer JSON::Validator and thus minimumExclusive
129 Koha::Exceptions::BadParameter->throw( { parameter => 'amount' } );
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};
138 my $credit = $account->add_credit(
140 type => $credit_type,
141 payment_type => $payment_type,
142 description => $description,
144 user_id => $user->id,
146 library_id => $library_id
149 $credit->discard_changes;
151 my $date = $body->{date};
152 $credit->date( $date )->store
155 my $debits_ids = $body->{account_lines_ids};
157 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => $debits_ids } })
162 $credit = $credit->apply({ debits => [ $debits->as_list ] });
165 if ($credit->amountoutstanding != 0) {
166 my $outstanding_debits = $account->outstanding_debits;
167 $credit->apply({ debits => [ $outstanding_debits->as_list ] });
170 $credit->discard_changes;
174 openapi => $credit->to_api
178 $c->unhandled_exception($_);
187 my $c = shift->openapi->valid_input or return;
189 my $patron_id = $c->validation->param('patron_id');
190 my $patron = Koha::Patrons->find($patron_id);
193 return $c->render( status => 404, openapi => { error => "Patron not found." } );
197 my $account = $patron->account;
199 my $debits_set = $account->debits;
200 my $debits = $c->objects->search( $debits_set );
201 return $c->render( status => 200, openapi => $debits );
204 $c->unhandled_exception($_);