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::Auth qw(checkpw);
31 our $kp; # koha patron
38 my ($class, $patron_id) = @_;
39 my $type = ref($class) || $class;
41 my $patron = Koha::Patrons->find( { cardnumber => $patron_id } )
42 || Koha::Patrons->find( { userid => $patron_id } );
43 $debug and warn "new Patron: " . Dumper($patron->unblessed) if $patron;
45 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
48 $kp = $patron->unblessed;
49 my $pw = $kp->{password};
50 my $flags = C4::Members::patronflags( $kp );
51 my $debarred = $patron->is_debarred;
52 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')); # Do we need more debug info here?
53 my ($day, $month, $year) = (localtime)[3,4,5];
54 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
55 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
57 if ($kp->{opacnote} ) {
58 $kp->{opacnote} .= q{ };
60 $kp->{opacnote} .= 'PATRON EXPIRED';
63 my $adr = _get_address($kp);
64 my $dob = $kp->{dateofbirth};
65 $dob and $dob =~ s/-//g; # YYYYMMDD
66 my $dexpiry = $kp->{dateexpiry};
67 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
68 my $fines_amount = $patron->account->balance;
69 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
70 my $fee_limit = _fee_limit();
71 my $fine_blocked = $fines_amount > $fee_limit;
72 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
74 no warnings; # any of these $kp->{fields} being concat'd could be undef
76 name => $kp->{firstname} . " " . $kp->{surname},
77 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
79 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
80 dateexpiry => $dexpiry,
81 dateexpiry_iso => $kp->{dateexpiry},
83 birthdate_iso => $kp->{dateofbirth},
84 branchcode => $kp->{branchcode},
85 library_name => "", # only populated if needed, cached here
86 borrowernumber => $kp->{borrowernumber},
88 home_phone => $kp->{phone},
89 email_addr => $kp->{email},
90 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
91 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
92 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
93 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
94 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
96 fines => $fines_amount,
97 fees => 0, # currently not distinct from fines
100 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
103 hold_items => $flags->{WAITING}->{itemlist},
104 overdue_items => $flags->{ODUES}->{itemlist},
108 inet => ( !$debarred && !$expired ),
110 fee_limit => $fee_limit,
111 userid => $kp->{userid},
114 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
116 if ( $patron->is_debarred and $patron->debarredcomment ) {
117 $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
119 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
120 ($flags->{$_}) or next;
121 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
122 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
124 if ($flags->{$_}->{noissues}) {
125 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
126 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
131 # FIXME: populate fine_items recall_items
132 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
133 my $pending_checkouts = $patron->pending_checkouts;
135 while ( my $c = $pending_checkouts->next ) {
136 push @items_infos, $c->unblessed_all_relateds;
138 $ilspatron{items} = \@items_infos;
141 $debug and warn Dumper($self);
142 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
162 charge_ok => 0, # for patron_status[0] (inverted)
163 renew_ok => 0, # for patron_status[1] (inverted)
164 recall_ok => 0, # for patron_status[2] (inverted)
165 hold_ok => 0, # for patron_status[3] (inverted)
166 card_lost => 0, # for patron_status[4]
172 too_many_charged => 0, # for patron_status[5]
173 too_many_overdue => 0, # for patron_status[6]
174 too_many_renewal => 0, # for patron_status[7]
175 too_many_claim_return => 0, # for patron_status[8]
176 too_many_lost => 0, # for patron_status[9]
177 # excessive_fines => 0, # for patron_status[10]
178 # excessive_fees => 0, # for patron_status[11]
179 recall_overdue => 0, # for patron_status[12]
180 too_many_billed => 0, # for patron_status[13]
181 inet => 0, # EnvisionWare extension
187 # be cool. needed for AUTOLOAD(?)
192 my $class = ref($self) or croak "$self is not an object";
193 my $name = $AUTOLOAD;
197 unless (exists $fields{$name}) {
198 croak "Cannot access '$name' field of class '$class'";
202 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
203 return $self->{$name} = shift;
205 return $self->{$name};
210 my ( $self, $template ) = @_;
214 require Koha::Patrons;
216 my $tt = Template->new();
218 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
221 $tt->process( \$template, { patron => $patron }, \$output );
225 return $self->{name};
230 my ( $self, $pwd ) = @_;
232 # you gotta give me something (at least ''), or no deal
233 return 0 unless defined $pwd;
235 # If the record has a NULL password, accept '' as match
236 return $pwd eq q{} unless $self->{password};
238 my $dbh = C4::Context->dbh;
240 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
244 # A few special cases, not in AUTOLOADed %fields
247 if ( $self->{fines} ) {
248 return $self->{fines};
255 return $self->fee_amount;
260 return $self->{language} || '000'; # Unspecified
265 return $self->{expired};
269 # remove the hold on item item_id from my hold queue.
270 # return true if I was holding the item, false otherwise.
273 my ($self, $item_id) = @_;
276 foreach (qw(hold_items unavail_holds)) {
278 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
279 my $held_item = $self->{$_}[$i]->{item_id} or next;
280 if ($held_item eq $item_id) {
281 splice @{$self->{$_}}, $i, 1;
289 # Accessor method for array_ref values, designed to get the "start" and "end" values
290 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
294 my $array_var = shift or return;
295 my ($start, $end) = @_;
298 if ($self->{$array_var}) {
299 if ($start && $start > 1) {
305 if ( $end && $end < @{$self->{$array_var}} ) {
308 $end = @{$self->{$array_var}};
311 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
318 # List of outstanding holds placed
322 my $item_arr = $self->x_items('hold_items', @_);
323 foreach my $item (@{$item_arr}) {
324 my $item_obj = Koha::Items->find($item->{itemnumber});
325 $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
332 return $self->x_items('overdue_items', @_);
336 return $self->x_items('items', @_);
339 require Koha::Database;
347 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
349 borrowernumber => $self->{borrowernumber},
350 amountoutstanding => { '>' => '0' },
354 $start = $start ? $start - 1 : 0;
355 $end = $end ? $end : scalar @fees - 1;
357 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
358 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
360 my $tt = Template->new();
363 for ( my $i = $start; $i <= $end; $i++ ) {
367 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
368 push( @return_values, { barcode => $output } );
371 return \@return_values;
375 return $self->x_items('recall_items', @_);
379 return $self->x_items('unavail_holds', @_);
383 my ($self, $card_retained, $blocked_card_msg) = @_;
384 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
387 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
388 # TODO: not really affecting patron record
394 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
397 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
398 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
399 $self->{recall_ok}, $self->{hold_ok});
400 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
404 sub inet_privileges {
406 return $self->{inet} ? 'Y' : 'N';
410 return C4::Context->preference('noissuescharge') || 5;
415 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
418 sub excessive_fines {
420 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
423 sub holds_blocked_by_excessive_fees {
425 return ( $self->fee_amount
426 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
431 unless ($self->{library_name}) {
432 my $library = Koha::Libraries->find( $self->{branchcode} );
433 $self->{library_name} = $library ? $library->branchname : '';
435 return $self->{library_name};
443 return "Please contact library staff";
448 return "Please contact library staff";
451 =head2 update_lastseen
453 $patron->update_lastseen();
455 Patron method to update lastseen field in borrower
456 to record that patron has been seen via sip connection
460 sub update_lastseen {
462 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
463 $kohaobj->track_login if $kohaobj; # track_login checks the pref
469 my $address = $patron->{streetnumber} || q{};
470 for my $field (qw( roaddetails address address2 city state zipcode country))
472 next unless $patron->{$field};
475 $address .= $patron->{$field};
478 $address .= $patron->{$field};
484 sub _get_outstanding_holds {
485 my $borrowernumber = shift;
487 my $patron = Koha::Patrons->find( $borrowernumber );
488 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
490 while ( my $hold = $holds->next ) {
492 if ($hold->itemnumber) {
496 # We need to return a barcode for the biblio so the client
497 # can request the biblio info
498 my $items = $hold->biblio->items;
499 $item = $items->count ? $items->next : undef;
501 my $unblessed_hold = $hold->unblessed;
503 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
505 push @holds, $unblessed_hold;
510 =head2 build_patron_attributes_string
512 This method builds the part of the sip message for extended patron
513 attributes as defined in the sip config
517 sub build_patron_attributes_string {
518 my ( $self, $server ) = @_;
522 if ( $server->{account}->{patron_attribute} ) {
523 my @attributes_to_send =
524 ref $server->{account}->{patron_attribute} eq "ARRAY"
525 ? @{ $server->{account}->{patron_attribute} }
526 : ( $server->{account}->{patron_attribute} );
528 foreach my $a ( @attributes_to_send ) {
529 my @attributes = Koha::Patron::Attributes->search(
531 borrowernumber => $self->{borrowernumber},
536 foreach my $attribute ( @attributes ) {
537 my $value = $attribute->attribute();
538 $string .= add_field( $a->{field}, $value );
551 our %patron_example = (
553 name => "David J. Fiander",
556 ptype => 'A', # 'A'dult. Whatever.
557 birthdate => '19640925',
558 address => '2 Meadowvale Dr. St Thomas, ON',
559 home_phone => '(519) 555 1234',
560 email_addr => 'djfiander@hotmail.com',
566 claims_returned => 0,
576 fine_items => ['Computer Time'],
583 From borrowers table:
584 +---------------------+--------------+------+-----+---------+----------------+
585 | Field | Type | Null | Key | Default | Extra |
586 +---------------------+--------------+------+-----+---------+----------------+
587 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
588 | cardnumber | varchar(16) | YES | UNI | NULL | |
589 | surname | mediumtext | NO | | NULL | |
590 | firstname | text | YES | | NULL | |
591 | title | mediumtext | YES | | NULL | |
592 | othernames | mediumtext | YES | | NULL | |
593 | initials | text | YES | | NULL | |
594 | streetnumber | varchar(10) | YES | | NULL | |
595 | streettype | varchar(50) | YES | | NULL | |
596 | address | mediumtext | NO | | NULL | |
597 | address2 | text | YES | | NULL | |
598 | city | mediumtext | NO | | NULL | |
599 | state | mediumtext | YES | | NULL | |
600 | zipcode | varchar(25) | YES | | NULL | |
601 | country | text | YES | | NULL | |
602 | email | mediumtext | YES | | NULL | |
603 | phone | text | YES | | NULL | |
604 | mobile | varchar(50) | YES | | NULL | |
605 | fax | mediumtext | YES | | NULL | |
606 | emailpro | text | YES | | NULL | |
607 | phonepro | text | YES | | NULL | |
608 | B_streetnumber | varchar(10) | YES | | NULL | |
609 | B_streettype | varchar(50) | YES | | NULL | |
610 | B_address | varchar(100) | YES | | NULL | |
611 | B_address2 | text | YES | | NULL | |
612 | B_city | mediumtext | YES | | NULL | |
613 | B_state | mediumtext | YES | | NULL | |
614 | B_zipcode | varchar(25) | YES | | NULL | |
615 | B_country | text | YES | | NULL | |
616 | B_email | text | YES | | NULL | |
617 | B_phone | mediumtext | YES | | NULL | |
618 | dateofbirth | date | YES | | NULL | |
619 | branchcode | varchar(10) | NO | MUL | | |
620 | categorycode | varchar(10) | NO | MUL | | |
621 | dateenrolled | date | YES | | NULL | |
622 | dateexpiry | date | YES | | NULL | |
623 | gonenoaddress | tinyint(1) | YES | | NULL | |
624 | lost | tinyint(1) | YES | | NULL | |
625 | debarred | tinyint(1) | YES | | NULL | |
626 | contactname | mediumtext | YES | | NULL | |
627 | contactfirstname | text | YES | | NULL | |
628 | contacttitle | text | YES | | NULL | |
629 | guarantorid | int(11) | YES | MUL | NULL | |
630 | borrowernotes | mediumtext | YES | | NULL | |
631 | relationship | varchar(100) | YES | | NULL | |
632 | ethnicity | varchar(50) | YES | | NULL | |
633 | ethnotes | varchar(255) | YES | | NULL | |
634 | sex | varchar(1) | YES | | NULL | |
635 | password | varchar(30) | YES | | NULL | |
636 | flags | int(11) | YES | | NULL | |
637 | userid | varchar(30) | YES | MUL | NULL | |
638 | opacnote | mediumtext | YES | | NULL | |
639 | contactnote | varchar(255) | YES | | NULL | |
640 | sort1 | varchar(80) | YES | | NULL | |
641 | sort2 | varchar(80) | YES | | NULL | |
642 | altcontactfirstname | varchar(255) | YES | | NULL | |
643 | altcontactsurname | varchar(255) | YES | | NULL | |
644 | altcontactaddress1 | varchar(255) | YES | | NULL | |
645 | altcontactaddress2 | varchar(255) | YES | | NULL | |
646 | altcontactaddress3 | varchar(255) | YES | | NULL | |
647 | altcontactstate | mediumtext | YES | | NULL | |
648 | altcontactzipcode | varchar(50) | YES | | NULL | |
649 | altcontactcountry | text | YES | | NULL | |
650 | altcontactphone | varchar(50) | YES | | NULL | |
651 | smsalertnumber | varchar(50) | YES | | NULL | |
652 | privacy | int(11) | NO | | 1 | |
653 +---------------------+--------------+------+-----+---------+----------------+
660 {message} Message showing patron's credit or debt
661 {noissues} Set if patron owes >$5.00
662 {GNA} Set if patron gone w/o address
663 {message} "Borrower has no valid address"
665 {LOST} Set if patron's card reported lost
666 {message} Message to this effect
668 {DBARRED} Set if patron is debarred
669 {message} Message to this effect
671 {NOTES} Set if patron has notes
672 {message} Notes about patron
673 {ODUES} Set if patron has overdue books
675 {itemlist} ref-to-array: list of overdue books
676 {itemlisttext} Text list of overdue items
677 {WAITING} Set if there are items available that the patron reserved
678 {message} Message to this effect
679 {itemlist} ref-to-array: list of available items