From 955e8c77cbbcf04aebc75fb2765c23a5187dfbf2 Mon Sep 17 00:00:00 2001
From: Lari Taskula <lari.taskula@hypernova.fi>
Date: Mon, 20 Apr 2020 07:47:11 +0000
Subject: [PATCH] Bug 25089: (OLD GUI) Add checkout_type to smart-rules

To test:
1. Go to Circulation rules page in Koha
2. Observe checkout type column in the rule matrix
3. Test circulation rules matrix by setting, modifying and deleting
   rules.
4. Test default checkout, hold and return policy settings.
5. Test default checkout, hold policy by patron category settings.

Sponsored-by: The National Library of Finland
---
 admin/smart-rules.pl                          | 151 ++++++-
 .../prog/en/modules/admin/smart-rules.tt      | 414 ++++++++++--------
 2 files changed, 377 insertions(+), 188 deletions(-)

diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl
index 941abdae73..d76b3da288 100755
--- a/admin/smart-rules.pl
+++ b/admin/smart-rules.pl
@@ -75,6 +75,8 @@ $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
 if ($op eq 'delete') {
     my $itemtype     = $input->param('itemtype');
     my $categorycode = $input->param('categorycode');
+    my $checkout_type = $input->param('checkout_type');
+
     $debug and warn "deleting $1 $2 $branch";
 
     Koha::CirculationRules->set_rules(
@@ -82,6 +84,7 @@ if ($op eq 'delete') {
             categorycode => $categorycode eq '*' ? undef : $categorycode,
             branchcode   => $branch eq '*' ? undef : $branch,
             itemtype     => $itemtype eq '*' ? undef : $itemtype,
+            checkout_type => $checkout_type eq '*' ? undef : $checkout_type,
             rules        => {
                 maxissueqty                      => undef,
                 rentaldiscount                   => undef,
@@ -125,7 +128,26 @@ elsif ($op eq 'delete-branch-cat') {
                     categorycode => undef,
                     rules        => {
                         max_holds                      => undef,
-                        patron_maxissueqty             => undef,
+                    }
+                }
+            );
+            Koha::CirculationRules->set_rules(
+                {
+                    branchcode   => undef,
+                    categorycode => undef,
+                    checkout_type => $Koha::Checkouts::type->{checkout},
+                    rules        => {
+                        patron_maxissueqty                      => undef,
+                    }
+                }
+            );
+            Koha::CirculationRules->set_rules(
+                {
+                    branchcode   => undef,
+                    categorycode => undef,
+                    checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                    rules        => {
+                        patron_maxissueqty       => undef,
                     }
                 }
             );
@@ -147,6 +169,25 @@ elsif ($op eq 'delete-branch-cat') {
                     branchcode   => undef,
                     rules        => {
                         max_holds                => undef,
+                    }
+                }
+            );
+            Koha::CirculationRules->set_rules(
+                {
+                    categorycode => $categorycode,
+                    branchcode   => undef,
+                    checkout_type => $Koha::Checkouts::type->{checkout},
+                    rules        => {
+                        patron_maxissueqty       => undef,
+                    }
+                }
+            );
+            Koha::CirculationRules->set_rules(
+                {
+                    categorycode => $categorycode,
+                    branchcode   => undef,
+                    checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                    rules        => {
                         patron_maxissueqty       => undef,
                     }
                 }
@@ -157,6 +198,17 @@ elsif ($op eq 'delete-branch-cat') {
             {
                 branchcode   => $branch,
                 categorycode => undef,
+                checkout_type => $Koha::Checkouts::type->{checkout},
+                rules        => {
+                    patron_maxissueqty       => undef,
+                }
+            }
+        );
+        Koha::CirculationRules->set_rules(
+            {
+                branchcode   => $branch,
+                categorycode => undef,
+                checkout_type => $Koha::Checkouts::type->{onsite_checkout},
                 rules        => {
                     patron_maxissueqty       => undef,
                 }
@@ -180,7 +232,26 @@ elsif ($op eq 'delete-branch-cat') {
                 branchcode   => $branch,
                 rules        => {
                     max_holds         => undef,
-                    patron_maxissueqty       => undef,
+                }
+            }
+        );
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => $categorycode,
+                branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{checkout},
+                rules        => {
+                    patron_maxissueqty => undef,
+                }
+            }
+        );
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => $categorycode,
+                branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                rules        => {
+                    patron_maxissueqty => undef,
                 }
             }
         );
