View | Details | Raw Unified | Return to bug 14834
Collapse All | Expand All

(-)a/C4/Members.pm (-10 / +33 lines)
Lines 1489-1513 sub GetExpiryDate { Link Here
1489
1489
1490
=head2 GetUpcomingMembershipExpires
1490
=head2 GetUpcomingMembershipExpires
1491
1491
1492
  my $upcoming_mem_expires = GetUpcomingMembershipExpires();
1492
    my $expires = GetUpcomingMembershipExpires({
1493
        branch => $branch, before => $before, after => $after,
1494
    });
1495
1496
    $branch is an optional branch code.
1497
    $before/$after is an optional number of days before/after the date that
1498
    is set by the preference MembershipExpiryDaysNotice.
1499
    If the pref would be 14, before 2 and after 3, you will get all expires
1500
    from 12 to 17 days.
1493
1501
1494
=cut
1502
=cut
1495
1503
1496
sub GetUpcomingMembershipExpires {
1504
sub GetUpcomingMembershipExpires {
1505
    my ( $params ) = @_;
1506
    my $before = $params->{before} || 0;
1507
    my $after  = $params->{after} || 0;
1508
    my $branch = $params->{branch};
1509
1497
    my $dbh = C4::Context->dbh;
1510
    my $dbh = C4::Context->dbh;
1498
    my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
1511
    my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
1499
    my $dateexpiry = output_pref({ dt => (dt_from_string()->add( days => $days)), dateformat => 'iso', dateonly => 1 });
1512
    my $date1 = dt_from_string->add( days => $days - $before );
1513
    my $date2 = dt_from_string->add( days => $days + $after );
1514
    $date1= output_pref({ dt => $date1, dateformat => 'iso', dateonly => 1 });
1515
    $date2= output_pref({ dt => $date2, dateformat => 'iso', dateonly => 1 });
1500
1516
1501
    my $query = "
1517
    my $query = q|
1502
        SELECT borrowers.*, categories.description,
1518
        SELECT borrowers.*, categories.description,
1503
        branches.branchname, branches.branchemail FROM borrowers
1519
        branches.branchname, branches.branchemail FROM borrowers
1504
        LEFT JOIN branches on borrowers.branchcode = branches.branchcode
1520
        LEFT JOIN branches USING (branchcode)
1505
        LEFT JOIN categories on borrowers.categorycode = categories.categorycode
1521
        LEFT JOIN categories USING (categorycode)
1506
        WHERE dateexpiry = ?;
1522
    |;
1507
    ";
1523
    if( $branch ) {
1508
    my $sth = $dbh->prepare($query);
1524
        $query.= 'WHERE branchcode=? AND dateexpiry BETWEEN ? AND ?';
1509
    $sth->execute($dateexpiry);
1525
    } else {
1510
    my $results = $sth->fetchall_arrayref({});
1526
        $query.= 'WHERE dateexpiry BETWEEN ? AND ?';
1527
    }
1528
1529
    my $sth = $dbh->prepare( $query );
1530
    my @pars = $branch? ( $branch ): ();
1531
    push @pars, $date1, $date2;
1532
    $sth->execute( @pars );
1533
    my $results = $sth->fetchall_arrayref( {} );
1511
    return $results;
1534
    return $results;
1512
}
1535
}
1513
1536
(-)a/misc/cronjobs/membership_expiry.pl (-3 / +23 lines)
Lines 61-66 the patrons are printed to standard out. Link Here
61
Confirm flag: Add this option. The script will only print a usage
61
Confirm flag: Add this option. The script will only print a usage
62
statement otherwise.
62
statement otherwise.
63
63
64
=item B<-branch>
65
66
Optional branchcode to restrict the cronjob to that branch.
67
68
=item B<-before>
69
70
Optional parameter to extend the selection with a number of days BEFORE
71
the date set by the preference.
72
73
=item B<-after>
74
75
Optional parameter to extend the selection with a number of days AFTER
76
the date set by the preference.
77
64
=back
78
=back
65
79
66
=head1 CONFIGURATION
80
=head1 CONFIGURATION
Lines 115-123 use C4::Log; Link Here
115
# These are defaults for command line options.
129
# These are defaults for command line options.
116
my $confirm;                              # -c: Confirm that the user has read and configured this script.
130
my $confirm;                              # -c: Confirm that the user has read and configured this script.
117
my $nomail;                               # -n: No mail. Will not send any emails.
131
my $nomail;                               # -n: No mail. Will not send any emails.
118
my $verbose= 0;                           # -v: verbose
132
my $verbose = 0;                           # -v: verbose
119
my $help    = 0;
133
my $help    = 0;
120
my $man     = 0;
134
my $man     = 0;
135
my $before  = 0;
136
my $after   = 0;
137
my $branch;
121
138
122
GetOptions(
139
GetOptions(
123
    'help|?'         => \$help,
140
    'help|?'         => \$help,
Lines 125-130 GetOptions( Link Here
125
    'c'              => \$confirm,
142
    'c'              => \$confirm,
126
    'n'              => \$nomail,
143
    'n'              => \$nomail,
127
    'v'              => \$verbose,
144
    'v'              => \$verbose,
145
    'branch:s'       => \$branch,
146
    'before:i'       => \$before,
147
    'after:i'        => \$after,
128
) or pod2usage(2);
148
) or pod2usage(2);
129
149
130
pod2usage( -verbose => 2 ) if $man;
150
pod2usage( -verbose => 2 ) if $man;
Lines 134-140 cronlogaction(); Link Here
134
154
135
my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
155
my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
136
warn 'getting upcoming membership expires' if $verbose;
156
warn 'getting upcoming membership expires' if $verbose;
137
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
157
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires({ branch => $branch, before => $before, after => $after });
138
warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
158
warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
139
    if $verbose;
159
    if $verbose;
140
160
Lines 156-162 foreach my $recent ( @$upcoming_mem_expires ) { Link Here
156
    });
176
    });
