Bug 36982: Collections facet does not get alphabetized based on collection descriptions
[koha.git] / Koha / Suggestion.pm
1 package Koha::Suggestion;
2
3 # Copyright ByWater Solutions 2015
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Context;
23 use C4::Letters;
24 use C4::Reserves qw( AddReserve );
25
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Patrons;
29 use Koha::AuthorisedValues;
30 use Koha::Exceptions::Suggestion;
31 use C4::Log qw(logaction);
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Suggestion - Koha Suggestion object class
38
39 =head1 API
40
41 =head2 Class methods
42
43 =cut
44
45 =head3 store
46
47 Override the default store behavior so that new suggestions have
48 a suggesteddate of today
49
50 =cut
51
52 sub store {
53     my ($self) = @_;
54
55     $self->STATUS("ASKED") unless $self->STATUS;
56     my @status_constants = qw(ASKED CHECKED ACCEPTED REJECTED ORDERED AVAILABLE);
57     Koha::Exceptions::Suggestion::StatusForbidden->throw( STATUS => $self->STATUS )
58         unless ( grep { $self->STATUS eq $_ } @status_constants )
59         || Koha::AuthorisedValues->search(
60         {
61             category         => 'SUGGEST_STATUS',
62             authorised_value => $self->STATUS
63         }
64     )->count;
65
66     $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq '';
67     unless ( $self->suggesteddate() ) {
68         $self->suggesteddate( dt_from_string()->ymd );
69     }
70
71     my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
72
73     my $new_suggestion = !$self->in_storage;
74
75     my $result = $self->SUPER::store();
76
77     if ( C4::Context->preference("SuggestionsLog") ) {
78         my $action = $new_suggestion ? 'CREATE' : 'MODIFY';
79         logaction( 'SUGGESTION', $action, $result->suggestionid, $self );
80     }
81
82     if ( $emailpurchasesuggestions && $self->STATUS eq 'ASKED' ) {
83
84         if (
85             my $letter = C4::Letters::GetPreparedLetter(
86                 module      => 'suggestions',
87                 letter_code => 'NEW_SUGGESTION',
88                 tables      => {
89                     'branches'    => $result->branchcode,
90                     'borrowers'   => $result->suggestedby,
91                     'suggestions' => $result->unblessed,
92                 },
93             )
94         ){
95
96             my $toaddress;
97             if ( $emailpurchasesuggestions eq "BranchEmailAddress" ) {
98                 my $library = $result->library;
99                 $toaddress = $library->inbound_email_address;
100             }
101             elsif ( $emailpurchasesuggestions eq "KohaAdminEmailAddress" ) {
102                 $toaddress = C4::Context->preference('ReplytoDefault')
103                   || C4::Context->preference('KohaAdminEmailAddress');
104             }
105             else {
106                 $toaddress =
107                      C4::Context->preference($emailpurchasesuggestions)
108                   || C4::Context->preference('ReplytoDefault')
109                   || C4::Context->preference('KohaAdminEmailAddress');
110             }
111
112             C4::Letters::EnqueueLetter(
113                 {
114                     letter         => $letter,
115                     borrowernumber => $result->suggestedby,
116                     suggestionid   => $result->id,
117                     to_address     => $toaddress,
118                     message_transport_type => 'email',
119                 }
120             ) or warn "can't enqueue letter $letter";
121         }
122     }
123
124     return $result;
125 }
126
127 =head3 library
128
129 my $library = $suggestion->library;
130
131 Returns the library of the suggestion (Koha::Library for branchcode field)
132
133 =cut
134
135 sub library {
136     my ($self) = @_;
137     my $library_rs = $self->_result->branchcode;
138     return unless $library_rs;
139     return Koha::Library->_new_from_dbic($library_rs);
140 }
141
142 =head3 suggester
143
144     my $patron = $suggestion->suggester
145
146 Returns the I<Koha::Patron> for the suggestion generator. I<undef> is
147 returned if no suggester is linked.
148
149 =cut
150
151 sub suggester {
152     my ($self) = @_;
153
154     my $suggester_rs = $self->_result->suggester;
155     return unless $suggester_rs;
156     return Koha::Patron->_new_from_dbic($suggester_rs);
157 }
158
159 =head3 manager
160
161 my $manager = $suggestion->manager;
162
163 Returns the manager of the suggestion (Koha::Patron for managedby field)
164
165 =cut
166
167 sub manager {
168     my ($self) = @_;
169     my $manager_rs = $self->_result->managedby;
170     return unless $manager_rs;
171     return Koha::Patron->_new_from_dbic($manager_rs);
172 }
173
174 =head3 rejecter
175
176 my $rejecter = $suggestion->rejecter;
177
178 Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
179
180 =cut
181
182 sub rejecter {
183     my ($self) = @_;
184     my $rejecter_rs = $self->_result->managedby;
185     return unless $rejecter_rs;
186     return Koha::Patron->_new_from_dbic($rejecter_rs);
187 }
188
189 =head3 last_modifier
190
191 my $last_modifier = $suggestion->last_modifier;
192
193 Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
194
195 =cut
196
197 sub last_modifier {
198     my ($self) = @_;
199     my $last_modifier_rs = $self->_result->managedby;
200     return unless $last_modifier_rs;
201     return Koha::Patron->_new_from_dbic($last_modifier_rs);
202 }
203
204 =head3 fund
205
206 my $fund = $suggestion->fund;
207
208 Return the fund associated to the suggestion
209
210 =cut
211
212 sub fund {
213     my ($self) = @_;
214     my $fund_rs = $self->_result->budgetid;
215     return unless $fund_rs;
216     return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
217 }
218
219 =head3 place_hold
220
221 my $hold_id = $suggestion->place_hold();
222
223 Places a hold for the suggester if the suggestion is tied to a biblio.
224
225 =cut
226
227 sub place_hold {
228     my ($self) = @_;
229
230     return unless C4::Context->preference('PlaceHoldsOnOrdersFromSuggestions');
231     return unless $self->biblionumber;
232
233     my $hold_id = AddReserve(
234         {
235             borrowernumber => $self->suggestedby,
236             biblionumber   => $self->biblionumber,
237             branchcode     => $self->branchcode,
238             title          => $self->title,
239         }
240     );
241 }
242
243 =head3 type
244
245 =cut
246
247 sub _type {
248     return 'Suggestion';
249 }
250
251 =head3 to_api_mapping
252
253 This method returns the mapping for representing a Koha::Patron object
254 on the API.
255
256 =cut
257
258 sub to_api_mapping {
259     return {
260         suggestionid         => 'suggestion_id',
261         suggestedby          => 'suggested_by',
262         suggesteddate        => 'suggestion_date',
263         managedby            => 'managed_by',
264         manageddate          => 'managed_date',
265         acceptedby           => 'accepted_by',
266         accepteddate         => 'accepted_date',
267         rejectedby           => 'rejected_by',
268         rejecteddate         => 'rejected_date',
269         lastmodificationdate => 'last_status_change_date',
270         lastmodificationby   => 'last_status_change_by',
271         STATUS               => 'status',
272         note                 => 'note',
273         staff_note           => 'staff_note',
274         author               => 'author',
275         title                => 'title',
276         copyrightdate        => 'copyright_date',
277         publishercode        => 'publisher_code',
278         date                 => 'timestamp',
279         volumedesc           => 'volume_desc',
280         publicationyear      => 'publication_year',
281         place                => 'publication_place',
282         isbn                 => 'isbn',
283         biblionumber         => 'biblio_id',
284         reason               => 'reason',
285         patronreason         => 'patron_reason',
286         budgetid             => 'budget_id',
287         branchcode           => 'library_id',
288         collectiontitle      => 'collection_title',
289         itemtype             => 'item_type',
290         quantity             => 'quantity',
291         currency             => 'currency',
292         price                => 'item_price',
293         total                => 'total_price',
294         archived             => 'archived',
295     };
296 }
297
298 =head1 AUTHOR
299
300 Kyle M Hall <kyle@bywatersolutions.com>
301
302 =cut
303
304 1;