2 use Test::More tests => 2;
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::IssuingRules->search->delete;
35 source => 'Issuingrule',
43 suspension_chargeperiod => 1,
48 my $borrowernumber = Koha::Patron->new({
49 firstname => 'my firstname',
50 surname => 'my surname',
51 categorycode => $patron_category->{categorycode},
52 branchcode => $branchcode,
53 })->store->borrowernumber;
54 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
56 my $record = MARC::Record->new();
57 $record->append_fields(
58 MARC::Field->new('100', ' ', ' ', a => 'My author'),
59 MARC::Field->new('245', ' ', ' ', a => 'My title'),
62 my $barcode = 'bc_maxsuspensiondays';
63 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
64 my (undef, undef, $itemnumber) = AddItem({
65 homebranch => $branchcode,
66 holdingbranch => $branchcode,
71 # clear any holidays to avoid throwing off the suspension day
73 $dbh->do('DELETE FROM special_holidays');
74 $dbh->do('DELETE FROM repeatable_holidays');
76 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
77 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
79 AddIssue( $borrower, $barcode, $daysago20 );
80 AddReturn( $barcode, $branchcode );
81 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
83 $debarments->[0]->{expiration},
84 output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
85 'calculate suspension with no maximum set'
87 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
89 # Test with maxsuspensiondays = 10 days
90 my $issuing_rule = Koha::IssuingRules->search->next;
91 $issuing_rule->maxsuspensiondays( 10 )->store;
93 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
94 AddIssue( $borrower, $barcode, $daysago20 );
95 AddReturn( $barcode, $branchcode );
96 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
98 $debarments->[0]->{expiration},
99 output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
100 'calculate suspension with a maximum set'
102 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
104 $schema->storage->txn_rollback;