&GetFirstValidEmailAddress
&GetNoticeEmailAddress
- &GetAge
-
&GetMemberAccountRecords
&GetBorNotifyAcctRecord
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(
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
}
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)) {
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
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);
};
##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++;
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");