|
Lines 17-23
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 4; |
20 |
use Test::More tests => 5; |
| 21 |
|
21 |
|
| 22 |
use Test::Mojo; |
22 |
use Test::Mojo; |
| 23 |
|
23 |
|
|
Lines 42-76
subtest 'get_balance() tests' => sub {
Link Here
|
| 42 |
# Enable AccountAutoReconcile |
42 |
# Enable AccountAutoReconcile |
| 43 |
t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 ); |
43 |
t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 ); |
| 44 |
|
44 |
|
| 45 |
my $patron = $builder->build_object({ |
45 |
my $patron = $builder->build_object( |
| 46 |
class => 'Koha::Patrons', |
46 |
{ |
| 47 |
value => { flags => 1 } |
47 |
class => 'Koha::Patrons', |
| 48 |
}); |
48 |
value => { flags => 1 } |
|
|
49 |
} |
| 50 |
); |
| 49 |
my $password = 'thePassword123'; |
51 |
my $password = 'thePassword123'; |
| 50 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
52 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 51 |
my $userid = $patron->userid; |
53 |
my $userid = $patron->userid; |
| 52 |
|
54 |
|
| 53 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
55 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 54 |
my $patron_id = $patron->id; |
56 |
my $patron_id = $patron->id; |
| 55 |
my $account = $patron->account; |
57 |
my $account = $patron->account; |
| 56 |
|
58 |
|
| 57 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
59 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
| 58 |
->status_is(200) |
60 |
->status_is(200)->json_is( |
| 59 |
->json_is( |
61 |
{ |
| 60 |
{ balance => 0.00, |
62 |
balance => 0.00, |
| 61 |
outstanding_debits => { total => 0, lines => [] }, |
63 |
outstanding_debits => { total => 0, lines => [] }, |
| 62 |
outstanding_credits => { total => 0, lines => [] } |
64 |
outstanding_credits => { total => 0, lines => [] } |
| 63 |
} |
65 |
} |
| 64 |
); |
66 |
); |
| 65 |
|
67 |
|
| 66 |
my $debit_1 = $account->add_debit( |
68 |
my $debit_1 = $account->add_debit( |
| 67 |
{ |
69 |
{ |
| 68 |
amount => 50, |
70 |
amount => 50, |
| 69 |
description => "A description", |
71 |
description => "A description", |
| 70 |
type => "NEW_CARD", |
72 |
type => "NEW_CARD", |
| 71 |
user_id => $patron->borrowernumber, |
73 |
user_id => $patron->borrowernumber, |
| 72 |
library_id => $library->id, |
74 |
library_id => $library->id, |
| 73 |
interface => 'test', |
75 |
interface => 'test', |
| 74 |
} |
76 |
} |
| 75 |
)->store(); |
77 |
)->store(); |
| 76 |
$debit_1->discard_changes; |
78 |
$debit_1->discard_changes; |
|
Lines 79-85
subtest 'get_balance() tests' => sub {
Link Here
|
| 79 |
{ |
81 |
{ |
| 80 |
amount => 50.01, |
82 |
amount => 50.01, |
| 81 |
description => "A description", |
83 |
description => "A description", |
| 82 |
type => "NEW_CARD", # New card |
84 |
type => "NEW_CARD", # New card |
| 83 |
user_id => $patron->borrowernumber, |
85 |
user_id => $patron->borrowernumber, |
| 84 |
library_id => $library->id, |
86 |
library_id => $library->id, |
| 85 |
interface => 'test', |
87 |
interface => 'test', |
|
Lines 88-112
subtest 'get_balance() tests' => sub {
Link Here
|
| 88 |
$debit_2->discard_changes; |
90 |
$debit_2->discard_changes; |
| 89 |
|
91 |
|
| 90 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
92 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
| 91 |
->status_is(200) |
93 |
->status_is(200)->json_is( |
| 92 |
->json_is( |
94 |
{ |
| 93 |
{ balance => 100.01, |
95 |
balance => 100.01, |
| 94 |
outstanding_debits => { |
96 |
outstanding_debits => { |
| 95 |
total => 100.01, |
97 |
total => 100.01, |
| 96 |
lines => [ |
98 |
lines => [ $debit_1->to_api, $debit_2->to_api ] |
| 97 |
$debit_1->to_api, |
|
|
| 98 |
$debit_2->to_api |
| 99 |
] |
| 100 |
}, |
99 |
}, |
| 101 |
outstanding_credits => { |
100 |
outstanding_credits => { |
| 102 |
total => 0, |
101 |
total => 0, |
| 103 |
lines => [] |
102 |
lines => [] |
| 104 |
} |
103 |
} |
| 105 |
} |
104 |
} |
| 106 |
); |
105 |
); |
| 107 |
|
106 |
|
| 108 |
$account->pay( |
107 |
$account->pay( |
| 109 |
{ amount => 100.01, |
108 |
{ |
|
|
109 |
amount => 100.01, |
| 110 |
note => 'He paid!', |
110 |
note => 'He paid!', |
| 111 |
description => 'Finally!', |
111 |
description => 'Finally!', |
| 112 |
library_id => $patron->branchcode, |
112 |
library_id => $patron->branchcode, |
|
Lines 115-138
subtest 'get_balance() tests' => sub {
Link Here
|
| 115 |
); |
115 |
); |
| 116 |
|
116 |
|
| 117 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
117 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
| 118 |
->status_is(200) |
118 |
->status_is(200)->json_is( |
| 119 |
->json_is( |
119 |
{ |
| 120 |
{ balance => 0, |
120 |
balance => 0, |
| 121 |
outstanding_debits => { total => 0, lines => [] }, |
121 |
outstanding_debits => { total => 0, lines => [] }, |
| 122 |
outstanding_credits => { total => 0, lines => [] } |
122 |
outstanding_credits => { total => 0, lines => [] } |
| 123 |
} |
123 |
} |
| 124 |
); |
124 |
); |
| 125 |
|
125 |
|
| 126 |
# add a credit |
126 |
# add a credit |
| 127 |
my $credit_line = $account->add_credit( |
127 |
my $credit_line = $account->add_credit( |
| 128 |
{ amount => 10, user_id => $patron->id, library_id => $library->id, interface => 'test' } ); |
128 |
{ |
|
|
129 |
amount => 10, |
| 130 |
user_id => $patron->id, |
| 131 |
library_id => $library->id, |
| 132 |
interface => 'test' |
| 133 |
} |
| 134 |
); |
| 135 |
|
| 129 |
# re-read from the DB |
136 |
# re-read from the DB |
| 130 |
$credit_line->discard_changes; |
137 |
$credit_line->discard_changes; |
| 131 |
|
138 |
|
| 132 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
139 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
| 133 |
->status_is(200) |
140 |
->status_is(200)->json_is( |
| 134 |
->json_is( |
141 |
{ |
| 135 |
{ balance => -10, |
142 |
balance => -10, |
| 136 |
outstanding_debits => { |
143 |
outstanding_debits => { |
| 137 |
total => 0, |
144 |
total => 0, |
| 138 |
lines => [] |
145 |
lines => [] |
|
Lines 142-155
subtest 'get_balance() tests' => sub {
Link Here
|
| 142 |
lines => [ $credit_line->to_api ] |
149 |
lines => [ $credit_line->to_api ] |
| 143 |
} |
150 |
} |
| 144 |
} |
151 |
} |
| 145 |
); |
152 |
); |
| 146 |
|
153 |
|
| 147 |
# Accountline without manager_id (happens with fines.pl cron for example) |
154 |
# Accountline without manager_id (happens with fines.pl cron for example) |
| 148 |
my $debit_3 = $account->add_debit( |
155 |
my $debit_3 = $account->add_debit( |
| 149 |
{ |
156 |
{ |
| 150 |
amount => 50, |
157 |
amount => 50, |
| 151 |
description => "A description", |
158 |
description => "A description", |
| 152 |
type => "NEW_CARD", # New card |
159 |
type => "NEW_CARD", # New card |
| 153 |
user_id => undef, |
160 |
user_id => undef, |
| 154 |
library_id => $library->id, |
161 |
library_id => $library->id, |
| 155 |
interface => 'test', |
162 |
interface => 'test', |
|
Lines 158-178
subtest 'get_balance() tests' => sub {
Link Here
|
| 158 |
$debit_3->discard_changes; |
165 |
$debit_3->discard_changes; |
| 159 |
|
166 |
|
| 160 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
167 |
$t->get_ok("//$userid:$password@/api/v1/patrons/$patron_id/account") |
| 161 |
->status_is(200) |
168 |
->status_is(200)->json_is( |
| 162 |
->json_is( |
169 |
{ |
| 163 |
{ balance => 40.00, |
170 |
balance => 40.00, |
| 164 |
outstanding_debits => { |
171 |
outstanding_debits => { |
| 165 |
total => 50.00, |
172 |
total => 50.00, |
| 166 |
lines => [ |
173 |
lines => [ $debit_3->to_api ] |
| 167 |
$debit_3->to_api |
|
|
| 168 |
] |
| 169 |
}, |
174 |
}, |
| 170 |
outstanding_credits => { |
175 |
outstanding_credits => { |
| 171 |
total => -10, |
176 |
total => -10, |
| 172 |
lines => [ $credit_line->to_api ] |
177 |
lines => [ $credit_line->to_api ] |
| 173 |
} |
178 |
} |
| 174 |
} |
179 |
} |
| 175 |
); |
180 |
); |
| 176 |
|
181 |
|
| 177 |
$schema->storage->txn_rollback; |
182 |
$schema->storage->txn_rollback; |
| 178 |
}; |
183 |
}; |
|
Lines 183-216
subtest 'add_credit() tests' => sub {
Link Here
|
| 183 |
|
188 |
|
| 184 |
$schema->storage->txn_begin; |
189 |
$schema->storage->txn_begin; |
| 185 |
|
190 |
|
| 186 |
my $patron = $builder->build_object({ |
191 |
my $patron = $builder->build_object( |
| 187 |
class => 'Koha::Patrons', |
192 |
{ |
| 188 |
value => { flags => 1 } |
193 |
class => 'Koha::Patrons', |
| 189 |
}); |
194 |
value => { flags => 1 } |
|
|
195 |
} |
| 196 |
); |
| 190 |
my $password = 'thePassword123'; |
197 |
my $password = 'thePassword123'; |
| 191 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
198 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 192 |
my $userid = $patron->userid; |
199 |
my $userid = $patron->userid; |
| 193 |
|
200 |
|
| 194 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
201 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 195 |
my $patron_id = $patron->id; |
202 |
my $patron_id = $patron->id; |
| 196 |
my $account = $patron->account; |
203 |
my $account = $patron->account; |
| 197 |
|
204 |
|
| 198 |
is( $account->outstanding_debits->count, 0, 'No outstanding debits for patron' ); |
205 |
is( $account->outstanding_debits->count, |
| 199 |
is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' ); |
206 |
0, 'No outstanding debits for patron' ); |
|
|
207 |
is( $account->outstanding_credits->count, |
| 208 |
0, 'No outstanding credits for patron' ); |
| 200 |
|
209 |
|
| 201 |
my $credit = { amount => 100 }; |
210 |
my $credit = { amount => 100 }; |
| 202 |
|
211 |
|
| 203 |
my $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit) |
212 |
my $ret = $t->post_ok( |
| 204 |
->status_is(201)->tx->res->json; |
213 |
"//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => |
|
|
214 |
json => $credit )->status_is(201)->tx->res->json; |
| 205 |
|
215 |
|
| 206 |
is_deeply( $ret, Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, 'Line returned correctly' ); |
216 |
is_deeply( |
|
|
217 |
$ret, |
| 218 |
Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, |
| 219 |
'Line returned correctly' |
| 220 |
); |
| 207 |
|
221 |
|
| 208 |
my $outstanding_credits = $account->outstanding_credits; |
222 |
my $outstanding_credits = $account->outstanding_credits; |
| 209 |
is( $outstanding_credits->count, 1 ); |
223 |
is( $outstanding_credits->count, 1 ); |
| 210 |
is( $outstanding_credits->total_outstanding, -100 ); |
224 |
is( $outstanding_credits->total_outstanding, -100 ); |
| 211 |
|
225 |
|
| 212 |
my $debit_1 = $account->add_debit( |
226 |
my $debit_1 = $account->add_debit( |
| 213 |
{ amount => 10, |
227 |
{ |
|
|
228 |
amount => 10, |
| 214 |
description => "A description", |
229 |
description => "A description", |
| 215 |
type => "NEW_CARD", |
230 |
type => "NEW_CARD", |
| 216 |
user_id => $patron->borrowernumber, |
231 |
user_id => $patron->borrowernumber, |
|
Lines 218-224
subtest 'add_credit() tests' => sub {
Link Here
|
| 218 |
} |
233 |
} |
| 219 |
)->store(); |
234 |
)->store(); |
| 220 |
my $debit_2 = $account->add_debit( |
235 |
my $debit_2 = $account->add_debit( |
| 221 |
{ amount => 15, |
236 |
{ |
|
|
237 |
amount => 15, |
| 222 |
description => "A description", |
238 |
description => "A description", |
| 223 |
type => "NEW_CARD", |
239 |
type => "NEW_CARD", |
| 224 |
user_id => $patron->borrowernumber, |
240 |
user_id => $patron->borrowernumber, |
|
Lines 229-239
subtest 'add_credit() tests' => sub {
Link Here
|
| 229 |
is( $account->outstanding_debits->total_outstanding, 25 ); |
245 |
is( $account->outstanding_debits->total_outstanding, 25 ); |
| 230 |
$credit->{library_id} = $library->id; |
246 |
$credit->{library_id} = $library->id; |
| 231 |
|
247 |
|
| 232 |
$ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit) |
248 |
$ret = $t->post_ok( |
| 233 |
->status_is(201) |
249 |
"//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => |
| 234 |
->tx->res->json; |
250 |
json => $credit )->status_is(201)->tx->res->json; |
| 235 |
|
251 |
|
| 236 |
is_deeply( $ret, Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, 'Line returned correctly' ); |
252 |
is_deeply( |
|
|
253 |
$ret, |
| 254 |
Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, |
| 255 |
'Line returned correctly' |
| 256 |
); |
| 237 |
|
257 |
|
| 238 |
my $account_line_id = $t->tx->res->json->{account_line_id}; |
258 |
my $account_line_id = $t->tx->res->json->{account_line_id}; |
| 239 |
is( Koha::Account::Lines->find($account_line_id)->branchcode, |
259 |
is( Koha::Account::Lines->find($account_line_id)->branchcode, |
|
Lines 243-249
subtest 'add_credit() tests' => sub {
Link Here
|
| 243 |
0, "Debits have been cancelled automatically" ); |
263 |
0, "Debits have been cancelled automatically" ); |
| 244 |
|
264 |
|
| 245 |
my $debit_3 = $account->add_debit( |
265 |
my $debit_3 = $account->add_debit( |
| 246 |
{ amount => 100, |
266 |
{ |
|
|
267 |
amount => 100, |
| 247 |
description => "A description", |
268 |
description => "A description", |
| 248 |
type => "NEW_CARD", |
269 |
type => "NEW_CARD", |
| 249 |
user_id => $patron->borrowernumber, |
270 |
user_id => $patron->borrowernumber, |
|
Lines 256-266
subtest 'add_credit() tests' => sub {
Link Here
|
| 256 |
account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ] |
277 |
account_lines_ids => [ $debit_1->id, $debit_2->id, $debit_3->id ] |
| 257 |
}; |
278 |
}; |
| 258 |
|
279 |
|
| 259 |
$ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => json => $credit) |
280 |
$ret = $t->post_ok( |
| 260 |
->status_is(201) |
281 |
"//$userid:$password@/api/v1/patrons/$patron_id/account/credits" => |
| 261 |
->tx->res->json; |
282 |
json => $credit )->status_is(201)->tx->res->json; |
| 262 |
|
283 |
|
| 263 |
is_deeply( $ret, Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, 'Line returned correctly' ); |
284 |
is_deeply( |
|
|
285 |
$ret, |
| 286 |
Koha::Account::Lines->find( $ret->{account_line_id} )->to_api, |
| 287 |
'Line returned correctly' |
| 288 |
); |
| 264 |
|
289 |
|
| 265 |
my $outstanding_debits = $account->outstanding_debits; |
290 |
my $outstanding_debits = $account->outstanding_debits; |
| 266 |
is( $outstanding_debits->total_outstanding, 65 ); |
291 |
is( $outstanding_debits->total_outstanding, 65 ); |
|
Lines 342-347
subtest 'list_debits() test' => sub {
Link Here
|
| 342 |
->tx->res->json; |
367 |
->tx->res->json; |
| 343 |
|
368 |
|
| 344 |
is(140, $ret->[0]->{amount} + $ret->[1]->{amount}, 'Total debits are 140'); |
369 |
is(140, $ret->[0]->{amount} + $ret->[1]->{amount}, 'Total debits are 140'); |
|
|
370 |
}; |
| 371 |
|
| 372 |
subtest 'add_debit() tests' => sub { |
| 373 |
|
| 374 |
plan tests => 13; |
| 375 |
|
| 376 |
$schema->storage->txn_begin; |
| 377 |
|
| 378 |
my $patron = $builder->build_object( |
| 379 |
{ |
| 380 |
class => 'Koha::Patrons', |
| 381 |
value => { flags => 1 } |
| 382 |
} |
| 383 |
); |
| 384 |
my $password = 'thePassword123'; |
| 385 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 386 |
my $userid = $patron->userid; |
| 387 |
|
| 388 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 389 |
my $patron_id = $patron->id; |
| 390 |
my $account = $patron->account; |
| 391 |
|
| 392 |
is( $account->outstanding_debits->count, |
| 393 |
0, 'No outstanding debits for patron' ); |
| 394 |
is( $account->outstanding_credits->count, |
| 395 |
0, 'No outstanding credits for patron' ); |
| 396 |
|
| 397 |
my $debit = { |
| 398 |
amount => 100, |
| 399 |
description => "A description", |
| 400 |
debit_type => "NEW_CARD", |
| 401 |
user_id => $patron->borrowernumber, |
| 402 |
interface => 'test', |
| 403 |
library_id => $library->id, |
| 404 |
}; |
| 405 |
|
| 406 |
my $ret = $t->post_ok( |
| 407 |
"//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => |
| 408 |
json => $debit )->status_is(201)->tx->res->json; |
| 409 |
my $account_line = Koha::Account::Lines->find( $ret->{account_line_id} ); |
| 410 |
|
| 411 |
is_deeply( $ret, $account_line->to_api, 'Line returned correctly' ); |
| 412 |
|
| 413 |
is( $account_line->branchcode, |
| 414 |
$library->id, 'Library id is sored correctly' ); |
| 415 |
|
| 416 |
my $outstanding_debits = $account->outstanding_debits; |
| 417 |
is( $outstanding_debits->count, 1 ); |
| 418 |
is( $outstanding_debits->total_outstanding, 100 ); |
| 419 |
|
| 420 |
my $credit_1 = $account->add_credit( |
| 421 |
{ |
| 422 |
amount => 10, |
| 423 |
interface => 'test', |
| 424 |
} |
| 425 |
)->store()->apply( { debits => [$account_line] } ); |
| 426 |
my $credit_2 = $account->add_credit( |
| 427 |
{ |
| 428 |
amount => 15, |
| 429 |
interface => 'test' |
| 430 |
} |
| 431 |
)->store()->apply( { debits => [$account_line] } ); |
| 432 |
|
| 433 |
is( $account->outstanding_credits->total_outstanding, 0 ); |
| 434 |
is( $account->outstanding_debits->total_outstanding, |
| 435 |
75, "Credits partially cancelled debit" ); |
| 436 |
|
| 437 |
my $account_line_id = $ret->{account_line_id}; |
| 438 |
|
| 439 |
$t->post_ok( |
| 440 |
"//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => |
| 441 |
json => $debit )->status_is(201); |
| 442 |
|
| 443 |
is( $account->outstanding_debits->total_outstanding, |
| 444 |
175, "Debit added to total outstanding debits" ); |
| 345 |
|
445 |
|
| 346 |
$schema->storage->txn_rollback; |
446 |
$schema->storage->txn_rollback; |
| 347 |
}; |
447 |
}; |
| 348 |
- |
|
|