Bug 31383: (bug 29691 follow-up) Remove get_opac_news_by_id
[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 { error => "Duplicate status found" };
52     }
53
54     # Ensure system statuses can't be created
55     $self->set(
56         {
57             code      => $fixed_code,
58             is_system => 0
59         }
60     )->store;
61
62     my $logger = Koha::Illrequest::Logger->new;
63
64     $logger->log_something(
65         {
66             modulename   => 'ILL',
67             actionname   => 'batch_status_create',
68             objectnumber => $self->id,
69             infos        => to_json( {} )
70         }
71     );
72 }
73
74 =head3 update_and_log
75
76     $status->update_and_log;
77
78 Log batch status update following storage
79
80 =cut
81
82 sub update_and_log {
83     my ( $self, $params ) = @_;
84
85     my $before = { name => $self->name };
86
87     # Ensure only the name can be changed
88     $self->set( { name => $params->{name} } );
89     my $update = $self->store;
90
91     my $after = { name => $self->name };
92
93     my $logger = Koha::Illrequest::Logger->new;
94
95     $logger->log_something(
96         {
97             modulename   => 'ILL',
98             actionname   => 'batch_status_update',
99             objectnumber => $self->id,
100             infos        => to_json(
101                 {
102                     before => $before,
103                     after  => $after
104                 }
105             )
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( { status_code => $self->code } );
128     $affected->update( { status_code => 'UNKNOWN' } );
129
130     my $logger = Koha::Illrequest::Logger->new;
131
132     $logger->log_something(
133         {
134             modulename   => 'ILL',
135             actionname   => 'batch_status_delete',
136             objectnumber => $self->id,
137             infos        => to_json( {} )
138         }
139     );
140
141     $self->delete;
142 }
143
144 =head2 Internal methods
145
146 =head3 _type
147
148     my $type = Koha::IllbatchStatus->_type;
149
150 Return this object's type
151
152 =cut
153
154 sub _type {
155     return 'IllbatchStatus';
156 }
157
158 =head1 AUTHOR
159
160 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
161
162 =cut
163
164 1;