@@ -245,6 +316,7 @@ elsif ($op eq 'add') {
     my $br = $branch; # branch
     my $bor  = $input->param('categorycode'); # borrower category
     my $itemtype  = $input->param('itemtype');     # item type
+    my $checkout_type = $input->param('checkout_type');
     my $fine = $input->param('fine');
     my $finedays     = $input->param('finedays');
     my $maxsuspensiondays = $input->param('maxsuspensiondays');
@@ -320,6 +392,7 @@ elsif ($op eq 'add') {
             categorycode => $bor eq '*' ? undef : $bor,
             itemtype     => $itemtype eq '*' ? undef : $itemtype,
             branchcode   => $br eq '*' ? undef : $br,
+            checkout_type => $checkout_type eq '*' ? undef : $checkout_type,
             rules        => $rules,
         }
     );
@@ -328,6 +401,7 @@ elsif ($op eq 'add') {
 elsif ($op eq "set-branch-defaults") {
     my $categorycode  = $input->param('categorycode');
     my $patron_maxissueqty   = strip_non_numeric($input->param('patron_maxissueqty'));
+    my $patron_maxonsiteissueqty   = strip_non_numeric($input->param('patron_maxonsiteissueqty'));
     my $holdallowed   = $input->param('holdallowed');
     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
     my $returnbranch  = $input->param('returnbranch');
@@ -351,11 +425,22 @@ elsif ($op eq "set-branch-defaults") {
             {
                 categorycode => undef,
                 branchcode   => undef,
+                checkout_type => $Koha::Checkouts::type->{checkout},
                 rules        => {
                     patron_maxissueqty             => $patron_maxissueqty,
                 }
             }
         );
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => undef,
+                branchcode   => undef,
+                checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                rules        => {
+                    patron_maxissueqty             => $patron_maxonsiteissueqty,
+                }
+            }
+        );
     } else {
         Koha::CirculationRules->set_rules(
             {
@@ -372,11 +457,22 @@ elsif ($op eq "set-branch-defaults") {
             {
                 categorycode => undef,
                 branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{checkout},
                 rules        => {
                     patron_maxissueqty             => $patron_maxissueqty,
                 }
             }
         );
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => undef,
+                branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                rules        => {
+                    patron_maxissueqty             => $patron_maxonsiteissueqty,
+                }
+            }
+        );
     }
     Koha::CirculationRules->set_rule(
         {
@@ -390,6 +486,7 @@ elsif ($op eq "set-branch-defaults") {
 elsif ($op eq "add-branch-cat") {
     my $categorycode  = $input->param('categorycode');
     my $patron_maxissueqty = strip_non_numeric($input->param('patron_maxissueqty'));
+    my $patron_maxonsiteissueqty = strip_non_numeric($input->param('patron_maxonsiteissueqty'));
     my $max_holds = $input->param('max_holds');
     $max_holds =~ s/\s//g;
     $max_holds = undef if $max_holds !~ /^\d+/;
@@ -400,46 +497,94 @@ elsif ($op eq "add-branch-cat") {
                 {
                     categorycode => undef,
                     branchcode   => undef,
+                    checkout_type => $Koha::Checkouts::type->{checkout},
                     rules        => {
                         max_holds         => $max_holds,
                         patron_maxissueqty       => $patron_maxissueqty,
                     }
                 }
             );
+            Koha::CirculationRules->set_rules(
+                {
+                    categorycode => undef,
+                    branchcode   => undef,
+                    checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                    rules        => {
+                        max_holds         => $max_holds,
+                        patron_maxissueqty       => $patron_maxonsiteissueqty,
+                    }
+                }
+            );
         } else {
             Koha::CirculationRules->set_rules(
                 {
                     categorycode => $categorycode,
                     branchcode   => undef,
+                    checkout_type => $Koha::Checkouts::type->{checkout},
                     rules        => {
                         max_holds         => $max_holds,
                         patron_maxissueqty       => $patron_maxissueqty,
                     }
                 }
             );
+            Koha::CirculationRules->set_rules(
+                {
+                    categorycode => $categorycode,
+                    branchcode   => undef,
+                    checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                    rules        => {
+                        max_holds         => $max_holds,
+                        patron_maxissueqty       => $patron_maxonsiteissueqty,
+                    }
+                }
+            );
         }
     } elsif ($categorycode eq "*") {
         Koha::CirculationRules->set_rules(
             {
                 categorycode => undef,
                 branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{checkout},
                 rules        => {
                     max_holds         => $max_holds,
                     patron_maxissueqty       => $patron_maxissueqty,
                 }
             }
         );
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => undef,
+                branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                rules        => {
+                    max_holds         => $max_holds,
+                    patron_maxissueqty       => $patron_maxonsiteissueqty,
+                }
+            }
+        );
     } else {
         Koha::CirculationRules->set_rules(
             {
                 categorycode => $categorycode,
                 branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{checkout},
                 rules        => {
                     max_holds         => $max_holds,
                     patron_maxissueqty       => $patron_maxissueqty,
                 }
             }
         );
+        Koha::CirculationRules->set_rules(
+            {
+                categorycode => $categorycode,
+                branchcode   => $branch,
+                checkout_type => $Koha::Checkouts::type->{onsite_checkout},
+                rules        => {
+                    max_holds         => $max_holds,
+                    patron_maxissueqty       => $patron_maxonsiteissueqty,
+                }
+            }
+        );
     }
 }
 elsif ($op eq "add-branch-item") {
@@ -549,7 +694,7 @@ my $definedbranch = $all_rules->count ? 1 : 0;
 my $rules = {};
 while ( my $r = $all_rules->next ) {
     $r = $r->unblessed;
-    $rules->{ $r->{categorycode} }->{ $r->{itemtype} }->{ $r->{rule_name} } = $r->{rule_value};
+    $rules->{ $r->{categorycode} }->{ $r->{itemtype} }->{ $r->{checkout_type} }->{ $r->{rule_name} } = $r->{rule_value};
 }
 
 $template->param(show_branch_cat_rule_form => 1);
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 60b0d19eac..f35a5f4a21 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
@@ -5,6 +5,7 @@
 [% USE Branches %]
 [% USE Categories %]
 [% USE ItemTypes %]
+[% USE Checkouts %]
 [% USE CirculationRules %]
 [% SET footerjs = 1 %]
 
@@ -22,6 +23,12 @@
 [% END %]
 [% itemtypes.push(undef) %]
 
+[% SET checkout_types = [] %]
+[% FOREACH ct IN Checkouts.type %]
+    [% checkout_types.push( ct.value ) %]
+[% END %]
+[% checkout_types.push(undef) %]
+
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Administration &rsaquo; Circulation and fine rules</title>
 [% INCLUDE 'doc-head-close.inc' %]
@@ -88,6 +95,7 @@
             <tr>
                 <th>Patron category</th>
                 <th>Item type</th>
+                <th>Checkout type</th>
                 <th>Actions</th>
                 <th>Note</th>
                 <th>Current checkouts allowed</th>
@@ -125,178 +133,191 @@
                     [% SET c = '' UNLESS c.defined %]
                     [% FOREACH i IN itemtypes %]
                         [% SET i = '' UNLESS i.defined %]
-                        [% SET note = all_rules.$c.$i.note %]
-                        [% SET maxissueqty = all_rules.$c.$i.maxissueqty %]
-                        [% SET issuelength = all_rules.$c.$i.issuelength %]
-                        [% SET lengthunit = all_rules.$c.$i.lengthunit %]
-                        [% SET hardduedate = all_rules.$c.$i.hardduedate %]
-                        [% SET hardduedatecompare = all_rules.$c.$i.hardduedatecompare %]
-                        [% SET fine = all_rules.$c.$i.fine %]
-                        [% SET chargeperiod = all_rules.$c.$i.chargeperiod %]
-                        [% SET chargeperiod_charge_at = all_rules.$c.$i.chargeperiod_charge_at %]
-                        [% SET firstremind = all_rules.$c.$i.firstremind %]
-                        [% SET overduefinescap = all_rules.$c.$i.overduefinescap %]
-                        [% SET cap_fine_to_replacement_price = all_rules.$c.$i.cap_fine_to_replacement_price %]
-                        [% SET finedays = all_rules.$c.$i.finedays %]
-                        [% SET maxsuspensiondays = all_rules.$c.$i.maxsuspensiondays %]
-                        [% SET suspension_chargeperiod = all_rules.$c.$i.suspension_chargeperiod %]
-                        [% SET renewalsallowed = all_rules.$c.$i.renewalsallowed %]
-                        [% SET renewalperiod = all_rules.$c.$i.renewalperiod %]
-                        [% SET norenewalbefore = all_rules.$c.$i.norenewalbefore %]
-                        [% SET auto_renew = all_rules.$c.$i.auto_renew %]
-                        [% SET no_auto_renewal_after = all_rules.$c.$i.no_auto_renewal_after %]
-                        [% SET no_auto_renewal_after_hard_limit = all_rules.$c.$i.no_auto_renewal_after_hard_limit %]
-                        [% SET reservesallowed = all_rules.$c.$i.reservesallowed %]
-                        [% SET holds_per_day = all_rules.$c.$i.holds_per_day %]
-                        [% SET holds_per_record = all_rules.$c.$i.holds_per_record %]
-                        [% SET onshelfholds = all_rules.$c.$i.onshelfholds %]
-                        [% SET opacitemholds = all_rules.$c.$i.opacitemholds %]
-                        [% SET article_requests = all_rules.$c.$i.article_requests %]
-                        [% SET rentaldiscount = all_rules.$c.$i.rentaldiscount %]
-
-                        [% SET show_rule = maxissueqty || issuelength || lengthunit || hardduedate || hardduedatebefore || hardduedateexact || fine || chargeperiod || chargeperiod_charge_at || firstremind || overduefinescap || cap_fine_to_replacement_price || finedays || maxsuspensiondays || suspension_chargeperiod || renewalsallowed || renewalsallowed || norenewalbefore || auto_renew || no_auto_renewal_after || no_auto_renewal_after_hard_limit || reservesallowed || holds_per_day || holds_per_record || onshelfholds || opacitemholds || article_requests || article_requests %]
-                        [% IF show_rule %]
-                            [% SET row_count = row_count + 1 %]
-                            <tr row_countd="row_[% row_count | html %]">
-                                    <td>
-                                        [% IF c == undef %]
-                                            <em>All</em>
-                                        [% ELSE %]
-                                            [% Categories.GetName(c) | html %]
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                        [% IF i == undef %]
-                                            <em>All</em>
-                                        [% ELSE %]
-                                            [% ItemTypes.GetDescription(i) | html %]
-                                        [% END %]
-                                    </td>
-                                    <td class="actions">
-                                      <a href="#" class="editrule btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
-                                      <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete&amp;itemtype=[% i || '*' | html %]&amp;categorycode=[% c || '*' | html %]&amp;branch=[% current_branch | html %]"><i class="fa fa-trash"></i> Delete</a>
-                                    </td>
-                                    <td>
-                                        [% IF note.defined && note != '' %]
-                                            <a name="viewnote" data-toggle="popover" title="Note" data-content="[% note | html %]" data-placement="top" data-trigger="hover">View note</a>
-                                        [% ELSE %]<span>&nbsp;</span>[% END %]
-                                    </td>
-                                    <td>
-                                        [% IF maxissueqty.defined && maxissueqty != '' %]
-                                            [% maxissueqty | html %]
-                                        [% ELSE %]
-                                            <span>Unlimited</span>
-                                        [% END %]
-                                    </td>
-                                    <td>[% issuelength | html %]</td>
-                                    <td>
-                                        [% IF ( lengthunit == 'days' ) %]
-                                            Days
-                                        [% ELSIF ( lengthunit == 'hours') %]
-                                            Hours
-                                        [% ELSE %]
-                                            Undefined
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                      [% IF ( hardduedate ) %]
-                                        [% IF ( hardduedatecompare == '-1' ) %]
-                                          before [% hardduedate | $KohaDates %]
-                                          <input type="hidden" name="hardduedatecomparebackup" value="-1" />
-                                        [% ELSIF ( hardduedatecompare == '0' ) %]
-                                          on [% hardduedate | $KohaDates %]
-                                          <input type="hidden" name="hardduedatecomparebackup" value="0" />
-                                        [% ELSIF ( hardduedatecompare == '1' ) %]
-                                          after [% hardduedate | $KohaDates %]
-                                          <input type="hidden" name="hardduedatecomparebackup" value="1" />
-                                        [% END %]
-                                      [% ELSE %]
-                                        <span>None defined</span>
-                                      [% END %]
-                                    </td>
-                                    <td>[% fine | html %]</td>
-                                    <td>[% chargeperiod | html %]</td>
-                                    <td>[% IF chargeperiod_charge_at %]Start of interval[% ELSE %]End of interval[% END %]</td>
-                                    <td>[% firstremind | html %]</td>
-                                    <td>[% overduefinescap FILTER format("%.2f") %]</td>
-                                    <td>
-                                        [% IF cap_fine_to_replacement_price %]
-                                            <input type="checkbox" checked="checked" disabled="disabled" />
-                                        [% ELSE %]
-                                            <input type="checkbox" disabled="disabled" />
-                                        [% END %]
-                                    </td>
-                                    <td>[% finedays | html %]</td>
-                                    <td>[% maxsuspensiondays | html %]</td>
-                                    <td>[% suspension_chargeperiod | html %]</td>
-                                    <td>[% renewalsallowed | html %]</td>
-                                    <td>[% renewalperiod | html %]</td>
-                                    <td>[% norenewalbefore | html %]</td>
-                                    <td>
-                                        [% IF auto_renew %]
-                                            <span>Yes</span>
-                                        [% ELSE %]
-                                            <span>No</span>
-                                        [% END %]
-                                    </td>
-                                    <td>[% no_auto_renewal_after | html %]</td>
-                                    <td>[% no_auto_renewal_after_hard_limit | $KohaDates %]</td>
-                                    <td>
-                                        [% IF reservesallowed.defined && reservesallowed != '' %]
-                                            [% reservesallowed | html %]
-                                        [% ELSE %]
-                                            <span>Unlimited</span>
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                        [% IF holds_per_day.defined && holds_per_day != '' %]
-                                            [% holds_per_day | html %]
-                                        [% ELSE %]
-                                            <span>Unlimited</span>
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                        [% IF holds_per_record.defined && holds_per_record != '' %]
-                                            [% holds_per_record | html %]
-                                        [% ELSE %]
-                                            <span>Unlimited</span>
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                        [% IF onshelfholds == 1 %]
-                                            <span>Yes</span>
-                                        [% ELSIF onshelfholds == 2 %]
-                                            <span>If all unavailable</span>
-                                        [% ELSE %]
-                                            <span>If any unavailable</span>
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                        [% IF opacitemholds == 'F'%]
-                                            <span>Force</span>
-                                        [% ELSIF opacitemholds == 'Y'%]
-                                            <span>Allow</span>
-                                        [% ELSE %]
-                                            <span>Don't allow</span>
-                                        [% END %]
-                                    </td>
-                                    <td>
-                                        [% IF article_requests == 'no' %]
-                                            <span>No</span>
-                                        [% ELSIF article_requests == 'yes' %]
-                                            <span>Yes</span>
-                                        [% ELSIF article_requests == 'bib_only' %]
-                                            <span>Record only</span>
-                                        [% ELSIF article_requests == 'item_only' %]
-                                            <span>Item only</span>
-                                        [% END %]
-                                    </td>
-                                    <td>[% rentaldiscount | html %]</td>
-                                    <td class="actions">
-                                      <a href="#" class="editrule btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
-                                      <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete&amp;itemtype=[% i || '*' | uri %]&amp;categorycode=[% c || '*' | uri %]&amp;branch=[% current_branch | uri %]"><i class="fa fa-trash"></i> Delete</a>
-                                    </td>
-                            </tr>
+                        [% FOREACH ct IN checkout_types %]
+                            [% SET ct = '' UNLESS ct.defined %]
+                            [% SET note = all_rules.$c.$i.$ct.note %]
+                            [% SET maxissueqty = all_rules.$c.$i.$ct.maxissueqty %]
+                            [% SET issuelength = all_rules.$c.$i.$ct.issuelength %]
+                            [% SET lengthunit = all_rules.$c.$i.$ct.lengthunit %]
+                            [% SET hardduedate = all_rules.$c.$i.$ct.hardduedate %]
+                            [% SET hardduedatecompare = all_rules.$c.$i.$ct.hardduedatecompare %]
+                            [% SET fine = all_rules.$c.$i.$ct.fine %]
+                            [% SET chargeperiod = all_rules.$c.$i.$ct.chargeperiod %]
+                            [% SET chargeperiod_charge_at = all_rules.$c.$i.$ct.chargeperiod_charge_at %]
+                            [% SET firstremind = all_rules.$c.$i.$ct.firstremind %]
+                            [% SET overduefinescap = all_rules.$c.$i.$ct.overduefinescap %]
+                            [% SET cap_fine_to_replacement_price = all_rules.$c.$i.$ct.cap_fine_to_replacement_price %]
+                            [% SET finedays = all_rules.$c.$i.$ct.finedays %]
+                            [% SET maxsuspensiondays = all_rules.$c.$i.$ct.maxsuspensiondays %]
+                            [% SET suspension_chargeperiod = all_rules.$c.$i.$ct.suspension_chargeperiod %]
+                            [% SET renewalsallowed = all_rules.$c.$i.$ct.renewalsallowed %]
+                            [% SET renewalperiod = all_rules.$c.$i.$ct.renewalperiod %]
+                            [% SET norenewalbefore = all_rules.$c.$i.$ct.norenewalbefore %]
+                            [% SET auto_renew = all_rules.$c.$i.$ct.auto_renew %]
+                            [% SET no_auto_renewal_after = all_rules.$c.$i.$ct.no_auto_renewal_after %]
+                            [% SET no_auto_renewal_after_hard_limit = all_rules.$c.$i.$ct.no_auto_renewal_after_hard_limit %]
+                            [% SET reservesallowed = all_rules.$c.$i.$ct.reservesallowed %]
+                            [% SET holds_per_day = all_rules.$c.$i.$ct.holds_per_day %]
+                            [% SET holds_per_record = all_rules.$c.$i.$ct.holds_per_record %]
+                            [% SET onshelfholds = all_rules.$c.$i.$ct.onshelfholds %]
+                            [% SET opacitemholds = all_rules.$c.$i.$ct.opacitemholds %]
+                            [% SET article_requests = all_rules.$c.$i.$ct.article_requests %]
+                            [% SET rentaldiscount = all_rules.$c.$i.$ct.rentaldiscount %]
+                            [% SET show_rule = maxissueqty || issuelength || lengthunit || hardduedate || hardduedatebefore || hardduedateexact || fine || chargeperiod || chargeperiod_charge_at || firstremind || overduefinescap || cap_fine_to_replacement_price || finedays || maxsuspensiondays || suspension_chargeperiod || renewalsallowed || renewalsallowed || norenewalbefore || auto_renew || no_auto_renewal_after || no_auto_renewal_after_hard_limit || reservesallowed || holds_per_day || holds_per_record || onshelfholds || opacitemholds || article_requests || article_requests %]
+                            [% IF show_rule %]
+                                [% SET row_count = row_count + 1 %]
+                                <tr row_countd="row_[% row_count | html %]">
+                                        <td>
+                                            [% IF c == undef %]
+                                                <em>All</em>
+                                            [% ELSE %]
+                                                [% Categories.GetName(c) | html %]
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF i == undef %]
+                                                <em>All</em>
+                                            [% ELSE %]
+                                                [% ItemTypes.GetDescription(i) | html %]
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF ct == undef %]
+                                                <em>All</em>
+                                            [% ELSE %]
+                                                [% IF ct == 'CHECKOUT' %]
+                                                    <em>Normal</em>
+                                                [% ELSIF ct == 'ONSITE' %]
+                                                    <em>On-site</em>
+                                                [% END %]
+                                            [% END %]
+                                        </td>
+                                        <td class="actions">
+                                          <a href="#" class="editrule btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
+                                          <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete&amp;itemtype=[% i || '*' | html %]&amp;categorycode=[% c || '*' | html %]&amp;branch=[% current_branch | html %]"><i class="fa fa-trash"></i> Delete</a>
+                                        </td>
+                                        <td>
+                                            [% IF note.defined && note != '' %]
+                                                <a name="viewnote" data-toggle="popover" title="Note" data-content="[% note | html %]" data-placement="top" data-trigger="hover">View note</a>
+                                            [% ELSE %]<span>&nbsp;</span>[% END %]
+                                        </td>
+                                        <td>
+                                            [% IF maxissueqty.defined && maxissueqty != '' %]
+                                                [% maxissueqty | html %]
+                                            [% ELSE %]
+                                                <span>Unlimited</span>
+                                            [% END %]
+                                        </td>
+                                        <td>[% issuelength | html %]</td>
+                                        <td>
+                                            [% IF ( lengthunit == 'days' ) %]
+                                                Days
+                                            [% ELSIF ( lengthunit == 'hours') %]
+                                                Hours
+                                            [% ELSE %]
+                                                Undefined
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                          [% IF ( hardduedate ) %]
+                                            [% IF ( hardduedatecompare == '-1' ) %]
+                                              before [% hardduedate | $KohaDates %]
+                                              <input type="hidden" name="hardduedatecomparebackup" value="-1" />
+                                            [% ELSIF ( hardduedatecompare == '0' ) %]
+                                              on [% hardduedate | $KohaDates %]
+                                              <input type="hidden" name="hardduedatecomparebackup" value="0" />
+                                            [% ELSIF ( hardduedatecompare == '1' ) %]
+                                              after [% hardduedate | $KohaDates %]
+                                              <input type="hidden" name="hardduedatecomparebackup" value="1" />
+                                            [% END %]
+                                          [% ELSE %]
+                                            <span>None defined</span>
+                                          [% END %]
+                                        </td>
+                                        <td>[% fine | html %]</td>
+                                        <td>[% chargeperiod | html %]</td>
+                                        <td>[% IF chargeperiod_charge_at %]Start of interval[% ELSE %]End of interval[% END %]</td>
+                                        <td>[% firstremind | html %]</td>
+                                        <td>[% overduefinescap FILTER format("%.2f") %]</td>
+                                        <td>
+                                            [% IF cap_fine_to_replacement_price %]
+                                                <input type="checkbox" checked="checked" disabled="disabled" />
+                                            [% ELSE %]
+                                                <input type="checkbox" disabled="disabled" />
+                                            [% END %]
+                                        </td>
+                                        <td>[% finedays | html %]</td>
+                                        <td>[% maxsuspensiondays | html %]</td>
+                                        <td>[% suspension_chargeperiod | html %]</td>
+                                        <td>[% renewalsallowed | html %]</td>
+                                        <td>[% renewalperiod | html %]</td>
+                                        <td>[% norenewalbefore | html %]</td>
+                                        <td>
+                                            [% IF auto_renew %]
+                                                <span>Yes</span>
+                                            [% ELSE %]
+                                                <span>No</span>
+                                            [% END %]
+                                        </td>
+                                        <td>[% no_auto_renewal_after | html %]</td>
+                                        <td>[% no_auto_renewal_after_hard_limit | $KohaDates %]</td>
+                                        <td>
+                                            [% IF reservesallowed.defined && reservesallowed != '' %]
+                                                [% reservesallowed | html %]
+                                            [% ELSE %]
+                                                <span>Unlimited</span>
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF holds_per_day.defined && holds_per_day != '' %]
+                                                [% holds_per_day | html %]
+                                            [% ELSE %]
+                                                <span>Unlimited</span>
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF holds_per_record.defined && holds_per_record != '' %]
+                                                [% holds_per_record | html %]
+                                            [% ELSE %]
+                                                <span>Unlimited</span>
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF onshelfholds == 1 %]
+                                                <span>Yes</span>
+                                            [% ELSIF onshelfholds == 2 %]
+                                                <span>If all unavailable</span>
+                                            [% ELSE %]
+                                                <span>If any unavailable</span>
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF opacitemholds == 'F'%]
+                                                <span>Force</span>
+                                            [% ELSIF opacitemholds == 'Y'%]
+                                                <span>Allow</span>
+                                            [% ELSE %]
+                                                <span>Don't allow</span>
+                                            [% END %]
+                                        </td>
+                                        <td>
+                                            [% IF article_requests == 'no' %]
+                                                <span>No</span>
+                                            [% ELSIF article_requests == 'yes' %]
+                                                <span>Yes</span>
+                                            [% ELSIF article_requests == 'bib_only' %]
+                                                <span>Record only</span>
+                                            [% ELSIF article_requests == 'item_only' %]
+                                                <span>Item only</span>
+                                            [% END %]
+                                        </td>
+                                        <td>[% rentaldiscount | html %]</td>
+                                        <td class="actions">
+                                          <a href="#" class="editrule btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
+                                          <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete&amp;itemtype=[% i || '*' | uri %]&amp;categorycode=[% c || '*' | uri %]&amp;branch=[% current_branch | uri %]"><i class="fa fa-trash"></i> Delete</a>
+                                        </td>
+                                </tr>
+                            [% END %]
                         [% END %]
                     [% END %]
                 [% END %]
