From ca8af968f73e59fdd4fcd000d45604133eb89682 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 4 Nov 2016 16:21:03 +0000 Subject: [PATCH] [SIGNED-OFF] Bug 17557: Koha::Patrons - Move GetAge to ->set_age (and remove SetAge) As said in the previous commit, I considered SetAge as unnecessary and removed it. Test plan: 1/ Edit a patron using the different 'Edit' links 2/ Play with the patron category limited to age ranges, and date of birth 3/ You should get the expected warning if the date of birth is inside the patron category date range. To finish: prove t/Circulation/AgeRestrictionMarkers.t t/db_dependent/Reserves.t \ t/db_dependent/Koha/Patrons.t t/db_dependent/Members.t should return green Signed-off-by: Josef Moravec --- C4/Members.pm | 71 ----------------------------------- Koha/Patron.pm | 24 ++++++++++++ members/memberentry.pl | 3 +- members/moremember.pl | 4 +- t/Circulation/AgeRestrictionMarkers.t | 6 +-- t/db_dependent/Reserves.t | 5 +-- 6 files changed, 31 insertions(+), 82 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 8953d85..4ee44ab 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -70,7 +70,6 @@ BEGIN { &GetFirstValidEmailAddress &GetNoticeEmailAddress - &GetAge &GetTitles &GetHideLostItemsPreference @@ -1211,76 +1210,6 @@ sub GetUpcomingMembershipExpires { return $results; } -=head2 GetAge - - $dateofbirth,$date = &GetAge($date); - -this function return the borrowers age with the value of dateofbirth - -=cut - -#' -sub GetAge{ - my ( $date, $date_ref ) = @_; - - if ( not defined $date_ref ) { - $date_ref = sprintf( '%04d-%02d-%02d', Today() ); - } - - my ( $year1, $month1, $day1 ) = split /-/, $date; - my ( $year2, $month2, $day2 ) = split /-/, $date_ref; - - my $age = $year2 - $year1; - if ( $month1 . $day1 > $month2 . $day2 ) { - $age--; - } - - return $age; -} # sub get_age - -=head2 SetAge - - $borrower = C4::Members::SetAge($borrower, $datetimeduration); - $borrower = C4::Members::SetAge($borrower, '0015-12-10'); - $borrower = C4::Members::SetAge($borrower, $datetimeduration, $datetime_reference); - - eval { $borrower = C4::Members::SetAge($borrower, '015-1-10'); }; - if ($@) {print $@;} #Catch a bad ISO Date or kill your script! - -This function sets the borrower's dateofbirth to match the given age. -Optionally relative to the given $datetime_reference. - -@PARAM1 koha.borrowers-object -@PARAM2 DateTime::Duration-object as the desired age - OR a ISO 8601 Date. (To make the API more pleasant) -@PARAM3 DateTime-object as the relative date, defaults to now(). -RETURNS The given borrower reference @PARAM1. -DIES If there was an error with the ISO Date handling. - -=cut - -#' -sub SetAge{ - my ( $borrower, $datetimeduration, $datetime_ref ) = @_; - $datetime_ref = DateTime->now() unless $datetime_ref; - - if ($datetimeduration && ref $datetimeduration ne 'DateTime::Duration') { - if ($datetimeduration =~ /^(\d{4})-(\d{2})-(\d{2})/) { - $datetimeduration = DateTime::Duration->new(years => $1, months => $2, days => $3); - } - else { - die "C4::Members::SetAge($borrower, $datetimeduration), datetimeduration not a valid ISO 8601 Date!\n"; - } - } - - my $new_datetime_ref = $datetime_ref->clone(); - $new_datetime_ref->subtract_duration( $datetimeduration ); - - $borrower->{dateofbirth} = $new_datetime_ref->ymd(); - - return $borrower; -} # sub SetAge - =head2 GetHideLostItemsPreference $hidelostitemspref = &GetHideLostItemsPreference($borrowernumber); diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 9a2fd18..c13464f 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -449,6 +449,30 @@ sub add_enrolment_fee_if_needed { return $enrolment_fee || 0; } +=head3 get_age + +my $age = $patron->get_age + +Return the age of the patron + +=cut + +sub get_age { + my ($self) = @_; + my $today_str = dt_from_string->strftime("%Y-%m-%d"); + my $dob_str = dt_from_string( $self->dateofbirth )->strftime("%Y-%m-%d"); + + my ( $dob_y, $dob_m, $dob_d ) = split /-/, $dob_str; + my ( $today_y, $today_m, $today_d ) = split /-/, $today_str; + + my $age = $today_y - $dob_y; + if ( $dob_m . $dob_d > $today_m . $today_d ) { + $age--; + } + + return $age; +} + =head3 type =cut diff --git a/members/memberentry.pl b/members/memberentry.pl index 27e6687..6f1545c 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -315,7 +315,8 @@ if ($op eq 'save' || $op eq 'insert'){ } if ( $dateofbirth ) { - my $age = GetAge($dateofbirth); + my $patron = Koha::Patron->new({ dateofbirth => $dateofbirth }); + my $age = $patron->get_age; my $borrowercategory = Koha::Patron::Categories->find($categorycode); my ($low,$high) = ($borrowercategory->dateofbirthrequired, $borrowercategory->upperagelimit); if (($high && ($age > $high)) or ($age < $low)) { diff --git a/members/moremember.pl b/members/moremember.pl index 01d16b6..190393f 100755 --- a/members/moremember.pl +++ b/members/moremember.pl @@ -236,9 +236,7 @@ my $overdues_exist = 0; my $totalprice = 0; # Calculate and display patron's age -my $dateofbirth = $data->{ 'dateofbirth' }; -my $age = GetAge($dateofbirth); -$template->param( age => $age ); +$template->param( age => Koha::Patron->new({ dateofbirth => $data->{dateofbirth} })->get_age ); ### ############################################################################### # BUILD HTML diff --git a/t/Circulation/AgeRestrictionMarkers.t b/t/Circulation/AgeRestrictionMarkers.t index 39dd44e..49c1d14 100644 --- a/t/Circulation/AgeRestrictionMarkers.t +++ b/t/Circulation/AgeRestrictionMarkers.t @@ -39,8 +39,7 @@ subtest 'Patron tests - 15 years old' => sub { plan tests => 5; ##Testing age restriction for a borrower. my $now = DateTime->now(); - my $borrower = {}; - C4::Members::SetAge( $borrower, '0015-00-00' ); + my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") }; TestPatron($borrower,0); }; @@ -57,8 +56,7 @@ subtest 'Patron tests - 15 years old (Time Zone shifts)' => sub { ##Testing age restriction for a borrower. my $now = DateTime->now(); - my $borrower = {}; - C4::Members::SetAge( $borrower, '0015-00-00' ); + my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") }; TestPatron($borrower,$offset); $offset++; diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 1d45893..858b138 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -529,14 +529,13 @@ C4::Biblio::ModBiblio( $record, $bibnum, '' ); is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" ); #Set the dateofbirth for the Borrower making him "too young". -my $now = DateTime->now(); -C4::Members::SetAge( $borrower, '0015-00-00' ); +$borrower->{dateofbirth} = DateTime->now->add( years => -15 ); C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} ); is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails"); #Set the dateofbirth for the Borrower making him "too old". -C4::Members::SetAge( $borrower, '0030-00-00' ); +$borrower->{dateofbirth} = DateTime->now->add( years => -30 ); C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} ); is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds"); -- 2.1.4