3 # This file is part of Koha.
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
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.
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.
20 use Test::More tests => 2;
25 use t::lib::TestBuilder;
28 use C4::Accounts qw(manualinvoice);
30 use Koha::Account::Line;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
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' );
39 my $remote_address = '127.0.0.1';
40 my $t = Test::Mojo->new('Koha::REST::V1');
42 subtest 'get_balance() tests' => sub {
46 $schema->storage->txn_begin;
48 my ( $patron, $session_id ) = create_user_and_session({ authorized => 0 });
49 my $library = $builder->build_object({ class => 'Koha::Libraries' });
50 my $patron_id = $patron->id;
51 my $account = $patron->account;
53 my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account");
54 $tx->req->cookies({ name => 'CGISESSID', value => $session_id });
55 $tx->req->env({ REMOTE_ADDR => '127.0.0.1' });
56 $t->request_ok($tx)->status_is(200)->json_is(
58 outstanding_debits => { total => 0, lines => [] },
59 outstanding_credits => { total => 0, lines => [] }
63 my $account_line_1 = Koha::Account::Line->new(
65 borrowernumber => $patron->borrowernumber,
68 description => "A description",
69 accounttype => "N", # New card
70 amountoutstanding => 50,
71 manager_id => $patron->borrowernumber,
72 branchcode => $library->id
75 $account_line_1->discard_changes;
77 my $account_line_2 = Koha::Account::Line->new(
79 borrowernumber => $patron->borrowernumber,
82 description => "A description",
83 accounttype => "N", # New card
84 amountoutstanding => 50.01,
85 manager_id => $patron->borrowernumber,
86 branchcode => $library->id
89 $account_line_2->discard_changes;
91 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
92 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
93 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
94 $t->request_ok($tx)->status_is(200)->json_is(
96 outstanding_debits => {
99 Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ),
100 Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON )
103 outstanding_credits => {
113 description => 'Finally!',
114 library_id => $patron->branchcode,
115 account_type => 'Pay',
116 offset_type => 'Payment'
120 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
121 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
122 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
123 $t->request_ok($tx)->status_is(200)->json_is(
125 outstanding_debits => { total => 0, lines => [] },
126 outstanding_credits => { total => 0, lines => [] }
131 my $credit_line = $account->add_credit(
132 { amount => 10, user_id => $patron->id, library_id => $library->id } );
133 # re-read from the DB
134 $credit_line->discard_changes;
135 $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" );
136 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
137 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
138 $t->request_ok($tx)->status_is(200)->json_is(
140 outstanding_debits => {
144 outstanding_credits => {
146 lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ]
151 $schema->storage->txn_rollback;
154 subtest 'add_credit() tests' => sub {
158 $schema->storage->txn_begin;
160 my ( $patron, $session_id ) = create_user_and_session( { authorized => 1 } );
161 my $library = $builder->build_object({ class => 'Koha::Libraries' });
162 my $patron_id = $patron->id;
163 my $account = $patron->account;
165 is( $account->outstanding_debits->count, 0, 'No outstanding debits for patron' );
166 is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' );
168 my $credit = { amount => 100 };
170 my $tx = $t->ua->build_tx(
171 POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
172 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
173 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
174 $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
176 my $outstanding_credits = $account->outstanding_credits;
177 is( $outstanding_credits->count, 1 );
178 is( $outstanding_credits->total_outstanding, -100 );
180 my $debit_1 = Koha::Account::Line->new(
181 { borrowernumber => $patron->borrowernumber,
184 description => "A description",
185 accounttype => "N", # New card
186 amountoutstanding => 10,
187 manager_id => $patron->borrowernumber,
190 my $debit_2 = Koha::Account::Line->new(
191 { borrowernumber => $patron->borrowernumber,
194 description => "A description",
195 accounttype => "N", # New card
196 amountoutstanding => 15,
197 manager_id => $patron->borrowernumber,
201 is( $account->outstanding_debits->total_outstanding, 25 );
202 $credit->{library_id} = $library->id;
203 $tx = $t->ua->build_tx(
204 POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
205 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
206 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
207 $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
209 my $account_line_id = $tx->res->json->{account_line_id};
210 is( Koha::Account::Lines->find($account_line_id)->branchcode,
211 $library->id, 'Library id is sored correctly' );
213 is( $account->outstanding_debits->total_outstanding,
214 0, "Debits have been cancelled automatically" );
216 my $debit_3 = Koha::Account::Line->new(
217 { borrowernumber => $patron->borrowernumber,
220 description => "A description",
221 accounttype => "N", # New card
222 amountoutstanding => 100,
223 manager_id => $patron->borrowernumber,
229 account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ]
232 $tx = $t->ua->build_tx(
233 POST => "/api/v1/patrons/$patron_id/account/credits" => json => $credit );
234 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
235 $tx->req->env( { REMOTE_ADDR => '127.0.0.1' } );
236 $t->request_ok($tx)->status_is(200)->json_has('/account_line_id');
238 my $outstanding_debits = $account->outstanding_debits;
239 is( $outstanding_debits->total_outstanding, 65 );
240 is( $outstanding_debits->count, 1 );
242 $schema->storage->txn_rollback;
245 sub create_user_and_session {
248 my $flags = ( $args->{authorized} ) ? 2**10 : 0;
250 my $patron = $builder->build_object(
252 class => 'Koha::Patrons',
259 # Create a session for the authorized user
260 my $session = C4::Auth::get_session('');
261 $session->param( 'number', $patron->id );
262 $session->param( 'id', $patron->userid );
263 $session->param( 'ip', '127.0.0.1' );
264 $session->param( 'lasttime', time() );
267 return ( $patron, $session->id );