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 $kp = Koha::Patrons->find( { cardnumber => $patron_id } )
42 || Koha::Patrons->find( { userid => $patron_id } );
43 $debug and warn "new Patron: " . Dumper($kp->unblessed) if $kp;
45 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
49 my $pw = $kp->{password};
50 my $flags = C4::Members::patronflags( $kp );
51 my $debarred = defined($flags->{DBARRED});
52 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%$flags);
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 = $flags->{CHARGES}->{amount};
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}";
115 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
116 ($flags->{$_}) or next;
117 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
118 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
120 if ($flags->{$_}->{noissues}) {
121 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
122 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
127 # FIXME: populate fine_items recall_items
128 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
129 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
131 $debug and warn Dumper($self);
132 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
152 charge_ok => 0, # for patron_status[0] (inverted)
153 renew_ok => 0, # for patron_status[1] (inverted)
154 recall_ok => 0, # for patron_status[2] (inverted)
155 hold_ok => 0, # for patron_status[3] (inverted)
156 card_lost => 0, # for patron_status[4]
162 too_many_charged => 0, # for patron_status[5]
163 too_many_overdue => 0, # for patron_status[6]
164 too_many_renewal => 0, # for patron_status[7]
165 too_many_claim_return => 0, # for patron_status[8]
166 too_many_lost => 0, # for patron_status[9]
167 # excessive_fines => 0, # for patron_status[10]
168 # excessive_fees => 0, # for patron_status[11]
169 recall_overdue => 0, # for patron_status[12]
170 too_many_billed => 0, # for patron_status[13]
171 inet => 0, # EnvisionWare extension
177 # be cool. needed for AUTOLOAD(?)
182 my $class = ref($self) or croak "$self is not an object";
183 my $name = $AUTOLOAD;
187 unless (exists $fields{$name}) {
188 croak "Cannot access '$name' field of class '$class'";
192 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
193 return $self->{$name} = shift;
195 return $self->{$name};
200 my ( $self, $template ) = @_;
204 require Koha::Patrons;
206 my $tt = Template->new();
208 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
211 $tt->process( \$template, { patron => $patron }, \$output );
215 return $self->{name};
220 my ( $self, $pwd ) = @_;
222 # you gotta give me something (at least ''), or no deal
223 return 0 unless defined $pwd;
225 # If the record has a NULL password, accept '' as match
226 return $pwd eq q{} unless $self->{password};
228 my $dbh = C4::Context->dbh;
230 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
234 # A few special cases, not in AUTOLOADed %fields
237 if ( $self->{fines} ) {
238 return $self->{fines};
245 return $self->fee_amount;
250 return $self->{language} || '000'; # Unspecified
255 return $self->{expired};
259 # remove the hold on item item_id from my hold queue.
260 # return true if I was holding the item, false otherwise.
263 my ($self, $item_id) = @_;
266 foreach (qw(hold_items unavail_holds)) {
268 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
269 my $held_item = $self->{$_}[$i]->{item_id} or next;
270 if ($held_item eq $item_id) {
271 splice @{$self->{$_}}, $i, 1;
279 # Accessor method for array_ref values, designed to get the "start" and "end" values
280 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
284 my $array_var = shift or return;
285 my ($start, $end) = @_;
288 if ($self->{$array_var}) {
289 if ($start && $start > 1) {
295 if ( $end && $end < @{$self->{$array_var}} ) {
298 $end = @{$self->{$array_var}};
301 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
308 # List of outstanding holds placed
312 my $item_arr = $self->x_items('hold_items', @_);
313 foreach my $item (@{$item_arr}) {
314 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
321 return $self->x_items('overdue_items', @_);
325 return $self->x_items('items', @_);
328 require Koha::Database;
336 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
338 borrowernumber => $self->{borrowernumber},
339 amountoutstanding => { '>' => '0' },
343 $start = $start ? $start - 1 : 0;
344 $end = $end ? $end : scalar @fees - 1;
346 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
347 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
349 my $tt = Template->new();
352 for ( my $i = $start; $i <= $end; $i++ ) {
356 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
357 push( @return_values, { barcode => $output } );
360 return \@return_values;
364 return $self->x_items('recall_items', @_);
368 return $self->x_items('unavail_holds', @_);
372 my ($self, $card_retained, $blocked_card_msg) = @_;
373 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
376 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
377 # TODO: not really affecting patron record
383 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
386 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
387 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
388 $self->{recall_ok}, $self->{hold_ok});
389 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
393 sub inet_privileges {
395 return $self->{inet} ? 'Y' : 'N';
399 return C4::Context->preference('noissuescharge') || 5;
404 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
407 sub excessive_fines {
409 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
412 sub holds_blocked_by_excessive_fees {
414 return ( $self->fee_amount
415 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
420 unless ($self->{library_name}) {
421 my $library = Koha::Libraries->find( $self->{branchcode} );
422 $self->{library_name} = $library ? $library->branchname : '';
424 return $self->{library_name};
432 return "Please contact library staff";
437 return "Please contact library staff";
443 my $address = $patron->{streetnumber} || q{};
444 for my $field (qw( roaddetails address address2 city state zipcode country))
446 next unless $patron->{$field};
449 $address .= $patron->{$field};
452 $address .= $patron->{$field};
458 sub _get_outstanding_holds {
459 my $borrowernumber = shift;
461 my $patron = Koha::Patrons->find( $borrowernumber );
462 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
464 while ( my $hold = $holds->next ) {
466 if ($hold->itemnumber) {
467 $item = $hold->itemnumber;
470 # We need to return a barcode for the biblio so the client
471 # can request the biblio info
472 $item = ( GetItemnumbersForBiblio($hold->biblionumber) )->[0];
474 my $unblessed_hold = $hold->unblessed;
475 $unblessed_hold->{barcode} = GetBarcodeFromItemnumber($item);
476 push @holds, $unblessed_hold;
481 =head2 build_patron_attributes_string
483 This method builds the part of the sip message for extended patron
484 attributes as defined in the sip config
488 sub build_patron_attributes_string {
489 my ( $self, $server ) = @_;
493 if ( $server->{account}->{patron_attribute} ) {
494 my @attributes_to_send =
495 ref $server->{account}->{patron_attribute} eq "ARRAY"
496 ? @{ $server->{account}->{patron_attribute} }
497 : ( $server->{account}->{patron_attribute} );
499 foreach my $a ( @attributes_to_send ) {
500 my @attributes = Koha::Patron::Attributes->search(
502 borrowernumber => $self->{borrowernumber},
507 foreach my $attribute ( @attributes ) {
508 my $value = $attribute->attribute();
509 $string .= add_field( $a->{field}, $value );
522 our %patron_example = (
524 name => "David J. Fiander",
527 ptype => 'A', # 'A'dult. Whatever.
528 birthdate => '19640925',
529 address => '2 Meadowvale Dr. St Thomas, ON',
530 home_phone => '(519) 555 1234',
531 email_addr => 'djfiander@hotmail.com',
537 claims_returned => 0,
547 fine_items => ['Computer Time'],
554 From borrowers table:
555 +---------------------+--------------+------+-----+---------+----------------+
556 | Field | Type | Null | Key | Default | Extra |
557 +---------------------+--------------+------+-----+---------+----------------+
558 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
559 | cardnumber | varchar(16) | YES | UNI | NULL | |
560 | surname | mediumtext | NO | | NULL | |
561 | firstname | text | YES | | NULL | |
562 | title | mediumtext | YES | | NULL | |
563 | othernames | mediumtext | YES | | NULL | |
564 | initials | text | YES | | NULL | |
565 | streetnumber | varchar(10) | YES | | NULL | |
566 | streettype | varchar(50) | YES | | NULL | |
567 | address | mediumtext | NO | | NULL | |
568 | address2 | text | YES | | NULL | |
569 | city | mediumtext | NO | | NULL | |
570 | state | mediumtext | YES | | NULL | |
571 | zipcode | varchar(25) | YES | | NULL | |
572 | country | text | YES | | NULL | |
573 | email | mediumtext | YES | | NULL | |
574 | phone | text | YES | | NULL | |
575 | mobile | varchar(50) | YES | | NULL | |
576 | fax | mediumtext | YES | | NULL | |
577 | emailpro | text | YES | | NULL | |
578 | phonepro | text | YES | | NULL | |
579 | B_streetnumber | varchar(10) | YES | | NULL | |
580 | B_streettype | varchar(50) | YES | | NULL | |
581 | B_address | varchar(100) | YES | | NULL | |
582 | B_address2 | text | YES | | NULL | |
583 | B_city | mediumtext | YES | | NULL | |
584 | B_state | mediumtext | YES | | NULL | |
585 | B_zipcode | varchar(25) | YES | | NULL | |
586 | B_country | text | YES | | NULL | |
587 | B_email | text | YES | | NULL | |
588 | B_phone | mediumtext | YES | | NULL | |
589 | dateofbirth | date | YES | | NULL | |
590 | branchcode | varchar(10) | NO | MUL | | |
591 | categorycode | varchar(10) | NO | MUL | | |
592 | dateenrolled | date | YES | | NULL | |
593 | dateexpiry | date | YES | | NULL | |
594 | gonenoaddress | tinyint(1) | YES | | NULL | |
595 | lost | tinyint(1) | YES | | NULL | |
596 | debarred | tinyint(1) | YES | | NULL | |
597 | contactname | mediumtext | YES | | NULL | |
598 | contactfirstname | text | YES | | NULL | |
599 | contacttitle | text | YES | | NULL | |
600 | guarantorid | int(11) | YES | MUL | NULL | |
601 | borrowernotes | mediumtext | YES | | NULL | |
602 | relationship | varchar(100) | YES | | NULL | |
603 | ethnicity | varchar(50) | YES | | NULL | |
604 | ethnotes | varchar(255) | YES | | NULL | |
605 | sex | varchar(1) | YES | | NULL | |
606 | password | varchar(30) | YES | | NULL | |
607 | flags | int(11) | YES | | NULL | |
608 | userid | varchar(30) | YES | MUL | NULL | |
609 | opacnote | mediumtext | YES | | NULL | |
610 | contactnote | varchar(255) | YES | | NULL | |
611 | sort1 | varchar(80) | YES | | NULL | |
612 | sort2 | varchar(80) | YES | | NULL | |
613 | altcontactfirstname | varchar(255) | YES | | NULL | |
614 | altcontactsurname | varchar(255) | YES | | NULL | |
615 | altcontactaddress1 | varchar(255) | YES | | NULL | |
616 | altcontactaddress2 | varchar(255) | YES | | NULL | |
617 | altcontactaddress3 | varchar(255) | YES | | NULL | |
618 | altcontactstate | mediumtext | YES | | NULL | |
619 | altcontactzipcode | varchar(50) | YES | | NULL | |
620 | altcontactcountry | text | YES | | NULL | |
621 | altcontactphone | varchar(50) | YES | | NULL | |
622 | smsalertnumber | varchar(50) | YES | | NULL | |
623 | privacy | int(11) | NO | | 1 | |
624 +---------------------+--------------+------+-----+---------+----------------+
631 {message} Message showing patron's credit or debt
632 {noissues} Set if patron owes >$5.00
633 {GNA} Set if patron gone w/o address
634 {message} "Borrower has no valid address"
636 {LOST} Set if patron's card reported lost
637 {message} Message to this effect
639 {DBARRED} Set if patron is debarred
640 {message} Message to this effect
642 {NOTES} Set if patron has notes
643 {message} Notes about patron
644 {ODUES} Set if patron has overdue books
646 {itemlist} ref-to-array: list of overdue books
647 {itemlisttext} Text list of overdue items
648 {WAITING} Set if there are items available that the patron reserved
649 {message} Message to this effect
650 {itemlist} ref-to-array: list of available items