|
Lines 19-65
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 1; |
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; |
| 28 |
|
31 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
32 |
my $schema = Koha::Database->new->schema; |
| 30 |
$schema->storage->txn_begin; |
|
|
| 31 |
|
| 32 |
my $builder = t::lib::TestBuilder->new; |
33 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
34 |
|
| 34 |
subtest 'item' => sub { |
35 |
subtest 'item() tests' => sub { |
| 35 |
plan tests => 2; |
36 |
plan tests => 2; |
| 36 |
|
37 |
|
| 37 |
my $library = $builder->build( { source => 'Branch' } ); |
38 |
$schema->storage->txn_begin; |
| 38 |
my $biblioitem = $builder->build( { source => 'Biblioitem' } ); |
39 |
|
| 39 |
my $patron = $builder->build( { source => 'Borrower' } ); |
40 |
my $library = $builder->build( { source => 'Branch' } ); |
| 40 |
my $item = Koha::Item->new( |
41 |
my $biblioitem = $builder->build( { source => 'Biblioitem' } ); |
| 41 |
{ |
42 |
my $patron = $builder->build( { source => 'Borrower' } ); |
| 42 |
biblionumber => $biblioitem->{biblionumber}, |
43 |
my $item = Koha::Item->new( |
| 43 |
biblioitemnumber => $biblioitem->{biblioitemnumber}, |
44 |
{ |
| 44 |
homebranch => $library->{branchcode}, |
45 |
biblionumber => $biblioitem->{biblionumber}, |
| 45 |
holdingbranch => $library->{branchcode}, |
46 |
biblioitemnumber => $biblioitem->{biblioitemnumber}, |
| 46 |
barcode => 'some_barcode_12', |
47 |
homebranch => $library->{branchcode}, |
| 47 |
itype => 'BK', |
48 |
holdingbranch => $library->{branchcode}, |
| 48 |
})->store; |
49 |
barcode => 'some_barcode_12', |
| 49 |
|
50 |
itype => 'BK', |
| 50 |
my $line = Koha::Account::Line->new( |
51 |
})->store; |
| 51 |
{ |
52 |
|
| 52 |
borrowernumber => $patron->{borrowernumber}, |
53 |
my $line = Koha::Account::Line->new( |
| 53 |
itemnumber => $item->itemnumber, |
54 |
{ |
| 54 |
accounttype => "F", |
55 |
borrowernumber => $patron->{borrowernumber}, |
| 55 |
amount => 10, |
56 |
itemnumber => $item->itemnumber, |
| 56 |
})->store; |
57 |
accounttype => "F", |
| 57 |
|
58 |
amount => 10, |
| 58 |
my $account_line_item = $line->item; |
59 |
})->store; |
| 59 |
is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' ); |
60 |
|
| 60 |
is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' ); |
61 |
my $account_line_item = $line->item; |
|
|
62 |
is( ref( $account_line_item ), 'Koha::Item', 'Koha::Account::Line->item should return a Koha::Item' ); |
| 63 |
is( $line->itemnumber, $account_line_item->itemnumber, 'Koha::Account::Line->item should return the correct item' ); |
| 64 |
|
| 65 |
$schema->storage->txn_rollback; |
| 61 |
}; |
66 |
}; |
| 62 |
|
67 |
|
| 63 |
$schema->storage->txn_rollback; |
68 |
subtest 'is_credit() tests' => sub { |
|
|
69 |
|
| 70 |
plan tests => 2; |
| 71 |
|
| 72 |
$schema->storage->txn_begin; |
| 73 |
|
| 74 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 75 |
my $account = $patron->account; |
| 76 |
|
| 77 |
my $credit = $account->add_credit({ amount => 100, user_id => $patron->id }); |
| 78 |
|
| 79 |
ok( $credit->is_credit, 'is_credit detects credits' ); |
| 80 |
|
| 81 |
my $debit = Koha::Account::Line->new( |
| 82 |
{ |
| 83 |
borrowernumber => $patron->id, |
| 84 |
accounttype => "F", |
| 85 |
amount => 10, |
| 86 |
})->store; |
| 87 |
|
| 88 |
ok( !$debit->is_credit, 'is_credit() detects debits' ); |
| 89 |
|
| 90 |
$schema->storage->txn_rollback; |
| 91 |
}; |
| 92 |
|
| 93 |
subtest 'apply() tests' => sub { |
| 94 |
|
| 95 |
plan tests => 12; |
| 96 |
|
| 97 |
$schema->storage->txn_begin; |
| 98 |
|
| 99 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 100 |
my $account = $patron->account; |
| 101 |
|
| 102 |
my $credit = $account->add_credit( { amount => 100, user_id => $patron->id } ); |
| 103 |
|
| 104 |
my $debit_1 = Koha::Account::Line->new( |
| 105 |
{ borrowernumber => $patron->id, |
| 106 |
accounttype => "F", |
| 107 |
amount => 10, |
| 108 |
amountoutstanding => 10 |
| 109 |
} |
| 110 |
)->store; |
| 111 |
|
| 112 |
my $debit_2 = Koha::Account::Line->new( |
| 113 |
{ borrowernumber => $patron->id, |
| 114 |
accounttype => "F", |
| 115 |
amount => 100, |
| 116 |
amountoutstanding => 100 |
| 117 |
} |
| 118 |
)->store; |
| 119 |
|
| 120 |
$credit->discard_changes; |
| 121 |
$debit_1->discard_changes; |
| 122 |
|
| 123 |
my $remaining_credit = $credit->apply( { debit => $debit_1, offset_type => 'Manual Credit' } ); |
| 124 |
is( $remaining_credit, 90, 'Remaining credit is correctly calculated' ); |
| 125 |
$credit->discard_changes; |
| 126 |
is( $credit->amountoutstanding * -1, $remaining_credit, 'Remaining credit correctly stored' ); |
| 127 |
|
| 128 |
# re-read debit info |
| 129 |
$debit_1->discard_changes; |
| 130 |
is( $debit_1->amountoutstanding * 1, 0, 'Debit has been cancelled' ); |
| 131 |
|
| 132 |
my $offsets = Koha::Account::Offsets->search( { credit_id => $credit->id, debit_id => $debit_1->id } ); |
| 133 |
is( $offsets->count, 1, 'Only one offset is generated' ); |
| 134 |
my $THE_offest = $offsets->next; |
| 135 |
is( $THE_offest->amount * 1, 10, 'Amount was calculated correctly (less than the available credit)' ); |
| 136 |
is( $THE_offest->type, 'Manual Credit', 'Passed type stored correctly' ); |
| 137 |
|
| 138 |
$remaining_credit = $credit->apply( { debit => $debit_2, offset_type => 'Manual Credit' } ); |
| 139 |
is( $remaining_credit, 0, 'No remaining credit left' ); |
| 140 |
$credit->discard_changes; |
| 141 |
is( $credit->amountoutstanding * 1, 0, 'No outstanding credit' ); |
| 142 |
$debit_2->discard_changes; |
| 143 |
is( $debit_2->amountoutstanding * 1, 10, 'Outstanding amount decremented correctly' ); |
| 144 |
|
| 145 |
throws_ok |
| 146 |
{ $debit_1->apply({ debit => $credit }); } |
| 147 |
'Koha::Exceptions::Account::IsNotCredit', |
| 148 |
'->apply() can only be used with credits'; |
| 149 |
|
| 150 |
throws_ok |
| 151 |
{ $credit->apply({ debit => $credit }); } |
| 152 |
'Koha::Exceptions::Account::IsNotDebit', |
| 153 |
'->apply() can only be applied to credits'; |
| 154 |
|
| 155 |
throws_ok |
| 156 |
{ $credit->apply({ debit => $debit_1 }); } |
| 157 |
'Koha::Exceptions::Account::NoAvailableCredit', |
| 158 |
'->apply() can only be used with outstanding credits'; |
| 159 |
|
| 160 |
|
| 161 |
$schema->storage->txn_rollback; |
| 162 |
}; |
| 64 |
|
163 |
|
| 65 |
1; |
|
|
| 66 |
- |