3 # Copyright 2019 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 4;
28 use t::lib::TestBuilder;
30 my $builder = t::lib::TestBuilder->new;
31 my $schema = Koha::Database->new->schema;
33 subtest 'library' => sub {
36 $schema->storage->txn_begin;
38 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
39 my $register = $builder->build_object(
41 class => 'Koha::Cash::Registers',
42 value => { branch => $library->branchcode },
46 is( ref( $register->library ),
48 'Koha::Cash::Register->library should return a Koha::Library' );
50 is( $register->library->id,
52 'Koha::Cash::Register->library returns the correct Koha::Library' );
54 $schema->storage->txn_rollback;
57 subtest 'accountlines' => sub {
60 $schema->storage->txn_begin;
63 $builder->build_object( { class => 'Koha::Cash::Registers' } );
64 my $accountline1 = $builder->build_object(
66 class => 'Koha::Account::Lines',
67 value => { register_id => $register->id },
70 my $accountline2 = $builder->build_object(
72 class => 'Koha::Account::Lines',
73 value => { register_id => $register->id },
77 my $accountlines = $register->accountlines;
78 is( ref($accountlines), 'Koha::Account::Lines',
79 'Koha::Cash::Register->accountlines should return a set of Koha::Account::Lines'
81 is( $accountlines->count, 2,
82 'Koha::Cash::Register->accountlines should return the correct number of accountlines'
85 $accountline1->delete;
86 is( $register->accountlines->next->id, $accountline2->id,
87 'Koha::Cash::Register->accountlines should return the correct acocuntlines'
90 $schema->storage->txn_rollback;
93 subtest 'branch_default' => sub {
96 $schema->storage->txn_begin;
97 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
98 my $register1 = $builder->build_object(
100 class => 'Koha::Cash::Registers',
101 value => { branch => $library->branchcode, branch_default => 1 },
104 my $register2 = $builder->build_object(
106 class => 'Koha::Cash::Registers',
107 value => { branch => $library->branchcode, branch_default => 0 },
111 subtest 'store' => sub {
114 $register1->name('Test till 1');
115 ok( $register1->store(),
116 "Store works as expected when branch_default is not changed" );
118 $register1->branch_default(0);
119 throws_ok { $register1->store(); }
120 'Koha::Exceptions::Object::ReadOnlyProperty',
121 'Exception thrown if direct update to branch_default is attempted';
125 subtest 'make_default' => sub {
128 ok( $register2->make_default, 'Koha::Register->make_default ran' );
130 $register1 = $register1->get_from_storage;
131 $register2 = $register2->get_from_storage;
132 is( $register1->branch_default, 0, 'register1 was unset as expected' );
133 is( $register2->branch_default, 1, 'register2 was set as expected' );
136 subtest 'drop_default' => sub {
139 ok( $register2->drop_default, 'Koha::Register->drop_default ran' );
141 $register2 = $register2->get_from_storage;
142 is( $register2->branch_default, 0, 'register2 was unset as expected' );
145 $schema->storage->txn_rollback;
148 subtest 'cashup' => sub {
151 $schema->storage->txn_begin;
154 $builder->build_object( { class => 'Koha::Cash::Registers' } );
155 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
158 subtest 'add_cashup' => sub {
162 $cashup1 = $register->add_cashup(
163 { manager_id => $patron->id, amount => '12.00' }
170 'Koha::Cash::Register::Action',
171 'return is Koha::Cash::Register::Action'
173 is( $cashup1->code, 'CASHUP',
174 'CASHUP code set in Koha::Cash::Register::Action' );
175 is( $cashup1->manager_id, $patron->id,
176 'manager_id set correctly in Koha::Cash::Register::Action' );
177 is( $cashup1->amount, '12.000000',
178 'amount set correctly in Koha::Cash::Register::Action' );
179 isnt( $cashup1->timestamp, undef,
180 'timestamp set in Koha::Cash::Register::Action' );
183 subtest 'last_cashup' => sub {
187 $register->add_cashup( { manager_id => $patron->id, amount => '6.00' } );
189 my $last_cashup = $register->last_cashup;
192 'Koha::Cash::Register::Action',
193 'A cashup was returned when one existed'
195 is( $last_cashup->id, $cashup2->id,
196 'The most recent cashup was returned' );
199 $last_cashup = $register->last_cashup;
200 is( $last_cashup, undef, 'undef is returned when no cashup exists' );
203 subtest 'outstanding_accountlines' => sub {
206 my $accountline1 = $builder->build_object(
208 class => 'Koha::Account::Lines',
209 value => { register_id => $register->id },
212 my $accountline2 = $builder->build_object(
214 class => 'Koha::Account::Lines',
215 value => { register_id => $register->id },
219 my $accountlines = $register->outstanding_accountlines;
220 is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
223 $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } );
225 $accountlines = $register->outstanding_accountlines;
226 is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
228 my $accountline3 = $builder->build_object(
230 class => 'Koha::Account::Lines',
231 value => { register_id => $register->id },
235 $accountlines = $register->outstanding_accountlines;
236 is( $accountlines->count, 1,
237 'Accountline added, one accountline returned' );
238 is( $accountlines->next->id,
239 $accountline3->id, 'Correct accountline returned' );
242 $schema->storage->txn_rollback;