From 74a80a35c72995311c7a76a9efe3853db1fb166c Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 2 May 2018 13:12:02 +0000
Subject: [PATCH] Bug 20691: Implement fines view

---
 .../opac-tmpl/bootstrap/en/modules/opac-account.tt | 62 ++++++++++++++++++++++
 .../opac-tmpl/bootstrap/en/modules/opac-user.tt    | 22 ++++++++
 opac/opac-account.pl                               | 27 ++++++++++
 opac/opac-user.pl                                  | 12 +++++
 4 files changed, 123 insertions(+)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-account.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-account.tt
index d5b5e346ec..072e4d87c8 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-account.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-account.tt
@@ -177,6 +177,68 @@
                     [% ELSE %]
                         <h4>You have no fines or charges</h4>
                     [% END %]
+
+                    [% FOREACH r IN relatives %]
+                        <h3 id="g[% r.patron.id %]">[% r.patron.firstname %] [% r.patron.surname %]'s fines and charges</h3>
+
+                        <table class="table table-bordered table-striped" id="finestable-[% r.id %]">
+                            <thead>
+                                <tr>
+                                    <th class="title-string">Date</th>
+                                    <th>Description</th>
+                                    <th>Fine amount</th>
+                                    <th>Amount outstanding</th>
+                                </tr>
+                            </thead>
+
+                            <tbody>
+                                [% SET account_sum = 0 %]
+                                [% FOREACH a IN r.accountlines %]
+                                    [% SET account_sum = account_sum + a.amountoutstanding %]
+                                    <tr>
+                                        <td><span title="[% a.date %]">[% a.date | $KohaDates %]</span></td>
+                                        <td>
+                                            [% SWITCH a.accounttype %]
+                                            [% CASE 'Pay' %]Payment, thanks
+                                            [% CASE 'Pay00' %]Payment, thanks (cash via SIP2)
+                                            [% CASE 'Pay01' %]Payment, thanks (VISA via SIP2)
+                                            [% CASE 'Pay02' %]Payment, thanks (credit card via SIP2)
+                                            [% CASE 'N' %]New card
+                                            [% CASE 'F' %]Fine
+                                            [% CASE 'A' %]Account management fee
+                                            [% CASE 'M' %]Sundry
+                                            [% CASE 'L' %]Lost item
+                                            [% CASE 'W' %]Writeoff
+                                            [% CASE 'FU' %]Accruing fine
+                                            [% CASE 'HE' %]Hold waiting too long
+                                            [% CASE 'Rent' %]Rental fee
+                                            [% CASE 'FOR' %]Forgiven
+                                            [% CASE 'LR' %]Lost item fee refund
+                                            [% CASE 'PF' %]Processing fee
+                                            [% CASE 'PAY' %]Payment
+                                            [% CASE 'WO' %]Writeoff
+                                            [% CASE 'C' %]Credit
+                                            [% CASE 'CR' %]Credit
+                                            [% CASE %][% a.accounttype %]
+                                          [%- END -%]
+                                          [%- IF a.payment_type %], [% AuthorisedValues.GetByCode('PAYMENT_TYPE', a.payment_type, 1) %][% END %]
+                                          [%- IF a.description %], [% a.description %][% END %]
+                                          [% IF a.itemnumber %]([% a.item.biblio.title %])[% END %]
+                                        </td>
+                                        [% IF ( a.amount < 0 ) %]<td class="credit">[% ELSE %]<td class="debit">[% END %][% a.amount | $Price %]</td>
+                                        [% IF ( a.amountoutstanding < 0 ) %]<td class="credit">[% ELSE %]<td class="debit">[% END %][% a.amountoutstanding | $Price %]</td>
+                                    </tr>
+                                [% END %]
+                            </tbody>
+
+                            <tfoot>
+                                <tr>
+                                    <th class="sum" colspan="3">Total due</th>
+                                    <td class="sum">[% account_sum | $Price %]</td>
+                                </tr>
+                            </tfoot>
+                        </table>
+                        [% END %]
                 </div> <!-- / #useraccount -->
             </div> <!-- / .span10 -->
         </div> <!-- / .row-fluid -->
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
index 4c87896b50..453d1aabfd 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
@@ -133,6 +133,8 @@
                                 [% IF ( BORROWER_INFO.amountoverfive ) %]<li><a href="#opac-user-fines">Fines ([% amountoutstanding | $Price %])</a></li>[% END %]
                                 [% IF ( BORROWER_INFO.amountoverzero ) %]<li><a href="#opac-user-fines">Fines ([% amountoutstanding | $Price %])</a></li>[% END %]
                                 [% IF ( BORROWER_INFO.amountlessthanzero ) %]<li><a href="#opac-user-fines">Credits ([% amountoutstanding | $Price %])</a></li>[% END %]
