-=head2 GetFirstValidEmailAddress
-
- $email = GetFirstValidEmailAddress($borrowernumber);
-
-Return the first valid email address for a borrower, given the borrowernumber. For now, the order
-is defined as email, emailpro, B_email. Returns the empty string if the borrower has no email
-addresses.
-
-=cut
-
-sub GetFirstValidEmailAddress {
- my $borrowernumber = shift;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare( "SELECT email, emailpro, B_email FROM borrowers where borrowernumber = ? ");
- $sth->execute( $borrowernumber );
- my $data = $sth->fetchrow_hashref;
-
- if ($data->{'email'}) {
- return $data->{'email'};
- } elsif ($data->{'emailpro'}) {
- return $data->{'emailpro'};
- } elsif ($data->{'B_email'}) {
- return $data->{'B_email'};
- } else {
- return '';
- }
-}
-
-=head2 GetNoticeEmailAddress
-
- $email = GetNoticeEmailAddress($borrowernumber);
-
-Return the email address of borrower used for notices, given the borrowernumber.
-Returns the empty string if no email address.
-
-=cut
-
-sub GetNoticeEmailAddress {
- my $borrowernumber = shift;
-
- my $which_address = C4::Context->preference("AutoEmailPrimaryAddress");
- # if syspref is set to 'first valid' (value == OFF), look up email address
- if ( $which_address eq 'OFF' ) {
- return GetFirstValidEmailAddress($borrowernumber);
- }
- # specified email address field
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare( qq{
- SELECT $which_address AS primaryemail
- FROM borrowers
- WHERE borrowernumber=?
- } );
- $sth->execute($borrowernumber);
- my $data = $sth->fetchrow_hashref;
- return $data->{'primaryemail'} || '';
-}
-
-=head2 GetUpcomingMembershipExpires
-
- my $expires = GetUpcomingMembershipExpires({
- branch => $branch, before => $before, after => $after,
- });
-
- $branch is an optional branch code.
- $before/$after is an optional number of days before/after the date that
- is set by the preference MembershipExpiryDaysNotice.
- If the pref would be 14, before 2 and after 3, you will get all expires
- from 12 to 17 days.
-
-=cut
-
-sub GetUpcomingMembershipExpires {
- my ( $params ) = @_;
- my $before = $params->{before} || 0;
- my $after = $params->{after} || 0;
- my $branch = $params->{branch};
-
- my $dbh = C4::Context->dbh;
- my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
- my $date1 = dt_from_string->add( days => $days - $before );
- my $date2 = dt_from_string->add( days => $days + $after );
- $date1= output_pref({ dt => $date1, dateformat => 'iso', dateonly => 1 });
- $date2= output_pref({ dt => $date2, dateformat => 'iso', dateonly => 1 });
-
- my $query = q|
- SELECT borrowers.*, categories.description,
- branches.branchname, branches.branchemail FROM borrowers
- LEFT JOIN branches USING (branchcode)
- LEFT JOIN categories USING (categorycode)
- |;
- if( $branch ) {
- $query.= 'WHERE branchcode=? AND dateexpiry BETWEEN ? AND ?';
- } else {
- $query.= 'WHERE dateexpiry BETWEEN ? AND ?';
- }
-
- my $sth = $dbh->prepare( $query );
- my @pars = $branch? ( $branch ): ();
- push @pars, $date1, $date2;
- $sth->execute( @pars );
- my $results = $sth->fetchall_arrayref( {} );
- 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);
-
-Returns the HideLostItems preference for the patron category of the supplied borrowernumber
-C<&$hidelostitemspref>return value of function, 0 or 1
-
-=cut
-
-sub GetHideLostItemsPreference {
- my ($borrowernumber) = @_;
- my $dbh = C4::Context->dbh;
- my $query = "SELECT hidelostitems FROM borrowers,categories WHERE borrowers.categorycode = categories.categorycode AND borrowernumber = ?";
- my $sth = $dbh->prepare($query);
- $sth->execute($borrowernumber);
- my $hidelostitems = $sth->fetchrow;
- return $hidelostitems;
-}
-