Index: openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-2.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-2.tcl,v
diff -u -r1.2 -r1.3
--- openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-2.tcl	5 Aug 2005 05:57:43 -0000	1.2
+++ openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-2.tcl	12 Aug 2005 10:10:14 -0000	1.3
@@ -93,18 +93,47 @@
     set item_id_list [list]
 }
 
+# See if a credit card was used for this purchase
+set method [db_string method {
+    select method
+    from dotlrn_ecommerce_transactions
+    where order_id = :order_id
+} -default cc]
+
+if { $method == "invoice" } {
+    if { [db_0or1row cc_transaction_in_invoice {
+	select 1
+	where exists (select *
+		      from dotlrn_ecommerce_transaction_invoice_payments
+		      where order_id = :order_id
+		      and method = 'cc')
+    }] } {
+	set method cc
+    }
+}
+
 set items_to_print ""
 db_foreach get_return_item_list $sql {
     
     if { [info exists all_items_p] } {
 	lappend item_id_list $item_id
     }
+
+    if { $method == "cc" } {
     append items_to_print "
 	<tr>
 	  <td>$product_name</td>
-	   <td><input type=text name=\"price_to_refund.${item_id}\" value=\"[format "%0.2f" $price_charged]\" size=\"5\"> (out of [ec_pretty_price $price_charged])</td>
+	   <td><input type=text name=\"price_to_refund.${item_id}\" value=\"[format "%0.2f" $price_charged]\" size=\"5\"> (<b>by credit card</b> out of [ec_pretty_price $price_charged]); <input type=text name=\"price_to_refund_manually.${item_id}\" value=\"0\" size=\"5\"> (<b>manually</b> out of [ec_pretty_price $price_charged])</td>
 	</tr>
  	   <input type=hidden name=\"shipping_to_refund.${item_id}\" value=\"[format "%0.2f" [expr $shipping_charged * $shipping_refund_percent]]\" size=\"5\">"
+    } else {
+    append items_to_print "
+	<tr>
+	  <td>$product_name</td>
+	   <td><input type=text name=\"price_to_refund_manually.${item_id}\" value=\"[format "%0.2f" $price_charged]\" size=\"5\"> (<b>manually</b> out of [ec_pretty_price $price_charged])</td>
+	</tr>
+ 	   <input type=hidden name=\"shipping_to_refund.${item_id}\" value=\"[format "%0.2f" [expr $shipping_charged * $shipping_refund_percent]]\" size=\"5\">"
+    }
 }
 
 append doc_body "
Index: openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3-postgresql.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3-postgresql.xql,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3-postgresql.xql	3 Aug 2005 22:45:46 -0000	1.1
+++ openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3-postgresql.xql	12 Aug 2005 10:10:14 -0000	1.2
@@ -41,7 +41,7 @@
 
   <fullquery name="get_cash_refunded">
     <querytext>
-      select coalesce(ec_cash_amount_to_refund(:total_amount_to_refund, :order_id),0) 
+      select coalesce(ec_cash_amount_to_refund((:total_amount_to_refund::float + :total_amount_to_refund_manually::float)::numeric, :order_id::integer),0) 
     </querytext>
   </fullquery>
 
Index: openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3.tcl,v
diff -u -r1.2 -r1.3
--- openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3.tcl	5 Aug 2005 02:52:53 -0000	1.2
+++ openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-3.tcl	12 Aug 2005 10:10:14 -0000	1.3
@@ -21,7 +21,8 @@
     received_back_datetime
     reason_for_return
     item_id_list
-    price_to_refund:array
+    price_to_refund:array,optional
+    price_to_refund_manually:array
     shipping_to_refund:array
     base_shipping_to_refund
 }
@@ -51,6 +52,7 @@
 # Add up the items' price/shipping/tax to refund as we go
 
 set total_price_to_refund 0
+set total_price_to_refund_manually 0
 set total_shipping_to_refund 0
 set total_price_tax_to_refund 0
 set total_shipping_tax_to_refund 0
@@ -62,17 +64,24 @@
     where i.product_id=p.product_id
     and i.item_id in ([join $item_id_list ", "])" {
     
