3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 2;
24 use t::lib::TestBuilder;
27 use Koha::Account::Lines;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34 my $t = Test::Mojo->new('Koha::REST::V1');
36 subtest 'get_balance() tests' => sub {
40 $schema->storage->txn_begin;
42 # Enable AccountAutoReconcile
43 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
45 my $patron = $builder->build_object({
46 class => 'Koha::Patrons',
47 value => { flags => 1 }
49 my $password = 'thePassword123';
50 $patron->set_password({ password => $password, skip_validation => 1 });
51 my $userid = $patron->userid;
53 my $library = $builder->build_object({ class => 'Koha::Libraries' });
54 my $patron_id = $patron->id;
55 my $account = $patron->account;
57 $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
61 outstanding_debits => { total => 0, lines => [] },
62 outstanding_credits => { total => 0, lines => [] }
66 my $debit_1 = $account->add_debit(
69 description => "A description",
71 user_id => $patron->borrowernumber,
72 library_id => $library->id,
76 $debit_1->discard_changes;
78 my $debit_2 = $account->add_debit(
81 description => "A description",
82 type => "NEW_CARD", # New card
83 user_id => $patron->borrowernumber,
84 library_id => $library->id,
88 $debit_2->discard_changes;
90 $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
94 outstanding_debits => {
101 outstanding_credits => {
111 description => 'Finally!',
112 library_id => $patron->branchcode,
113 account_type => 'PAYMENT'
117 $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
121 outstanding_debits => { total => 0, lines => [] },
122 outstanding_credits => { total => 0, lines => [] }
127 my $credit_line = $account->add_credit(
128 { amount => 10, user_id => $patron->id, library_id => $library->id, interface => 'test' } );
129 # re-read from the DB
130 $credit_line->discard_changes;
132 $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
136 outstanding_debits => {
140 outstanding_credits => {
142 lines => [ $credit_line->to_api ]
147 # Accountline without manager_id (happens with fines.pl cron for example)
148 my $debit_3 = $account->add_debit(
151 description => "A description",
152 type => "NEW_CARD", # New card
154 library_id => $library->id,
158 $debit_3->discard_changes;
160 $t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account")
164 outstanding_debits => {
170 outstanding_credits => {
172 lines => [ $credit_line->to_api ]
177 $schema->storage->txn_rollback;
180 subtest 'add_credit() tests' => sub {
184 $schema->storage->txn_begin;
186 my $patron = $builder->build_object({
187 class => 'Koha::Patrons',
188 value => { flags => 1 }
190 my $password = 'thePassword123';
191 $patron->set_password({ password => $password, skip_validation => 1 });
192 my $userid = $patron->userid;
194 my $library = $builder->build_object({ class => 'Koha::Libraries' });
195 my $patron_id = $patron->id;
196 my $account = $patron->account;
198 is( $account->outstanding_debits->count, 0, 'No outstanding debits for patron' );
199 is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' );
201 my $credit = { amount => 100 };
203 my $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit)
204 ->status_is(201)->tx->res->json;
206 is_deeply( $ret, Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, 'Line returned correctly' );
208 my $outstanding_credits = $account->outstanding_credits;
209 is( $outstanding_credits->count, 1 );
210 is( $outstanding_credits->total_outstanding, -100 );
212 my $debit_1 = $account->add_debit(
214 description => "A description",
216 user_id => $patron->borrowernumber,
220 my $debit_2 = $account->add_debit(
222 description => "A description",
224 user_id => $patron->borrowernumber,
229 is( $account->outstanding_debits->total_outstanding, 25 );
230 $credit->{library_id} = $library->id;
232 $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit)
236 is_deeply( $ret, Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, 'Line returned correctly' );
238 my $account_line_id = $t->tx->res->json->{account_line_id};
239 is( Koha::Account::Lines->find($account_line_id)->branchcode,
240 $library->id, 'Library id is sored correctly' );
242 is( $account->outstanding_debits->total_outstanding,
243 0, "Debits have been cancelled automatically" );
245 my $debit_3 = $account->add_debit(
247 description => "A description",
249 user_id => $patron->borrowernumber,
256 account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ]
259 $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit)
263 is_deeply( $ret, Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, 'Line returned correctly' );
265 my $outstanding_debits = $account->outstanding_debits;
266 is( $outstanding_debits->total_outstanding, 65 );
267 is( $outstanding_debits->count, 1 );
269 $schema->storage->txn_rollback;