Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Circulation / IssuingRules / maxsuspensiondays.t
1 use Modern::Perl;
2 use Test::More tests => 4;
3
4 use MARC::Record;
5 use MARC::Field;
6 use C4::Context;
7
8 use C4::Circulation qw( AddIssue AddReturn );
9 use C4::Biblio qw( AddBiblio );
10 use Koha::Database;
11 use Koha::DateUtils;
12 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
13 use Koha::Patrons;
14
15 use t::lib::TestBuilder;
16 use t::lib::Mocks;
17
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new;
21 my $dbh = C4::Context->dbh;
22
23 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
24 my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
25 my $patron_category = $builder->build({ source => 'Category' });
26
27 t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
28
29 # Test without maxsuspensiondays set
30 Koha::CirculationRules->search->delete;
31 Koha::CirculationRules->set_rules(
32     {
33         categorycode => undef,
34         itemtype     => undef,
35         branchcode   => undef,
36         rules        => {
37             firstremind => 0,
38             finedays    => 2,
39             lengthunit  => 'days',
40             suspension_chargeperiod => 1,
41         }
42     }
43 );
44
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;
52
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'),
57 );
58
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,
65         barcode => $barcode,
66         itype => $itemtype
67     })->store->itemnumber;
68
69 # clear any holidays to avoid throwing off the suspension day
70 # calculations
71 $dbh->do('DELETE FROM special_holidays');
72 $dbh->do('DELETE FROM repeatable_holidays');
73
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));
76
77 AddIssue( $borrower, $barcode, $daysago20 );
78 AddReturn( $barcode, $branchcode );
79 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
80 is(
81     $debarments->[0]->{expiration},
82     output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
83     'calculate suspension with no maximum set'
84 );
85 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
86
87 # Test with maxsuspensiondays = 10 days
88 Koha::CirculationRules->set_rules(
89     {
90         categorycode => undef,
91         itemtype     => undef,
92         branchcode   => undef,
93         rules        => {
94             maxsuspensiondays => 10,
95         }
96     }
97 );
98
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}});
103 is(
104     $debarments->[0]->{expiration},
105     output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
106     'calculate suspension with a maximum set'
107 );
108 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
109
110 subtest "suspension_chargeperiod" => sub {
111     Koha::CirculationRules->set_rules(
112         {
113             categorycode => undef,
114             itemtype     => undef,
115             branchcode   => undef,
116             rules        => {
117                 firstremind  => 0,
118                 finedays     => 7,
119                 lengthunit   => 'days',
120                 suspension_chargeperiod => 15,
121                 maxsuspensiondays => 333,
122             }
123         }
124     );
125     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
126     my $item = $builder->build_sample_item;
127
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' ) );
133
134 };
135
136 subtest "maxsuspensiondays" => sub {
137     Koha::CirculationRules->set_rules(
138         {
139             categorycode => undef,
140             itemtype     => undef,
141             branchcode   => undef,
142             rules        => {
143                 firstremind  => 0,
144                 finedays     => 15,
145                 lengthunit   => 'days',
146                 suspension_chargeperiod => 7,
147                 maxsuspensiondays => 333,
148             }
149         }
150     );
151     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
152     my $item = $builder->build_sample_item;
153
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' ) );
159 };
160
161 $schema->storage->txn_rollback;