Bug 9343: handles the new format of Debarred
[koha-equinox.git] / C4 / SIP / ILS / Patron.pm
1 #
2 # ILS::Patron.pm
3
4 # A Class for hiding the ILS's concept of the patron from the OpenSIP
5 # system
6 #
7
8 package ILS::Patron;
9
10 use strict;
11 use warnings;
12 use Exporter;
13 use Carp;
14
15 use Sys::Syslog qw(syslog);
16 use Data::Dumper;
17
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 Digest::MD5 qw(md5_base64);
25
26 our $VERSION = 3.07.00.049;
27
28 our $kp;    # koha patron
29
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;
39     }
40     $kp = GetMemberDetails($kp->{borrowernumber});
41     $debug and warn "new Patron (GetMemberDetails): " . Dumper($kp);
42     my $pw        = $kp->{password};  ### FIXME - md5hash -- deal with .
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{ };
52         }
53         $kp->{opacnote} .= 'PATRON EXPIRED';
54     }
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     {
64     no warnings;    # any of these $kp->{fields} being concat'd could be undef
65     %ilspatron = (
66         getmemberdetails_object => $kp,
67         name => $kp->{firstname} . " " . $kp->{surname},
68         id   => $kp->{cardnumber},    # to SIP, the id is the BARCODE, not userid
69         password        => $pw,
70         ptype           => $kp->{categorycode},     # 'A'dult.  Whatever.
71         dateexpiry      => $dexpiry,
72         dateexpiry_iso  => $kp->{dateexpiry},
73         birthdate       => $dob,
74         birthdate_iso   => $kp->{dateofbirth},
75         branchcode      => $kp->{branchcode},
76         library_name    => "",                      # only populated if needed, cached here
77         borrowernumber  => $kp->{borrowernumber},
78         address         => $adr,
79         home_phone      => $kp->{phone},
80         email_addr      => $kp->{email},
81         charge_ok       => ( !$debarred && !$expired ),
82         renew_ok        => ( !$debarred && !$expired ),
83         recall_ok       => ( !$debarred && !$expired ),
84         hold_ok         => ( !$debarred && !$expired ),
85         card_lost       => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
86         claims_returned => 0,
87         fines           => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
88         fees            => 0,             # currently not distinct from fines
89         recall_overdue  => 0,
90         items_billed    => 0,
91         screen_msg      => 'Greetings from Koha. ' . $kp->{opacnote},
92         print_line      => '',
93         items           => [],
94         hold_items      => $flags->{WAITING}{itemlist},
95         overdue_items   => $flags->{ODUES}{itemlist},
96         fine_items      => [],
97         recall_items    => [],
98         unavail_holds   => [],
99         inet            => ( !$debarred && !$expired ),
100         expired         => $expired,
101     );
102     }
103     $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
104     for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
105         ($flags->{$_}) or next;
106         if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
107             $ilspatron{screen_msg} .= " -- " . $flags->{$_}->{message};  # show all but internal NOTES
108         }
109         if ($flags->{$_}->{noissues}) {
110             foreach my $toggle (qw(charge_ok renew_ok recall_ok hold_ok inet)) {
111                 $ilspatron{$toggle} = 0;    # if we get noissues, disable everything
112             }
113         }
114     }
115
116     # FIXME: populate fine_items recall_items
117 #   $ilspatron{hold_items}    = (GetReservesFromBorrowernumber($kp->{borrowernumber},'F'));
118     $ilspatron{unavail_holds} = [(GetReservesFromBorrowernumber($kp->{borrowernumber}))];
119     $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
120     $self = \%ilspatron;
121     $debug and warn Dumper($self);
122     syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
123     bless $self, $type;
124     return $self;
125 }
126
127
128 # 0 means read-only
129 # 1 means read/write
130
131 my %fields = (
132     id                      => 0,
133     name                    => 0,
134     address                 => 0,
135     email_addr              => 0,
136     home_phone              => 0,
137     birthdate               => 0,
138     birthdate_iso           => 0,
139     dateexpiry              => 0,
140     dateexpiry_iso          => 0,
141     ptype                   => 0,
142     charge_ok               => 0,   # for patron_status[0] (inverted)
143     renew_ok                => 0,   # for patron_status[1] (inverted)
144     recall_ok               => 0,   # for patron_status[2] (inverted)
145     hold_ok                 => 0,   # for patron_status[3] (inverted)
146     card_lost               => 0,   # for patron_status[4]
147     recall_overdue          => 0,
148     currency                => 1,
149 #   fee_limit               => 0,
150     screen_msg              => 1,
151     print_line              => 1,
152     too_many_charged        => 0,   # for patron_status[5]
153     too_many_overdue        => 0,   # for patron_status[6]
154     too_many_renewal        => 0,   # for patron_status[7]
155     too_many_claim_return   => 0,   # for patron_status[8]
156     too_many_lost           => 0,   # for patron_status[9]
157 #   excessive_fines         => 0,   # for patron_status[10]
158 #   excessive_fees          => 0,   # for patron_status[11]
159     recall_overdue          => 0,   # for patron_status[12]
160     too_many_billed         => 0,   # for patron_status[13]
161     inet                    => 0,   # EnvisionWare extension
162     getmemberdetails_object => 0,
163 );
164
165 our $AUTOLOAD;
166
167 sub DESTROY {
168     # be cool.  needed for AUTOLOAD(?)
169 }
170
171 sub AUTOLOAD {
172     my $self = shift;
173     my $class = ref($self) or croak "$self is not an object";
174     my $name = $AUTOLOAD;
175
176     $name =~ s/.*://;
177
178     unless (exists $fields{$name}) {
179         croak "Cannot access '$name' field of class '$class'";
180     }
181
182     if (@_) {
183         $fields{$name} or croak "Field '$name' of class '$class' is READ ONLY.";
184         return $self->{$name} = shift;
185     } else {
186         return $self->{$name};
187     }
188 }
189
190 sub check_password {
191     my ($self, $pwd) = @_;
192     my $md5pwd = $self->{password};
193     # warn sprintf "check_password for %s: '%s' vs. '%s'",($self->{name}||''),($self->{password}||''),($pwd||'');
194     (defined $pwd   ) or return 0;        # you gotta give me something (at least ''), or no deal
195     (defined $md5pwd) or return($pwd eq '');    # if the record has a NULL password, accept '' as match
196     return (md5_base64($pwd) eq $md5pwd);
197 }
198
199 # A few special cases, not in AUTOLOADed %fields
200 sub fee_amount {
201     my $self = shift;
202     if ( $self->{fines} ) {
203         return $self->{fines};
204     }
205     return;
206 }
207
208 sub fines_amount {
209     my $self = shift;
210     return $self->fee_amount;
211 }
212
213 sub language {
214     my $self = shift;
215     return $self->{language} || '000'; # Unspecified
216 }
217
218 sub expired {
219     my $self = shift;
220     return $self->{expired};
221 }
222
223 #
224 # remove the hold on item item_id from my hold queue.
225 # return true if I was holding the item, false otherwise.
226
227 sub drop_hold {
228     my ($self, $item_id) = @_;
229     return if !$item_id;
230     my $result = 0;
231     foreach (qw(hold_items unavail_holds)) {
232         $self->{$_} or next;
233         for (my $i = 0; $i < scalar @{$self->{$_}}; $i++) {
234             my $held_item = $self->{$_}[$i]->{item_id} or next;
235             if ($held_item eq $item_id) {
236                 splice @{$self->{$_}}, $i, 1;
237                 $result++;
238             }
239         }
240     }
241     return $result;
242 }
243
244 # Accessor method for array_ref values, designed to get the "start" and "end" values
245 # from the SIP request.  Note those incoming values are 1-indexed, not 0-indexed.
246 #
247 sub x_items {
248     my $self      = shift;
249     my $array_var = shift or return;
250     my ($start, $end) = @_;
251     $self->{$array_var} or return [];
252     $start = 1 unless defined($start);
253     $end   = scalar @{$self->{$array_var}} unless defined($end);
254     # syslog("LOG_DEBUG", "$array_var: start = %d, end = %d; items(%s)", $start, $end, join(', ', @{$self->{items}}));
255
256     return [@{$self->{$array_var}}[$start-1 .. $end-1]];
257 }
258
259 #
260 # List of outstanding holds placed
261 #
262 sub hold_items {
263     my $self = shift;
264     return $self->x_items('hold_items', @_);
265 }
266
267 sub overdue_items {
268     my $self = shift;
269     return $self->x_items('overdue_items', @_);
270 }
271 sub charged_items {
272     my $self = shift;
273     return $self->x_items('items', @_);
274 }
275 sub fine_items {
276     my $self = shift;
277     return $self->x_items('fine_items', @_);
278 }
279 sub recall_items {
280     my $self = shift;
281     return $self->x_items('recall_items', @_);
282 }
283 sub unavail_holds {
284     my $self = shift;
285     return $self->x_items('unavail_holds', @_);
286 }
287
288 sub block {
289     my ($self, $card_retained, $blocked_card_msg) = @_;
290     foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
291         $self->{$field} = 0;
292     }
293     $self->{screen_msg} = "Block feature not implemented";  # $blocked_card_msg || "Card Blocked.  Please contact library staff";
294     # TODO: not really affecting patron record
295     return $self;
296 }
297
298 sub enable {
299     my $self = shift;
300     foreach my $field ('charge_ok', 'renew_ok', 'recall_ok', 'hold_ok', 'inet') {
301         $self->{$field} = 1;
302     }
303     syslog("LOG_DEBUG", "Patron(%s)->enable: charge: %s, renew:%s, recall:%s, hold:%s",
304        $self->{id}, $self->{charge_ok}, $self->{renew_ok},
305        $self->{recall_ok}, $self->{hold_ok});
306     $self->{screen_msg} = "Enable feature not implemented."; # "All privileges restored.";   # TODO: not really affecting patron record
307     return $self;
308 }
309
310 sub inet_privileges {
311     my $self = shift;
312     return $self->{inet} ? 'Y' : 'N';
313 }
314
315 sub fee_limit {
316     my $self = shift;
317     return C4::Context->preference("noissuescharge") || 5;
318 }
319
320 sub excessive_fees {
321     my $self = shift;
322     return ($self->fee_amount and $self->fee_amount > $self->fee_limit);
323 }
324 sub excessive_fines {
325     my $self = shift;
326     return $self->excessive_fees;   # excessive_fines is the same thing as excessive_fees for Koha
327 }
328     
329 sub library_name {
330     my $self = shift;
331     unless ($self->{library_name}) {
332         $self->{library_name} = GetBranchName($self->{branchcode});
333     }
334     return $self->{library_name};
335 }
336 #
337 # Messages
338 #
339
340 sub invalid_patron {
341     my $self = shift;
342     return "Please contact library staff";
343 }
344
345 sub charge_denied {
346     my $self = shift;
347     return "Please contact library staff";
348 }
349
350 sub _get_address {
351     my $patron = shift;
352
353     my $address = $patron->{streetnumber} || q{};
354     for my $field (qw( roaddetails address address2 city state zipcode country))
355     {
356         next unless $patron->{$field};
357         if ($address) {
358             $address .= q{ };
359             $address .= $patron->{$field};
360         }
361         else {
362             $address .= $patron->{$field};
363         }
364     }
365     return $address;
366 }
367
368 1;
369 __END__
370
371 =head1 EXAMPLES
372
373   our %patron_example = (
374           djfiander => {
375               name => "David J. Fiander",
376               id => 'djfiander',
377               password => '6789',
378               ptype => 'A', # 'A'dult.  Whatever.
379               birthdate => '19640925',
380               address => '2 Meadowvale Dr. St Thomas, ON',
381               home_phone => '(519) 555 1234',
382               email_addr => 'djfiander@hotmail.com',
383               charge_ok => 1,
384               renew_ok => 1,
385               recall_ok => 0,
386               hold_ok => 1,
387               card_lost => 0,
388               claims_returned => 0,
389               fines => 100,
390               fees => 0,
391               recall_overdue => 0,
392               items_billed => 0,
393               screen_msg => '',
394               print_line => '',
395               items => [],
396               hold_items => [],
397               overdue_items => [],
398               fine_items => ['Computer Time'],
399               recall_items => [],
400               unavail_holds => [],
401               inet => 1,
402           },
403   );
404
405  From borrowers table:
406 +---------------------+--------------+------+-----+---------+----------------+
407 | Field               | Type         | Null | Key | Default | Extra          |
408 +---------------------+--------------+------+-----+---------+----------------+
409 | borrowernumber      | int(11)      | NO   | PRI | NULL    | auto_increment |
410 | cardnumber          | varchar(16)  | YES  | UNI | NULL    |                |
411 | surname             | mediumtext   | NO   |     | NULL    |                |
412 | firstname           | text         | YES  |     | NULL    |                |
413 | title               | mediumtext   | YES  |     | NULL    |                |
414 | othernames          | mediumtext   | YES  |     | NULL    |                |
415 | initials            | text         | YES  |     | NULL    |                |
416 | streetnumber        | varchar(10)  | YES  |     | NULL    |                |
417 | streettype          | varchar(50)  | YES  |     | NULL    |                |
418 | address             | mediumtext   | NO   |     | NULL    |                |
419 | address2            | text         | YES  |     | NULL    |                |
420 | city                | mediumtext   | NO   |     | NULL    |                |
421 | state               | mediumtext   | YES  |     | NULL    |                |
422 | zipcode             | varchar(25)  | YES  |     | NULL    |                |
423 | country             | text         | YES  |     | NULL    |                |
424 | email               | mediumtext   | YES  |     | NULL    |                |
425 | phone               | text         | YES  |     | NULL    |                |
426 | mobile              | varchar(50)  | YES  |     | NULL    |                |
427 | fax                 | mediumtext   | YES  |     | NULL    |                |
428 | emailpro            | text         | YES  |     | NULL    |                |
429 | phonepro            | text         | YES  |     | NULL    |                |
430 | B_streetnumber      | varchar(10)  | YES  |     | NULL    |                |
431 | B_streettype        | varchar(50)  | YES  |     | NULL    |                |
432 | B_address           | varchar(100) | YES  |     | NULL    |                |
433 | B_address2          | text         | YES  |     | NULL    |                |
434 | B_city              | mediumtext   | YES  |     | NULL    |                |
435 | B_state             | mediumtext   | YES  |     | NULL    |                |
436 | B_zipcode           | varchar(25)  | YES  |     | NULL    |                |
437 | B_country           | text         | YES  |     | NULL    |                |
438 | B_email             | text         | YES  |     | NULL    |                |
439 | B_phone             | mediumtext   | YES  |     | NULL    |                |
440 | dateofbirth         | date         | YES  |     | NULL    |                |
441 | branchcode          | varchar(10)  | NO   | MUL |         |                |
442 | categorycode        | varchar(10)  | NO   | MUL |         |                |
443 | dateenrolled        | date         | YES  |     | NULL    |                |
444 | dateexpiry          | date         | YES  |     | NULL    |                |
445 | gonenoaddress       | tinyint(1)   | YES  |     | NULL    |                |
446 | lost                | tinyint(1)   | YES  |     | NULL    |                |
447 | debarred            | tinyint(1)   | YES  |     | NULL    |                |
448 | contactname         | mediumtext   | YES  |     | NULL    |                |
449 | contactfirstname    | text         | YES  |     | NULL    |                |
450 | contacttitle        | text         | YES  |     | NULL    |                |
451 | guarantorid         | int(11)      | YES  | MUL | NULL    |                |
452 | borrowernotes       | mediumtext   | YES  |     | NULL    |                |
453 | relationship        | varchar(100) | YES  |     | NULL    |                |
454 | ethnicity           | varchar(50)  | YES  |     | NULL    |                |
455 | ethnotes            | varchar(255) | YES  |     | NULL    |                |
456 | sex                 | varchar(1)   | YES  |     | NULL    |                |
457 | password            | varchar(30)  | YES  |     | NULL    |                |
458 | flags               | int(11)      | YES  |     | NULL    |                |
459 | userid              | varchar(30)  | YES  | MUL | NULL    |                |
460 | opacnote            | mediumtext   | YES  |     | NULL    |                |
461 | contactnote         | varchar(255) | YES  |     | NULL    |                |
462 | sort1               | varchar(80)  | YES  |     | NULL    |                |
463 | sort2               | varchar(80)  | YES  |     | NULL    |                |
464 | altcontactfirstname | varchar(255) | YES  |     | NULL    |                |
465 | altcontactsurname   | varchar(255) | YES  |     | NULL    |                |
466 | altcontactaddress1  | varchar(255) | YES  |     | NULL    |                |
467 | altcontactaddress2  | varchar(255) | YES  |     | NULL    |                |
468 | altcontactaddress3  | varchar(255) | YES  |     | NULL    |                |
469 | altcontactstate     | mediumtext   | YES  |     | NULL    |                |
470 | altcontactzipcode   | varchar(50)  | YES  |     | NULL    |                |
471 | altcontactcountry   | text         | YES  |     | NULL    |                |
472 | altcontactphone     | varchar(50)  | YES  |     | NULL    |                |
473 | smsalertnumber      | varchar(50)  | YES  |     | NULL    |                |
474 | privacy             | int(11)      | NO   |     | 1       |                |
475 +---------------------+--------------+------+-----+---------+----------------+
476
477
478  From C4::Members
479
480  $flags->{KEY}
481  {CHARGES}
482     {message}     Message showing patron's credit or debt
483     {noissues}    Set if patron owes >$5.00
484  {GNA}             Set if patron gone w/o address
485     {message}     "Borrower has no valid address"
486     {noissues}    Set.
487  {LOST}            Set if patron's card reported lost
488     {message}     Message to this effect
489     {noissues}    Set.
490  {DBARRED}         Set if patron is debarred
491     {message}     Message to this effect
492     {noissues}    Set.
493  {NOTES}           Set if patron has notes
494     {message}     Notes about patron
495  {ODUES}           Set if patron has overdue books
496     {message}     "Yes"
497     {itemlist}    ref-to-array: list of overdue books
498     {itemlisttext}    Text list of overdue items
499  {WAITING}         Set if there are items available that the patron reserved
500     {message}     Message to this effect
501     {itemlist}    ref-to-array: list of available items
502
503 =cut
504