From f4a43819317b1031d00d148e86083d18964d870f Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Tue, 15 Jul 2014 10:59:03 -0400
Subject: [PATCH] Bug 6427 - Update existing intranet scripts

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Sean McGarvey <seanm@pascolibraries.org>
---
 circ/add_message.pl                                |    8 +---
 circ/branchoverdues.pl                             |    3 +-
 circ/circulation.pl                                |    4 +-
 circ/del_message.pl                                |    8 +---
 .../prog/en/includes/browser-strings.inc           |   33 +++++++++++
 .../intranet-tmpl/prog/en/includes/circ-menu.inc   |    2 +-
 .../intranet-tmpl/prog/en/includes/circ-menu.tt    |    2 +-
 .../intranet-tmpl/prog/en/includes/datatables.inc  |    1 +
 .../prog/en/includes/members-menu.inc              |    2 +-
 koha-tmpl/intranet-tmpl/prog/en/js/datatables.js   |   24 ++++++++
 .../prog/en/modules/circ/circulation.tt            |    7 ++-
 .../intranet-tmpl/prog/en/modules/circ/returns.tt  |    2 +-
 .../prog/en/modules/help/reports/guided_reports.tt |   18 ------
 .../prog/en/modules/members/moremember-print.tt    |   57 +++++++++++++------
 .../prog/en/modules/reports/reports-home.tt        |    1 -
 members/moremember.pl                              |   25 +++++----
 members/summary-print.pl                           |   21 ++-----
 offline_circ/enqueue_koc.pl                        |    1 -
 offline_circ/process_koc.pl                        |   12 +++-
 19 files changed, 137 insertions(+), 94 deletions(-)

diff --git a/circ/add_message.pl b/circ/add_message.pl
index 912314a..159fccf 100755
--- a/circ/add_message.pl
+++ b/circ/add_message.pl
@@ -17,8 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
@@ -26,11 +25,6 @@ use C4::Context;
 use C4::Auth;
 use C4::Output;
 use C4::Members;
-use C4::Accounts;
-use C4::Stats;
-use C4::Koha;
-use C4::Overdues;
-use C4::Branch;    # GetBranches
 
 my $input = new CGI;
 
diff --git a/circ/branchoverdues.pl b/circ/branchoverdues.pl
index d04f5e4..5344f6e 100755
--- a/circ/branchoverdues.pl
+++ b/circ/branchoverdues.pl
@@ -49,9 +49,8 @@ use Data::Dumper;
  	level 3 : only methode is possible  : - Considered Lost
 
  	the documents displayed on this interface, are checked on three points
- 	- 1) the document must be on accountlines (Type 'FU')
+    - 1) the document must be overdue with fines
  	- 2) item issues is not returned
-	- 3) this item as not been already notify
 
   FIXME: who is the author?
   FIXME: No privisions (i.e. "actions") for handling notices are implemented.
