Bug 31203: Alter other cronjobs that currenlty use cronlogaction
[koha.git] / misc / cronjobs / cart_to_shelf.pl
1 #!/usr/bin/perl
2 #-----------------------------------
3 # Copyright 2009 PTFS Inc.
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
21 =head1 NAME
22
23 cart_to_shelf.pl  cron script to set items with location of CART to original shelving location after X hours.
24                   Execute without options for help.
25
26 =cut
27
28 use strict;
29 use warnings;
30
31 use Koha::Script -cron;
32 use C4::Items qw( CartToShelf );
33 use C4::Log qw( cronlogaction );
34
35 use C4::Context;
36 use Getopt::Long qw( GetOptions );
37
38 my $hours = 0;
39
40 my $command_line_options = join(" ",@ARGV);
41
42 GetOptions( 'h|hours=s' => \$hours, );
43
44 my $usage = << 'ENDUSAGE';
45 cart_to_shelf.pl: This cron script will set any item of the location CART ( Shelving Cart ) to it's original shelving location
46                  after the given numer of hours has passed.
47
48 This script takes the following parameters :
49
50     --hours | -h         The number of hours that need to pass before the location is updated.
51
52   examples :
53   {Koha script directory}/cronjobs/cart_to_shelf.pl --hours 24
54     Would make any item that has a location of CART for more than 24 hours change to it's original shelving location.
55
56 ENDUSAGE
57
58 unless ($hours) {
59     print $usage;
60     die "ERROR: No --hours (-h) option defined";
61 }
62
63 cronlogaction({ info => $command_line_options });
64
65 my $query = "SELECT itemnumber FROM items WHERE location = 'CART' AND TIMESTAMPDIFF(HOUR, items.timestamp, NOW() ) > ?";
66 my $sth = C4::Context->dbh->prepare($query);
67 $sth->execute($hours);
68 while (my ($itemnumber) = $sth->fetchrow_array) {
69     CartToShelf($itemnumber);
70 }
71
72 cronlogaction({ action => 'End', info => "COMPLETED" });