@@ -317,6 +338,13 @@
                         [% END %]
                         </select>
                     </td>
+                    <td>
+                        <select name="checkout_type" id="matrixcheckout_type" style="width:13em;">
+                            <option value="*">All</option>
+                            <option value="CHECKOUT">Normal checkout</option>
+                            <option value="ONSITE">On-site checkout</option>
+                        </select>
+                    </td>
                     <td class="actions">
                         <input type="hidden" name="branch" value="[% current_branch | html %]"/>
                         <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-save"></i> Save</button>
@@ -404,6 +432,7 @@
                     <tr>
                       <th>Patron category</th>
                       <th>Item type</th>
+                      <th>Checkout type</th>
                       <th>&nbsp;</th>
                       <th>Note</th>
                       <th>Current checkouts allowed</th>
@@ -449,6 +478,7 @@
                 <tr>
                     <th>&nbsp;</th>
                     <th>Total current checkouts allowed</th>
+                    <th>Total current on-site checkouts allowed</th>
                     <th>Maximum total holds allowed (count)</th>
                     <th>Hold policy</th>
                     <th>Hold pickup library match</th>
@@ -458,16 +488,20 @@
                 <tr>
                     <td><em>Defaults</em></td>
                     <td>
-                        [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, 'patron_maxissueqty' ) %]
+                        [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, Checkouts.type.checkout, 'patron_maxissueqty' ) %]
                         <input type="text" name="patron_maxissueqty" size="3" value="[% patron_maxissueqty | html %]"/>
                     </td>
                     <td>