+
+                                [% IF relatives %]<li><a href="#opac-user-relative-fines">Relatives' fines</a></li>[% END %]
                             [% END %]
 
                             [% IF borrower_club_enrollments.count || borrower_enrollable_clubs.count %]
@@ -396,6 +398,26 @@
                                     </table>
                                 </div>
                             [% END %]
+
+                            [% IF relatives_with_fines %]
+                               <div id="opac-user-relative-fines"> <h3>Fines and charges</h3>
+                                    <table class="table table-bordered table-striped">
+                                        <thead>
+                                            <tr>
+                                                <th colspan="2">Amount</th>
+                                            </tr>
+                                        </thead>
+                                        <tbody>
+                                            [% FOREACH r IN relatives_with_fines %]
+                                            <tr>
+                                                <td>[% r.firstname %] [% r.surname %] currently owes fines and charges amounting to:</td>
+                                                <td><a href="/cgi-bin/koha/opac-account.pl#g[% r.id %]">[% r.account.balance | $Price %]</a></td>
+                                            </tr>
+                                            [% END %]
+                                        </tbody>
+                                    </table>
+                                </div>
+                            [% END %]
                         [% END # / OPACFinesTab %]
 
                         [% IF relatives %]
diff --git a/opac/opac-account.pl b/opac/opac-account.pl
index 9611554b58..6d9c222fe6 100755
--- a/opac/opac-account.pl
+++ b/opac/opac-account.pl
@@ -67,6 +67,33 @@ while ( my $line = $accts->next ) {
     push @accountlines, $accountline;
 }
 
+if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor')
+    || C4::Context->preference('AllowStaffToSetCheckoutsVisibilityForGuarantor')
+  )
+{
+    my @relatives;
+
+    # Filter out guarantees that don't want guarantor to see checkouts
+    foreach my $gr ( $patron->guarantee_relationships() ) {
+        my $g = $gr->guarantee;
+        if ( $g->privacy_guarantor_checkouts ) {
+
+            my $relatives_accountlines = Koha::Account::Lines->search(
+                { borrowernumber => $g->borrowernumber },
+                { order_by       => { -desc => 'accountlines_id' } }
+            );
+            push(
+                @relatives,
+                {
+                    patron       => $g,
+                    accountlines => $relatives_accountlines,
+                }
+            );
+        }
+    }
+    $template->param( relatives => \@relatives );
+}
+
 $template->param(
     ACCOUNT_LINES => \@accountlines,
     total         => sprintf( "%.2f", $total ), # FIXME Use TT plugin Price
diff --git a/opac/opac-user.pl b/opac/opac-user.pl
index a40cb60c5a..b4acebbec2 100755
--- a/opac/opac-user.pl
+++ b/opac/opac-user.pl
@@ -350,6 +350,18 @@ if (   C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor'
     $template->param( relatives => \@relatives );
 }
 
+if (   C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor')
+    || C4::Context->preference('AllowStaffToSetFinesVisibilityForGuarantor') )
+{
+    my @relatives_with_fines;
+    # Filter out guarantees that don't want guarantor to see checkouts
+    foreach my $gr ( $patron->guarantee_relationships() ) {
+        my $g = $gr->guarantee;
+        push( @relatives_with_fines, $g ) if $g->privacy_guarantor_fines;
+    }
+    $template->param( relatives_with_fines => \@relatives_with_fines );
+}
+
 $template->param(
     borrower                 => scalar Koha::Patrons->find($borrowernumber),
     patron_messages          => $patron_messages,
-- 
2.15.1 (Apple Git-101)