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