Bug 15253: Add Koha::Logger based logging for SIP2
[koha-equinox.git] / C4 / SIP / ILS / Patron.pm
index 64ef4f6..17c9081 100644 (file)
@@ -12,38 +12,44 @@ use warnings;
 use Exporter;
 use Carp;
 
-use Sys::Syslog qw(syslog);
+use C4::SIP::Sip qw(syslog);
 use Data::Dumper;
 
+use C4::SIP::Sip qw(add_field);
+
 use C4::Debug;
 use C4::Context;
 use C4::Koha;
 use C4::Members;
 use C4::Reserves;
-use C4::Items qw( GetBarcodeFromItemnumber GetItemnumbersForBiblio);
 use C4::Auth qw(checkpw);
 
+use Koha::Items;
 use Koha::Libraries;
 use Koha::Patrons;
 
 our $kp;    # koha patron
 
+=head1 Methods
+
+=cut
+
 sub new {
     my ($class, $patron_id) = @_;
     my $type = ref($class) || $class;
     my $self;
-    $kp = GetMember(cardnumber=>$patron_id) || GetMember(userid=>$patron_id);
-    $debug and warn "new Patron (GetMember): " . Dumper($kp);
-    unless (defined $kp) {
+    my $patron = Koha::Patrons->find( { cardnumber => $patron_id } )
+      || Koha::Patrons->find( { userid => $patron_id } );
+    $debug and warn "new Patron: " . Dumper($patron->unblessed) if $patron;
+    unless ($patron) {
         syslog("LOG_DEBUG", "new ILS::Patron(%s): no such patron", $patron_id);
         return;
     }
-    $kp = GetMember( borrowernumber => $kp->{borrowernumber});
-    $debug and warn "new Patron (GetMember): " . Dumper($kp);
+    $kp = $patron->unblessed;
     my $pw        = $kp->{password};
     my $flags     = C4::Members::patronflags( $kp );
-    my $debarred  = defined($flags->{DBARRED});
-    $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')) . Dumper(%$flags);
+    my $debarred  = $patron->is_debarred;
+    $debug and warn sprintf("Debarred = %s : ", ($debarred||'undef')); # Do we need more debug info here?
     my ($day, $month, $year) = (localtime)[3,4,5];
     my $today    = sprintf '%04d-%02d-%02d', $year+1900, $month+1, $day;
     my $expired  = ($today gt $kp->{dateexpiry}) ? 1 : 0;
@@ -59,14 +65,14 @@ sub new {
     $dob and $dob =~ s/-//g;    # YYYYMMDD
     my $dexpiry     = $kp->{dateexpiry};
     $dexpiry and $dexpiry =~ s/-//g;    # YYYYMMDD
-    my $fines_amount = $flags->{CHARGES}->{amount};
+    my $fines_amount = $patron->account->balance;
     $fines_amount = ($fines_amount and $fines_amount > 0) ? $fines_amount : 0;
     my $fee_limit = _fee_limit();
     my $fine_blocked = $fines_amount > $fee_limit;
+    my $circ_blocked =( C4::Context->preference('OverduesBlockCirc') ne "noblock" &&  defined $flags->{ODUES}->{itemlist} ) ? 1 : 0;
     {
     no warnings;    # any of these $kp->{fields} being concat'd could be undef
     %ilspatron = (
-        getmemberdetails_object => $kp,
         name => $kp->{firstname} . " " . $kp->{surname},
         id   => $kp->{cardnumber},    # to SIP, the id is the BARCODE, not userid
         password        => $pw,
@@ -81,13 +87,13 @@ sub new {
         address         => $adr,
         home_phone      => $kp->{phone},
         email_addr      => $kp->{email},
-        charge_ok       => ( !$debarred && !$expired && !$fine_blocked),
+        charge_ok       => ( !$debarred && !$expired && !$fine_blocked && !$circ_blocked),
         renew_ok        => ( !$debarred && !$expired && !$fine_blocked),
         recall_ok       => ( !$debarred && !$expired && !$fine_blocked),
         hold_ok         => ( !$debarred && !$expired && !$fine_blocked),
         card_lost       => ( $kp->{lost} || $kp->{gonenoaddress} || $flags->{LOST} ),
         claims_returned => 0,
-        fines           => $fines_amount, # GetMemberAccountRecords($kp->{borrowernumber})
+        fines           => $fines_amount,
         fees            => 0,             # currently not distinct from fines
         recall_overdue  => 0,
         items_billed    => 0,
@@ -106,6 +112,10 @@ sub new {
     );
     }
     $debug and warn "patron fines: $ilspatron{fines} ... amountoutstanding: $kp->{amountoutstanding} ... CHARGES->amount: $flags->{CHARGES}->{amount}";
+
+    if ( $patron->is_debarred and $patron->debarredcomment ) {
+        $ilspatron{screen_msg} .= " -- " . $patron->debarredcomment;
+    }
     for (qw(EXPIRED CHARGES CREDITS GNA LOST DBARRED NOTES)) {
         ($flags->{$_}) or next;
         if ($_ ne 'NOTES' and $flags->{$_}->{message}) {
@@ -120,7 +130,14 @@ sub new {
 
     # FIXME: populate fine_items recall_items
     $ilspatron{unavail_holds} = _get_outstanding_holds($kp->{borrowernumber});
-    $ilspatron{items} = GetPendingIssues($kp->{borrowernumber});
+
+    my $pending_checkouts = $patron->pending_checkouts;
+    my @barcodes;
+    while ( my $c = $pending_checkouts->next ) {
+        push @barcodes, { barcode => $c->item->barcode };
+    }
+    $ilspatron{items} = \@barcodes;
+
     $self = \%ilspatron;
     $debug and warn Dumper($self);
     syslog("LOG_DEBUG", "new ILS::Patron(%s): found patron '%s'", $patron_id,$self->{id});
@@ -163,7 +180,6 @@ my %fields = (
     recall_overdue          => 0,   # for patron_status[12]
     too_many_billed         => 0,   # for patron_status[13]
     inet                    => 0,   # EnvisionWare extension
-    getmemberdetails_object => 0,
 );
 
 our $AUTOLOAD;
@@ -191,6 +207,26 @@ sub AUTOLOAD {
     }
 }
 
+sub name {
+    my ( $self, $template ) = @_;
+
+    if ($template) {
+        require Template;
+        require Koha::Patrons;
+
+        my $tt = Template->new();
+
+        my $patron = Koha::Patrons->find( $self->{borrowernumber} );
+
+        my $output;
+        $tt->process( \$template, { patron => $patron }, \$output );
+        return $output;
+    }
+    else {
+        return $self->{name};
+    }
+}
+
 sub check_password {
     my ( $self, $pwd ) = @_;
 
@@ -286,7 +322,8 @@ sub hold_items {
     my $self = shift;
     my $item_arr = $self->x_items('hold_items', @_);
     foreach my $item (@{$item_arr}) {
-        $item->{barcode} = GetBarcodeFromItemnumber($item->{itemnumber});
+        my $item_obj = Koha::Items->find($item->{itemnumber});
+        $item->{barcode} = $item_obj ? $item_obj->barcode : undef;
     }
     return $item_arr;
 }
@@ -412,6 +449,21 @@ sub charge_denied {
     return "Please contact library staff";
 }
 
+=head2 update_lastseen
+
+    $patron->update_lastseen();
+
+    Patron method to update lastseen field in borrower
+    to record that patron has been seen via sip connection
+
+=cut
+
+sub update_lastseen {
+    my $self = shift;
+    my $kohaobj = Koha::Patrons->find( $self->{borrowernumber} );
+    $kohaobj->track_login if $kohaobj; # track_login checks the pref
+}
+
 sub _get_address {
     my $patron = shift;
 
@@ -439,20 +491,59 @@ sub _get_outstanding_holds {
     while ( my $hold = $holds->next ) {
         my $item;
         if ($hold->itemnumber) {
-            $item = $hold->itemnumber;
+            $item = $hold->item;
         }
         else {
             # We need to return a barcode for the biblio so the client
             # can request the biblio info
-            $item = ( GetItemnumbersForBiblio($hold->biblionumber) )->[0];
+            my $items = $hold->biblio->items;
+            $item = $items->count ? $items->next : undef;
         }
         my $unblessed_hold = $hold->unblessed;
-        $unblessed_hold->{barcode} = GetBarcodeFromItemnumber($item);
+
+        $unblessed_hold->{barcode} = $item ? $item->barcode : undef;
+
         push @holds, $unblessed_hold;
     }
     return \@holds;
 }
 
+=head2 build_patron_attributes_string
+
+This method builds the part of the sip message for extended patron
+attributes as defined in the sip config
+
+=cut
+
+sub build_patron_attributes_string {
+    my ( $self, $server ) = @_;
+
+    my $string = q{};
+
+    if ( $server->{account}->{patron_attribute} ) {
+        my @attributes_to_send =
+          ref $server->{account}->{patron_attribute} eq "ARRAY"
+          ? @{ $server->{account}->{patron_attribute} }
+          : ( $server->{account}->{patron_attribute} );
+
+        foreach my $a ( @attributes_to_send ) {
+            my @attributes = Koha::Patron::Attributes->search(
+                {
+                    borrowernumber => $self->{borrowernumber},
+                    code           => $a->{code}
+                }
+            );
+
+            foreach my $attribute ( @attributes ) {
+                my $value = $attribute->attribute();
+                $string .= add_field( $a->{field}, $value );
+            }
+        }
+    }
+
+    return $string;
+}
+
 1;
 __END__