-                        [% SET rule_value = CirculationRules.Search( current_branch, undef , undef, 'max_holds' ) %]
+                        [% SET patron_maxissueqty = CirculationRules.Search( current_branch, undef, undef, Checkouts.type.onsite_checkout, 'patron_maxissueqty' ) %]
+                        <input type="text" name="patron_maxonsiteissueqty" size="3" value="[% patron_maxissueqty | html %]"/>
+                    </td>
+                    <td>
+                        [% SET rule_value = CirculationRules.Search( current_branch, undef , undef, undef, 'max_holds' ) %]
                         <input name="max_holds" size="3" value="[% rule_value | html %]" />
                     </td>
                     <td>
                         <select name="holdallowed">
-                            [% SET holdallowed = CirculationRules.Search( current_branch, undef, undef, 'holdallowed' ) %]
+                            [% SET holdallowed = CirculationRules.Search( current_branch, undef, undef, undef, 'holdallowed' ) %]
                             <option value="">
                                 Not set
                             </option>
@@ -507,7 +541,7 @@
                     </td>
                     <td>
                         <select name="hold_fulfillment_policy">
-                            [% SET hold_fulfillment_policy = CirculationRules.Search( current_branch, undef, undef, 'hold_fulfillment_policy' ) %]
+                            [% SET hold_fulfillment_policy = CirculationRules.Search( current_branch, undef, undef, undef, 'hold_fulfillment_policy' ) %]
 
                             <option value="">
                                 Not set
