Bug 25650: Add location and itype descriptions in ILS-DI GetRecords
[koha.git] / misc / cronjobs / archive_purchase_suggestions.pl
blobbd16600d03c158625af3e15bbca2a6df50f177da
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Pod::Usage;
6 use Getopt::Long;
8 use Koha::Script -cron;
10 use Koha::DateUtils qw( dt_from_string output_pref );
11 use Koha::Suggestions;
12 use C4::Koha;
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 );
24 if ($help) {
25 pod2usage( verbose => 2 );
28 unless ( $age or @statuses ) {
29 pod2usage(q{At least --age or --status must be provided});
30 exit;
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;
39 my $params = { archived => 0 };
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;
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 ),
59 exit;
62 $params->{STATUS} = { -in => \@statuses } if @statuses;
65 if ($age_date_field) {
66 if ( !grep { $_ eq $age_date_field }
67 qw( suggesteddate manageddate accepteddate rejecteddate lastmodificationdate)
70 pod2usage( sprintf( "The parameter for --age-field (%s) is invalid", $age_date_field ) );
71 exit;
74 else {
75 $age_date_field = 'manageddate';
78 my $date = dt_from_string;
79 if ($age) {
80 if ( $age =~ m|^(\d)$| || $age =~ m|^days:(\d+)$| ) {
81 $date->subtract( days => $1 );
83 elsif ( $age =~ m|^hours:(\d+)$| ) {
84 $date->subtract( hours => $1 );
86 elsif ( $age =~ m|^weeks:(\d+)$| ) {
87 $date->subtract( weeks => $1 );
89 elsif ( $age =~ m|^months:(\d+)$| ) {
90 $date->subtract( months => $1 );
92 elsif ( $age =~ m|^years:(\d+)$| ) {
93 $date->subtract( years => $1 );
95 else {
96 pod2usage( sprintf( "The parameter for --age (%s) is invalid", $age ) );
97 exit;
99 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
100 $params->{$age_date_field} = { '<=' => $dtf->format_date($date) };
102 my $suggestions = Koha::Suggestions->search($params);
103 say sprintf( "Found %d suggestions", $suggestions->count )
105 exists $params->{$age_date_field} ? sprintf( " with %s older than %s",
106 $age_date_field, output_pref( { dt => $date, dateonly => 1 } ) )
107 : ""
110 exists $params->{status}
111 ? sprintf( " and one of the following statuses: %s",
112 join( ', ', @available_statuses ) )
113 : ""
114 ) if $verbose;
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 } );
122 else {
123 say sprintf( "Suggestion %s would have been archived",
124 $suggestion->suggestionid );
128 =head1 NAME
130 archive_purchase_suggestions.pl - Archive purchase suggestions given their age and status
132 =head1 SYNOPSIS
134 archive_purchase_suggestions.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--age=AGE] [--age-date-field=DATE_FIELD] [--status=STATUS]
136 =head1 OPTIONS
138 =over
140 =item B<-h|--help>
142 Print a brief help message
144 =item B<-c|--confirm>
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.
150 =item B<--age>
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.
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.
156 =item B<--age-date-field>
158 You can specify one of the date fields of suggestions: suggesteddate, manageddate, accepteddate, rejecteddate or lastmodificationdate. Default is manageddate.
160 =item B<--status>
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.
164 Can be passed several times.
166 =item B<-v|--verbose>
168 Verbose mode.
170 =back
172 =head1 AUTHOR
174 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
176 =head1 LICENSE
178 This file is part of Koha.
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.
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.
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>.
193 =cut