1bf64aa58c98194f81f3a1d65c476829e813b3d4
[srvgit] / 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
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Patrons;
23
24 use Scalar::Util qw(blessed);
25 use Try::Tiny;
26
27 =head1 NAME
28
29 Koha::REST::V1::Patrons::Account
30
31 =head1 API
32
33 =head2 Methods
34
35 =head3 get
36
37 Controller function that handles retrieving a patron's account balance
38
39 =cut
40
41 sub get {
42     my $c = shift->openapi->valid_input or return;
43
44     my $patron_id = $c->validation->param('patron_id');
45     my $patron    = Koha::Patrons->find($patron_id);
46
47     unless ($patron) {
48         return $c->render( status => 404, openapi => { error => "Patron not found." } );
49     }
50
51     return try {
52         my $account = $patron->account;
53
54         # get outstanding debits and credits
55         my $debits  = $account->outstanding_debits;
56         my $credits = $account->outstanding_credits;
57
58         return $c->render(
59             status  => 200,
60             openapi => {
61                 balance => $account->balance,
62                 outstanding_debits => {
63                     total => $debits->total_outstanding,
64                     lines => $debits->to_api
65                 },
66                 outstanding_credits => {
67                     total => $credits->total_outstanding,
68                     lines => $credits->to_api
69                 }
70             }
71         );
72     }
73     catch {
74         $c->unhandled_exception($_);
75     };
76 }
77
78 =head3 add_credit
79
80 Controller function that handles adding a credit to a patron's account
81
82 =cut
83
84 sub add_credit {
85     my $c = shift->openapi->valid_input or return;
86
87     my $patron_id = $c->validation->param('patron_id');
88     my $patron    = Koha::Patrons->find($patron_id);
89     my $user      = $c->stash('koha.user');
90
91
92     unless ($patron) {
93         return $c->render( status => 404, openapi => { error => "Patron not found." } );
94     }
95
96     my $account = $patron->account;
97     my $body    = $c->validation->param('body');
98
99     return try {
100         my $credit_type = $body->{credit_type} || 'PAYMENT';    # default to 'PAYMENT'
101         my $amount = $body->{amount};                           # mandatory, validated by openapi
102
103         unless ( $amount > 0 ) {  # until we support newer JSON::Validator and thus minimumExclusive
104             Koha::Exceptions::BadParameter->throw( { parameter => 'amount' } );
105         }
106
107         # read the rest of the params
108         my $payment_type = $body->{payment_type};
109         my $description  = $body->{description};
110         my $note         = $body->{note};
111         my $library_id   = $body->{library_id};
112
113         my $credit = $account->add_credit(
114             {   amount       => $amount,
115                 type         => $credit_type,
116                 payment_type => $payment_type,
117                 description  => $description,
118                 note         => $note,
119                 user_id      => $user->id,
120                 interface    => 'api',
121                 library_id   => $library_id
122             }
123         );
124         $credit->discard_changes;
125
126         my $date = $body->{date};
127         $credit->date( $date )->store
128             if $date;
129
130         my $debits_ids = $body->{account_lines_ids};
131         my $debits;
132         $debits = Koha::Account::Lines->search({ accountlines_id => { -in => $debits_ids } })
133             if $debits_ids;
134
135         if ($debits) {
136             # pay them!
137             $credit = $credit->apply({ debits => [ $debits->as_list ], offset_type => 'payment' });
138         }
139
140         if ($credit->amountoutstanding != 0) {
141             my $outstanding_debits = $account->outstanding_debits;
142             $credit->apply({ debits => [ $outstanding_debits->as_list ], offset_type => 'payment' });
143         }
144
145         $credit->discard_changes;
146
147         return $c->render(
148             status  => 201,
149             openapi => $credit->to_api
150         );
151     }
152     catch {
153         $c->unhandled_exception($_);
154     };
155 }
156
157 1;