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