Bug 27266: (follow-up) Remove instances of GetMarcAuthors
[srvgit] / Koha / CirculationRules.pm
index 2f6da88..eb66f5f 100644 (file)
@@ -4,21 +4,21 @@ 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;
@@ -46,7 +46,7 @@ Any attempt to set a rule with a nonsensical scope (for instance, setting the C<
 =cut
 
 our $RULE_KINDS = {
-    refund => {
+    lostreturn => {
         scope => [ 'branchcode' ],
     },
 
@@ -62,17 +62,24 @@ 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' ],
     },
+    open_article_requests_limit => {
+        scope => [ 'branchcode', 'categorycode' ],
+    },
+
     auto_renew => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
@@ -100,12 +107,18 @@ our $RULE_KINDS = {
     hardduedatecompare => {
         scope => [ 'branchcode', 'categorycode', 'itemtype' ],
     },
+    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,12 +155,25 @@ 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' ],
+    },
     # Not included (deprecated?):
     #   * accountsent
     #   * reservecharge
@@ -203,7 +229,7 @@ sub get_effective_rule {
     return $rule;
 }
 
-=head3 get_effective_rule
+=head3 get_effective_rules
 
 =cut
 
@@ -266,6 +292,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 '*';
@@ -348,6 +376,20 @@ 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);
+    }
+}
+
 =head3 get_opacitemholds_policy
 
 my $can_place_a_hold_at_item_level = Koha::CirculationRules->get_opacitemholds_policy( { patron => $patron, item => $item } );
@@ -366,7 +408,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,
@@ -397,7 +439,51 @@ sub get_onshelfholds_policy {
             rule_name    => 'onshelfholds',
         }
     );
-    return $rule ? $rule->rule_value : undef;
+    return $rule ? $rule->rule_value : 0;
+}
+
+=head3 get_lostreturn_policy
+
+  my $lostrefund_policy = Koha::CirculationRules->get_lostreturn_policy( { return_branch => $return_branch, item => $item } );
+
+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
+
+=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 $rule = Koha::CirculationRules->get_effective_rule(
+        {
+            branchcode => $branch,
+            rule_name  => 'lostreturn',
+        }
+    );
+
+    return $rule ? $rule->rule_value : 'refund';
 }
 
 =head3 article_requestable_rules
@@ -415,7 +501,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' },
     });
@@ -457,13 +543,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_daysmode_effective_value
+
+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