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});
134 my $pending_checkouts = $patron->pending_checkouts;
136 while ( my $c = $pending_checkouts->next ) {
137 push @barcodes, { barcode => $c->item->barcode };
139 $ilspatron{items} = \@barcodes;
142 $debug and warn Dumper($self);
143 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
163 charge_ok => 0, # for patron_status[0] (inverted)
164 renew_ok => 0, # for patron_status[1] (inverted)
165 recall_ok => 0, # for patron_status[2] (inverted)
166 hold_ok => 0, # for patron_status[3] (inverted)
167 card_lost => 0, # for patron_status[4]
173 too_many_charged => 0, # for patron_status[5]
174 too_many_overdue => 0, # for patron_status[6]
175 too_many_renewal => 0, # for patron_status[7]
176 too_many_claim_return => 0, # for patron_status[8]
177 too_many_lost => 0, # for patron_status[9]
178 # excessive_fines => 0, # for patron_status[10]
179 # excessive_fees => 0, # for patron_status[11]
180 recall_overdue => 0, # for patron_status[12]
181 too_many_billed => 0, # for patron_status[13]
182 inet => 0, # EnvisionWare extension
188 # be cool. needed for AUTOLOAD(?)
193 my $class = ref($self) or croak "$self is not an object";
194 my $name = $AUTOLOAD;
198 unless (exists $fields{$name}) {
199 croak "Cannot access '$name' field of class '$class'";
203 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
204 return $self->{$name} = shift;
206 return $self->{$name};
211 my ( $self, $template ) = @_;
215 require Koha::Patrons;
217 my $tt = Template->new();
219 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
222 $tt->process( \$template, { patron => $patron }, \$output );
226 return $self->{name};
231 my ( $self, $pwd ) = @_;
233 # you gotta give me something (at least ''), or no deal
234 return 0 unless defined $pwd;
236 # If the record has a NULL password, accept '' as match
237 return $pwd eq q{} unless $self->{password};
239 my $dbh = C4::Context->dbh;
241 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
245 # A few special cases, not in AUTOLOADed %fields
248 if ( $self->{fines} ) {
249 return $self->{fines};
256 return $self->fee_amount;
261 return $self->{language} || '000'; # Unspecified
266 return $self->{expired};
270 # remove the hold on item item_id from my hold queue.
271 # return true if I was holding the item, false otherwise.
274 my ($self, $item_id) = @_;
277 foreach (qw(hold_items unavail_holds)) {
279 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
280 my $held_item = $self->{$_}[$i]->{item_id} or next;
281 if ($held_item eq $item_id) {
282 splice @{$self->{$_}}, $i, 1;
290 # Accessor method for array_ref values, designed to get the "start" and "end" values
291 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
295 my $array_var = shift or return;
296 my ($start, $end) = @_;
299 if ($self->{$array_var}) {
300 if ($start && $start > 1) {
306 if ( $end && $end < @{$self->{$array_var}} ) {
309 $end = @{$self->{$array_var}};
312 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
319 # List of outstanding holds placed
323 my $item_arr = $self->x_items('hold_items', @_);
324 foreach my $item (@{$item_arr}) {
325 my $item_obj = Koha::Items->find($item->{itemnumber});
326 $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
333 return $self->x_items('overdue_items', @_);
337 return $self->x_items('items', @_);
340 require Koha::Database;
348 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
350 borrowernumber => $self->{borrowernumber},
351 amountoutstanding => { '>' => '0' },
355 $start = $start ? $start - 1 : 0;
356 $end = $end ? $end : scalar @fees - 1;
358 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
359 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
361 my $tt = Template->new();
364 for ( my $i = $start; $i <= $end; $i++ ) {
368 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
369 push( @return_values, { barcode => $output } );
372 return \@return_values;
376 return $self->x_items('recall_items', @_);
380 return $self->x_items('unavail_holds', @_);
384 my ($self, $card_retained, $blocked_card_msg) = @_;
385 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
388 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
389 # TODO: not really affecting patron record
395 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
398 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
399 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
400 $self->{recall_ok}, $self->{hold_ok});
401 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
405 sub inet_privileges {
407 return $self->{inet} ? 'Y' : 'N';
411 return C4::Context->preference('noissuescharge') || 5;
416 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
419 sub excessive_fines {
421 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
424 sub holds_blocked_by_excessive_fees {
426 return ( $self->fee_amount
427 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
432 unless ($self->{library_name}) {
433 my $library = Koha::Libraries->find( $self->{branchcode} );
434 $self->{library_name} = $library ? $library->branchname : '';
436 return $self->{library_name};
444 return "Please contact library staff";
449 return "Please contact library staff";
452 =head2 update_lastseen
454 $patron->update_lastseen();
456 Patron method to update lastseen field in borrower
457 to record that patron has been seen via sip connection
461 sub update_lastseen {
463 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
464 $kohaobj->track_login if $kohaobj; # track_login checks the pref
470 my $address = $patron->{streetnumber} || q{};
471 for my $field (qw( roaddetails address address2 city state zipcode country))
473 next unless $patron->{$field};
476 $address .= $patron->{$field};
479 $address .= $patron->{$field};
485 sub _get_outstanding_holds {
486 my $borrowernumber = shift;
488 my $patron = Koha::Patrons->find( $borrowernumber );
489 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
491 while ( my $hold = $holds->next ) {
493 if ($hold->itemnumber) {
497 # We need to return a barcode for the biblio so the client
498 # can request the biblio info
499 my $items = $hold->biblio->items;
500 $item = $items->count ? $items->next : undef;
502 my $unblessed_hold = $hold->unblessed;
504 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
506 push @holds, $unblessed_hold;
511 =head2 build_patron_attributes_string
513 This method builds the part of the sip message for extended patron
514 attributes as defined in the sip config
518 sub build_patron_attributes_string {
519 my ( $self, $server ) = @_;
523 if ( $server->{account}->{patron_attribute} ) {
524 my @attributes_to_send =
525 ref $server->{account}->{patron_attribute} eq "ARRAY"
526 ? @{ $server->{account}->{patron_attribute} }
527 : ( $server->{account}->{patron_attribute} );
529 foreach my $a ( @attributes_to_send ) {
530 my @attributes = Koha::Patron::Attributes->search(
532 borrowernumber => $self->{borrowernumber},
537 foreach my $attribute ( @attributes ) {
538 my $value = $attribute->attribute();
539 $string .= add_field( $a->{field}, $value );
552 our %patron_example = (
554 name => "David J. Fiander",
557 ptype => 'A', # 'A'dult. Whatever.
558 birthdate => '19640925',
559 address => '2 Meadowvale Dr. St Thomas, ON',
560 home_phone => '(519) 555 1234',
561 email_addr => 'djfiander@hotmail.com',
567 claims_returned => 0,
577 fine_items => ['Computer Time'],
584 From borrowers table:
585 +---------------------+--------------+------+-----+---------+----------------+
586 | Field | Type | Null | Key | Default | Extra |
587 +---------------------+--------------+------+-----+---------+----------------+
588 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
589 | cardnumber | varchar(16) | YES | UNI | NULL | |
590 | surname | mediumtext | NO | | NULL | |
591 | firstname | text | YES | | NULL | |
592 | title | mediumtext | YES | | NULL | |
593 | othernames | mediumtext | YES | | NULL | |
594 | initials | text | YES | | NULL | |
595 | streetnumber | varchar(10) | YES | | NULL | |
596 | streettype | varchar(50) | YES | | NULL | |
597 | address | mediumtext | NO | | NULL | |
598 | address2 | text | YES | | NULL | |
599 | city | mediumtext | NO | | NULL | |
600 | state | mediumtext | YES | | NULL | |
601 | zipcode | varchar(25) | YES | | NULL | |
602 | country | text | YES | | NULL | |
603 | email | mediumtext | YES | | NULL | |
604 | phone | text | YES | | NULL | |
605 | mobile | varchar(50) | YES | | NULL | |
606 | fax | mediumtext | YES | | NULL | |
607 | emailpro | text | YES | | NULL | |
608 | phonepro | text | YES | | NULL | |
609 | B_streetnumber | varchar(10) | YES | | NULL | |
610 | B_streettype | varchar(50) | YES | | NULL | |
611 | B_address | varchar(100) | YES | | NULL | |
612 | B_address2 | text | YES | | NULL | |
613 | B_city | mediumtext | YES | | NULL | |
614 | B_state | mediumtext | YES | | NULL | |
615 | B_zipcode | varchar(25) | YES | | NULL | |
616 | B_country | text | YES | | NULL | |
617 | B_email | text | YES | | NULL | |
618 | B_phone | mediumtext | YES | | NULL | |
619 | dateofbirth | date | YES | | NULL | |
620 | branchcode | varchar(10) | NO | MUL | | |
621 | categorycode | varchar(10) | NO | MUL | | |
622 | dateenrolled | date | YES | | NULL | |
623 | dateexpiry | date | YES | | NULL | |
624 | gonenoaddress | tinyint(1) | YES | | NULL | |
625 | lost | tinyint(1) | YES | | NULL | |
626 | debarred | tinyint(1) | YES | | NULL | |
627 | contactname | mediumtext | YES | | NULL | |
628 | contactfirstname | text | YES | | NULL | |
629 | contacttitle | text | YES | | 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