Bug 23051: (follow-up) Add renewal feedback and move code to subroutines and test
[koha.git] / Koha / REST / V1 / Patrons / Account.pm
blobaca65dd17ef3f28d0915eb9275022fd1d1d9caec
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>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Patrons;
24 use Scalar::Util qw(blessed);
25 use Try::Tiny;
27 =head1 NAME
29 Koha::REST::V1::Patrons::Account
31 =head1 API
33 =head2 Methods
35 =head3 get
37 Controller function that handles retrieving a patron's account balance
39 =cut
41 sub get {
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);
47 unless ($patron) {
48 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;
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
65 outstanding_credits => {
66 total => $credits->total_outstanding,
67 lines => $credits->to_api
73 =head3 add_credit
75 Controller function that handles adding a credit to a patron's account
77 =cut
79 sub add_credit {
80 my $c = shift->openapi->valid_input or return;
82 my $patron_id = $c->validation->param('patron_id');
83 my $patron = Koha::Patrons->find($patron_id);
84 my $user = $c->stash('koha.user');
87 unless ($patron) {
88 return $c->render( status => 404, openapi => { error => "Patron not found." } );
91 my $account = $patron->account;
92 my $body = $c->validation->param('body');
94 return try {
95 my $credit_type = $body->{credit_type} || 'PAYMENT'; # default to 'PAYMENT'
96 my $amount = $body->{amount}; # mandatory, validated by openapi
98 unless ( $amount > 0 ) { # until we support newer JSON::Validator and thus minimumExclusive
99 Koha::Exceptions::BadParameter->throw( { parameter => 'amount' } );
102 # read the rest of the params
103 my $payment_type = $body->{payment_type};
104 my $description = $body->{description};
105 my $note = $body->{note};
106 my $library_id = $body->{library_id};
108 my $credit = $account->add_credit(
109 { amount => $amount,
110 type => $credit_type,
111 payment_type => $payment_type,
112 description => $description,
113 note => $note,
114 user_id => $user->id,
115 interface => 'api',
116 library_id => $library_id
119 $credit->discard_changes;
121 my $date = $body->{date};
122 $credit->date( $date )->store
123 if $date;
125 my $debits_ids = $body->{account_lines_ids};
126 my $debits;
127 $debits = Koha::Account::Lines->search({ accountlines_id => { -in => $debits_ids } })
128 if $debits_ids;
130 my $outstanding_credit = $credit->amountoutstanding;
131 if ($debits) {
132 # pay them!
133 $outstanding_credit = $credit->apply({ debits => [ $debits->as_list ], offset_type => 'payment' })->{outstanding_amount};
136 if ($outstanding_credit) {
137 my $outstanding_debits = $account->outstanding_debits;
138 $credit->apply({ debits => [ $outstanding_debits->as_list ], offset_type => 'payment' });
141 return $c->render( status => 200, openapi => { account_line_id => $credit->id } );
143 catch {
144 if ( blessed $_ && $_->can('rethrow') ) {
145 return $c->render(
146 status => 400,
147 openapi => { error => "$_" }
150 else {
151 # Exception, rely on the stringified exception
152 return $c->render(
153 status => 500,
154 openapi => { error => "Something went wrong, check the logs" }