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( GetBarcodeFromItemnumber GetItemnumbersForBiblio);
26 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 $ilspatron{items} = $patron->pending_checkouts->unblessed;
135 $debug and warn Dumper($self);
136 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
156 charge_ok => 0, # for patron_status[0] (inverted)
157 renew_ok => 0, # for patron_status[1] (inverted)
158 recall_ok => 0, # for patron_status[2] (inverted)
159 hold_ok => 0, # for patron_status[3] (inverted)
160 card_lost => 0, # for patron_status[4]
166 too_many_charged => 0, # for patron_status[5]
167 too_many_overdue => 0, # for patron_status[6]
168 too_many_renewal => 0, # for patron_status[7]
169 too_many_claim_return => 0, # for patron_status[8]
170 too_many_lost => 0, # for patron_status[9]
171 # excessive_fines => 0, # for patron_status[10]
172 # excessive_fees => 0, # for patron_status[11]
173 recall_overdue => 0, # for patron_status[12]
174 too_many_billed => 0, # for patron_status[13]
175 inet => 0, # EnvisionWare extension
181 # be cool. needed for AUTOLOAD(?)
186 my $class = ref($self) or croak "$self is not an object";
187 my $name = $AUTOLOAD;
191 unless (exists $fields{$name}) {
192 croak "Cannot access '$name' field of class '$class'";
196 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
197 return $self->{$name} = shift;
199 return $self->{$name};
204 my ( $self, $template ) = @_;
208 require Koha::Patrons;
210 my $tt = Template->new();
212 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
215 $tt->process( \$template, { patron => $patron }, \$output );
219 return $self->{name};
224 my ( $self, $pwd ) = @_;
226 # you gotta give me something (at least ''), or no deal
227 return 0 unless defined $pwd;
229 # If the record has a NULL password, accept '' as match
230 return $pwd eq q{} unless $self->{password};
232 my $dbh = C4::Context->dbh;
234 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
238 # A few special cases, not in AUTOLOADed %fields
241 if ( $self->{fines} ) {
242 return $self->{fines};
249 return $self->fee_amount;
254 return $self->{language} || '000'; # Unspecified
259 return $self->{expired};
263 # remove the hold on item item_id from my hold queue.
264 # return true if I was holding the item, false otherwise.
267 my ($self, $item_id) = @_;
270 foreach (qw(hold_items unavail_holds)) {
272 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
273 my $held_item = $self->{$_}[$i]->{item_id} or next;
274 if ($held_item eq $item_id) {
275 splice @{$self->{$_}}, $i, 1;
283 # Accessor method for array_ref values, designed to get the "start" and "end" values
284 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
288 my $array_var = shift or return;
289 my ($start, $end) = @_;
292 if ($self->{$array_var}) {
293 if ($start && $start > 1) {
299 if ( $end && $end < @{$self->{$array_var}} ) {
302 $end = @{$self->{$array_var}};
305 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
312 # List of outstanding holds placed
316 my $item_arr = $self->x_items('hold_items', @_);
317 foreach my $item (@{$item_arr}) {
318 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
325 return $self->x_items('overdue_items', @_);
329 return $self->x_items('items', @_);
332 require Koha::Database;
340 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
342 borrowernumber => $self->{borrowernumber},
343 amountoutstanding => { '>' => '0' },
347 $start = $start ? $start - 1 : 0;
348 $end = $end ? $end : scalar @fees - 1;
350 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
351 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
353 my $tt = Template->new();
356 for ( my $i = $start; $i <= $end; $i++ ) {
360 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
361 push( @return_values, { barcode => $output } );
364 return \@return_values;
368 return $self->x_items('recall_items', @_);
372 return $self->x_items('unavail_holds', @_);
376 my ($self, $card_retained, $blocked_card_msg) = @_;
377 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
380 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
381 # TODO: not really affecting patron record
387 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
390 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
391 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
392 $self->{recall_ok}, $self->{hold_ok});
393 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
397 sub inet_privileges {
399 return $self->{inet} ? 'Y' : 'N';
403 return C4::Context->preference('noissuescharge') || 5;
408 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
411 sub excessive_fines {
413 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
416 sub holds_blocked_by_excessive_fees {
418 return ( $self->fee_amount
419 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
424 unless ($self->{library_name}) {
425 my $library = Koha::Libraries->find( $self->{branchcode} );
426 $self->{library_name} = $library ? $library->branchname : '';
428 return $self->{library_name};
436 return "Please contact library staff";
441 return "Please contact library staff";
444 =head2 update_lastseen
446 $patron->update_lastseen();
448 Patron method to update lastseen field in borrower
449 to record that patron has been seen via sip connection
453 sub update_lastseen {
455 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
456 $kohaobj->track_login if $kohaobj; # track_login checks the pref
462 my $address = $patron->{streetnumber} || q{};
463 for my $field (qw( roaddetails address address2 city state zipcode country))
465 next unless $patron->{$field};
468 $address .= $patron->{$field};
471 $address .= $patron->{$field};
477 sub _get_outstanding_holds {
478 my $borrowernumber = shift;
480 my $patron = Koha::Patrons->find( $borrowernumber );
481 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
483 while ( my $hold = $holds->next ) {
485 if ($hold->itemnumber) {
486 $item = $hold->itemnumber;
489 # We need to return a barcode for the biblio so the client
490 # can request the biblio info
491 $item = ( GetItemnumbersForBiblio($hold->biblionumber) )->[0];
493 my $unblessed_hold = $hold->unblessed;
494 $unblessed_hold->{barcode} = GetBarcodeFromItemnumber($item);
495 push @holds, $unblessed_hold;
500 =head2 build_patron_attributes_string
502 This method builds the part of the sip message for extended patron
503 attributes as defined in the sip config
507 sub build_patron_attributes_string {
508 my ( $self, $server ) = @_;
512 if ( $server->{account}->{patron_attribute} ) {
513 my @attributes_to_send =
514 ref $server->{account}->{patron_attribute} eq "ARRAY"
515 ? @{ $server->{account}->{patron_attribute} }
516 : ( $server->{account}->{patron_attribute} );
518 foreach my $a ( @attributes_to_send ) {
519 my @attributes = Koha::Patron::Attributes->search(
521 borrowernumber => $self->{borrowernumber},
526 foreach my $attribute ( @attributes ) {
527 my $value = $attribute->attribute();
528 $string .= add_field( $a->{field}, $value );
541 our %patron_example = (
543 name => "David J. Fiander",
546 ptype => 'A', # 'A'dult. Whatever.
547 birthdate => '19640925',
548 address => '2 Meadowvale Dr. St Thomas, ON',
549 home_phone => '(519) 555 1234',
550 email_addr => 'djfiander@hotmail.com',
556 claims_returned => 0,
566 fine_items => ['Computer Time'],
573 From borrowers table:
574 +---------------------+--------------+------+-----+---------+----------------+
575 | Field | Type | Null | Key | Default | Extra |
576 +---------------------+--------------+------+-----+---------+----------------+
577 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
578 | cardnumber | varchar(16) | YES | UNI | NULL | |
579 | surname | mediumtext | NO | | NULL | |
580 | firstname | text | YES | | NULL | |
581 | title | mediumtext | YES | | NULL | |
582 | othernames | mediumtext | YES | | NULL | |
583 | initials | text | YES | | NULL | |
584 | streetnumber | varchar(10) | YES | | NULL | |
585 | streettype | varchar(50) | YES | | NULL | |
586 | address | mediumtext | NO | | NULL | |
587 | address2 | text | YES | | NULL | |
588 | city | mediumtext | NO | | NULL | |
589 | state | mediumtext | YES | | NULL | |
590 | zipcode | varchar(25) | YES | | NULL | |
591 | country | text | YES | | NULL | |
592 | email | mediumtext | YES | | NULL | |
593 | phone | text | YES | | NULL | |
594 | mobile | varchar(50) | YES | | NULL | |
595 | fax | mediumtext | YES | | NULL | |
596 | emailpro | text | YES | | NULL | |
597 | phonepro | text | YES | | NULL | |
598 | B_streetnumber | varchar(10) | YES | | NULL | |
599 | B_streettype | varchar(50) | YES | | NULL | |
600 | B_address | varchar(100) | YES | | NULL | |
601 | B_address2 | text | YES | | NULL | |
602 | B_city | mediumtext | YES | | NULL | |
603 | B_state | mediumtext | YES | | NULL | |
604 | B_zipcode | varchar(25) | YES | | NULL | |
605 | B_country | text | YES | | NULL | |
606 | B_email | text | YES | | NULL | |
607 | B_phone | mediumtext | YES | | NULL | |
608 | dateofbirth | date | YES | | NULL | |
609 | branchcode | varchar(10) | NO | MUL | | |
610 | categorycode | varchar(10) | NO | MUL | | |
611 | dateenrolled | date | YES | | NULL | |
612 | dateexpiry | date | YES | | NULL | |
613 | gonenoaddress | tinyint(1) | YES | | NULL | |
614 | lost | tinyint(1) | YES | | NULL | |
615 | debarred | tinyint(1) | YES | | NULL | |
616 | contactname | mediumtext | YES | | NULL | |
617 | contactfirstname | text | YES | | NULL | |
618 | contacttitle | text | YES | | NULL | |
619 | guarantorid | int(11) | YES | MUL | NULL | |
620 | borrowernotes | mediumtext | YES | | NULL | |
621 | relationship | varchar(100) | YES | | NULL | |
622 | ethnicity | varchar(50) | YES | | NULL | |
623 | ethnotes | varchar(255) | YES | | NULL | |
624 | sex | varchar(1) | YES | | NULL | |
625 | password | varchar(30) | YES | | NULL | |
626 | flags | int(11) | YES | | NULL | |
627 | userid | varchar(30) | YES | MUL | NULL | |
628 | opacnote | mediumtext | YES | | NULL | |
629 | contactnote | varchar(255) | YES | | NULL | |
630 | sort1 | varchar(80) | YES | | NULL | |
631 | sort2 | varchar(80) | YES | | NULL | |
632 | altcontactfirstname | varchar(255) | YES | | NULL | |
633 | altcontactsurname | varchar(255) | YES | | NULL | |
634 | altcontactaddress1 | varchar(255) | YES | | NULL | |
635 | altcontactaddress2 | varchar(255) | YES | | NULL | |
636 | altcontactaddress3 | varchar(255) | YES | | NULL | |
637 | altcontactstate | mediumtext | YES | | NULL | |
638 | altcontactzipcode | varchar(50) | YES | | NULL | |
639 | altcontactcountry | text | YES | | NULL | |
640 | altcontactphone | varchar(50) | YES | | NULL | |
641 | smsalertnumber | varchar(50) | YES | | NULL | |
642 | privacy | int(11) | NO | | 1 | |
643 +---------------------+--------------+------+-----+---------+----------------+
650 {message} Message showing patron's credit or debt
651 {noissues} Set if patron owes >$5.00
652 {GNA} Set if patron gone w/o address
653 {message} "Borrower has no valid address"
655 {LOST} Set if patron's card reported lost
656 {message} Message to this effect
658 {DBARRED} Set if patron is debarred
659 {message} Message to this effect
661 {NOTES} Set if patron has notes
662 {message} Notes about patron
663 {ODUES} Set if patron has overdue books
665 {itemlist} ref-to-array: list of overdue books
666 {itemlisttext} Text list of overdue items
667 {WAITING} Set if there are items available that the patron reserved
668 {message} Message to this effect
669 {itemlist} ref-to-array: list of available items