Bug 20944: Add route to add credits to a patron's account
[koha.git] / t / db_dependent / api / v1 / patrons_accounts.t
1 #!/usr/bin/perl
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 Test::More tests => 2;
21
22 use Test::Mojo;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Accounts qw(manualinvoice);
29 use C4::Auth;
30 use Koha::Account::Line;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
36 # this affects the other REST api tests
37 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
38
39 my $remote_address = '127.0.0.1';
40 my $t              = Test::Mojo->new('Koha::REST::V1');
41
42 subtest 'get_balance() tests' => sub {
43
44     plan tests => 12;
45
46     $schema->storage->txn_begin;
47
48     my ( $patron, $session_id ) = create_user_and_session({ authorized => 0 });
49     my $patron_id  = $patron->id;
50     my $account = $patron->account;
51
52     my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
53     $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
54     $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
55     $t->request_ok($tx)->status_is(200)->json_is(
56         {   balance             => 0.00,
57             outstanding_debits  => { total => 0, lines => [] },
58             outstanding_credits => { total => 0, lines => [] }
59         }
60     );
61
62     my $account_line_1 = Koha::Account::Line->new(
63         {
64             borrowernumber    => $patron->borrowernumber,
65             date              => \'NOW()',
66             amount            => 50,
67             description       => "A description",
68             accounttype       => "N", # New card
69             amountoutstanding => 50,
70             manager_id        => $patron->borrowernumber,
71         }
72     )->store();
73     $account_line_1->discard_changes;
74
75     my $account_line_2 = Koha::Account::Line->new(
76         {
77             borrowernumber    => $patron->borrowernumber,
78             date              => \'NOW()',
79             amount            => 50.01,
80             description       => "A description",
81             accounttype       => "N", # New card
82             amountoutstanding => 50.01,
83             manager_id        => $patron->borrowernumber,
84         }
85     )->store();
86     $account_line_2->discard_changes;
87
88     $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
89     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
90     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
91     $t->request_ok($tx)->status_is(200)->json_is(
92         {   balance            => 100.01,
93             outstanding_debits => {
94                 total => 100.01,
95                 lines => [
96                     Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
97                     Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
98                 ]
99             },
100             outstanding_credits => {
101                 total => 0,
102                 lines => []
103             }
104         }
105     );
106
107     $account->pay(
108         {   amount       => 100.01,
109             note         => 'He paid!',
110             description  => 'Finally!',
111             library_id   => $patron->branchcode,
112             account_type => 'Pay',
113             offset_type  => 'Payment'
114         }
115     );
116
117     $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
118     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
119     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
120     $t->request_ok($tx)->status_is(200)->json_is(
121         {   balance             => 0,
122             outstanding_debits  => { total => 0, lines => [] },
123             outstanding_credits => { total => 0, lines => [] }
124         }
125     );
126
127     # add a credit
128     my $credit_line = $account->add_credit({ amount => 10, user_id => $patron->id });
129     # re-read from the DB
130     $credit_line->discard_changes;
131     $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
132     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
133     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
134     $t->request_ok($tx)->status_is(200)->json_is(
135         {   balance            => -10,
136             outstanding_debits => {
137                 total => 0,
138                 lines => []
139             },
140             outstanding_credits => {
141                 total => -10,
142                 lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ]
143             }
144         }
145     );
146
147     $schema->storage->txn_rollback;
148 };
149
150 subtest 'add_credit() tests' => sub {
151
152     plan tests => 17;
153
154     $schema->storage->txn_begin;
155
156     my ( $patron, $session_id ) = create_user_and_session( { authorized => 1 } );
157     my $patron_id = $patron->id;
158     my $account   = $patron->account;
159
160     is( $account->outstanding_debits->count,  0, 'No outstanding debits for patron' );
161     is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' );
162
163     my $credit = { amount => 100 };
164
165     my $tx = $t->ua->build_tx(
166         POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
167     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
168     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
169     $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
170
171     my $outstanding_credits = $account->outstanding_credits;
172     is( $outstanding_credits->count,             1 );
173     is( $outstanding_credits->total_outstanding, -100 );
174
175     my $debit_1 = Koha::Account::Line->new(
176         {   borrowernumber    => $patron->borrowernumber,
177             date              => \'NOW()',
178             amount            => 10,
179             description       => "A description",
180             accounttype       => "N",                       # New card
181             amountoutstanding => 10,
182             manager_id        => $patron->borrowernumber,
183         }
184     )->store();
185     my $debit_2 = Koha::Account::Line->new(
186         {   borrowernumber    => $patron->borrowernumber,
187             date              => \'NOW()',
188             amount            => 15,
189             description       => "A description",
190             accounttype       => "N",                       # New card
191             amountoutstanding => 15,
192             manager_id        => $patron->borrowernumber,
193         }
194     )->store();
195
196     is( $account->outstanding_debits->total_outstanding, 25 );
197     $tx = $t->ua->build_tx(
198         POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
199     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
200     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
201     $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
202
203     is( $account->outstanding_debits->total_outstanding,
204         0, "Debits have been cancelled automatically" );
205
206     my $debit_3 = Koha::Account::Line->new(
207         {   borrowernumber    => $patron->borrowernumber,
208             date              => \'NOW()',
209             amount            => 100,
210             description       => "A description",
211             accounttype       => "N",                       # New card
212             amountoutstanding => 100,
213             manager_id        => $patron->borrowernumber,
214         }
215     )->store();
216
217     $credit = {
218         amount            => 35,
219         account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ]
220     };
221
222     $tx = $t->ua->build_tx(
223         POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
224     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
225     $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
226     $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
227
228     my $outstanding_debits = $account->outstanding_debits;
229     is( $outstanding_debits->total_outstanding, 65 );
230     is( $outstanding_debits->count,             1 );
231
232     $schema->storage->txn_rollback;
233 };
234
235 sub create_user_and_session {
236
237     my $args  = shift;
238     my $flags = ( $args->{authorized} ) ? 2**10 : 0;
239
240     my $patron = $builder->build_object(
241         {
242             class => 'Koha::Patrons',
243             value  => {
244                 flags         => $flags
245             }
246         }
247     );
248
249     # Create a session for the authorized user
250     my $session = C4::Auth::get_session('');
251     $session->param( 'number',   $patron->id );
252     $session->param( 'id',       $patron->userid );
253     $session->param( 'ip',       '127.0.0.1' );
254     $session->param( 'lasttime', time() );
255     $session->flush;
256
257     return ( $patron, $session->id );
258 }