Bug 30719: ILL Batches
[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(
44         scalar $self->_result->statuscode
45     );
46 }
47
48 =head3 patron
49
50     my $patron = Koha::Illbatch->patron;
51
52 Return the patron object associated with this batch
53
54 =cut
55
56 sub patron {
57     my ( $self ) = @_;
58     return Koha::Patron->_new_from_dbic(
59         scalar $self->_result->borrowernumber
60     );
61 }
62
63 =head3 branch
64
65     my $branch = Koha::Illbatch->branch;
66
67 Return the branch object associated with this batch
68
69 =cut
70
71 sub branch {
72     my ( $self ) = @_;
73     return Koha::Library->_new_from_dbic(
74         scalar $self->_result->branchcode
75     );
76 }
77
78 =head3 requests_count
79
80     my $requests_count = Koha::Illbatch->requests_count;
81
82 Return the number of requests associated with this batch
83
84 =cut
85
86 sub requests_count {
87     my ( $self ) = @_;
88     return Koha::Illrequests->search({
89         batch_id => $self->id
90     })->count;
91 }
92
93 =head3 create_and_log
94
95     $batch->create_and_log;
96
97 Log batch creation following storage
98
99 =cut
100
101 sub create_and_log {
102     my ( $self ) = @_;
103
104     $self->store;
105
106     my $logger = Koha::Illrequest::Logger->new;
107
108     $logger->log_something({
109         modulename   => 'ILL',
110         actionname  => 'batch_create',
111         objectnumber => $self->id,
112         infos        => to_json({})
113     });
114 }
115
116 =head3 update_and_log
117
118     $batch->update_and_log;
119
120 Log batch update following storage
121
122 =cut
123
124 sub update_and_log {
125     my ( $self, $params ) = @_;
126
127     my $before = {
128         name       => $self->name,
129         branchcode => $self->branchcode
130     };
131
132     $self->set( $params );
133     my $update = $self->store;
134
135     my $after = {
136         name       => $self->name,
137         branchcode => $self->branchcode
138     };
139
140     my $logger = Koha::Illrequest::Logger->new;
141
142     $logger->log_something({
143         modulename   => 'ILL',
144         actionname  => 'batch_update',
145         objectnumber => $self->id,
146         infos        => to_json({
147             before => $before,
148             after  => $after
149         })
150     });
151 }
152
153 =head3 delete_and_log
154
155     $batch->delete_and_log;
156
157 Log batch delete
158
159 =cut
160
161 sub delete_and_log {
162     my ( $self ) = @_;
163
164     my $logger = Koha::Illrequest::Logger->new;
165
166     $logger->log_something({
167         modulename   => 'ILL',
168         actionname  => 'batch_delete',
169         objectnumber => $self->id,
170         infos        => to_json({})
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;