|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2020 Koha Development team |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Test::More tests => 3; |
| 22 |
|
| 23 |
use Koha::Database; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
my $builder = t::lib::TestBuilder->new; |
| 28 |
my $schema = Koha::Database->new->schema; |
| 29 |
|
| 30 |
subtest 'manager' => sub { |
| 31 |
plan tests => 2; |
| 32 |
|
| 33 |
$schema->storage->txn_begin; |
| 34 |
|
| 35 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 36 |
my $action = $builder->build_object( |
| 37 |
{ |
| 38 |
class => 'Koha::Cash::Register::Actions', |
| 39 |
value => { manager_id => $manager->borrowernumber }, |
| 40 |
} |
| 41 |
); |
| 42 |
|
| 43 |
is( ref( $action->manager ), |
| 44 |
'Koha::Patron', |
| 45 |
'Koha::Cash::Register::Action->manager should return a Koha::Patron' ); |
| 46 |
|
| 47 |
is( $action->manager->id, $manager->id, |
| 48 |
'Koha::Cash::Registeri::Action->manager returns the correct Koha::Patron' |
| 49 |
); |
| 50 |
|
| 51 |
$schema->storage->txn_rollback; |
| 52 |
|
| 53 |
}; |
| 54 |
|
| 55 |
subtest 'register' => sub { |
| 56 |
plan tests => 2; |
| 57 |
|
| 58 |
$schema->storage->txn_begin; |
| 59 |
|
| 60 |
my $register = |
| 61 |
$builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 62 |
my $action = $builder->build_object( |
| 63 |
{ |
| 64 |
class => 'Koha::Cash::Register::Actions', |
| 65 |
value => { register_id => $register->id }, |
| 66 |
} |
| 67 |
); |
| 68 |
|
| 69 |
is( |
| 70 |
ref( $action->register ), |
| 71 |
'Koha::Cash::Register', |
| 72 |
'Koha::Cash::Register::Action->register should return a Koha::Cash::Register' |
| 73 |
); |
| 74 |
|
| 75 |
is( $action->register->id, $register->id, |
| 76 |
'Koha::Cash::Register::Action->register returns the correct Koha::Cash::Register' |
| 77 |
); |
| 78 |
|
| 79 |
$schema->storage->txn_rollback; |
| 80 |
|
| 81 |
}; |
| 82 |
|
| 83 |
subtest 'cashup_summary' => sub { |
| 84 |
plan tests => 8; |
| 85 |
|
| 86 |
$schema->storage->txn_begin; |
| 87 |
|
| 88 |
my $register = |
| 89 |
$builder->build_object( { class => 'Koha::Cash::Registers' } ); |
| 90 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 91 |
my $manager = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 92 |
|
| 93 |
# Transaction 1 |
| 94 |
my $debt1 = $builder->build_object( |
| 95 |
{ |
| 96 |
class => 'Koha::Account::Lines', |
| 97 |
value => { |
| 98 |
register_id => undef, |
| 99 |
amount => '1.00', |
| 100 |
amountoutstanding => '0.00', |
| 101 |
credit_type_code => undef, |
| 102 |
debit_type_code => 'OVERDUE', |
| 103 |
date => \'NOW() - INTERVAL 10 MINUTE' |
| 104 |
}, |
| 105 |
} |
| 106 |
); |
| 107 |
my $income1 = $builder->build_object( |
| 108 |
{ |
| 109 |
class => 'Koha::Account::Lines', |
| 110 |
value => { |
| 111 |
register_id => $register->id, |
| 112 |
amount => '-1.00', |
| 113 |
amountoutstanding => '0.00', |
| 114 |
credit_type_code => 'PAYMENT', |
| 115 |
debit_type_code => undef, |
| 116 |
date => \'NOW() - INTERVAL 5 MINUTE' |
| 117 |
}, |
| 118 |
} |
| 119 |
); |
| 120 |
$builder->build_object( |
| 121 |
{ |
| 122 |
class => 'Koha::Account::Offsets', |
| 123 |
value => { |
| 124 |
credit_id => $income1->accountlines_id, |
| 125 |
debit_id => $debt1->accountlines_id, |
| 126 |
amount => '1.00', |
| 127 |
type => 'Payment' |
| 128 |
}, |
| 129 |
} |
| 130 |
); |
| 131 |
|
| 132 |
# Transaction 2 |
| 133 |
my $debt2 = $builder->build_object( |
| 134 |
{ |
| 135 |
class => 'Koha::Account::Lines', |
| 136 |
value => { |
| 137 |
register_id => undef, |
| 138 |
amount => '1.00', |
| 139 |
amountoutstanding => '0.00', |
| 140 |
credit_type_code => undef, |
| 141 |
debit_type_code => 'ACCOUNT', |
| 142 |
date => \'NOW() - INTERVAL 3 MINUTE' |
| 143 |
}, |
| 144 |
} |
| 145 |
); |
| 146 |
my $debt3 = $builder->build_object( |
| 147 |
{ |
| 148 |
class => 'Koha::Account::Lines', |
| 149 |
value => { |
| 150 |
register_id => undef, |
| 151 |
amount => '0.50', |
| 152 |
amountoutstanding => '0.00', |
| 153 |
credit_type_code => undef, |
| 154 |
debit_type_code => 'LOST', |
| 155 |
date => \'NOW() - INTERVAL 3 MINUTE' |
| 156 |
}, |
| 157 |
} |
| 158 |
); |
| 159 |
my $income2 = $builder->build_object( |
| 160 |
{ |
| 161 |
class => 'Koha::Account::Lines', |
| 162 |
value => { |
| 163 |
register_id => $register->id, |
| 164 |
amount => '-1.50', |
| 165 |
amountoutstanding => '0.00', |
| 166 |
credit_type_code => 'PAYMENT', |
| 167 |
debit_type_code => undef, |
| 168 |
date => \'NOW() - INTERVAL 3 MINUTE' |
| 169 |
}, |
| 170 |
} |
| 171 |
); |
| 172 |
$builder->build_object( |
| 173 |
{ |
| 174 |
class => 'Koha::Account::Offsets', |
| 175 |
value => { |
| 176 |
credit_id => $income2->accountlines_id, |
| 177 |
debit_id => $debt2->accountlines_id, |
| 178 |
amount => '1.00', |
| 179 |
type => 'Payment' |
| 180 |
}, |
| 181 |
} |
| 182 |
); |
| 183 |
$builder->build_object( |
| 184 |
{ |
| 185 |
class => 'Koha::Account::Offsets', |
| 186 |
value => { |
| 187 |
credit_id => $income2->accountlines_id, |
| 188 |
debit_id => $debt3->accountlines_id, |
| 189 |
amount => '0.50', |
| 190 |
type => 'Payment' |
| 191 |
}, |
| 192 |
} |
| 193 |
); |
| 194 |
my $expected_income = [ |
| 195 |
{ |
| 196 |
debit_type_code => 'ACCOUNT', |
| 197 |
total => '1.000000', |
| 198 |
debit_type => { 'description' => 'Account creation fee' } |
| 199 |
}, |
| 200 |
{ |
| 201 |
debit_type_code => 'LOST', |
| 202 |
total => '0.500000', |
| 203 |
debit_type => { description => 'Lost item' } |
| 204 |
}, |
| 205 |
{ |
| 206 |
debit_type_code => 'OVERDUE', |
| 207 |
total => '1.000000', |
| 208 |
debit_type => { 'description' => 'Overdue fine' } |
| 209 |
} |
| 210 |
]; |
| 211 |
|
| 212 |
# Transaction 3 |
| 213 |
my $refund1 = $builder->build_object( |
| 214 |
{ |
| 215 |
class => 'Koha::Account::Lines', |
| 216 |
value => { |
| 217 |
register_id => undef, |
| 218 |
amount => '-0.50', |
| 219 |
amountoutstanding => '0.00', |
| 220 |
credit_type_code => 'REFUND', |
| 221 |
debit_type_code => undef, |
| 222 |
date => \'NOW() - INTERVAL 3 MINUTE' |
| 223 |
}, |
| 224 |
} |
| 225 |
); |
| 226 |
my $outgoing1 = $builder->build_object( |
| 227 |
{ |
| 228 |
class => 'Koha::Account::Lines', |
| 229 |
value => { |
| 230 |
register_id => $register->id, |
| 231 |
amount => '0.50', |
| 232 |
amountoutstanding => '0.00', |
| 233 |
credit_type_code => undef, |
| 234 |
debit_type_code => 'PAYOUT', |
| 235 |
date => \'NOW() - INTERVAL 3 MINUTE' |
| 236 |
}, |
| 237 |
} |
| 238 |
); |
| 239 |
$builder->build_object( |
| 240 |
{ |
| 241 |
class => 'Koha::Account::Offsets', |
| 242 |
value => { |
| 243 |
credit_id => $refund1->accountlines_id, |
| 244 |
debit_id => $outgoing1->accountlines_id, |
| 245 |
amount => '0.50', |
| 246 |
type => 'Refund' |
| 247 |
}, |
| 248 |
} |
| 249 |
); |
| 250 |
my $expected_outgoing = [ |
| 251 |
{ |
| 252 |
'total' => '0.500000', |
| 253 |
'credit_type' => { |
| 254 |
'description' => 'A refund applied to a patrons fine' |
| 255 |
}, |
| 256 |
'credit_type_code' => 'REFUND' |
| 257 |
} |
| 258 |
]; |
| 259 |
|
| 260 |
my $cashup1 = |
| 261 |
$register->add_cashup( { manager_id => $manager->id, amount => '2.00' } ); |
| 262 |
|
| 263 |
my $summary = $cashup1->cashup_summary; |
| 264 |
|
| 265 |
is( $summary->{from_date}, undef, |
| 266 |
"from_date is undefined if there is only one recorded" ); |
| 267 |
is( $summary->{to_date}, $cashup1->timestamp, |
| 268 |
"to_date equals cashup timestamp" ); |
| 269 |
is( ref( $summary->{income_transactions} ), |
| 270 |
'Koha::Account::Lines', |
| 271 |
"income_transactions contains Koha::Account::Lines" ); |
| 272 |
is( $summary->{income_transactions}->count, |
| 273 |
2, "income_transactions contains 2 transactions" ); |
| 274 |
is( ref( $summary->{outgoing_transactions} ), |
| 275 |
'Koha::Account::Lines', |
| 276 |
"outgoing_transactions contains Koha::Account::Lines" ); |
| 277 |
is( $summary->{outgoing_transactions}->count, |
| 278 |
1, "outgoing_transactions contains 1 transaction" ); |
| 279 |
is_deeply( $summary->{income}, $expected_income, |
| 280 |
"income arrayref is correct" ); |
| 281 |
is_deeply( $summary->{outgoing}, $expected_outgoing, |
| 282 |
"outgoing arrayref is correct" ); |
| 283 |
|
| 284 |
$schema->storage->txn_rollback; |
| 285 |
}; |
| 286 |
|
| 287 |
1; |