2 use Test::More tests => 4;
8 use C4::Circulation qw( AddIssue AddReturn );
9 use C4::Items qw( AddItem );
10 use C4::Biblio qw( AddBiblio );
13 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
16 use t::lib::TestBuilder;
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $builder = t::lib::TestBuilder->new;
22 my $dbh = C4::Context->dbh;
23 $dbh->{RaiseError} = 1;
25 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
26 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
27 my $patron_category = $builder->build({ source => 'Category' });
29 t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
31 # Test without maxsuspensiondays set
32 Koha::CirculationRules->search->delete;
33 Koha::CirculationRules->set_rules(
42 suspension_chargeperiod => 1,
47 my $borrowernumber = Koha::Patron->new({
48 firstname => 'my firstname',
49 surname => 'my surname',
50 categorycode => $patron_category->{categorycode},
51 branchcode => $branchcode,
52 })->store->borrowernumber;
53 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
55 my $record = MARC::Record->new();
56 $record->append_fields(
57 MARC::Field->new('100', ' ', ' ', a => 'My author'),
58 MARC::Field->new('245', ' ', ' ', a => 'My title'),
61 my $barcode = 'bc_maxsuspensiondays';
62 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
63 my (undef, undef, $itemnumber) = AddItem({
64 homebranch => $branchcode,
65 holdingbranch => $branchcode,
70 # clear any holidays to avoid throwing off the suspension day
72 $dbh->do('DELETE FROM special_holidays');
73 $dbh->do('DELETE FROM repeatable_holidays');
75 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
76 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
78 AddIssue( $borrower, $barcode, $daysago20 );
79 AddReturn( $barcode, $branchcode );
80 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
82 $debarments->[0]->{expiration},
83 output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
84 'calculate suspension with no maximum set'
86 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
88 # Test with maxsuspensiondays = 10 days
89 Koha::CirculationRules->set_rules(
95 maxsuspensiondays => 10,
100 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
101 AddIssue( $borrower, $barcode, $daysago20 );
102 AddReturn( $barcode, $branchcode );
103 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
105 $debarments->[0]->{expiration},
106 output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
107 'calculate suspension with a maximum set'
109 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
111 subtest "suspension_chargeperiod" => sub {
112 Koha::IssuingRules->search->delete;
115 source => 'Issuingrule',
122 lengthunit => 'days',
123 suspension_chargeperiod => 15,
124 maxsuspensiondays => 333,
128 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
129 my $item = $builder->build_sample_item;
131 my $last_year = dt_from_string->clone->subtract( years => 1 );
132 my $today = dt_from_string;
133 my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron->unblessed, $item->unblessed, $last_year, $today );
134 is( $new_debar_dt->truncate( to => 'day' ),
135 $today->clone->add( days => 365 / 15 * 7 )->truncate( to => 'day' ) );
139 subtest "maxsuspensiondays" => sub {
140 Koha::IssuingRules->search->delete;
143 source => 'Issuingrule',
150 lengthunit => 'days',
151 suspension_chargeperiod => 7,
152 maxsuspensiondays => 333,
156 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
157 my $item = $builder->build_sample_item;
159 my $last_year = dt_from_string->clone->subtract( years => 1 );
160 my $today = dt_from_string;
161 my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron->unblessed, $item->unblessed, $last_year, $today );
162 is( $new_debar_dt->truncate( to => 'day' ),
163 $today->clone->add( days => 333 )->truncate( to => 'day' ) );
166 $schema->storage->txn_rollback;