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." } );
50 $balance->{balance} = $patron->account->balance;
52 my @outstanding_lines = Koha::Account::Lines->search(
53 { borrowernumber => $patron->borrowernumber,
54 amountoutstanding => { '!=' => 0 }
57 foreach my $line ( @outstanding_lines ) {
58 push @{ $balance->{outstanding_lines} }, _to_api($line->TO_JSON)
61 return $c->render( status => 200, openapi => $balance );
66 Helper function that maps unblessed Koha::Account::Line objects
67 into REST API attribute names.
72 my $account_line = shift;
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 )
81 $account_line->{ $mapped_column } = delete $account_line->{ $column };
83 elsif ( exists $account_line->{ $column }
84 && !defined $mapped_column )
87 delete $account_line->{ $column };
96 Helper function that maps REST API objects into Koha::Account::Line
102 my $account_line = shift;
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 )
110 $account_line->{ $mapped_attribute } = delete $account_line->{ $attribute };
112 elsif ( exists $account_line->{ $attribute }
113 && !defined $mapped_attribute )
115 # key => undef / to be deleted
116 delete $account_line->{ $attribute };
120 return $account_line;
123 =head2 Global variables
125 =head3 $to_api_mapping
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',
136 issue_id => 'checkout_id',
137 itemnumber => 'item_id',
138 manager_id => 'staff_id',
139 note => 'internal_note',
142 =head3 $to_model_mapping
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'