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