Bug 35174: Add misc/translator/po to .gitignore
[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
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Patrons;
26 use Koha::AuthorisedValues;
27 use Koha::Exceptions::Suggestion;
28
29 use base qw(Koha::Object);
30
31 =head1 NAME
32
33 Koha::Suggestion - Koha Suggestion object class
34
35 =head1 API
36
37 =head2 Class methods
38
39 =cut
40
41 =head3 store
42
43 Override the default store behavior so that new suggestions have
44 a suggesteddate of today
45
46 =cut
47
48 sub store {
49     my ($self) = @_;
50
51     $self->STATUS("ASKED") unless $self->STATUS;
52     my @status_constants = qw(ASKED CHECKED ACCEPTED REJECTED ORDERED AVAILABLE);
53     Koha::Exceptions::Suggestion::StatusForbidden->throw( STATUS => $self->STATUS )
54         unless ( grep { $self->STATUS eq $_ } @status_constants )
55         || Koha::AuthorisedValues->search(
56         {
57             category         => 'SUGGEST_STATUS',
58             authorised_value => $self->STATUS
59         }
60     )->count;
61
62     $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq '';
63     unless ( $self->suggesteddate() ) {
64         $self->suggesteddate( dt_from_string()->ymd );
65     }
66
67     return $self->SUPER::store();
68 }
69
70 =head3 suggester
71
72     my $patron = $suggestion->suggester
73
74 Returns the I<Koha::Patron> for the suggestion generator. I<undef> is
75 returned if no suggester is linked.
76
77 =cut
78
79 sub suggester {
80     my ($self) = @_;
81
82     my $suggester_rs = $self->_result->suggester;
83     return unless $suggester_rs;
84     return Koha::Patron->_new_from_dbic($suggester_rs);
85 }
86
87 =head3 manager
88
89 my $manager = $suggestion->manager;
90
91 Returns the manager of the suggestion (Koha::Patron for managedby field)
92
93 =cut
94
95 sub manager {
96     my ($self) = @_;
97     my $manager_rs = $self->_result->managedby;
98     return unless $manager_rs;
99     return Koha::Patron->_new_from_dbic($manager_rs);
100 }
101
102 =head3 rejecter
103
104 my $rejecter = $suggestion->rejecter;
105
106 Returns the rejecter of the suggestion (Koha::Patron for rejectebby field)
107
108 =cut
109
110 sub rejecter {
111     my ($self) = @_;
112     my $rejecter_rs = $self->_result->managedby;
113     return unless $rejecter_rs;
114     return Koha::Patron->_new_from_dbic($rejecter_rs);
115 }
116
117 =head3 last_modifier
118
119 my $last_modifier = $suggestion->last_modifier;
120
121 Returns the librarian who last modified the suggestion (Koha::Patron for lastmodificationby field)
122
123 =cut
124
125 sub last_modifier {
126     my ($self) = @_;
127     my $last_modifier_rs = $self->_result->managedby;
128     return unless $last_modifier_rs;
129     return Koha::Patron->_new_from_dbic($last_modifier_rs);
130 }
131
132 =head3 fund
133
134 my $fund = $suggestion->fund;
135
136 Return the fund associated to the suggestion
137
138 =cut
139
140 sub fund {
141     my ($self) = @_;
142     my $fund_rs = $self->_result->budgetid;
143     return unless $fund_rs;
144     return Koha::Acquisition::Fund->_new_from_dbic($fund_rs);
145 }
146
147 =head3 type
148
149 =cut
150
151 sub _type {
152     return 'Suggestion';
153 }
154
155 =head3 to_api_mapping
156
157 This method returns the mapping for representing a Koha::Patron object
158 on the API.
159
160 =cut
161
162 sub to_api_mapping {
163     return {
164         suggestionid         => 'suggestion_id',
165         suggestedby          => 'suggested_by',
166         suggesteddate        => 'suggestion_date',
167         managedby            => 'managed_by',
168         manageddate          => 'managed_date',
169         acceptedby           => 'accepted_by',
170         accepteddate         => 'accepted_date',
171         rejectedby           => 'rejected_by',
172         rejecteddate         => 'rejected_date',
173         lastmodificationdate => 'last_status_change_date',
174         lastmodificationby   => 'last_status_change_by',
175         STATUS               => 'status',
176         note                 => 'note',
177         staff_note           => 'staff_note',
178         author               => 'author',
179         title                => 'title',
180         copyrightdate        => 'copyright_date',
181         publishercode        => 'publisher_code',
182         date                 => 'timestamp',
183         volumedesc           => 'volume_desc',
184         publicationyear      => 'publication_year',
185         place                => 'publication_place',
186         isbn                 => 'isbn',
187         biblionumber         => 'biblio_id',
188         reason               => 'reason',
189         patronreason         => 'patron_reason',
190         budgetid             => 'budget_id',
191         branchcode           => 'library_id',
192         collectiontitle      => 'collection_title',
193         itemtype             => 'item_type',
194         quantity             => 'quantity',
195         currency             => 'currency',
196         price                => 'item_price',
197         total                => 'total_price',
198         archived             => 'archived',
199     };
200 }
201
202 =head1 AUTHOR
203
204 Kyle M Hall <kyle@bywatersolutions.com>
205
206 =cut
207
208 1;