Bug 33146: Unit tests
[koha-ffzg.git] / Koha / CirculationRules.pm
index fdcd9d5..f96df49 100644 (file)
@@ -4,28 +4,30 @@ package Koha::CirculationRules;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Carp qw(croak);
+use Carp qw( croak );
 
 use Koha::Exceptions;
 use Koha::CirculationRule;
+use Koha::Caches;
+use Koha::Cache::Memory::Lite;
 
 use base qw(Koha::Objects);
 
-use constant GUESSED_ITEMTYPES_KEY => 'Koha_IssuingRules_last_guess';
+use constant GUESSED_ITEMTYPES_KEY => 'Koha_CirculationRules_last_guess';
 
 =head1 NAME
 
@@ -46,10 +48,12 @@ Any attempt to set a rule with a nonsensical scope (for instance, setting the C<
 =cut
 
 our $RULE_KINDS = {
-    refund => {
+    lostreturn => {
+        scope => [ 'branchcode' ],
+    },
+    processingreturn => {
         scope => [ 'branchcode' ],
     },
-
     patron_maxissueqty => {
         scope => [ 'branchcode', 'categorycode' ],
     },
@@ -62,17 +66,27 @@ our $RULE_KINDS = {
 
     holdallowed => {
         scope => [ 'branchcode', 'itemtype' ],
+        can_be_blank => 0,
     },
     hold_fulfillment_policy => {
         scope => [ 'branchcode', 'itemtype' ],
+        can_be_blank => 0,
     },
     returnbranch => {
         scope => [ 'branchcode', 'itemtype' ],
+        can_be_blank => 0,
     },
 
     article_requests => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
+    article_request_fee => {
+        scope => [ 'branchcode', 'categorycode' ],
+    },
+    open_article_requests_limit => {
+        scope => [ 'branchcode', 'categorycode' ],
+    },
+
     auto_renew => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
@@ -100,12 +114,22 @@ our $RULE_KINDS = {
     hardduedatecompare => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
+    waiting_hold_cancellation => {
+        scope        => [ 'branchcode', 'categorycode', 'itemtype' ],
+        can_be_blank => 0,
+    },
+    holds_per_day => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
     holds_per_record => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
     issuelength => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
+    daysmode => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
     lengthunit => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
@@ -142,15 +166,45 @@ our $RULE_KINDS = {
     renewalsallowed => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
+    unseen_renewals_allowed => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
     rentaldiscount => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+        can_be_blank => 0,
     },
     reservesallowed => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
+    suspension_chargeperiod => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    note => { # This is not really a rule. Maybe we will want to separate this later.
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    decreaseloanholds => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    recalls_allowed => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    recalls_per_record => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    on_shelf_recalls => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    recall_due_date_interval => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    recall_overdue_fine => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
+    recall_shelf_time => {
+        scope => [ 'branchcode', 'categorycode', 'itemtype' ],
+    },
     # Not included (deprecated?):
     #   * accountsent
-    #   * chargename
     #   * reservecharge
     #   * restrictedtype
 };
@@ -161,6 +215,18 @@ sub rule_kinds {
 
 =head3 get_effective_rule
 
+  my $effective_rule = Koha::CirculationRules->get_effective_rule(
+    {
+        rule_name    => $name,
+        categorycode => $categorycode,
+        itemtype     => $itemtype,
+        branchcode   => $branchcode
+    }
+  );
+
+Return the effective rule object for the rule associated with the criteria passed.
+
+
 =cut
 
 sub get_effective_rule {
@@ -204,7 +270,47 @@ sub get_effective_rule {
     return $rule;
 }
 
-=head3 get_effective_rule
+=head3 get_effective_rule_value
+
+  my $effective_rule_value = Koha::CirculationRules->get_effective_rule_value(
+    {
+        rule_name    => $name,
+        categorycode => $categorycode,
+        itemtype     => $itemtype,
+        branchcode   => $branchcode
+    }
+  );
+
+Return the effective value for the rule associated with the criteria passed.
+
+This is a cached method so should be used in preference to get_effective_rule where possible
+to aid performance.
+
+=cut
+
+sub get_effective_rule_value {
+    my ( $self, $params ) = @_;
+
+    my $rule_name    = $params->{rule_name};
+    my $categorycode = $params->{categorycode};
+    my $itemtype     = $params->{itemtype};
+    my $branchcode   = $params->{branchcode};
+
+    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
+    my $cache_key = sprintf "CircRules:%s:%s:%s:%s", $rule_name // q{},
+      $categorycode // q{}, $branchcode // q{}, $itemtype // q{};
+
+    my $cached       = $memory_cache->get_from_cache($cache_key);
+    return $cached if $cached;
+
+    my $rule = $self->get_effective_rule($params);
+
+    my $value= $rule ? $rule->rule_value : undef;
+    $memory_cache->set_in_cache( $cache_key, $value );
+    return $value;
+}
+
+=head3 get_effective_rules
 
 =cut
 
@@ -218,7 +324,7 @@ sub get_effective_rules {
 
     my $r;
     foreach my $rule (@$rules) {
-        my $effective_rule = $self->get_effective_rule(
+        my $effective_rule = $self->get_effective_rule_value(
             {
                 rule_name    => $rule,
                 categorycode => $categorycode,
@@ -227,7 +333,7 @@ sub get_effective_rules {
             }
         );
 
-        $r->{$rule} = $effective_rule->rule_value if $effective_rule;
+        $r->{$rule} = $effective_rule if defined $effective_rule;
     }
 
     return $r;
@@ -267,6 +373,8 @@ sub set_rule {
     my $itemtype     = $params->{itemtype};
     my $rule_name    = $params->{rule_name};
     my $rule_value   = $params->{rule_value};
+    my $can_be_blank = defined $kind_info->{can_be_blank} ? $kind_info->{can_be_blank} : 1;
+    $rule_value = undef if defined $rule_value && $rule_value eq "" && !$can_be_blank;
 
     for my $v ( $branchcode, $categorycode, $itemtype ) {
         $v = undef if $v and $v eq '*';
@@ -304,6 +412,11 @@ sub set_rule {
         }
     }
 
+    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
+    for my $k ( $memory_cache->all_keys ) {
+        $memory_cache->clear_from_cache($k) if $k =~ m{^CircRules:};
+    }
+
     return $rule;
 }
 
@@ -349,6 +462,67 @@ sub delete {
     }
 }
 
+=head3 clone
+
+Clone a set of circulation rules to another branch
+
+=cut
+
+sub clone {
+    my ( $self, $to_branch ) = @_;
+
+    while ( my $rule = $self->next ){
+        $rule->clone($to_branch);
+    }
+}
+
+=head2 get_return_branch_policy
+
+  my $returnbranch = Koha::CirculationRules->get_return_branch_policy($item);
+
+Returns the branch to use for returning the item based on the
+item type, and a branch selected via CircControlReturnsBranch.
+
+The return value is the branch to which to return the item. Possible values:
+  noreturn: do not return, let item remain where checked in (floating collections)
+  homebranch: return to item's home branch
+  holdingbranch: return to issuer branch
+
+This searches branchitemrules in the following order:
+  * Same branchcode and itemtype
+  * Same branchcode, itemtype '*'
+  * branchcode '*', same itemtype
+  * branchcode '*' and itemtype '*'
+
+=cut
+
+sub get_return_branch_policy {
+    my ( $self, $item ) = @_;
+
+    my $pref = C4::Context->preference('CircControlReturnsBranch');
+
+    my $branchcode =
+        $pref eq 'ItemHomeLibrary'     ? $item->homebranch
+      : $pref eq 'ItemHoldingLibrary' ? $item->holdingbranch
+      : $pref eq 'CheckInLibrary'      ? C4::Context->userenv
+          ? C4::Context->userenv->{branch}
+          : $item->homebranch
+      : $item->homebranch;
+
+    my $itemtype = $item->effective_itemtype;
+
+    my $rule = Koha::CirculationRules->get_effective_rule(
+        {
+            rule_name  => 'returnbranch',
+            itemtype   => $itemtype,
+            branchcode => $branchcode,
+        }
+    );
+
+    return $rule ? $rule->rule_value : 'homebranch';
+}
+
+
 =head3 get_opacitemholds_policy
 
 my $can_place_a_hold_at_item_level = Koha::CirculationRules->get_opacitemholds_policy( { patron => $patron, item => $item } );
@@ -367,7 +541,7 @@ sub get_opacitemholds_policy {
 
     return unless $item or $patron;
 
-    my $rule = Koha::CirculationRules->get_effective_issuing_rule(
+    my $rule = Koha::CirculationRules->get_effective_rule(
         {
             categorycode => $patron->categorycode,
             itemtype     => $item->effective_itemtype,
@@ -398,7 +572,68 @@ sub get_onshelfholds_policy {
             rule_name    => 'onshelfholds',
         }
     );
-    return $rule ? $rule->rule_value : undef;
+    return $rule ? $rule->rule_value : 0;
+}
+
+=head3 get_lostreturn_policy
+
+  my $lost_proc_refund_policy = Koha::CirculationRules->get_lostreturn_policy( { return_branch => $return_branch, item => $item } );
+
+lostreturn return values are:
+
+=over 2
+
+=item '0' - Do not refund
+
+=item 'refund' - Refund the lost item charge
+
+=item 'restore' - Refund the lost item charge and restore the original overdue fine
+
+=item 'charge' - Refund the lost item charge and charge a new overdue fine
+
+=back
+
+processing return return values are:
+
+=over 2
+
+=item '0' - Do not refund
+
+=item 'refund' - Refund the lost item processing charge
+
+=item 'restore' - Refund the lost item processing charge and restore the original overdue fine
+
+=item 'charge' - Refund the lost item processing charge and charge a new overdue fine
+
+=back
+
+
+=cut
+
+sub get_lostreturn_policy {
+    my ( $class, $params ) = @_;
+
+    my $item   = $params->{item};
+
+    my $behaviour = C4::Context->preference( 'RefundLostOnReturnControl' ) // 'CheckinLibrary';
+    my $behaviour_mapping = {
+        CheckinLibrary    => $params->{'return_branch'} // $item->homebranch,
+        ItemHomeBranch    => $item->homebranch,
+        ItemHoldingBranch => $item->holdingbranch
+    };
+
+    my $branch = $behaviour_mapping->{ $behaviour };
+
+    my $rules = Koha::CirculationRules->get_effective_rules(
+        {
+            branchcode => $branch,
+            rules  => ['lostreturn','processingreturn']
+        }
+    );
+
+    $rules->{lostreturn} //= 'refund';
+    $rules->{processingreturn} //= 'refund';
+    return $rules;
 }
 
 =head3 article_requestable_rules
@@ -416,7 +651,7 @@ sub article_requestable_rules {
 
     return if !C4::Context->preference('ArticleRequests');
     return $class->search({
-        $category ? ( categorycode => [ $category, '*' ] ) : (),
+        $category ? ( categorycode => [ $category, undef ] ) : (),
         rule_name => 'article_requests',
         rule_value => { '!=' => 'no' },
     });
@@ -458,13 +693,43 @@ sub guess_article_requestable_itemtypes {
     });
     return $res if !$rules;
     foreach my $rule ( $rules->as_list ) {
-        $res->{ $rule->itemtype } = 1;
+        $res->{ $rule->itemtype // '*' } = 1;
     }
     $last_article_requestable_guesses->{$key} = $res;
     $cache->set_in_cache(GUESSED_ITEMTYPES_KEY, $last_article_requestable_guesses);
     return $res;
 }
 
+=head3 get_effective_daysmode
+
+Return the value for daysmode defined in the circulation rules.
+If not defined (or empty string), the value of the system preference useDaysMode is returned
+
+=cut
+
+sub get_effective_daysmode {
+    my ( $class, $params ) = @_;
+
+    my $categorycode     = $params->{categorycode};
+    my $itemtype         = $params->{itemtype};
+    my $branchcode       = $params->{branchcode};
+
+    my $daysmode_rule = $class->get_effective_rule(
+        {
+            categorycode => $categorycode,
+            itemtype     => $itemtype,
+            branchcode   => $branchcode,
+            rule_name    => 'daysmode',
+        }
+    );
+
+    return ( defined($daysmode_rule)
+          and $daysmode_rule->rule_value ne '' )
+      ? $daysmode_rule->rule_value
+      : C4::Context->preference('useDaysMode');
+
+}
+
 
 =head3 type