From 2ec43ab54aeea5b08205ab9f1c4c3473fa632263 Mon Sep 17 00:00:00 2001
From: Jason Stephenson <jstephenson@mvlc.org>
Date: Thu, 30 Jun 2011 13:43:24 -0400
Subject: [PATCH] Add SIP2 chargeable loan support.

Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Signed-off-by: Bill Erickson <berick@esilibrary.com>
---
 Open-ILS/src/perlmods/lib/OpenILS/SIP.pm            | 21 +++++++++++++++++++--
 Open-ILS/src/perlmods/lib/OpenILS/SIP/Item.pm       |  9 ++++++---
 .../src/perlmods/lib/OpenILS/SIP/Transaction.pm     |  1 +
 .../lib/OpenILS/SIP/Transaction/Checkout.pm         |  3 +++
 4 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm
index 729fc5c607..acfa572f78 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP.pm
@@ -297,16 +297,18 @@ sub offline_ok {
 
 
 ##
-## Checkout(patron_id, item_id, sc_renew):
+## Checkout(patron_id, item_id, sc_renew, fee_ack):
 ##    patron_id & item_id are the identifiers send by the terminal
 ##    sc_renew is the renewal policy configured on the terminal
 ## returns a status opject that can be queried for the various bits
 ## of information that the protocol (SIP or NCIP) needs to generate
 ## the response.
+##    fee_ack is the fee_acknowledged field (BO) sent from the sc
+## when doing chargeable loans.
 ##
 
 sub checkout {
-	my ($self, $patron_id, $item_id, $sc_renew) = @_;
+	my ($self, $patron_id, $item_id, $sc_renew, $fee_ack) = @_;
 	$sc_renew = 0;
 
 	$self->verify_session;
@@ -347,6 +349,21 @@ sub checkout {
 		$xact->ok(0);
 	} 
 
+        # Check for fee and $fee_ack. If there is a fee, and $fee_ack
+        # is 'Y', we proceed, otherwise we reject the checkout.
+        if ($item->fee > 0.0) {
+            $xact->fee_amount($item->fee);
+            $xact->sip_fee_type($item->sip_fee_type);
+            $xact->sip_currency($item->fee_currency);
+            if ($fee_ack && $fee_ack eq 'Y') {
+                $xact->fee_ack(1);
+            } else {
+                $xact->screen_msg('Fee required');
+                $xact->ok(0);
+                return $xact;
+            }
+        }
+
 	$xact->do_checkout($sc_renew);
 	$xact->desensitize(!$item->magnetic);
 
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Item.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Item.pm
index 91302a140c..35d9ee4b83 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Item.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Item.pm
@@ -364,12 +364,15 @@ sub sip_security_marker {
 }
 
 sub sip_fee_type {
-    return '01';    # FIXME? 01-09 enumerated in spec.  We just use O1-other/unknown.
+    my $self = shift;
+    # Return '06' for rental unless the fee is a deposit, or there is
+    # no fee. In the latter cases, return '01'.
+    return ($self->{copy}->deposit_amount > 0.0 && $self->{copy}->deposit =~ /^f/i) ? '06' : '01';
 }
 
-sub fee {           # TODO
+sub fee {
     my $self = shift;
-    return 0;
+    return $self->{copy}->deposit_amount;
 }
 
 
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm
index a4813cb147..5ef185dd3d 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction.pm
@@ -26,6 +26,7 @@ my %fields = (
       print_line    => '',
       editor        => undef,
       authtoken     => '',
+      fee_ack       => 0,
 );
 
 our $AUTOLOAD;
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkout.pm b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkout.pm
index bb751b262e..bcfdc3bb79 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkout.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkout.pm
@@ -102,6 +102,9 @@ sub do_checkout {
                     if ($override_events{$txt} && $method !~ /override$/) {
                         # Found an event we've been configured to override.
                         $override = 1;
+                    } elsif ($txt =~ /ITEM_(?:DEPOSIT|RENTAL)_FEE_REQUIRED/ && $self->fee_ack) {
+                        # Patron acknowledged the fee.
+                        $override = 1;
                     } elsif ( $txt eq 'OPEN_CIRCULATION_EXISTS' ) {
                         $self->screen_msg(OILS_SIP_MSG_CIRC_EXISTS);
                         return 0;
-- 
2.11.0