View | Details | Raw Unified | Return to bug 20997
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Account/Lines.t (-4 / +102 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 3;
23
use Test::Exception;
23
24
25
use Koha::Account;
24
use Koha::Account::Lines;
26
use Koha::Account::Lines;
27
use Koha::Account::Offsets;
25
use Koha::Items;
28
use Koha::Items;
26
29
27
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
Lines 29-35 use t::lib::TestBuilder; Link Here
29
my $schema = Koha::Database->new->schema;
32
my $schema = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
33
my $builder = t::lib::TestBuilder->new;
31
34
32
subtest 'item' => sub {
35
subtest 'item() tests' => sub {
33
36
34
    plan tests => 2;
37
    plan tests => 2;
35
38
Lines 63-69 subtest 'item' => sub { Link Here
63
    $schema->storage->txn_rollback;
66
    $schema->storage->txn_rollback;
64
};
67
};
65
68
66
subtest 'total_outstanding' => sub {
69
subtest 'total_outstanding() tests' => sub {
67
70
68
    plan tests => 5;
71
    plan tests => 5;
69
72
Lines 128-130 subtest 'total_outstanding' => sub { Link Here
128
131
129
    $schema->storage->txn_rollback;
132
    $schema->storage->txn_rollback;
130
};
133
};
131
- 
134
135
subtest 'is_credit() tests' => sub {
136
137
    plan tests => 2;
138
139
    $schema->storage->txn_begin;
140
141
    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
142
    my $account = $patron->account;
143
144
    my $credit = $account->add_credit({ amount => 100, user_id => $patron->id });
145
146
    ok( $credit->is_credit, 'is_credit detects credits' );
147
148
    my $debit = Koha::Account::Line->new(
149
    {
150
        borrowernumber => $patron->id,
151
        accounttype    => "F",
152
        amount         => 10,
153
    })->store;
154
155
    ok( !$debit->is_credit, 'is_credit() detects debits' );
156
157
    $schema->storage->txn_rollback;
158
};
159
160
subtest 'apply() tests' => sub {
161
162
    plan tests => 12;
163
164
    $schema->storage->txn_begin;
165
166
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
167
    my $account = $patron->account;
168
169
    my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } );
170
171
    my $debit_1 = Koha::Account::Line->new(
172
        {   borrowernumber    => $patron->id,
173
            accounttype       => "F",
174
            amount            => 10,
175
            amountoutstanding => 10
176
        }
177
    )->store;
178
179
    my $debit_2 = Koha::Account::Line->new(
180
        {   borrowernumber    => $patron->id,
181
            accounttype       => "F",
182
            amount            => 100,
183
            amountoutstanding => 100
184
        }
185
    )->store;
186
187
    $credit->discard_changes;
188
    $debit_1->discard_changes;
189
190
    my $remaining_credit = $credit->apply( { debit => $debit_1, offset_type => 'Manual Credit' } );
191
    is( $remaining_credit, 90, 'Remaining credit is correctly calculated' );
192
    $credit->discard_changes;
193
    is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' );
194
195
    # re-read debit info
196
    $debit_1->discard_changes;
197
    is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' );
198
199
    my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } );
200
    is( $offsets->count, 1, 'Only one offset is generated' );
201
    my $THE_offest = $offsets->next;
202
    is( $THE_offest->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' );
203
    is( $THE_offest->type, 'Manual Credit', 'Passed type stored correctly' );
204
205
    $remaining_credit = $credit->apply( { debit => $debit_2, offset_type => 'Manual Credit' } );
206
    is( $remaining_credit, 0, 'No remaining credit left' );
207
    $credit->discard_changes;
208
    is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' );
209
    $debit_2->discard_changes;
210
    is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' );
211
212
    throws_ok
213
        { $debit_1->apply({ debit => $credit }); }
214
        'Koha::Exceptions::Account::IsNotCredit',
215
        '->apply() can only be used with credits';
216
217
    throws_ok
218
        { $credit->apply({ debit => $credit }); }
219
        'Koha::Exceptions::Account::IsNotDebit',
220
        '->apply() can only be applied to credits';
221
222
    throws_ok
223
        { $credit->apply({ debit => $debit_1 }); }
224
        'Koha::Exceptions::Account::NoAvailableCredit',
225
        '->apply() can only be used with outstanding credits';
226
227
228
    $schema->storage->txn_rollback;
229
};

Return to bug 20997