From e4b008e8b5e2fa8a708f318d5c994d1e4f136b67 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 3 Jun 2016 11:26:11 -0300
Subject: [PATCH] Bug 14048: Add a refund rules setting form in smart-rules

This patch introduces the CRUD UI for managing the refund lost item fee
rules on the general 'Circulation and fines rules' page.

Verify that rules can be added and changed (by adding a new one for
the same branch). The default rule is locked so it cannot be deleted.

Sponsored-by: DoverNet
Sponsored-by: South-East Kansas Library System
Sponsored-by: SWITCH Library Consortium
---
 admin/smart-rules.pl                               | 25 ++++++++++
 .../prog/en/modules/admin/smart-rules.tt           | 58 ++++++++++++++++++++++
 2 files changed, 83 insertions(+)

diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl
index 11a5637..ac6efef 100755
--- a/admin/smart-rules.pl
+++ b/admin/smart-rules.pl
@@ -30,6 +30,9 @@ use Koha::DateUtils;
 use Koha::Database;
 use Koha::IssuingRule;
 use Koha::IssuingRules;
+use Koha::Logger;
+use Koha::RefundLostItemFeeRule;
+use Koha::RefundLostItemFeeRules;
 use Koha::Libraries;
 
 my $input = CGI->new;
@@ -423,6 +426,28 @@ elsif ($op eq "add-branch-item") {
         }
     }
 }
+elsif ( $op eq 'add-refund-lost-item-fee-rule' ) {
+    my $ref_branchcode = $input->param('ref_branchcode');
+    my $ref_refund     = $input->param('ref_refund');
+    # We retrieve the object to update it, or create a new one
+    my $refundLostItemFeeRule = Koha::RefundLostItemFeeRules->find({
+                                    branchcode => $ref_branchcode
+                                }) // Koha::RefundLostItemFeeRule->new;
+    $refundLostItemFeeRule->set({
+        branchcode => $ref_branchcode,
+            refund => $ref_refund
+    })->store();
+}
+elsif ( $op eq 'del-refund-lost-item-fee-rule' ) {
+    my $ref_branchcode = $input->param('ref_branchcode');
+    eval {
+        Koha::RefundLostItemFeeRules->find({
+            branchcode => $ref_branchcode
+        })->delete;
+    };
+}
+
+$template->param( refundLostItemFeeRules => scalar Koha::RefundLostItemFeeRules->search ) ;
 
 my $branches = GetBranches();
 my @branchloop;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
index fea01f3..4a156cd 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt
@@ -2,6 +2,7 @@
 <title>Koha &rsaquo; Administration &rsaquo; Circulation and fine rules</title>
 [% INCLUDE 'doc-head-close.inc' %]
 [% INCLUDE 'calendar.inc' %]
+[% USE Branches %]
 <script type="text/javascript">
 //<![CDATA[
 
@@ -557,6 +558,63 @@ for="tobranch"><strong>Clone these rules to:</strong></label> <input type="hidde
         </form>
     </div>
     [% END %]
+
+    <div id="refund-lost-item-fee-on-return" class="container">
+    <h3>Lost item fee refund on return policy</h3>
+        <p>Specify the default policy for lost item fees on return.
+        </p>
+        <form method="post" action="/cgi-bin/koha/admin/smart-rules.pl">
+            <input type="hidden" name="op" value="add-refund-lost-item-fee-rule" />
+            <table>
+                <tr>
+                    <th>Library</th>
+                    <th>Refund lost item fee</th>
+                    <th>&nbsp;</th>
+                </tr>
+            [% FOREACH refundLostItemFeeRule IN refundLostItemFeeRules %]
+                <tr>
+                    <td>
+                [% IF refundLostItemFeeRule.branchcode == '*' %]
+                        All
+                [% ELSE %]
+                        [% Branches.GetName(refundLostItemFeeRule.branchcode) %]
+                [% END %]
+                    </td>
+                    <td>
+                [% IF refundLostItemFeeRule.refund %]
+                        Yes
+                [% ELSE %]
+                        No
+                [% END %]
+                    </td>
+                    <td class="actions">
+                    [% IF refundLostItemFeeRule.branchcode != '*' %]
+                        <a class="btn btn-mini delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=del-refund-lost-item-fee-rule&amp;ref_branchcode=[% refundLostItemFeeRule.branchcode %]"><i class="fa fa-trash"></i> Delete</a>
+                    [% END %]
+                    </td>
+                </tr>
+            [% END %]
+                <tr>
+                    <td>
+                        <select name="ref_branchcode">
+                            <option value='*'>All</option>
+                        [% FOREACH branch IN Branches.all %]
+                            <option value="[% branch.branchcode %]">[% branch.branchname %]</option>
+                        [% END %]
+                        </select>
+                    </td>
+                    <td>
+                        <select name="ref_refund">
+                            <option value="0">No</option>
+                            <option value="1">Yes</option>
+                        </select>
+                    </td>
+                    <td class="actions"><button type="submit" class="btn btn-mini"><i class="fa fa-plus"></i> Add</td>
+                </tr>
+            </table>
+        </form>
+    </div>
+
     <div id="holds-policy-by-item-type" class="container">
     <h3>[% IF humanbranch %]Holds policy by item type for [% humanbranch %][% ELSE %]Default holds policy by item type[% END %]</h3>
         <p>
-- 
2.8.3