Bug 24545: Fix license statements
[koha.git] / t / db_dependent / Koha / Charges / Sales.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2018 ByWater Solutions
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 => 5;
23 use Test::Exception;
24
25 use Koha::Database;
26
27 use t::lib::Mocks;
28 use t::lib::TestBuilder;
29
30 BEGIN {
31     use_ok('Koha::Charges::Sales');
32 }
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new();
36
37 subtest 'new' => sub {
38
39     plan tests => 4;
40
41     $schema->storage->txn_begin;
42
43     throws_ok { Koha::Charges::Sales->new( { staff_id => 1 } ) }
44     'Koha::Exceptions::MissingParameter',
45       'Exception thrown if cash_register parameter missing';
46
47     throws_ok { Koha::Charges::Sales->new( { cash_register => 1 } ) }
48     'Koha::Exceptions::MissingParameter',
49       'Exception thrown if staff_id parameter missing';
50
51     throws_ok {
52         Koha::Charges::Sales->new( { staff_id => 1, cash_register => 1 } )
53     }
54     qr/Koha::Cash::Register/,
55       'Exception thrown if cash_register is not a Koha::Cash::Register';
56
57     my $cash_register =
58       $builder->build_object( { class => 'Koha::Cash::Registers' } );
59
60     my $sale = Koha::Charges::Sales->new(
61         { staff_id => 1, cash_register => $cash_register } );
62     ok(
63         $sale->isa('Koha::Charges::Sales'),
64         'New returns a Koha::Charges::Sales object'
65     );
66
67     $schema->storage->txn_rollback;
68 };
69
70 subtest 'payment_type (_get_valid_payments) tests' => sub {
71     plan tests => 5;
72
73     $schema->storage->txn_begin;
74
75     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
76     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
77     my $cash_register = $builder->build_object(
78         {
79             class => 'Koha::Cash::Registers',
80             value => { branchcode => $library1->branchcode }
81         }
82     );
83
84     my $sale = Koha::Charges::Sales->new(
85         { staff_id => 1, cash_register => $cash_register } );
86
87     is( $sale->payment_type, undef, "payment_type does not have a default" );
88
89     throws_ok { $sale->payment_type('BOBBYRANDOM') }
90     'Koha::Exceptions::Account::UnrecognisedType',
91       "Exception thrown if passed a payment type that doesn't exist";
92
93     my $av = Koha::AuthorisedValue->new(
94         {
95             category         => 'PAYMENT_TYPE',
96             authorised_value => 'BOBBYRANDOM',
97             lib              => 'Test bobbyrandom',
98             lib_opac         => 'Test bobbyrandom',
99         }
100     )->store;
101     $av->replace_library_limits( [ $library2->branchcode ] );
102
103     throws_ok { $sale->payment_type('BOBBYRANDOM') }
104     'Koha::Exceptions::Account::UnrecognisedType',
105 'Exception thrown if passed payment type that is not valid for the cash registers branch';
106
107     $av->replace_library_limits();
108     $sale->{valid_payments} = undef;   # Flush object cache for 'valid_payments'
109
110     my $pt = $sale->payment_type('BOBBYRANDOM');
111     is( $pt, 'BOBBYRANDOM', 'Payment type set successfully' );
112     is( $sale->payment_type, 'BOBBYRANDOM',
113         'Getter returns the current payment_type' );
114
115     $schema->storage->txn_rollback;
116 };
117
118 subtest 'add_item (_get_valid_items) tests' => sub {
119     plan tests => 6;
120
121     $schema->storage->txn_begin;
122
123     my $staff    = $builder->build_object( { class => 'Koha::Patrons' } );
124     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
125     my $cash_register = $builder->build_object(
126         {
127             class => 'Koha::Cash::Registers',
128             value => { branchcode => $library1->branchcode }
129         }
130     );
131
132     my $sale = Koha::Charges::Sales->new(
133         { staff_id => $staff->borrowernumber, cash_register => $cash_register }
134     );
135
136     throws_ok { $sale->add_item( { price => 1.00, quantity => 1 } ) }
137     'Koha::Exceptions::MissingParameter',
138       'Exception thrown if `code` parameter is missing';
139
140     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
141     my $dt = Koha::Account::DebitType->new(
142         {
143             code        => 'BOBBYRANDOM',
144             description => 'Test bobbyrandom',
145         }
146     )->store;
147     $dt->replace_library_limits( [ $library2->branchcode ] );
148
149     throws_ok { $sale->add_item( { code => 'BOBBYRANDOM' } ) }
150     'Koha::Exceptions::Account::UnrecognisedType',
151 'Exception thrown if passed an item code that is not valid for the cash registers branch';
152
153     $dt->replace_library_limits();
154     $sale->{valid_items} = undef;    # Flush object cache for 'valid_items'
155
156     throws_ok {
157         $sale->add_item( { code => 'BOBBYRANDOM', quantity => 1 } )
158     }
159     'Koha::Exceptions::MissingParameter',
160       'Exception thrown if `price` parameter is missing';
161
162     throws_ok {
163         $sale->add_item( { code => 'BOBBYRANDOM', price => 1.00 } )
164     }
165     'Koha::Exceptions::MissingParameter',
166       'Exception thrown if `quantity` parameter is missing';
167
168     is(
169         ref(
170             $sale->add_item(
171                 { code => 'BOBBYRANDOM', price => 1.00, quantity => 1 }
172             )
173         ),
174         'Koha::Charges::Sales',
175         'Original object returned succesfully'
176     );
177
178     is( scalar @{ $sale->{items} }, 1, 'Item added successfully' );
179
180     $schema->storage->txn_rollback;
181 };
182
183 subtest 'purchase tests' => sub {
184     plan tests => 12;
185
186     $schema->storage->txn_begin;
187
188     my $staff   = $builder->build_object( { class => 'Koha::Patrons' } );
189     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
190     my $cash_register = $builder->build_object(
191         {
192             class => 'Koha::Cash::Registers',
193             value => { branchcode => $library->branchcode }
194         }
195     );
196
197     my $payment_type = Koha::AuthorisedValue->new(
198         {
199             category         => 'PAYMENT_TYPE',
200             authorised_value => 'CASH',
201             lib              => 'Cash transaction',
202             lib_opac         => 'Cash transaction',
203         }
204     )->store;
205
206     my $item1 = Koha::Account::DebitType->new(
207         {
208             code        => 'COPYRANDOM',
209             description => 'Copier fee',
210         }
211     )->store;
212     my $item2 = Koha::Account::DebitType->new(
213         {
214             code        => 'CARDRANDOM',
215             description => 'New card fee',
216         }
217     )->store;
218
219     my $sale = Koha::Charges::Sales->new(
220         { staff_id => $staff->borrowernumber, cash_register => $cash_register }
221     );
222
223     throws_ok {
224         $sale->purchase()
225     }
226     'Koha::Exceptions::MissingParameter',
227       'Exception thrown if `payment_type` is neither set nor passed';
228
229     $sale->payment_type('CASH');    # Set payment_type
230     throws_ok {
231         $sale->purchase()
232     }
233     'Koha::Exceptions::NoChanges',
234       'Exception thrown if `add_item` is not called before `purchase`';
235
236     $sale->add_item( { code => 'COPYRANDOM', price => 1.00, quantity => 1 } );
237     $sale->add_item( { code => 'CARDRANDOM', price => 2.00, quantity => 2 } );
238
239     $sale->{payment_type} = undef;    # Flush payment_type cache in object
240
241     my $credit;
242     ok( $credit = $sale->purchase( { payment_type => 'CASH' } ),
243         "No exception when payment_type passed" );
244
245     is(ref($credit), 'Koha::Account::Line', "Koha::Account::Line returned");
246     ok($credit->is_credit, "return is a credit for payment");
247     is($credit->credit_type_code, 'PURCHASE', "credit_type_code set correctly to 'PURCHASE' for payment");
248     is($credit->amount, -5.00, "amount is calculated correctly for payment");
249     is($credit->amountoutstanding, 0.00, "amountoutstanding is set to zero for payment");
250     is($credit->manager_id, $staff->borrowernumber, "manager_id set correctionly for payment");
251     is($credit->register_id, $cash_register->id, "register_id set correctly for payment");
252     is($credit->payment_type, 'CASH', "payment_type set correctly for payment");
253
254     my $offsets = Koha::Account::Offsets->search({credit_id => $credit->accountlines_id});
255     is($offsets->count, 3, "One offset was added for each item added"); # 2 items + 1 purchase
256
257 #ensure relevant fields are set
258 #ensure register_id is only ever set with a corresponding payment_type having been set
259
260     $schema->storage->txn_rollback;
261 };