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);
24 use C4::Auth qw(checkpw);
30 our $kp; # koha patron
37 my ($class, $patron_id) = @_;
38 my $type = ref($class) || $class;
42 if ( ref $patron_id eq "HASH" ) {
43 if ( $patron_id->{borrowernumber} ) {
44 $patron = Koha::Patrons->find( $patron_id->{borrowernumber} );
45 } elsif ( $patron_id->{cardnumber} ) {
46 $patron = Koha::Patrons->find( { cardnumber => $patron_id->{cardnumber} } );
47 } elsif ( $patron_id->{userid} ) {
48 $patron = Koha::Patrons->find( { userid => $patron_id->{userid} } );
51 $patron = Koha::Patrons->find( { cardnumber => $patron_id } )
52 || Koha::Patrons->find( { userid => $patron_id } );
56 siplog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
59 $kp = $patron->unblessed;
60 my $pw = $kp->{password};
61 my $flags = C4::Members::patronflags( $kp );
62 my $debarred = $patron->is_debarred;
63 my ($day, $month, $year) = (localtime)[3,4,5];
64 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
65 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
67 if ($kp->{opacnote} ) {
68 $kp->{opacnote} .= q{ };
70 $kp->{opacnote} .= 'PATRON EXPIRED';
73 my $adr = _get_address($kp);
74 my $dob = $kp->{dateofbirth};
75 $dob and $dob =~ s/-//g; # YYYYMMDD
76 my $dexpiry = $kp->{dateexpiry};
77 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
79 # Get fines and add fines for guarantees (depends on preference NoIssuesChargeGuarantees)
80 my $fines_amount = $flags->{CHARGES}->{amount}; #TODO Replace with $patron->account->non_issues_charges
81 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
82 if ( C4::Context->preference('NoIssuesChargeGuarantorsWithGuarantees') ) {
83 $fines_amount += $patron->relationships_debt({ include_guarantors => 1, only_this_guarantor => 0, include_this_patron => 0 });
85 my $guarantees_fines_amount = $flags->{CHARGES_GUARANTEES} ? $flags->{CHARGES_GUARANTEES}->{amount} : 0; #TODO: Replace with $patron->relationships_debt
86 $fines_amount += $guarantees_fines_amount;
89 my $fee_limit = _fee_limit();
90 my $fine_blocked = $fines_amount > $fee_limit;
91 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
93 no warnings; # any of these $kp->{fields} being concat'd could be undef
95 name => $kp->{firstname} . " " . $kp->{surname},
96 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
98 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
99 dateexpiry => $dexpiry,
100 dateexpiry_iso => $kp->{dateexpiry},
102 birthdate_iso => $kp->{dateofbirth},
103 branchcode => $kp->{branchcode},
104 library_name => "", # only populated if needed, cached here
105 borrowernumber => $kp->{borrowernumber},
107 home_phone => $kp->{phone},
108 email_addr => $kp->{email},
109 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
110 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
111 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
112 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
113 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
114 claims_returned => 0,
115 fines => $fines_amount,
116 fees => 0, # currently not distinct from fines
119 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
122 hold_items => $flags->{WAITING}->{itemlist},
123 overdue_items => $flags->{ODUES}->{itemlist},
124 too_many_overdue => $circ_blocked,
128 inet => ( !$debarred && !$expired ),
129 debarred => $debarred,
131 fine_blocked => $fine_blocked,
132 fee_limit => $fee_limit,
133 userid => $kp->{userid},
137 if ( $patron->is_debarred and $patron->debarredcomment ) {
138 $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
140 if ( $circ_blocked ) {
141 $ilspatron{screen_msg} .= " -- " . "Patron has overdues";
143 for (qw(EXPIRED CHARGES CREDITS GNA LOST NOTES)) {
144 ($flags->{$_}) or next;
145 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
146 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
148 if ($flags->{$_}->{noissues}) {
149 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
150 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
155 # FIXME: populate fine_items recall_items
156 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
158 my $pending_checkouts = $patron->pending_checkouts;
160 while ( my $c = $pending_checkouts->next ) {
161 push @barcodes, { barcode => $c->item->barcode };
163 $ilspatron{items} = \@barcodes;
166 siplog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
189 charge_ok => 0, # for patron_status[0] (inverted)
190 renew_ok => 0, # for patron_status[1] (inverted)
191 recall_ok => 0, # for patron_status[2] (inverted)
192 hold_ok => 0, # for patron_status[3] (inverted)
193 card_lost => 0, # for patron_status[4]
199 too_many_charged => 0, # for patron_status[5]
200 too_many_overdue => 0, # for patron_status[6]
201 too_many_renewal => 0, # for patron_status[7]
202 too_many_claim_return => 0, # for patron_status[8]
203 too_many_lost => 0, # for patron_status[9]
204 # excessive_fines => 0, # for patron_status[10]
205 # excessive_fees => 0, # for patron_status[11]
206 recall_overdue => 0, # for patron_status[12]
207 too_many_billed => 0, # for patron_status[13]
208 inet => 0, # EnvisionWare extension
214 # be cool. needed for AUTOLOAD(?)
219 my $class = ref($self) or croak "$self is not an object";
220 my $name = $AUTOLOAD;
224 unless (exists $fields{$name}) {
225 croak "Cannot access '$name' field of class '$class'";
229 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
230 return $self->{$name} = shift;
232 return $self->{$name};
238 This method uses a template to build a string from a Koha::Patron object
239 If errors are encountered in processing template we log them and return nothing
244 my ( $self, $template ) = @_;
248 require Koha::Patrons;
250 my $tt = Template->new();
252 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
256 $tt->process( \$template, { patron => $patron }, \$output );
259 siplog("LOG_DEBUG", "Error processing template: $template");
267 my ( $self, $pwd ) = @_;
269 # you gotta give me something (at least ''), or no deal
270 return 0 unless defined $pwd;
272 # If the record has a NULL password, accept '' as match
273 return $pwd eq q{} unless $self->{password};
275 my $dbh = C4::Context->dbh;
277 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
281 # A few special cases, not in AUTOLOADed %fields
284 if ( $self->{fines} ) {
285 return $self->{fines};
292 return $self->fee_amount;
297 return $self->{language} || '000'; # Unspecified
302 return $self->{expired};
306 # remove the hold on item item_id from my hold queue.
307 # return true if I was holding the item, false otherwise.
310 my ($self, $item_id) = @_;
313 foreach (qw(hold_items unavail_holds)) {
315 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
316 my $held_item = $self->{$_}[$i]->{barcode} or next;
317 if ($held_item eq $item_id) {
318 splice @{$self->{$_}}, $i, 1;
326 # Accessor method for array_ref values, designed to get the "start" and "end" values
327 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
331 my $array_var = shift or return;
332 my ($start, $end) = @_;
335 if ($self->{$array_var}) {
336 if ($start && $start > 1) {
342 if ( $end && $end < @{$self->{$array_var}} ) {
345 $end = @{$self->{$array_var}};
348 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
355 # List of outstanding holds placed
359 my $item_arr = $self->x_items('hold_items', @_);
360 foreach my $item (@{$item_arr}) {
361 my $item_obj = Koha::Items->find($item->{itemnumber});
362 $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
369 return $self->x_items('overdue_items', @_);
373 return $self->x_items('items', @_);
376 require Koha::Database;
384 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
386 borrowernumber => $self->{borrowernumber},
387 amountoutstanding => { '>' => '0' },
391 $start = $start ? $start - 1 : 0;
392 $end = $end ? $end - 1 : scalar @fees - 1;
394 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
395 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
397 my $tt = Template->new();
400 for ( my $i = $start; $i <= $end; $i++ ) {
406 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
407 push( @return_values, { barcode => $output } );
410 return \@return_values;
414 return $self->x_items('recall_items', @_);
418 return $self->x_items('unavail_holds', @_);
422 my ($self, $card_retained, $blocked_card_msg) = @_;
423 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
426 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
427 # TODO: not really affecting patron record
433 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
436 siplog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
437 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
438 $self->{recall_ok}, $self->{hold_ok});
439 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
443 sub inet_privileges {
445 return $self->{inet} ? 'Y' : 'N';
449 return C4::Context->preference('noissuescharge') || 5;
454 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
457 sub excessive_fines {
459 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
462 sub holds_blocked_by_excessive_fees {
464 return ( $self->fee_amount
465 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
470 unless ($self->{library_name}) {
471 my $library = Koha::Libraries->find( $self->{branchcode} );
472 $self->{library_name} = $library ? $library->branchname : '';
474 return $self->{library_name};
482 return "Please contact library staff";
487 return "Please contact library staff";
490 =head2 update_lastseen
492 $patron->update_lastseen();
494 Patron method to update lastseen field in borrower
495 to record that patron has been seen via sip connection
499 sub update_lastseen {
501 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
502 $kohaobj->track_login if $kohaobj; # track_login checks the pref
508 my $address = $patron->{streetnumber} || q{};
509 for my $field (qw( roaddetails address address2 city state zipcode country))
511 next unless $patron->{$field};
514 $address .= $patron->{$field};
517 $address .= $patron->{$field};
523 sub _get_outstanding_holds {
524 my $borrowernumber = shift;
526 my $patron = Koha::Patrons->find( $borrowernumber );
527 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
529 while ( my $hold = $holds->next ) {
531 if ($hold->itemnumber) {
535 # We need to return a barcode for the biblio so the client
536 # can request the biblio info
537 my $items = $hold->biblio->items;
538 $item = $items->count ? $items->next : undef;
540 my $unblessed_hold = $hold->unblessed;
542 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
544 push @holds, $unblessed_hold;
549 =head2 build_patron_attributes_string
551 This method builds the part of the sip message for extended patron
552 attributes as defined in the sip config
556 sub build_patron_attributes_string {
557 my ( $self, $server ) = @_;
560 if ( $server->{account}->{patron_attribute} ) {
561 my @attributes_to_send =
562 ref $server->{account}->{patron_attribute} eq "ARRAY"
563 ? @{ $server->{account}->{patron_attribute} }
564 : ( $server->{account}->{patron_attribute} );
566 foreach my $a ( @attributes_to_send ) {
567 my @attributes = Koha::Patron::Attributes->search(
569 borrowernumber => $self->{borrowernumber},
574 foreach my $attribute ( @attributes ) {
575 my $value = $attribute->attribute();
576 $string .= add_field( $a->{field}, $value );
585 =head2 build_custom_field_string
587 This method builds the part of the sip message for custom patron fields as defined in the sip config
591 sub build_custom_field_string {
592 my ( $self, $server ) = @_;
596 if ( $server->{account}->{custom_patron_field} ) {
598 ref $server->{account}->{custom_patron_field} eq "ARRAY"
599 ? @{ $server->{account}->{custom_patron_field} }
600 : $server->{account}->{custom_patron_field};
601 foreach my $custom_field ( @custom_fields ) {
602 $string .= maybe_add( $custom_field->{field}, $self->format( $custom_field->{template} ) ) if defined $custom_field->{field};
613 our %patron_example = (
615 name => "David J. Fiander",
618 ptype => 'A', # 'A'dult. Whatever.
619 birthdate => '19640925',
620 address => '2 Meadowvale Dr. St Thomas, ON',
621 home_phone => '(519) 555 1234',
622 email_addr => 'djfiander@hotmail.com',
628 claims_returned => 0,
638 fine_items => ['Computer Time'],
645 From borrowers table:
646 +---------------------+--------------+------+-----+---------+----------------+
647 | Field | Type | Null | Key | Default | Extra |
648 +---------------------+--------------+------+-----+---------+----------------+
649 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
650 | cardnumber | varchar(16) | YES | UNI | NULL | |
651 | surname | mediumtext | NO | | NULL | |
652 | firstname | text | YES | | NULL | |
653 | title | mediumtext | YES | | NULL | |
654 | othernames | mediumtext | YES | | NULL | |
655 | initials | text | YES | | NULL | |
656 | streetnumber | varchar(10) | YES | | NULL | |
657 | streettype | varchar(50) | YES | | NULL | |
658 | address | mediumtext | NO | | NULL | |
659 | address2 | text | YES | | NULL | |
660 | city | mediumtext | NO | | NULL | |
661 | state | mediumtext | YES | | NULL | |
662 | zipcode | varchar(25) | YES | | NULL | |
663 | country | text | YES | | NULL | |
664 | email | mediumtext | YES | | NULL | |
665 | phone | text | YES | | NULL | |
666 | mobile | varchar(50) | YES | | NULL | |
667 | fax | mediumtext | YES | | NULL | |
668 | emailpro | text | YES | | NULL | |
669 | phonepro | text | YES | | NULL | |
670 | B_streetnumber | varchar(10) | YES | | NULL | |
671 | B_streettype | varchar(50) | YES | | NULL | |
672 | B_address | varchar(100) | YES | | NULL | |
673 | B_address2 | text | YES | | NULL | |
674 | B_city | mediumtext | YES | | NULL | |
675 | B_state | mediumtext | YES | | NULL | |
676 | B_zipcode | varchar(25) | YES | | NULL | |
677 | B_country | text | YES | | NULL | |
678 | B_email | text | YES | | NULL | |
679 | B_phone | mediumtext | YES | | NULL | |
680 | dateofbirth | date | YES | | NULL | |
681 | branchcode | varchar(10) | NO | MUL | | |
682 | categorycode | varchar(10) | NO | MUL | | |
683 | dateenrolled | date | YES | | NULL | |
684 | dateexpiry | date | YES | | NULL | |
685 | gonenoaddress | tinyint(1) | YES | | NULL | |
686 | lost | tinyint(1) | YES | | NULL | |
687 | debarred | tinyint(1) | YES | | NULL | |
688 | contactname | mediumtext | YES | | NULL | |
689 | contactfirstname | text | YES | | NULL | |
690 | contacttitle | text | YES | | NULL | |
691 | borrowernotes | mediumtext | YES | | NULL | |
692 | relationship | varchar(100) | YES | | NULL | |
693 | ethnicity | varchar(50) | YES | | NULL | |
694 | ethnotes | varchar(255) | YES | | NULL | |
695 | sex | varchar(1) | YES | | NULL | |
696 | password | varchar(30) | YES | | NULL | |
697 | flags | int(11) | YES | | NULL | |
698 | userid | varchar(30) | YES | MUL | NULL | |
699 | opacnote | mediumtext | YES | | NULL | |
700 | contactnote | varchar(255) | YES | | NULL | |
701 | sort1 | varchar(80) | YES | | NULL | |
702 | sort2 | varchar(80) | YES | | NULL | |
703 | altcontactfirstname | varchar(255) | YES | | NULL | |
704 | altcontactsurname | varchar(255) | YES | | NULL | |
705 | altcontactaddress1 | varchar(255) | YES | | NULL | |
706 | altcontactaddress2 | varchar(255) | YES | | NULL | |
707 | altcontactaddress3 | varchar(255) | YES | | NULL | |
708 | altcontactstate | mediumtext | YES | | NULL | |
709 | altcontactzipcode | varchar(50) | YES | | NULL | |
710 | altcontactcountry | text | YES | | NULL | |
711 | altcontactphone | varchar(50) | YES | | NULL | |
712 | smsalertnumber | varchar(50) | YES | | NULL | |
713 | privacy | int(11) | NO | | 1 | |
714 +---------------------+--------------+------+-----+---------+----------------+
721 {message} Message showing patron's credit or debt
722 {noissues} Set if patron owes >$5.00
723 {GNA} Set if patron gone w/o address
724 {message} "Borrower has no valid address"
726 {LOST} Set if patron's card reported lost
727 {message} Message to this effect
729 {DBARRED} Set if patron is debarred
730 {message} Message to this effect
732 {NOTES} Set if patron has notes
733 {message} Notes about patron
734 {ODUES} Set if patron has overdue books
736 {itemlist} ref-to-array: list of overdue books
737 {itemlisttext} Text list of overdue items
738 {WAITING} Set if there are items available that the patron reserved
739 {message} Message to this effect
740 {itemlist} ref-to-array: list of available items