View | Details | Raw Unified | Return to bug 22784
Collapse All | Expand All

(-)a/misc/cronjobs/archive_purchase_suggestions.pl (-1 / +193 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Pod::Usage;
6
use Getopt::Long;
7
8
use Koha::Script -cron;
9
10
use Koha::DateUtils qw( dt_from_string output_pref );
11
use Koha::Suggestions;
12
use C4::Koha;
13
14
my ( $help, $verbose, $confirm, $age, $age_date_field, @statuses );
15
GetOptions(
16
    'h|help'           => \$help,
17
    'v|verbose'        => \$verbose,
18
    'age:s'            => \$age,
19
    'age-date-field:s' => \$age_date_field,
20
    'status:s'         => \@statuses,
21
    'c|confirm'        => \$confirm,
22
) || pod2usage( verbose => 2 );
23
24
if ($help) {
25
    pod2usage( verbose => 2 );
26
}
27
28
unless ( $age or @statuses ) {
29
    pod2usage(q{At least --age or --status must be provided});
30
    exit;
31
}
32
33
unless ($confirm) {
34
    say "Doing a dry run; no suggestion will be modified.";
35
    say "Run again with --confirm to modify suggestions.";
36
    $verbose = 1 unless $verbose;
37
}
38
39
my $params = { archived => 0 };
40
41
my @available_statuses;
42
if (@statuses) {
43
    @available_statuses = map { $_->{authorised_value} } @{ GetAuthorisedValues('SUGGEST_STATUS') };
44
    push @available_statuses, qw( ASKED ACCEPTED CHECKED REJECTED ORDERED AVAILABLE );
45
    my @unknown_statuses;
46
    for my $status (@statuses) {
47
        push @unknown_statuses, $status
48
          if !grep { $_ eq $status } @available_statuses;
49
    }
50
    if (@unknown_statuses) {
51
        pod2usage(
52
            sprintf(
53
                "%s (%s)\nValid statuses are: %s",
54
                'Invalid status ',
55
                join( ', ', @unknown_statuses ),
56
                join( ', ', @available_statuses ),
57
            )
58
        );
59
        exit;
60
    }
61
62
    $params->{STATUS} = { -in => \@statuses } if @statuses;
63
}
64
65
if ($age_date_field) {
66
    if ( !grep { $_ eq $age_date_field }
67
        qw( suggesteddate manageddate accepteddate rejecteddate lastmodificationdate)
68
      )
69
    {
70
        pod2usage( sprintf( "The parameter for --age-field (%s) is invalid", $age_date_field ) );
71
        exit;
72
    }
73
}
74
else {
75
    $age_date_field = 'manageddate';
76
}
77
78
my $date = dt_from_string;
79
if ($age) {
80
    if ( $age =~ m|^(\d)$| || $age =~ m|^days:(\d+)$| ) {
81
        $date->subtract( days => $1 );
82
    }
83
    elsif ( $age =~ m|^hours:(\d+)$| ) {
84
        $date->subtract( hours => $1 );
85
    }
86
    elsif ( $age =~ m|^weeks:(\d+)$| ) {
87
        $date->subtract( weeks => $1 );
88
    }
89
    elsif ( $age =~ m|^months:(\d+)$| ) {
90
        $date->subtract( months => $1 );
91
    }
92
    elsif ( $age =~ m|^years:(\d+)$| ) {
93
        $date->subtract( years => $1 );
94
    }
95
    else {
96
        pod2usage( sprintf( "The parameter for --age (%s) is invalid", $age ) );
97
        exit;
98
    }
99
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
100
    $params->{$age_date_field} = { '<=' => $dtf->format_date($date) };
101
}
102
my $suggestions = Koha::Suggestions->search($params);
103
say sprintf( "Found %d suggestions", $suggestions->count )
104
  . (
105
    exists $params->{$age_date_field} ? sprintf( " with %s older than %s",
106
        $age_date_field, output_pref( { dt => $date, dateonly => 1 } ) )
107
    : ""
108
  )
109
  . (
110
    exists $params->{status}
111
    ? sprintf( " and one of the following statuses: %s",
112
        join( ', ', @available_statuses ) )
113
    : ""
114
  ) if $verbose;
115
116
while ( my $suggestion = $suggestions->next ) {
117
    if ($confirm) {
118
        say sprintf( "Archiving suggestion %s", $suggestion->suggestionid )
119
          if $verbose;
120
        $suggestion->update( { archived => 1 } );
121
    }
122
    else {
123
        say sprintf( "Suggestion %s would have been archived",
124
            $suggestion->suggestionid );
125
    }
126
}
127
128
=head1 NAME
129
130
archive_purchase_suggestions.pl - Archive purchase suggestions given their age and status
131
132
=head1 SYNOPSIS
133
134
archive_purchase_suggestions.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--age=AGE] [--age-date-field=DATE_FIELD] [--status=STATUS]
135
136
=head1 OPTIONS
137
138
=over
139
140
=item B<-h|--help>
141
142
Print a brief help message
143
144
=item B<-c|--confirm>
145
146
This flag must be provided in order for the script to actually
147
archive purchase suggestions.  If it is not supplied, the script will
148
only report on the suggestions it would have archived.
149
150
=item B<--age>
151
152
It must contain an integer representing the number of days elapsed since the suggestions has been modified. You can use it along with B<--age-date-field> to specify the database column you want to apply this number.
153
154
You can also provide a number of hours, days, weeks, months or years. Like --age=months:1 to archive purchase suggestions older than a month.
155
156
=item B<--age-date-field>
157
158
You can specify one of the date fields of suggestions: suggesteddate, manageddate, accepteddate, rejecteddate or lastmodificationdate. Default is manageddate.
159
160
=item B<--status>
161
162
It must be one of the 6 default statuses (ASKED, ACCEPTED, CHECKED, REJECTED, ORDERED or AVAILABLE), or one define in the SUGGEST_STATUS authorized value's category.
163
164
Can be passed several times.
165
166
=item B<-v|--verbose>
167
168
Verbose mode.
169
170
=back
171
172
=head1 AUTHOR
173
174
Jonathan Druart <jonathan.druart@bugs.koha-community.org>
175
176
=head1 LICENSE
177
178
This file is part of Koha.
179
180
Koha is free software; you can redistribute it and/or modify it
181
under the terms of the GNU General Public License as published by
182
the Free Software Foundation; either version 3 of the License, or
183
(at your option) any later version.
184
185
Koha is distributed in the hope that it will be useful, but
186
WITHOUT ANY WARRANTY; without even the implied warranty of
187
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
188
GNU General Public License for more details.
189
190
You should have received a copy of the GNU General Public License
191
along with Koha; if not, see <http://www.gnu.org/licenses>.
192
193
=cut

Return to bug 22784