3 # Copyright 2000-2003 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Parts Copyright 2010 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 #use warnings; FIXME - Bug 2505
26 use String
::Random
qw( random_string );
27 use Scalar
::Util
qw( looks_like_number );
28 use Date
::Calc qw
/Today check_date Date_to_Days/;
29 use C4
::Log
; # logaction
35 use C4
::Members
::Attributes
qw(SearchIdMatchingAttribute UpdateBorrowerAttribute);
36 use C4
::NewsChannels
; #get slip news
40 use Text
::Unaccent
qw( unac_string );
41 use Koha
::AuthUtils
qw(hash_password);
44 use Koha
::List
::Patron
;
46 use Koha
::Patron
::Categories
;
49 our (@ISA,@EXPORT,@EXPORT_OK,$debug);
51 use Module
::Load
::Conditional
qw( can_load );
52 if ( ! can_load
( modules
=> { 'Koha::NorwegianPatronDB' => undef } ) ) {
53 $debug && warn "Unable to load Koha::NorwegianPatronDB";
58 $debug = $ENV{DEBUG
} || 0;
66 &GetMemberIssuesAndFines
70 &GetFirstValidEmailAddress
71 &GetNoticeEmailAddress
75 &GetMemberAccountRecords
76 &GetBorNotifyAcctRecord
78 &GetBorrowersToExpunge
79 &GetBorrowersWhoHaveNeverBorrowed
80 &GetBorrowersWithIssuesHistoryOlderThan
82 &GetUpcomingMembershipExpires
115 C4::Members - Perl Module containing convenience functions for member handling
123 This module contains routines for adding, modifying and deleting members/patrons/borrowers
127 =head2 GetMemberDetails
129 ($borrower) = &GetMemberDetails($borrowernumber, $cardnumber);
131 Looks up a patron and returns information about him or her. If
132 C<$borrowernumber> is true (nonzero), C<&GetMemberDetails> looks
133 up the borrower by number; otherwise, it looks up the borrower by card
136 C<$borrower> is a reference-to-hash whose keys are the fields of the
137 borrowers table in the Koha database. In addition,
138 C<$borrower-E<gt>{flags}> is a hash giving more detailed information
139 about the patron. Its keys act as flags :
141 if $borrower->{flags}->{LOST} {
142 # Patron's card was reported lost
145 If the state of a flag means that the patron should not be
146 allowed to borrow any more books, then it will have a C<noissues> key
149 See patronflags for more details.
151 C<$borrower-E<gt>{authflags}> is a hash giving more detailed information
152 about the top-level permissions flags set for the borrower. For example,
153 if a user has the "editcatalogue" permission,
154 C<$borrower-E<gt>{authflags}-E<gt>{editcatalogue}> will exist and have
159 sub GetMemberDetails
{
160 my ( $borrowernumber, $cardnumber ) = @_;
161 my $dbh = C4
::Context
->dbh;
164 if ($borrowernumber) {
165 $sth = $dbh->prepare("
168 categories.description,
169 categories.BlockExpiredPatronOpacActions,
173 LEFT JOIN categories ON borrowers.categorycode=categories.categorycode
174 WHERE borrowernumber = ?
176 $sth->execute($borrowernumber);
178 elsif ($cardnumber) {
179 $sth = $dbh->prepare("
182 categories.description,
183 categories.BlockExpiredPatronOpacActions,
187 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode
190 $sth->execute($cardnumber);
195 my $borrower = $sth->fetchrow_hashref;
196 return unless $borrower;
197 my ($amount) = GetMemberAccountRecords
($borrower->{borrowernumber
});
198 $borrower->{'amountoutstanding'} = $amount;
199 # FIXME - patronflags calls GetMemberAccountRecords... just have patronflags return $amount
200 my $flags = patronflags
( $borrower);
203 $sth = $dbh->prepare("select bit,flag from userflags");
205 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
206 if ( $borrower->{'flags'} && $borrower->{'flags'} & 2**$bit ) {
207 $accessflagshash->{$flag} = 1;
210 $borrower->{'flags'} = $flags;
211 $borrower->{'authflags'} = $accessflagshash;
213 # Handle setting the true behavior for BlockExpiredPatronOpacActions
214 $borrower->{'BlockExpiredPatronOpacActions'} =
215 C4
::Context
->preference('BlockExpiredPatronOpacActions')
216 if ( $borrower->{'BlockExpiredPatronOpacActions'} == -1 );
218 $borrower->{'is_expired'} = 0;
219 $borrower->{'is_expired'} = 1 if
220 defined($borrower->{dateexpiry
}) &&
221 $borrower->{'dateexpiry'} ne '0000-00-00' &&
222 Date_to_Days
( Today
() ) >
223 Date_to_Days
( split /-/, $borrower->{'dateexpiry'} );
225 return ($borrower); #, $flags, $accessflagshash);
230 $flags = &patronflags($patron);
232 This function is not exported.
234 The following will be set where applicable:
235 $flags->{CHARGES}->{amount} Amount of debt
236 $flags->{CHARGES}->{noissues} Set if debt amount >$5.00 (or syspref noissuescharge)
237 $flags->{CHARGES}->{message} Message -- deprecated
239 $flags->{CREDITS}->{amount} Amount of credit
240 $flags->{CREDITS}->{message} Message -- deprecated
242 $flags->{ GNA } Patron has no valid address
243 $flags->{ GNA }->{noissues} Set for each GNA
244 $flags->{ GNA }->{message} "Borrower has no valid address" -- deprecated
246 $flags->{ LOST } Patron's card reported lost
247 $flags->{ LOST }->{noissues} Set for each LOST
248 $flags->{ LOST }->{message} Message -- deprecated
250 $flags->{DBARRED} Set if patron debarred, no access
251 $flags->{DBARRED}->{noissues} Set for each DBARRED
252 $flags->{DBARRED}->{message} Message -- deprecated
255 $flags->{ NOTES }->{message} The note itself. NOT deprecated
257 $flags->{ ODUES } Set if patron has overdue books.
258 $flags->{ ODUES }->{message} "Yes" -- deprecated
259 $flags->{ ODUES }->{itemlist} ref-to-array: list of overdue books
260 $flags->{ ODUES }->{itemlisttext} Text list of overdue items -- deprecated
262 $flags->{WAITING} Set if any of patron's reserves are available
263 $flags->{WAITING}->{message} Message -- deprecated
264 $flags->{WAITING}->{itemlist} ref-to-array: list of available items
268 =item C<$flags-E<gt>{ODUES}-E<gt>{itemlist}> is a reference-to-array listing the
269 overdue items. Its elements are references-to-hash, each describing an
270 overdue item. The keys are selected fields from the issues, biblio,
271 biblioitems, and items tables of the Koha database.
273 =item C<$flags-E<gt>{ODUES}-E<gt>{itemlisttext}> is a string giving a text listing of
274 the overdue items, one per line. Deprecated.
276 =item C<$flags-E<gt>{WAITING}-E<gt>{itemlist}> is a reference-to-array listing the
277 available items. Each element is a reference-to-hash whose keys are
278 fields from the reserves table of the Koha database.
282 All the "message" fields that include language generated in this function are deprecated,
283 because such strings belong properly in the display layer.
285 The "message" field that comes from the DB is OK.
289 # TODO: use {anonymous => hashes} instead of a dozen %flaginfo
290 # FIXME rename this function.
293 my ( $patroninformation) = @_;
294 my $dbh=C4
::Context
->dbh;
295 my ($balance, $owing) = GetMemberAccountBalance
( $patroninformation->{'borrowernumber'});
298 my $noissuescharge = C4
::Context
->preference("noissuescharge") || 5;
299 $flaginfo{'message'} = sprintf 'Patron owes %.02f', $owing;
300 $flaginfo{'amount'} = sprintf "%.02f", $owing;
301 if ( $owing > $noissuescharge && !C4
::Context
->preference("AllowFineOverride") ) {
302 $flaginfo{'noissues'} = 1;
304 $flags{'CHARGES'} = \
%flaginfo;
306 elsif ( $balance < 0 ) {
308 $flaginfo{'message'} = sprintf 'Patron has credit of %.02f', -$balance;
309 $flaginfo{'amount'} = sprintf "%.02f", $balance;
310 $flags{'CREDITS'} = \
%flaginfo;
313 # Check the debt of the guarntees of this patron
314 my $no_issues_charge_guarantees = C4
::Context
->preference("NoIssuesChargeGuarantees");
315 $no_issues_charge_guarantees = undef unless looks_like_number
( $no_issues_charge_guarantees );
316 if ( defined $no_issues_charge_guarantees ) {
317 my $p = Koha
::Patrons
->find( $patroninformation->{borrowernumber
} );
318 my @guarantees = $p->guarantees();
319 my $guarantees_non_issues_charges;
320 foreach my $g ( @guarantees ) {
321 my ( $b, $n, $o ) = C4
::Members
::GetMemberAccountBalance
( $g->id );
322 $guarantees_non_issues_charges += $n;
325 if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
327 $flaginfo{'message'} = sprintf 'patron guarantees owe %.02f', $guarantees_non_issues_charges;
328 $flaginfo{'amount'} = $guarantees_non_issues_charges;
329 $flaginfo{'noissues'} = 1 unless C4
::Context
->preference("allowfineoverride");
330 $flags{'CHARGES_GUARANTEES'} = \
%flaginfo;
334 if ( $patroninformation->{'gonenoaddress'}
335 && $patroninformation->{'gonenoaddress'} == 1 )
338 $flaginfo{'message'} = 'Borrower has no valid address.';
339 $flaginfo{'noissues'} = 1;
340 $flags{'GNA'} = \
%flaginfo;
342 if ( $patroninformation->{'lost'} && $patroninformation->{'lost'} == 1 ) {
344 $flaginfo{'message'} = 'Borrower\'s card reported lost.';
345 $flaginfo{'noissues'} = 1;
346 $flags{'LOST'} = \
%flaginfo;
348 if ( $patroninformation->{'debarred'} && check_date
( split( /-/, $patroninformation->{'debarred'} ) ) ) {
349 if ( Date_to_Days
(Date
::Calc
::Today
) < Date_to_Days
( split( /-/, $patroninformation->{'debarred'} ) ) ) {
351 $flaginfo{'debarredcomment'} = $patroninformation->{'debarredcomment'};
352 $flaginfo{'message'} = $patroninformation->{'debarredcomment'};
353 $flaginfo{'noissues'} = 1;
354 $flaginfo{'dateend'} = $patroninformation->{'debarred'};
355 $flags{'DBARRED'} = \
%flaginfo;
358 if ( $patroninformation->{'borrowernotes'}
359 && $patroninformation->{'borrowernotes'} )
362 $flaginfo{'message'} = $patroninformation->{'borrowernotes'};
363 $flags{'NOTES'} = \
%flaginfo;
365 my ( $odues, $itemsoverdue ) = C4
::Overdues
::checkoverdues
($patroninformation->{'borrowernumber'});
366 if ( $odues && $odues > 0 ) {
368 $flaginfo{'message'} = "Yes";
369 $flaginfo{'itemlist'} = $itemsoverdue;
370 foreach ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
373 $flaginfo{'itemlisttext'} .=
374 "$_->{'date_due'} $_->{'barcode'} $_->{'title'} \n"; # newline is display layer
376 $flags{'ODUES'} = \
%flaginfo;
378 my @itemswaiting = C4
::Reserves
::GetReservesFromBorrowernumber
( $patroninformation->{'borrowernumber'},'W' );
379 my $nowaiting = scalar @itemswaiting;
380 if ( $nowaiting > 0 ) {
382 $flaginfo{'message'} = "Reserved items available";
383 $flaginfo{'itemlist'} = \
@itemswaiting;
384 $flags{'WAITING'} = \
%flaginfo;
392 $borrower = &GetMember(%information);
394 Retrieve the first patron record meeting on criteria listed in the
395 C<%information> hash, which should contain one or more
396 pairs of borrowers column names and values, e.g.,
398 $borrower = GetMember(borrowernumber => id);
400 C<&GetBorrower> returns a reference-to-hash whose keys are the fields of
401 the C<borrowers> table in the Koha database.
403 FIXME: GetMember() is used throughout the code as a lookup
404 on a unique key such as the borrowernumber, but this meaning is not
405 enforced in the routine itself.
411 my ( %information ) = @_;
412 if (exists $information{borrowernumber
} && !defined $information{borrowernumber
}) {
413 #passing mysql's kohaadmin?? Makes no sense as a query
416 my $dbh = C4
::Context
->dbh;
418 q{SELECT borrowers.*, categories.category_type, categories.description
420 LEFT JOIN categories on borrowers.categorycode=categories.categorycode WHERE };
423 for (keys %information ) {
431 if (defined $information{$_}) {
433 push @values, $information{$_};
436 $select .= "$_ IS NULL";
439 $debug && warn $select, " ",values %information;
440 my $sth = $dbh->prepare("$select");
441 $sth->execute(@values);
442 my $data = $sth->fetchall_arrayref({});
443 #FIXME interface to this routine now allows generation of a result set
444 #so whole array should be returned but bowhere in the current code expects this
452 =head2 GetMemberIssuesAndFines
454 ($overdue_count, $issue_count, $total_fines) = &GetMemberIssuesAndFines($borrowernumber);
456 Returns aggregate data about items borrowed by the patron with the
457 given borrowernumber.
459 C<&GetMemberIssuesAndFines> returns a three-element array. C<$overdue_count> is the
460 number of overdue items the patron currently has borrowed. C<$issue_count> is the
461 number of books the patron currently has borrowed. C<$total_fines> is
462 the total fine currently due by the borrower.
467 sub GetMemberIssuesAndFines
{
468 my ( $borrowernumber ) = @_;
469 my $dbh = C4
::Context
->dbh;
470 my $query = "SELECT COUNT(*) FROM issues WHERE borrowernumber = ?";
472 $debug and warn $query."\n";
473 my $sth = $dbh->prepare($query);
474 $sth->execute($borrowernumber);
475 my $issue_count = $sth->fetchrow_arrayref->[0];
477 $sth = $dbh->prepare(
478 "SELECT COUNT(*) FROM issues
479 WHERE borrowernumber = ?
480 AND date_due < now()"
482 $sth->execute($borrowernumber);
483 my $overdue_count = $sth->fetchrow_arrayref->[0];
485 $sth = $dbh->prepare("SELECT SUM(amountoutstanding) FROM accountlines WHERE borrowernumber = ?");
486 $sth->execute($borrowernumber);
487 my $total_fines = $sth->fetchrow_arrayref->[0];
489 return ($overdue_count, $issue_count, $total_fines);
495 my $success = ModMember(borrowernumber => $borrowernumber,
496 [ field => value ]... );
498 Modify borrower's data. All date fields should ALREADY be in ISO format.
501 true on success, or false on failure
507 # test to know if you must update or not the borrower password
508 if (exists $data{password
}) {
509 if ($data{password
} eq '****' or $data{password
} eq '') {
510 delete $data{password
};
512 if ( C4
::Context
->preference('NorwegianPatronDBEnable') && C4
::Context
->preference('NorwegianPatronDBEnable') == 1 ) {
513 # Update the hashed PIN in borrower_sync.hashed_pin, before Koha hashes it
514 Koha
::NorwegianPatronDB
::NLUpdateHashedPIN
( $data{'borrowernumber'}, $data{password
} );
516 $data{password
} = hash_password
($data{password
});
520 my $old_categorycode = Koha
::Patrons
->find( $data{borrowernumber
} )->categorycode;
522 # get only the columns of a borrower
523 my $schema = Koha
::Database
->new()->schema;
524 my @columns = $schema->source('Borrower')->columns;
525 my $new_borrower = { map { join(' ', @columns) =~ /$_/ ?
( $_ => $data{$_} ) : () } keys(%data) };
526 delete $new_borrower->{flags
};
528 $new_borrower->{dateofbirth
} ||= undef if exists $new_borrower->{dateofbirth
};
529 $new_borrower->{dateenrolled
} ||= undef if exists $new_borrower->{dateenrolled
};
530 $new_borrower->{dateexpiry
} ||= undef if exists $new_borrower->{dateexpiry
};
531 $new_borrower->{debarred
} ||= undef if exists $new_borrower->{debarred
};
532 $new_borrower->{sms_provider_id
} ||= undef if exists $new_borrower->{sms_provider_id
};
533 $new_borrower->{guarantorid
} ||= undef if exists $new_borrower->{guarantorid
};
535 my $patron = Koha
::Patrons
->find( $new_borrower->{borrowernumber
} );
537 delete $new_borrower->{userid
} if exists $new_borrower->{userid
} and not $new_borrower->{userid
};
539 my $execute_success = $patron->store if $patron->set($new_borrower);
541 if ($execute_success) { # only proceed if the update was a success
542 # If the patron changes to a category with enrollment fee, we add a fee
543 if ( $data{categorycode
} and $data{categorycode
} ne $old_categorycode ) {
544 if ( C4
::Context
->preference('FeeOnChangePatronCategory') ) {
545 $patron->add_enrolment_fee_if_needed;
549 # If NorwegianPatronDBEnable is enabled, we set syncstatus to something that a
550 # cronjob will use for syncing with NL
551 if ( C4
::Context
->preference('NorwegianPatronDBEnable') && C4
::Context
->preference('NorwegianPatronDBEnable') == 1 ) {
552 my $borrowersync = Koha
::Database
->new->schema->resultset('BorrowerSync')->find({
553 'synctype' => 'norwegianpatrondb',
554 'borrowernumber' => $data{'borrowernumber'}
556 # Do not set to "edited" if syncstatus is "new". We need to sync as new before
557 # we can sync as changed. And the "new sync" will pick up all changes since
558 # the patron was created anyway.
559 if ( $borrowersync->syncstatus ne 'new' && $borrowersync->syncstatus ne 'delete' ) {
560 $borrowersync->update( { 'syncstatus' => 'edited' } );
562 # Set the value of 'sync'
563 $borrowersync->update( { 'sync' => $data{'sync'} } );
564 # Try to do the live sync
565 Koha
::NorwegianPatronDB
::NLSync
({ 'borrowernumber' => $data{'borrowernumber'} });
568 logaction
("MEMBERS", "MODIFY", $data{'borrowernumber'}, "UPDATE (executed w/ arg: $data{'borrowernumber'})") if C4
::Context
->preference("BorrowersLog");
570 return $execute_success;
575 $borrowernumber = &AddMember(%borrower);
577 insert new borrower into table
579 (%borrower keys are database columns. Database columns could be
580 different in different versions. Please look into database for correct
583 Returns the borrowernumber upon success
585 Returns as undef upon any db error without further processing
592 my $dbh = C4
::Context
->dbh;
593 my $schema = Koha
::Database
->new()->schema;
595 # generate a proper login if none provided
596 $data{'userid'} = Generate_Userid
( $data{'borrowernumber'}, $data{'firstname'}, $data{'surname'} )
597 if ( $data{'userid'} eq '' || !Check_Userid
( $data{'userid'} ) );
599 # add expiration date if it isn't already there
600 $data{dateexpiry
} ||= Koha
::Patron
::Categories
->find( $data{categorycode
} )->get_expiry_date;
602 # add enrollment date if it isn't already there
603 unless ( $data{'dateenrolled'} ) {
604 $data{'dateenrolled'} = output_pref
( { dt
=> dt_from_string
, dateonly
=> 1, dateformat
=> 'iso' } );
607 my $patron_category = $schema->resultset('Category')->find( $data{'categorycode'} );
609 $patron_category->default_privacy() eq 'default' ?
1
610 : $patron_category->default_privacy() eq 'never' ?
2
611 : $patron_category->default_privacy() eq 'forever' ?
0
614 $data{'privacy_guarantor_checkouts'} = 0 unless defined( $data{'privacy_guarantor_checkouts'} );
616 # Make a copy of the plain text password for later use
617 my $plain_text_password = $data{'password'};
619 # create a disabled account if no password provided
620 $data{'password'} = ($data{'password'})? hash_password
($data{'password'}) : '!';
622 # we don't want invalid dates in the db (mysql has a bad habit of inserting 0000-00-00
623 $data{'dateofbirth'} = undef if ( not $data{'dateofbirth'} );
624 $data{'debarred'} = undef if ( not $data{'debarred'} );
625 $data{'sms_provider_id'} = undef if ( not $data{'sms_provider_id'} );
627 # get only the columns of Borrower
628 # FIXME Do we really need this check?
629 my @columns = $schema->source('Borrower')->columns;
630 my $new_member = { map { join(' ',@columns) =~ /$_/ ?
( $_ => $data{$_} ) : () } keys(%data) } ;
632 delete $new_member->{borrowernumber
};
634 my $patron = Koha
::Patron
->new( $new_member )->store;
635 $data{borrowernumber
} = $patron->borrowernumber;
637 # If NorwegianPatronDBEnable is enabled, we set syncstatus to something that a
638 # cronjob will use for syncing with NL
639 if ( exists $data{'borrowernumber'} && C4
::Context
->preference('NorwegianPatronDBEnable') && C4
::Context
->preference('NorwegianPatronDBEnable') == 1 ) {
640 Koha
::Database
->new->schema->resultset('BorrowerSync')->create({
641 'borrowernumber' => $data{'borrowernumber'},
642 'synctype' => 'norwegianpatrondb',
644 'syncstatus' => 'new',
645 'hashed_pin' => Koha
::NorwegianPatronDB
::NLEncryptPIN
( $plain_text_password ),
649 logaction
("MEMBERS", "CREATE", $data{'borrowernumber'}, "") if C4
::Context
->preference("BorrowersLog");
651 $patron->add_enrolment_fee_if_needed;
653 return $data{borrowernumber
};
658 my $uniqueness = Check_Userid($userid,$borrowernumber);
660 $borrowernumber is optional (i.e. it can contain a blank value). If $userid is passed with a blank $borrowernumber variable, the database will be checked for all instances of that userid (i.e. userid=? AND borrowernumber != '').
662 If $borrowernumber is provided, the database will be checked for every instance of that userid coupled with a different borrower(number) than the one provided.
665 0 for not unique (i.e. this $userid already exists)
666 1 for unique (i.e. this $userid does not exist, or this $userid/$borrowernumber combination already exists)
671 my ( $uid, $borrowernumber ) = @_;
673 return 0 unless ($uid); # userid is a unique column, we should assume NULL is not unique
675 return 0 if ( $uid eq C4
::Context
->config('user') );
677 my $rs = Koha
::Database
->new()->schema()->resultset('Borrower');
680 $params->{userid
} = $uid;
681 $params->{borrowernumber
} = { '!=' => $borrowernumber } if ($borrowernumber);
683 my $count = $rs->count( $params );
685 return $count ?
0 : 1;
688 =head2 Generate_Userid
690 my $newuid = Generate_Userid($borrowernumber, $firstname, $surname);
692 Generate a userid using the $surname and the $firstname (if there is a value in $firstname).
694 $borrowernumber is optional (i.e. it can contain a blank value). A value is passed when generating a new userid for an existing borrower. When a new userid is created for a new borrower, a blank value is passed to this sub.
697 new userid ($firstname.$surname if there is a $firstname, or $surname if there is no value in $firstname) plus offset (0 if the $newuid is unique, or a higher numeric value if Check_Userid finds an existing match for the $newuid in the database).
701 sub Generate_Userid
{
702 my ($borrowernumber, $firstname, $surname) = @_;
705 #The script will "do" the following code and increment the $offset until Check_Userid = 1 (i.e. until $newuid comes back as unique)
707 $firstname =~ s/[[:digit:][:space:][:blank:][:punct:][:cntrl:]]//g;
708 $surname =~ s/[[:digit:][:space:][:blank:][:punct:][:cntrl:]]//g;
709 $newuid = lc(($firstname)?
"$firstname.$surname" : $surname);
710 $newuid = unac_string
('utf-8',$newuid);
711 $newuid .= $offset unless $offset == 0;
714 } while (!Check_Userid
($newuid,$borrowernumber));
719 =head2 fixup_cardnumber
721 Warning: The caller is responsible for locking the members table in write
722 mode, to avoid database corruption.
726 use vars
qw( @weightings );
727 my @weightings = ( 8, 4, 6, 3, 5, 2, 1 );
729 sub fixup_cardnumber {
730 my ($cardnumber) = @_;
731 my $autonumber_members = C4::Context->boolean_preference('autoMemberNum') || 0;
733 # Find out whether member numbers should be generated
734 # automatically. Should be either "1" or something else.
735 # Defaults to "0", which is interpreted as "no".
737 # if ($cardnumber !~ /\S/ && $autonumber_members) {
738 ($autonumber_members) or return $cardnumber;
739 my $checkdigit = C4::Context->preference('checkdigit');
740 my $dbh = C4::Context->dbh;
741 if ( $checkdigit and $checkdigit eq 'katipo' ) {
743 # if checkdigit is selected, calculate katipo-style cardnumber.
744 # otherwise, just use the max()
745 # purpose: generate checksum'd member numbers.
746 # We'll assume we just got the max value of digits 2-8 of member #'s
747 # from the database and our job is to increment that by one,
748 # determine the 1st and 9th digits and return the full string.
749 my $sth = $dbh->prepare(
750 "select max(substring(borrowers.cardnumber,2,7)) as new_num from borrowers"
753 my $data = $sth->fetchrow_hashref;
754 $cardnumber = $data->{new_num};
755 if ( !$cardnumber ) { # If DB has no values,
756 $cardnumber = 1000000; # start at 1000000
762 for ( my $i = 0 ; $i < 8 ; $i += 1 ) {
763 # read weightings, left to right, 1 char at a time
764 my $temp1 = $weightings[$i];
766 # sequence left to right, 1 char at a time
767 my $temp2 = substr( $cardnumber, $i, 1 );
769 # mult each char 1-7 by its corresponding weighting
770 $sum += $temp1 * $temp2;
773 my $rem = ( $sum % 11 );
774 $rem = 'X' if $rem == 10;
776 return "V$cardnumber$rem";
779 my $sth = $dbh->prepare(
780 'SELECT MAX( CAST( cardnumber AS SIGNED ) ) FROM borrowers WHERE cardnumber REGEXP "^-?[0-9]+$"'
783 my ($result) = $sth->fetchrow;
786 return $cardnumber; # just here as a fallback/reminder
789 =head2 GetPendingIssues
791 my $issues = &GetPendingIssues(@borrowernumber);
793 Looks up what the patron with the given borrowernumber has borrowed.
795 C<&GetPendingIssues> returns a
796 reference-to-array where each element is a reference-to-hash; the
797 keys are the fields from the C<issues>, C<biblio>, and C<items> tables.
798 The keys include C<biblioitems> fields except marc and marcxml.
802 sub GetPendingIssues {
803 my @borrowernumbers = @_;
805 unless (@borrowernumbers ) { # return a ref_to_array
806 return \@borrowernumbers; # to not cause surprise to caller
809 # Borrowers part of the query
811 for (my $i = 0; $i < @borrowernumbers; $i++) {
812 $bquery .= ' issues.borrowernumber = ?';
813 if ($i < $#borrowernumbers ) {
818 # must avoid biblioitems.* to prevent large marc and marcxml fields from killing performance
819 # FIXME: namespace collision: each table has "timestamp" fields. Which one is "timestamp" ?
820 # FIXME: circ/ciculation.pl tries to sort by timestamp!
821 # FIXME: namespace collision: other collisions possible.
822 # FIXME: most of this data isn't really being used by callers.
829 biblioitems.itemtype,
832 biblioitems.publicationyear,
833 biblioitems.publishercode,
834 biblioitems.volumedate,
835 biblioitems.volumedesc,
840 borrowers.cardnumber,
841 issues.timestamp AS timestamp,
842 issues.renewals AS renewals,
843 issues.borrowernumber AS borrowernumber,
844 items.renewals AS totalrenewals
846 LEFT JOIN items ON items.itemnumber = issues.itemnumber
847 LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
848 LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
849 LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
852 ORDER BY issues.issuedate"
855 my $sth = C4::Context->dbh->prepare($query);
856 $sth->execute(@borrowernumbers);
857 my $data = $sth->fetchall_arrayref({});
858 my $today = dt_from_string;
860 if ($_->{issuedate}) {
861 $_->{issuedate} = dt_from_string($_->{issuedate}, 'sql');
863 $_->{date_due_sql} = $_->{date_due};
864 # FIXME no need to have this value
865 $_->{date_due} or next;
866 $_->{date_due_sql} = $_->{date_due};
867 # FIXME no need to have this value
868 $_->{date_due} = dt_from_string($_->{date_due}, 'sql');
869 if ( DateTime->compare($_->{date_due}, $today) == -1 ) {
878 $issues = &GetAllIssues($borrowernumber, $sortkey, $limit);
880 Looks up what the patron with the given borrowernumber has borrowed,
881 and sorts the results.
883 C<$sortkey> is the name of a field on which to sort the results. This
884 should be the name of a field in the C<issues>, C<biblio>,
885 C<biblioitems>, or C<items> table in the Koha database.
887 C<$limit> is the maximum number of results to return.
889 C<&GetAllIssues> an arrayref, C<$issues>, of hashrefs, the keys of which
890 are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
891 C<items> tables of the Koha database.
897 my ( $borrowernumber, $order, $limit ) = @_;
899 return unless $borrowernumber;
900 $order = 'date_due desc' unless $order;
902 my $dbh = C4::Context->dbh;
904 'SELECT *, issues.timestamp as issuestimestamp, issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp
906 LEFT JOIN items on items.itemnumber=issues.itemnumber
907 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
908 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
909 WHERE borrowernumber=?
911 SELECT *, old_issues.timestamp as issuestimestamp, old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp
913 LEFT JOIN items on items.itemnumber=old_issues.itemnumber
914 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
915 LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
916 WHERE borrowernumber=? AND old_issues.itemnumber IS NOT NULL
919 $query .= " limit $limit";
922 my $sth = $dbh->prepare($query);
923 $sth->execute( $borrowernumber, $borrowernumber );
924 return $sth->fetchall_arrayref( {} );
928 =head2 GetMemberAccountRecords
930 ($total, $acctlines, $count) = &GetMemberAccountRecords($borrowernumber);
932 Looks up accounting data for the patron with the given borrowernumber.
934 C<&GetMemberAccountRecords> returns a three-element array. C<$acctlines> is a
935 reference-to-array, where each element is a reference-to-hash; the
936 keys are the fields of the C<accountlines> table in the Koha database.
937 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
938 total amount outstanding for all of the account lines.
942 sub GetMemberAccountRecords {
943 my ($borrowernumber) = @_;
944 my $dbh = C4::Context->dbh;
950 WHERE borrowernumber=?);
951 $strsth.=" ORDER BY accountlines_id desc";
952 my $sth= $dbh->prepare( $strsth );
953 $sth->execute( $borrowernumber );
956 while ( my $data = $sth->fetchrow_hashref ) {
957 if ( $data->{itemnumber} ) {
958 my $biblio = GetBiblioFromItemNumber( $data->{itemnumber} );
959 $data->{biblionumber} = $biblio->{biblionumber};
960 $data->{title} = $biblio->{title};
962 $acctlines[$numlines] = $data;
964 $total += sprintf "%.0f", 1000*$data->{amountoutstanding}; # convert float to integer to avoid round-off errors
967 return ( $total, \@acctlines,$numlines);
970 =head2 GetMemberAccountBalance
972 ($total_balance, $non_issue_balance, $other_charges) = &GetMemberAccountBalance($borrowernumber);
974 Calculates amount immediately owing by the patron - non-issue charges.
975 Based on GetMemberAccountRecords.
976 Charges exempt from non-issue are:
978 * Rent (rental) if RentalsInNoissuesCharge syspref is set to false
979 * Manual invoices if ManInvInNoissuesCharge syspref is set to false
983 sub GetMemberAccountBalance {
984 my ($borrowernumber) = @_;
986 my $ACCOUNT_TYPE_LENGTH = 5; # this is plain ridiculous...
989 push @not_fines, 'Res' unless C4::Context->preference('HoldsInNoissuesCharge');
990 push @not_fines, 'Rent' unless C4::Context->preference('RentalsInNoissuesCharge');
991 unless ( C4::Context->preference('ManInvInNoissuesCharge') ) {
992 my $dbh = C4::Context->dbh;
993 my $man_inv_types = $dbh->selectcol_arrayref(qq{SELECT authorised_value FROM authorised_values WHERE category = 'MANUAL_INV'});
994 push @not_fines, map substr($_, 0, $ACCOUNT_TYPE_LENGTH), @$man_inv_types;
996 my %not_fine = map {$_ => 1} @not_fines;
998 my ($total, $acctlines) = GetMemberAccountRecords($borrowernumber);
999 my $other_charges = 0;
1000 foreach (@$acctlines) {
1001 $other_charges += $_->{amountoutstanding} if $not_fine{ substr($_->{accounttype}, 0, $ACCOUNT_TYPE_LENGTH) };
1004 return ( $total, $total - $other_charges, $other_charges);
1007 =head2 GetBorNotifyAcctRecord
1009 ($total, $acctlines, $count) = &GetBorNotifyAcctRecord($params,$notifyid);
1011 Looks up accounting data for the patron with the given borrowernumber per file number.
1013 C<&GetBorNotifyAcctRecord> returns a three-element array. C<$acctlines> is a
1014 reference-to-array, where each element is a reference-to-hash; the
1015 keys are the fields of the C<accountlines> table in the Koha database.
1016 C<$count> is the number of elements in C<$acctlines>. C<$total> is the
1017 total amount outstanding for all of the account lines.
1021 sub GetBorNotifyAcctRecord {
1022 my ( $borrowernumber, $notifyid ) = @_;
1023 my $dbh = C4::Context->dbh;
1026 my $sth = $dbh->prepare(
1029 WHERE borrowernumber=?
1031 AND amountoutstanding != '0'
1032 ORDER BY notify_id,accounttype
1035 $sth->execute( $borrowernumber, $notifyid );
1037 while ( my $data = $sth->fetchrow_hashref ) {
1038 if ( $data->{itemnumber} ) {
1039 my $biblio = GetBiblioFromItemNumber( $data->{itemnumber} );
1040 $data->{biblionumber} = $biblio->{biblionumber};
1041 $data->{title} = $biblio->{title};
1043 $acctlines[$numlines] = $data;
1045 $total += int(100 * $data->{'amountoutstanding'});
1048 return ( $total, \@acctlines, $numlines );
1051 sub checkcardnumber {
1052 my ( $cardnumber, $borrowernumber ) = @_;
1054 # If cardnumber is null, we assume they're allowed.
1055 return 0 unless defined $cardnumber;
1057 my $dbh = C4::Context->dbh;
1058 my $query = "SELECT * FROM borrowers WHERE cardnumber=?";
1059 $query .= " AND borrowernumber <> ?" if ($borrowernumber);
1060 my $sth = $dbh->prepare($query);
1063 ( $borrowernumber ? $borrowernumber : () )
1066 return 1 if $sth->fetchrow_hashref;
1068 my ( $min_length, $max_length ) = get_cardnumber_length();
1070 if length $cardnumber > $max_length
1071 or length $cardnumber < $min_length;
1076 =head2 get_cardnumber_length
1078 my ($min, $max) = C4::Members::get_cardnumber_length()
1080 Returns the minimum and maximum length for patron cardnumbers as
1081 determined by the CardnumberLength system preference, the
1082 BorrowerMandatoryField system preference, and the width of the
1087 sub get_cardnumber_length {
1088 my ( $min, $max ) = ( 0, 16 ); # borrowers.cardnumber is a nullable varchar(16)
1089 $min = 1 if C4::Context->preference('BorrowerMandatoryField') =~ /cardnumber/;
1090 if ( my $cardnumber_length = C4::Context->preference('CardnumberLength') ) {
1091 # Is integer and length match
1092 if ( $cardnumber_length =~ m|^\d+$| ) {
1093 $min = $max = $cardnumber_length
1094 if $cardnumber_length >= $min
1095 and $cardnumber_length <= $max;
1097 # Else assuming it is a range
1098 elsif ( $cardnumber_length =~ m|(\d*),(\d*)| ) {
1099 $min = $1 if $1 and $min < $1;
1100 $max = $2 if $2 and $max > $2;
1104 my $borrower = Koha::Schema->resultset('Borrower');
1105 my $field_size = $borrower->result_source->column_info('cardnumber')->{size};
1106 $min = $field_size if $min > $field_size;
1107 return ( $min, $max );
1110 =head2 GetFirstValidEmailAddress
1112 $email = GetFirstValidEmailAddress($borrowernumber);
1114 Return the first valid email address for a borrower, given the borrowernumber. For now, the order
1115 is defined as email, emailpro, B_email. Returns the empty string if the borrower has no email
1120 sub GetFirstValidEmailAddress {
1121 my $borrowernumber = shift;
1122 my $dbh = C4::Context->dbh;
1123 my $sth = $dbh->prepare( "SELECT email, emailpro, B_email FROM borrowers where borrowernumber = ? ");
1124 $sth->execute( $borrowernumber );
1125 my $data = $sth->fetchrow_hashref;
1127 if ($data->{'email'}) {
1128 return $data->{'email'};
1129 } elsif ($data->{'emailpro'}) {
1130 return $data->{'emailpro'};
1131 } elsif ($data->{'B_email'}) {
1132 return $data->{'B_email'};
1138 =head2 GetNoticeEmailAddress
1140 $email = GetNoticeEmailAddress($borrowernumber);
1142 Return the email address of borrower used for notices, given the borrowernumber.
1143 Returns the empty string if no email address.
1147 sub GetNoticeEmailAddress {
1148 my $borrowernumber = shift;
1150 my $which_address = C4::Context->preference("AutoEmailPrimaryAddress");
1151 # if syspref is set to 'first valid' (value == OFF), look up email address
1152 if ( $which_address eq 'OFF' ) {
1153 return GetFirstValidEmailAddress($borrowernumber);
1155 # specified email address field
1156 my $dbh = C4::Context->dbh;
1157 my $sth = $dbh->prepare( qq{
1158 SELECT $which_address AS primaryemail
1160 WHERE borrowernumber=?
1162 $sth->execute($borrowernumber);
1163 my $data = $sth->fetchrow_hashref;
1164 return $data->{'primaryemail'} || '';
1167 =head2 GetUpcomingMembershipExpires
1169 my $expires = GetUpcomingMembershipExpires({
1170 branch => $branch, before => $before, after => $after,
1173 $branch is an optional branch code.
1174 $before/$after is an optional number of days before/after the date that
1175 is set by the preference MembershipExpiryDaysNotice.
1176 If the pref would be 14, before 2 and after 3, you will get all expires
1181 sub GetUpcomingMembershipExpires {
1182 my ( $params ) = @_;
1183 my $before = $params->{before} || 0;
1184 my $after = $params->{after} || 0;
1185 my $branch = $params->{branch};
1187 my $dbh = C4::Context->dbh;
1188 my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
1189 my $date1 = dt_from_string->add( days => $days - $before );
1190 my $date2 = dt_from_string->add( days => $days + $after );
1191 $date1= output_pref({ dt => $date1, dateformat => 'iso', dateonly => 1 });
1192 $date2= output_pref({ dt => $date2, dateformat => 'iso', dateonly => 1 });
1195 SELECT borrowers.*, categories.description,
1196 branches.branchname, branches.branchemail FROM borrowers
1197 LEFT JOIN branches USING (branchcode)
1198 LEFT JOIN categories USING (categorycode)
1201 $query.= 'WHERE branchcode=? AND dateexpiry BETWEEN ? AND ?';
1203 $query.= 'WHERE dateexpiry BETWEEN ? AND ?';
1206 my $sth = $dbh->prepare( $query );
1207 my @pars = $branch? ( $branch ): ();
1208 push @pars, $date1, $date2;
1209 $sth->execute( @pars );
1210 my $results = $sth->fetchall_arrayref( {} );
1216 $dateofbirth,$date = &GetAge($date);
1218 this function return the borrowers age with the value of dateofbirth
1224 my ( $date, $date_ref ) = @_;
1226 if ( not defined $date_ref ) {
1227 $date_ref = sprintf( '%04d-%02d-%02d', Today() );
1230 my ( $year1, $month1, $day1 ) = split /-/, $date;
1231 my ( $year2, $month2, $day2 ) = split /-/, $date_ref;
1233 my $age = $year2 - $year1;
1234 if ( $month1 . $day1 > $month2 . $day2 ) {
1243 $borrower = C4::Members::SetAge($borrower, $datetimeduration);
1244 $borrower = C4::Members::SetAge($borrower, '0015-12-10');
1245 $borrower = C4::Members::SetAge($borrower, $datetimeduration, $datetime_reference);
1247 eval { $borrower = C4::Members::SetAge($borrower, '015-1-10'); };
1248 if ($@) {print $@;} #Catch a bad ISO Date or kill your script!
1250 This function sets the borrower's dateofbirth to match the given age.
1251 Optionally relative to the given $datetime_reference.
1253 @PARAM1 koha.borrowers-object
1254 @PARAM2 DateTime::Duration-object as the desired age
1255 OR a ISO 8601 Date. (To make the API more pleasant)
1256 @PARAM3 DateTime-object as the relative date, defaults to now().
1257 RETURNS The given borrower reference @PARAM1.
1258 DIES If there was an error with the ISO Date handling.
1264 my ( $borrower, $datetimeduration, $datetime_ref ) = @_;
1265 $datetime_ref = DateTime->now() unless $datetime_ref;
1267 if ($datetimeduration && ref $datetimeduration ne 'DateTime::Duration') {
1268 if ($datetimeduration =~ /^(\d{4})-(\d{2})-(\d{2})/) {
1269 $datetimeduration = DateTime::Duration->new(years => $1, months => $2, days => $3);
1272 die "C4::Members::SetAge($borrower, $datetimeduration), datetimeduration not a valid ISO 8601 Date!\n";
1276 my $new_datetime_ref = $datetime_ref->clone();
1277 $new_datetime_ref->subtract_duration( $datetimeduration );
1279 $borrower->{dateofbirth} = $new_datetime_ref->ymd();
1284 =head2 GetBorrowersToExpunge
1286 $borrowers = &GetBorrowersToExpunge(
1287 not_borrowed_since => $not_borrowed_since,
1288 expired_before => $expired_before,
1289 category_code => $category_code,
1290 patron_list_id => $patron_list_id,
1291 branchcode => $branchcode
1294 This function get all borrowers based on the given criteria.
1298 sub GetBorrowersToExpunge {
1301 my $filterdate = $params->{'not_borrowed_since'};
1302 my $filterexpiry = $params->{'expired_before'};
1303 my $filterlastseen = $params->{'last_seen'};
1304 my $filtercategory = $params->{'category_code'};
1305 my $filterbranch = $params->{'branchcode'} ||
1306 ((C4::Context->preference('IndependentBranches')
1307 && C4::Context->userenv
1308 && !C4::Context->IsSuperLibrarian()
1309 && C4::Context->userenv->{branch})
1310 ? C4::Context->userenv->{branch}
1312 my $filterpatronlist = $params->{'patron_list_id'};
1314 my $dbh = C4::Context->dbh;
1316 SELECT borrowers.borrowernumber,
1317 MAX(old_issues.timestamp) AS latestissue,
1318 MAX(issues.timestamp) AS currentissue
1320 JOIN categories USING (categorycode)
1324 WHERE guarantorid IS NOT NULL
1325 AND guarantorid <> 0
1326 ) as tmp ON borrowers.borrowernumber=tmp.guarantorid
1327 LEFT JOIN old_issues USING (borrowernumber)
1328 LEFT JOIN issues USING (borrowernumber)|;
1329 if ( $filterpatronlist ){
1330 $query .= q| LEFT JOIN patron_list_patrons USING (borrowernumber)|;
1332 $query .= q| WHERE category_type <> 'S'
1333 AND tmp.guarantorid IS NULL
1336 if ( $filterbranch && $filterbranch ne "" ) {
1337 $query.= " AND borrowers.branchcode = ? ";
1338 push( @query_params, $filterbranch );
1340 if ( $filterexpiry ) {
1341 $query .= " AND dateexpiry < ? ";
1342 push( @query_params, $filterexpiry );
1344 if ( $filterlastseen ) {
1345 $query .= ' AND lastseen < ? ';
1346 push @query_params, $filterlastseen;
1348 if ( $filtercategory ) {
1349 $query .= " AND categorycode = ? ";
1350 push( @query_params, $filtercategory );
1352 if ( $filterpatronlist ){
1353 $query.=" AND patron_list_id = ? ";
1354 push( @query_params, $filterpatronlist );
1356 $query.=" GROUP BY borrowers.borrowernumber HAVING currentissue IS NULL ";
1357 if ( $filterdate ) {
1358 $query.=" AND ( latestissue < ? OR latestissue IS NULL ) ";
1359 push @query_params,$filterdate;
1361 warn $query if $debug;
1363 my $sth = $dbh->prepare($query);
1364 if (scalar(@query_params)>0){
1365 $sth->execute(@query_params);
1372 while ( my $data = $sth->fetchrow_hashref ) {
1373 push @results, $data;
1378 =head2 GetBorrowersWhoHaveNeverBorrowed
1380 $results = &GetBorrowersWhoHaveNeverBorrowed
1382 This function get all borrowers who have never borrowed.
1384 I<$result> is a ref to an array which all elements are a hasref.
1388 sub GetBorrowersWhoHaveNeverBorrowed {
1389 my $filterbranch = shift ||
1390 ((C4::Context->preference('IndependentBranches')
1391 && C4::Context->userenv
1392 && !C4::Context->IsSuperLibrarian()
1393 && C4::Context->userenv->{branch})
1394 ? C4::Context->userenv->{branch}
1396 my $dbh = C4::Context->dbh;
1398 SELECT borrowers.borrowernumber,max(timestamp) as latestissue
1400 LEFT JOIN issues ON borrowers.borrowernumber = issues.borrowernumber
1401 WHERE issues.borrowernumber IS NULL
1404 if ($filterbranch && $filterbranch ne ""){
1405 $query.=" AND borrowers.branchcode= ?";
1406 push @query_params,$filterbranch;
1408 warn $query if $debug;
1410 my $sth = $dbh->prepare($query);
1411 if (scalar(@query_params)>0){
1412 $sth->execute(@query_params);
1419 while ( my $data = $sth->fetchrow_hashref ) {
1420 push @results, $data;
1425 =head2 GetBorrowersWithIssuesHistoryOlderThan
1427 $results = &GetBorrowersWithIssuesHistoryOlderThan($date)
1429 this function get all borrowers who has an issue history older than I<$date> given on input arg.
1431 I<$result> is a ref to an array which all elements are a hashref.
1432 This hashref is containt the number of time this borrowers has borrowed before I<$date> and the borrowernumber.
1436 sub GetBorrowersWithIssuesHistoryOlderThan {
1437 my $dbh = C4::Context->dbh;
1438 my $date = shift ||POSIX::strftime("%Y-%m-%d",localtime());
1439 my $filterbranch = shift ||
1440 ((C4::Context->preference('IndependentBranches')
1441 && C4::Context->userenv
1442 && !C4::Context->IsSuperLibrarian()
1443 && C4::Context->userenv->{branch})
1444 ? C4::Context->userenv->{branch}
1447 SELECT count(borrowernumber) as n,borrowernumber
1449 WHERE returndate < ?
1450 AND borrowernumber IS NOT NULL
1453 push @query_params, $date;
1455 $query.=" AND branchcode = ?";
1456 push @query_params, $filterbranch;
1458 $query.=" GROUP BY borrowernumber ";
1459 warn $query if $debug;
1460 my $sth = $dbh->prepare($query);
1461 $sth->execute(@query_params);
1464 while ( my $data = $sth->fetchrow_hashref ) {
1465 push @results, $data;
1472 IssueSlip($branchcode, $borrowernumber, $quickslip)
1474 Returns letter hash ( see C4::Letters::GetPreparedLetter )
1476 $quickslip is boolean, to indicate whether we want a quick slip
1478 IssueSlip populates ISSUESLIP and ISSUEQSLIP, and will make the following expansions:
1514 NOTE: Not all table fields are available, pleasee see GetPendingIssues for a list of available fields.
1519 my ($branch, $borrowernumber, $quickslip) = @_;
1521 # FIXME Check callers before removing this statement
1522 #return unless $borrowernumber;
1524 my @issues = @{ GetPendingIssues($borrowernumber) };
1526 for my $issue (@issues) {
1527 $issue->{date_due} = $issue->{date_due_sql};
1529 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
1530 if ( substr( $issue->{issuedate}, 0, 10 ) eq $today
1531 or substr( $issue->{lastreneweddate}, 0, 10 ) eq $today ) {
1537 # Sort on timestamp then on issuedate (useful for tests and could be if modified in a batch
1539 my $s = $b->{timestamp} <=> $a->{timestamp};
1541 $b->{issuedate} <=> $a->{issuedate} : $s;
1544 my ($letter_code, %repeat);
1546 $letter_code = 'ISSUEQSLIP';
1548 'checkedout' => [ map {
1551 'biblioitems' => $_,
1553 }, grep { $_->{'now
'} } @issues ],
1557 $letter_code = 'ISSUESLIP
';
1559 'checkedout
' => [ map {
1562 'biblioitems
' => $_,
1564 }, grep { !$_->{'overdue
'} } @issues ],
1566 'overdue
' => [ map {
1569 'biblioitems
' => $_,
1571 }, grep { $_->{'overdue
'} } @issues ],
1574 $_->{'timestamp
'} = $_->{'newdate
'};
1576 } @{ GetNewsToDisplay("slip",$branch) } ],
1580 return C4::Letters::GetPreparedLetter (
1581 module => 'circulation
',
1582 letter_code => $letter_code,
1583 branchcode => $branch,
1585 'branches
' => $branch,
1586 'borrowers
' => $borrowernumber,
1592 =head2 GetBorrowersWithEmail
1594 ([$borrnum,$userid], ...) = GetBorrowersWithEmail('me
@example.com
');
1596 This gets a list of users and their basic details from their email address.
1597 As it's possible
for multiple user to have the same email address
, it provides
1598 you with all of them
. If there is
no userid
for the user
, there will be an
1599 C
<undef> there
. An empty list will be returned
if there are
no matches
.
1603 sub GetBorrowersWithEmail
{
1606 my $dbh = C4
::Context
->dbh;
1608 my $query = "SELECT borrowernumber, userid FROM borrowers WHERE email=?";
1609 my $sth=$dbh->prepare($query);
1610 $sth->execute($email);
1612 while (my $ref = $sth->fetch) {
1615 die "Failure searching for borrowers by email address: $sth->errstr" if $sth->err;
1619 =head2 AddMember_Opac
1623 sub AddMember_Opac
{
1624 my ( %borrower ) = @_;
1626 $borrower{'categorycode'} //= C4
::Context
->preference('PatronSelfRegistrationDefaultCategory');
1627 if (not defined $borrower{'password'}){
1628 my $sr = new String
::Random
;
1629 $sr->{'A'} = [ 'A'..'Z', 'a'..'z' ];
1630 my $password = $sr->randpattern("AAAAAAAAAA");
1631 $borrower{'password'} = $password;
1634 $borrower{'cardnumber'} = fixup_cardnumber
( $borrower{'cardnumber'} );
1636 my $borrowernumber = AddMember
(%borrower);
1638 return ( $borrowernumber, $borrower{'password'} );
1641 =head2 DeleteExpiredOpacRegistrations
1643 Delete accounts that haven't been upgraded from the 'temporary' category
1644 Returns the number of removed patrons
1648 sub DeleteExpiredOpacRegistrations
{
1650 my $delay = C4
::Context
->preference('PatronSelfRegistrationExpireTemporaryAccountsDelay');
1651 my $category_code = C4
::Context
->preference('PatronSelfRegistrationDefaultCategory');
1653 return 0 if not $category_code or not defined $delay or $delay eq q
||;
1656 SELECT borrowernumber
1658 WHERE categorycode
= ? AND DATEDIFF
( NOW
(), dateenrolled
) > ?
|;
1660 my $dbh = C4
::Context
->dbh;
1661 my $sth = $dbh->prepare($query);
1662 $sth->execute( $category_code, $delay );
1664 while ( my ($borrowernumber) = $sth->fetchrow_array() ) {
1665 Koha
::Patrons
->find($borrowernumber)->delete;
1671 =head2 DeleteUnverifiedOpacRegistrations
1673 Delete all unverified self registrations in borrower_modifications,
1674 older than the specified number of days.
1678 sub DeleteUnverifiedOpacRegistrations
{
1680 my $dbh = C4
::Context
->dbh;
1682 DELETE FROM borrower_modifications
1683 WHERE borrowernumber
= 0 AND DATEDIFF
( NOW
(), timestamp
) > ?
|;
1684 my $cnt=$dbh->do($sql, undef, ($days) );
1685 return $cnt eq '0E0'?
0: $cnt;
1688 sub GetOverduesForPatron
{
1689 my ( $borrowernumber ) = @_;
1693 FROM issues, items, biblio, biblioitems
1694 WHERE items.itemnumber=issues.itemnumber
1695 AND biblio.biblionumber = items.biblionumber
1696 AND biblio.biblionumber = biblioitems.biblionumber
1697 AND issues.borrowernumber = ?
1698 AND date_due < NOW()
1701 my $sth = C4
::Context
->dbh->prepare( $sql );
1702 $sth->execute( $borrowernumber );
1704 return $sth->fetchall_arrayref({});
1707 END { } # module clean-up code here (global destructor)