Bug 20912: Rental Fees based on Time Period
[koha.git] / t / db_dependent / Koha / ItemTypes.t
blob34ab094c9fb483883038907f19635bf8bddbdfe6
1 #!/usr/bin/perl
3 # Copyright 2014 Catalyst IT
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
10 # version.
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.
20 use Modern::Perl;
22 use Data::Dumper;
23 use Test::More tests => 25;
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
28 use C4::Calendar;
29 use Koha::Biblioitems;
30 use Koha::Libraries;
31 use Koha::Database;
32 use Koha::DateUtils qw(dt_from_string);;
33 use Koha::Items;
35 BEGIN {
36 use_ok('Koha::ItemType');
37 use_ok('Koha::ItemTypes');
40 my $database = Koha::Database->new();
41 my $schema = $database->schema();
42 $schema->txn_begin;
43 Koha::ItemTypes->delete;
45 Koha::ItemType->new(
47 itemtype => 'type1',
48 description => 'description',
49 rentalcharge => '0.00',
50 imageurl => 'imageurl',
51 summary => 'summary',
52 checkinmsg => 'checkinmsg',
53 checkinmsgtype => 'checkinmsgtype',
55 )->store;
57 Koha::ItemType->new(
59 itemtype => 'type2',
60 description => 'description',
61 rentalcharge => '0.00',
62 imageurl => 'imageurl',
63 summary => 'summary',
64 checkinmsg => 'checkinmsg',
65 checkinmsgtype => 'checkinmsgtype',
67 )->store;
69 Koha::ItemType->new(
71 itemtype => 'type3',
72 description => 'description',
73 rentalcharge => '0.00',
74 imageurl => 'imageurl',
75 summary => 'summary',
76 checkinmsg => 'checkinmsg',
77 checkinmsgtype => 'checkinmsgtype',
79 )->store;
81 Koha::Localization->new(
83 entity => 'itemtypes',
84 code => 'type1',
85 lang => 'en',
86 translation => 'b translated itemtype desc'
88 )->store;
89 Koha::Localization->new(
91 entity => 'itemtypes',
92 code => 'type2',
93 lang => 'en',
94 translation => 'a translated itemtype desc'
96 )->store;
97 Koha::Localization->new(
99 entity => 'something_else',
100 code => 'type2',
101 lang => 'en',
102 translation => 'another thing'
104 )->store;
106 my $type = Koha::ItemTypes->find('type1');
107 ok( defined($type), 'first result' );
108 is( $type->itemtype, 'type1', 'itemtype/code' );
109 is( $type->description, 'description', 'description' );
110 is( $type->rentalcharge, '0.000000', 'rentalcharge' );
111 is( $type->imageurl, 'imageurl', 'imageurl' );
112 is( $type->summary, 'summary', 'summary' );
113 is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' );
114 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
116 $type = Koha::ItemTypes->find('type2');
117 ok( defined($type), 'second result' );
118 is( $type->itemtype, 'type2', 'itemtype/code' );
119 is( $type->description, 'description', 'description' );
120 is( $type->rentalcharge, '0.000000', 'rentalcharge' );
121 is( $type->imageurl, 'imageurl', 'imageurl' );
122 is( $type->summary, 'summary', 'summary' );
123 is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' );
124 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
126 t::lib::Mocks::mock_preference('language', 'en');
127 t::lib::Mocks::mock_preference('opaclanguages', 'en');
128 my $itemtypes = Koha::ItemTypes->search_with_localization;
129 is( $itemtypes->count, 3, 'There are 3 item types' );
130 my $first_itemtype = $itemtypes->next;
132 $first_itemtype->translated_description,
133 'a translated itemtype desc',
134 'item types should be sorted by translated description'
137 my $builder = t::lib::TestBuilder->new;
138 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
140 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
142 my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }});
143 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
144 $item->delete;
146 my $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }});
147 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
148 $biblioitem->delete;
150 is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
152 subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
153 plan tests => 4;
155 my $library = Koha::Libraries->search()->next();
156 my $module = new Test::MockModule('C4::Context');
157 $module->mock('userenv', sub { { branch => $library->id } });
159 my $itemtype = Koha::ItemType->new(
161 itemtype => 'type4',
162 description => 'description',
163 rental_charge_daily => 1.00,
165 )->store;
167 is( $itemtype->rental_charge_daily, 1.00, 'Daily rental charge stored and retreived correctly' );
169 my $dt_from = dt_from_string();
170 my $dt_to = dt_from_string()->add( days => 7 );
172 t::lib::Mocks::mock_preference('finesCalendar', 'ignoreCalendar');
173 my $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
174 is( $charge, 7.00, "Daily rental charge calulated correctly with finesCalendar = ignoreCalendar" );
176 t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
177 $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
178 is( $charge, 7.00, "Daily rental charge calulated correctly with finesCalendar = noFinesWhenClosed" );
180 my $calendar = C4::Calendar->new( branchcode => $library->id );
181 $calendar->insert_week_day_holiday(
182 weekday => 3,
183 title => 'Test holiday',
184 description => 'Test holiday'
186 $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
187 is( $charge, 6.00, "Daily rental charge calulated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays" );
191 $schema->txn_rollback;