Bug 30642: Make renewal_type an enum in spec and add test
[srvgit] / Koha / CirculationRules.pm
index b781138..f96df49 100644 (file)
@@ -51,7 +51,9 @@ our $RULE_KINDS = {
     lostreturn => {
         scope => [ 'branchcode' ],
     },
-
+    processingreturn => {
+        scope => [ 'branchcode' ],
+    },
     patron_maxissueqty => {
         scope => [ 'branchcode', 'categorycode' ],
     },
@@ -112,6 +114,10 @@ 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' ],
     },
@@ -209,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 {
@@ -252,6 +270,24 @@ sub get_effective_rule {
     return $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 ) = @_;
 
@@ -377,10 +413,9 @@ sub set_rule {
     }
 
     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{};
-
-    Koha::Cache::Memory::Lite->flush();
+    for my $k ( $memory_cache->all_keys ) {
+        $memory_cache->clear_from_cache($k) if $k =~ m{^CircRules:};
+    }
 
     return $rule;
 }
@@ -410,7 +445,6 @@ sub set_rules {
         push( @$rule_objects, $rule_object );
     }
 
-    Koha::Cache::Memory::Lite->flush();
     return $rule_objects;
 }
 
@@ -442,6 +476,53 @@ sub clone {
     }
 }
 
+=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 } );
@@ -496,9 +577,9 @@ sub get_onshelfholds_policy {
 
 =head3 get_lostreturn_policy
 
-  my $lostrefund_policy = Koha::CirculationRules->get_lostreturn_policy( { return_branch => $return_branch, item => $item } );
+  my $lost_proc_refund_policy = Koha::CirculationRules->get_lostreturn_policy( { return_branch => $return_branch, item => $item } );
 
-Return values are:
+lostreturn return values are:
 
 =over 2
 
@@ -512,6 +593,21 @@ Return values are:
 
 =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 {
@@ -528,14 +624,16 @@ sub get_lostreturn_policy {
 
     my $branch = $behaviour_mapping->{ $behaviour };
 
-    my $rule = Koha::CirculationRules->get_effective_rule(
+    my $rules = Koha::CirculationRules->get_effective_rules(
         {
             branchcode => $branch,
-            rule_name  => 'lostreturn',
+            rules  => ['lostreturn','processingreturn']
         }
     );
 
-    return $rule ? $rule->rule_value : 'refund';
+    $rules->{lostreturn} //= 'refund';
+    $rules->{processingreturn} //= 'refund';
+    return $rules;
 }
 
 =head3 article_requestable_rules
@@ -602,7 +700,7 @@ sub guess_article_requestable_itemtypes {
     return $res;
 }
 
-=head3 get_daysmode_effective_value
+=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