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';
26 Koha::REST::V1::Patrons::Account
34 Controller function that handles retrieving a patron's account balance
39 my $c = shift->openapi->valid_input or return;
41 my $patron_id = $c->validation->param('patron_id');
42 my $patron = Koha::Patrons->find($patron_id);
45 return $c->render( status => 404, openapi => { error => "Patron not found." } );
48 my $account = $patron->account;
51 $balance->{balance} = $account->balance;
53 # get outstanding debits
54 my ( $debits_total, $debits ) = $account->outstanding_debits;
55 my ( $credits_total, $credits ) = $account->outstanding_credits;
57 my @debit_lines = map { _to_api( $_->TO_JSON ) } @{ $debits->as_list };
58 $balance->{outstanding_debits} = {
59 total => $debits_total,
60 lines => \@debit_lines
63 my @credit_lines = map { _to_api( $_->TO_JSON ) } @{ $credits->as_list };
64 $balance->{outstanding_credits} = {
65 total => $credits_total,
66 lines => \@credit_lines
69 return $c->render( status => 200, openapi => $balance );
74 Helper function that maps unblessed Koha::Account::Line objects
75 into REST API attribute names.
80 my $account_line = shift;
83 foreach my $column ( keys %{ $Koha::REST::V1::Patrons::Account::to_api_mapping } ) {
84 my $mapped_column = $Koha::REST::V1::Patrons::Account::to_api_mapping->{$column};
85 if ( exists $account_line->{ $column }
86 && defined $mapped_column )
89 $account_line->{ $mapped_column } = delete $account_line->{ $column };
91 elsif ( exists $account_line->{ $column }
92 && !defined $mapped_column )
95 delete $account_line->{ $column };
104 Helper function that maps REST API objects into Koha::Account::Line
110 my $account_line = shift;
112 foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::Account::to_model_mapping } ) {
113 my $mapped_attribute = $Koha::REST::V1::Patrons::Account::to_model_mapping->{$attribute};
114 if ( exists $account_line->{ $attribute }
115 && defined $mapped_attribute )
118 $account_line->{ $mapped_attribute } = delete $account_line->{ $attribute };
120 elsif ( exists $account_line->{ $attribute }
121 && !defined $mapped_attribute )
123 # key => undef / to be deleted
124 delete $account_line->{ $attribute };
128 return $account_line;
131 =head2 Global variables
133 =head3 $to_api_mapping
137 our $to_api_mapping = {
138 accountlines_id => 'account_line_id',
139 accountno => undef, # removed
140 accounttype => 'account_type',
141 amountoutstanding => 'amount_outstanding',
142 borrowernumber => 'patron_id',
144 issue_id => 'checkout_id',
145 itemnumber => 'item_id',
146 manager_id => 'user_id',
147 note => 'internal_note',
150 =head3 $to_model_mapping
154 our $to_model_mapping = {
155 account_line_id => 'accountlines_id',
156 account_type => 'accounttype',
157 amount_outstanding => 'amountoutstanding',
158 checkout_id => 'issue_id',
159 internal_note => 'note',
160 item_id => 'itemnumber',
161 patron_id => 'borrowernumber',
162 user_id => 'manager_id'