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;
32 use base qw(Koha::Object);
36 Koha::Suggestion - Koha Suggestion object class
46 Override the default store behavior so that new suggestions have
47 a suggesteddate of today
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(
60 category => 'SUGGEST_STATUS',
61 authorised_value => $self->STATUS
65 $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq '';
66 unless ( $self->suggesteddate() ) {
67 $self->suggesteddate( dt_from_string()->ymd );
70 my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
72 my $result = $self->SUPER::store();
74 if( $emailpurchasesuggestions ){
77 my $letter = C4::Letters::GetPreparedLetter(
78 module => 'suggestions',
79 letter_code => 'NEW_SUGGESTION',
81 'branches' => $result->branchcode,
82 'borrowers' => $result->suggestedby,
83 'suggestions' => $result->unblessed,
89 if ( $emailpurchasesuggestions eq "BranchEmailAddress" ) {
90 my $library = $result->library;
91 $toaddress = $library->inbound_email_address;
93 elsif ( $emailpurchasesuggestions eq "KohaAdminEmailAddress" ) {
94 $toaddress = C4::Context->preference('ReplytoDefault')
95 || C4::Context->preference('KohaAdminEmailAddress');
99 C4::Context->preference($emailpurchasesuggestions)
100 || C4::Context->preference('ReplytoDefault')
101 || C4::Context->preference('KohaAdminEmailAddress');
104 C4::Letters::EnqueueLetter(
107 borrowernumber => $result->suggestedby,
108 suggestionid => $result->id,
109 to_address => $toaddress,
110 message_transport_type => 'email',
112 ) or warn "can't enqueue letter $letter";
121 my $library = $suggestion->library;
123 Returns the library of the suggestion (Koha::Library for branchcode field)
129 my $library_rs = $self->_result->branchcode;
130 return unless $library_rs;
131 return Koha::Library->_new_from_dbic($library_rs);
136 my $patron = $suggestion->suggester
138 Returns the I<Koha::Patron> for the suggestion generator. I<undef> is
139 returned if no suggester is linked.
146 my $suggester_rs = $self->_result->suggester;
147 return unless $suggester_rs;
148 return Koha::Patron->_new_from_dbic($suggester_rs);
153 my $manager = $suggestion->manager;
155 Returns the manager of the suggestion (Koha::Patron for managedby field)
161 my $manager_rs = $self->_result->managedby;
162 return unless $manager_rs;
163 return Koha::Patron->_new_from_dbic($manager_rs);
168 my $rejecter = $suggestion->rejecter;
170 Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
176 my $rejecter_rs = $self->_result->managedby;
177 return unless $rejecter_rs;
178 return Koha::Patron->_new_from_dbic($rejecter_rs);
183 my $last_modifier = $suggestion->last_modifier;
185 Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
191 my $last_modifier_rs = $self->_result->managedby;
192 return unless $last_modifier_rs;
193 return Koha::Patron->_new_from_dbic($last_modifier_rs);
198 my $fund = $suggestion->fund;
200 Return the fund associated to the suggestion
206 my $fund_rs = $self->_result->budgetid;
207 return unless $fund_rs;
208 return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
213 my $hold_id = $suggestion->place_hold();
215 Places a hold for the suggester if the suggestion is tied to a biblio.
222 return unless C4::Context->preference('PlaceHoldsOnOrdersFromSuggestions');
223 return unless $self->biblionumber;
225 my $hold_id = AddReserve(
227 borrowernumber => $self->suggestedby,
228 biblionumber => $self->biblionumber,
229 branchcode => $self->branchcode,
230 title => $self->title,
243 =head3 to_api_mapping
245 This method returns the mapping for representing a Koha::Patron object
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',
265 staff_note => 'staff_note',
268 copyrightdate => 'copyright_date',
269 publishercode => 'publisher_code',
271 volumedesc => 'volume_desc',
272 publicationyear => 'publication_year',
273 place => 'publication_place',
275 biblionumber => 'biblio_id',
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',
292 Kyle M Hall <kyle@bywatersolutions.com>