157
    if ($letter) {
177
    if ($letter) {
158
        if ($nomail) {
178
        if ($nomail) {
159
            print $letter->{'content'};
179
            print $letter->{'content'}."\n";
160
        } else {
180
        } else {
161
            C4::Letters::EnqueueLetter({
181
            C4::Letters::EnqueueLetter({
162
                letter                 => $letter,
182
                letter                 => $letter,
(-)a/t/db_dependent/Members/GetUpcomingMembershipExpires.t (-32 / +45 lines)
Lines 1-5 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Tests for C4::Members::GetUpcomingMembershipExpires
4
3
# This file is part of Koha.
5
# This file is part of Koha.
4
#
6
#
5
# Copyright 2015 Biblibre
7
# Copyright 2015 Biblibre
Lines 18-34 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
21
20
use Modern::Perl;
22
use Modern::Perl;
21
use C4::Members;
22
use Test::MockModule;
23
use t::lib::TestBuilder;
24
use t::lib::Mocks qw( mock_preference );
25
23
26
use Test::More tests => 5;
27
use Test::MockModule;
24
use Test::MockModule;
25
use Test::More tests => 6;
28
26
29
BEGIN {
27
use C4::Members qw|GetUpcomingMembershipExpires|;
30
    use_ok('C4::Members');
28
use t::lib::TestBuilder;
31
}
29
use t::lib::Mocks qw( mock_preference );
32
30
33
my $date_time = new Test::MockModule('DateTime');
31
my $date_time = new Test::MockModule('DateTime');
34
$date_time->mock(
32
$date_time->mock(
Lines 38-47 $date_time->mock( Link Here
38
            month     => 6,
36
            month     => 6,
39
            day       => 15,
37
            day       => 15,
40
        );
38
        );
41
42
});
39
});
43
40
44
t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 15);
41
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
45
42
46
my $builder = t::lib::TestBuilder->new();
43
my $builder = t::lib::TestBuilder->new();
47
$builder->build({
44
$builder->build({
Lines 55-67 $builder->build({ Link Here
55
    },
52
    },
56
});
53
});
57
54
58
$builder->build({
55
my $branch = $builder->build({
59
    source => 'Branch',
56
    source => 'Branch',
60
    value  => {
57
    value  => {
61
        branchcode              => 'CR',
62
        branchname              => 'My branch',
58
        branchname              => 'My branch',
63
    },
59
    },
64
});
60
});
61
my $branchcode = $branch->{branchcode};
62
# before we add borrowers to this branch, add the expires we have now
63
# note that this pertains to the current mocked setting of the pref
64
# for this reason we add the new branchcode to most of the tests
65
my $expires = scalar @{ GetUpcomingMembershipExpires() };
65
66
66
$builder->build({
67
$builder->build({
67
    source => 'Borrower',
68
    source => 'Borrower',
Lines 70-76 $builder->build({ Link Here
70
        surname                 => 'Martin',
71
        surname                 => 'Martin',
71
        cardnumber              => '80808081',
72
        cardnumber              => '80808081',
72
        categorycode            => 'AD',
73
        categorycode            => 'AD',
73
        branchcode              => 'CR',
74
        branchcode              => $branchcode,
74
        dateexpiry              => '2015-06-30'
75
        dateexpiry              => '2015-06-30'
75
    },
76
    },
76
});
77
});
Lines 82-88 $builder->build({ Link Here
82
        surname                 => 'Dupont',
83
        surname                 => 'Dupont',
83
        cardnumber              => '80808082',
84
        cardnumber              => '80808082',
84
        categorycode            => 'AD',
85
        categorycode            => 'AD',
85
        branchcode              => 'CR',
86
        branchcode              => $branchcode,
86
        dateexpiry              => '2015-06-29'
87
        dateexpiry              => '2015-06-29'
87
    },
88
    },
88
});
89
});
Lines 94-117 $builder->build({ Link Here
94
        surname                 => 'Dupond',
95
        surname                 => 'Dupond',
95
        cardnumber              => '80808083',
96
        cardnumber              => '80808083',
96
        categorycode            => 'AD',
97
        categorycode            => 'AD',
97
        branchcode              => 'CR',
98
        branchcode              => $branchcode,
98
        dateexpiry              => '2015-07-02'
99
        dateexpiry              => '2015-07-02'
99
    },
100
    },