@@ -566,7 +600,7 @@
                     </td>
                     <td>
                         <select name="returnbranch">
-                            [% SET returnbranch = CirculationRules.Search( current_branch, undef, undef, 'returnbranch' ) %]
+                            [% SET returnbranch = CirculationRules.Search( current_branch, undef, undef, undef, 'returnbranch' ) %]
 
                             <option value="">
                                 Not set
@@ -619,13 +653,15 @@
                 <tr>
                     <th>Patron category</th>
                     <th>Total current checkouts allowed</th>
+                    <th>Total current on-site checkouts allowed</th>
                     <th>Total holds allowed</th>
                     <th>&nbsp;</th>
                 </tr>
                 [% FOREACH c IN categorycodes %]
                     [% NEXT UNLESS c %]
-                    [% SET patron_maxissueqty = CirculationRules.Search( branchcode, c, undef, 'patron_maxissueqty' ) %]
-                    [% SET max_holds = CirculationRules.Search( branchcode, c, undef, 'max_holds' ) %]
+                    [% SET patron_maxissueqty = CirculationRules.Search( branchcode, c, undef, Checkouts.type.checkout, 'patron_maxissueqty' ) %]
+                    [% SET patron_maxonsiteissueqty = CirculationRules.Search( branchcode, c, undef, Checkouts.type.onsite_checkout, 'patron_maxissueqty' ) %]
+                    [% SET max_holds = CirculationRules.Search( branchcode, c, undef, undef, 'max_holds' ) %]
 
                     [% IF  ( patron_maxissueqty.defined && patron_maxissueqty != '' ) || ( max_holds.defined && max_holds != '' ) %]
                     <tr>
