Bug 30719: (QA follow-up) Squash:
[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 use Koha::Database;
22 use Koha::Illrequest::Logger;
23 use Koha::IllbatchStatus;
24 use JSON qw( to_json );
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::Illbatch - Koha Illbatch Object class
30
31 =head2 Class methods
32
33 =head3 status
34
35     my $status = Koha::Illbatch->status;
36
37 Return the status object associated with this batch
38
39 =cut
40
41 sub status {
42     my ($self) = @_;
43     return Koha::IllbatchStatus->_new_from_dbic( scalar $self->_result->statuscode );
44 }
45
46 =head3 patron
47
48     my $patron = Koha::Illbatch->patron;
49
50 Return the patron object associated with this batch
51
52 =cut
53
54 sub patron {
55     my ($self) = @_;
56     return Koha::Patron->_new_from_dbic( scalar $self->_result->borrowernumber );
57 }
58
59 =head3 branch
60
61     my $branch = Koha::Illbatch->branch;
62
63 Return the branch object associated with this batch
64
65 =cut
66
67 sub branch {
68     my ($self) = @_;
69     return Koha::Library->_new_from_dbic( scalar $self->_result->branchcode );
70 }
71
72 =head3 requests_count
73
74     my $requests_count = Koha::Illbatch->requests_count;
75
76 Return the number of requests associated with this batch
77
78 =cut
79
80 sub requests_count {
81     my ($self) = @_;
82     return Koha::Illrequests->search( { batch_id => $self->id } )->count;
83 }
84
85 =head3 create_and_log
86
87     $batch->create_and_log;
88
89 Log batch creation following storage
90
91 =cut
92
93 sub create_and_log {
94     my ($self) = @_;
95
96     $self->store;
97
98     my $logger = Koha::Illrequest::Logger->new;
99
100     $logger->log_something(
101         {
102             modulename   => 'ILL',
103             actionname   => 'batch_create',
104             objectnumber => $self->id,
105             infos        => to_json( {} )
106         }
107     );
108 }
109
110 =head3 update_and_log
111
112     $batch->update_and_log;
113
114 Log batch update following storage
115
116 =cut
117
118 sub update_and_log {
119     my ( $self, $params ) = @_;
120
121     my $before = {
122         name       => $self->name,
123         branchcode => $self->branchcode
124     };
125
126     $self->set($params);
127     my $update = $self->store;
128
129     my $after = {
130         name       => $self->name,
131         branchcode => $self->branchcode
132     };
133
134     my $logger = Koha::Illrequest::Logger->new;
135
136     $logger->log_something(
137         {
138             modulename   => 'ILL',
139             actionname   => 'batch_update',
140             objectnumber => $self->id,
141             infos        => to_json(
142                 {
143                     before => $before,
144                     after  => $after
145                 }
146             )
147         }
148     );
149 }
150
151 =head3 delete_and_log
152
153     $batch->delete_and_log;
154
155 Log batch delete
156
157 =cut
158
159 sub delete_and_log {
160     my ($self) = @_;
161
162     my $logger = Koha::Illrequest::Logger->new;
163
164     $logger->log_something(
165         {
166             modulename   => 'ILL',
167             actionname   => 'batch_delete',
168             objectnumber => $self->id,
169             infos        => to_json( {} )
170         }
171     );
172
173     $self->delete;
174 }
175
176 =head2 Internal methods
177
178 =head3 _type
179
180     my $type = Koha::Illbatch->_type;
181
182 Return this object's type
183
184 =cut
185
186 sub _type {
187     return 'Illbatch';
188 }
189
190 =head1 AUTHOR
191
192 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
193
194 =cut
195
196 1;