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>.
26 use Koha::DateUtils qw( dt_from_string );
28 use Koha::AuthorisedValues;
29 use Koha::Exceptions::Suggestion;
31 use base qw(Koha::Object);
35 Koha::Suggestion - Koha Suggestion object class
45 Override the default store behavior so that new suggestions have
46 a suggesteddate of today
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(
59 category => 'SUGGEST_STATUS',
60 authorised_value => $self->STATUS
64 $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq '';
65 unless ( $self->suggesteddate() ) {
66 $self->suggesteddate( dt_from_string()->ymd );
69 my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
71 my $result = $self->SUPER::store();
73 if( $emailpurchasesuggestions ){
76 my $letter = C4::Letters::GetPreparedLetter(
77 module => 'suggestions',
78 letter_code => 'NEW_SUGGESTION',
80 'branches' => $result->branchcode,
81 'borrowers' => $result->suggestedby,
82 'suggestions' => $result->unblessed,
88 if ( $emailpurchasesuggestions eq "BranchEmailAddress" ) {
89 my $library = $result->library;
90 $toaddress = $library->inbound_email_address;
92 elsif ( $emailpurchasesuggestions eq "KohaAdminEmailAddress" ) {
93 $toaddress = C4::Context->preference('ReplytoDefault')
94 || C4::Context->preference('KohaAdminEmailAddress');
98 C4::Context->preference($emailpurchasesuggestions)
99 || C4::Context->preference('ReplytoDefault')
100 || C4::Context->preference('KohaAdminEmailAddress');
103 C4::Letters::EnqueueLetter(
106 borrowernumber => $result->suggestedby,
107 suggestionid => $result->id,
108 to_address => $toaddress,
109 message_transport_type => 'email',
111 ) or warn "can't enqueue letter $letter";
120 my $library = $suggestion->library;
122 Returns the library of the suggestion (Koha::Library for branchcode field)
128 my $library_rs = $self->_result->branchcode;
129 return unless $library_rs;
130 return Koha::Library->_new_from_dbic($library_rs);
135 my $patron = $suggestion->suggester
137 Returns the I<Koha::Patron> for the suggestion generator. I<undef> is
138 returned if no suggester is linked.
145 my $suggester_rs = $self->_result->suggester;
146 return unless $suggester_rs;
147 return Koha::Patron->_new_from_dbic($suggester_rs);
152 my $manager = $suggestion->manager;
154 Returns the manager of the suggestion (Koha::Patron for managedby field)
160 my $manager_rs = $self->_result->managedby;
161 return unless $manager_rs;
162 return Koha::Patron->_new_from_dbic($manager_rs);
167 my $rejecter = $suggestion->rejecter;
169 Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
175 my $rejecter_rs = $self->_result->managedby;
176 return unless $rejecter_rs;
177 return Koha::Patron->_new_from_dbic($rejecter_rs);
182 my $last_modifier = $suggestion->last_modifier;
184 Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
190 my $last_modifier_rs = $self->_result->managedby;
191 return unless $last_modifier_rs;
192 return Koha::Patron->_new_from_dbic($last_modifier_rs);
197 my $fund = $suggestion->fund;
199 Return the fund associated to the suggestion
205 my $fund_rs = $self->_result->budgetid;
206 return unless $fund_rs;
207 return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
218 =head3 to_api_mapping
220 This method returns the mapping for representing a Koha::Patron object
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',
240 staff_note => 'staff_note',
243 copyrightdate => 'copyright_date',
244 publishercode => 'publisher_code',
246 volumedesc => 'volume_desc',
247 publicationyear => 'publication_year',
248 place => 'publication_place',
250 biblionumber => 'biblio_id',
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',
267 Kyle M Hall <kyle@bywatersolutions.com>