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 = GetMember(cardnumber=>$patron_id) || GetMember(userid=>$patron_id);
36 $debug and warn "new Patron (GetMember): " . Dumper($kp);
37 unless (defined $kp) {
38 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
41 $kp = GetMember( borrowernumber => $kp->{borrowernumber});
42 $debug and warn "new Patron (GetMember): " . Dumper($kp);
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;
67 no warnings; # any of these $kp->{fields} being concat'd could be undef
69 getmemberdetails_object => $kp,
70 name => $kp->{firstname} . " " . $kp->{surname},
71 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
73 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
74 dateexpiry => $dexpiry,
75 dateexpiry_iso => $kp->{dateexpiry},
77 birthdate_iso => $kp->{dateofbirth},
78 branchcode => $kp->{branchcode},
79 library_name => "", # only populated if needed, cached here
80 borrowernumber => $kp->{borrowernumber},
82 home_phone => $kp->{phone},
83 email_addr => $kp->{email},
84 charge_ok => ( !$debarred && !$expired && !$fine_blocked),
85 renew_ok => ( !$debarred && !$expired && !$fine_blocked),
86 recall_ok => ( !$debarred && !$expired && !$fine_blocked),
87 hold_ok => ( !$debarred && !$expired && !$fine_blocked),
88 card_lost => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
90 fines => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
91 fees => 0, # currently not distinct from fines
94 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
97 hold_items => $flags->{WAITING}->{itemlist},
98 overdue_items => $flags->{ODUES}->{itemlist},
102 inet => ( !$debarred && !$expired ),
104 fee_limit => $fee_limit,
105 userid => $kp->{userid},
108 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
109 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
110 ($flags->{$_}) or next;
111 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
112 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
114 if ($flags->{$_}->{noissues}) {
115 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
116 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
121 # FIXME: populate fine_items recall_items
122 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
123 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
125 $debug and warn Dumper($self);
126 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
146 charge_ok => 0, # for patron_status[0] (inverted)
147 renew_ok => 0, # for patron_status[1] (inverted)
148 recall_ok => 0, # for patron_status[2] (inverted)
149 hold_ok => 0, # for patron_status[3] (inverted)
150 card_lost => 0, # for patron_status[4]
156 too_many_charged => 0, # for patron_status[5]
157 too_many_overdue => 0, # for patron_status[6]
158 too_many_renewal => 0, # for patron_status[7]
159 too_many_claim_return => 0, # for patron_status[8]
160 too_many_lost => 0, # for patron_status[9]
161 # excessive_fines => 0, # for patron_status[10]
162 # excessive_fees => 0, # for patron_status[11]
163 recall_overdue => 0, # for patron_status[12]
164 too_many_billed => 0, # for patron_status[13]
165 inet => 0, # EnvisionWare extension
166 getmemberdetails_object => 0,
172 # be cool. needed for AUTOLOAD(?)
177 my $class = ref($self) or croak "$self is not an object";
178 my $name = $AUTOLOAD;
182 unless (exists $fields{$name}) {
183 croak "Cannot access '$name' field of class '$class'";
187 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
188 return $self->{$name} = shift;
190 return $self->{$name};
195 my ( $self, $template ) = @_;
199 require Koha::Patrons;
201 my $tt = Template->new();
203 my $patron = Koha::Patrons->find( $self->{borrowernumber} );
206 $tt->process( \$template, { patron => $patron }, \$output );
210 return $self->{name};
215 my ( $self, $pwd ) = @_;
217 # you gotta give me something (at least ''), or no deal
218 return 0 unless defined $pwd;
220 # If the record has a NULL password, accept '' as match
221 return $pwd eq q{} unless $self->{password};
223 my $dbh = C4::Context->dbh;
225 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
229 # A few special cases, not in AUTOLOADed %fields
232 if ( $self->{fines} ) {
233 return $self->{fines};
240 return $self->fee_amount;
245 return $self->{language} || '000'; # Unspecified
250 return $self->{expired};
254 # remove the hold on item item_id from my hold queue.
255 # return true if I was holding the item, false otherwise.
258 my ($self, $item_id) = @_;
261 foreach (qw(hold_items unavail_holds)) {
263 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
264 my $held_item = $self->{$_}[$i]->{item_id} or next;
265 if ($held_item eq $item_id) {
266 splice @{$self->{$_}}, $i, 1;
274 # Accessor method for array_ref values, designed to get the "start" and "end" values
275 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
279 my $array_var = shift or return;
280 my ($start, $end) = @_;
283 if ($self->{$array_var}) {
284 if ($start && $start > 1) {
290 if ( $end && $end < @{$self->{$array_var}} ) {
293 $end = @{$self->{$array_var}};
296 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
303 # List of outstanding holds placed
307 my $item_arr = $self->x_items('hold_items', @_);
308 foreach my $item (@{$item_arr}) {
309 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
316 return $self->x_items('overdue_items', @_);
320 return $self->x_items('items', @_);
323 require Koha::Database;
331 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
333 borrowernumber => $self->{borrowernumber},
334 amountoutstanding => { '>' => '0' },
338 $start = $start ? $start - 1 : 0;
339 $end = $end ? $end : scalar @fees - 1;
341 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
342 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
344 my $tt = Template->new();
347 for ( my $i = $start; $i <= $end; $i++ ) {
351 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
352 push( @return_values, { barcode => $output } );
355 return \@return_values;
359 return $self->x_items('recall_items', @_);
363 return $self->x_items('unavail_holds', @_);
367 my ($self, $card_retained, $blocked_card_msg) = @_;
368 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
371 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
372 # TODO: not really affecting patron record
378 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
381 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
382 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
383 $self->{recall_ok}, $self->{hold_ok});
384 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
388 sub inet_privileges {
390 return $self->{inet} ? 'Y' : 'N';
394 return C4::Context->preference('noissuescharge') || 5;
399 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
402 sub excessive_fines {
404 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
407 sub holds_blocked_by_excessive_fees {
409 return ( $self->fee_amount
410 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
415 unless ($self->{library_name}) {
416 my $library = Koha::Libraries->find( $self->{branchcode} );
417 $self->{library_name} = $library ? $library->branchname : '';
419 return $self->{library_name};
427 return "Please contact library staff";
432 return "Please contact library staff";
438 my $address = $patron->{streetnumber} || q{};
439 for my $field (qw( roaddetails address address2 city state zipcode country))
441 next unless $patron->{$field};
444 $address .= $patron->{$field};
447 $address .= $patron->{$field};
453 sub _get_outstanding_holds {
454 my $borrowernumber = shift;
456 my $patron = Koha::Patrons->find( $borrowernumber );
457 my $holds = $patron->holds->search( { -or => [ { found => undef }, { found => { '!=' => 'W' } } ] } );
459 while ( my $hold = $holds->next ) {
461 if ($hold->itemnumber) {
462 $item = $hold->itemnumber;
465 # We need to return a barcode for the biblio so the client
466 # can request the biblio info
467 $item = ( GetItemnumbersForBiblio($hold->biblionumber) )->[0];
469 my $unblessed_hold = $hold->unblessed;
470 $unblessed_hold->{barcode} = GetBarcodeFromItemnumber($item);
471 push @holds, $unblessed_hold;
481 our %patron_example = (
483 name => "David J. Fiander",
486 ptype => 'A', # 'A'dult. Whatever.
487 birthdate => '19640925',
488 address => '2 Meadowvale Dr. St Thomas, ON',
489 home_phone => '(519) 555 1234',
490 email_addr => 'djfiander@hotmail.com',
496 claims_returned => 0,
506 fine_items => ['Computer Time'],
513 From borrowers table:
514 +---------------------+--------------+------+-----+---------+----------------+
515 | Field | Type | Null | Key | Default | Extra |
516 +---------------------+--------------+------+-----+---------+----------------+
517 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
518 | cardnumber | varchar(16) | YES | UNI | NULL | |
519 | surname | mediumtext | NO | | NULL | |
520 | firstname | text | YES | | NULL | |
521 | title | mediumtext | YES | | NULL | |
522 | othernames | mediumtext | YES | | NULL | |
523 | initials | text | YES | | NULL | |
524 | streetnumber | varchar(10) | YES | | NULL | |
525 | streettype | varchar(50) | YES | | NULL | |
526 | address | mediumtext | NO | | NULL | |
527 | address2 | text | YES | | NULL | |
528 | city | mediumtext | NO | | NULL | |
529 | state | mediumtext | YES | | NULL | |
530 | zipcode | varchar(25) | YES | | NULL | |
531 | country | text | YES | | NULL | |
532 | email | mediumtext | YES | | NULL | |
533 | phone | text | YES | | NULL | |
534 | mobile | varchar(50) | YES | | NULL | |
535 | fax | mediumtext | YES | | NULL | |
536 | emailpro | text | YES | | NULL | |
537 | phonepro | text | YES | | NULL | |
538 | B_streetnumber | varchar(10) | YES | | NULL | |
539 | B_streettype | varchar(50) | YES | | NULL | |
540 | B_address | varchar(100) | YES | | NULL | |
541 | B_address2 | text | YES | | NULL | |
542 | B_city | mediumtext | YES | | NULL | |
543 | B_state | mediumtext | YES | | NULL | |
544 | B_zipcode | varchar(25) | YES | | NULL | |
545 | B_country | text | YES | | NULL | |
546 | B_email | text | YES | | NULL | |
547 | B_phone | mediumtext | YES | | NULL | |
548 | dateofbirth | date | YES | | NULL | |
549 | branchcode | varchar(10) | NO | MUL | | |
550 | categorycode | varchar(10) | NO | MUL | | |
551 | dateenrolled | date | YES | | NULL | |
552 | dateexpiry | date | YES | | NULL | |
553 | gonenoaddress | tinyint(1) | YES | | NULL | |
554 | lost | tinyint(1) | YES | | NULL | |
555 | debarred | tinyint(1) | YES | | NULL | |
556 | contactname | mediumtext | YES | | NULL | |
557 | contactfirstname | text | YES | | NULL | |
558 | contacttitle | text | YES | | NULL | |
559 | guarantorid | int(11) | YES | MUL | NULL | |
560 | borrowernotes | mediumtext | YES | | NULL | |
561 | relationship | varchar(100) | YES | | NULL | |
562 | ethnicity | varchar(50) | YES | | NULL | |
563 | ethnotes | varchar(255) | YES | | NULL | |
564 | sex | varchar(1) | YES | | NULL | |
565 | password | varchar(30) | YES | | NULL | |
566 | flags | int(11) | YES | | NULL | |
567 | userid | varchar(30) | YES | MUL | NULL | |
568 | opacnote | mediumtext | YES | | NULL | |
569 | contactnote | varchar(255) | YES | | NULL | |
570 | sort1 | varchar(80) | YES | | NULL | |
571 | sort2 | varchar(80) | YES | | NULL | |
572 | altcontactfirstname | varchar(255) | YES | | NULL | |
573 | altcontactsurname | varchar(255) | YES | | NULL | |
574 | altcontactaddress1 | varchar(255) | YES | | NULL | |
575 | altcontactaddress2 | varchar(255) | YES | | NULL | |
576 | altcontactaddress3 | varchar(255) | YES | | NULL | |
577 | altcontactstate | mediumtext | YES | | NULL | |
578 | altcontactzipcode | varchar(50) | YES | | NULL | |
579 | altcontactcountry | text | YES | | NULL | |
580 | altcontactphone | varchar(50) | YES | | NULL | |
581 | smsalertnumber | varchar(50) | YES | | NULL | |
582 | privacy | int(11) | NO | | 1 | |
583 +---------------------+--------------+------+-----+---------+----------------+
590 {message} Message showing patron's credit or debt
591 {noissues} Set if patron owes >$5.00
592 {GNA} Set if patron gone w/o address
593 {message} "Borrower has no valid address"
595 {LOST} Set if patron's card reported lost
596 {message} Message to this effect
598 {DBARRED} Set if patron is debarred
599 {message} Message to this effect
601 {NOTES} Set if patron has notes
602 {message} Notes about patron
603 {ODUES} Set if patron has overdue books
605 {itemlist} ref-to-array: list of overdue books
606 {itemlisttext} Text list of overdue items
607 {WAITING} Set if there are items available that the patron reserved
608 {message} Message to this effect
609 {itemlist} ref-to-array: list of available items