@@ -643,6 +679,13 @@
                                 <span>Unlimited</span>
                             [% END %]
                         </td>
+                        <td>
+                            [% IF patron_maxonsiteissueqty.defined && patron_maxonsiteissueqty != '' %]
+                                [% patron_maxonsiteissueqty | html %]
+                            [% ELSE %]
+                                <span>Unlimited</span>
+                            [% END %]
+                        </td>
                         <td>
                             [% IF max_holds.defined && max_holds != '' %]
                                 [% max_holds | html %]
@@ -666,6 +709,7 @@
                         </select>
                     </td>
                     <td><input name="patron_maxissueqty" size="3" type="text" /></td>
+                    <td><input name="patron_maxonsiteissueqty" size="3" type="text" /></td>
                     <td><input name="max_holds" size="3" type="text" /></td>
                     <td class="actions"><button type="submit" class="btn btn-default btn-xs"><i class="fa fa-plus"></i> Add</td>
                 </tr>
@@ -785,9 +829,9 @@
                     <th>&nbsp;</th>
                 </tr>
                 [% FOREACH i IN itemtypeloop %]
-                    [% SET holdallowed = CirculationRules.Search( branchcode, undef, i.itemtype, 'holdallowed' ) %]
-                    [% SET hold_fulfillment_policy = CirculationRules.Search( branchcode, undef, i.itemtype, 'hold_fulfillment_policy' ) %]
-                    [% SET returnbranch = CirculationRules.Search( branchcode, undef, i.itemtype, 'returnbranch' ) %]
+                    [% SET holdallowed = CirculationRules.Search( branchcode, undef, i.itemtype, undef, 'holdallowed' ) %]
+                    [% SET hold_fulfillment_policy = CirculationRules.Search( branchcode, undef, i.itemtype, undef, 'hold_fulfillment_policy' ) %]
+                    [% SET returnbranch = CirculationRules.Search( branchcode, undef, i.itemtype, undef, 'returnbranch' ) %]
 
                     [% IF holdallowed || hold_fulfillment_policy || returnbranch %]
                         <tr>
