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