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