3 # Copyright 2018 ByWater Solutions
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 => 5;
28 use t::lib::TestBuilder;
31 use_ok('Koha::Charges::Sales');
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new();
37 subtest 'new' => sub {
41 $schema->storage->txn_begin;
43 throws_ok { Koha::Charges::Sales->new( { staff_id => 1 } ) }
44 'Koha::Exceptions::MissingParameter',
45 'Exception thrown if cash_register parameter missing';
47 throws_ok { Koha::Charges::Sales->new( { cash_register => 1 } ) }
48 'Koha::Exceptions::MissingParameter',
49 'Exception thrown if staff_id parameter missing';
52 Koha::Charges::Sales->new( { staff_id => 1, cash_register => 1 } )
54 qr/Koha::Cash::Register/,
55 'Exception thrown if cash_register is not a Koha::Cash::Register';
58 $builder->build_object( { class => 'Koha::Cash::Registers' } );
60 my $sale = Koha::Charges::Sales->new(
61 { staff_id => 1, cash_register => $cash_register } );
63 $sale->isa('Koha::Charges::Sales'),
64 'New returns a Koha::Charges::Sales object'
67 $schema->storage->txn_rollback;
70 subtest 'payment_type (_get_valid_payments) tests' => sub {
73 $schema->storage->txn_begin;
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(
79 class => 'Koha::Cash::Registers',
80 value => { branchcode => $library1->branchcode }
84 my $sale = Koha::Charges::Sales->new(
85 { staff_id => 1, cash_register => $cash_register } );
87 is( $sale->payment_type, undef, "payment_type does not have a default" );
89 throws_ok { $sale->payment_type('BOBBYRANDOM') }
90 'Koha::Exceptions::Account::UnrecognisedType',
91 "Exception thrown if passed a payment type that doesn't exist";
93 my $av = Koha::AuthorisedValue->new(
95 category => 'PAYMENT_TYPE',
96 authorised_value => 'BOBBYRANDOM',
97 lib => 'Test bobbyrandom',
98 lib_opac => 'Test bobbyrandom',
101 $av->replace_library_limits( [ $library2->branchcode ] );
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';
107 $av->replace_library_limits();
108 $sale->{valid_payments} = undef; # Flush object cache for 'valid_payments'
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' );
115 $schema->storage->txn_rollback;
118 subtest 'add_item (_get_valid_items) tests' => sub {
121 $schema->storage->txn_begin;
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(
127 class => 'Koha::Cash::Registers',
128 value => { branchcode => $library1->branchcode }
132 my $sale = Koha::Charges::Sales->new(
133 { staff_id => $staff->borrowernumber, cash_register => $cash_register }
136 throws_ok { $sale->add_item( { price => 1.00, quantity => 1 } ) }
137 'Koha::Exceptions::MissingParameter',
138 'Exception thrown if `code` parameter is missing';
140 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
141 my $dt = Koha::Account::DebitType->new(
143 code => 'BOBBYRANDOM',
144 description => 'Test bobbyrandom',
147 $dt->replace_library_limits( [ $library2->branchcode ] );
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';
153 $dt->replace_library_limits();
154 $sale->{valid_items} = undef; # Flush object cache for 'valid_items'
157 $sale->add_item( { code => 'BOBBYRANDOM', quantity => 1 } )
159 'Koha::Exceptions::MissingParameter',
160 'Exception thrown if `price` parameter is missing';
163 $sale->add_item( { code => 'BOBBYRANDOM', price => 1.00 } )
165 'Koha::Exceptions::MissingParameter',
166 'Exception thrown if `quantity` parameter is missing';
171 { code => 'BOBBYRANDOM', price => 1.00, quantity => 1 }
174 'Koha::Charges::Sales',
175 'Original object returned succesfully'
178 is( scalar @{ $sale->{items} }, 1, 'Item added successfully' );
180 $schema->storage->txn_rollback;
183 subtest 'purchase tests' => sub {
186 $schema->storage->txn_begin;
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(
192 class => 'Koha::Cash::Registers',
193 value => { branchcode => $library->branchcode }
197 my $item1 = Koha::Account::DebitType->new(
199 code => 'COPYRANDOM',
200 description => 'Copier fee',
203 my $item2 = Koha::Account::DebitType->new(
205 code => 'CARDRANDOM',
206 description => 'New card fee',
210 my $sale = Koha::Charges::Sales->new(
211 { staff_id => $staff->borrowernumber, cash_register => $cash_register }
217 'Koha::Exceptions::MissingParameter',
218 'Exception thrown if `payment_type` is neither set nor passed';
220 $sale->payment_type('CASH'); # Set payment_type
224 'Koha::Exceptions::NoChanges',
225 'Exception thrown if `add_item` is not called before `purchase`';
227 $sale->add_item( { code => 'COPYRANDOM', price => 1.00, quantity => 1 } );
228 $sale->add_item( { code => 'CARDRANDOM', price => 2.00, quantity => 2 } );
230 $sale->{payment_type} = undef; # Flush payment_type cache in object
233 ok( $credit = $sale->purchase( { payment_type => 'CASH' } ),
234 "No exception when payment_type passed" );
236 is(ref($credit), 'Koha::Account::Line', "Koha::Account::Line returned");
237 ok($credit->is_credit, "return is a credit for payment");
238 is($credit->credit_type_code, 'PURCHASE', "credit_type_code set correctly to 'PURCHASE' for payment");
239 is($credit->amount, -5.00, "amount is calculated correctly for payment");
240 is($credit->amountoutstanding, 0.00, "amountoutstanding is set to zero for payment");
241 is($credit->manager_id, $staff->borrowernumber, "manager_id set correctionly for payment");
242 is($credit->register_id, $cash_register->id, "register_id set correctly for payment");
243 is($credit->payment_type, 'CASH', "payment_type set correctly for payment");
245 my $offsets = Koha::Account::Offsets->search({credit_id => $credit->accountlines_id});
246 is($offsets->count, 3, "One offset was added for each item added"); # 2 items + 1 purchase
248 #ensure relevant fields are set
249 #ensure register_id is only ever set with a corresponding payment_type having been set
251 $schema->storage->txn_rollback;