@@ -, +, @@ remove SetAge) prove t/Circulation/AgeRestrictionMarkers.t t/db_dependent/Reserves.t \ t/db_dependent/Koha/Patrons.t t/db_dependent/Members.t --- C4/Members.pm | 72 ----------------------------------- 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(+), 83 deletions(-) --- a/C4/Members.pm +++ a/C4/Members.pm @@ -70,8 +70,6 @@ BEGIN { &GetFirstValidEmailAddress &GetNoticeEmailAddress - &GetAge - &GetMemberAccountRecords &GetBorNotifyAcctRecord @@ -1211,76 +1209,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 GetBorrowersToExpunge $borrowers = &GetBorrowersToExpunge( --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -519,6 +519,30 @@ sub get_overdues { return $issues; } +=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 --- a/members/memberentry.pl +++ a/members/memberentry.pl @@ -316,7 +316,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)) { --- a/members/moremember.pl +++ a/members/moremember.pl @@ -237,9 +237,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 --- a/t/Circulation/AgeRestrictionMarkers.t +++ a/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++; --- a/t/db_dependent/Reserves.t +++ a/t/db_dependent/Reserves.t @@ -526,14 +526,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"); --