Bug 20942: Add route to get patron's account balance
[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 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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Try::Tiny;
23
24 =head1 NAME
25
26 Koha::REST::V1::Patrons::Account
27
28 =head1 API
29
30 =head2 Methods
31
32 =head3 get
33
34 Controller function that handles retrieving a patron's account balance
35
36 =cut
37
38 sub get {
39     my $c = shift->openapi->valid_input or return;
40
41     my $patron_id = $c->validation->param('patron_id');
42     my $patron    = Koha::Patrons->find($patron_id);
43
44     unless ($patron) {
45         return $c->render( status => 404, openapi => { error => "Patron not found." } );
46     }
47
48     my $balance;
49
50     $balance->{balance} = $patron->account->balance;
51
52     my @outstanding_lines = Koha::Account::Lines->search(
53         {   borrowernumber    => $patron->borrowernumber,
54             amountoutstanding => { '!=' => 0 }
55         }
56     );
57     foreach my $line ( @outstanding_lines ) {
58         push @{ $balance->{outstanding_lines} },  _to_api($line->TO_JSON)
59     }
60
61     return $c->render( status => 200, openapi => $balance );
62 }
63
64 =head3 _to_api
65
66 Helper function that maps unblessed Koha::Account::Line objects
67 into REST API attribute names.
68
69 =cut
70
71 sub _to_api {
72     my $account_line = shift;
73
74     # Rename attributes
75     foreach my $column ( keys %{ $Koha::REST::V1::Patrons::Account::to_api_mapping } ) {
76         my $mapped_column = $Koha::REST::V1::Patrons::Account::to_api_mapping->{$column};
77         if (    exists $account_line->{ $column }
78              && defined $mapped_column )
79         {
80             # key != undef
81             $account_line->{ $mapped_column } = delete $account_line->{ $column };
82         }
83         elsif (    exists $account_line->{ $column }
84                 && !defined $mapped_column )
85         {
86             # key == undef
87             delete $account_line->{ $column };
88         }
89     }
90
91     return $account_line;
92 }
93
94 =head3 _to_model
95
96 Helper function that maps REST API objects into Koha::Account::Line
97 attribute names.
98
99 =cut
100
101 sub _to_model {
102     my $account_line = shift;
103
104     foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::Account::to_model_mapping } ) {
105         my $mapped_attribute = $Koha::REST::V1::Patrons::Account::to_model_mapping->{$attribute};
106         if (    exists $account_line->{ $attribute }
107              && defined $mapped_attribute )
108         {
109             # key => !undef
110             $account_line->{ $mapped_attribute } = delete $account_line->{ $attribute };
111         }
112         elsif (    exists $account_line->{ $attribute }
113                 && !defined $mapped_attribute )
114         {
115             # key => undef / to be deleted
116             delete $account_line->{ $attribute };
117         }
118     }
119
120     return $account_line;
121 }
122
123 =head2 Global variables
124
125 =head3 $to_api_mapping
126
127 =cut
128
129 our $to_api_mapping = {
130     accountlines_id   => 'account_line_id',
131     accountno         => undef,                  # removed
132     accounttype       => 'account_type',
133     amountoutstanding => 'amount_outstanding',
134     borrowernumber    => 'patron_id',
135     dispute           => undef,
136     issue_id          => 'checkout_id',
137     itemnumber        => 'item_id',
138     manager_id        => 'staff_id',
139     note              => 'internal_note',
140 };
141
142 =head3 $to_model_mapping
143
144 =cut
145
146 our $to_model_mapping = {
147     account_line_id    => 'accountlines_id',
148     account_type       => 'accounttype',
149     amount_outstanding => 'amountoutstanding',
150     checkout_id        => 'issue_id',
151     internal_note      => 'note',
152     item_id            => 'itemnumber',
153     patron_id          => 'borrowernumber',
154     staff_id           => 'manager_id'
155 };
156
157 1;