Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / 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      => 'catalogue',
121             letter_code => 'TICKET_ACKNOWLEDGE',
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         # Notify cataloger by email
139         if ( $self->biblio_id && C4::Context->preference('CatalogerEmails') ) {
140
141             # notify the library if a notice exists
142             my $notify_letter = C4::Letters::GetPreparedLetter(
143                 module      => 'catalogue',
144                 letter_code => 'TICKET_NOTIFY',
145                 branchcode  => $self->reporter->branchcode,
146                 tables      => { tickets => $self->id }
147             );
148
149             if ($notify_letter) {
150                 my $message_id = C4::Letters::EnqueueLetter(
151                     {
152                         letter                 => $notify_letter,
153                         message_transport_type => 'email',
154                         to_address             =>
155                           C4::Context->preference('CatalogerEmails'),
156                         reply_address => $self->reporter->notice_email_address,
157                     }
158                 );
159                 C4::Letters::SendQueuedMessages(
160                     { message_id => $message_id } );
161             }
162         }
163     }
164
165     return $self;
166 }
167
168 =head2 Internal methods
169
170 =cut
171
172 =head3 to_api_mapping
173
174 This method returns the mapping for representing a Koha::Ticket object
175 on the API.
176
177 =cut
178
179 sub to_api_mapping {
180     return { id => 'ticket_id', };
181 }
182
183 =head3 _type
184
185 =cut
186
187 sub _type {
188     return 'Ticket';
189 }
190
191 1;