diff --git a/circ/circulation.pl b/circ/circulation.pl
index ab6db3b..11d98b3 100755
--- a/circ/circulation.pl
+++ b/circ/circulation.pl
@@ -497,8 +497,6 @@ foreach my $flag ( sort keys %$flags ) {
 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
 $amountold =~ s/^.*\$//;    # remove upto the $, if any
 
-my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
-
 if ( $borrowernumber && $borrower->{'category_type'} eq 'C') {
     my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
     my $cnt = scalar(@$catcodes);
@@ -553,7 +551,7 @@ $template->param(
     stickyduedate     => $stickyduedate,
     duedatespec       => $duedatespec,
     message           => $message,
-    totaldue          => sprintf('%.2f', $total),
+    totaldue          => sprintf('%.2f', $borrower->{account_balance}),
     inprocess         => $inprocess,
     is_child          => ($borrowernumber && $borrower->{'category_type'} eq 'C'),
     circview => 1,
diff --git a/circ/del_message.pl b/circ/del_message.pl
index 6b8d560..22db502 100755
--- a/circ/del_message.pl
+++ b/circ/del_message.pl
@@ -17,8 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
@@ -26,11 +25,6 @@ use C4::Context;
 use C4::Auth;
 use C4::Output;
 use C4::Members;
-use C4::Accounts;
-use C4::Stats;
-use C4::Koha;
-use C4::Overdues;
-use C4::Branch;    # GetBranches
 
 my $input = new CGI;
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/browser-strings.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/browser-strings.inc
index 3dda230..01a06a9 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/browser-strings.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/browser-strings.inc
@@ -1,7 +1,40 @@
+[% USE AuthorisedValues %]
 <script type="text/javascript">
 //<![CDATA[
     var BROWSER_RETURN_TO_SEARCH = _("Return to results");
     var BROWSER_PREVIOUS = _("Previous");
     var BROWSER_NEXT = _("Next");
+
+    var STRINGS = {
+        "DebitTypes": {
+            "FINE"                      : _("Fine"),
+            "ACCOUNT_MANAGEMENT_FEE"    : _("Account management fee"),
+            "SUNDRY"                    : _("Sundry"),
+            "LOST"                      : _("Lost item"),
+            "HOLD"                      : _("Hold fee"),
+            "RENTAL"                    : _("Rental fee"),
+            "NEW_CARD"                  : _("New card"),
+            [% FOREACH a IN AuthorisedValues.Get('MANUAL_INV') %]
+                "[% a.authorised_value %]" : "[% a.lib %]",
+            [% END %]
+        },
+
+        "CreditTypes": {
+            "CREDIT"                    : _("Credit"),
+            "PAYMENT"                   : _("Payment"),
+            "WRITEOFF"                  : _("Writeoff"),
+            "FOUND"                     : _("Lost item found"),
+            "FORGIVEN"                  : _("Forgiven"),
+            [% FOREACH a IN AuthorisedValues.Get('MANUAL_CREDIT') %]
+                "[% a.authorised_value %]" : "[% a.lib %]",
+            [% END %]
+        },
+
+        "OffsetTypes": {
+            "DROPBOX"                   : _("Dropbox fine reduction"),
+            "FINE"                      : _("Fine increment"),
+            "VOID"                      : _("Voided"),
+        },
+    }
 //]]>
 </script>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc
index ee9818b..020a290 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.inc
@@ -67,7 +67,7 @@
         [% IF ( detailview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% borrowernumber %]">Details</a></li>
     [% END %]
     [% IF ( CAN_user_updatecharges ) %]
-        [% IF ( finesview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/pay.pl?borrowernumber=[% borrowernumber %]">Fines</a></li>
+        [% IF ( accounts_view ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/account.pl?borrowernumber=[% borrowernumber %]">Fines</a></li>
     [% END %]
     [% IF ( RoutingSerials ) %][% IF ( routinglistview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/routing-lists.pl?borrowernumber=[% borrowernumber %]">Routing lists</a></li>[% END %]
     <!-- Added check for borrowers permission as this is needed to access Cirulation History -->
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.tt b/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.tt
index 5c953a5..75102cb 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/circ-menu.tt
@@ -72,7 +72,7 @@ in the global namespace %]
 	[% IF ( detailview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% borrower.borrowernumber %]">Details</a></li>
 	[% END %]
 	 [% IF ( CAN_user_updatecharges ) %]
-	[% IF ( finesview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/pay.pl?borrowernumber=[% borrower.borrowernumber %]">Fines</a></li>
+     [% IF ( accounts_view ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/account.pl?borrowernumber=[% borrower.borrowernumber %]">Fines</a></li>
 	[% END %]
     [% IF ( RoutingSerials ) %][% IF ( routinglistview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/routing-lists.pl?borrowernumber=[% borrower.borrowernumber %]">Routing lists</a></li>[% END %]
     <!-- Added check for borrowers permission as this is needed to access Cirulation History -->
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/datatables.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/datatables.inc
index 4381b01..77e1419 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/datatables.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/datatables.inc
@@ -1,5 +1,6 @@
 [% INCLUDE 'format_price.inc' %]
 <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.min.js"></script>
+<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/TableTools/media/js/TableTools.min.js"></script>
 <script type="text/javascript">
 //<![CDATA[
     var MSG_DT_FIRST = _("First");
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/members-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/members-menu.inc
index e7e8772..6e12997 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/members-menu.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/members-menu.inc
@@ -4,7 +4,7 @@
     [% IF ( circview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/circ/circulation.pl?borrowernumber=[% borrowernumber %]">Check out</a></li>
     [% IF ( detailview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% borrowernumber %]">Details</a></li>
     [% IF ( CAN_user_updatecharges ) %]
-        [% IF ( finesview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/boraccount.pl?borrowernumber=[% borrowernumber %]">Fines</a></li>
+        [% IF ( finesview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/account.pl?borrowernumber=[% borrowernumber %]">Fines</a></li>
     [% END %]
     [% IF ( intranetreadinghistory ) %]
         [% IF ( readingrecordview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/members/readingrec.pl?borrowernumber=[% borrowernumber %]">Circulation history</a></li>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js
index e42918c..d5eb929 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/datatables.js
@@ -28,6 +28,30 @@ var dataTablesDefaults = {
     "iDisplayLength": 20
 };
 
+// Set the classes that TableTools uses to something suitable for Bootstrap
+$.extend( true, $.fn.DataTable.TableTools.classes, {
+    "container": "btn-group",
+    "buttons": {
+        "normal": "btn btn-small",
+        "disabled": "btn btn-small disabled"
+    },
+    "collection": {
+        "container": "DTTT_dropdown dropdown-menu",
+        "buttons": {
+            "normal": "",
+            "disabled": "disabled"
+        }
+    }
+} );
+
+// Have the collection use a bootstrap compatible dropdown
+$.extend( true, $.fn.DataTable.TableTools.DEFAULTS.oTags, {
+    "collection": {
+        "container": "ul",
+        "button": "li",
+        "liner": "a"
+    }
+} );
 
 // Return an array of string containing the values of a particular column
 $.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index 35447b7..dccb0a8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -2,6 +2,7 @@
 [% USE Branches %]
 [% USE KohaDates %]
 [% USE ColumnsSettings %]
+[% USE Currency %]
 [% IF Koha.Preference('ExportRemoveFields') OR Koha.Preference('ExportWithCsvProfile') %]
    [% SET exports_enabled = 1 %]
 [% END %]
@@ -735,16 +736,16 @@ No patron matched <span class="ex">[% message %]</span>
 
         	[% IF ( charges ) %]
 			    <li>
-            <span class="circ-hlt">Fees &amp; Charges:</span> Patron has  <a href="/cgi-bin/koha/members/boraccount.pl?borrowernumber=[% borrowernumber %]">Outstanding fees &amp; charges[% IF ( chargesamount ) %] of [% chargesamount %][% END %]</a>.
+            <span class="circ-hlt">Fees &amp; Charges:</span> Patron has  <a href="/cgi-bin/koha/members/account.pl?borrowernumber=[% borrowernumber %]">Outstanding fees &amp; charges[% IF ( chargesamount ) %] of [% chargesamount | $Currency %][% END %]</a>.
                 [% IF ( charges_is_blocker ) %]
                     Checkouts are <span class="circ-hlt">BLOCKED</span> because fine balance is <span class="circ-hlt">OVER THE LIMIT</span>.
                 [% END %]
-            <a href="/cgi-bin/koha/members/pay.pl?borrowernumber=[% borrowernumber %]">Make payment</a></li>
+            <a href="/cgi-bin/koha/members/account_payment.pl?borrowernumber=[% borrowernumber %]">Make payment</a></li>
 			[% END %]
 
         	[% IF ( credits ) %]
 			<li>
-                <span class="circ-hlt">Credits:</span> Patron has a credit[% IF ( creditsamount ) %] of [% creditsamount %][% END %]
+                <span class="circ-hlt">Credits:</span> Patron has a credit[% IF ( creditsamount ) %] of [% creditsamount | $Currency %][% END %]
             </li>
 			[% END %]
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
index 211b131..59e4f65 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
@@ -121,7 +121,7 @@ $(document).ready(function () {
 [% IF ( fines ) %]
     <div class="dialog alert">
         <h3>Patron has outstanding fines of [% fines %].</h3>
-        <p><a href="/cgi-bin/koha/members/pay.pl?borrowernumber=[% fineborrowernumber %]">Make payment</a>.</p>
+        <p><a href="/cgi-bin/koha/members/account_payment.pl?borrowernumber=[% fineborrowernumber %]">Make payment</a>.</p>
     </div>
 [% END %]
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/help/reports/guided_reports.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/help/reports/guided_reports.tt
index 9d08f7d..31ef70e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/help/reports/guided_reports.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/help/reports/guided_reports.tt
@@ -108,24 +108,6 @@
 
 <h3>DB table value for reports</h3>
 
-<p><strong>Question:</strong> What do the codes in the accounttype field in the accountlines table stand for?</p>
-
-<p><strong>Answer:</strong></p>
-
-<ul>
-	<li>A = Account management fee</li>
-	<li>C = Credit</li>
-	<li>F = Overdue fine</li>
-	<li>FOR = Forgiven</li>
-	<li>FU = Overdue, still acccruing</li>
-	<li>L = Lost item</li>
-	<li>LR = Lost item returned/refunded</li>
-	<li>M = Sundry</li>
-	<li>N = New card</li>
-	<li>PAY = Payment</li>
-	<li>W = Writeoff</li>
-</ul>
-
 <p><strong>Question:</strong> What are the possible codes for the type field in the statistics table?</p>
 
 <p><strong>Answer:</strong></p>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember-print.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember-print.tt
index 92526e8..1f26233 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember-print.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember-print.tt
@@ -1,6 +1,7 @@
 [% USE Koha %]
 [% USE Branches %]
 [% USE KohaDates %]
+[% USE Currency %]
 [% INCLUDE 'doc-head-open.inc' %]
     <title>Summary for [% firstname %] [% surname %] ([% cardnumber %])</title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
@@ -67,37 +68,57 @@
         </table>
     [% END %]
 
-    [% IF ( accounts && ( totaldue != '0.00' ) ) %]
+[% IF account_balance %]
+    [% IF account_debits %]
         <table>
-            <caption>Account fines and payments</caption>
+            <caption>Account fees</caption>
             <tr>
-                <th>Description of charges</th>
+                <th>Description</th>
                 <th>Date</th>
                 <th>Amount</th>
                 <th>Outstanding</th>
             </tr>
-
-            [% FOREACH account IN accounts %]
-                [% NEXT IF account.amountoutstanding == '0.00' %]
-                <tr>
-                    <td>
-                        [% IF ( account.itemnumber ) %]<a href="/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=[% account.biblionumber %]&amp;itemnumber=[% account.itemnumber %]">[% END %]
-                        [% account.description %]&nbsp;[% IF ( account.printtitle ) %] [% account.title |html %][% END %]
-                        [% IF ( account.itemnumber ) %]</a>[% END %]
-                    </td>
-                    <td>[% account.date | $KohaDates %]</td>
-                    <td>[% account.amount %]</td>
-                    <td>[% account.amountoutstanding %]</td>
-                </tr>
+            [% FOREACH d IN account_debits %]
+                [% IF d.amount_outstanding > 0 %]
+                    <tr>
+                        <td>[% d.description %]</td>
+                        <td>[% d.created_on | $KohaDates %]</td>
+                        <td>[% d.amount_original | $Currency %]</td>
+                        <td>[% d.amount_outstanding | $Currency %]</td>
+                    </tr>
+                [% END %]
             [% END %]
-
             <tfoot>
                 <tr>
                     <td colspan="3">Total due</td>
-                    <td colspan="2">[% totaldue %]</td>
+                    <td colspan="2">[% totaldue | $Currency %]</td>
                 </tr>
             </tfoot>
         </table>
     [% END %]
 
+    [% FOREACH c IN account_credits %]
+        [% total_remaining = total_remaining + c.amount_remaining %]
+    [% END %]
+    [% IF total_remaining %]
+        <table>
+            <caption>Account payments</caption>
+            <tr>
+                <th>Date</th>
+                <th>Amount</th>
+                <th>Balance remaining</th>
+            </tr>
+            [% FOREACH c IN account_credits %]
+                [% IF c.amount_remaining > 0 %]
+                    <tr>
+                        <td>[% c.created_on | $KohaDates %]</td>
+                        <td>[% c.amount_paid | $Currency %]</td>
+                        <td>[% c.amount_remaining | $Currency %]</td>
+                    </tr>
+                [% END %]
+            [% END %]
+        </table>
+    [% END %]
+[% END %]
+
 [% INCLUDE 'intranet-bottom.inc' %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/reports-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/reports-home.tt
index fde9ad3..db7edb7 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/reports-home.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/reports-home.tt
@@ -65,7 +65,6 @@
 		<li><a href="/cgi-bin/koha/reports/issues_avg_stats.pl">Average loan time</a></li>
         <li><a href="http://schema.koha-community.org/" target="blank">Koha database schema</a></li>
         <li><a href="http://wiki.koha-community.org/wiki/SQL_Reports_Library" target="blank">Koha reports library</a></li>
-        <!--<li><a href="/cgi-bin/koha/reports/stats.screen.pl">Till reconciliation</a></li> -->
 	</ul></div>
 </div>
 
diff --git a/members/moremember.pl b/members/moremember.pl
index d448b91..eab5098 100755
--- a/members/moremember.pl
+++ b/members/moremember.pl
@@ -226,23 +226,24 @@ else {
 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
 @{$data}{keys %$branchdetail} = values %$branchdetail; # merge in all branch columns
 
-my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
 $template->param( lib1 => $lib1 ) if ($lib1);
 $template->param( lib2 => $lib2 ) if ($lib2);
 
 # If printing a page, send the account informations to the template
-if ($print eq "page") {
-    foreach my $accountline (@$accts) {
-        $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
-        $accountline->{amountoutstanding} = sprintf '%.2f', $accountline->{amountoutstanding};
+if ( $print eq "page" ) {
+    my $schema = Koha::Database->new()->schema();
 
-        if ($accountline->{accounttype} ne 'F' && $accountline->{accounttype} ne 'FU'){
-            $accountline->{printtitle} = 1;
-        }
-    }
-    $template->param( accounts => $accts );
+    my @account_debits = $schema->resultset('AccountDebit')
+      ->search( { borrowernumber => $borrowernumber } );
+    my @account_credits = $schema->resultset('AccountCredit')
+      ->search( { borrowernumber => $borrowernumber } );
+
+    $template->param(
+        account_debits  => \@account_debits,
+        account_credits => \@account_credits,
+    );
 }
 
 # Show OPAC privacy preference is system preference is set
@@ -350,8 +351,8 @@ $template->param(
     branch          => $branch,
     todaysdate      => C4::Dates->today(),
     totalprice      => sprintf("%.2f", $totalprice),
-    totaldue        => sprintf("%.2f", $total),
-    totaldue_raw    => $total,
+    totaldue        => sprintf("%.2f", $data->{account_balance}),
+    totaldue_raw    => $data->{account_balance},
     overdues_exist  => $overdues_exist,
     StaffMember     => ($category_type eq 'S'),
     is_child        => ($category_type eq 'C'),
diff --git a/members/summary-print.pl b/members/summary-print.pl
index 4530e14..197e563 100755
--- a/members/summary-print.pl
+++ b/members/summary-print.pl
@@ -24,6 +24,9 @@ use C4::Output;
 use C4::Members;
 use C4::Koha qw( getitemtypeinfo );
 use C4::Circulation qw( GetIssuingCharges );
+use Koha::Database;
+
+my $schema = Koha::Database->new()->schema();
 
 my $input          = CGI->new;
 my $borrowernumber = $input->param('borrowernumber');
@@ -41,19 +44,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 
 my $data = GetMember( 'borrowernumber' => $borrowernumber );
 
-my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
-foreach my $accountline (@$accts) {
-    $accountline->{amount} = sprintf '%.2f', $accountline->{amount};
-    $accountline->{amountoutstanding} = sprintf '%.2f',
-      $accountline->{amountoutstanding};
-
-    if (   $accountline->{accounttype} ne 'F'
-        && $accountline->{accounttype} ne 'FU' )
-    {
-        $accountline->{printtitle} = 1;
-    }
-}
-
 my $roadtype =
   C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
 
@@ -64,8 +54,9 @@ $template->param(
     borrowernumber => $borrowernumber,
     address => $data->{'streetnumber'} . " $roadtype " . $data->{'address'},
 
-    accounts => $accts,
-    totaldue => sprintf( "%.2f", $total ),
+    account_balance => $data->{account_balance},
+    account_debits => $schema->resultset('AccountDebit')->search({ borrowernumber => $borrowernumber }),
+    account_credits => $schema->resultset('AccountDebit')->search({ borrowernumber => $borrowernumber }),
 
     issues     => build_issue_data( GetPendingIssues($borrowernumber) ),
     totalprice => $totalprice,
diff --git a/offline_circ/enqueue_koc.pl b/offline_circ/enqueue_koc.pl
index 6798f3f..2ef77d2 100755
--- a/offline_circ/enqueue_koc.pl
+++ b/offline_circ/enqueue_koc.pl
@@ -27,7 +27,6 @@ use C4::Auth;
 use C4::Koha;
 use C4::Context;
 use C4::Biblio;
-use C4::Accounts;
 use C4::Circulation;
 use C4::Items;
 use C4::Members;
diff --git a/offline_circ/process_koc.pl b/offline_circ/process_koc.pl
index d7e5606..4778c3c 100755
--- a/offline_circ/process_koc.pl
+++ b/offline_circ/process_koc.pl
@@ -27,13 +27,14 @@ use C4::Auth;
 use C4::Koha;
 use C4::Context;
 use C4::Biblio;
-use C4::Accounts;
 use C4::Circulation;
 use C4::Items;
 use C4::Members;
 use C4::Stats;
 use C4::UploadedFile;
 use C4::BackgroundJob;
+use Koha::Accounts;
+use Koha::Database;
 
 use Date::Calc qw( Add_Delta_Days Date_to_Days );
 
@@ -357,8 +358,13 @@ sub kocReturnItem {
 
 sub kocMakePayment {
     my ( $circ ) = @_;
-    my $borrower = GetMember( 'cardnumber'=>$circ->{ 'cardnumber' } );
-    recordpayment( $borrower->{'borrowernumber'}, $circ->{'amount'} );
+
+    my $borrower =
+      Koha::Database->new()->schema()->resultset('Borrower')
+      ->single( { cardnumber => $circ->{'cardnumber'} } );
+
+    AddCredit({ borrower => $borrower, amount => $circ->{'amount'} });
+
     push @output, {
         payment => 1,
         amount => $circ->{'amount'},
-- 
1.7.2.5