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