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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 $payment_type = Koha
::AuthorisedValue
->new(
199 category
=> 'PAYMENT_TYPE',
200 authorised_value
=> 'CASH',
201 lib
=> 'Cash transaction',
202 lib_opac
=> 'Cash transaction',
206 my $item1 = Koha
::Account
::DebitType
->new(
208 code
=> 'COPYRANDOM',
209 description
=> 'Copier fee',
212 my $item2 = Koha
::Account
::DebitType
->new(
214 code
=> 'CARDRANDOM',
215 description
=> 'New card fee',
219 my $sale = Koha
::Charges
::Sales
->new(
220 { staff_id
=> $staff->borrowernumber, cash_register
=> $cash_register }
226 'Koha::Exceptions::MissingParameter',
227 'Exception thrown if `payment_type` is neither set nor passed';
229 $sale->payment_type('CASH'); # Set payment_type
233 'Koha::Exceptions::NoChanges',
234 'Exception thrown if `add_item` is not called before `purchase`';
236 $sale->add_item( { code
=> 'COPYRANDOM', price
=> 1.00, quantity
=> 1 } );
237 $sale->add_item( { code
=> 'CARDRANDOM', price
=> 2.00, quantity
=> 2 } );
239 $sale->{payment_type
} = undef; # Flush payment_type cache in object
242 ok
( $credit = $sale->purchase( { payment_type
=> 'CASH' } ),
243 "No exception when payment_type passed" );
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");
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
257 #ensure relevant fields are set
258 #ensure register_id is only ever set with a corresponding payment_type having been set
260 $schema->storage->txn_rollback;