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();
72 if ( $emailpurchasesuggestions && $self->STATUS eq 'ASKED' ) {
75 my $letter = C4::Letters::GetPreparedLetter(
76 module => 'suggestions',
77 letter_code => 'NEW_SUGGESTION',
79 'branches' => $result->branchcode,
80 'borrowers' => $result->suggestedby,
81 'suggestions' => $result->unblessed,
87 if ( $emailpurchasesuggestions eq "BranchEmailAddress" ) {
88 my $library = $result->library;
89 $toaddress = $library->inbound_email_address;
91 elsif ( $emailpurchasesuggestions eq "KohaAdminEmailAddress" ) {
92 $toaddress = C4::Context->preference('ReplytoDefault')
93 || C4::Context->preference('KohaAdminEmailAddress');
97 C4::Context->preference($emailpurchasesuggestions)
98 || C4::Context->preference('ReplytoDefault')
99 || C4::Context->preference('KohaAdminEmailAddress');
102 C4::Letters::EnqueueLetter(
105 borrowernumber => $result->suggestedby,
106 suggestionid => $result->id,
107 to_address => $toaddress,
108 message_transport_type => 'email',
110 ) or warn "can't enqueue letter $letter";
119 my $library = $suggestion->library;
121 Returns the library of the suggestion (Koha::Library for branchcode field)
127 my $library_rs = $self->_result->branchcode;
128 return unless $library_rs;
129 return Koha::Library->_new_from_dbic($library_rs);
134 my $patron = $suggestion->suggester
136 Returns the I<Koha::Patron> for the suggestion generator. I<undef> is
137 returned if no suggester is linked.
144 my $suggester_rs = $self->_result->suggester;
145 return unless $suggester_rs;
146 return Koha::Patron->_new_from_dbic($suggester_rs);
151 my $manager = $suggestion->manager;
153 Returns the manager of the suggestion (Koha::Patron for managedby field)
159 my $manager_rs = $self->_result->managedby;
160 return unless $manager_rs;
161 return Koha::Patron->_new_from_dbic($manager_rs);
166 my $rejecter = $suggestion->rejecter;
168 Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
174 my $rejecter_rs = $self->_result->managedby;
175 return unless $rejecter_rs;
176 return Koha::Patron->_new_from_dbic($rejecter_rs);
181 my $last_modifier = $suggestion->last_modifier;
183 Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
189 my $last_modifier_rs = $self->_result->managedby;
190 return unless $last_modifier_rs;
191 return Koha::Patron->_new_from_dbic($last_modifier_rs);
196 my $fund = $suggestion->fund;
198 Return the fund associated to the suggestion
204 my $fund_rs = $self->_result->budgetid;
205 return unless $fund_rs;
206 return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
217 =head3 to_api_mapping
219 This method returns the mapping for representing a Koha::Patron object
226 suggestionid => 'suggestion_id',
227 suggestedby => 'suggested_by',
228 suggesteddate => 'suggestion_date',
229 managedby => 'managed_by',
230 manageddate => 'managed_date',
231 acceptedby => 'accepted_by',
232 accepteddate => 'accepted_date',
233 rejectedby => 'rejected_by',
234 rejecteddate => 'rejected_date',
235 lastmodificationdate => 'last_status_change_date',
236 lastmodificationby => 'last_status_change_by',
239 staff_note => 'staff_note',
242 copyrightdate => 'copyright_date',
243 publishercode => 'publisher_code',
245 volumedesc => 'volume_desc',
246 publicationyear => 'publication_year',
247 place => 'publication_place',
249 biblionumber => 'biblio_id',
251 patronreason => 'patron_reason',
252 budgetid => 'budget_id',
253 branchcode => 'library_id',
254 collectiontitle => 'collection_title',
255 itemtype => 'item_type',
256 quantity => 'quantity',
257 currency => 'currency',
258 price => 'item_price',
259 total => 'total_price',
260 archived => 'archived',
266 Kyle M Hall <kyle@bywatersolutions.com>