From 858c15b50a805da75a0d1723cdbcaa569707c933 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 3 Mar 2015 17:31:09 +1300 Subject: [PATCH] Bug 13607: correct handling of dateexpiry and dateenrolled With this patch: * if a dateexpiry is not provided, it is generated based on the category. * if a dateenrolled is not provided, it is generated based on 'now'. This applies when a user is created only. --- Koha/Borrower/Categories.pm | 67 +++++++++++++++++++++++++++++++++++++++++++++ svc/members/upsert | 11 ++++++++ 2 files changed, 78 insertions(+) create mode 100644 Koha/Borrower/Categories.pm diff --git a/Koha/Borrower/Categories.pm b/Koha/Borrower/Categories.pm new file mode 100644 index 0000000..cddd39f --- /dev/null +++ b/Koha/Borrower/Categories.pm @@ -0,0 +1,67 @@ +package Koha::Borrower::Categories; + +# Copyright 2015 Catalyst IT +# +# This file is part of Koha. +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use C4::Context; + +use Carp; +use Date::Calc qw( Today Add_Delta_Days ); + +=head1 NAME + +Koha::Borrower::Categories - functions for working with borrower category information + +=head1 SYNOPSIS + + use Koha::Borrower::Categories; + $expiry = Koha::Borrower::Categories::GetExpiryDate($catcode, $datefrom); + +=head1 DESCRIPTION + +This is a place for functions that involve dealing with borrower categories. + +=head1 FUNCTIONS + +=head2 get_expiry_date + + my $expiry = Koha::Borrower::Categories::get_expiry_date($catcode, [$datefrom]); + +This calculates the expiry date for a given category. If no origin date is +provided, then it is taken from C. Provided dates must be ISO8601 form, +and that's what will be returned. + +=cut + +sub get_expiry_date { + my ($catcode, $datefrom) = @_; + + croak "No categorycode specified" unless $catcode; + + my @datefrom = $datefrom ? split /-/, $datefrom : Today(); + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare('SELECT enrolmentperiod,enrolmentperioddate FROM categories WHERE categorycode=?'); + $sth->execute($catcode); + $enrolments = $sth->fetchrow_hashref; + if ($enrolments->{enrolmentperiod}) { + return sprintf('%04d-%02d-%02d', Add_Delta_Days(@datefrom, $enrolments->{enrolmentperiod})); + } else { + return $enrolments->{enrolmentperioddate}; + } +} + +1; diff --git a/svc/members/upsert b/svc/members/upsert index b019aa1..b112716 100755 --- a/svc/members/upsert +++ b/svc/members/upsert @@ -78,6 +78,9 @@ use C4::Service; use Koha::Borrower::Search qw( find_borrower find_borrower_from_ext get_borrower_fields ); +use Koha::Borrower::Categories; + +use Date::Calc qw( Today ); process_upsert(); @@ -272,6 +275,14 @@ sub create_borrower { die "Critical field missing (create): $c\n" unless $borr_fields->{$c}; } + # Add enrolement/expiry dates if they're not already there. + unless ($borr_fields->{dateexpiry}) { + $borr_fields->{dateexpiry} = Koha::Borrower::Categories::get_expiry_date($borr_fields->{categorycode}); + } + unless ($borr_fields->{dateenrolled}) { + $borr_fields->{dateenrolled} = sprintf('%04d-%02d-%02d', Today()); + } + # Borrower fields my ( @f_borr, @v_borr ); while ( my ( $f, $v ) = each %$borr_fields ) { -- 2.1.0