Bug 31028: Add acknowledgement notice
[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 resolver
51
52 Return the user who resolved this ticket
53
54 =cut
55
56 sub resolver {
57     my ($self) = @_;
58     my $rs = $self->_result->resolver;
59     return unless $rs;
60     return Koha::Patron->_new_from_dbic($rs) if $rs;
61 }
62
63 =head3 biblio
64
65 Return the biblio linked to this ticket
66
67 =cut
68
69 sub biblio {
70     my ($self) = @_;
71     my $rs = $self->_result->biblio;
72     return unless $rs;
73     return Koha::Biblio->_new_from_dbic($rs);
74 }
75
76 =head3 updates
77
78 Return any updates attached to this ticket
79
80 =cut
81
82 sub updates {
83     my ($self) = @_;
84     my $rs = $self->_result->ticket_updates;
85     return unless $rs;
86     return Koha::Ticket::Updates->_new_from_dbic($rs) if $rs;
87 }
88
89 =head2 Actions
90
91 =head3 add_update
92
93 =cut
94
95 sub add_update {
96     my ( $self, $params ) = @_;
97
98     my $rs = $self->_result->add_to_ticket_updates($params)->discard_changes;
99     return Koha::Ticket::Update->_new_from_dbic($rs);
100 }
101
102 =head2 Core methods
103
104 =head3 store
105
106 Overloaded I<store> method to trigger notices as required
107
108 =cut
109
110 sub store {
111     my ($self) = @_;
112
113     my $is_new = !$self->in_storage;
114     $self = $self->SUPER::store;
115
116     if ($is_new) {
117
118         # Send patron acknowledgement
119         my $acknowledgement_letter = C4::Letters::GetPreparedLetter(
120             module      => 'catalog',
121             letter_code => 'TICKET_ACKNOWLEDGEMENT',
122             branchcode  => $self->reporter->branchcode,
123             tables      => { tickets => $self->id }
124         );
125
126         if ($acknowledgement_letter) {
127             my $acknowledgement_message_id = C4::Letters::EnqueueLetter(
128                 {
129                     letter                 => $acknowledgement_letter,
130                     message_transport_type => 'email',
131                     borrowernumber         => $self->reporter_id,
132                 }
133             );
134             C4::Letters::SendQueuedMessages(
135                 { message_id => $acknowledgement_message_id } );
136         }
137     }
138
139     return $self;
140 }
141
142 =head2 Internal methods
143
144 =cut
145
146 =head3 to_api_mapping
147
148 This method returns the mapping for representing a Koha::Ticket object
149 on the API.
150
151 =cut
152
153 sub to_api_mapping {
154     return { id => 'ticket_id', };
155 }
156
157 =head3 _type
158
159 =cut
160
161 sub _type {
162     return 'Ticket';
163 }
164
165 1;