4 # A Class for hiding the ILS's concept of the patron from the OpenSIP
15 use Sys::Syslog qw(syslog);
24 use C4::Branch qw(GetBranchName);
25 use Digest::MD5 qw(md5_base64);
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
32 @EXPORT_OK = qw(invalid_patron);
35 our $kp; # koha patron
38 my ($class, $patron_id) = @_;
39 my $type = ref($class) || $class;
41 $kp = GetMember($patron_id,'cardnumber');
42 $debug and warn "new Patron (GetMember): " . Dumper($kp);
43 unless (defined $kp) {
44 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
47 $kp = GetMemberDetails(undef,$patron_id);
48 $debug and warn "new Patron (GetMemberDetails): " . Dumper($kp);
49 my $pw = $kp->{password}; ### FIXME - md5hash -- deal with .
50 my $flags = $kp->{flags}; # or warn "Warning: No flags from patron object for '$patron_id'";
51 my $debarred = $kp->{debarred}; # 1 if ($kp->{flags}->{DBARRED}->{noissues});
52 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%{$kp->{flags}});
54 my $adr = $kp->{streetnumber} || '';
55 my $address = $kp->{address} || '';
56 my $dob = $kp->{dateofbirth};
57 $dob and $dob =~ s/-//g; # YYYYMMDD
58 $adr .= ($adr && $address) ? " $address" : $address;
59 my $fines_amount = $flags->{CHARGES}->{amount};
60 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
62 no warnings; # any of these $kp->{fields} being concat'd could be undef
64 getmemberdetails_object => $kp,
65 name => $kp->{firstname} . " " . $kp->{surname},
66 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
68 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
70 birthdate_iso => $kp->{dateofbirth},
71 branchcode => $kp->{branchcode},
72 library_name => "", # only populated if needed, cached here
73 borrowernumber => $kp->{borrowernumber},
75 home_phone => $kp->{phone},
76 email_addr => $kp->{email},
77 charge_ok => ( !$debarred ),
78 renew_ok => ( !$debarred ),
79 recall_ok => ( !$debarred ),
80 hold_ok => ( !$debarred ),
81 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
83 fines => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
84 fees => 0, # currently not distinct from fines
87 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
90 hold_items => $flags->{WAITING}{itemlist},
91 overdue_items => $flags->{ODUES}{itemlist},
95 inet => ( !$debarred ),
98 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
99 for (qw(CHARGES CREDITS GNA LOST DBARRED NOTES)) {
100 ($flags->{$_}) or next;
101 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
102 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
104 if ($flags->{$_}->{noissues}) {
105 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
106 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
111 # FIXME: populate fine_items recall_items
112 # $ilspatron{hold_items} = (GetReservesFromBorrowernumber($kp->{borrowernumber},'F'));
113 $ilspatron{unavail_holds} = [(GetReservesFromBorrowernumber($kp->{borrowernumber}))];
114 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
116 $debug and warn Dumper($self);
117 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
135 charge_ok => 0, # for patron_status[0] (inverted)
136 renew_ok => 0, # for patron_status[1] (inverted)
137 recall_ok => 0, # for patron_status[2] (inverted)
138 hold_ok => 0, # for patron_status[3] (inverted)
139 card_lost => 0, # for patron_status[4]
145 too_many_charged => 0, # for patron_status[5]
146 too_many_overdue => 0, # for patron_status[6]
147 too_many_renewal => 0, # for patron_status[7]
148 too_many_claim_return => 0, # for patron_status[8]
149 too_many_lost => 0, # for patron_status[9]
150 # excessive_fines => 0, # for patron_status[10]
151 # excessive_fees => 0, # for patron_status[11]
152 recall_overdue => 0, # for patron_status[12]
153 too_many_billed => 0, # for patron_status[13]
154 inet => 0, # EnvisionWare extension
155 getmemberdetails_object => 0,
161 # be cool. needed for AUTOLOAD(?)
166 my $class = ref($self) or croak "$self is not an object";
167 my $name = $AUTOLOAD;
171 unless (exists $fields{$name}) {
172 croak "Cannot access '$name' field of class '$class'";
176 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
177 return $self->{$name} = shift;
179 return $self->{$name};
184 my ($self, $pwd) = @_;
185 my $md5pwd = $self->{password};
186 # warn sprintf "check_password for %s: '%s' vs. '%s'",($self->{name}||''),($self->{password}||''),($pwd||'');
187 (defined $pwd ) or return 0; # you gotta give me something (at least ''), or no deal
188 (defined $md5pwd) or return($pwd eq ''); # if the record has a NULL password, accept '' as match
189 return (md5_base64($pwd) eq $md5pwd);
192 # A few special cases, not in AUTOLOADed %fields
195 return $self->{fines} || undef;
200 return $self->fee_amount;
205 return $self->{language} || '000'; # Unspecified
209 # remove the hold on item item_id from my hold queue.
210 # return true if I was holding the item, false otherwise.
213 my ($self, $item_id) = @_;
214 $item_id or return undef;
216 foreach (qw(hold_items unavail_holds)) {
218 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
219 my $held_item = $self->{$_}[$i]->{item_id} or next;
220 if ($held_item eq $item_id) {
221 splice @{$self->{$_}}, $i, 1;
229 # Accessor method for array_ref values, designed to get the "start" and "end" values
230 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
233 my $self = shift or return;
234 my $array_var = shift or return;
235 my ($start, $end) = @_;
236 $self->{$array_var} or return [];
237 $start = 1 unless defined($start);
238 $end = scalar @{$self->{$array_var}} unless defined($end);
239 # syslog("LOG_DEBUG", "$array_var: start = %d, end = %d; items(%s)", $start, $end, join(', ', @{$self->{items}}));
241 return [@{$self->{$array_var}}[$start-1 .. $end-1]];
245 # List of outstanding holds placed
248 my $self = shift or return;
249 return $self->x_items('hold_items', @_);
253 my $self = shift or return;
254 return $self->x_items('overdue_items', @_);
257 my $self = shift or return;
258 return $self->x_items('items', @_);
261 my $self = shift or return;
262 return $self->x_items('fine_items', @_);
265 my $self = shift or return;
266 return $self->x_items('recall_items', @_);
269 my $self = shift or return;
270 return $self->x_items('unavail_holds', @_);
274 my ($self, $card_retained, $blocked_card_msg) = @_;
275 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
278 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
279 # TODO: not really affecting patron record
285 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
288 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
289 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
290 $self->{recall_ok}, $self->{hold_ok});
291 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
295 sub inet_privileges {
297 return $self->{inet} ? 'Y' : 'N';
302 return C4::Context->preference("noissuescharge") || 5;
306 my $self = shift or return;
307 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
309 sub excessive_fines {
310 my $self = shift or return;
311 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
316 unless ($self->{library_name}) {
317 $self->{library_name} = GetBranchName($self->{branchcode});
319 return $self->{library_name};
326 return "Please contact library staff";
330 return "Please contact library staff";
338 our %patron_example = (
340 name => "David J. Fiander",
343 ptype => 'A', # 'A'dult. Whatever.
344 birthdate => '19640925',
345 address => '2 Meadowvale Dr. St Thomas, ON',
346 home_phone => '(519) 555 1234',
347 email_addr => 'djfiander@hotmail.com',
353 claims_returned => 0,
363 fine_items => ['Computer Time'],
370 From borrowers table:
371 +---------------------+--------------+------+-----+
372 | Field | Type | Null | Key |
373 +---------------------+--------------+------+-----+
374 | borrowernumber | int(11) | NO | PRI |
375 | cardnumber | varchar(16) | YES | UNI |
376 | surname | mediumtext | NO | |
377 | firstname | text | YES | |
378 | title | mediumtext | YES | |
379 | othernames | mediumtext | YES | |
380 | initials | text | YES | |
381 | streetnumber | varchar(10) | YES | |
382 | streettype | varchar(50) | YES | |
383 | address | mediumtext | NO | |
384 | address2 | text | YES | |
385 | city | mediumtext | NO | |
386 | zipcode | varchar(25) | YES | |
387 | email | mediumtext | YES | |
388 | phone | text | YES | |
389 | mobile | varchar(50) | YES | |
390 | fax | mediumtext | YES | |
391 | emailpro | text | YES | |
392 | phonepro | text | YES | |
393 | B_streetnumber | varchar(10) | YES | |
394 | B_streettype | varchar(50) | YES | |
395 | B_address | varchar(100) | YES | |
396 | B_city | mediumtext | YES | |
397 | B_zipcode | varchar(25) | YES | |
398 | B_email | text | YES | |
399 | B_phone | mediumtext | YES | |
400 | dateofbirth | date | YES | |
401 | branchcode | varchar(10) | NO | MUL |
402 | categorycode | varchar(10) | NO | MUL |
403 | dateenrolled | date | YES | |
404 | dateexpiry | date | YES | |
405 | gonenoaddress | tinyint(1) | YES | |
406 | lost | tinyint(1) | YES | |
407 | debarred | tinyint(1) | YES | |
408 | contactname | mediumtext | YES | |
409 | contactfirstname | text | YES | |
410 | contacttitle | text | YES | |
411 | guarantorid | int(11) | YES | |
412 | borrowernotes | mediumtext | YES | |
413 | relationship | varchar(100) | YES | |
414 | ethnicity | varchar(50) | YES | |
415 | ethnotes | varchar(255) | YES | |
416 | sex | varchar(1) | YES | |
417 | password | varchar(30) | YES | |
418 | flags | int(11) | YES | |
419 | userid | varchar(30) | YES | MUL |
420 | opacnote | mediumtext | YES | |
421 | contactnote | varchar(255) | YES | |
422 | sort1 | varchar(80) | YES | |
423 | sort2 | varchar(80) | YES | |
424 | altcontactfirstname | varchar(255) | YES | |
425 | altcontactsurname | varchar(255) | YES | |
426 | altcontactaddress1 | varchar(255) | YES | |
427 | altcontactaddress2 | varchar(255) | YES | |
428 | altcontactaddress3 | varchar(255) | YES | |
429 | altcontactzipcode | varchar(50) | YES | |
430 | altcontactphone | varchar(50) | YES | |
431 +---------------------+--------------+------+-----+
437 {message} Message showing patron's credit or debt
438 {noissues} Set if patron owes >$5.00
439 {GNA} Set if patron gone w/o address
440 {message} "Borrower has no valid address"
442 {LOST} Set if patron's card reported lost
443 {message} Message to this effect
445 {DBARRED} Set if patron is debarred
446 {message} Message to this effect
448 {NOTES} Set if patron has notes
449 {message} Notes about patron
450 {ODUES} Set if patron has overdue books
452 {itemlist} ref-to-array: list of overdue books
453 {itemlisttext} Text list of overdue items
454 {WAITING} Set if there are items available that the patron reserved
455 {message} Message to this effect
456 {itemlist} ref-to-array: list of available items