From e5caccb17a563f19e18789a41d4d9ef127dd34c9 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 11 Jun 2015 12:17:07 +0200 Subject: [PATCH] Bug 6810: Add new systempreference (MembershipExpiryDaysNotice) Signed-off-by: Indranil Das Gupta (L2C2 Technologies) Bug 6810 - Fix QA failures - Use KohaDates to convert dateexpiry - remove MYSQL specifics methods for date handling in GetUpcomingMembershipExpires - make the script membership_expiry.pl write in Koha system logs - add tests Signed-off-by: Indranil Das Gupta (L2C2 Technologies) Bug 6810 - Fix QA failures: - use Koha::DateUtils instead of Koha::Template::Plugin::KohaDates, - Add test with syspref MembershipExpiryDaysNotice equals 0 and undef, - fix (new) test failure (when MembershipExpiryDaysNotice is undef). Signed-off-by: Indranil Das Gupta (L2C2 Technologies) Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Letters.pm | 11 +- C4/Members.pm | 8 +- ...add_MembershipExpiryDaysNotice_syspref.sql | 1 + installer/data/mysql/sysprefs.sql | 3 +- .../en/modules/admin/preferences/patrons.pref | 2 +- misc/cronjobs/membership_expiry.pl | 3 + t/Members.t | 117 ++++++++++++++++++ 7 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_6810-add_MembershipExpiryDaysNotice_syspref.sql create mode 100644 t/Members.t diff --git a/C4/Letters.pm b/C4/Letters.pm index 764f238a78..6f148a9d60 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -35,6 +35,7 @@ use Date::Calc qw( Add_Delta_Days ); use Encode; use Carp; use Koha::Email; +use Koha::DateUtils qw( format_sqldatetime ); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -799,15 +800,7 @@ sub _parseletter { my ( $letter, $table, $values ) = @_; if ( $table eq 'borrowers' && $values->{'dateexpiry'} ){ - my @dateexpiry = split /-/, $values->{'dateexpiry'}; - - $values->{'dateexpiry'} = C4::Dates->new( - sprintf( - '%04d-%02d-%02d', - Add_Delta_Days( @dateexpiry,0) - ), - 'iso' - )->output(); + $values->{'dateexpiry'} = format_sqldatetime( $values->{'dateexpiry'} ); } if ( $table eq 'reserves' && $values->{'waitingdate'} ) { diff --git a/C4/Members.pm b/C4/Members.pm index ebb8a1f62e..ede73da886 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -1493,16 +1493,18 @@ sub GetExpiryDate { sub GetUpcomingMembershipExpires { my $dbh = C4::Context->dbh; - my $days = C4::Context->preference("MembershipExpiryDaysNotice"); + my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0; + my $dateexpiry = DateTime->now()->add(days => $days)->ymd(); + my $query = " SELECT borrowers.*, categories.description, branches.branchname, branches.branchemail FROM borrowers LEFT JOIN branches on borrowers.branchcode = branches.branchcode LEFT JOIN categories on borrowers.categorycode = categories.categorycode - WHERE dateexpiry = DATE_ADD(CURDATE(),INTERVAL $days DAY); + WHERE dateexpiry = ?; "; my $sth = $dbh->prepare($query); - $sth->execute; + $sth->execute($dateexpiry); my $results = $sth->fetchall_arrayref({}); return $results; } diff --git a/installer/data/mysql/atomicupdate/bug_6810-add_MembershipExpiryDaysNotice_syspref.sql b/installer/data/mysql/atomicupdate/bug_6810-add_MembershipExpiryDaysNotice_syspref.sql new file mode 100644 index 0000000000..19bbba92c9 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_6810-add_MembershipExpiryDaysNotice_syspref.sql @@ -0,0 +1 @@ +INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('MembershipExpiryDaysNotice',14,'Send an account expiration notice that a patron''s card is about to expire after',NULL,'Integer') \ No newline at end of file diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 39ed1b6476..dd4b4dcbcb 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -492,5 +492,6 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('XSLTDetailsDisplay','default','','Enable XSL stylesheet control over details page display on intranet','Free'), ('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'), ('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'), -('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo') +('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo'), +('MembershipExpiryDaysNotice' ,14,'Send an account expiration notice that a patron\'s card is about to expire after',NULL,'Integer') ; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 89e07b1014..a0e8587b39 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -84,7 +84,7 @@ Patrons: class: integer - days beforehand. - - - Send a membership expiry notice that a patron is about to expire after + - Send an account expiration notice when a patron's card will expire in. - pref: MembershipExpiryDaysNotice class: integer - days. diff --git a/misc/cronjobs/membership_expiry.pl b/misc/cronjobs/membership_expiry.pl index 2ed3d75959..f92b77fbda 100755 --- a/misc/cronjobs/membership_expiry.pl +++ b/misc/cronjobs/membership_expiry.pl @@ -45,6 +45,9 @@ BEGIN { use C4::Context; use C4::Letters; use C4::Dates qw/format_date/; +use C4::Log; + +cronlogaction(); # These are defaults for command line options. my $confirm; # -c: Confirm that the user has read and configured this script. diff --git a/t/Members.t b/t/Members.t new file mode 100644 index 0000000000..241858faa4 --- /dev/null +++ b/t/Members.t @@ -0,0 +1,117 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright 2015 Biblibre +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use C4::Members; +use Test::MockModule; +use t::lib::TestBuilder; +use t::lib::Mocks qw( mock_preference ); + +use Test::More tests => 5; +use Test::MockModule; + +BEGIN { + use_ok('C4::Members'); +} + +my $date_time = new Test::MockModule('DateTime'); +$date_time->mock( + 'now', sub { + return DateTime->new( + year => 2015, + month => 6, + day => 15, + ); + +}); + +t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 15); + +my $builder = t::lib::TestBuilder->new(); +$builder->build({ + source => 'Category', + value => { + categorycode => 'AD', + description => 'Adult', + enrolmentperiod => 18, + upperagelimit => 99, + category_type => 'A', + }, +}); + +$builder->build({ + source => 'Branch', + value => { + branchcode => 'CR', + branchname => 'My branch', + }, +}); + +$builder->build({ + source => 'Borrower', + value => { + firstname => 'Vincent', + surname => 'Martin', + cardnumber => '80808081', + categorycode => 'AD', + branchcode => 'CR', + dateexpiry => '2015-06-30' + }, +}); + +$builder->build({ + source => 'Borrower', + value => { + firstname => 'Claude', + surname => 'Dupont', + cardnumber => '80808082', + categorycode => 'AD', + branchcode => 'CR', + dateexpiry => '2015-06-29' + }, +}); + +$builder->build({ + source => 'Borrower', + value => { + firstname => 'Gilles', + surname => 'Dupond', + cardnumber => '80808083', + categorycode => 'AD', + branchcode => 'CR', + dateexpiry => '2015-07-02' + }, +}); + +my $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires(); +is(scalar(@$upcoming_mem_expires), 1, 'Get upcoming membership expires should return 1 borrower.'); + +is($upcoming_mem_expires->[0]{surname}, 'Martin', 'Get upcoming membership expires should return borrower "Martin".'); + +# Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == 0 +t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', 0); + +$upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires(); +is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires with 0 MembershipExpiryDaysNotice should return 0.'); + +# Test GetUpcomingMembershipExpires() with MembershipExpiryDaysNotice == undef +t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', undef); + +$upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires(); +is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should return 0.'); -- 2.39.5