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);
18 use C4::SIP::Sip qw(add_field);
25 use C4::Items qw( GetItemnumbersForBiblio);
26 use C4::Auth qw(checkpw);
32 our $kp; # koha patron
39 my ($class, $patron_id) = @_;
40 my $type = ref($class) || $class;
42 my $patron = Koha::Patrons->find( { cardnumber => $patron_id } )
43 || Koha::Patrons->find( { userid => $patron_id } );
44 $debug and warn "new Patron: " . Dumper($patron->unblessed) if $patron;
46 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
49 $kp = $patron->unblessed;
50 my $pw = $kp->{password};
51 my $flags = C4::Members::patronflags( $kp );
52 my $debarred = $patron->is_debarred;
53 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')); # Do we need more debug info here?
54 my ($day, $month, $year) = (localtime)[3,4,5];
55 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
56 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
58 if ($kp->{opacnote} ) {
59 $kp->{opacnote} .= q{ };
61 $kp->{opacnote} .= 'PATRON EXPIRED';
64 my $adr = _get_address($kp);
65 my $dob = $kp->{dateofbirth};
66 $dob and $dob =~ s/-//g; # YYYYMMDD
67 my $dexpiry = $kp->{dateexpiry};
68 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
69 my $fines_amount = $patron->account->balance;
70 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
71 my $fee_limit = _fee_limit();
72 my $fine_blocked = $fines_amount > $fee_limit;
73 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
75 no warnings; # any of these $kp->{fields} being concat'd could be undef
77 name => $kp->{firstname} . " " . $kp->{surname},
78 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
80 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
81 dateexpiry => $dexpiry,
82 dateexpiry_iso => $kp->{dateexpiry},
84 birthdate_iso => $kp->{dateofbirth},
85 branchcode => $kp->{branchcode},
86 library_name => "", # only populated if needed, cached here
87 borrowernumber => $kp->{borrowernumber},
89 home_phone => $kp->{phone},
90 email_addr => $kp->{email},
91 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
92 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
93 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
94 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
95 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
97 fines => $fines_amount,
98 fees => 0, # currently not distinct from fines
101 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
104 hold_items => $flags->{WAITING}->{itemlist},
105 overdue_items => $flags->{ODUES}->{itemlist},
109 inet => ( !$debarred && !$expired ),
111 fee_limit => $fee_limit,
112 userid => $kp->{userid},
115 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
117 if ( $patron->is_debarred and $patron->debarredcomment ) {
118 $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
120 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
121 ($flags->{$_}) or next;
122 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
123 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
125 if ($flags->{$_}->{noissues}) {
126 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
127 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
132 # FIXME: populate fine_items recall_items
133 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
134 $ilspatron{items} = $patron->pending_checkouts->unblessed;
136 $debug and warn Dumper($self);
137 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
157 charge_ok => 0, # for patron_status[0] (inverted)
158 renew_ok => 0, # for patron_status[1] (inverted)
159 recall_ok => 0, # for patron_status[2] (inverted)
160 hold_ok => 0, # for patron_status[3] (inverted)
161 card_lost => 0, # for patron_status[4]
167 too_many_charged => 0, # for patron_status[5]
168 too_many_overdue => 0, # for patron_status[6]
169 too_many_renewal => 0, # for patron_status[7]
170 too_many_claim_return => 0, # for patron_status[8]
171 too_many_lost => 0, # for patron_status[9]
172 # excessive_fines => 0, # for patron_status[10]
173 # excessive_fees => 0, # for patron_status[11]
174 recall_overdue => 0, # for patron_status[12]
175 too_many_billed => 0, # for patron_status[13]
176 inet => 0, # EnvisionWare extension
182 # be cool. needed for AUTOLOAD(?)
187 my $class = ref($self) or croak "$self is not an object";
188 my $name = $AUTOLOAD;
192 unless (exists $fields{$name}) {
193 croak "Cannot access '$name' field of class '$class'";
197 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
198 return $self->{$name} = shift;
200 return $self->{$name};
205 my ( $self, $template ) = @_;
209 require Koha::Patrons;
211 my $tt = Template->new();
213 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
216 $tt->process( \$template, { patron => $patron }, \$output );
220 return $self->{name};
225 my ( $self, $pwd ) = @_;
227 # you gotta give me something (at least ''), or no deal
228 return 0 unless defined $pwd;
230 # If the record has a NULL password, accept '' as match
231 return $pwd eq q{} unless $self->{password};
233 my $dbh = C4::Context->dbh;
235 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
239 # A few special cases, not in AUTOLOADed %fields
242 if ( $self->{fines} ) {
243 return $self->{fines};
250 return $self->fee_amount;
255 return $self->{language} || '000'; # Unspecified
260 return $self->{expired};
264 # remove the hold on item item_id from my hold queue.
265 # return true if I was holding the item, false otherwise.
268 my ($self, $item_id) = @_;
271 foreach (qw(hold_items unavail_holds)) {
273 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
274 my $held_item = $self->{$_}[$i]->{item_id} or next;
275 if ($held_item eq $item_id) {
276 splice @{$self->{$_}}, $i, 1;
284 # Accessor method for array_ref values, designed to get the "start" and "end" values
285 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
289 my $array_var = shift or return;
290 my ($start, $end) = @_;
293 if ($self->{$array_var}) {
294 if ($start && $start > 1) {
300 if ( $end && $end < @{$self->{$array_var}} ) {
303 $end = @{$self->{$array_var}};
306 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
313 # List of outstanding holds placed
317 my $item_arr = $self->x_items('hold_items', @_);
318 foreach my $item (@{$item_arr}) {
319 my $item_obj = Koha::Items->find($item->{itemnumber});
320 $item_arr->{barcode} = $item_obj ? $item_obj->barcode : undef;
327 return $self->x_items('overdue_items', @_);
331 return $self->x_items('items', @_);
334 require Koha::Database;
342 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
344 borrowernumber => $self->{borrowernumber},
345 amountoutstanding => { '>' => '0' },
349 $start = $start ? $start - 1 : 0;
350 $end = $end ? $end : scalar @fees - 1;
352 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
353 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
355 my $tt = Template->new();
358 for ( my $i = $start; $i <= $end; $i++ ) {
362 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
363 push( @return_values, { barcode => $output } );
366 return \@return_values;
370 return $self->x_items('recall_items', @_);
374 return $self->x_items('unavail_holds', @_);
378 my ($self, $card_retained, $blocked_card_msg) = @_;
379 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
382 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
383 # TODO: not really affecting patron record
389 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
392 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
393 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
394 $self->{recall_ok}, $self->{hold_ok});
395 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
399 sub inet_privileges {
401 return $self->{inet} ? 'Y' : 'N';
405 return C4::Context->preference('noissuescharge') || 5;
410 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
413 sub excessive_fines {
415 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
418 sub holds_blocked_by_excessive_fees {
420 return ( $self->fee_amount
421 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
426 unless ($self->{library_name}) {
427 my $library = Koha::Libraries->find( $self->{branchcode} );
428 $self->{library_name} = $library ? $library->branchname : '';
430 return $self->{library_name};
438 return "Please contact library staff";
443 return "Please contact library staff";
446 =head2 update_lastseen
448 $patron->update_lastseen();
450 Patron method to update lastseen field in borrower
451 to record that patron has been seen via sip connection
455 sub update_lastseen {
457 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
458 $kohaobj->track_login if $kohaobj; # track_login checks the pref
464 my $address = $patron->{streetnumber} || q{};
465 for my $field (qw( roaddetails address address2 city state zipcode country))
467 next unless $patron->{$field};
470 $address .= $patron->{$field};
473 $address .= $patron->{$field};
479 sub _get_outstanding_holds {
480 my $borrowernumber = shift;
482 my $patron = Koha::Patrons->find( $borrowernumber );
483 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
485 while ( my $hold = $holds->next ) {
487 if ($hold->itemnumber) {
491 # We need to return a barcode for the biblio so the client
492 # can request the biblio info
493 my $items = $hold->biblio->items;
494 $item = $items->count ? $item->next : undef;
496 my $unblessed_hold = $hold->unblessed;
498 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
500 push @holds, $unblessed_hold;
505 =head2 build_patron_attributes_string
507 This method builds the part of the sip message for extended patron
508 attributes as defined in the sip config
512 sub build_patron_attributes_string {
513 my ( $self, $server ) = @_;
517 if ( $server->{account}->{patron_attribute} ) {
518 my @attributes_to_send =
519 ref $server->{account}->{patron_attribute} eq "ARRAY"
520 ? @{ $server->{account}->{patron_attribute} }
521 : ( $server->{account}->{patron_attribute} );
523 foreach my $a ( @attributes_to_send ) {
524 my @attributes = Koha::Patron::Attributes->search(
526 borrowernumber => $self->{borrowernumber},
531 foreach my $attribute ( @attributes ) {
532 my $value = $attribute->attribute();
533 $string .= add_field( $a->{field}, $value );
546 our %patron_example = (
548 name => "David J. Fiander",
551 ptype => 'A', # 'A'dult. Whatever.
552 birthdate => '19640925',
553 address => '2 Meadowvale Dr. St Thomas, ON',
554 home_phone => '(519) 555 1234',
555 email_addr => 'djfiander@hotmail.com',
561 claims_returned => 0,
571 fine_items => ['Computer Time'],
578 From borrowers table:
579 +---------------------+--------------+------+-----+---------+----------------+
580 | Field | Type | Null | Key | Default | Extra |
581 +---------------------+--------------+------+-----+---------+----------------+
582 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
583 | cardnumber | varchar(16) | YES | UNI | NULL | |
584 | surname | mediumtext | NO | | NULL | |
585 | firstname | text | YES | | NULL | |
586 | title | mediumtext | YES | | NULL | |
587 | othernames | mediumtext | YES | | NULL | |
588 | initials | text | YES | | NULL | |
589 | streetnumber | varchar(10) | YES | | NULL | |
590 | streettype | varchar(50) | YES | | NULL | |
591 | address | mediumtext | NO | | NULL | |
592 | address2 | text | YES | | NULL | |
593 | city | mediumtext | NO | | NULL | |
594 | state | mediumtext | YES | | NULL | |
595 | zipcode | varchar(25) | YES | | NULL | |
596 | country | text | YES | | NULL | |
597 | email | mediumtext | YES | | NULL | |
598 | phone | text | YES | | NULL | |
599 | mobile | varchar(50) | YES | | NULL | |
600 | fax | mediumtext | YES | | NULL | |
601 | emailpro | text | YES | | NULL | |
602 | phonepro | text | YES | | NULL | |
603 | B_streetnumber | varchar(10) | YES | | NULL | |
604 | B_streettype | varchar(50) | YES | | NULL | |
605 | B_address | varchar(100) | YES | | NULL | |
606 | B_address2 | text | YES | | NULL | |
607 | B_city | mediumtext | YES | | NULL | |
608 | B_state | mediumtext | YES | | NULL | |
609 | B_zipcode | varchar(25) | YES | | NULL | |
610 | B_country | text | YES | | NULL | |
611 | B_email | text | YES | | NULL | |
612 | B_phone | mediumtext | YES | | NULL | |
613 | dateofbirth | date | YES | | NULL | |
614 | branchcode | varchar(10) | NO | MUL | | |
615 | categorycode | varchar(10) | NO | MUL | | |
616 | dateenrolled | date | YES | | NULL | |
617 | dateexpiry | date | YES | | NULL | |
618 | gonenoaddress | tinyint(1) | YES | | NULL | |
619 | lost | tinyint(1) | YES | | NULL | |
620 | debarred | tinyint(1) | YES | | NULL | |
621 | contactname | mediumtext | YES | | NULL | |
622 | contactfirstname | text | YES | | NULL | |
623 | contacttitle | text | YES | | NULL | |
624 | guarantorid | int(11) | YES | MUL | NULL | |
625 | borrowernotes | mediumtext | YES | | NULL | |
626 | relationship | varchar(100) | YES | | NULL | |
627 | ethnicity | varchar(50) | YES | | NULL | |
628 | ethnotes | varchar(255) | YES | | NULL | |
629 | sex | varchar(1) | YES | | NULL | |
630 | password | varchar(30) | YES | | NULL | |
631 | flags | int(11) | YES | | NULL | |
632 | userid | varchar(30) | YES | MUL | NULL | |
633 | opacnote | mediumtext | YES | | NULL | |
634 | contactnote | varchar(255) | YES | | NULL | |
635 | sort1 | varchar(80) | YES | | NULL | |
636 | sort2 | varchar(80) | YES | | NULL | |
637 | altcontactfirstname | varchar(255) | YES | | NULL | |
638 | altcontactsurname | varchar(255) | YES | | NULL | |
639 | altcontactaddress1 | varchar(255) | YES | | NULL | |
640 | altcontactaddress2 | varchar(255) | YES | | NULL | |
641 | altcontactaddress3 | varchar(255) | YES | | NULL | |
642 | altcontactstate | mediumtext | YES | | NULL | |
643 | altcontactzipcode | varchar(50) | YES | | NULL | |
644 | altcontactcountry | text | YES | | NULL | |
645 | altcontactphone | varchar(50) | YES | | NULL | |
646 | smsalertnumber | varchar(50) | YES | | NULL | |
647 | privacy | int(11) | NO | | 1 | |
648 +---------------------+--------------+------+-----+---------+----------------+
655 {message} Message showing patron's credit or debt
656 {noissues} Set if patron owes >$5.00
657 {GNA} Set if patron gone w/o address
658 {message} "Borrower has no valid address"
660 {LOST} Set if patron's card reported lost
661 {message} Message to this effect
663 {DBARRED} Set if patron is debarred
664 {message} Message to this effect
666 {NOTES} Set if patron has notes
667 {message} Notes about patron
668 {ODUES} Set if patron has overdue books
670 {itemlist} ref-to-array: list of overdue books
671 {itemlisttext} Text list of overdue items
672 {WAITING} Set if there are items available that the patron reserved
673 {message} Message to this effect
674 {itemlist} ref-to-array: list of available items