Bug 23712: Silence warns from Koha/Illrequest/Logger.pm
[koha.git] / misc / maintenance / fix_accountlines_date.pl
blob0c7a92e1788776babea0bfa59b609dba4f4d2f75
1 #!/usr/bin/perl
3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
22 BEGIN {
23 # find Koha's Perl modules
24 # test carefully before changing this
25 use FindBin;
26 eval { require "$FindBin::Bin/../kohalib.pl" };
29 use Koha::Script;
30 use C4::Context;
31 use Getopt::Long;
32 use Pod::Usage;
33 use Koha::DateUtils;
35 =head1 NAME
37 fix_accountlines_date.pl - Fix date code in the description of fines
39 =head1 SYNOPSIS
41 fix_accountlines_date.pl -m date_format [ -n fines_to_process ] [ -d ] [ --help or -h ]
43 Options:
44 --help or -h Brief usage message
45 --man Full documentation
46 -n fines_to_process How many fines to process; if left off will
47 process all
48 -m date_format What format the dates are currently in; 'us'
49 or 'metric' (REQUIRED)
50 -d Run in debugging mode
52 =head1 DESCRIPTION
54 This script fixes the date code in the description of fines. Previously, the
55 format of this was determined by which script you were using to update fines (see the -m option)
57 =over 8
59 =item B<--help>
61 Prints a brief usage message and exits.
63 =item B<--man>
65 Prints a full manual page and exits.
67 =item B<-n>
69 Process only a certain amount of fines. If this option is left off, this script
70 will process everything.
72 =item B<-m>
74 This required option tells the script what format your dates are currently in.
75 If you were previously using the fines2.pl or fines-sanop.pl script to update
76 your fines, they will be in 'metric' format. If you were using the fines-ll.pl
77 script, they will be in 'us' format. After this script is finished, they will
78 be in whatever format your 'dateformat' system preference specifies.
80 =item B<-d>
82 Run in debugging mode; this prints out a lot of information and should be used
83 only if there is a problem and with the '-n' option.
85 =back
87 =cut
89 my $mode = '';
90 my $want_help = 0;
91 my $limit = -1;
92 my $done = 0;
93 my $DEBUG = 0;
95 # Regexes for the two date formats
96 our $US_DATE = '((0\d|1[0-2])\/([0-2]\d|3[01])\/(\d{4}))';
97 our $METRIC_DATE = '(([0-2]\d|3[01])\/(0\d|1[0-2])\/(\d{4}))';
99 sub print_usage {
100 print <<_USAGE_
101 $0: Fix the date code in the description of fines
103 Due to the multiple scripts used to update fines in earlier versions of Koha,
104 this script should be used to change the format of the date codes in the
105 accountlines table before you start using Koha 3.0.
107 Parameters:
108 --mode or -m This should be 'us' or 'metric', and tells the script
109 what format your old dates are in.
110 --debug or -d Run this script in debug mode.
111 --limit or -n How many accountlines rows to fix; useful for testing.
112 --help or -h Print out this help message.
113 _USAGE_
116 my $result = GetOptions(
117 'm=s' => \$mode,
118 'd' => \$DEBUG,
119 'n=i' => \$limit,
120 'help|h' => \$want_help,
123 if (not $result or $want_help or ($mode ne 'us' and $mode ne 'metric')) {
124 print_usage();
125 exit 0;
128 our $dbh = C4::Context->dbh;
129 $dbh->{AutoCommit} = 0;
130 my $sth = $dbh->prepare("
131 SELECT accountlines_id, description
132 FROM accountlines
133 WHERE accounttype in ('FU', 'F', 'M')
134 ;");
135 $sth->execute();
137 my $update_sth = $dbh->prepare('
138 UPDATE accountlines
139 SET description = ?
140 WHERE accountlines_id = ?
141 ;');
144 while (my $accountline = $sth->fetchrow_hashref) {
145 my $description = $accountline->{'description'};
146 my $updated = 0;
148 if ($mode eq 'us') {
149 if ($description =~ /$US_DATE/) { # mm/dd/yyyy
150 my $date = output_pref( { dt => dt_from_string( $1, 'us' ), dateonly => 1} );
151 print "Converting $1 (us) to " . $date . "\n" if $DEBUG;i
152 $description =~ s/$US_DATE/$date/;
153 $updated = 1;
155 } elsif ($mode eq 'metric') {
156 if ($description =~ /$METRIC_DATE/) { # dd/mm/yyyy
157 my $date = output_pref( { dt => dt_from_string( $1, 'metric' ), dateonly => 1} );
158 print "Converting $1 (metric) to " . $date . "\n" if $DEBUG;
159 $description =~ s/$METRIC_DATE/$date/;
160 $updated = 2;
164 print "Changing description from '" . $accountline->{'description'} . "' to '" . $description . "'\n" if $DEBUG;
165 $update_sth->execute($description, $accountline->{'accountlines_id'});
167 $done++;
169 last if ($done == $limit); # $done can't be -1, so this works
172 $dbh->commit();