100
});
101
});
101
102
102
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
103
# Test without extra parameters
103
is(scalar(@$upcoming_mem_expires), 1, 'Get upcoming membership expires should return 1 borrower.');
104
my $upcoming_mem_expires = GetUpcomingMembershipExpires();
104
105
is( scalar(@$upcoming_mem_expires), $expires + 1, 'Get upcoming membership expires should return one new borrower.' );
105
is($upcoming_mem_expires->[0]{surname}, 'Martin', 'Get upcoming membership expires should return borrower "Martin".');
106
106
107
# Test with branch
107
# Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == 0
108
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
108
t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 0);
109
is( @$upcoming_mem_expires==1 && $upcoming_mem_expires->[0]{surname} eq 'Martin',1 , 'Get upcoming membership expires should return borrower "Martin".' );
109
110
110
$upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
111
# Test MembershipExpiryDaysNotice == 0
111
is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with 0 MembershipExpiryDaysNotice should return 0.');
112
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
112
113
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
113
# Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == undef
114
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
114
t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', undef);
115
115
116
# Test MembershipExpiryDaysNotice == undef
116
$upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
117
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
117
is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should return 0.');
118
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
119
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
120
121
# Test the before parameter
122
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
123
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1 });
124
# Expect 29/6 and 30/6
125
is( scalar(@$upcoming_mem_expires), 2, 'Expect two results for before==1');
126
# Test after parameter also
127
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1, after => 2 });
128
# Expect 29/6, 30/6 and 2/7
129
is( scalar(@$upcoming_mem_expires), 3, 'Expect three results when adding after' );
130
131
# End
118
- 

Return to bug 14834