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

(-)a/C4/Members.pm (-10 / +33 lines)
Lines 1330-1354 sub GetExpiryDate { Link Here
1330
1330
1331
=head2 GetUpcomingMembershipExpires
1331
=head2 GetUpcomingMembershipExpires
1332
1332
1333
  my $upcoming_mem_expires = GetUpcomingMembershipExpires();
1333
    my $expires = GetUpcomingMembershipExpires({
1334
        branch => $branch, before => $before, after => $after,
1335
    });
1336
1337
    $branch is an optional branch code.
1338
    $before/$after is an optional number of days before/after the date that
1339
    is set by the preference MembershipExpiryDaysNotice.
1340
    If the pref would be 14, before 2 and after 3, you will get all expires
1341
    from 12 to 17 days.
1334
1342
1335
=cut
1343
=cut
1336
1344
1337
sub GetUpcomingMembershipExpires {
1345
sub GetUpcomingMembershipExpires {
1346
    my ( $params ) = @_;
1347
    my $before = $params->{before} || 0;
1348
    my $after  = $params->{after} || 0;
1349
    my $branch = $params->{branch};
1350
1338
    my $dbh = C4::Context->dbh;
1351
    my $dbh = C4::Context->dbh;
1339
    my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
1352
    my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
1340
    my $dateexpiry = output_pref({ dt => (dt_from_string()->add( days => $days)), dateformat => 'iso', dateonly => 1 });
1353
    my $date1 = dt_from_string->add( days => $days - $before );
1354
    my $date2 = dt_from_string->add( days => $days + $after );
1355
    $date1= output_pref({ dt => $date1, dateformat => 'iso', dateonly => 1 });
1356
    $date2= output_pref({ dt => $date2, dateformat => 'iso', dateonly => 1 });
1341
1357
1342
    my $query = "
1358
    my $query = q|
1343
        SELECT borrowers.*, categories.description,
1359
        SELECT borrowers.*, categories.description,
1344
        branches.branchname, branches.branchemail FROM borrowers
1360
        branches.branchname, branches.branchemail FROM borrowers
1345
        LEFT JOIN branches on borrowers.branchcode = branches.branchcode
1361
        LEFT JOIN branches USING (branchcode)
1346
        LEFT JOIN categories on borrowers.categorycode = categories.categorycode
1362
        LEFT JOIN categories USING (categorycode)
1347
        WHERE dateexpiry = ?;
1363
    |;
1348
    ";
1364
    if( $branch ) {
1349
    my $sth = $dbh->prepare($query);
1365
        $query.= 'WHERE branchcode=? AND dateexpiry BETWEEN ? AND ?';
1350
    $sth->execute($dateexpiry);
1366
    } else {
1351
    my $results = $sth->fetchall_arrayref({});
1367
        $query.= 'WHERE dateexpiry BETWEEN ? AND ?';
1368
    }
1369
1370
    my $sth = $dbh->prepare( $query );
1371
    my @pars = $branch? ( $branch ): ();
1372
    push @pars, $date1, $date2;
1373
    $sth->execute( @pars );
1374
    my $results = $sth->fetchall_arrayref( {} );
1352
    return $results;
1375
    return $results;
1353
}
1376
}
1354
1377
(-)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 142-148 if( !$expdays ) { Link Here
142
162
143
my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
163
my $admin_adress = C4::Context->preference('KohaAdminEmailAddress');
144
warn 'getting upcoming membership expires' if $verbose;
164
warn 'getting upcoming membership expires' if $verbose;
145
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
165
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires({ branch => $branch, before => $before, after => $after });
146
warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
166
warn 'found ' . scalar( @$upcoming_mem_expires ) . ' soon expiring members'
147
    if $verbose;
167
    if $verbose;
148
168
Lines 164-170 foreach my $recent ( @$upcoming_mem_expires ) { Link Here
164
    });
184
    });
