Bug 7904 Change SIP modules to use standard LIB path
[koha.git] / C4 / SIP / ILS / Patron.pm
blob4e9c1521abc1a91a52da5a7d3ef8031407466314
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::Branch qw(GetBranchName);
24 use C4::Items qw( GetBarcodeFromItemnumber GetItemnumbersForBiblio);
25 use C4::Auth qw(checkpw_hash);
27 our $VERSION = 3.07.00.049;
29 our $kp; # koha patron
31 sub new {
32 my ($class, $patron_id) = @_;
33 my $type = ref($class) || $class;
34 my $self;
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);
39 return;
41 $kp = GetMemberDetails($kp->{borrowernumber});
42 $debug and warn "new Patron (GetMemberDetails): " . Dumper($kp);
43 my $pw = $kp->{password};
44 my $flags = $kp->{flags}; # or warn "Warning: No flags from patron object for '$patron_id'";
45 my $debarred = defined($kp->{flags}->{DBARRED});
46 $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%{$kp->{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;
50 if ($expired) {
51 if ($kp->{opacnote} ) {
52 $kp->{opacnote} .= q{ };
54 $kp->{opacnote} .= 'PATRON EXPIRED';
56 my %ilspatron;
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
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),
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,
107 $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
108 for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
109 ($flags->{$_}) or next;
110 if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
111 $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message}; # show all but internal NOTES
113 if ($flags->{$_}->{noissues}) {
114 foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
115 $ilspatron{$toggle} = 0; # if we get noissues, disable everything
120 # FIXME: populate fine_items recall_items
121 $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
122 $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
123 $self = \%ilspatron;
124 $debug and warn Dumper($self);
125 syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
126 bless $self, $type;
127 return $self;
131 # 0 means read-only
132 # 1 means read/write
134 my %fields = (
135 id => 0,
136 name => 0,
137 address => 0,
138 email_addr => 0,
139 home_phone => 0,
140 birthdate => 0,
141 birthdate_iso => 0,
142 dateexpiry => 0,
143 dateexpiry_iso => 0,
144 ptype => 0,
145 charge_ok => 0, # for patron_status[0] (inverted)
146 renew_ok => 0, # for patron_status[1] (inverted)
147 recall_ok => 0, # for patron_status[2] (inverted)
148 hold_ok => 0, # for patron_status[3] (inverted)
149 card_lost => 0, # for patron_status[4]
150 recall_overdue => 0,
151 currency => 1,
152 fee_limit => 0,
153 screen_msg => 1,
154 print_line => 1,
155 too_many_charged => 0, # for patron_status[5]
156 too_many_overdue => 0, # for patron_status[6]
157 too_many_renewal => 0, # for patron_status[7]
158 too_many_claim_return => 0, # for patron_status[8]
159 too_many_lost => 0, # for patron_status[9]
160 # excessive_fines => 0, # for patron_status[10]
161 # excessive_fees => 0, # for patron_status[11]
162 recall_overdue => 0, # for patron_status[12]
163 too_many_billed => 0, # for patron_status[13]
164 inet => 0, # EnvisionWare extension
165 getmemberdetails_object => 0,
168 our $AUTOLOAD;
170 sub DESTROY {
171 # be cool. needed for AUTOLOAD(?)
174 sub AUTOLOAD {
175 my $self = shift;
176 my $class = ref($self) or croak "$self is not an object";
177 my $name = $AUTOLOAD;
179 $name =~ s/.*://;
181 unless (exists $fields{$name}) {
182 croak "Cannot access '$name' field of class '$class'";
185 if (@_) {
186 $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
187 return $self->{$name} = shift;
188 } else {
189 return $self->{$name};
193 sub check_password {
194 my ($self, $pwd) = @_;
195 defined $pwd or return 0; # you gotta give me something (at least ''), or no deal
197 my $hashed_pwd = $self->{password};
198 defined $hashed_pwd or return $pwd eq ''; # if the record has a NULL password, accept '' as match
200 # warn sprintf "check_password for %s: '%s' vs. '%s'",($self->{name}||''),($self->{password}||''),($pwd||'');
201 return checkpw_hash($pwd, $hashed_pwd);
204 # A few special cases, not in AUTOLOADed %fields
205 sub fee_amount {
206 my $self = shift;
207 if ( $self->{fines} ) {
208 return $self->{fines};
210 return;
213 sub fines_amount {
214 my $self = shift;
215 return $self->fee_amount;
218 sub language {
219 my $self = shift;
220 return $self->{language} || '000'; # Unspecified
223 sub expired {
224 my $self = shift;
225 return $self->{expired};
229 # remove the hold on item item_id from my hold queue.
230 # return true if I was holding the item, false otherwise.
232 sub drop_hold {
233 my ($self, $item_id) = @_;
234 return if !$item_id;
235 my $result = 0;
236 foreach (qw(hold_items unavail_holds)) {
237 $self->{$_} or next;
238 for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
239 my $held_item = $self->{$_}[$i]->{item_id} or next;
240 if ($held_item eq $item_id) {
241 splice @{$self->{$_}}, $i, 1;
242 $result++;
246 return $result;
249 # Accessor method for array_ref values, designed to get the "start" and "end" values
250 # from the SIP request. Note those incoming values are 1-indexed, not 0-indexed.
252 sub x_items {
253 my $self = shift;
254 my $array_var = shift or return;
255 my ($start, $end) = @_;
257 my $item_list = [];
258 if ($self->{$array_var}) {
259 if ($start && $start > 1) {
260 --$start;
262 else {
263 $start = 0;
265 if ( $end && $end < @{$self->{$array_var}} ) {
267 else {
268 $end = @{$self->{$array_var}};
269 --$end;
271 @{$item_list} = @{$self->{$array_var}}[ $start .. $end ];
274 return $item_list;
278 # List of outstanding holds placed
280 sub hold_items {
281 my $self = shift;
282 my $item_arr = $self->x_items('hold_items', @_);
283 foreach my $item (@{$item_arr}) {
284 $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
286 return $item_arr;
289 sub overdue_items {
290 my $self = shift;
291 return $self->x_items('overdue_items', @_);
293 sub charged_items {
294 my $self = shift;
295 return $self->x_items('items', @_);
297 sub fine_items {
298 my $self = shift;
299 return $self->x_items('fine_items', @_);
301 sub recall_items {
302 my $self = shift;
303 return $self->x_items('recall_items', @_);
305 sub unavail_holds {
306 my $self = shift;
307 return $self->x_items('unavail_holds', @_);
310 sub block {
311 my ($self, $card_retained, $blocked_card_msg) = @_;
312 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
313 $self->{$field} = 0;
315 $self->{screen_msg} = "Block feature not implemented"; # $blocked_card_msg || "Card Blocked. Please contact library staff";
316 # TODO: not really affecting patron record
317 return $self;
320 sub enable {
321 my $self = shift;
322 foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
323 $self->{$field} = 1;
325 syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
326 $self->{id}, $self->{charge_ok}, $self->{renew_ok},
327 $self->{recall_ok}, $self->{hold_ok});
328 $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored."; # TODO: not really affecting patron record
329 return $self;
332 sub inet_privileges {
333 my $self = shift;
334 return $self->{inet} ? 'Y' : 'N';
337 sub _fee_limit {
338 return C4::Context->preference('noissuescharge') || 5;
341 sub excessive_fees {
342 my $self = shift;
343 return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
346 sub excessive_fines {
347 my $self = shift;
348 return $self->excessive_fees; # excessive_fines is the same thing as excessive_fees for Koha
351 sub holds_blocked_by_excessive_fees {
352 my $self = shift;
353 return ( $self->fee_amount
354 && $self->fee_amount > C4::Context->preference("maxoutstanding") );
357 sub library_name {
358 my $self = shift;
359 unless ($self->{library_name}) {
360 $self->{library_name} = GetBranchName($self->{branchcode});
362 return $self->{library_name};
365 # Messages
368 sub invalid_patron {
369 my $self = shift;
370 return "Please contact library staff";
373 sub charge_denied {
374 my $self = shift;
375 return "Please contact library staff";
378 sub _get_address {
379 my $patron = shift;
381 my $address = $patron->{streetnumber} || q{};
382 for my $field (qw( roaddetails address address2 city state zipcode country))
384 next unless $patron->{$field};
385 if ($address) {
386 $address .= q{ };
387 $address .= $patron->{$field};
389 else {
390 $address .= $patron->{$field};
393 return $address;
396 sub _get_outstanding_holds {
397 my $borrowernumber = shift;
398 my @hold_array = grep { !defined $_->{found} || $_->{found} ne 'W'} GetReservesFromBorrowernumber($borrowernumber);
399 foreach my $h (@hold_array) {
400 my $item;
401 if ($h->{itemnumber}) {
402 $item = $h->{itemnumber};
404 else {
405 # We need to return a barcode for the biblio so the client
406 # can request the biblio info
407 $item = ( GetItemnumbersForBiblio($h->{biblionumber}) )->[0];
409 $h->{barcode} = GetBarcodeFromItemnumber($item);
411 return \@hold_array;
415 __END__
417 =head1 EXAMPLES
419 our %patron_example = (
420 djfiander => {
421 name => "David J. Fiander",
422 id => 'djfiander',
423 password => '6789',
424 ptype => 'A', # 'A'dult. Whatever.
425 birthdate => '19640925',
426 address => '2 Meadowvale Dr. St Thomas, ON',
427 home_phone => '(519) 555 1234',
428 email_addr => 'djfiander@hotmail.com',
429 charge_ok => 1,
430 renew_ok => 1,
431 recall_ok => 0,
432 hold_ok => 1,
433 card_lost => 0,
434 claims_returned => 0,
435 fines => 100,
436 fees => 0,
437 recall_overdue => 0,
438 items_billed => 0,
439 screen_msg => '',
440 print_line => '',
441 items => [],
442 hold_items => [],
443 overdue_items => [],
444 fine_items => ['Computer Time'],
445 recall_items => [],
446 unavail_holds => [],
447 inet => 1,
451 From borrowers table:
452 +---------------------+--------------+------+-----+---------+----------------+
453 | Field | Type | Null | Key | Default | Extra |
454 +---------------------+--------------+------+-----+---------+----------------+
455 | borrowernumber | int(11) | NO | PRI | NULL | auto_increment |
456 | cardnumber | varchar(16) | YES | UNI | NULL | |
457 | surname | mediumtext | NO | | NULL | |
458 | firstname | text | YES | | NULL | |
459 | title | mediumtext | YES | | NULL | |
460 | othernames | mediumtext | YES | | NULL | |
461 | initials | text | YES | | NULL | |
462 | streetnumber | varchar(10) | YES | | NULL | |
463 | streettype | varchar(50) | YES | | NULL | |
464 | address | mediumtext | NO | | NULL | |
465 | address2 | text | YES | | NULL | |
466 | city | mediumtext | NO | | NULL | |
467 | state | mediumtext | YES | | NULL | |
468 | zipcode | varchar(25) | YES | | NULL | |
469 | country | text | YES | | NULL | |
470 | email | mediumtext | YES | | NULL | |
471 | phone | text | YES | | NULL | |
472 | mobile | varchar(50) | YES | | NULL | |
473 | fax | mediumtext | YES | | NULL | |
474 | emailpro | text | YES | | NULL | |
475 | phonepro | text | YES | | NULL | |
476 | B_streetnumber | varchar(10) | YES | | NULL | |
477 | B_streettype | varchar(50) | YES | | NULL | |
478 | B_address | varchar(100) | YES | | NULL | |
479 | B_address2 | text | YES | | NULL | |
480 | B_city | mediumtext | YES | | NULL | |
481 | B_state | mediumtext | YES | | NULL | |
482 | B_zipcode | varchar(25) | YES | | NULL | |
483 | B_country | text | YES | | NULL | |
484 | B_email | text | YES | | NULL | |
485 | B_phone | mediumtext | YES | | NULL | |
486 | dateofbirth | date | YES | | NULL | |
487 | branchcode | varchar(10) | NO | MUL | | |
488 | categorycode | varchar(10) | NO | MUL | | |
489 | dateenrolled | date | YES | | NULL | |
490 | dateexpiry | date | YES | | NULL | |
491 | gonenoaddress | tinyint(1) | YES | | NULL | |
492 | lost | tinyint(1) | YES | | NULL | |
493 | debarred | tinyint(1) | YES | | NULL | |
494 | contactname | mediumtext | YES | | NULL | |
495 | contactfirstname | text | YES | | NULL | |
496 | contacttitle | text | YES | | NULL | |
497 | guarantorid | int(11) | YES | MUL | NULL | |
498 | borrowernotes | mediumtext | YES | | NULL | |
499 | relationship | varchar(100) | YES | | NULL | |
500 | ethnicity | varchar(50) | YES | | NULL | |
501 | ethnotes | varchar(255) | YES | | NULL | |
502 | sex | varchar(1) | YES | | NULL | |
503 | password | varchar(30) | YES | | NULL | |
504 | flags | int(11) | YES | | NULL | |
505 | userid | varchar(30) | YES | MUL | NULL | |
506 | opacnote | mediumtext | YES | | NULL | |
507 | contactnote | varchar(255) | YES | | NULL | |
508 | sort1 | varchar(80) | YES | | NULL | |
509 | sort2 | varchar(80) | YES | | NULL | |
510 | altcontactfirstname | varchar(255) | YES | | NULL | |
511 | altcontactsurname | varchar(255) | YES | | NULL | |
512 | altcontactaddress1 | varchar(255) | YES | | NULL | |
513 | altcontactaddress2 | varchar(255) | YES | | NULL | |
514 | altcontactaddress3 | varchar(255) | YES | | NULL | |
515 | altcontactstate | mediumtext | YES | | NULL | |
516 | altcontactzipcode | varchar(50) | YES | | NULL | |
517 | altcontactcountry | text | YES | | NULL | |
518 | altcontactphone | varchar(50) | YES | | NULL | |
519 | smsalertnumber | varchar(50) | YES | | NULL | |
520 | privacy | int(11) | NO | | 1 | |
521 +---------------------+--------------+------+-----+---------+----------------+
524 From C4::Members
526 $flags->{KEY}
527 {CHARGES}
528 {message} Message showing patron's credit or debt
529 {noissues} Set if patron owes >$5.00
530 {GNA} Set if patron gone w/o address
531 {message} "Borrower has no valid address"
532 {noissues} Set.
533 {LOST} Set if patron's card reported lost
534 {message} Message to this effect
535 {noissues} Set.
536 {DBARRED} Set if patron is debarred
537 {message} Message to this effect
538 {noissues} Set.
539 {NOTES} Set if patron has notes
540 {message} Notes about patron
541 {ODUES} Set if patron has overdue books
542 {message} "Yes"
543 {itemlist} ref-to-array: list of overdue books
544 {itemlisttext} Text list of overdue items
545 {WAITING} Set if there are items available that the patron reserved
546 {message} Message to this effect
547 {itemlist} ref-to-array: list of available items
549 =cut