4 # A Class for hiding the ILS's concept of the patron from the OpenSIP
8 package C4::SIP::ILS::Patron;
15 use C4::SIP::Sip qw(siplog);
18 use C4::SIP::Sip qw(add_field maybe_add);
25 use C4::Auth qw(checkpw);
31 our $kp; # koha patron
38 my ($class, $patron_id) = @_;
39 my $type = ref($class) || $class;
43 if ( ref $patron_id eq "HASH" ) {
44 if ( $patron_id->{borrowernumber} ) {
45 $patron = Koha::Patrons->find( $patron_id->{borrowernumber} );
46 } elsif ( $patron_id->{cardnumber} ) {
47 $patron = Koha::Patrons->find( { cardnumber => $patron_id->{cardnumber} } );
48 } elsif ( $patron_id->{userid} ) {
49 $patron = Koha::Patrons->find( { userid => $patron_id->{userid} } );
52 $patron = Koha::Patrons->find( { cardnumber => $patron_id } )
53 || Koha::Patrons->find( { userid => $patron_id } );
56 $debug and warn "new Patron: " . Dumper($patron->unblessed) if $patron;
58 siplog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
61 $kp = $patron->unblessed;
62 my $pw = $kp->{password};
63 my $flags = C4::Members::patronflags( $kp );
64 my $debarred = $patron->is_debarred;
65 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')); # Do we need more debug info here?
66 my ($day, $month, $year) = (localtime)[3,4,5];
67 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
68 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
70 if ($kp->{opacnote} ) {
71 $kp->{opacnote} .= q{ };
73 $kp->{opacnote} .= 'PATRON EXPIRED';
76 my $adr = _get_address($kp);
77 my $dob = $kp->{dateofbirth};
78 $dob and $dob =~ s/-//g; # YYYYMMDD
79 my $dexpiry = $kp->{dateexpiry};
80 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
82 # Get fines and add fines for guarantees (depends on preference NoIssuesChargeGuarantees)
83 my $fines_amount = $flags->{CHARGES}->{amount}; #TODO Replace with $patron->account->non_issues_charges
84 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
85 if ( C4::Context->preference('NoIssuesChargeGuarantorsWithGuarantees') ) {
86 $fines_amount += $patron->relationships_debt({ include_guarantors => 1, only_this_guarantor => 0, include_this_patron => 1 });
88 my $guarantees_fines_amount = $flags->{CHARGES_GUARANTEES} ? $flags->{CHARGES_GUARANTEES}->{amount} : 0; #TODO: Replace with $patron->relationships_debt
89 $fines_amount += $guarantees_fines_amount;
92 my $fee_limit = _fee_limit();
93 my $fine_blocked = $fines_amount > $fee_limit;
94 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
96 no warnings; # any of these $kp->{fields} being concat'd could be undef
98 name => $kp->{firstname} . " " . $kp->{surname},
99 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
101 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
102 dateexpiry => $dexpiry,
103 dateexpiry_iso => $kp->{dateexpiry},
105 birthdate_iso => $kp->{dateofbirth},
106 branchcode => $kp->{branchcode},
107 library_name => "", # only populated if needed, cached here
108 borrowernumber => $kp->{borrowernumber},
110 home_phone => $kp->{phone},
111 email_addr => $kp->{email},
112 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
113 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
114 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
115 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
116 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
117 claims_returned => 0,
118 fines => $fines_amount,
119 fees => 0, # currently not distinct from fines
122 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
125 hold_items => $flags->{WAITING}->{itemlist},
126 overdue_items => $flags->{ODUES}->{itemlist},
127 too_many_overdue => $circ_blocked,
131 inet => ( !$debarred && !$expired ),
133 fee_limit => $fee_limit,
134 userid => $kp->{userid},
137 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
139 if ( $patron->is_debarred and $patron->debarredcomment ) {
140 $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
142 if ( $circ_blocked ) {
143 $ilspatron{screen_msg} .= " -- " . "Patron has overdues";
145 for (qw(EXPIRED CHARGES CREDITS GNA LOST NOTES)) {
146 ($flags->{$_}) or next;
147 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
148 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
150 if ($flags->{$_}->{noissues}) {
151 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
152 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
157 # FIXME: populate fine_items recall_items
158 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
160 my $pending_checkouts = $patron->pending_checkouts;
162 while ( my $c = $pending_checkouts->next ) {
163 push @barcodes, { barcode => $c->item->barcode };
165 $ilspatron{items} = \@barcodes;
168 $debug and warn Dumper($self);
169 siplog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
190 charge_ok => 0, # for patron_status[0] (inverted)
191 renew_ok => 0, # for patron_status[1] (inverted)
192 recall_ok => 0, # for patron_status[2] (inverted)
193 hold_ok => 0, # for patron_status[3] (inverted)
194 card_lost => 0, # for patron_status[4]
200 too_many_charged => 0, # for patron_status[5]
201 too_many_overdue => 0, # for patron_status[6]
202 too_many_renewal => 0, # for patron_status[7]
203 too_many_claim_return => 0, # for patron_status[8]
204 too_many_lost => 0, # for patron_status[9]
205 # excessive_fines => 0, # for patron_status[10]
206 # excessive_fees => 0, # for patron_status[11]
207 recall_overdue => 0, # for patron_status[12]
208 too_many_billed => 0, # for patron_status[13]
209 inet => 0, # EnvisionWare extension
215 # be cool. needed for AUTOLOAD(?)
220 my $class = ref($self) or croak "$self is not an object";
221 my $name = $AUTOLOAD;
225 unless (exists $fields{$name}) {
226 croak "Cannot access '$name' field of class '$class'";
230 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
231 return $self->{$name} = shift;
233 return $self->{$name};
239 This method uses a template to build a string from a Koha::Patron object
240 If errors are encountered in processing template we log them and return nothing
245 my ( $self, $template ) = @_;
249 require Koha::Patrons;
251 my $tt = Template->new();
253 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
257 $tt->process( \$template, { patron => $patron }, \$output );
260 siplog("LOG_DEBUG", "Error processing template: $template");
268 my ( $self, $pwd ) = @_;
270 # you gotta give me something (at least ''), or no deal
271 return 0 unless defined $pwd;
273 # If the record has a NULL password, accept '' as match
274 return $pwd eq q{} unless $self->{password};
276 my $dbh = C4::Context->dbh;
278 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
282 # A few special cases, not in AUTOLOADed %fields
285 if ( $self->{fines} ) {
286 return $self->{fines};
293 return $self->fee_amount;
298 return $self->{language} || '000'; # Unspecified
303 return $self->{expired};
307 # remove the hold on item item_id from my hold queue.
308 # return true if I was holding the item, false otherwise.
311 my ($self, $item_id) = @_;
314 foreach (qw(hold_items unavail_holds)) {
316 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
317 my $held_item = $self->{$_}[$i]->{barcode} or next;
318 if ($held_item eq $item_id) {
319 splice @{$self->{$_}}, $i, 1;
327 # Accessor method for array_ref values, designed to get the "start" and "end" values
328 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
332 my $array_var = shift or return;
333 my ($start, $end) = @_;
336 if ($self->{$array_var}) {
337 if ($start && $start > 1) {
343 if ( $end && $end < @{$self->{$array_var}} ) {
346 $end = @{$self->{$array_var}};
349 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
356 # List of outstanding holds placed
360 my $item_arr = $self->x_items('hold_items', @_);
361 foreach my $item (@{$item_arr}) {
362 my $item_obj = Koha::Items->find($item->{itemnumber});
363 $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
370 return $self->x_items('overdue_items', @_);
374 return $self->x_items('items', @_);
377 require Koha::Database;
385 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
387 borrowernumber => $self->{borrowernumber},
388 amountoutstanding => { '>' => '0' },
392 $start = $start ? $start - 1 : 0;
393 $end = $end ? $end - 1 : scalar @fees - 1;
395 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
396 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
398 my $tt = Template->new();
401 for ( my $i = $start; $i <= $end; $i++ ) {
407 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
408 push( @return_values, { barcode => $output } );
411 return \@return_values;
415 return $self->x_items('recall_items', @_);
419 return $self->x_items('unavail_holds', @_);
423 my ($self, $card_retained, $blocked_card_msg) = @_;
424 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
427 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
428 # TODO: not really affecting patron record
434 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
437 siplog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
438 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
439 $self->{recall_ok}, $self->{hold_ok});
440 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
444 sub inet_privileges {
446 return $self->{inet} ? 'Y' : 'N';
450 return C4::Context->preference('noissuescharge') || 5;
455 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
458 sub excessive_fines {
460 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
463 sub holds_blocked_by_excessive_fees {
465 return ( $self->fee_amount
466 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
471 unless ($self->{library_name}) {
472 my $library = Koha::Libraries->find( $self->{branchcode} );
473 $self->{library_name} = $library ? $library->branchname : '';
475 return $self->{library_name};
483 return "Please contact library staff";
488 return "Please contact library staff";
491 =head2 update_lastseen
493 $patron->update_lastseen();
495 Patron method to update lastseen field in borrower
496 to record that patron has been seen via sip connection
500 sub update_lastseen {
502 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
503 $kohaobj->track_login if $kohaobj; # track_login checks the pref
509 my $address = $patron->{streetnumber} || q{};
510 for my $field (qw( roaddetails address address2 city state zipcode country))
512 next unless $patron->{$field};
515 $address .= $patron->{$field};
518 $address .= $patron->{$field};
524 sub _get_outstanding_holds {
525 my $borrowernumber = shift;
527 my $patron = Koha::Patrons->find( $borrowernumber );
528 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
530 while ( my $hold = $holds->next ) {
532 if ($hold->itemnumber) {
536 # We need to return a barcode for the biblio so the client
537 # can request the biblio info
538 my $items = $hold->biblio->items;
539 $item = $items->count ? $items->next : undef;
541 my $unblessed_hold = $hold->unblessed;
543 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
545 push @holds, $unblessed_hold;
550 =head2 build_patron_attributes_string
552 This method builds the part of the sip message for extended patron
553 attributes as defined in the sip config
557 sub build_patron_attributes_string {
558 my ( $self, $server ) = @_;
561 if ( $server->{account}->{patron_attribute} ) {
562 my @attributes_to_send =
563 ref $server->{account}->{patron_attribute} eq "ARRAY"
564 ? @{ $server->{account}->{patron_attribute} }
565 : ( $server->{account}->{patron_attribute} );
567 foreach my $a ( @attributes_to_send ) {
568 my @attributes = Koha::Patron::Attributes->search(
570 borrowernumber => $self->{borrowernumber},
575 foreach my $attribute ( @attributes ) {
576 my $value = $attribute->attribute();
577 $string .= add_field( $a->{field}, $value );
586 =head2 build_custom_field_string
588 This method builds the part of the sip message for custom patron fields as defined in the sip config
592 sub build_custom_field_string {
593 my ( $self, $server ) = @_;
597 if ( $server->{account}->{custom_patron_field} ) {
599 ref $server->{account}->{custom_patron_field} eq "ARRAY"
600 ? @{ $server->{account}->{custom_patron_field} }
601 : $server->{account}->{custom_patron_field};
602 foreach my $custom_field ( @custom_fields ) {
603 $string .= maybe_add( $custom_field->{field}, $self->format( $custom_field->{template} ) ) if defined $custom_field->{field};
614 our %patron_example = (
616 name => "David J. Fiander",
619 ptype => 'A', # 'A'dult. Whatever.
620 birthdate => '19640925',
621 address => '2 Meadowvale Dr. St Thomas, ON',
622 home_phone => '(519) 555 1234',
623 email_addr => 'djfiander@hotmail.com',
629 claims_returned => 0,
639 fine_items => ['Computer Time'],
646 From borrowers table:
647 +---------------------+--------------+------+-----+---------+----------------+
648 | Field | Type | Null | Key | Default | Extra |
649 +---------------------+--------------+------+-----+---------+----------------+
650 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
651 | cardnumber | varchar(16) | YES | UNI | NULL | |
652 | surname | mediumtext | NO | | NULL | |
653 | firstname | text | YES | | NULL | |
654 | title | mediumtext | YES | | NULL | |
655 | othernames | mediumtext | YES | | NULL | |
656 | initials | text | YES | | NULL | |
657 | streetnumber | varchar(10) | YES | | NULL | |
658 | streettype | varchar(50) | YES | | NULL | |
659 | address | mediumtext | NO | | NULL | |
660 | address2 | text | YES | | NULL | |
661 | city | mediumtext | NO | | NULL | |
662 | state | mediumtext | YES | | NULL | |
663 | zipcode | varchar(25) | YES | | NULL | |
664 | country | text | YES | | NULL | |
665 | email | mediumtext | YES | | NULL | |
666 | phone | text | YES | | NULL | |
667 | mobile | varchar(50) | YES | | NULL | |
668 | fax | mediumtext | YES | | NULL | |
669 | emailpro | text | YES | | NULL | |
670 | phonepro | text | YES | | NULL | |
671 | B_streetnumber | varchar(10) | YES | | NULL | |
672 | B_streettype | varchar(50) | YES | | NULL | |
673 | B_address | varchar(100) | YES | | NULL | |
674 | B_address2 | text | YES | | NULL | |
675 | B_city | mediumtext | YES | | NULL | |
676 | B_state | mediumtext | YES | | NULL | |
677 | B_zipcode | varchar(25) | YES | | NULL | |
678 | B_country | text | YES | | NULL | |
679 | B_email | text | YES | | NULL | |
680 | B_phone | mediumtext | YES | | NULL | |
681 | dateofbirth | date | YES | | NULL | |
682 | branchcode | varchar(10) | NO | MUL | | |
683 | categorycode | varchar(10) | NO | MUL | | |
684 | dateenrolled | date | YES | | NULL | |
685 | dateexpiry | date | YES | | NULL | |
686 | gonenoaddress | tinyint(1) | YES | | NULL | |
687 | lost | tinyint(1) | YES | | NULL | |
688 | debarred | tinyint(1) | YES | | NULL | |
689 | contactname | mediumtext | YES | | NULL | |
690 | contactfirstname | text | YES | | NULL | |
691 | contacttitle | text | YES | | NULL | |
692 | borrowernotes | mediumtext | YES | | NULL | |
693 | relationship | varchar(100) | YES | | NULL | |
694 | ethnicity | varchar(50) | YES | | NULL | |
695 | ethnotes | varchar(255) | YES | | NULL | |
696 | sex | varchar(1) | YES | | NULL | |
697 | password | varchar(30) | YES | | NULL | |
698 | flags | int(11) | YES | | NULL | |
699 | userid | varchar(30) | YES | MUL | NULL | |
700 | opacnote | mediumtext | YES | | NULL | |
701 | contactnote | varchar(255) | YES | | NULL | |
702 | sort1 | varchar(80) | YES | | NULL | |
703 | sort2 | varchar(80) | YES | | NULL | |
704 | altcontactfirstname | varchar(255) | YES | | NULL | |
705 | altcontactsurname | varchar(255) | YES | | NULL | |
706 | altcontactaddress1 | varchar(255) | YES | | NULL | |
707 | altcontactaddress2 | varchar(255) | YES | | NULL | |
708 | altcontactaddress3 | varchar(255) | YES | | NULL | |
709 | altcontactstate | mediumtext | YES | | NULL | |
710 | altcontactzipcode | varchar(50) | YES | | NULL | |
711 | altcontactcountry | text | YES | | NULL | |
712 | altcontactphone | varchar(50) | YES | | NULL | |
713 | smsalertnumber | varchar(50) | YES | | NULL | |
714 | privacy | int(11) | NO | | 1 | |
715 +---------------------+--------------+------+-----+---------+----------------+
722 {message} Message showing patron's credit or debt
723 {noissues} Set if patron owes >$5.00
724 {GNA} Set if patron gone w/o address
725 {message} "Borrower has no valid address"
727 {LOST} Set if patron's card reported lost
728 {message} Message to this effect
730 {DBARRED} Set if patron is debarred
731 {message} Message to this effect
733 {NOTES} Set if patron has notes
734 {message} Notes about patron
735 {ODUES} Set if patron has overdue books
737 {itemlist} ref-to-array: list of overdue books
738 {itemlisttext} Text list of overdue items
739 {WAITING} Set if there are items available that the patron reserved
740 {message} Message to this effect
741 {itemlist} ref-to-array: list of available items