+=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 } );
+
+Return 'Y' or 'F' if the patron can place a hold on this item according to the issuing rules
+and the "Item level holds" (opacitemholds).
+Can be 'N' - Don't allow, 'Y' - Allow, and 'F' - Force
+
+=cut
+
+sub get_opacitemholds_policy {
+ my ( $class, $params ) = @_;
+
+ my $item = $params->{item};
+ my $patron = $params->{patron};
+
+ return unless $item or $patron;
+
+ my $rule = Koha::CirculationRules->get_effective_rule(
+ {
+ categorycode => $patron->categorycode,
+ itemtype => $item->effective_itemtype,
+ branchcode => $item->homebranch,
+ rule_name => 'opacitemholds',
+ }
+ );
+
+ return $rule ? $rule->rule_value : undef;
+}
+
+=head3 get_onshelfholds_policy
+
+ my $on_shelf_holds = Koha::CirculationRules->get_onshelfholds_policy({ item => $item, patron => $patron });
+
+=cut
+
+sub get_onshelfholds_policy {
+ my ( $class, $params ) = @_;
+ my $item = $params->{item};
+ my $itemtype = $item->effective_itemtype;
+ my $patron = $params->{patron};
+ my $rule = Koha::CirculationRules->get_effective_rule(
+ {
+ categorycode => ( $patron ? $patron->categorycode : undef ),
+ itemtype => $itemtype,
+ branchcode => $item->holdingbranch,
+ rule_name => 'onshelfholds',
+ }
+ );
+ 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
+
+ Return rules that allow article requests, optionally filtered by
+ patron categorycode.
+
+ Use with care; see guess_article_requestable_itemtypes.
+
+=cut
+
+sub article_requestable_rules {
+ my ( $class, $params ) = @_;
+ my $category = $params->{categorycode};
+
+ return if !C4::Context->preference('ArticleRequests');
+ return $class->search({
+ $category ? ( categorycode => [ $category, undef ] ) : (),
+ rule_name => 'article_requests',
+ rule_value => { '!=' => 'no' },
+ });
+}
+
+=head3 guess_article_requestable_itemtypes
+
+ Return item types in a hashref that are likely possible to be
+ 'article requested'. Constructed by an intelligent guess in the
+ issuing rules (see article_requestable_rules).
+
+ Note: pref ArticleRequestsLinkControl overrides the algorithm.
+
+ Optional parameters: categorycode.
+
+ Note: the routine is used in opac-search to obtain a reasonable
+ estimate within performance borders (not looking at all items but
+ just using default itemtype). Also we are not looking at the
+ branchcode here, since home or holding branch of the item is
+ leading and branch may be unknown too (anonymous opac session).
+
+=cut
+
+sub guess_article_requestable_itemtypes {
+ my ( $class, $params ) = @_;
+ my $category = $params->{categorycode};
+ return {} if !C4::Context->preference('ArticleRequests');
+ return { '*' => 1 } if C4::Context->preference('ArticleRequestsLinkControl') eq 'always';
+
+ my $cache = Koha::Caches->get_instance;
+ my $last_article_requestable_guesses = $cache->get_from_cache(GUESSED_ITEMTYPES_KEY);
+ my $key = $category || '*';
+ return $last_article_requestable_guesses->{$key}
+ if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
+
+ my $res = {};
+ my $rules = $class->article_requestable_rules({
+ $category ? ( categorycode => $category ) : (),
+ });
+ return $res if !$rules;
+ foreach my $rule ( $rules->as_list ) {
+ $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');
+
+}
+
+