From 670cce6f60ac6ce75397b9f3b4c2a2e06ea153c6 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 10 Dec 2015 11:01:29 +0100 Subject: [PATCH] Bug 15206 - C4::Member::GetAge returns years and months MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tested all patches together, works as expected. Signed-off-by: Marc VĂ©ron --- C4/Members.pm | 20 +++++++++++------ members/memberentry.pl | 4 ++-- t/db_dependent/Members.t | 53 +++++++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 19a7cfc..708b98e 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -1640,7 +1640,7 @@ sub GetBorrowercategoryList { =head2 GetAge - $dateofbirth,$date = &GetAge($date); + my ($age_year, $age_month) = &GetAge($dateofbirth); this function return the borrowers age with the value of dateofbirth @@ -1648,21 +1648,27 @@ this function return the borrowers age with the value of dateofbirth #' sub GetAge{ - my ( $date, $date_ref ) = @_; + my ( $dateofbirth, $date_ref ) = @_; if ( not defined $date_ref ) { $date_ref = sprintf( '%04d-%02d-%02d', Today() ); } - my ( $year1, $month1, $day1 ) = split /-/, $date; + my ( $year1, $month1, $day1 ) = split /-/, $dateofbirth; my ( $year2, $month2, $day2 ) = split /-/, $date_ref; - my $age = $year2 - $year1; - if ( $month1 . $day1 > $month2 . $day2 ) { - $age--; + my $age_year = $year2 - $year1; + my $age_month = $month2 - $month1; + my $age_day = $day2 - $day1; + + $age_month-- if $age_day < 0; + + if ($age_month < 0) { + $age_year--; + $age_month += 12; } - return $age; + return ($age_year, $age_month); } # sub get_age =head2 SetAge diff --git a/members/memberentry.pl b/members/memberentry.pl index ae54383..1bd457c 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -289,10 +289,10 @@ if ($op eq 'save' || $op eq 'insert'){ } if ( $newdata{dateofbirth} ) { - my $age = GetAge($newdata{dateofbirth}); + my ($age_year, $age_month) = GetAge($newdata{dateofbirth}); my $borrowercategory=GetBorrowercategory($newdata{'categorycode'}); my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'}); - if (($high && ($age > $high)) or ($age < $low)) { + if (($high && ($age_year > $high)) or ($age_year < $low)) { push @errors, 'ERROR_age_limitations'; $template->param( age_low => $low); $template->param( age_high => $high); diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index 4c87e84..3157729 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 74; +use Test::More tests => 79; use Test::MockModule; use Data::Dumper; use C4::Context; @@ -374,43 +374,48 @@ sub testAgeAccessors { my $original_dateofbirth = $member->{dateofbirth}; ##Testing GetAge() - my $age=GetAge("1992-08-14", "2011-01-19"); - is ($age, "18", "Age correct"); + my ($age_year, $age_month) = GetAge("1992-08-14", "2011-01-19"); + is ($age_year, "18", "Age year correct"); + is ($age_month, "5", "Age month correct"); - $age=GetAge("2011-01-19", "1992-01-19"); - is ($age, "-19", "Birthday In the Future"); + ($age_year, $age_month) = GetAge("2011-01-19", "1992-01-19"); + is ($age_year, "-19", "Birthday In the Future"); ##Testing SetAge() for now() my $dt_now = DateTime->now(); - $age = DateTime::Duration->new(years => 12, months => 6, days => 1); + my $age = DateTime::Duration->new(years => 12, months => 6, days => 1); C4::Members::SetAge( $member, $age ); - $age = C4::Members::GetAge( $member->{dateofbirth} ); - is ($age, '12', "SetAge 12 years"); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth} ); + is ($age_year, '12', "SetAge 12 years and 6 months"); + is ($age_month, '6', "SetAge 12 years and 6 months"); $age = DateTime::Duration->new(years => 18, months => 12, days => 31); C4::Members::SetAge( $member, $age ); - $age = C4::Members::GetAge( $member->{dateofbirth} ); - is ($age, '19', "SetAge 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18. + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth} ); + is ($age_year, '19', "SetAge 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18. + is ($age_month, '1', "SetAge 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18. $age = DateTime::Duration->new(years => 18, months => 12, days => 30); C4::Members::SetAge( $member, $age ); - $age = C4::Members::GetAge( $member->{dateofbirth} ); - is ($age, '19', "SetAge 18 years"); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth} ); + is ($age_year, '19', "SetAge 18 years"); + is ($age_month, '1', "SetAge 18 years"); $age = DateTime::Duration->new(years => 0, months => 1, days => 1); C4::Members::SetAge( $member, $age ); - $age = C4::Members::GetAge( $member->{dateofbirth} ); - is ($age, '0', "SetAge 0 years"); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth} ); + is ($age_year, '0', "SetAge 0 years"); + is ($age_month, '1', "SetAge 0 years"); $age = '0018-12-31'; C4::Members::SetAge( $member, $age ); - $age = C4::Members::GetAge( $member->{dateofbirth} ); - is ($age, '19', "SetAge ISO_Date 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18. + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth} ); + is ($age_year, '19', "SetAge ISO_Date 18+1 years"); #This is a special case, where months=>12 and days=>31 constitute one full year, hence we get age 19 instead of 18. $age = '0018-12-30'; C4::Members::SetAge( $member, $age ); - $age = C4::Members::GetAge( $member->{dateofbirth} ); - is ($age, '19', "SetAge ISO_Date 18 years"); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth} ); + is ($age_year, '19', "SetAge ISO_Date 18 years"); $age = '18-1-1'; eval { C4::Members::SetAge( $member, $age ); }; @@ -425,18 +430,18 @@ sub testAgeAccessors { $age = DateTime::Duration->new(years => 10, months => 3); C4::Members::SetAge( $member, $age, $relative_date ); - $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() ); - is ($age, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd()); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() ); + is ($age_year, '10', "SetAge, 10 years and 3 months old person was born on ".$member->{dateofbirth}." if todays is ".$relative_date->ymd()); $age = DateTime::Duration->new(years => 112, months => 1, days => 1); C4::Members::SetAge( $member, $age, $relative_date ); - $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() ); - is ($age, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd()); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() ); + is ($age_year, '112', "SetAge, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd()); $age = '0112-01-01'; C4::Members::SetAge( $member, $age, $relative_date ); - $age = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() ); - is ($age, '112', "SetAge ISO_Date, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd()); + ($age_year, $age_month) = C4::Members::GetAge( $member->{dateofbirth}, $relative_date->ymd() ); + is ($age_year, '112', "SetAge ISO_Date, 112 years, 1 months and 1 days old person was born on ".$member->{dateofbirth}." if today is ".$relative_date->ymd()); $member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests. } #sub testAgeAccessors -- 1.7.10.4