Bug 25683: Patron with no accountlines should have 0 outstanding (not NULL)
[koha.git] / Koha / AuthorisedValues.pm
bloba4916d571e2291dc854615880b40f723d5c0943c
1 package Koha::AuthorisedValues;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
26 use Koha::AuthorisedValue;
27 use Koha::MarcSubfieldStructures;
28 use Koha::Cache::Memory::Lite;
30 use base qw(Koha::Objects);
32 =head1 NAME
34 Koha::AuthorisedValues - Koha Authorised value Object set class
36 =head1 API
38 =head2 Class Methods
40 =cut
42 =head3 Koha::AuthorisedValues->search();
44 my @objects = Koha::AuthorisedValues->search($params);
46 =cut
48 sub search {
49 my ( $self, $params, $attributes ) = @_;
51 my $branchcode = $params->{branchcode};
52 delete( $params->{branchcode} );
54 my $or =
55 $branchcode
56 ? {
57 '-or' => [
58 'authorised_values_branches.branchcode' => undef,
59 'authorised_values_branches.branchcode' => $branchcode,
62 : {};
63 my $join = $branchcode ? { join => 'authorised_values_branches' } : {};
64 $attributes //= {};
65 $attributes = { %$attributes, %$join };
66 return $self->SUPER::search( { %$params, %$or, }, $attributes );
69 sub search_by_marc_field {
70 my ( $self, $params ) = @_;
71 my $frameworkcode = $params->{frameworkcode} || '';
72 my $tagfield = $params->{tagfield};
73 my $tagsubfield = $params->{tagsubfield};
75 return unless $tagfield or $tagsubfield;
77 return $self->SUPER::search(
78 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
79 ( defined $tagfield ? ( 'marc_subfield_structures.tagfield' => $tagfield ) : () ),
80 ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ),
82 { join => { category => 'marc_subfield_structures' } }
86 sub search_by_koha_field {
87 my ( $self, $params ) = @_;
88 my $frameworkcode = $params->{frameworkcode} || '';
89 my $kohafield = $params->{kohafield};
90 my $category = $params->{category};
92 return unless $kohafield;
94 return $self->SUPER::search(
95 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
96 'marc_subfield_structures.kohafield' => $kohafield,
97 ( defined $category ? ( category_name => $category ) : () ),
99 { join => { category => 'marc_subfield_structures' },
100 distinct => 1,
105 sub find_by_koha_field {
106 my ( $self, $params ) = @_;
107 my $frameworkcode = $params->{frameworkcode} || '';
108 my $kohafield = $params->{kohafield};
109 my $authorised_value = $params->{authorised_value};
111 my $av = $self->SUPER::search(
112 { 'marc_subfield_structures.frameworkcode' => $frameworkcode,
113 'marc_subfield_structures.kohafield' => $kohafield,
114 'me.authorised_value' => $authorised_value,
116 { join => { category => 'marc_subfield_structures' },
117 distinct => 1,
120 return $av->count ? $av->next : undef;
123 sub get_description_by_koha_field {
124 my ( $self, $params ) = @_;
125 my $frameworkcode = $params->{frameworkcode} || '';
126 my $kohafield = $params->{kohafield};
127 my $authorised_value = $params->{authorised_value};
129 return {} unless defined $authorised_value;
131 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
132 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield:$authorised_value";
133 my $cached = $memory_cache->get_from_cache($cache_key);
134 return $cached if $cached;
136 my $av = $self->find_by_koha_field($params);
137 return {} unless defined $av;
138 my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
139 $memory_cache->set_in_cache( $cache_key, $descriptions );
140 return $descriptions;
143 sub get_descriptions_by_koha_field {
144 my ( $self, $params ) = @_;
145 my $frameworkcode = $params->{frameworkcode} || '';
146 my $kohafield = $params->{kohafield};
148 my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
149 my $cache_key = "AV_descriptions:$frameworkcode:$kohafield";
150 my $cached = $memory_cache->get_from_cache($cache_key);
151 return @$cached if $cached;
153 my @avs = $self->search_by_koha_field($params);
154 my @descriptions = map {
156 authorised_value => $_->authorised_value,
157 lib => $_->lib,
158 opac_description => $_->opac_description
160 } @avs;
161 $memory_cache->set_in_cache( $cache_key, \@descriptions );
162 return @descriptions;
165 sub categories {
166 my ( $self ) = @_;
167 my $rs = $self->_resultset->search(
168 undef,
170 select => ['category'],
171 distinct => 1,
172 order_by => 'category',
175 return map $_->get_column('category'), $rs->all;
178 =head3 type
180 =cut
182 sub _type {
183 return 'AuthorisedValue';
186 sub object_class {
187 return 'Koha::AuthorisedValue';
190 =head1 AUTHOR
192 Kyle M Hall <kyle@bywatersolutions.com>
194 =cut