Update release notes for 23.05.12 release
[koha.git] / Koha / ItemType.pm
1 package Koha::ItemType;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use C4::Koha qw( getitemtypeimagelocation );
21 use C4::Languages;
22 use Koha::Caches;
23 use Koha::Database;
24 use Koha::CirculationRules;
25 use Koha::Localizations;
26
27 use base qw(Koha::Object Koha::Object::Limit::Library);
28
29 my $cache = Koha::Caches->get_instance();
30
31 =head1 NAME
32
33 Koha::ItemType - Koha Item type Object class
34
35 =head1 API
36
37 =head2 Class methods
38
39 =cut
40
41 =head3 store
42
43 ItemType specific store to ensure relevant caches are flushed on change
44
45 =cut
46
47 sub store {
48     my ($self) = @_;
49
50     my $flush = 0;
51
52     if ( !$self->in_storage ) {
53         $flush = 1;
54     } else {
55         my $self_from_storage = $self->get_from_storage;
56         $flush = 1 if ( $self_from_storage->description ne $self->description );
57     }
58
59     $self = $self->SUPER::store;
60
61     if ($flush) {
62         my $key = "itemtype:description:en";
63         $cache->clear_from_cache($key);
64     }
65
66     return $self;
67 }
68
69 =head2 delete
70
71 ItemType specific C<delete> to clear relevant caches on delete.
72
73 =cut
74
75 sub delete {
76     my $self = shift @_;
77     $cache->clear_from_cache('itemtype:description:en');
78     $self->SUPER::delete(@_);
79 }
80
81 =head3 image_location
82
83 =cut
84
85 sub image_location {
86     my ( $self, $interface ) = @_;
87     return C4::Koha::getitemtypeimagelocation( $interface, $self->SUPER::imageurl );
88 }
89
90 =head3 translated_description
91
92 =cut
93
94 sub translated_description {
95     my ( $self, $lang ) = @_;
96     if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
97
98         # If the value has already been fetched (eg. from sarch_with_localization),
99         # do not search for it again
100         # Note: This is a bit hacky but should be fast
101         return $translated_description
102             ? $translated_description
103             : $self->description;
104     }
105     $lang ||= C4::Languages::getlanguage;
106     my $translated_description = Koha::Localizations->search(
107         {
108             code   => $self->itemtype,
109             entity => 'itemtypes',
110             lang   => $lang
111         }
112     )->next;
113     return $translated_description
114         ? $translated_description->translation
115         : $self->description;
116 }
117
118 =head3 translated_descriptions
119
120 =cut
121
122 sub translated_descriptions {
123     my ($self) = @_;
124     my @translated_descriptions = Koha::Localizations->search(
125         {
126             entity => 'itemtypes',
127             code   => $self->itemtype,
128         }
129     )->as_list;
130     return [
131         map {
132             {
133                 lang        => $_->lang,
134                 translation => $_->translation,
135             }
136         } @translated_descriptions
137     ];
138 }
139
140 =head3 can_be_deleted
141
142 my $can_be_deleted = Koha::ItemType->can_be_deleted();
143
144 Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
145
146 =cut
147
148 sub can_be_deleted {
149     my ($self)         = @_;
150     my $nb_items       = Koha::Items->search( { itype => $self->itemtype } )->count;
151     my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
152     return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
153 }
154
155 =head3 may_article_request
156
157     Returns true if it is likely possible to make an article request for
158     this item type.
159     Optional parameter: categorycode (for patron).
160
161 =cut
162
163 sub may_article_request {
164     my ( $self, $params ) = @_;
165     return q{} if !C4::Context->preference('ArticleRequests');
166     my $itemtype = $self->itemtype;
167     my $category = $params->{categorycode};
168
169     my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes(
170         {
171             $category ? ( categorycode => $category ) : (),
172         }
173     );
174     return ( $guess->{ $itemtype // q{} } || $guess->{'*'} ) ? 1 : q{};
175 }
176
177 =head3 _library_limits
178
179  configure library limits
180
181 =cut
182
183 sub _library_limits {
184     return {
185         class   => "ItemtypesBranch",
186         id      => "itemtype",
187         library => "branchcode",
188     };
189 }
190
191 =head3 parent
192
193     Returns the ItemType object of the parent_type or undef.
194
195 =cut
196
197 sub parent {
198     my ($self) = @_;
199     my $parent_rs = $self->_result->parent_type;
200     return unless $parent_rs;
201     return Koha::ItemType->_new_from_dbic($parent_rs);
202
203 }
204
205 =head3 children_with_localization
206
207     Returns the ItemType objects of the children of this type or undef.
208
209 =cut
210
211 sub children_with_localization {
212     my ($self) = @_;
213     return Koha::ItemTypes->search_with_localization( { parent_type => $self->itemtype } );
214 }
215
216 =head3 to_api_mapping
217
218 This method returns the mapping for representing a Koha::ItemType object
219 on the API.
220
221 =cut
222
223 sub to_api_mapping {
224     return {
225         checkinmsg                   => 'checkin_message',
226         checkinmsgtype               => 'checkin_message_type',
227         defaultreplacecost           => 'default_replacement_cost',
228         hideinopac                   => 'hide_in_opac',
229         imageurl                     => 'image_url',
230         itemtype                     => 'item_type_id',
231         notforloan                   => 'not_for_loan_status',
232         processfee                   => 'process_fee',
233         rentalcharge_daily           => 'daily_rental_charge',
234         rentalcharge_daily_calendar  => 'daily_rental_charge_calendar',
235         rentalcharge_hourly          => 'hourly_rental_charge',
236         rentalcharge_hourly_calendar => 'hourly_rental_charge_calendar',
237     };
238 }
239
240 =head2 Internal methods
241
242 =head3 _type
243
244 =cut
245
246 sub _type {
247     return 'Itemtype';
248 }
249
250 1;