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
69 my $fines_amount = $flags->{CHARGES}->{amount}; # This "amount" is the negative balance or the one of the guarantees
70 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
71 my $guarantees_fines_amount = $flags->{CHARGES_GUARANTEES} ? $flags->{CHARGES_GUARANTEES}->{amount} : 0;
72 $fines_amount += $guarantees_fines_amount;
74 my $fee_limit = _fee_limit();
75 my $fine_blocked = $fines_amount > $fee_limit;
76 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
78 no warnings; # any of these $kp->{fields} being concat'd could be undef
80 name => $kp->{firstname} . " " . $kp->{surname},
81 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
83 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
84 dateexpiry => $dexpiry,
85 dateexpiry_iso => $kp->{dateexpiry},
87 birthdate_iso => $kp->{dateofbirth},
88 branchcode => $kp->{branchcode},
89 library_name => "", # only populated if needed, cached here
90 borrowernumber => $kp->{borrowernumber},
92 home_phone => $kp->{phone},
93 email_addr => $kp->{email},
94 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
95 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
96 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
97 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
98 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
100 fines => $fines_amount,
101 fees => 0, # currently not distinct from fines
104 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
107 hold_items => $flags->{WAITING}->{itemlist},
108 overdue_items => $flags->{ODUES}->{itemlist},
112 inet => ( !$debarred && !$expired ),
114 fee_limit => $fee_limit,
115 userid => $kp->{userid},
118 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
120 if ( $patron->is_debarred and $patron->debarredcomment ) {
121 $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
123 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
124 ($flags->{$_}) or next;
125 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
126 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
128 if ($flags->{$_}->{noissues}) {
129 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
130 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
135 # FIXME: populate fine_items recall_items
136 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
138 my $pending_checkouts = $patron->pending_checkouts;
140 while ( my $c = $pending_checkouts->next ) {
141 push @barcodes, { barcode => $c->item->barcode };
143 $ilspatron{items} = \@barcodes;
146 $debug and warn Dumper($self);
147 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
167 charge_ok => 0, # for patron_status[0] (inverted)
168 renew_ok => 0, # for patron_status[1] (inverted)
169 recall_ok => 0, # for patron_status[2] (inverted)
170 hold_ok => 0, # for patron_status[3] (inverted)
171 card_lost => 0, # for patron_status[4]
177 too_many_charged => 0, # for patron_status[5]
178 too_many_overdue => 0, # for patron_status[6]
179 too_many_renewal => 0, # for patron_status[7]
180 too_many_claim_return => 0, # for patron_status[8]
181 too_many_lost => 0, # for patron_status[9]
182 # excessive_fines => 0, # for patron_status[10]
183 # excessive_fees => 0, # for patron_status[11]
184 recall_overdue => 0, # for patron_status[12]
185 too_many_billed => 0, # for patron_status[13]
186 inet => 0, # EnvisionWare extension
192 # be cool. needed for AUTOLOAD(?)
197 my $class = ref($self) or croak "$self is not an object";
198 my $name = $AUTOLOAD;
202 unless (exists $fields{$name}) {
203 croak "Cannot access '$name' field of class '$class'";
207 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
208 return $self->{$name} = shift;
210 return $self->{$name};
215 my ( $self, $template ) = @_;
219 require Koha::Patrons;
221 my $tt = Template->new();
223 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
226 $tt->process( \$template, { patron => $patron }, \$output );
230 return $self->{name};
235 my ( $self, $pwd ) = @_;
237 # you gotta give me something (at least ''), or no deal
238 return 0 unless defined $pwd;
240 # If the record has a NULL password, accept '' as match
241 return $pwd eq q{} unless $self->{password};
243 my $dbh = C4::Context->dbh;
245 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
249 # A few special cases, not in AUTOLOADed %fields
252 if ( $self->{fines} ) {
253 return $self->{fines};
260 return $self->fee_amount;
265 return $self->{language} || '000'; # Unspecified
270 return $self->{expired};
274 # remove the hold on item item_id from my hold queue.
275 # return true if I was holding the item, false otherwise.
278 my ($self, $item_id) = @_;
281 foreach (qw(hold_items unavail_holds)) {
283 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
284 my $held_item = $self->{$_}[$i]->{item_id} or next;
285 if ($held_item eq $item_id) {
286 splice @{$self->{$_}}, $i, 1;
294 # Accessor method for array_ref values, designed to get the "start" and "end" values
295 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
299 my $array_var = shift or return;
300 my ($start, $end) = @_;
303 if ($self->{$array_var}) {
304 if ($start && $start > 1) {
310 if ( $end && $end < @{$self->{$array_var}} ) {
313 $end = @{$self->{$array_var}};
316 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
323 # List of outstanding holds placed
327 my $item_arr = $self->x_items('hold_items', @_);
328 foreach my $item (@{$item_arr}) {
329 my $item_obj = Koha::Items->find($item->{itemnumber});
330 $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
337 return $self->x_items('overdue_items', @_);
341 return $self->x_items('items', @_);
344 require Koha::Database;
352 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
354 borrowernumber => $self->{borrowernumber},
355 amountoutstanding => { '>' => '0' },
359 $start = $start ? $start - 1 : 0;
360 $end = $end ? $end : scalar @fees - 1;
362 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
363 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
365 my $tt = Template->new();
368 for ( my $i = $start; $i <= $end; $i++ ) {
372 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
373 push( @return_values, { barcode => $output } );
376 return \@return_values;
380 return $self->x_items('recall_items', @_);
384 return $self->x_items('unavail_holds', @_);
388 my ($self, $card_retained, $blocked_card_msg) = @_;
389 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
392 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
393 # TODO: not really affecting patron record
399 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
402 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
403 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
404 $self->{recall_ok}, $self->{hold_ok});
405 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
409 sub inet_privileges {
411 return $self->{inet} ? 'Y' : 'N';
415 return C4::Context->preference('noissuescharge') || 5;
420 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
423 sub excessive_fines {
425 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
428 sub holds_blocked_by_excessive_fees {
430 return ( $self->fee_amount
431 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
436 unless ($self->{library_name}) {
437 my $library = Koha::Libraries->find( $self->{branchcode} );
438 $self->{library_name} = $library ? $library->branchname : '';
440 return $self->{library_name};
448 return "Please contact library staff";
453 return "Please contact library staff";
456 =head2 update_lastseen
458 $patron->update_lastseen();
460 Patron method to update lastseen field in borrower
461 to record that patron has been seen via sip connection
465 sub update_lastseen {
467 my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
468 $kohaobj->track_login if $kohaobj; # track_login checks the pref
474 my $address = $patron->{streetnumber} || q{};
475 for my $field (qw( roaddetails address address2 city state zipcode country))
477 next unless $patron->{$field};
480 $address .= $patron->{$field};
483 $address .= $patron->{$field};
489 sub _get_outstanding_holds {
490 my $borrowernumber = shift;
492 my $patron = Koha::Patrons->find( $borrowernumber );
493 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
495 while ( my $hold = $holds->next ) {
497 if ($hold->itemnumber) {
501 # We need to return a barcode for the biblio so the client
502 # can request the biblio info
503 my $items = $hold->biblio->items;
504 $item = $items->count ? $items->next : undef;
506 my $unblessed_hold = $hold->unblessed;
508 $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
510 push @holds, $unblessed_hold;
515 =head2 build_patron_attributes_string
517 This method builds the part of the sip message for extended patron
518 attributes as defined in the sip config
522 sub build_patron_attributes_string {
523 my ( $self, $server ) = @_;
527 if ( $server->{account}->{patron_attribute} ) {
528 my @attributes_to_send =
529 ref $server->{account}->{patron_attribute} eq "ARRAY"
530 ? @{ $server->{account}->{patron_attribute} }
531 : ( $server->{account}->{patron_attribute} );
533 foreach my $a ( @attributes_to_send ) {
534 my @attributes = Koha::Patron::Attributes->search(
536 borrowernumber => $self->{borrowernumber},
541 foreach my $attribute ( @attributes ) {
542 my $value = $attribute->attribute();
543 $string .= add_field( $a->{field}, $value );
556 our %patron_example = (
558 name => "David J. Fiander",
561 ptype => 'A', # 'A'dult. Whatever.
562 birthdate => '19640925',
563 address => '2 Meadowvale Dr. St Thomas, ON',
564 home_phone => '(519) 555 1234',
565 email_addr => 'djfiander@hotmail.com',
571 claims_returned => 0,
581 fine_items => ['Computer Time'],
588 From borrowers table:
589 +---------------------+--------------+------+-----+---------+----------------+
590 | Field | Type | Null | Key | Default | Extra |
591 +---------------------+--------------+------+-----+---------+----------------+
592 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
593 | cardnumber | varchar(16) | YES | UNI | NULL | |
594 | surname | mediumtext | NO | | NULL | |
595 | firstname | text | YES | | NULL | |
596 | title | mediumtext | YES | | NULL | |
597 | othernames | mediumtext | YES | | NULL | |
598 | initials | text | YES | | NULL | |
599 | streetnumber | varchar(10) | YES | | NULL | |
600 | streettype | varchar(50) | YES | | NULL | |
601 | address | mediumtext | NO | | NULL | |
602 | address2 | text | YES | | NULL | |
603 | city | mediumtext | NO | | NULL | |
604 | state | mediumtext | YES | | NULL | |
605 | zipcode | varchar(25) | YES | | NULL | |
606 | country | text | YES | | NULL | |
607 | email | mediumtext | YES | | NULL | |
608 | phone | text | YES | | NULL | |
609 | mobile | varchar(50) | YES | | NULL | |
610 | fax | mediumtext | YES | | NULL | |
611 | emailpro | text | YES | | NULL | |
612 | phonepro | text | YES | | NULL | |
613 | B_streetnumber | varchar(10) | YES | | NULL | |
614 | B_streettype | varchar(50) | YES | | NULL | |
615 | B_address | varchar(100) | YES | | NULL | |
616 | B_address2 | text | YES | | NULL | |
617 | B_city | mediumtext | YES | | NULL | |
618 | B_state | mediumtext | YES | | NULL | |
619 | B_zipcode | varchar(25) | YES | | NULL | |
620 | B_country | text | YES | | NULL | |
621 | B_email | text | YES | | NULL | |
622 | B_phone | mediumtext | YES | | NULL | |
623 | dateofbirth | date | YES | | NULL | |
624 | branchcode | varchar(10) | NO | MUL | | |
625 | categorycode | varchar(10) | NO | MUL | | |
626 | dateenrolled | date | YES | | NULL | |
627 | dateexpiry | date | YES | | NULL | |
628 | gonenoaddress | tinyint(1) | YES | | NULL | |
629 | lost | tinyint(1) | YES | | NULL | |
630 | debarred | tinyint(1) | YES | | NULL | |
631 | contactname | mediumtext | YES | | NULL | |
632 | contactfirstname | text | YES | | NULL | |
633 | contacttitle | text | YES | | NULL | |
634 | borrowernotes | mediumtext | YES | | NULL | |
635 | relationship | varchar(100) | YES | | NULL | |
636 | ethnicity | varchar(50) | YES | | NULL | |
637 | ethnotes | varchar(255) | YES | | NULL | |
638 | sex | varchar(1) | YES | | NULL | |
639 | password | varchar(30) | YES | | NULL | |
640 | flags | int(11) | YES | | NULL | |
641 | userid | varchar(30) | YES | MUL | NULL | |
642 | opacnote | mediumtext | YES | | NULL | |
643 | contactnote | varchar(255) | YES | | NULL | |
644 | sort1 | varchar(80) | YES | | NULL | |
645 | sort2 | varchar(80) | YES | | NULL | |
646 | altcontactfirstname | varchar(255) | YES | | NULL | |
647 | altcontactsurname | varchar(255) | YES | | NULL | |
648 | altcontactaddress1 | varchar(255) | YES | | NULL | |
649 | altcontactaddress2 | varchar(255) | YES | | NULL | |
650 | altcontactaddress3 | varchar(255) | YES | | NULL | |
651 | altcontactstate | mediumtext | YES | | NULL | |
652 | altcontactzipcode | varchar(50) | YES | | NULL | |
653 | altcontactcountry | text | YES | | NULL | |
654 | altcontactphone | varchar(50) | YES | | NULL | |
655 | smsalertnumber | varchar(50) | YES | | NULL | |
656 | privacy | int(11) | NO | | 1 | |
657 +---------------------+--------------+------+-----+---------+----------------+
664 {message} Message showing patron's credit or debt
665 {noissues} Set if patron owes >$5.00
666 {GNA} Set if patron gone w/o address
667 {message} "Borrower has no valid address"
669 {LOST} Set if patron's card reported lost
670 {message} Message to this effect
672 {DBARRED} Set if patron is debarred
673 {message} Message to this effect
675 {NOTES} Set if patron has notes
676 {message} Notes about patron
677 {ODUES} Set if patron has overdue books
679 {itemlist} ref-to-array: list of overdue books
680 {itemlisttext} Text list of overdue items
681 {WAITING} Set if there are items available that the patron reserved
682 {message} Message to this effect
683 {itemlist} ref-to-array: list of available items