Bug 18936: (follow-up) Add foreign key and scope enhancement to circ rules
[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::Items qw( AddItem );
10 use C4::Biblio qw( AddBiblio );
11 use Koha::Database;
12 use Koha::DateUtils;
13 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
14 use Koha::Patrons;
15
16 use t::lib::TestBuilder;
17 use t::lib::Mocks;
18
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;
24
25 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
26 my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
27 my $patron_category = $builder->build({ source => 'Category' });
28
29 t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
30
31 # Test without maxsuspensiondays set
32 Koha::CirculationRules->search->delete;
33 Koha::CirculationRules->set_rules(
34     {
35         categorycode => undef,
36         itemtype     => undef,
37         branchcode   => undef,
38         rules        => {
39             firstremind => 0,
40             finedays    => 2,
41             lengthunit  => 'days',
42             suspension_chargeperiod => 1,
43         }
44     }
45 );
46
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;
54
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'),
59 );
60
61 my $barcode = 'bc_maxsuspensiondays';
62 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
63 my (undef, undef, $itemnumber) = AddItem({
64         homebranch => $branchcode,
65         holdingbranch => $branchcode,
66         barcode => $barcode,
67         itype => $itemtype
68     } , $biblionumber);
69
70 # clear any holidays to avoid throwing off the suspension day
71 # calculations
72 $dbh->do('DELETE FROM special_holidays');
73 $dbh->do('DELETE FROM repeatable_holidays');
74
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));
77
78 AddIssue( $borrower, $barcode, $daysago20 );
79 AddReturn( $barcode, $branchcode );
80 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
81 is(
82     $debarments->[0]->{expiration},
83     output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
84     'calculate suspension with no maximum set'
85 );
86 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
87
88 # Test with maxsuspensiondays = 10 days
89 Koha::CirculationRules->set_rules(
90     {
91         categorycode => undef,
92         itemtype     => undef,
93         branchcode   => undef,
94         rules        => {
95             maxsuspensiondays => 10,
96         }
97     }
98 );
99
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}});
104 is(
105     $debarments->[0]->{expiration},
106     output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
107     'calculate suspension with a maximum set'
108 );
109 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
110
111 subtest "suspension_chargeperiod" => sub {
112     Koha::IssuingRules->search->delete;
113     $builder->build(
114         {
115             source => 'Issuingrule',
116             value  => {
117                 categorycode => '*',
118                 itemtype     => '*',
119                 branchcode   => '*',
120                 firstremind  => 0,
121                 finedays     => 7,
122                 lengthunit   => 'days',
123                 suspension_chargeperiod => 15,
124                 maxsuspensiondays => 333,
125             }
126         }
127     );
128     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
129     my $item = $builder->build_sample_item;
130
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' ) );
136
137 };
138
139 subtest "maxsuspensiondays" => sub {
140     Koha::IssuingRules->search->delete;
141     $builder->build(
142         {
143             source => 'Issuingrule',
144             value  => {
145                 categorycode => '*',
146                 itemtype     => '*',
147                 branchcode   => '*',
148                 firstremind  => 0,
149                 finedays     => 15,
150                 lengthunit   => 'days',
151                 suspension_chargeperiod => 7,
152                 maxsuspensiondays => 333,
153             }
154         }
155     );
156     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
157     my $item = $builder->build_sample_item;
158
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' ) );
164 };
165
166 $schema->storage->txn_rollback;