Bug 23355: Rename staff_id for manager_id
[srvgit] / t / db_dependent / Koha / Cash / Register.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 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
22 use Test::More tests => 4;
23
24 use Test::Exception;
25
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29
30 my $builder = t::lib::TestBuilder->new;
31 my $schema  = Koha::Database->new->schema;
32
33 subtest 'library' => sub {
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
39     my $register = $builder->build_object(
40         {
41             class => 'Koha::Cash::Registers',
42             value => { branch => $library->branchcode },
43         }
44     );
45
46     is( ref( $register->library ),
47         'Koha::Library',
48         'Koha::Cash::Register->library should return a Koha::Library' );
49
50     is( $register->library->id,
51         $library->id,
52         'Koha::Cash::Register->library returns the correct Koha::Library' );
53
54     $schema->storage->txn_rollback;
55 };
56
57 subtest 'accountlines' => sub {
58     plan tests => 3;
59
60     $schema->storage->txn_begin;
61
62     my $register =
63       $builder->build_object( { class => 'Koha::Cash::Registers' } );
64     my $accountline1 = $builder->build_object(
65         {
66             class => 'Koha::Account::Lines',
67             value => { register_id => $register->id },
68         }
69     );
70     my $accountline2 = $builder->build_object(
71         {
72             class => 'Koha::Account::Lines',
73             value => { register_id => $register->id },
74         }
75     );
76
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'
80     );
81     is( $accountlines->count, 2,
82 'Koha::Cash::Register->accountlines should return the correct number of accountlines'
83     );
84
85     $accountline1->delete;
86     is( $register->accountlines->next->id, $accountline2->id,
87 'Koha::Cash::Register->accountlines should return the correct acocuntlines'
88     );
89
90     $schema->storage->txn_rollback;
91 };
92
93 subtest 'branch_default' => sub {
94     plan tests => 3;
95
96     $schema->storage->txn_begin;
97     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
98     my $register1 = $builder->build_object(
99         {
100             class => 'Koha::Cash::Registers',
101             value => { branch => $library->branchcode, branch_default => 1 },
102         }
103     );
104     my $register2 = $builder->build_object(
105         {
106             class => 'Koha::Cash::Registers',
107             value => { branch => $library->branchcode, branch_default => 0 },
108         }
109     );
110
111     subtest 'store' => sub {
112         plan tests => 2;
113
114         $register1->name('Test till 1');
115         ok( $register1->store(),
116             "Store works as expected when branch_default is not changed" );
117
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';
122
123     };
124
125     subtest 'make_default' => sub {
126         plan tests => 3;
127
128         ok( $register2->make_default, 'Koha::Register->make_default ran' );
129
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' );
134     };
135
136     subtest 'drop_default' => sub {
137         plan tests => 2;
138
139         ok( $register2->drop_default, 'Koha::Register->drop_default ran' );
140
141         $register2 = $register2->get_from_storage;
142         is( $register2->branch_default, 0, 'register2 was unset as expected' );
143     };
144
145     $schema->storage->txn_rollback;
146 };
147
148 subtest 'cashup' => sub {
149     plan tests => 3;
150
151     $schema->storage->txn_begin;
152
153     my $register =
154       $builder->build_object( { class => 'Koha::Cash::Registers' } );
155     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
156
157     my $cashup1;
158     subtest 'add_cashup' => sub {
159         plan tests => 6;
160
161         ok(
162             $cashup1 = $register->add_cashup(
163                 { manager_id => $patron->id, amount => '12.00' }
164             ),
165             'call successfull'
166         );
167
168         is(
169             ref($cashup1),
170             'Koha::Cash::Register::Action',
171             'return is Koha::Cash::Register::Action'
172         );
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' );
181     };
182
183     subtest 'last_cashup' => sub {
184         plan tests => 3;
185
186         my $cashup2 =
187           $register->add_cashup( { manager_id => $patron->id, amount => '6.00' } );
188
189         my $last_cashup = $register->last_cashup;
190         is(
191             ref($last_cashup),
192             'Koha::Cash::Register::Action',
193             'A cashup was returned when one existed'
194         );
195         is( $last_cashup->id, $cashup2->id,
196             'The most recent cashup was returned' );
197         $cashup1->delete;
198         $cashup2->delete;
199         $last_cashup = $register->last_cashup;
200         is( $last_cashup, undef, 'undef is returned when no cashup exists' );
201     };
202
203     subtest 'outstanding_accountlines' => sub {
204         plan tests => 4;
205
206         my $accountline1 = $builder->build_object(
207             {
208                 class => 'Koha::Account::Lines',
209                 value => { register_id => $register->id },
210             }
211         );
212         my $accountline2 = $builder->build_object(
213             {
214                 class => 'Koha::Account::Lines',
215                 value => { register_id => $register->id },
216             }
217         );
218
219         my $accountlines = $register->outstanding_accountlines;
220         is( $accountlines->count, 2, 'No cashup, all accountlines returned' );
221
222         my $cashup3 =
223           $register->add_cashup( { manager_id => $patron->id, amount => '2.50' } );
224
225         $accountlines = $register->outstanding_accountlines;
226         is( $accountlines->count, 0, 'Cashup added, no accountlines returned' );
227
228         my $accountline3 = $builder->build_object(
229             {
230                 class => 'Koha::Account::Lines',
231                 value => { register_id => $register->id },
232             }
233         );
234
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' );
240     };
241
242     $schema->storage->txn_rollback;
243 };