Bug 30719: ILL Batch Statuses
[koha.git] / Koha / IllbatchStatus.pm
1 package Koha::IllbatchStatus;
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::Illbatch;
24 use JSON qw( to_json );
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::IllbatchStatus - Koha IllbatchStatus Object class
30
31 =head2 Class methods
32
33 =head3 create_and_log
34
35     $status->create_and_log;
36
37 Log batch status creation following storage
38
39 =cut
40
41 sub create_and_log {
42     my ( $self ) = @_;
43
44     # Ensure code is uppercase and contains only word characters
45     my $fixed_code = uc $self->code;
46     $fixed_code =~ s/\W/_/;
47
48     # Ensure this status doesn't already exist
49     my $status = Koha::IllbatchStatuses->find({ code => $fixed_code });
50     if ($status) {
51         return {
52             error => "Duplicate status found"
53         };
54     }
55
56     # Ensure system statuses can't be created
57     $self->set({
58         code      => $fixed_code,
59         is_system => 0
60     })->store;
61
62     my $logger = Koha::Illrequest::Logger->new;
63
64     $logger->log_something({
65         modulename   => 'ILL',
66         actionname   => 'batch_status_create',
67         objectnumber => $self->id,
68         infos        => to_json({})
69     });
70 }
71
72 =head3 update_and_log
73
74     $status->update_and_log;
75
76 Log batch status update following storage
77
78 =cut
79
80 sub update_and_log {
81     my ( $self, $params ) = @_;
82
83     my $before = {
84         name => $self->name
85     };
86
87     # Ensure only the name can be changed
88     $self->set({
89         name => $params->{name}
90     });
91     my $update = $self->store;
92
93     my $after = {
94         name => $self->name
95     };
96
97     my $logger = Koha::Illrequest::Logger->new;
98
99     $logger->log_something({
100         modulename   => 'ILL',
101         actionname  => 'batch_status_update',
102         objectnumber => $self->id,
103         infos        => to_json({
104             before => $before,
105             after  => $after
106         })
107     });
108 }
109
110 =head3 delete_and_log
111
112     $batch->delete_and_log;
113
114 Log batch status delete
115
116 =cut
117
118 sub delete_and_log {
119     my ( $self ) = @_;
120
121     # Don't permit deletion of system statuses
122     if ($self->is_system) {
123         return;
124     }
125
126     # Update all batches that use this status to have status UNKNOWN
127     my $affected = Koha::Illbatches->search({ statuscode => $self->code });
128     $affected->update({ statuscode => 'UNKNOWN'});
129
130     my $logger = Koha::Illrequest::Logger->new;
131
132     $logger->log_something({
133         modulename   => 'ILL',
134         actionname   => 'batch_status_delete',
135         objectnumber => $self->id,
136         infos        => to_json({})
137     });
138
139     $self->delete;
140 }
141
142 =head2 Internal methods
143
144 =head3 _type
145
146     my $type = Koha::IllbatchStatus->_type;
147
148 Return this object's type
149
150 =cut
151
152 sub _type {
153     return 'IllbatchStatus';
154 }
155
156 =head1 AUTHOR
157
158 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
159
160 =cut
161
162 1;