1 package Koha::Suggestion;
3 # Copyright ByWater Solutions 2015
5 # This file is part of Koha.
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.
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.
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>.
24 use C4::Reserves qw( AddReserve );
27 use Koha::DateUtils qw( dt_from_string );
29 use Koha::AuthorisedValues;
30 use Koha::Exceptions::Suggestion;
31 use C4::Log qw(logaction);
33 use base qw(Koha::Object);
37 Koha::Suggestion - Koha Suggestion object class
47 Override the default store behavior so that new suggestions have
48 a suggesteddate of today
55 $self->STATUS("ASKED") unless $self->STATUS;
56 my @status_constants = qw(ASKED CHECKED ACCEPTED REJECTED ORDERED AVAILABLE);
57 Koha::Exceptions::Suggestion::StatusForbidden->throw( STATUS => $self->STATUS )
58 unless ( grep { $self->STATUS eq $_ } @status_constants )
59 || Koha::AuthorisedValues->search(
61 category => 'SUGGEST_STATUS',
62 authorised_value => $self->STATUS
66 $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq '';
67 unless ( $self->suggesteddate() ) {
68 $self->suggesteddate( dt_from_string()->ymd );
71 my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
73 my $new_suggestion = !$self->in_storage;
75 my $result = $self->SUPER::store();
77 if ( C4::Context->preference("SuggestionsLog") ) {
78 my $action = $new_suggestion ? 'CREATE' : 'MODIFY';
79 logaction( 'SUGGESTION', $action, $result->suggestionid, $self );
82 if ( $emailpurchasesuggestions && $self->STATUS eq 'ASKED' ) {
85 my $letter = C4::Letters::GetPreparedLetter(
86 module => 'suggestions',
87 letter_code => 'NEW_SUGGESTION',
89 'branches' => $result->branchcode,
90 'borrowers' => $result->suggestedby,
91 'suggestions' => $result->unblessed,
97 if ( $emailpurchasesuggestions eq "BranchEmailAddress" ) {
98 my $library = $result->library;
99 $toaddress = $library->inbound_email_address;
101 elsif ( $emailpurchasesuggestions eq "KohaAdminEmailAddress" ) {
102 $toaddress = C4::Context->preference('ReplytoDefault')
103 || C4::Context->preference('KohaAdminEmailAddress');
107 C4::Context->preference($emailpurchasesuggestions)
108 || C4::Context->preference('ReplytoDefault')
109 || C4::Context->preference('KohaAdminEmailAddress');
112 C4::Letters::EnqueueLetter(
115 borrowernumber => $result->suggestedby,
116 suggestionid => $result->id,
117 to_address => $toaddress,
118 message_transport_type => 'email',
120 ) or warn "can't enqueue letter $letter";
129 my $library = $suggestion->library;
131 Returns the library of the suggestion (Koha::Library for branchcode field)
137 my $library_rs = $self->_result->branchcode;
138 return unless $library_rs;
139 return Koha::Library->_new_from_dbic($library_rs);
144 my $patron = $suggestion->suggester
146 Returns the I<Koha::Patron> for the suggestion generator. I<undef> is
147 returned if no suggester is linked.
154 my $suggester_rs = $self->_result->suggester;
155 return unless $suggester_rs;
156 return Koha::Patron->_new_from_dbic($suggester_rs);
161 my $manager = $suggestion->manager;
163 Returns the manager of the suggestion (Koha::Patron for managedby field)
169 my $manager_rs = $self->_result->managedby;
170 return unless $manager_rs;
171 return Koha::Patron->_new_from_dbic($manager_rs);
176 my $rejecter = $suggestion->rejecter;
178 Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
184 my $rejecter_rs = $self->_result->managedby;
185 return unless $rejecter_rs;
186 return Koha::Patron->_new_from_dbic($rejecter_rs);
191 my $last_modifier = $suggestion->last_modifier;
193 Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
199 my $last_modifier_rs = $self->_result->managedby;
200 return unless $last_modifier_rs;
201 return Koha::Patron->_new_from_dbic($last_modifier_rs);
206 my $fund = $suggestion->fund;
208 Return the fund associated to the suggestion
214 my $fund_rs = $self->_result->budgetid;
215 return unless $fund_rs;
216 return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
221 my $hold_id = $suggestion->place_hold();
223 Places a hold for the suggester if the suggestion is tied to a biblio.
230 return unless C4::Context->preference('PlaceHoldsOnOrdersFromSuggestions');
231 return unless $self->biblionumber;
233 my $hold_id = AddReserve(
235 borrowernumber => $self->suggestedby,
236 biblionumber => $self->biblionumber,
237 branchcode => $self->branchcode,
238 title => $self->title,
251 =head3 to_api_mapping
253 This method returns the mapping for representing a Koha::Patron object
260 suggestionid => 'suggestion_id',
261 suggestedby => 'suggested_by',
262 suggesteddate => 'suggestion_date',
263 managedby => 'managed_by',
264 manageddate => 'managed_date',
265 acceptedby => 'accepted_by',
266 accepteddate => 'accepted_date',
267 rejectedby => 'rejected_by',
268 rejecteddate => 'rejected_date',
269 lastmodificationdate => 'last_status_change_date',
270 lastmodificationby => 'last_status_change_by',
273 staff_note => 'staff_note',
276 copyrightdate => 'copyright_date',
277 publishercode => 'publisher_code',
279 volumedesc => 'volume_desc',
280 publicationyear => 'publication_year',
281 place => 'publication_place',
283 biblionumber => 'biblio_id',
285 patronreason => 'patron_reason',
286 budgetid => 'budget_id',
287 branchcode => 'library_id',
288 collectiontitle => 'collection_title',
289 itemtype => 'item_type',
290 quantity => 'quantity',
291 currency => 'currency',
292 price => 'item_price',
293 total => 'total_price',
294 archived => 'archived',
300 Kyle M Hall <kyle@bywatersolutions.com>