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 ),
130 fee_limit => $fee_limit,
131 userid => $kp->{userid},
135 if ( $patron->is_debarred and $patron->debarredcomment ) {
136 $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
138 if ( $circ_blocked ) {
139 $ilspatron{screen_msg} .= " -- " . "Patron has overdues";
141 for (qw(EXPIRED CHARGES CREDITS GNA LOST NOTES)) {
142 ($flags->{$_}) or next;
143 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
144 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
146 if ($flags->{$_}->{noissues}) {
147 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
148 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
153 # FIXME: populate fine_items recall_items
154 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
156 my $pending_checkouts = $patron->pending_checkouts;
158 while ( my $c = $pending_checkouts->next ) {
159 push @barcodes, { barcode => $c->item->barcode };
161 $ilspatron{items} = \@barcodes;
164 siplog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
185 charge_ok => 0, # for patron_status[0] (inverted)
186 renew_ok => 0, # for patron_status[1] (inverted)
187 recall_ok => 0, # for patron_status[2] (inverted)
188 hold_ok => 0, # for patron_status[3] (inverted)
189 card_lost => 0, # for patron_status[4]
195 too_many_charged => 0, # for patron_status[5]
196 too_many_overdue => 0, # for patron_status[6]
197 too_many_renewal => 0, # for patron_status[7]
198 too_many_claim_return => 0, # for patron_status[8]
199 too_many_lost => 0, # for patron_status[9]
200 # excessive_fines => 0, # for patron_status[10]
201 # excessive_fees => 0, # for patron_status[11]
202 recall_overdue => 0, # for patron_status[12]
203 too_many_billed => 0, # for patron_status[13]
204 inet => 0, # EnvisionWare extension
210 # be cool. needed for AUTOLOAD(?)
215 my $class = ref($self) or croak "$self is not an object";
216 my $name = $AUTOLOAD;
220 unless (exists $fields{$name}) {
221 croak "Cannot access '$name' field of class '$class'";
225 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
226 return $self->{$name} = shift;
228 return $self->{$name};
234 This method uses a template to build a string from a Koha::Patron object
235 If errors are encountered in processing template we log them and return nothing
240 my ( $self, $template ) = @_;
244 require Koha::Patrons;
246 my $tt = Template->new();
248 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
252 $tt->process( \$template, { patron => $patron }, \$output );
255 siplog("LOG_DEBUG", "Error processing template: $template");
263 my ( $self, $pwd ) = @_;
265 # you gotta give me something (at least ''), or no deal
266 return 0 unless defined $pwd;
268 # If the record has a NULL password, accept '' as match
269 return $pwd eq q{} unless $self->{password};
271 my $dbh = C4::Context->dbh;
273 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
277 # A few special cases, not in AUTOLOADed %fields
280 if ( $self->{fines} ) {
281 return $self->{fines};
288 return $self->fee_amount;
293 return $self->{language} || '000'; # Unspecified
298 return $self->{expired};
302 # remove the hold on item item_id from my hold queue.
303 # return true if I was holding the item, false otherwise.
306 my ($self, $item_id) = @_;
309 foreach (qw(hold_items unavail_holds)) {
311 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
312 my $held_item = $self->{$_}[$i]->{barcode} or next;
313 if ($held_item eq $item_id) {
314 splice @{$self->{$_}}, $i, 1;
322 # Accessor method for array_ref values, designed to get the "start" and "end" values
323 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
327 my $array_var = shift or return;
328 my ($start, $end) = @_;
331 if ($self->{$array_var}) {
332 if ($start && $start > 1) {
338 if ( $end && $end < @{$self->{$array_var}} ) {
341 $end = @{$self->{$array_var}};
344 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
351 # List of outstanding holds placed
355 my $item_arr = $self->x_items('hold_items', @_);
356 foreach my $item (@{$item_arr}) {
357 my $item_obj = Koha::Items->find($item->{itemnumber});
358 $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
365 return $self->x_items('overdue_items', @_);
369 return $self->x_items('items', @_);
372 require Koha::Database;
380 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
382 borrowernumber => $self->{borrowernumber},
383 amountoutstanding => { '>' => '0' },
387 $start = $start ? $start - 1 : 0;
388 $end = $end ? $end - 1 : scalar @fees - 1;
390 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
391 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
393 my $tt = Template->new();
396 for ( my $i = $start; $i <= $end; $i++ ) {
402 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
403 push( @return_values, { barcode => $output } );
406 return \@return_values;
410 return $self->x_items('recall_items', @_);
414 return $self->x_items('unavail_holds', @_);
418 my ($self, $card_retained, $blocked_card_msg) = @_;
419 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
422 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
423 # TODO: not really affecting patron record
429 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
432 siplog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
433 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
434 $self->{recall_ok}, $self->{hold_ok});
435 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
439 sub inet_privileges {
441 return $self->{inet} ? 'Y' : 'N';
445 return C4::Context->preference('noissuescharge') || 5;
450 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
453 sub excessive_fines {
455 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
458 sub holds_blocked_by_excessive_fees {
460 return ( $self->fee_amount
461 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
466 unless ($self->{library_name}) {
467 my $library = Koha::Libraries->find( $self->{branchcode} );
468 $self->{library_name} = $library ? $library->branchname : '';
470 return $self->{library_name};
478 return "Please contact library staff";
483 return "Please contact library staff";
486 =head2 update_lastseen
488 $patron->update_lastseen();
490 Patron method to update lastseen field in borrower
491 to record that patron has been seen via sip connection
495 sub update_lastseen {
497 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
498 $kohaobj->track_login if $kohaobj; # track_login checks the pref
504 my $address = $patron->{streetnumber} || q{};
505 for my $field (qw( roaddetails address address2 city state zipcode country))
507 next unless $patron->{$field};
510 $address .= $patron->{$field};
513 $address .= $patron->{$field};
519 sub _get_outstanding_holds {
520 my $borrowernumber = shift;
522 my $patron = Koha::Patrons->find( $borrowernumber );
523 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
525 while ( my $hold = $holds->next ) {
527 if ($hold->itemnumber) {
531 # We need to return a barcode for the biblio so the client
532 # can request the biblio info
533 my $items = $hold->biblio->items;
534 $item = $items->count ? $items->next : undef;
536 my $unblessed_hold = $hold->unblessed;
538 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
540 push @holds, $unblessed_hold;
545 =head2 build_patron_attributes_string
547 This method builds the part of the sip message for extended patron
548 attributes as defined in the sip config
552 sub build_patron_attributes_string {
553 my ( $self, $server ) = @_;
556 if ( $server->{account}->{patron_attribute} ) {
557 my @attributes_to_send =
558 ref $server->{account}->{patron_attribute} eq "ARRAY"
559 ? @{ $server->{account}->{patron_attribute} }
560 : ( $server->{account}->{patron_attribute} );
562 foreach my $a ( @attributes_to_send ) {
563 my @attributes = Koha::Patron::Attributes->search(
565 borrowernumber => $self->{borrowernumber},
570 foreach my $attribute ( @attributes ) {
571 my $value = $attribute->attribute();
572 $string .= add_field( $a->{field}, $value );
581 =head2 build_custom_field_string
583 This method builds the part of the sip message for custom patron fields as defined in the sip config
587 sub build_custom_field_string {
588 my ( $self, $server ) = @_;
592 if ( $server->{account}->{custom_patron_field} ) {
594 ref $server->{account}->{custom_patron_field} eq "ARRAY"
595 ? @{ $server->{account}->{custom_patron_field} }
596 : $server->{account}->{custom_patron_field};
597 foreach my $custom_field ( @custom_fields ) {
598 $string .= maybe_add( $custom_field->{field}, $self->format( $custom_field->{template} ) ) if defined $custom_field->{field};
609 our %patron_example = (
611 name => "David J. Fiander",
614 ptype => 'A', # 'A'dult. Whatever.
615 birthdate => '19640925',
616 address => '2 Meadowvale Dr. St Thomas, ON',
617 home_phone => '(519) 555 1234',
618 email_addr => 'djfiander@hotmail.com',
624 claims_returned => 0,
634 fine_items => ['Computer Time'],
641 From borrowers table:
642 +---------------------+--------------+------+-----+---------+----------------+
643 | Field | Type | Null | Key | Default | Extra |
644 +---------------------+--------------+------+-----+---------+----------------+
645 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
646 | cardnumber | varchar(16) | YES | UNI | NULL | |
647 | surname | mediumtext | NO | | NULL | |
648 | firstname | text | YES | | NULL | |
649 | title | mediumtext | YES | | NULL | |
650 | othernames | mediumtext | YES | | NULL | |
651 | initials | text | YES | | NULL | |
652 | streetnumber | varchar(10) | YES | | NULL | |
653 | streettype | varchar(50) | YES | | NULL | |
654 | address | mediumtext | NO | | NULL | |
655 | address2 | text | YES | | NULL | |
656 | city | mediumtext | NO | | NULL | |
657 | state | mediumtext | YES | | NULL | |
658 | zipcode | varchar(25) | YES | | NULL | |
659 | country | text | YES | | NULL | |
660 | email | mediumtext | YES | | NULL | |
661 | phone | text | YES | | NULL | |
662 | mobile | varchar(50) | YES | | NULL | |
663 | fax | mediumtext | YES | | NULL | |
664 | emailpro | text | YES | | NULL | |
665 | phonepro | text | YES | | NULL | |
666 | B_streetnumber | varchar(10) | YES | | NULL | |
667 | B_streettype | varchar(50) | YES | | NULL | |
668 | B_address | varchar(100) | YES | | NULL | |
669 | B_address2 | text | YES | | NULL | |
670 | B_city | mediumtext | YES | | NULL | |
671 | B_state | mediumtext | YES | | NULL | |
672 | B_zipcode | varchar(25) | YES | | NULL | |
673 | B_country | text | YES | | NULL | |
674 | B_email | text | YES | | NULL | |
675 | B_phone | mediumtext | YES | | NULL | |
676 | dateofbirth | date | YES | | NULL | |
677 | branchcode | varchar(10) | NO | MUL | | |
678 | categorycode | varchar(10) | NO | MUL | | |
679 | dateenrolled | date | YES | | NULL | |
680 | dateexpiry | date | YES | | NULL | |
681 | gonenoaddress | tinyint(1) | YES | | NULL | |
682 | lost | tinyint(1) | YES | | NULL | |
683 | debarred | tinyint(1) | YES | | NULL | |
684 | contactname | mediumtext | YES | | NULL | |
685 | contactfirstname | text | YES | | NULL | |
686 | contacttitle | text | YES | | NULL | |
687 | borrowernotes | mediumtext | YES | | NULL | |
688 | relationship | varchar(100) | YES | | NULL | |
689 | ethnicity | varchar(50) | YES | | NULL | |
690 | ethnotes | varchar(255) | YES | | NULL | |
691 | sex | varchar(1) | YES | | NULL | |
692 | password | varchar(30) | YES | | NULL | |
693 | flags | int(11) | YES | | NULL | |
694 | userid | varchar(30) | YES | MUL | NULL | |
695 | opacnote | mediumtext | YES | | NULL | |
696 | contactnote | varchar(255) | YES | | NULL | |
697 | sort1 | varchar(80) | YES | | NULL | |
698 | sort2 | varchar(80) | YES | | NULL | |
699 | altcontactfirstname | varchar(255) | YES | | NULL | |
700 | altcontactsurname | varchar(255) | YES | | NULL | |
701 | altcontactaddress1 | varchar(255) | YES | | NULL | |
702 | altcontactaddress2 | varchar(255) | YES | | NULL | |
703 | altcontactaddress3 | varchar(255) | YES | | NULL | |
704 | altcontactstate | mediumtext | YES | | NULL | |
705 | altcontactzipcode | varchar(50) | YES | | NULL | |
706 | altcontactcountry | text | YES | | NULL | |
707 | altcontactphone | varchar(50) | YES | | NULL | |
708 | smsalertnumber | varchar(50) | YES | | NULL | |
709 | privacy | int(11) | NO | | 1 | |
710 +---------------------+--------------+------+-----+---------+----------------+
717 {message} Message showing patron's credit or debt
718 {noissues} Set if patron owes >$5.00
719 {GNA} Set if patron gone w/o address
720 {message} "Borrower has no valid address"
722 {LOST} Set if patron's card reported lost
723 {message} Message to this effect
725 {DBARRED} Set if patron is debarred
726 {message} Message to this effect
728 {NOTES} Set if patron has notes
729 {message} Notes about patron
730 {ODUES} Set if patron has overdue books
732 {itemlist} ref-to-array: list of overdue books
733 {itemlisttext} Text list of overdue items
734 {WAITING} Set if there are items available that the patron reserved
735 {message} Message to this effect
736 {itemlist} ref-to-array: list of available items