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
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.
23 use Test::More tests => 25;
26 use t::lib::TestBuilder;
29 use Koha::Biblioitems;
32 use Koha::DateUtils qw(dt_from_string);;
36 use_ok('Koha::ItemType');
37 use_ok('Koha::ItemTypes');
40 my $database = Koha::Database->new();
41 my $schema = $database->schema();
43 Koha::ItemTypes->delete;
48 description => 'description',
49 rentalcharge => '0.00',
50 imageurl => 'imageurl',
52 checkinmsg => 'checkinmsg',
53 checkinmsgtype => 'checkinmsgtype',
60 description => 'description',
61 rentalcharge => '0.00',
62 imageurl => 'imageurl',
64 checkinmsg => 'checkinmsg',
65 checkinmsgtype => 'checkinmsgtype',
72 description => 'description',
73 rentalcharge => '0.00',
74 imageurl => 'imageurl',
76 checkinmsg => 'checkinmsg',
77 checkinmsgtype => 'checkinmsgtype',
81 Koha::Localization->new(
83 entity => 'itemtypes',
86 translation => 'b translated itemtype desc'
89 Koha::Localization->new(
91 entity => 'itemtypes',
94 translation => 'a translated itemtype desc'
97 Koha::Localization->new(
99 entity => 'something_else',
102 translation => 'another thing'
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' );
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' );
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 {
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(
162 description => 'description',
163 rental_charge_daily => 1.00,
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 calculated 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 calculated correctly with finesCalendar = noFinesWhenClosed" );
180 my $calendar = C4::Calendar->new( branchcode => $library->id );
181 $calendar->insert_week_day_holiday(
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 calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays" );
191 $schema->txn_rollback;