Bug 20942: Split debit and credit lines
[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 => 1;
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_id, $session_id ) = create_user_and_session({ authorized => 0 });
49     my $patron  = Koha::Patrons->find($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 sub create_user_and_session {
151
152     my $args  = shift;
153     my $flags = ( $args->{authorized} ) ? 16 : 0;
154
155     my $user = $builder->build(
156         {
157             source => 'Borrower',
158             value  => {
159                 flags => $flags,
160                 gonenoaddress => 0,
161                 lost => 0,
162                 email => 'nobody@example.com',
163                 emailpro => 'nobody@example.com',
164                 B_email => 'nobody@example.com'
165             }
166         }
167     );
168
169     # Create a session for the authorized user
170     my $session = C4::Auth::get_session('');
171     $session->param( 'number',   $user->{borrowernumber} );
172     $session->param( 'id',       $user->{userid} );
173     $session->param( 'ip',       '127.0.0.1' );
174     $session->param( 'lasttime', time() );
175     $session->flush;
176
177     return ( $user->{borrowernumber}, $session->id );
178 }