@@ -962,7 +1006,7 @@
                         // specific processing for the Note column
                         var note = $(this).find("a[name='viewnote']").data("content");
                         $(current_column).find("input[type='text']").val(note);
-                    } else if ( i == 7 ) {
+                    } else if ( i == 8 ) {
                         // specific processing for the Hard due date column
                         var select_value = $(this).find("input[type='hidden'][name='hardduedatecomparebackup']").val();
                         var input_value = '';
@@ -973,7 +1017,7 @@
                         }
                         $(current_column).find("input[type='text']").val(input_value);
                         $(current_column).find("select").val(select_value);
-                    } else if ( i == 13 ) {
+                    } else if ( i == 14 ) {
                         // specific processing for cap_fine_to_replacement_price
                         var cap_fine_to_replacement_price = $(this).find("input[type='checkbox']");
                         $('#cap_fine_to_replacement_price').prop('checked', cap_fine_to_replacement_price.is(':checked') );
@@ -998,7 +1042,7 @@
                             // Remove potential previous input added
                             $(current_column).find("input").remove();
                             $(current_column).append("<input type='hidden' name='"+name+"' value='"+val+"' />");
-                        } else if ( i == 4 || i == 23 || i == 24 || i == 25 ) {
+                        } else if ( i == 4 || i == 24 || i == 25 || i == 26 ) {
                             // If the value is not an integer for
                             //     - "Current checkouts allowed"
                             //     - "Holds allowed (total)"
-- 
2.17.1