165
    if ($letter) {
185
    if ($letter) {
166
        if ($nomail) {
186
        if ($nomail) {
167
            print $letter->{'content'};
187
            print $letter->{'content'}."\n";
168
        } else {
188
        } else {
169
            C4::Letters::EnqueueLetter({
189
            C4::Letters::EnqueueLetter({
170
                letter                 => $letter,
190
                letter                 => $letter,
(-)a/t/db_dependent/Members/GetUpcomingMembershipExpires.t (-35 / +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 19-35 Link Here
19
21
20
use Modern::Perl;
22
use Modern::Perl;
21
23
22
use Test::More tests => 5;
23
use Test::MockModule;
24
use Test::MockModule;
24
use t::lib::TestBuilder;
25
use Test::More tests => 6;
25
use t::lib::Mocks qw( mock_preference );
26
26
27
use C4::Members;
27
use C4::Members qw|GetUpcomingMembershipExpires|;
28
use Koha::Database;
28
use Koha::Database;
29
29
use t::lib::TestBuilder;
30
BEGIN {
30
use t::lib::Mocks qw( mock_preference );
31
    use_ok('C4::Members');
32
}
33
31
34
my $schema = Koha::Database->new->schema;
32
my $schema = Koha::Database->new->schema;
35
$schema->storage->txn_begin;
33
$schema->storage->txn_begin;
Lines 42-51 $date_time->mock( Link Here
42
            month     => 6,
40
            month     => 6,
43
            day       => 15,
41
            day       => 15,
44
        );
42
        );
45
46
});
43
});
47
44
48
t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 15);
45
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
49
46
50
my $builder = t::lib::TestBuilder->new();
47
my $builder = t::lib::TestBuilder->new();
51
$builder->build({
48
$builder->build({
Lines 59-71 $builder->build({ Link Here
59
    },
56
    },
60
});
57
});
61
58
62
$builder->build({
59
my $branch = $builder->build({
63
    source => 'Branch',
60
    source => 'Branch',
64
    value  => {
61
    value  => {
65
        branchcode              => 'CR',
66
        branchname              => 'My branch',
62
        branchname              => 'My branch',
67
    },
63
    },
68
});
64
});
65
my $branchcode = $branch->{branchcode};
66
# before we add borrowers to this branch, add the expires we have now
67
# note that this pertains to the current mocked setting of the pref
68
# for this reason we add the new branchcode to most of the tests
69
my $expires = scalar @{ GetUpcomingMembershipExpires() };
69
70
70
$builder->build({
71
$builder->build({
71
    source => 'Borrower',
72
    source => 'Borrower',
Lines 74-80 $builder->build({ Link Here
74
        surname                 => 'Martin',
75
        surname                 => 'Martin',
75
        cardnumber              => '80808081',
76
        cardnumber              => '80808081',
76
        categorycode            => 'AD',
77
        categorycode            => 'AD',
77
        branchcode              => 'CR',
78
        branchcode              => $branchcode,
78
        dateexpiry              => '2015-06-30'
79
        dateexpiry              => '2015-06-30'
79
    },
80
    },
80
});
81
});
Lines 86-92 $builder->build({ Link Here
86
        surname                 => 'Dupont',
87
        surname                 => 'Dupont',
87
        cardnumber              => '80808082',
88
        cardnumber              => '80808082',
88
        categorycode            => 'AD',
89
        categorycode            => 'AD',
89
        branchcode              => 'CR',
90
        branchcode              => $branchcode,
90
        dateexpiry              => '2015-06-29'
91
        dateexpiry              => '2015-06-29'
91
    },
92
    },
92
});
93
});
Lines 98-125 $builder->build({ Link Here
98
        surname                 => 'Dupond',
99
        surname                 => 'Dupond',
99
        cardnumber              => '80808083',
100
        cardnumber              => '80808083',
100
        categorycode            => 'AD',
101
        categorycode            => 'AD',
101
        branchcode              => 'CR',
102
        branchcode              => $branchcode,
102
        dateexpiry              => '2015-07-02'
103
        dateexpiry              => '2015-07-02'
103
    },
104
    },
104
});
105
});
105
106
106
my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
107
# Test without extra parameters
107
is(scalar(@$upcoming_mem_expires), 1, 'Get upcoming membership expires should return 1 borrower.');
108
my $upcoming_mem_expires = GetUpcomingMembershipExpires();
108
109
is( scalar(@$upcoming_mem_expires), $expires + 1, 'Get upcoming membership expires should return one new borrower.' );
109
is($upcoming_mem_expires->[0]{surname}, 'Martin', 'Get upcoming membership expires should return borrower "Martin".');
110
110
111
# Test with branch
111
# Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == 0
112
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
112
t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 0);
113
is( @$upcoming_mem_expires==1 && $upcoming_mem_expires->[0]{surname} eq 'Martin',1 , 'Get upcoming membership expires should return borrower "Martin".' );
113
114
114
$upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
115
# Test MembershipExpiryDaysNotice == 0
115
is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with 0 MembershipExpiryDaysNotice should return 0.');
116
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 0 );
116
117
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
117
# Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == undef
118
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with MembershipExpiryDaysNotice==0 should not return new records.' );
118
t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', undef);
119
119
120
# Test MembershipExpiryDaysNotice == undef
120
$upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires();
121
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', undef );
121
is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should return 0.');
122
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode });
122
123
is( scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should not return new records.' );
124
125
# Test the before parameter
126
t::lib::Mocks::mock_preference( 'MembershipExpiryDaysNotice', 15 );
127
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1 });
128
# Expect 29/6 and 30/6
129
is( scalar(@$upcoming_mem_expires), 2, 'Expect two results for before==1');
130
# Test after parameter also
131
$upcoming_mem_expires = GetUpcomingMembershipExpires({ branch => $branchcode, before => 1, after => 2 });
132
# Expect 29/6, 30/6 and 2/7
133
is( scalar(@$upcoming_mem_expires), 3, 'Expect three results when adding after' );
134
135
# End
123
$schema->storage->txn_rollback;
136
$schema->storage->txn_rollback;
124
125
1;
126
- 

Return to bug 14834