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} = GetPendingIssues($kp->{borrowernumber});
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";
447 my $address = $patron->{streetnumber} || q{};
448 for my $field (qw( roaddetails address address2 city state zipcode country))
450 next unless $patron->{$field};
453 $address .= $patron->{$field};
456 $address .= $patron->{$field};
462 sub _get_outstanding_holds {
463 my $borrowernumber = shift;
465 my $patron = Koha::Patrons->find( $borrowernumber );
466 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
468 while ( my $hold = $holds->next ) {
470 if ($hold->itemnumber) {
471 $item = $hold->itemnumber;
474 # We need to return a barcode for the biblio so the client
475 # can request the biblio info
476 $item = ( GetItemnumbersForBiblio($hold->biblionumber) )->[0];
478 my $unblessed_hold = $hold->unblessed;
479 $unblessed_hold->{barcode} = GetBarcodeFromItemnumber($item);
480 push @holds, $unblessed_hold;
485 =head2 build_patron_attributes_string
487 This method builds the part of the sip message for extended patron
488 attributes as defined in the sip config
492 sub build_patron_attributes_string {
493 my ( $self, $server ) = @_;
497 if ( $server->{account}->{patron_attribute} ) {
498 my @attributes_to_send =
499 ref $server->{account}->{patron_attribute} eq "ARRAY"
500 ? @{ $server->{account}->{patron_attribute} }
501 : ( $server->{account}->{patron_attribute} );
503 foreach my $a ( @attributes_to_send ) {
504 my @attributes = Koha::Patron::Attributes->search(
506 borrowernumber => $self->{borrowernumber},
511 foreach my $attribute ( @attributes ) {
512 my $value = $attribute->attribute();
513 $string .= add_field( $a->{field}, $value );
526 our %patron_example = (
528 name => "David J. Fiander",
531 ptype => 'A', # 'A'dult. Whatever.
532 birthdate => '19640925',
533 address => '2 Meadowvale Dr. St Thomas, ON',
534 home_phone => '(519) 555 1234',
535 email_addr => 'djfiander@hotmail.com',
541 claims_returned => 0,
551 fine_items => ['Computer Time'],
558 From borrowers table:
559 +---------------------+--------------+------+-----+---------+----------------+
560 | Field | Type | Null | Key | Default | Extra |
561 +---------------------+--------------+------+-----+---------+----------------+
562 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
563 | cardnumber | varchar(16) | YES | UNI | NULL | |
564 | surname | mediumtext | NO | | NULL | |
565 | firstname | text | YES | | NULL | |
566 | title | mediumtext | YES | | NULL | |
567 | othernames | mediumtext | YES | | NULL | |
568 | initials | text | YES | | NULL | |
569 | streetnumber | varchar(10) | YES | | NULL | |
570 | streettype | varchar(50) | YES | | NULL | |
571 | address | mediumtext | NO | | NULL | |
572 | address2 | text | YES | | NULL | |
573 | city | mediumtext | NO | | NULL | |
574 | state | mediumtext | YES | | NULL | |
575 | zipcode | varchar(25) | YES | | NULL | |
576 | country | text | YES | | NULL | |
577 | email | mediumtext | YES | | NULL | |
578 | phone | text | YES | | NULL | |
579 | mobile | varchar(50) | YES | | NULL | |
580 | fax | mediumtext | YES | | NULL | |
581 | emailpro | text | YES | | NULL | |
582 | phonepro | text | YES | | NULL | |
583 | B_streetnumber | varchar(10) | YES | | NULL | |
584 | B_streettype | varchar(50) | YES | | NULL | |
585 | B_address | varchar(100) | YES | | NULL | |
586 | B_address2 | text | YES | | NULL | |
587 | B_city | mediumtext | YES | | NULL | |
588 | B_state | mediumtext | YES | | NULL | |
589 | B_zipcode | varchar(25) | YES | | NULL | |
590 | B_country | text | YES | | NULL | |
591 | B_email | text | YES | | NULL | |
592 | B_phone | mediumtext | YES | | NULL | |
593 | dateofbirth | date | YES | | NULL | |
594 | branchcode | varchar(10) | NO | MUL | | |
595 | categorycode | varchar(10) | NO | MUL | | |
596 | dateenrolled | date | YES | | NULL | |
597 | dateexpiry | date | YES | | NULL | |
598 | gonenoaddress | tinyint(1) | YES | | NULL | |
599 | lost | tinyint(1) | YES | | NULL | |
600 | debarred | tinyint(1) | YES | | NULL | |
601 | contactname | mediumtext | YES | | NULL | |
602 | contactfirstname | text | YES | | NULL | |
603 | contacttitle | text | YES | | NULL | |
604 | guarantorid | int(11) | YES | MUL | NULL | |
605 | borrowernotes | mediumtext | YES | | NULL | |
606 | relationship | varchar(100) | YES | | NULL | |
607 | ethnicity | varchar(50) | YES | | NULL | |
608 | ethnotes | varchar(255) | YES | | NULL | |
609 | sex | varchar(1) | YES | | NULL | |
610 | password | varchar(30) | YES | | NULL | |
611 | flags | int(11) | YES | | NULL | |
612 | userid | varchar(30) | YES | MUL | NULL | |
613 | opacnote | mediumtext | YES | | NULL | |
614 | contactnote | varchar(255) | YES | | NULL | |
615 | sort1 | varchar(80) | YES | | NULL | |
616 | sort2 | varchar(80) | YES | | NULL | |
617 | altcontactfirstname | varchar(255) | YES | | NULL | |
618 | altcontactsurname | varchar(255) | YES | | NULL | |
619 | altcontactaddress1 | varchar(255) | YES | | NULL | |
620 | altcontactaddress2 | varchar(255) | YES | | NULL | |
621 | altcontactaddress3 | varchar(255) | YES | | NULL | |
622 | altcontactstate | mediumtext | YES | | NULL | |
623 | altcontactzipcode | varchar(50) | YES | | NULL | |
624 | altcontactcountry | text | YES | | NULL | |
625 | altcontactphone | varchar(50) | YES | | NULL | |
626 | smsalertnumber | varchar(50) | YES | | NULL | |
627 | privacy | int(11) | NO | | 1 | |
628 +---------------------+--------------+------+-----+---------+----------------+
635 {message} Message showing patron's credit or debt
636 {noissues} Set if patron owes >$5.00
637 {GNA} Set if patron gone w/o address
638 {message} "Borrower has no valid address"
640 {LOST} Set if patron's card reported lost
641 {message} Message to this effect
643 {DBARRED} Set if patron is debarred
644 {message} Message to this effect
646 {NOTES} Set if patron has notes
647 {message} Notes about patron
648 {ODUES} Set if patron has overdue books
650 {itemlist} ref-to-array: list of overdue books
651 {itemlisttext} Text list of overdue items
652 {WAITING} Set if there are items available that the patron reserved
653 {message} Message to this effect
654 {itemlist} ref-to-array: list of available items