-    if { [empty_string_p $price_to_refund($item_id)] } {
+ 	if { ! [array exists price_to_refund] } {
+	    foreach _item [array names price_to_refund_manually] {
+		set price_to_refund($_item) 0
+	    }
+	}
+
+    if { [empty_string_p price_to_refund($item_id)] || [empty_string_p price_to_refund_manually($item_id)] } {
 	incr exception_count
 	append exception_text "<li>Please enter a price to refund for $product_name."
-    } elseif {[regexp {[^0-9\.]} $price_to_refund($item_id)]} {
+    } elseif {[regexp {[^0-9\.]} $price_to_refund($item_id)] || [regexp {[^0-9\.]} $price_to_refund_manually($item_id)]} {
 	incr exception_count
 	append exception_text "<li>Please enter a purely numeric price to refund for $product_name (no letters or special characters)."
-    } elseif { $price_to_refund($item_id) > $price_charged } {
+    } elseif { ($price_to_refund($item_id) + $price_to_refund_manually($item_id)) > $price_charged } {
 	incr exception_count
 	append exception_text "<li>Please enter a price to refund for $product_name that is less than or equal to [ec_pretty_price $price_charged]."
     } else {
 	set total_price_to_refund [expr $total_price_to_refund + $price_to_refund($item_id)]
+	set total_price_to_refund_manually [expr $total_price_to_refund_manually + $price_to_refund_manually($item_id)]
 
 	# Tax will be the minimum of the tax actually charged and the
 	# tax that would have been charged on the price to refund (tax
@@ -135,17 +144,33 @@
 
 set total_tax_to_refund [expr $total_price_tax_to_refund + $total_shipping_tax_to_refund]
 set total_amount_to_refund [expr $total_price_to_refund + $total_shipping_to_refund + $total_tax_to_refund]
+set total_amount_to_refund_manually [expr $total_price_to_refund_manually + $total_shipping_to_refund + $total_tax_to_refund]
 
 # Determine how much of this will be refunded in cash
 
 set cash_amount_to_refund [db_string get_cash_refunded "
     select nvl(ec_cash_amount_to_refund(:total_amount_to_refund,:order_id),0) 
     from dual"]
 
+# Calculate how much to refund to the credit card and how much to
+# refund manually
+if { $total_amount_to_refund >= $cash_amount_to_refund } {
+    # Requested amount to refund to credit card is greater than what
+    # is available for refund (the rest probably paid via gift
+    # certs/scholarship), give priority to credit card refunds than
+    # manual refunds
+    set cash_amount_to_refund_cc $cash_amount_to_refund
+    set cash_amount_to_refund_manually 0
+} else {
+    # Give priority to credit card refunds, the rest refund manually
+    set cash_amount_to_refund_cc $total_amount_to_refund
+    set cash_amount_to_refund_manually [expr $cash_amount_to_refund - $total_amount_to_refund]
+}
+
 # Calculate gift certificate amount and tax to refund
 
-set certificate_amount_to_reinstate [expr $total_amount_to_refund - $cash_amount_to_refund]
-if { $certificate_amount_to_reinstate < 0 } {
+set certificate_amount_to_reinstate [expr ($total_amount_to_refund + $total_amount_to_refund_manually) - $cash_amount_to_refund]
+if { $certificate_amount_to_reinstate < 0.01 } {
 
     # Because of rounding
 
@@ -179,29 +204,6 @@
     set billing_city ""
 }
 
-append doc_body "
-    [ad_admin_header "Refund Totals"]
-
-    <h2>Refund Totals</h2>
-
-    [ad_context_bar [list "../" "Ecommerce([ec_system_name])"] [list "index" "Orders"] [list "one?[export_url_vars order_id]" "One"] "Refund Totals"]
-
-    <hr>
-    <form method=post action=items-return-4>
-     [export_entire_form]
-     [export_form_vars cash_amount_to_refund certificate_amount_to_reinstate]
-     <blockquote>
-       <p>Total refund amount: [ec_pretty_price $total_amount_to_refund] (price: [ec_pretty_price $total_price_to_refund], shipping: [ec_pretty_price $total_shipping_to_refund], tax: [ec_pretty_price $total_tax_to_refund])</p>
-       <ul>
-        <li>[ec_pretty_price $certificate_amount_to_reinstate] will be reinstated in gift certificates.<br>
-        <li>[ec_pretty_price $cash_amount_to_refund] will be refunded to the customer's credit card.<br>
-      </ul>"
-
-# Request the credit card number to be re-entered if it is no longer
-# on file, yet there is money to refund.
-
-# Only ask for credit card info if credit card was used in purchase
-
 set method [db_string method {
     select method
     from dotlrn_ecommerce_transactions
@@ -220,6 +222,45 @@
     }
 }
 
+append doc_body "
+    [ad_admin_header "Refund Totals"]
+$total_amount_to_refund, $total_amount_to_refund_manually, $cash_amount_to_refund, $certificate_amount_to_reinstate, [expr ($total_amount_to_refund + $total_amount_to_refund_manually) - $cash_amount_to_refund]
+    <h2>Refund Totals</h2>
+
+    [ad_context_bar [list "../" "Ecommerce([ec_system_name])"] [list "index" "Orders"] [list "one?[export_url_vars order_id]" "One"] "Refund Totals"]
+
+    <hr>
+    <form method=post action=items-return-4>
+     [export_entire_form]
+     [export_form_vars cash_amount_to_refund certificate_amount_to_reinstate cash_amount_to_refund_cc cash_amount_to_refund_manually]
+     <blockquote>
+       <p>Total refund amount: [ec_pretty_price [expr $total_amount_to_refund + $total_amount_to_refund_manually]] (price: [ec_pretty_price [expr $total_price_to_refund + $total_price_to_refund_manually]], shipping: [ec_pretty_price $total_shipping_to_refund], tax: [ec_pretty_price $total_tax_to_refund])</p>
+       <ul>"
+
+if { $certificate_amount_to_reinstate > 0 } {
+    if { $method == "scholarship" } {
+	append doc_body "<li>[ec_pretty_price $certificate_amount_to_reinstate] will be reinstated in the scholarship fund.<br>"
+    } else {
+	append doc_body "<li>[ec_pretty_price $certificate_amount_to_reinstate] will be reinstated in gift certificates.<br>"
+    }
+}
+
+if { $method == "cc" } {
+    append doc_body "        
+        <li>[ec_pretty_price $cash_amount_to_refund_cc] will be refunded to the customer's credit card.<br>"
+}
+
+append doc_body "        
+        <li>[ec_pretty_price $cash_amount_to_refund_manually] will be refunded to the customer manually.<br>"
+
+append doc_body "
+      </ul>"
+
+# Request the credit card number to be re-entered if it is no longer
+# on file, yet there is money to refund.
+
+# Only ask for credit card info if credit card was used in purchase
+
 if { [empty_string_p $creditcard_number] && $cash_amount_to_refund > 0 && $method == "cc" } {
     append doc_body "
     <p>Please re-enter the credit card number of the card used for this order:</p>
Index: openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4-postgresql.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4-postgresql.xql,v
diff -u -r1.2 -r1.3
--- openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4-postgresql.xql	5 Aug 2005 02:52:53 -0000	1.2
+++ openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4-postgresql.xql	12 Aug 2005 10:10:14 -0000	1.3
@@ -8,7 +8,7 @@
       insert into ec_refunds
       (refund_id, order_id, refund_amount, refund_date, refunded_by, refund_reasons)
       values
-      (:refund_id, :order_id, :cash_amount_to_refund, current_timestamp, :customer_service_rep,:reason_for_return)
+      (:refund_id, :order_id, :cash_amount_to_refund_cc, current_timestamp, :customer_service_rep,:reason_for_return)
     </querytext>
   </fullquery>
 
Index: openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4.tcl,v
diff -u -r1.4 -r1.5
--- openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4.tcl	8 Aug 2005 13:10:07 -0000	1.4
+++ openacs-4/packages/dotlrn-ecommerce/www/admin/ecommerce/items-return-4.tcl	12 Aug 2005 10:10:14 -0000	1.5
@@ -32,10 +32,13 @@
     received_back_datetime
     reason_for_return
     item_id_list:notnull
-    price_to_refund:array
+    price_to_refund:array,optional
+    price_to_refund_manually:array
     shipping_to_refund:array
     base_shipping_to_refund
     cash_amount_to_refund:optional
+    cash_amount_to_refund_cc:optional
+    cash_amount_to_refund_manually:optional
     certificate_amount_to_reinstate
 
     creditcard_id:optional
@@ -44,6 +47,12 @@
     creditcard_last_four:optional
 }
 
+if { ! [array exists price_to_refund] } {
+    foreach _item [array names price_to_refund_manually] {
+	set price_to_refund($_item) 0
+    }
+}
+
 set method [db_string method {
     select method
     from dotlrn_ecommerce_transactions
@@ -95,7 +104,7 @@
 # Check if money needs to be refunded and if the credit card number is
 # still on file.
 
-if { [expr $cash_amount_to_refund] > 0 && $method == "cc" } {
+if { $cash_amount_to_refund > 0 && $method == "cc" } {
 
     # Make sure that all the credit card information is there.
 
@@ -174,7 +183,7 @@
     insert into ec_refunds
     (refund_id, order_id, refund_amount, refund_date, refunded_by, refund_reasons)
     values
-    (:refund_id, :order_id, :cash_amount_to_refund, sysdate, :customer_service_rep,:reason_for_return)"
+    (:refund_id, :order_id, :cash_amount_to_refund_cc, sysdate, :customer_service_rep,:reason_for_return)"
 
 foreach item_id $item_id_list {
 
@@ -238,7 +247,7 @@
 # amount can exceed the charge amount when the order was shipped
 # in parts and the customer returned items from various shipments.
 
-set refund_amount $cash_amount_to_refund 
+set refund_amount $cash_amount_to_refund_cc
 while { $refund_amount > 0 && $method == "cc" } {
     
     # See if the refund matches a single charge transaction. The
@@ -512,6 +521,9 @@
 		    set refunded_date=sysdate 
 		    where transaction_id=:pgw_transaction_id"
 		append results_explanation "<p>Refund transaction $pgw_transaction_id for [ec_pretty_price $transaction_amount] is complete!</p>";# 
+		if { $cash_amount_to_refund_manually > 0 } {
+		    append results_explanation "<p>The amount [ec_pretty_price $cash_amount_to_refund_manually] is to be refunded manually</p>";# 
+		}
 	    }
 	} else {
 
@@ -528,6 +540,9 @@
 	set page_title "No credit card refund needed."
 	set results_explanation "No credit card refund was necessary because the entire amount was refunded to the gift certificates the customer used when purchasing the order."
     }
+} elseif { $cash_amount_to_refund_manually > 0 } {
+    set page_title "Refund results"
+    append results_explanation "<p>The amount [ec_pretty_price $cash_amount_to_refund_manually] is to be refunded manually</p>";# 
 } else {
 	set page_title "No credit card refund needed."
 	set results_explanation "No credit card refund was necessary because the entire amount was refunded to the gift certificates the customer used when purchasing the order."