From cf27a62f707bbf474023fb5edf86960c7cf4e4b4 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 17 Sep 2019 12:28:56 +0100 Subject: [PATCH] Bug 23354: Add tests for Koha::Charges::Sales Sponsored-by: PTFS Europe Sponsored-by: Cheshire Libraries Shared Services Signed-off-by: Kyle M Hall Signed-off-by: Josef Moravec Signed-off-by: Martin Renvoize --- t/db_dependent/Koha/Charges/Sales.pm | 267 +++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 t/db_dependent/Koha/Charges/Sales.pm diff --git a/t/db_dependent/Koha/Charges/Sales.pm b/t/db_dependent/Koha/Charges/Sales.pm new file mode 100644 index 0000000000..e4dbdd366e --- /dev/null +++ b/t/db_dependent/Koha/Charges/Sales.pm @@ -0,0 +1,267 @@ +#!/usr/bin/perl +# +# Copyright 2018 ByWater Solutions +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Test::More tests => 5; +use Test::Exception; + +use Koha::Database; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +BEGIN { + use_ok('Koha::Charges::Sales'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new(); + +subtest 'new' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + throws_ok { Koha::Charges::Sales->new( { staff_id => 1 } ) } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if cash_register parameter missing'; + + throws_ok { Koha::Charges::Sales->new( { cash_register => 1 } ) } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if staff_id parameter missing'; + + throws_ok { + Koha::Charges::Sales->new( { staff_id => 1, cash_register => 1 } ) + } + qr/Koha::Cash::Register/, + 'Exception thrown if cash_register is not a Koha::Cash::Register'; + + my $cash_register = + $builder->build_object( { class => 'Koha::Cash::Registers' } ); + + my $sale = Koha::Charges::Sales->new( + { staff_id => 1, cash_register => $cash_register } ); + ok( + $sale->isa('Koha::Charges::Sales'), + 'New returns a Koha::Charges::Sales object' + ); + + $schema->storage->txn_rollback; +}; + +subtest 'payment_type (_get_valid_payments) tests' => sub { + plan tests => 5; + + $schema->storage->txn_begin; + + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $cash_register = $builder->build_object( + { + class => 'Koha::Cash::Registers', + value => { branchcode => $library1->branchcode } + } + ); + + my $sale = Koha::Charges::Sales->new( + { staff_id => 1, cash_register => $cash_register } ); + + is( $sale->payment_type, undef, "payment_type does not have a default" ); + + throws_ok { $sale->payment_type('BOBBYRANDOM') } + 'Koha::Exceptions::Account::UnrecognisedType', + "Exception thrown if passed a payment type that doesn't exist"; + + my $av = Koha::AuthorisedValue->new( + { + category => 'PAYMENT_TYPE', + authorised_value => 'BOBBYRANDOM', + lib => 'Test bobbyrandom', + lib_opac => 'Test bobbyrandom', + } + )->store; + $av->replace_library_limits( [ $library2->branchcode ] ); + + throws_ok { $sale->payment_type('BOBBYRANDOM') } + 'Koha::Exceptions::Account::UnrecognisedType', +'Exception thrown if passed payment type that is not valid for the cash registers branch'; + + $av->replace_library_limits(); + $sale->{valid_payments} = undef; # Flush object cache for 'valid_payments' + + my $pt = $sale->payment_type('BOBBYRANDOM'); + is( $pt, 'BOBBYRANDOM', 'Payment type set successfully' ); + is( $sale->payment_type, 'BOBBYRANDOM', + 'Getter returns the current payment_type' ); + + $schema->storage->txn_rollback; +}; + +subtest 'add_item (_get_valid_items) tests' => sub { + plan tests => 6; + + $schema->storage->txn_begin; + + my $staff = $builder->build_object( { class => 'Koha::Patrons' } ); + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $cash_register = $builder->build_object( + { + class => 'Koha::Cash::Registers', + value => { branchcode => $library1->branchcode } + } + ); + + my $sale = Koha::Charges::Sales->new( + { staff_id => $staff->borrowernumber, cash_register => $cash_register } + ); + + throws_ok { $sale->add_item( { price => 1.00, quantity => 1 } ) } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if `code` parameter is missing'; + + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $av = Koha::AuthorisedValue->new( + { + category => 'MANUAL_INV', + authorised_value => 'BOBBYRANDOM', + lib => 'Test bobbyrandom', + lib_opac => 'Test bobbyrandom', + } + )->store; + $av->replace_library_limits( [ $library2->branchcode ] ); + + throws_ok { $sale->add_item( { code => 'BOBBYRANDOM' } ) } + 'Koha::Exceptions::Account::UnrecognisedType', +'Exception thrown if passed an item code that is not valid for the cash registers branch'; + + $av->replace_library_limits(); + $sale->{valid_items} = undef; # Flush object cache for 'valid_items' + + throws_ok { + $sale->add_item( { code => 'BOBBYRANDOM', quantity => 1 } ) + } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if `price` parameter is missing'; + + throws_ok { + $sale->add_item( { code => 'BOBBYRANDOM', price => 1.00 } ) + } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if `quantity` parameter is missing'; + + is( + ref( + $sale->add_item( + { code => 'BOBBYRANDOM', price => 1.00, quantity => 1 } + ) + ), + 'Koha::Charges::Sales', + 'Original object returned succesfully' + ); + + is( scalar @{ $sale->{items} }, 1, 'Item added successfully' ); + + $schema->storage->txn_rollback; +}; + +subtest 'purchase tests' => sub { + plan tests => 12; + + $schema->storage->txn_begin; + + my $staff = $builder->build_object( { class => 'Koha::Patrons' } ); + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $cash_register = $builder->build_object( + { + class => 'Koha::Cash::Registers', + value => { branchcode => $library->branchcode } + } + ); + + my $payment_type = Koha::AuthorisedValue->new( + { + category => 'PAYMENT_TYPE', + authorised_value => 'CASH', + lib => 'Cash transaction', + lib_opac => 'Cash transaction', + } + )->store; + + my $item1 = Koha::AuthorisedValue->new( + { + category => 'MANUAL_INV', + authorised_value => 'COPYRANDOM', + lib => 'Copier fee', + lib_opac => 'Copier fee', + } + )->store; + my $item2 = Koha::AuthorisedValue->new( + { + category => 'MANUAL_INV', + authorised_value => 'CARDRANDOM', + lib => 'New card fee', + lib_opac => 'New card fee', + } + )->store; + + my $sale = Koha::Charges::Sales->new( + { staff_id => $staff->borrowernumber, cash_register => $cash_register } + ); + + throws_ok { + $sale->purchase() + } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if `payment_type` is neither set nor passed'; + + $sale->payment_type('CASH'); # Set payment_type + throws_ok { + $sale->purchase() + } + 'Koha::Exceptions::NoChanges', + 'Exception thrown if `add_item` is not called before `purchase`'; + + $sale->add_item( { code => 'COPYRANDOM', price => 1.00, quantity => 1 } ); + $sale->add_item( { code => 'CARDRANDOM', price => 2.00, quantity => 2 } ); + + $sale->{payment_type} = undef; # Flush payment_type cache in object + + my $credit; + ok( $credit = $sale->purchase( { payment_type => 'CASH' } ), + "No exception when payment_type passed" ); + + is(ref($credit), 'Koha::Account::Line', "Koha::Account::Line returned"); + ok($credit->is_credit, "return is a credit for payment"); + is($credit->accounttype, 'Purchase', "accounttype set correctly to 'Purchase' for payment"); + is($credit->amount, -5.00, "amount is calculated correctly for payment"); + is($credit->amountoutstanding, 0.00, "amountoutstanding is set to zero for payment"); + is($credit->manager_id, $staff->borrowernumber, "manager_id set correctionly for payment"); + is($credit->register_id, $cash_register->id, "register_id set correctly for payment"); + is($credit->payment_type, 'CASH', "payment_type set correctly for payment"); + + my $offsets = Koha::Account::Offsets->search({credit_id => $credit->accountlines_id}); + is($offsets->count, 2, "One offset was added for each item added"); + +#ensure relevant fields are set +#ensure register_id is only ever set with a corresponding payment_type having been set + + $schema->storage->txn_rollback; +}; -- 2.39.5