4 # A Class for hiding the ILS's concept of the patron from the OpenSIP
15 use Sys::Syslog qw(syslog);
23 use C4::Branch qw(GetBranchName);
24 use C4::Items qw( GetBarcodeFromItemnumber GetItemnumbersForBiblio);
25 use C4::Auth qw(checkpw_hash);
27 our $VERSION = 3.07.00.049;
29 our $kp; # koha patron
32 my ($class, $patron_id) = @_;
33 my $type = ref($class) || $class;
35 $kp = GetMember(cardnumber=>$patron_id) || GetMember(userid=>$patron_id);
36 $debug and warn "new Patron (GetMember): " . Dumper($kp);
37 unless (defined $kp) {
38 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
41 $kp = GetMemberDetails($kp->{borrowernumber});
42 $debug and warn "new Patron (GetMemberDetails): " . Dumper($kp);
43 my $pw = $kp->{password};
44 my $flags = $kp->{flags}; # or warn "Warning: No flags from patron object for '$patron_id'";
45 my $debarred = defined($kp->{flags}->{DBARRED});
46 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%{$kp->{flags}});
47 my ($day, $month, $year) = (localtime)[3,4,5];
48 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
49 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
51 if ($kp->{opacnote} ) {
52 $kp->{opacnote} .= q{ };
54 $kp->{opacnote} .= 'PATRON EXPIRED';
57 my $adr = _get_address($kp);
58 my $dob = $kp->{dateofbirth};
59 $dob and $dob =~ s/-//g; # YYYYMMDD
60 my $dexpiry = $kp->{dateexpiry};
61 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
62 my $fines_amount = $flags->{CHARGES}->{amount};
63 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
65 no warnings; # any of these $kp->{fields} being concat'd could be undef
67 getmemberdetails_object => $kp,
68 name => $kp->{firstname} . " " . $kp->{surname},
69 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
71 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
72 dateexpiry => $dexpiry,
73 dateexpiry_iso => $kp->{dateexpiry},
75 birthdate_iso => $kp->{dateofbirth},
76 branchcode => $kp->{branchcode},
77 library_name => "", # only populated if needed, cached here
78 borrowernumber => $kp->{borrowernumber},
80 home_phone => $kp->{phone},
81 email_addr => $kp->{email},
82 charge_ok => ( !$debarred && !$expired ),
83 renew_ok => ( !$debarred && !$expired ),
84 recall_ok => ( !$debarred && !$expired ),
85 hold_ok => ( !$debarred && !$expired ),
86 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
88 fines => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
89 fees => 0, # currently not distinct from fines
92 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
95 hold_items => $flags->{WAITING}->{itemlist},
96 overdue_items => $flags->{ODUES}->{itemlist},
100 inet => ( !$debarred && !$expired ),
104 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
105 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
106 ($flags->{$_}) or next;
107 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
108 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
110 if ($flags->{$_}->{noissues}) {
111 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
112 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
117 # FIXME: populate fine_items recall_items
118 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
119 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
121 $debug and warn Dumper($self);
122 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
142 charge_ok => 0, # for patron_status[0] (inverted)
143 renew_ok => 0, # for patron_status[1] (inverted)
144 recall_ok => 0, # for patron_status[2] (inverted)
145 hold_ok => 0, # for patron_status[3] (inverted)
146 card_lost => 0, # for patron_status[4]
152 too_many_charged => 0, # for patron_status[5]
153 too_many_overdue => 0, # for patron_status[6]
154 too_many_renewal => 0, # for patron_status[7]
155 too_many_claim_return => 0, # for patron_status[8]
156 too_many_lost => 0, # for patron_status[9]
157 # excessive_fines => 0, # for patron_status[10]
158 # excessive_fees => 0, # for patron_status[11]
159 recall_overdue => 0, # for patron_status[12]
160 too_many_billed => 0, # for patron_status[13]
161 inet => 0, # EnvisionWare extension
162 getmemberdetails_object => 0,
168 # be cool. needed for AUTOLOAD(?)
173 my $class = ref($self) or croak "$self is not an object";
174 my $name = $AUTOLOAD;
178 unless (exists $fields{$name}) {
179 croak "Cannot access '$name' field of class '$class'";
183 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
184 return $self->{$name} = shift;
186 return $self->{$name};
191 my ($self, $pwd) = @_;
192 defined $pwd or return 0; # you gotta give me something (at least ''), or no deal
194 my $hashed_pwd = $self->{password};
195 defined $hashed_pwd or return $pwd eq ''; # if the record has a NULL password, accept '' as match
197 # warn sprintf "check_password for %s: '%s' vs. '%s'",($self->{name}||''),($self->{password}||''),($pwd||'');
198 return checkpw_hash($pwd, $hashed_pwd);
201 # A few special cases, not in AUTOLOADed %fields
204 if ( $self->{fines} ) {
205 return $self->{fines};
212 return $self->fee_amount;
217 return $self->{language} || '000'; # Unspecified
222 return $self->{expired};
226 # remove the hold on item item_id from my hold queue.
227 # return true if I was holding the item, false otherwise.
230 my ($self, $item_id) = @_;
233 foreach (qw(hold_items unavail_holds)) {
235 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
236 my $held_item = $self->{$_}[$i]->{item_id} or next;
237 if ($held_item eq $item_id) {
238 splice @{$self->{$_}}, $i, 1;
246 # Accessor method for array_ref values, designed to get the "start" and "end" values
247 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
251 my $array_var = shift or return;
252 my ($start, $end) = @_;
255 if ($self->{$array_var}) {
256 if ($start && $start > 1) {
262 if ( $end && $end < @{$self->{$array_var}} ) {
265 $end = @{$self->{$array_var}};
268 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
275 # List of outstanding holds placed
279 my $item_arr = $self->x_items('hold_items', @_);
280 foreach my $item (@{$item_arr}) {
281 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
288 return $self->x_items('overdue_items', @_);
292 return $self->x_items('items', @_);
296 return $self->x_items('fine_items', @_);
300 return $self->x_items('recall_items', @_);
304 return $self->x_items('unavail_holds', @_);
308 my ($self, $card_retained, $blocked_card_msg) = @_;
309 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
312 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
313 # TODO: not really affecting patron record
319 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
322 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
323 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
324 $self->{recall_ok}, $self->{hold_ok});
325 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
329 sub inet_privileges {
331 return $self->{inet} ? 'Y' : 'N';
336 return C4::Context->preference("noissuescharge") || 5;
341 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
344 sub excessive_fines {
346 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
349 sub holds_blocked_by_excessive_fees {
351 return ( $self->fee_amount
352 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
357 unless ($self->{library_name}) {
358 $self->{library_name} = GetBranchName($self->{branchcode});
360 return $self->{library_name};
368 return "Please contact library staff";
373 return "Please contact library staff";
379 my $address = $patron->{streetnumber} || q{};
380 for my $field (qw( roaddetails address address2 city state zipcode country))
382 next unless $patron->{$field};
385 $address .= $patron->{$field};
388 $address .= $patron->{$field};
394 sub _get_outstanding_holds {
395 my $borrowernumber = shift;
396 my @hold_array = grep { !defined $_->{found} || $_->{found} ne 'W'} GetReservesFromBorrowernumber($borrowernumber);
397 foreach my $h (@hold_array) {
399 if ($h->{itemnumber}) {
400 $item = $h->{itemnumber};
403 # We need to return a barcode for the biblio so the client
404 # can request the biblio info
405 $item = ( GetItemnumbersForBiblio($h->{biblionumber}) )->[0];
407 $h->{barcode} = GetBarcodeFromItemnumber($item);
417 our %patron_example = (
419 name => "David J. Fiander",
422 ptype => 'A', # 'A'dult. Whatever.
423 birthdate => '19640925',
424 address => '2 Meadowvale Dr. St Thomas, ON',
425 home_phone => '(519) 555 1234',
426 email_addr => 'djfiander@hotmail.com',
432 claims_returned => 0,
442 fine_items => ['Computer Time'],
449 From borrowers table:
450 +---------------------+--------------+------+-----+---------+----------------+
451 | Field | Type | Null | Key | Default | Extra |
452 +---------------------+--------------+------+-----+---------+----------------+
453 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
454 | cardnumber | varchar(16) | YES | UNI | NULL | |
455 | surname | mediumtext | NO | | NULL | |
456 | firstname | text | YES | | NULL | |
457 | title | mediumtext | YES | | NULL | |
458 | othernames | mediumtext | YES | | NULL | |
459 | initials | text | YES | | NULL | |
460 | streetnumber | varchar(10) | YES | | NULL | |
461 | streettype | varchar(50) | YES | | NULL | |
462 | address | mediumtext | NO | | NULL | |
463 | address2 | text | YES | | NULL | |
464 | city | mediumtext | NO | | NULL | |
465 | state | mediumtext | YES | | NULL | |
466 | zipcode | varchar(25) | YES | | NULL | |
467 | country | text | YES | | NULL | |
468 | email | mediumtext | YES | | NULL | |
469 | phone | text | YES | | NULL | |
470 | mobile | varchar(50) | YES | | NULL | |
471 | fax | mediumtext | YES | | NULL | |
472 | emailpro | text | YES | | NULL | |
473 | phonepro | text | YES | | NULL | |
474 | B_streetnumber | varchar(10) | YES | | NULL | |
475 | B_streettype | varchar(50) | YES | | NULL | |
476 | B_address | varchar(100) | YES | | NULL | |
477 | B_address2 | text | YES | | NULL | |
478 | B_city | mediumtext | YES | | NULL | |
479 | B_state | mediumtext | YES | | NULL | |
480 | B_zipcode | varchar(25) | YES | | NULL | |
481 | B_country | text | YES | | NULL | |
482 | B_email | text | YES | | NULL | |
483 | B_phone | mediumtext | YES | | NULL | |
484 | dateofbirth | date | YES | | NULL | |
485 | branchcode | varchar(10) | NO | MUL | | |
486 | categorycode | varchar(10) | NO | MUL | | |
487 | dateenrolled | date | YES | | NULL | |
488 | dateexpiry | date | YES | | NULL | |
489 | gonenoaddress | tinyint(1) | YES | | NULL | |
490 | lost | tinyint(1) | YES | | NULL | |
491 | debarred | tinyint(1) | YES | | NULL | |
492 | contactname | mediumtext | YES | | NULL | |
493 | contactfirstname | text | YES | | NULL | |
494 | contacttitle | text | YES | | NULL | |
495 | guarantorid | int(11) | YES | MUL | NULL | |
496 | borrowernotes | mediumtext | YES | | NULL | |
497 | relationship | varchar(100) | YES | | NULL | |
498 | ethnicity | varchar(50) | YES | | NULL | |
499 | ethnotes | varchar(255) | YES | | NULL | |
500 | sex | varchar(1) | YES | | NULL | |
501 | password | varchar(30) | YES | | NULL | |
502 | flags | int(11) | YES | | NULL | |
503 | userid | varchar(30) | YES | MUL | NULL | |
504 | opacnote | mediumtext | YES | | NULL | |
505 | contactnote | varchar(255) | YES | | NULL | |
506 | sort1 | varchar(80) | YES | | NULL | |
507 | sort2 | varchar(80) | YES | | NULL | |
508 | altcontactfirstname | varchar(255) | YES | | NULL | |
509 | altcontactsurname | varchar(255) | YES | | NULL | |
510 | altcontactaddress1 | varchar(255) | YES | | NULL | |
511 | altcontactaddress2 | varchar(255) | YES | | NULL | |
512 | altcontactaddress3 | varchar(255) | YES | | NULL | |
513 | altcontactstate | mediumtext | YES | | NULL | |
514 | altcontactzipcode | varchar(50) | YES | | NULL | |
515 | altcontactcountry | text | YES | | NULL | |
516 | altcontactphone | varchar(50) | YES | | NULL | |
517 | smsalertnumber | varchar(50) | YES | | NULL | |
518 | privacy | int(11) | NO | | 1 | |
519 +---------------------+--------------+------+-----+---------+----------------+
526 {message} Message showing patron's credit or debt
527 {noissues} Set if patron owes >$5.00
528 {GNA} Set if patron gone w/o address
529 {message} "Borrower has no valid address"
531 {LOST} Set if patron's card reported lost
532 {message} Message to this effect
534 {DBARRED} Set if patron is debarred
535 {message} Message to this effect
537 {NOTES} Set if patron has notes
538 {message} Notes about patron
539 {ODUES} Set if patron has overdue books
541 {itemlist} ref-to-array: list of overdue books
542 {itemlisttext} Text list of overdue items
543 {WAITING} Set if there are items available that the patron reserved
544 {message} Message to this effect
545 {itemlist} ref-to-array: list of available items