-=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 GetExpiryDate
-
- $expirydate = GetExpiryDate($categorycode, $dateenrolled);
-
-Calculate expiry date given a categorycode and starting date. Date argument must be in ISO format.
-Return date is also in ISO format.
-
-=cut
-
-sub GetExpiryDate {
- my ( $categorycode, $dateenrolled ) = @_;
- my $enrolments;
- if ($categorycode) {
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("SELECT enrolmentperiod,enrolmentperioddate FROM categories WHERE categorycode=?");
- $sth->execute($categorycode);
- $enrolments = $sth->fetchrow_hashref;
- }
- # die "GetExpiryDate: for enrollmentperiod $enrolmentperiod (category '$categorycode') starting $dateenrolled.\n";
- my @date = split (/-/,$dateenrolled);
- if($enrolments->{enrolmentperiod}){
- return sprintf("%04d-%02d-%02d", Add_Delta_YM(@date,0,$enrolments->{enrolmentperiod}));
- }else{
- return $enrolments->{enrolmentperioddate};
- }
-}
-
-=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 GetborCatFromCatType
-
- ($codes_arrayref, $labels_hashref) = &GetborCatFromCatType();
-
-Looks up the different types of borrowers in the database. Returns two
-elements: a reference-to-array, which lists the borrower category
-codes, and a reference-to-hash, which maps the borrower category codes
-to category descriptions.
-
-=cut
-
-#'
-sub GetborCatFromCatType {
- my ( $category_type, $action, $no_branch_limit ) = @_;
-
- my $branch_limit = $no_branch_limit
- ? 0
- : C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
-
- # FIXME - This API seems both limited and dangerous.
- my $dbh = C4::Context->dbh;
-
- my $request = qq{
- SELECT categories.categorycode, categories.description
- FROM categories
- };
- $request .= qq{
- LEFT JOIN categories_branches ON categories.categorycode = categories_branches.categorycode
- } if $branch_limit;
- if($action) {
- $request .= " $action ";
- $request .= " AND (branchcode = ? OR branchcode IS NULL) GROUP BY description" if $branch_limit;
- } else {
- $request .= " WHERE branchcode = ? OR branchcode IS NULL GROUP BY description" if $branch_limit;
- }
- $request .= " ORDER BY categorycode";
-
- my $sth = $dbh->prepare($request);
- $sth->execute(
- $action ? $category_type : (),
- $branch_limit ? $branch_limit : ()
- );
-
- my %labels;
- my @codes;
-
- while ( my $data = $sth->fetchrow_hashref ) {
- push @codes, $data->{'categorycode'};
- $labels{ $data->{'categorycode'} } = $data->{'description'};
- }
- $sth->finish;
- return ( \@codes, \%labels );
-}
-
-=head2 GetBorrowercategory
-
- $hashref = &GetBorrowercategory($categorycode);
-
-Given the borrower's category code, the function returns the corresponding
-data hashref for a comprehensive information display.
-
-=cut
-
-sub GetBorrowercategory {
- my ($catcode) = @_;
- my $dbh = C4::Context->dbh;
- if ($catcode){
- my $sth =
- $dbh->prepare(
- "SELECT description,dateofbirthrequired,upperagelimit,category_type
- FROM categories
- WHERE categorycode = ?"
- );
- $sth->execute($catcode);
- my $data =
- $sth->fetchrow_hashref;
- return $data;
- }
- return;
-} # sub getborrowercategory
-
-
-=head2 GetBorrowerCategorycode
-
- $categorycode = &GetBorrowerCategoryCode( $borrowernumber );
-
-Given the borrowernumber, the function returns the corresponding categorycode
-
-=cut
-
-sub GetBorrowerCategorycode {
- my ( $borrowernumber ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare( qq{
- SELECT categorycode
- FROM borrowers
- WHERE borrowernumber = ?
- } );
- $sth->execute( $borrowernumber );
- return $sth->fetchrow;
-}
-
-=head2 GetBorrowercategoryList
-
- $arrayref_hashref = &GetBorrowercategoryList;
-If no category code provided, the function returns all the categories.
-
-=cut
-
-sub GetBorrowercategoryList {
- my $no_branch_limit = @_ ? shift : 0;
- my $branch_limit = $no_branch_limit
- ? 0
- : C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
- my $dbh = C4::Context->dbh;
- my $query = "SELECT categories.* FROM categories";
- $query .= qq{
- LEFT JOIN categories_branches ON categories.categorycode = categories_branches.categorycode
- WHERE branchcode = ? OR branchcode IS NULL GROUP BY description
- } if $branch_limit;
- $query .= " ORDER BY description";
- my $sth = $dbh->prepare( $query );
- $sth->execute( $branch_limit ? $branch_limit : () );
- my $data = $sth->fetchall_arrayref( {} );
- $sth->finish;
- return $data;
-} # sub getborrowercategory
-
-=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 GetSortDetails (OUEST-PROVENCE)
-
- ($lib) = &GetSortDetails($category,$sortvalue);
-
-Returns the authorized value details
-C<&$lib>return value of authorized value details
-C<&$sortvalue>this is the value of authorized value
-C<&$category>this is the value of authorized value category
-
-=cut
-
-sub GetSortDetails {
- my ( $category, $sortvalue ) = @_;
- my $dbh = C4::Context->dbh;
- my $query = qq|SELECT lib
- FROM authorised_values
- WHERE category=?
- AND authorised_value=? |;
- my $sth = $dbh->prepare($query);
- $sth->execute( $category, $sortvalue );
- my $lib = $sth->fetchrow;
- return ($lib) if ($lib);
- return ($sortvalue) unless ($lib);
-}
-
-=head2 MoveMemberToDeleted
-
- $result = &MoveMemberToDeleted($borrowernumber);
-
-Copy the record from borrowers to deletedborrowers table.
-The routine returns 1 for success, undef for failure.
-
-=cut
-
-sub MoveMemberToDeleted {
- my ($member) = shift or return;
-
- my $schema = Koha::Database->new()->schema();
- my $borrowers_rs = $schema->resultset('Borrower');
- $borrowers_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
- my $borrower = $borrowers_rs->find($member);
- return unless $borrower;
-
- my $deleted = $schema->resultset('Deletedborrower')->create($borrower);
-
- return $deleted ? 1 : undef;
-}
-
-=head2 DelMember
-
- DelMember($borrowernumber);
-
-This function remove directly a borrower whitout writing it on deleteborrower.
-+ Deletes reserves for the borrower
-
-=cut
-
-sub DelMember {
- my $dbh = C4::Context->dbh;
- my $borrowernumber = shift;
- #warn "in delmember with $borrowernumber";
- return unless $borrowernumber; # borrowernumber is mandatory.
-
- my $query = qq|DELETE
- FROM reserves
- WHERE borrowernumber=?|;
- my $sth = $dbh->prepare($query);
- $sth->execute($borrowernumber);
- $query = "
- DELETE
- FROM borrowers
- WHERE borrowernumber = ?
- ";
- $sth = $dbh->prepare($query);
- $sth->execute($borrowernumber);
- logaction("MEMBERS", "DELETE", $borrowernumber, "") if C4::Context->preference("BorrowersLog");
- return $sth->rows;
-}
-
-=head2 HandleDelBorrower
-
- HandleDelBorrower($borrower);
-
-When a member is deleted (DelMember in Members.pm), you should call me first.
-This routine deletes/moves lists and entries for the deleted member/borrower.
-Lists owned by the borrower are deleted, but entries from the borrower to
-other lists are kept.
-
-=cut
-
-sub HandleDelBorrower {
- my ($borrower)= @_;
- my $query;
- my $dbh = C4::Context->dbh;
-
- #Delete all lists and all shares of this borrower
- #Consistent with the approach Koha uses on deleting individual lists
- #Note that entries in virtualshelfcontents added by this borrower to
- #lists of others will be handled by a table constraint: the borrower
- #is set to NULL in those entries.
- $query="DELETE FROM virtualshelves WHERE owner=?";
- $dbh->do($query,undef,($borrower));
-
- #NOTE:
- #We could handle the above deletes via a constraint too.
- #But a new BZ report 11889 has been opened to discuss another approach.
- #Instead of deleting we could also disown lists (based on a pref).
- #In that way we could save shared and public lists.
- #The current table constraints support that idea now.
- #This pref should then govern the results of other routines/methods such as
- #Koha::Virtualshelf->new->delete too.
-}
-
-=head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
-
- $date = ExtendMemberSubscriptionTo($borrowerid, $date);
-
-Extending the subscription to a given date or to the expiry date calculated on ISO date.
-Returns ISO date.
-
-=cut
-
-sub ExtendMemberSubscriptionTo {
- my ( $borrowerid,$date) = @_;
- my $dbh = C4::Context->dbh;
- my $borrower = GetMember('borrowernumber'=>$borrowerid);
- unless ($date){
- $date = (C4::Context->preference('BorrowerRenewalPeriodBase') eq 'dateexpiry') ?
- eval { output_pref( { dt => dt_from_string( $borrower->{'dateexpiry'} ), dateonly => 1, dateformat => 'iso' } ); }
- :
- output_pref( { dt => dt_from_string, dateonly => 1, dateformat => 'iso' } );
- $date = GetExpiryDate( $borrower->{'categorycode'}, $date );
- }
- my $sth = $dbh->do(<<EOF);
-UPDATE borrowers
-SET dateexpiry='$date'
-WHERE borrowernumber='$borrowerid'
-EOF
-
- AddEnrolmentFeeIfNeeded( $borrower->{categorycode}, $borrower->{borrowernumber} );
-
- logaction("MEMBERS", "RENEW", $borrower->{'borrowernumber'}, "Membership renewed")if C4::Context->preference("BorrowersLog");
- return $date if ($sth);
- return 0;
-}
-
-=head2 GetTitles (OUEST-PROVENCE)
-
- ($borrowertitle)= &GetTitles();
-
-Looks up the different title . Returns array with all borrowers title
-
-=cut
-
-sub GetTitles {
- my @borrowerTitle = split (/,|\|/,C4::Context->preference('BorrowersTitles'));
- unshift( @borrowerTitle, "" );
- my $count=@borrowerTitle;
- if ($count == 1){
- return ();
- }
- else {
- return ( \@borrowerTitle);
- }
-}
-
-=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;
-}
-