From 4372797b134ba2c3b4738efca60dfe121866a187 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 11 Jul 2016 23:45:55 +0100 Subject: [PATCH] Bug 16911: Koha::Patron::Categories - Move GetExpiryDate to ->get_expiry_date This patch moves the code from C4::Members::GetExpiryDate to Koha::Patron::Categories. Nothing much exiting, please follow test plan in next patches. Signed-off-by: Aleisha Amohia Signed-off-by: Tomas Cohen Arazi --- C4/Members.pm | 37 ++++--------------------------------- Koha/Patron/Category.pm | 11 +++++++++++ members/memberentry.pl | 7 ++++--- tools/import_borrowers.pl | 6 +++--- 4 files changed, 22 insertions(+), 39 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 061172f..432cbb6 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -25,7 +25,7 @@ use strict; use C4::Context; use String::Random qw( random_string ); use Scalar::Util qw( looks_like_number ); -use Date::Calc qw/Today Add_Delta_YM check_date Date_to_Days/; +use Date::Calc qw/Today check_date Date_to_Days/; use C4::Log; # logaction use C4::Overdues; use C4::Reserves; @@ -43,6 +43,7 @@ use Koha::Database; use Koha::Holds; use Koha::List::Patron; use Koha::Patrons; +use Koha::Patron::Categories; our (@ISA,@EXPORT,@EXPORT_OK,$debug); @@ -87,7 +88,6 @@ BEGIN { &GetBorrowersWhoHaveNeverBorrowed &GetBorrowersWithIssuesHistoryOlderThan - &GetExpiryDate &GetUpcomingMembershipExpires &IssueSlip @@ -657,9 +657,7 @@ sub AddMember { if ( $data{'userid'} eq '' || !Check_Userid( $data{'userid'} ) ); # add expiration date if it isn't already there - unless ( $data{'dateexpiry'} ) { - $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ) ); - } + $data{dateexpiry} ||= Koha::Patron::Categories->find( $data{categorycode} )->get_expiry_date; # add enrollment date if it isn't already there unless ( $data{'dateenrolled'} ) { @@ -1223,33 +1221,6 @@ sub GetNoticeEmailAddress { return $data->{'primaryemail'} || ''; } -=head2 GetExpiryDate - - $expirydate = GetExpiryDate($categorycode, $dateenrolled); - -Calculate expiry date given a categorycode and starting date. Date argument must be in ISO format. -Return date is also in ISO format. - -=cut - -sub GetExpiryDate { - my ( $categorycode, $dateenrolled ) = @_; - my $enrolments; - if ($categorycode) { - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT enrolmentperiod,enrolmentperioddate FROM categories WHERE categorycode=?"); - $sth->execute($categorycode); - $enrolments = $sth->fetchrow_hashref; - } - # die "GetExpiryDate: for enrollmentperiod $enrolmentperiod (category '$categorycode') starting $dateenrolled.\n"; - my @date = split (/-/,$dateenrolled); - if($enrolments->{enrolmentperiod}){ - return sprintf("%04d-%02d-%02d", Add_Delta_YM(@date,0,$enrolments->{enrolmentperiod})); - }else{ - return $enrolments->{enrolmentperioddate}; - } -} - =head2 GetUpcomingMembershipExpires my $expires = GetUpcomingMembershipExpires({ @@ -1624,7 +1595,7 @@ sub ExtendMemberSubscriptionTo { eval { output_pref( { dt => dt_from_string( $borrower->{'dateexpiry'} ), dateonly => 1, dateformat => 'iso' } ); } : output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } ); - $date = GetExpiryDate( $borrower->{'categorycode'}, $date ); + $date = Koha::Patron::Categories->find( $borrower->{categorycode} )->get_expiry_date( $date ); } my $sth = $dbh->do(<{_catb_resultset}; } +sub get_expiry_date { + my ($self, $date ) = @_; + if ( $self->enrolmentperiod ) { + $date ||= dt_from_string; + return $date->add( months => $self->enrolmentperiod ); + } else { + return $self->enrolmentperioddate; + } +} + =head3 type =cut diff --git a/members/memberentry.pl b/members/memberentry.pl index 413db97..c5bb894 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -370,8 +370,8 @@ if ($op eq 'save' || $op eq 'insert'){ if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){ unless ($newdata{'dateexpiry'}){ - my $arg2 = $newdata{'dateenrolled'} || output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); - $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2); + my $patron_category = Koha::Patron::Categories->find( $newdata{categorycode} ); + $newdata{'dateexpiry'} = $patron_category->get_expiry_date( $newdata{dateenrolled} ); } } @@ -632,7 +632,8 @@ if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){ } if ( $op eq 'duplicate' ) { $data{'dateenrolled'} = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }); - $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, $data{'dateenrolled'} ); + my $patron_category = Koha::Patron::Categories->find( $data{categorycode} ); + $data{dateexpiry} = $patron_category->get_expiry_date( $data{dateenrolled} ); } if (C4::Context->preference('uppercasesurnames')) { $data{'surname'} &&= uc( $data{'surname'} ); diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index d350a7f..064062a 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -151,7 +151,7 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { } push @feedback, {feedback=>1, name=>'headerrow', value=>join(', ', @csvcolumns)}; - my $today_iso = output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' }); + my $today = output_pref; my @criticals = qw(surname branchcode categorycode); # there probably should be others my @bad_dates; # I've had a few. LINE: while ( my $borrowerline = <$handle> ) { @@ -227,8 +227,8 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { push @missing_criticals, {key=>$_, line=>$. , lineraw=>$borrowerline, bad_date=>1}; } } - $borrower{dateenrolled} = $today_iso unless $borrower{dateenrolled}; - $borrower{dateexpiry} = GetExpiryDate($borrower{categorycode},$borrower{dateenrolled}) unless $borrower{dateexpiry}; + $borrower{dateenrolled} ||= $today; + $borrower{dateexpiry} ||= Koha::Patron::Categories->find( $borrower{categorycode} )->get_expiry_date( $borrower{dateenrolled} ); my $borrowernumber; my $member; if ( ($matchpoint eq 'cardnumber') && ($borrower{'cardnumber'}) ) { -- 2.7.4