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