Bug 30719: ILL Batches
[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
73     if( $emailpurchasesuggestions ){
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 type
211
212 =cut
213
214 sub _type {
215     return 'Suggestion';
216 }
217
218 =head3 to_api_mapping
219
220 This method returns the mapping for representing a Koha::Patron object
221 on the API.
222
223 =cut
224
225 sub to_api_mapping {
226     return {
227         suggestionid         => 'suggestion_id',
228         suggestedby          => 'suggested_by',
229         suggesteddate        => 'suggestion_date',
230         managedby            => 'managed_by',
231         manageddate          => 'managed_date',
232         acceptedby           => 'accepted_by',
233         accepteddate         => 'accepted_date',
234         rejectedby           => 'rejected_by',
235         rejecteddate         => 'rejected_date',
236         lastmodificationdate => 'last_status_change_date',
237         lastmodificationby   => 'last_status_change_by',
238         STATUS               => 'status',
239         note                 => 'note',
240         staff_note           => 'staff_note',
241         author               => 'author',
242         title                => 'title',
243         copyrightdate        => 'copyright_date',
244         publishercode        => 'publisher_code',
245         date                 => 'timestamp',
246         volumedesc           => 'volume_desc',
247         publicationyear      => 'publication_year',
248         place                => 'publication_place',
249         isbn                 => 'isbn',
250         biblionumber         => 'biblio_id',
251         reason               => 'reason',
252         patronreason         => 'patron_reason',
253         budgetid             => 'budget_id',
254         branchcode           => 'library_id',
255         collectiontitle      => 'collection_title',
256         itemtype             => 'item_type',
257         quantity             => 'quantity',
258         currency             => 'currency',
259         price                => 'item_price',
260         total                => 'total_price',
261         archived             => 'archived',
262     };
263 }
264
265 =head1 AUTHOR
266
267 Kyle M Hall <kyle@bywatersolutions.com>
268
269 =cut
270
271 1;