Bug 33749: (QA follow-up) Tidy code for qa script
[koha.git] / Koha / Illbatch.pm
1 package Koha::Illbatch;
2
3 # Copyright PTFS Europe 2022
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 use Koha::Database;
23
24 use Koha::Illrequests;
25 use Koha::Illrequest::Logger;
26 use Koha::IllbatchStatuses;
27 use Koha::Libraries;
28 use Koha::Patrons;
29
30 use JSON qw( to_json );
31 use base qw(Koha::Object);
32
33 =head1 NAME
34
35 Koha::Illbatch - Koha Illbatch Object class
36
37 =head2 Class methods
38
39 =head3 status
40
41     my $status = Koha::Illbatch->status;
42
43 Return the status object associated with this batch
44
45 =cut
46
47 sub status {
48     my ($self) = @_;
49     return Koha::IllbatchStatus->_new_from_dbic( scalar $self->_result->status_code );
50 }
51
52 =head3 patron
53
54     my $patron = Koha::Illbatch->patron;
55
56 Return the I<Koha::Patron> object associated with this batch
57
58 =cut
59
60 sub patron {
61     my ($self) = @_;
62     my $patron = $self->_result->patron;
63     return unless $patron;
64     return Koha::Patron->_new_from_dbic($patron);
65 }
66
67 =head3 library
68
69     my $library = Koha::Illbatch->library;
70
71 Return the I<Koha::Library> object associated with this batch
72
73 =cut
74
75 sub library {
76     my ($self) = @_;
77     my $library = $self->_result->library;
78     return unless $library;
79     return Koha::Library->_new_from_dbic($library);
80 }
81
82 =head3 requests
83
84 Return the I<Koha::Illrequests> for this batch
85
86 =cut
87
88 sub requests {
89     my ($self) = @_;
90     my $requests = $self->_result->requests;
91     return Koha::Illrequests->_new_from_dbic($requests);
92 }
93
94 =head3 create_and_log
95
96     $batch->create_and_log;
97
98 Log batch creation following storage
99
100 =cut
101
102 sub create_and_log {
103     my ($self) = @_;
104
105     $self->store;
106
107     my $logger = Koha::Illrequest::Logger->new;
108
109     $logger->log_something(
110         {
111             modulename   => 'ILL',
112             actionname   => 'batch_create',
113             objectnumber => $self->id,
114             infos        => to_json( {} )
115         }
116     );
117 }
118
119 =head3 update_and_log
120
121     $batch->update_and_log;
122
123 Log batch update following storage
124
125 =cut
126
127 sub update_and_log {
128     my ( $self, $params ) = @_;
129
130     my $before = {
131         name       => $self->name,
132         library_id => $self->library_id,
133     };
134
135     $self->set($params);
136     my $update = $self->store;
137
138     my $after = {
139         name       => $self->name,
140         library_id => $self->library_id,
141     };
142
143     my $logger = Koha::Illrequest::Logger->new;
144
145     $logger->log_something(
146         {
147             modulename   => 'ILL',
148             actionname   => 'batch_update',
149             objectnumber => $self->id,
150             infos        => to_json(
151                 {
152                     before => $before,
153                     after  => $after
154                 }
155             )
156         }
157     );
158 }
159
160 =head3 delete_and_log
161
162     $batch->delete_and_log;
163
164 Log batch delete
165
166 =cut
167
168 sub delete_and_log {
169     my ($self) = @_;
170
171     my $logger = Koha::Illrequest::Logger->new;
172
173     $logger->log_something(
174         {
175             modulename   => 'ILL',
176             actionname   => 'batch_delete',
177             objectnumber => $self->id,
178             infos        => to_json( {} )
179         }
180     );
181
182     $self->delete;
183 }
184
185 =head2 Internal methods
186
187
188 =head3 strings_map
189
190 Returns a map of column name to string representations including the string,
191 the mapping type and the mapping category where appropriate.
192
193 Currently handles library and ILL batch status expansions.
194 expansions.
195
196 Accepts a param hashref where the I<public> key denotes whether we want the public
197 or staff client strings.
198
199 Note: the I<public> parameter is not currently used.
200
201 =cut
202
203 sub strings_map {
204     my ( $self, $params ) = @_;
205
206     my $strings = {};
207
208     if ( defined $self->status_code ) {
209         my $status = $self->status;
210
211         if ($status) {
212             $strings->{status_code} = {
213                 str  => $status->name,
214                 type => 'ill_batch_status',
215             };
216         }
217     }
218
219     if ( defined $self->library_id ) {
220         my $library = $self->library;
221
222         if ($library) {
223             $strings->{library_id} = {
224                 str  => $library->branchname,
225                 type => 'library',
226             };
227         }
228     }
229
230     return $strings;
231 }
232
233 =head3 _type
234
235     my $type = Koha::Illbatch->_type;
236
237 Return this object's type
238
239 =cut
240
241 sub _type {
242     return 'Illbatch';
243 }
244
245 =head1 AUTHOR
246
247 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
248
249 =cut
250
251 1;