Bug 18812 - SIP Patron status does not respect OverduesBlockCirc
[koha.git] / C4 / SIP / ILS / Patron.pm
blobcf8dee415e67e423b35e58961b6e88f58f6a8dc2
2 # ILS::Patron.pm
3 #
4 # A Class for hiding the ILS's concept of the patron from the OpenSIP
5 # system
8 package C4::SIP::ILS::Patron;
10 use strict;
11 use warnings;
12 use Exporter;
13 use Carp;
15 use Sys::Syslog qw(syslog);
16 use Data::Dumper;
18 use C4::Debug;
19 use C4::Context;
20 use C4::Koha;
21 use C4::Members;
22 use C4::Reserves;
23 use C4::Items qw( GetBarcodeFromItemnumber GetItemnumbersForBiblio);
24 use C4::Auth qw(checkpw);
26 use Koha::Libraries;
28 our $kp; # koha patron
30 sub new {
31 my ($class, $patron_id) = @_;
32 my $type = ref($class) || $class;
33 my $self;
34 $kp = GetMember(cardnumber=>$patron_id) || GetMember(userid=>$patron_id);
35 $debug and warn "new Patron (GetMember): " . Dumper($kp);
36 unless (defined $kp) {
37 syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
38 return;
40 $kp = GetMemberDetails($kp->{borrowernumber});
41 $debug and warn "new Patron (GetMemberDetails): " . Dumper($kp);
42 my $pw = $kp->{password};
43 my $flags = $kp->{flags}; # or warn "Warning: No flags from patron object for '$patron_id'";
44 my $debarred = defined($kp->{flags}->{DBARRED});
45 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%{$kp->{flags}});
46 my ($day, $month, $year) = (localtime)[3,4,5];
47 my $today = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
48 my $expired = ($today gt $kp->{dateexpiry}) ? 1 : 0;
49 if ($expired) {
50 if ($kp->{opacnote} ) {
51 $kp->{opacnote} .= q{ };
53 $kp->{opacnote} .= 'PATRON EXPIRED';
55 my %ilspatron;
56 my $adr = _get_address($kp);
57 my $dob = $kp->{dateofbirth};
58 $dob and $dob =~ s/-//g; # YYYYMMDD
59 my $dexpiry = $kp->{dateexpiry};
60 $dexpiry and $dexpiry =~ s/-//g; # YYYYMMDD
61 my $fines_amount = $flags->{CHARGES}->{amount};
62 $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
63 my $fee_limit = _fee_limit();
64 my $fine_blocked = $fines_amount > $fee_limit;
65 my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" && defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
67 no warnings; # any of these $kp->{fields} being concat'd could be undef
68 %ilspatron = (
69 getmemberdetails_object => $kp,
70 name => $kp->{firstname} . " " . $kp->{surname},
71 id => $kp->{cardnumber}, # to SIP, the id is the BARCODE, not userid
72 password => $pw,
73 ptype => $kp->{categorycode}, # 'A'dult. Whatever.
74 dateexpiry => $dexpiry,
75 dateexpiry_iso => $kp->{dateexpiry},
76 birthdate => $dob,
77 birthdate_iso => $kp->{dateofbirth},
78 branchcode => $kp->{branchcode},
79 library_name => "", # only populated if needed, cached here
80 borrowernumber => $kp->{borrowernumber},
81 address => $adr,
82 home_phone => $kp->{phone},
83 email_addr => $kp->{email},
84 charge_ok => ( !$debarred && !$expired && !$fine_blocked && !$circ_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} ),
89 claims_returned => 0,
90 fines => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
91 fees => 0, # currently not distinct from fines
92 recall_overdue => 0,
93 items_billed => 0,
94 screen_msg => 'Greetings from Koha. ' . $kp->{opacnote},
95 print_line => '',
96 items => [],
97 hold_items => $flags->{WAITING}->{itemlist},
98 overdue_items => $flags->{ODUES}->{itemlist},
99 fine_items => [],
100 recall_items => [],
101 unavail_holds => [],
102 inet => ( !$debarred && !$expired ),
103 expired => $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});
124 $self = \%ilspatron;
125 $debug and warn Dumper($self);
126 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
127 bless $self, $type;
128 return $self;
132 # 0 means read-only
133 # 1 means read/write
135 my %fields = (
136 id => 0,
137 name => 0,
138 address => 0,
139 email_addr => 0,
140 home_phone => 0,
141 birthdate => 0,
142 birthdate_iso => 0,
143 dateexpiry => 0,
144 dateexpiry_iso => 0,
145 ptype => 0,
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]
151 recall_overdue => 0,
152 currency => 1,
153 fee_limit => 0,
154 screen_msg => 1,
155 print_line => 1,
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,
169 our $AUTOLOAD;
171 sub DESTROY {
172 # be cool. needed for AUTOLOAD(?)
175 sub AUTOLOAD {
176 my $self = shift;
177 my $class = ref($self) or croak "$self is not an object";
178 my $name = $AUTOLOAD;
180 $name =~ s/.*://;
182 unless (exists $fields{$name}) {
183 croak "Cannot access '$name' field of class '$class'";
186 if (@_) {
187 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
188 return $self->{$name} = shift;
189 } else {
190 return $self->{$name};
194 sub check_password {
195 my ( $self, $pwd ) = @_;
197 # you gotta give me something (at least ''), or no deal
198 return 0 unless defined $pwd;
200 # If the record has a NULL password, accept '' as match
201 return $pwd eq q{} unless $self->{password};
203 my $dbh = C4::Context->dbh;
204 my $ret = 0;
205 ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
206 return $ret;
209 # A few special cases, not in AUTOLOADed %fields
210 sub fee_amount {
211 my $self = shift;
212 if ( $self->{fines} ) {
213 return $self->{fines};
215 return;
218 sub fines_amount {
219 my $self = shift;
220 return $self->fee_amount;
223 sub language {
224 my $self = shift;
225 return $self->{language} || '000'; # Unspecified
228 sub expired {
229 my $self = shift;
230 return $self->{expired};
234 # remove the hold on item item_id from my hold queue.
235 # return true if I was holding the item, false otherwise.
237 sub drop_hold {
238 my ($self, $item_id) = @_;
239 return if !$item_id;
240 my $result = 0;
241 foreach (qw(hold_items unavail_holds)) {
242 $self->{$_} or next;
243 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
244 my $held_item = $self->{$_}[$i]->{item_id} or next;
245 if ($held_item eq $item_id) {
246 splice @{$self->{$_}}, $i, 1;
247 $result++;
251 return $result;
254 # Accessor method for array_ref values, designed to get the "start" and "end" values
255 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
257 sub x_items {
258 my $self = shift;
259 my $array_var = shift or return;
260 my ($start, $end) = @_;
262 my $item_list = [];
263 if ($self->{$array_var}) {
264 if ($start && $start > 1) {
265 --$start;
267 else {
268 $start = 0;
270 if ( $end && $end < @{$self->{$array_var}} ) {
272 else {
273 $end = @{$self->{$array_var}};
274 --$end;
276 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
279 return $item_list;
283 # List of outstanding holds placed
285 sub hold_items {
286 my $self = shift;
287 my $item_arr = $self->x_items('hold_items', @_);
288 foreach my $item (@{$item_arr}) {
289 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
291 return $item_arr;
294 sub overdue_items {
295 my $self = shift;
296 return $self->x_items('overdue_items', @_);
298 sub charged_items {
299 my $self = shift;
300 return $self->x_items('items', @_);
302 sub fine_items {
303 require Koha::Database;
304 require Template;
306 my $self = shift;
307 my $start = shift;
308 my $end = shift;
309 my $server = shift;
311 my @fees = Koha::Database->new()->schema()->resultset('Accountline')->search(
313 borrowernumber => $self->{borrowernumber},
314 amountoutstanding => { '>' => '0' },
318 $start = $start ? $start - 1 : 0;
319 $end = $end ? $end : scalar @fees - 1;
321 my $av_field_template = $server ? $server->{account}->{av_field_template} : undef;
322 $av_field_template ||= "[% accountline.description %] [% accountline.amountoutstanding | format('%.2f') %]";
324 my $tt = Template->new();
326 my @return_values;
327 for ( my $i = $start; $i <= $end; $i++ ) {
328 my $fee = $fees[$i];
330 my $output;
331 $tt->process( \$av_field_template, { accountline => $fee }, \$output );
332 push( @return_values, { barcode => $output } );
335 return \@return_values;
337 sub recall_items {
338 my $self = shift;
339 return $self->x_items('recall_items', @_);
341 sub unavail_holds {
342 my $self = shift;
343 return $self->x_items('unavail_holds', @_);
346 sub block {
347 my ($self, $card_retained, $blocked_card_msg) = @_;
348 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
349 $self->{$field} = 0;
351 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
352 # TODO: not really affecting patron record
353 return $self;
356 sub enable {
357 my $self = shift;
358 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
359 $self->{$field} = 1;
361 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
362 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
363 $self->{recall_ok}, $self->{hold_ok});
364 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
365 return $self;
368 sub inet_privileges {
369 my $self = shift;
370 return $self->{inet} ? 'Y' : 'N';
373 sub _fee_limit {
374 return C4::Context->preference('noissuescharge') || 5;
377 sub excessive_fees {
378 my $self = shift;
379 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
382 sub excessive_fines {
383 my $self = shift;
384 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
387 sub holds_blocked_by_excessive_fees {
388 my $self = shift;
389 return ( $self->fee_amount
390 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
393 sub library_name {
394 my $self = shift;
395 unless ($self->{library_name}) {
396 my $library = Koha::Libraries->find( $self->{branchcode} );
397 $self->{library_name} = $library ? $library->branchname : '';
399 return $self->{library_name};
402 # Messages
405 sub invalid_patron {
406 my $self = shift;
407 return "Please contact library staff";
410 sub charge_denied {
411 my $self = shift;
412 return "Please contact library staff";
415 sub _get_address {
416 my $patron = shift;
418 my $address = $patron->{streetnumber} || q{};
419 for my $field (qw( roaddetails address address2 city state zipcode country))
421 next unless $patron->{$field};
422 if ($address) {
423 $address .= q{ };
424 $address .= $patron->{$field};
426 else {
427 $address .= $patron->{$field};
430 return $address;
433 sub _get_outstanding_holds {
434 my $borrowernumber = shift;
435 my @hold_array = grep { !defined $_->{found} || $_->{found} ne 'W'} GetReservesFromBorrowernumber($borrowernumber);
436 foreach my $h (@hold_array) {
437 my $item;
438 if ($h->{itemnumber}) {
439 $item = $h->{itemnumber};
441 else {
442 # We need to return a barcode for the biblio so the client
443 # can request the biblio info
444 $item = ( GetItemnumbersForBiblio($h->{biblionumber}) )->[0];
446 $h->{barcode} = GetBarcodeFromItemnumber($item);
448 return \@hold_array;
452 __END__
454 =head1 EXAMPLES
456 our %patron_example = (
457 djfiander => {
458 name => "David J. Fiander",
459 id => 'djfiander',
460 password => '6789',
461 ptype => 'A', # 'A'dult. Whatever.
462 birthdate => '19640925',
463 address => '2 Meadowvale Dr. St Thomas, ON',
464 home_phone => '(519) 555 1234',
465 email_addr => 'djfiander@hotmail.com',
466 charge_ok => 1,
467 renew_ok => 1,
468 recall_ok => 0,
469 hold_ok => 1,
470 card_lost => 0,
471 claims_returned => 0,
472 fines => 100,
473 fees => 0,
474 recall_overdue => 0,
475 items_billed => 0,
476 screen_msg => '',
477 print_line => '',
478 items => [],
479 hold_items => [],
480 overdue_items => [],
481 fine_items => ['Computer Time'],
482 recall_items => [],
483 unavail_holds => [],
484 inet => 1,
488 From borrowers table:
489 +---------------------+--------------+------+-----+---------+----------------+
490 | Field | Type | Null | Key | Default | Extra |
491 +---------------------+--------------+------+-----+---------+----------------+
492 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
493 | cardnumber | varchar(16) | YES | UNI | NULL | |
494 | surname | mediumtext | NO | | NULL | |
495 | firstname | text | YES | | NULL | |
496 | title | mediumtext | YES | | NULL | |
497 | othernames | mediumtext | YES | | NULL | |
498 | initials | text | YES | | NULL | |
499 | streetnumber | varchar(10) | YES | | NULL | |
500 | streettype | varchar(50) | YES | | NULL | |
501 | address | mediumtext | NO | | NULL | |
502 | address2 | text | YES | | NULL | |
503 | city | mediumtext | NO | | NULL | |
504 | state | mediumtext | YES | | NULL | |
505 | zipcode | varchar(25) | YES | | NULL | |
506 | country | text | YES | | NULL | |
507 | email | mediumtext | YES | | NULL | |
508 | phone | text | YES | | NULL | |
509 | mobile | varchar(50) | YES | | NULL | |
510 | fax | mediumtext | YES | | NULL | |
511 | emailpro | text | YES | | NULL | |
512 | phonepro | text | YES | | NULL | |
513 | B_streetnumber | varchar(10) | YES | | NULL | |
514 | B_streettype | varchar(50) | YES | | NULL | |
515 | B_address | varchar(100) | YES | | NULL | |
516 | B_address2 | text | YES | | NULL | |
517 | B_city | mediumtext | YES | | NULL | |
518 | B_state | mediumtext | YES | | NULL | |
519 | B_zipcode | varchar(25) | YES | | NULL | |
520 | B_country | text | YES | | NULL | |
521 | B_email | text | YES | | NULL | |
522 | B_phone | mediumtext | YES | | NULL | |
523 | dateofbirth | date | YES | | NULL | |
524 | branchcode | varchar(10) | NO | MUL | | |
525 | categorycode | varchar(10) | NO | MUL | | |
526 | dateenrolled | date | YES | | NULL | |
527 | dateexpiry | date | YES | | NULL | |
528 | gonenoaddress | tinyint(1) | YES | | NULL | |
529 | lost | tinyint(1) | YES | | NULL | |
530 | debarred | tinyint(1) | YES | | NULL | |
531 | contactname | mediumtext | YES | | NULL | |
532 | contactfirstname | text | YES | | NULL | |
533 | contacttitle | text | YES | | NULL | |
534 | guarantorid | int(11) | YES | MUL | NULL | |
535 | borrowernotes | mediumtext | YES | | NULL | |
536 | relationship | varchar(100) | YES | | NULL | |
537 | ethnicity | varchar(50) | YES | | NULL | |
538 | ethnotes | varchar(255) | YES | | NULL | |
539 | sex | varchar(1) | YES | | NULL | |
540 | password | varchar(30) | YES | | NULL | |
541 | flags | int(11) | YES | | NULL | |
542 | userid | varchar(30) | YES | MUL | NULL | |
543 | opacnote | mediumtext | YES | | NULL | |
544 | contactnote | varchar(255) | YES | | NULL | |
545 | sort1 | varchar(80) | YES | | NULL | |
546 | sort2 | varchar(80) | YES | | NULL | |
547 | altcontactfirstname | varchar(255) | YES | | NULL | |
548 | altcontactsurname | varchar(255) | YES | | NULL | |
549 | altcontactaddress1 | varchar(255) | YES | | NULL | |
550 | altcontactaddress2 | varchar(255) | YES | | NULL | |
551 | altcontactaddress3 | varchar(255) | YES | | NULL | |
552 | altcontactstate | mediumtext | YES | | NULL | |
553 | altcontactzipcode | varchar(50) | YES | | NULL | |
554 | altcontactcountry | text | YES | | NULL | |
555 | altcontactphone | varchar(50) | YES | | NULL | |
556 | smsalertnumber | varchar(50) | YES | | NULL | |
557 | privacy | int(11) | NO | | 1 | |
558 +---------------------+--------------+------+-----+---------+----------------+
561 From C4::Members
563 $flags->{KEY}
564 {CHARGES}
565 {message} Message showing patron's credit or debt
566 {noissues} Set if patron owes >$5.00
567 {GNA} Set if patron gone w/o address
568 {message} "Borrower has no valid address"
569 {noissues} Set.
570 {LOST} Set if patron's card reported lost
571 {message} Message to this effect
572 {noissues} Set.
573 {DBARRED} Set if patron is debarred
574 {message} Message to this effect
575 {noissues} Set.
576 {NOTES} Set if patron has notes
577 {message} Notes about patron
578 {ODUES} Set if patron has overdue books
579 {message} "Yes"
580 {itemlist} ref-to-array: list of overdue books
581 {itemlisttext} Text list of overdue items
582 {WAITING} Set if there are items available that the patron reserved
583 {message} Message to this effect
584 {itemlist} ref-to-array: list of available items
586 =cut