Bug 29948: OPACAuthorIdentifiersAndInformation
[koha.git] / Koha / Ticket.pm
1 package Koha::Ticket;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use base qw(Koha::Object);
21
22 use C4::Letters;
23
24 use Koha::Ticket::Update;
25 use Koha::Ticket::Updates;
26
27 =head1 NAME
28
29 Koha::Ticket - Koha Ticket Object class
30
31 =head1 API
32
33 =head2 Relations
34
35 =cut
36
37 =head3 reporter
38
39 Return the patron who submitted this ticket
40
41 =cut
42
43 sub reporter {
44     my ($self) = @_;
45     my $rs = $self->_result->reporter;
46     return unless $rs;
47     return Koha::Patron->_new_from_dbic($rs);
48 }
49
50 =head3 assignee
51
52 Return the patron who submitted this ticket
53
54 =cut
55
56 sub assignee {
57     my ($self) = @_;
58     my $rs = $self->_result->assignee;
59     return unless $rs;
60     return Koha::Patron->_new_from_dbic($rs);
61 }
62
63 =head3 resolver
64
65 Return the user who resolved this ticket
66
67 =cut
68
69 sub resolver {
70     my ($self) = @_;
71     my $rs = $self->_result->resolver;
72     return unless $rs;
73     return Koha::Patron->_new_from_dbic($rs) if $rs;
74 }
75
76 =head3 biblio
77
78 Return the biblio linked to this ticket
79
80 =cut
81
82 sub biblio {
83     my ($self) = @_;
84     my $rs = $self->_result->biblio;
85     return unless $rs;
86     return Koha::Biblio->_new_from_dbic($rs);
87 }
88
89 =head3 updates
90
91 Return any updates attached to this ticket
92
93 =cut
94
95 sub updates {
96     my ($self) = @_;
97     my $rs = $self->_result->ticket_updates;
98     return unless $rs;
99     return Koha::Ticket::Updates->_new_from_dbic($rs) if $rs;
100 }
101
102 =head2 Actions
103
104 =head3 add_update
105
106 =cut
107
108 sub add_update {
109     my ( $self, $params ) = @_;
110
111     my $rs = $self->_result->add_to_ticket_updates($params)->discard_changes;
112     return Koha::Ticket::Update->_new_from_dbic($rs);
113 }
114
115 =head2 Core methods
116
117 =head3 store
118
119 Overloaded I<store> method to trigger notices as required
120
121 =cut
122
123 sub store {
124     my ($self) = @_;
125
126     my $is_new = !$self->in_storage;
127     $self = $self->SUPER::store;
128
129     if ($is_new) {
130
131         # Send patron acknowledgement
132         my $acknowledgement_letter = C4::Letters::GetPreparedLetter(
133             module      => 'catalogue',
134             letter_code => 'TICKET_ACKNOWLEDGE',
135             branchcode  => $self->reporter->branchcode,
136             tables      => { tickets => $self->id }
137         );
138
139         if ($acknowledgement_letter) {
140             my $acknowledgement_message_id = C4::Letters::EnqueueLetter(
141                 {
142                     letter                 => $acknowledgement_letter,
143                     message_transport_type => 'email',
144                     borrowernumber         => $self->reporter_id,
145                 }
146             );
147             C4::Letters::SendQueuedMessages( { message_id => $acknowledgement_message_id } )
148                 if $acknowledgement_message_id;
149         }
150
151         # Notify cataloger by email
152         if ( $self->biblio_id && C4::Context->preference('CatalogerEmails') ) {
153
154             # notify the library if a notice exists
155             my $notify_letter = C4::Letters::GetPreparedLetter(
156                 module      => 'catalogue',
157                 letter_code => 'TICKET_NOTIFY',
158                 branchcode  => $self->reporter->branchcode,
159                 tables      => { tickets => $self->id }
160             );
161
162             if ($notify_letter) {
163                 my $message_id = C4::Letters::EnqueueLetter(
164                     {
165                         letter                 => $notify_letter,
166                         message_transport_type => 'email',
167                         to_address             =>
168                           C4::Context->preference('CatalogerEmails'),
169                         reply_address => $self->reporter->notice_email_address,
170                     }
171                 );
172                 C4::Letters::SendQueuedMessages( { message_id => $message_id } ) if $message_id;
173             }
174         }
175     }
176
177     return $self;
178 }
179
180 =head2 Internal methods
181
182 =cut
183
184 =head3 public_read_list
185
186 This method returns the list of publicly readable database fields for both API and UI output purposes
187
188 =cut
189
190 sub public_read_list {
191     return [
192         'ticket_id',   'title',         'body',
193         'reporter_id', 'reported_date', 'resolved_date',
194         'biblio_id',   'source'
195     ];
196 }
197
198 =head3 to_api_mapping
199
200 This method returns the mapping for representing a Koha::Ticket object
201 on the API.
202
203 =cut
204
205 sub to_api_mapping {
206     return { id => 'ticket_id', };
207 }
208
209 =head3 strings_map
210
211 =cut
212
213 sub strings_map {
214     my ( $self, $params ) = @_;
215
216     my $strings = {};
217
218     if ( defined $self->status ) {
219         my $av = Koha::AuthorisedValues->search(
220             {
221                 category         => 'TICKET_STATUS',
222                 authorised_value => $self->status,
223             }
224         );
225
226         # Fall back to TICKET_RESOLUTION as needed
227         if ( !$av->count ) {
228             $av = Koha::AuthorisedValues->search(
229                 {
230                     category         => 'TICKET_RESOLUTION',
231                     authorised_value => $self->status,
232                 }
233             );
234         }
235
236         my $status_str =
237               $av->count
238             ? $params->{public}
239                 ? $av->next->opac_description
240                 : $av->next->lib
241             : $self->status;
242
243         $strings->{status} = {
244             category => 'TICKET_STATUS',
245             str      => $status_str,
246             type     => 'av',
247         };
248     }
249
250     return $strings;
251 }
252
253 =head3 _type
254
255 =cut
256
257 sub _type {
258     return 'Ticket';
259 }
260
261 1;