Bug 20942: Split debit and credit lines
[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 $account = $patron->account;
49     my $balance;
50
51     $balance->{balance} = $account->balance;
52
53     # get outstanding debits
54     my ( $debits_total,  $debits )  = $account->outstanding_debits;
55     my ( $credits_total, $credits ) = $account->outstanding_credits;
56
57     my @debit_lines = map { _to_api( $_->TO_JSON ) } @{ $debits->as_list };
58     $balance->{outstanding_debits} = {
59         total => $debits_total,
60         lines => \@debit_lines
61     };
62
63     my @credit_lines = map { _to_api( $_->TO_JSON ) } @{ $credits->as_list };
64     $balance->{outstanding_credits} = {
65         total => $credits_total,
66         lines => \@credit_lines
67     };
68
69     return $c->render( status => 200, openapi => $balance );
70 }
71
72 =head3 _to_api
73
74 Helper function that maps unblessed Koha::Account::Line objects
75 into REST API attribute names.
76
77 =cut
78
79 sub _to_api {
80     my $account_line = shift;
81
82     # Rename attributes
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 )
87         {
88             # key != undef
89             $account_line->{ $mapped_column } = delete $account_line->{ $column };
90         }
91         elsif (    exists $account_line->{ $column }
92                 && !defined $mapped_column )
93         {
94             # key == undef
95             delete $account_line->{ $column };
96         }
97     }
98
99     return $account_line;
100 }
101
102 =head3 _to_model
103
104 Helper function that maps REST API objects into Koha::Account::Line
105 attribute names.
106
107 =cut
108
109 sub _to_model {
110     my $account_line = shift;
111
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 )
116         {
117             # key => !undef
118             $account_line->{ $mapped_attribute } = delete $account_line->{ $attribute };
119         }
120         elsif (    exists $account_line->{ $attribute }
121                 && !defined $mapped_attribute )
122         {
123             # key => undef / to be deleted
124             delete $account_line->{ $attribute };
125         }
126     }
127
128     return $account_line;
129 }
130
131 =head2 Global variables
132
133 =head3 $to_api_mapping
134
135 =cut
136
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',
143     dispute           => undef,
144     issue_id          => 'checkout_id',
145     itemnumber        => 'item_id',
146     manager_id        => 'user_id',
147     note              => 'internal_note',
148 };
149
150 =head3 $to_model_mapping
151
152 =cut
153
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'
163 };
164
165 1;