2 use Test::More tests => 4;
8 use C4::Circulation qw( AddIssue AddReturn );
9 use C4::Biblio qw( AddBiblio );
12 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
15 use t::lib::TestBuilder;
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new;
21 my $dbh = C4::Context->dbh;
23 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
24 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
25 my $patron_category = $builder->build({ source => 'Category' });
27 t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
29 # Test without maxsuspensiondays set
30 Koha::CirculationRules->search->delete;
31 Koha::CirculationRules->set_rules(
33 categorycode => undef,
40 suspension_chargeperiod => 1,
45 my $borrowernumber = Koha::Patron->new({
46 firstname => 'my firstname',
47 surname => 'my surname',
48 categorycode => $patron_category->{categorycode},
49 branchcode => $branchcode,
50 })->store->borrowernumber;
51 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
53 my $record = MARC::Record->new();
54 $record->append_fields(
55 MARC::Field->new('100', ' ', ' ', a => 'My author'),
56 MARC::Field->new('245', ' ', ' ', a => 'My title'),
59 my $barcode = 'bc_maxsuspensiondays';
60 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
61 my $itemnumber = Koha::Item->new({
62 biblionumber => $biblionumber,
63 homebranch => $branchcode,
64 holdingbranch => $branchcode,
67 })->store->itemnumber;
69 # clear any holidays to avoid throwing off the suspension day
71 $dbh->do('DELETE FROM special_holidays');
72 $dbh->do('DELETE FROM repeatable_holidays');
74 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
75 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
77 AddIssue( $borrower, $barcode, $daysago20 );
78 AddReturn( $barcode, $branchcode );
79 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
81 $debarments->[0]->{expiration},
82 output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
83 'calculate suspension with no maximum set'
85 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
87 # Test with maxsuspensiondays = 10 days
88 Koha::CirculationRules->set_rules(
90 categorycode => undef,
94 maxsuspensiondays => 10,
99 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
100 AddIssue( $borrower, $barcode, $daysago20 );
101 AddReturn( $barcode, $branchcode );
102 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
104 $debarments->[0]->{expiration},
105 output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
106 'calculate suspension with a maximum set'
108 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
110 subtest "suspension_chargeperiod" => sub {
111 Koha::CirculationRules->set_rules(
113 categorycode => undef,
119 lengthunit => 'days',
120 suspension_chargeperiod => 15,
121 maxsuspensiondays => 333,
125 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
126 my $item = $builder->build_sample_item;
128 my $last_year = dt_from_string->clone->subtract( years => 1 );
129 my $today = dt_from_string;
130 my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron->unblessed, $item->unblessed, $last_year, $today );
131 is( $new_debar_dt->truncate( to => 'day' ),
132 $today->clone->add( days => 365 / 15 * 7 )->truncate( to => 'day' ) );
136 subtest "maxsuspensiondays" => sub {
137 Koha::CirculationRules->set_rules(
139 categorycode => undef,
145 lengthunit => 'days',
146 suspension_chargeperiod => 7,
147 maxsuspensiondays => 333,
151 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
152 my $item = $builder->build_sample_item;
154 my $last_year = dt_from_string->clone->subtract( years => 1 );
155 my $today = dt_from_string;
156 my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron->unblessed, $item->unblessed, $last_year, $today );
157 is( $new_debar_dt->truncate( to => 'day' ),
158 $today->clone->add( days => 333 )->truncate( to => 'day' ) );
161 $schema->storage->txn_rollback;