From 1053a3c2b7e0d8333032b378fc814c8f3c395de6 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 6 Mar 2017 18:20:03 +0000 Subject: [PATCH] Bug 18887 - Add Koha::CirculationRule(s) --- Koha/CirculationRule.pm | 82 +++++++++++++++ Koha/CirculationRules.pm | 171 +++++++++++++++++++++++++++++++ Koha/Template/Plugin/CirculationRules.pm | 45 ++++++++ 3 files changed, 298 insertions(+) create mode 100644 Koha/CirculationRule.pm create mode 100644 Koha/CirculationRules.pm create mode 100644 Koha/Template/Plugin/CirculationRules.pm diff --git a/Koha/CirculationRule.pm b/Koha/CirculationRule.pm new file mode 100644 index 0000000000..918385c0ca --- /dev/null +++ b/Koha/CirculationRule.pm @@ -0,0 +1,82 @@ +package Koha::CirculationRule; + +# Copyright Vaara-kirjastot 2015 +# +# 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 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. + +use Modern::Perl; + +use base qw(Koha::Object); + +use Koha::Libraries; +use Koha::Patron::Categories; +use Koha::ItemTypes; + +=head1 NAME + +Koha::Hold - Koha Hold object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 library + +=cut + +sub library { + my ($self) = @_; + + $self->{_library} ||= Koha::Libraries->find( $self->branchcode ); + + return $self->{_library}; +} + +=head3 patron_category + +=cut + +sub patron_category { + my ($self) = @_; + + $self->{_patron_category} ||= Koha::Patron::Categories->find( $self->categorycode ); + + return $self->{_patron_category}; +} + +=head3 item_type + +=cut + +sub item_type { + my ($self) = @_; + + $self->{_item_type} ||= Koha::ItemTypes->find( $self->itemtype ); + + return $self->{item_type}; +} + +=head3 _type + +=cut + +sub _type { + return 'CirculationRule'; +} + +1; diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm new file mode 100644 index 0000000000..8004e95196 --- /dev/null +++ b/Koha/CirculationRules.pm @@ -0,0 +1,171 @@ +package Koha::CirculationRules; + +# Copyright Vaara-kirjastot 2015 +# Copyright Koha Development Team 2016 +# +# 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 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. + +use Modern::Perl; + +use Carp qw(croak); + +use Koha::CirculationRule; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::IssuingRules - Koha IssuingRule Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 get_effective_rule + +=cut + +sub get_effective_rule { + my ( $self, $params ) = @_; + + my $rule_name = $params->{rule_name}; + my $categorycode = $params->{categorycode}; + my $itemtype = $params->{itemtype}; + my $branchcode = $params->{branchcode}; + + croak q{No rule name passed in!} unless $rule_name; + + my $search_params; + $search_params->{rule_name} = $rule_name; + + $search_params->{categorycode} = defined $categorycode ? { 'in' => [ $categorycode, '*' ] } : undef; + $search_params->{itemtype} = defined $itemtype ? { 'in' => [ $itemtype, '*' ] } : undef; + $search_params->{branchcode} = defined $branchcode ? { 'in' => [ $branchcode, '*' ] } : undef; + + my $rule = $self->search( + $search_params, + { + order_by => { + -desc => [ 'branchcode', 'categorycode', 'itemtype' ] + }, + rows => 1, + } + )->single; + + return $rule; +} + +=head3 set_rule + +=cut + +sub set_rule { + my ( $self, $params ) = @_; + + croak q{set_rule requires the parameter 'branchcode'!} + unless exists $params->{branchcode}; + croak q{set_rule requires the parameter 'categorycode'!} + unless exists $params->{categorycode}; + croak q{set_rule requires the parameter 'itemtype'!} + unless exists $params->{itemtype}; + croak q{set_rule requires the parameter 'rule_name'!} + unless exists $params->{rule_name}; + croak q{set_rule requires the parameter 'rule_value'!} + unless exists $params->{rule_value}; + + my $branchcode = $params->{branchcode}; + my $categorycode = $params->{categorycode}; + my $itemtype = $params->{itemtype}; + my $rule_name = $params->{rule_name}; + my $rule_value = $params->{rule_value}; + + my $rule = $self->search( + { + rule_name => $rule_name, + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + } + )->next(); + + if ($rule) { + if ( defined $rule_value ) { + $rule->rule_value($rule_value); + $rule->update(); + } + else { + $rule->delete(); + } + } + else { + if ( defined $rule_value ) { + $rule = Koha::CirculationRule->new( + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => $rule_name, + rule_value => $rule_value, + } + ); + $rule->store(); + } + } + + return $rule; +} + +=head3 set_rules + +=cut + +sub set_rules { + my ( $self, $params ) = @_; + warn Data::Dumper::Dumper( $params ); + + my $branchcode = $params->{branchcode}; + my $categorycode = $params->{categorycode}; + my $itemtype = $params->{itemtype}; + my $rules = $params->{rules}; + + foreach my $rule (@$rules) { + Koha::CirculationRules->set_rule( + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => $rule->{rule_name}, + rule_value => $rule->{rule_value}, + } + ); + } +} + +=head3 type + +=cut + +sub _type { + return 'CirculationRule'; +} + +sub object_class { + return 'Koha::CirculationRule'; +} + +1; diff --git a/Koha/Template/Plugin/CirculationRules.pm b/Koha/Template/Plugin/CirculationRules.pm new file mode 100644 index 0000000000..cc1e9127af --- /dev/null +++ b/Koha/Template/Plugin/CirculationRules.pm @@ -0,0 +1,45 @@ +package Koha::Template::Plugin::CirculationRules; + +# Copyright ByWater Solutions 2017 + +# 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 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, see . + +use Modern::Perl; + +use base qw( Template::Plugin ); + +use Koha::CirculationRules; + +sub Get { + my ( $self, $branchcode, $categorycode, $itemtype, $rule_name ) = @_; + + $branchcode = undef if $branchcode eq q{}; + $categorycode = undef if $categorycode eq q{}; + $itemtype = undef if $itemtype eq q{}; + + my $rule = Koha::CirculationRules->search( + { + branchcode => $branchcode, + categorycode => $categorycode, + itemtype => $itemtype, + rule_name => $rule_name, + } + )->next(); + + return $rule->rule_value if $rule; +} + +1; -- 2.15.1