4 # A Class for hiding the ILS's concept of the patron from the OpenSIP
8 package C4::SIP::ILS::Patron;
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);
28 our $kp; # koha patron
31 my ($class, $patron_id) = @_;
32 my $type = ref($class) || $class;
34 $kp = GetMember(cardnumber=>$patron_id) || GetMember(userid=>$patron_id);
35 $debug and warn "new Patron (GetMember): " . Dumper($kp);
36 unless (defined $kp) {
37 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
40 $kp = GetMemberDetails($kp->{borrowernumber});
41 $debug and warn "new Patron (GetMemberDetails): " . Dumper($kp);
42 my $pw = $kp->{password};
43 my $flags = $kp->{flags}; # or warn "Warning: No flags from patron object for '$patron_id'";
44 my $debarred = defined($kp->{flags}->{DBARRED});
45 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%{$kp->{flags}});
46 my ($day, $month, $year) = (localtime)[3,4,5];
47 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
48 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
50 if ($kp->{opacnote} ) {
51 $kp->{opacnote} .= q{ };
53 $kp->{opacnote} .= 'PATRON EXPIRED';
56 my $adr = _get_address($kp);
57 my $dob = $kp->{dateofbirth};
58 $dob and $dob =~ s/-//g; # YYYYMMDD
59 my $dexpiry = $kp->{dateexpiry};
60 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
61 my $fines_amount = $flags->{CHARGES}->{amount};
62 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
63 my $fee_limit = _fee_limit();
64 my $fine_blocked = $fines_amount > $fee_limit;
66 no warnings; # any of these $kp->{fields} being concat'd could be undef
68 getmemberdetails_object => $kp,
69 name => $kp->{firstname} . " " . $kp->{surname},
70 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
72 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
73 dateexpiry => $dexpiry,
74 dateexpiry_iso => $kp->{dateexpiry},
76 birthdate_iso => $kp->{dateofbirth},
77 branchcode => $kp->{branchcode},
78 library_name => "", # only populated if needed, cached here
79 borrowernumber => $kp->{borrowernumber},
81 home_phone => $kp->{phone},
82 email_addr => $kp->{email},
83 charge_ok => ( !$debarred && !$expired && !$fine_blocked),
84 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
85 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
86 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
87 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
89 fines => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
90 fees => 0, # currently not distinct from fines
93 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
96 hold_items => $flags->{WAITING}->{itemlist},
97 overdue_items => $flags->{ODUES}->{itemlist},
101 inet => ( !$debarred && !$expired ),
103 fee_limit => $fee_limit,
104 userid => $kp->{userid},
107 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
108 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
109 ($flags->{$_}) or next;
110 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
111 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
113 if ($flags->{$_}->{noissues}) {
114 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
115 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
120 # FIXME: populate fine_items recall_items
121 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
122 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
124 $debug and warn Dumper($self);
125 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
145 charge_ok => 0, # for patron_status[0] (inverted)
146 renew_ok => 0, # for patron_status[1] (inverted)
147 recall_ok => 0, # for patron_status[2] (inverted)
148 hold_ok => 0, # for patron_status[3] (inverted)
149 card_lost => 0, # for patron_status[4]
155 too_many_charged => 0, # for patron_status[5]
156 too_many_overdue => 0, # for patron_status[6]
157 too_many_renewal => 0, # for patron_status[7]
158 too_many_claim_return => 0, # for patron_status[8]
159 too_many_lost => 0, # for patron_status[9]
160 # excessive_fines => 0, # for patron_status[10]
161 # excessive_fees => 0, # for patron_status[11]
162 recall_overdue => 0, # for patron_status[12]
163 too_many_billed => 0, # for patron_status[13]
164 inet => 0, # EnvisionWare extension
165 getmemberdetails_object => 0,
171 # be cool. needed for AUTOLOAD(?)
176 my $class = ref($self) or croak "$self is not an object";
177 my $name = $AUTOLOAD;
181 unless (exists $fields{$name}) {
182 croak "Cannot access '$name' field of class '$class'";
186 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
187 return $self->{$name} = shift;
189 return $self->{$name};
194 my ( $self, $pwd ) = @_;
197 or return 0; # you gotta give me something (at least ''), or no deal
200 if $pwd eq q{}; # if the record has a NULL password, accept '' as match
202 my $dbh = C4::Context->dbh;
204 ($ret) = checkpw( $dbh, $self->{userid}, $pwd );
208 # A few special cases, not in AUTOLOADed %fields
211 if ( $self->{fines} ) {
212 return $self->{fines};
219 return $self->fee_amount;
224 return $self->{language} || '000'; # Unspecified
229 return $self->{expired};
233 # remove the hold on item item_id from my hold queue.
234 # return true if I was holding the item, false otherwise.
237 my ($self, $item_id) = @_;
240 foreach (qw(hold_items unavail_holds)) {
242 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
243 my $held_item = $self->{$_}[$i]->{item_id} or next;
244 if ($held_item eq $item_id) {
245 splice @{$self->{$_}}, $i, 1;
253 # Accessor method for array_ref values, designed to get the "start" and "end" values
254 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
258 my $array_var = shift or return;
259 my ($start, $end) = @_;
262 if ($self->{$array_var}) {
263 if ($start && $start > 1) {
269 if ( $end && $end < @{$self->{$array_var}} ) {
272 $end = @{$self->{$array_var}};
275 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
282 # List of outstanding holds placed
286 my $item_arr = $self->x_items('hold_items', @_);
287 foreach my $item (@{$item_arr}) {
288 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
295 return $self->x_items('overdue_items', @_);
299 return $self->x_items('items', @_);
303 return $self->x_items('fine_items', @_);
307 return $self->x_items('recall_items', @_);
311 return $self->x_items('unavail_holds', @_);
315 my ($self, $card_retained, $blocked_card_msg) = @_;
316 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
319 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
320 # TODO: not really affecting patron record
326 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
329 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
330 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
331 $self->{recall_ok}, $self->{hold_ok});
332 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
336 sub inet_privileges {
338 return $self->{inet} ? 'Y' : 'N';
342 return C4::Context->preference('noissuescharge') || 5;
347 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
350 sub excessive_fines {
352 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
355 sub holds_blocked_by_excessive_fees {
357 return ( $self->fee_amount
358 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
363 unless ($self->{library_name}) {
364 $self->{library_name} = GetBranchName($self->{branchcode});
366 return $self->{library_name};
374 return "Please contact library staff";
379 return "Please contact library staff";
385 my $address = $patron->{streetnumber} || q{};
386 for my $field (qw( roaddetails address address2 city state zipcode country))
388 next unless $patron->{$field};
391 $address .= $patron->{$field};
394 $address .= $patron->{$field};
400 sub _get_outstanding_holds {
401 my $borrowernumber = shift;
402 my @hold_array = grep { !defined $_->{found} || $_->{found} ne 'W'} GetReservesFromBorrowernumber($borrowernumber);
403 foreach my $h (@hold_array) {
405 if ($h->{itemnumber}) {
406 $item = $h->{itemnumber};
409 # We need to return a barcode for the biblio so the client
410 # can request the biblio info
411 $item = ( GetItemnumbersForBiblio($h->{biblionumber}) )->[0];
413 $h->{barcode} = GetBarcodeFromItemnumber($item);
423 our %patron_example = (
425 name => "David J. Fiander",
428 ptype => 'A', # 'A'dult. Whatever.
429 birthdate => '19640925',
430 address => '2 Meadowvale Dr. St Thomas, ON',
431 home_phone => '(519) 555 1234',
432 email_addr => 'djfiander@hotmail.com',
438 claims_returned => 0,
448 fine_items => ['Computer Time'],
455 From borrowers table:
456 +---------------------+--------------+------+-----+---------+----------------+
457 | Field | Type | Null | Key | Default | Extra |
458 +---------------------+--------------+------+-----+---------+----------------+
459 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
460 | cardnumber | varchar(16) | YES | UNI | NULL | |
461 | surname | mediumtext | NO | | NULL | |
462 | firstname | text | YES | | NULL | |
463 | title | mediumtext | YES | | NULL | |
464 | othernames | mediumtext | YES | | NULL | |
465 | initials | text | YES | | NULL | |
466 | streetnumber | varchar(10) | YES | | NULL | |
467 | streettype | varchar(50) | YES | | NULL | |
468 | address | mediumtext | NO | | NULL | |
469 | address2 | text | YES | | NULL | |
470 | city | mediumtext | NO | | NULL | |
471 | state | mediumtext | YES | | NULL | |
472 | zipcode | varchar(25) | YES | | NULL | |
473 | country | text | YES | | NULL | |
474 | email | mediumtext | YES | | NULL | |
475 | phone | text | YES | | NULL | |
476 | mobile | varchar(50) | YES | | NULL | |
477 | fax | mediumtext | YES | | NULL | |
478 | emailpro | text | YES | | NULL | |
479 | phonepro | text | YES | | NULL | |
480 | B_streetnumber | varchar(10) | YES | | NULL | |
481 | B_streettype | varchar(50) | YES | | NULL | |
482 | B_address | varchar(100) | YES | | NULL | |
483 | B_address2 | text | YES | | NULL | |
484 | B_city | mediumtext | YES | | NULL | |
485 | B_state | mediumtext | YES | | NULL | |
486 | B_zipcode | varchar(25) | YES | | NULL | |
487 | B_country | text | YES | | NULL | |
488 | B_email | text | YES | | NULL | |
489 | B_phone | mediumtext | YES | | NULL | |
490 | dateofbirth | date | YES | | NULL | |
491 | branchcode | varchar(10) | NO | MUL | | |
492 | categorycode | varchar(10) | NO | MUL | | |
493 | dateenrolled | date | YES | | NULL | |
494 | dateexpiry | date | YES | | NULL | |
495 | gonenoaddress | tinyint(1) | YES | | NULL | |
496 | lost | tinyint(1) | YES | | NULL | |
497 | debarred | tinyint(1) | YES | | NULL | |
498 | contactname | mediumtext | YES | | NULL | |
499 | contactfirstname | text | YES | | NULL | |
500 | contacttitle | text | YES | | NULL | |
501 | guarantorid | int(11) | YES | MUL | NULL | |
502 | borrowernotes | mediumtext | YES | | NULL | |
503 | relationship | varchar(100) | YES | | NULL | |
504 | ethnicity | varchar(50) | YES | | NULL | |
505 | ethnotes | varchar(255) | YES | | NULL | |
506 | sex | varchar(1) | YES | | NULL | |
507 | password | varchar(30) | YES | | NULL | |
508 | flags | int(11) | YES | | NULL | |
509 | userid | varchar(30) | YES | MUL | NULL | |
510 | opacnote | mediumtext | YES | | NULL | |
511 | contactnote | varchar(255) | YES | | NULL | |
512 | sort1 | varchar(80) | YES | | NULL | |
513 | sort2 | varchar(80) | YES | | NULL | |
514 | altcontactfirstname | varchar(255) | YES | | NULL | |
515 | altcontactsurname | varchar(255) | YES | | NULL | |
516 | altcontactaddress1 | varchar(255) | YES | | NULL | |
517 | altcontactaddress2 | varchar(255) | YES | | NULL | |
518 | altcontactaddress3 | varchar(255) | YES | | NULL | |
519 | altcontactstate | mediumtext | YES | | NULL | |
520 | altcontactzipcode | varchar(50) | YES | | NULL | |
521 | altcontactcountry | text | YES | | NULL | |
522 | altcontactphone | varchar(50) | YES | | NULL | |
523 | smsalertnumber | varchar(50) | YES | | NULL | |
524 | privacy | int(11) | NO | | 1 | |
525 +---------------------+--------------+------+-----+---------+----------------+
532 {message} Message showing patron's credit or debt
533 {noissues} Set if patron owes >$5.00
534 {GNA} Set if patron gone w/o address
535 {message} "Borrower has no valid address"
537 {LOST} Set if patron's card reported lost
538 {message} Message to this effect
540 {DBARRED} Set if patron is debarred
541 {message} Message to this effect
543 {NOTES} Set if patron has notes
544 {message} Notes about patron
545 {ODUES} Set if patron has overdue books
547 {itemlist} ref-to-array: list of overdue books
548 {itemlisttext} Text list of overdue items
549 {WAITING} Set if there are items available that the patron reserved
550 {message} Message to this effect
551 {itemlist} ref-to-array: list of available items