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);
23 use C4::Items qw( GetBarcodeFromItemnumber GetItemnumbersForBiblio);
24 use C4::Auth qw(checkpw);
29 our $kp; # koha patron
32 my ($class, $patron_id) = @_;
33 my $type = ref($class) || $class;
35 $kp = Koha::Patrons->find( { cardnumber => $patron_id } )
36 or Koha::Patrons->find( { userid => $patron_id } );
37 $debug and warn "new Patron: " . Dumper($kp->unblessed) if $kp;
39 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
43 my $pw = $kp->{password};
44 my $flags = C4::Members::patronflags( $kp );
45 my $debarred = defined($flags->{DBARRED});
46 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%$flags);
47 my ($day, $month, $year) = (localtime)[3,4,5];
48 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
49 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
51 if ($kp->{opacnote} ) {
52 $kp->{opacnote} .= q{ };
54 $kp->{opacnote} .= 'PATRON EXPIRED';
57 my $adr = _get_address($kp);
58 my $dob = $kp->{dateofbirth};
59 $dob and $dob =~ s/-//g; # YYYYMMDD
60 my $dexpiry = $kp->{dateexpiry};
61 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
62 my $fines_amount = $flags->{CHARGES}->{amount};
63 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
64 my $fee_limit = _fee_limit();
65 my $fine_blocked = $fines_amount > $fee_limit;
66 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
68 no warnings; # any of these $kp->{fields} being concat'd could be undef
70 getmemberdetails_object => $kp,
71 name => $kp->{firstname} . " " . $kp->{surname},
72 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
74 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
75 dateexpiry => $dexpiry,
76 dateexpiry_iso => $kp->{dateexpiry},
78 birthdate_iso => $kp->{dateofbirth},
79 branchcode => $kp->{branchcode},
80 library_name => "", # only populated if needed, cached here
81 borrowernumber => $kp->{borrowernumber},
83 home_phone => $kp->{phone},
84 email_addr => $kp->{email},
85 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
86 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
87 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
88 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
89 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
91 fines => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
92 fees => 0, # currently not distinct from fines
95 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
98 hold_items => $flags->{WAITING}->{itemlist},
99 overdue_items => $flags->{ODUES}->{itemlist},
103 inet => ( !$debarred && !$expired ),
105 fee_limit => $fee_limit,
106 userid => $kp->{userid},
109 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
110 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
111 ($flags->{$_}) or next;
112 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
113 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
115 if ($flags->{$_}->{noissues}) {
116 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
117 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
122 # FIXME: populate fine_items recall_items
123 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
124 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
126 $debug and warn Dumper($self);
127 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
147 charge_ok => 0, # for patron_status[0] (inverted)
148 renew_ok => 0, # for patron_status[1] (inverted)
149 recall_ok => 0, # for patron_status[2] (inverted)
150 hold_ok => 0, # for patron_status[3] (inverted)
151 card_lost => 0, # for patron_status[4]
157 too_many_charged => 0, # for patron_status[5]
158 too_many_overdue => 0, # for patron_status[6]
159 too_many_renewal => 0, # for patron_status[7]
160 too_many_claim_return => 0, # for patron_status[8]
161 too_many_lost => 0, # for patron_status[9]
162 # excessive_fines => 0, # for patron_status[10]
163 # excessive_fees => 0, # for patron_status[11]
164 recall_overdue => 0, # for patron_status[12]
165 too_many_billed => 0, # for patron_status[13]
166 inet => 0, # EnvisionWare extension
167 getmemberdetails_object => 0,
173 # be cool. needed for AUTOLOAD(?)
178 my $class = ref($self) or croak "$self is not an object";
179 my $name = $AUTOLOAD;
183 unless (exists $fields{$name}) {
184 croak "Cannot access '$name' field of class '$class'";
188 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
189 return $self->{$name} = shift;
191 return $self->{$name};
196 my ( $self, $template ) = @_;
200 require Koha::Patrons;
202 my $tt = Template->new();
204 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
207 $tt->process( \$template, { patron => $patron }, \$output );
211 return $self->{name};
216 my ( $self, $pwd ) = @_;
218 # you gotta give me something (at least ''), or no deal
219 return 0 unless defined $pwd;
221 # If the record has a NULL password, accept '' as match
222 return $pwd eq q{} unless $self->{password};
224 my $dbh = C4::Context->dbh;
226 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
230 # A few special cases, not in AUTOLOADed %fields
233 if ( $self->{fines} ) {
234 return $self->{fines};
241 return $self->fee_amount;
246 return $self->{language} || '000'; # Unspecified
251 return $self->{expired};
255 # remove the hold on item item_id from my hold queue.
256 # return true if I was holding the item, false otherwise.
259 my ($self, $item_id) = @_;
262 foreach (qw(hold_items unavail_holds)) {
264 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
265 my $held_item = $self->{$_}[$i]->{item_id} or next;
266 if ($held_item eq $item_id) {
267 splice @{$self->{$_}}, $i, 1;
275 # Accessor method for array_ref values, designed to get the "start" and "end" values
276 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
280 my $array_var = shift or return;
281 my ($start, $end) = @_;
284 if ($self->{$array_var}) {
285 if ($start && $start > 1) {
291 if ( $end && $end < @{$self->{$array_var}} ) {
294 $end = @{$self->{$array_var}};
297 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
304 # List of outstanding holds placed
308 my $item_arr = $self->x_items('hold_items', @_);
309 foreach my $item (@{$item_arr}) {
310 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
317 return $self->x_items('overdue_items', @_);
321 return $self->x_items('items', @_);
324 require Koha::Database;
332 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
334 borrowernumber => $self->{borrowernumber},
335 amountoutstanding => { '>' => '0' },
339 $start = $start ? $start - 1 : 0;
340 $end = $end ? $end : scalar @fees - 1;
342 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
343 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
345 my $tt = Template->new();
348 for ( my $i = $start; $i <= $end; $i++ ) {
352 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
353 push( @return_values, { barcode => $output } );
356 return \@return_values;
360 return $self->x_items('recall_items', @_);
364 return $self->x_items('unavail_holds', @_);
368 my ($self, $card_retained, $blocked_card_msg) = @_;
369 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
372 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
373 # TODO: not really affecting patron record
379 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
382 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
383 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
384 $self->{recall_ok}, $self->{hold_ok});
385 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
389 sub inet_privileges {
391 return $self->{inet} ? 'Y' : 'N';
395 return C4::Context->preference('noissuescharge') || 5;
400 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
403 sub excessive_fines {
405 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
408 sub holds_blocked_by_excessive_fees {
410 return ( $self->fee_amount
411 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
416 unless ($self->{library_name}) {
417 my $library = Koha::Libraries->find( $self->{branchcode} );
418 $self->{library_name} = $library ? $library->branchname : '';
420 return $self->{library_name};
428 return "Please contact library staff";
433 return "Please contact library staff";
439 my $address = $patron->{streetnumber} || q{};
440 for my $field (qw( roaddetails address address2 city state zipcode country))
442 next unless $patron->{$field};
445 $address .= $patron->{$field};
448 $address .= $patron->{$field};
454 sub _get_outstanding_holds {
455 my $borrowernumber = shift;
457 my $patron = Koha::Patrons->find( $borrowernumber );
458 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
460 while ( my $hold = $holds->next ) {
462 if ($hold->itemnumber) {
463 $item = $hold->itemnumber;
466 # We need to return a barcode for the biblio so the client
467 # can request the biblio info
468 $item = ( GetItemnumbersForBiblio($hold->biblionumber) )->[0];
470 my $unblessed_hold = $hold->unblessed;
471 $unblessed_hold->{barcode} = GetBarcodeFromItemnumber($item);
472 push @holds, $unblessed_hold;
482 our %patron_example = (
484 name => "David J. Fiander",
487 ptype => 'A', # 'A'dult. Whatever.
488 birthdate => '19640925',
489 address => '2 Meadowvale Dr. St Thomas, ON',
490 home_phone => '(519) 555 1234',
491 email_addr => 'djfiander@hotmail.com',
497 claims_returned => 0,
507 fine_items => ['Computer Time'],
514 From borrowers table:
515 +---------------------+--------------+------+-----+---------+----------------+
516 | Field | Type | Null | Key | Default | Extra |
517 +---------------------+--------------+------+-----+---------+----------------+
518 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
519 | cardnumber | varchar(16) | YES | UNI | NULL | |
520 | surname | mediumtext | NO | | NULL | |
521 | firstname | text | YES | | NULL | |
522 | title | mediumtext | YES | | NULL | |
523 | othernames | mediumtext | YES | | NULL | |
524 | initials | text | YES | | NULL | |
525 | streetnumber | varchar(10) | YES | | NULL | |
526 | streettype | varchar(50) | YES | | NULL | |
527 | address | mediumtext | NO | | NULL | |
528 | address2 | text | YES | | NULL | |
529 | city | mediumtext | NO | | NULL | |
530 | state | mediumtext | YES | | NULL | |
531 | zipcode | varchar(25) | YES | | NULL | |
532 | country | text | YES | | NULL | |
533 | email | mediumtext | YES | | NULL | |
534 | phone | text | YES | | NULL | |
535 | mobile | varchar(50) | YES | | NULL | |
536 | fax | mediumtext | YES | | NULL | |
537 | emailpro | text | YES | | NULL | |
538 | phonepro | text | YES | | NULL | |
539 | B_streetnumber | varchar(10) | YES | | NULL | |
540 | B_streettype | varchar(50) | YES | | NULL | |
541 | B_address | varchar(100) | YES | | NULL | |
542 | B_address2 | text | YES | | NULL | |
543 | B_city | mediumtext | YES | | NULL | |
544 | B_state | mediumtext | YES | | NULL | |
545 | B_zipcode | varchar(25) | YES | | NULL | |
546 | B_country | text | YES | | NULL | |
547 | B_email | text | YES | | NULL | |
548 | B_phone | mediumtext | YES | | NULL | |
549 | dateofbirth | date | YES | | NULL | |
550 | branchcode | varchar(10) | NO | MUL | | |
551 | categorycode | varchar(10) | NO | MUL | | |
552 | dateenrolled | date | YES | | NULL | |
553 | dateexpiry | date | YES | | NULL | |
554 | gonenoaddress | tinyint(1) | YES | | NULL | |
555 | lost | tinyint(1) | YES | | NULL | |
556 | debarred | tinyint(1) | YES | | NULL | |
557 | contactname | mediumtext | YES | | NULL | |
558 | contactfirstname | text | YES | | NULL | |
559 | contacttitle | text | YES | | NULL | |
560 | guarantorid | int(11) | YES | MUL | NULL | |
561 | borrowernotes | mediumtext | YES | | NULL | |
562 | relationship | varchar(100) | YES | | NULL | |
563 | ethnicity | varchar(50) | YES | | NULL | |
564 | ethnotes | varchar(255) | YES | | NULL | |
565 | sex | varchar(1) | YES | | NULL | |
566 | password | varchar(30) | YES | | NULL | |
567 | flags | int(11) | YES | | NULL | |
568 | userid | varchar(30) | YES | MUL | NULL | |
569 | opacnote | mediumtext | YES | | NULL | |
570 | contactnote | varchar(255) | YES | | NULL | |
571 | sort1 | varchar(80) | YES | | NULL | |
572 | sort2 | varchar(80) | YES | | NULL | |
573 | altcontactfirstname | varchar(255) | YES | | NULL | |
574 | altcontactsurname | varchar(255) | YES | | NULL | |
575 | altcontactaddress1 | varchar(255) | YES | | NULL | |
576 | altcontactaddress2 | varchar(255) | YES | | NULL | |
577 | altcontactaddress3 | varchar(255) | YES | | NULL | |
578 | altcontactstate | mediumtext | YES | | NULL | |
579 | altcontactzipcode | varchar(50) | YES | | NULL | |
580 | altcontactcountry | text | YES | | NULL | |
581 | altcontactphone | varchar(50) | YES | | NULL | |
582 | smsalertnumber | varchar(50) | YES | | NULL | |
583 | privacy | int(11) | NO | | 1 | |
584 +---------------------+--------------+------+-----+---------+----------------+
591 {message} Message showing patron's credit or debt
592 {noissues} Set if patron owes >$5.00
593 {GNA} Set if patron gone w/o address
594 {message} "Borrower has no valid address"
596 {LOST} Set if patron's card reported lost
597 {message} Message to this effect
599 {DBARRED} Set if patron is debarred
600 {message} Message to this effect
602 {NOTES} Set if patron has notes
603 {message} Notes about patron
604 {ODUES} Set if patron has overdue books
606 {itemlist} ref-to-array: list of overdue books
607 {itemlisttext} Text list of overdue items
608 {WAITING} Set if there are items available that the patron reserved
609 {message} Message to this effect
610 {itemlist} ref-to-array: list of available items