new script to fix date code in accountlines
[koha.git] / misc / maintenance / fix_accountlines_date.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 BEGIN {
22     # find Koha's Perl modules
23     # test carefully before changing this
24     use FindBin;
25     eval { require "$FindBin::Bin/../kohalib.pl" };
26 }
27
28 use C4::Context;
29 use C4::Dates;
30 use Getopt::Long;
31 use Pod::Usage;
32
33 =head1 NAME
34
35 fix_accountlines_date.pl - Fix date code in the description of fines
36
37 =head1 SYNOPSIS
38
39 fix_accountlines_date.pl -m date_format [ -n fines_to_process ] [ -d ] [ --help or -h ]
40
41  Options:
42    --help or -h                Brief usage message
43    --man                       Full documentation
44    -n fines_to_process         How many fines to process; if left off will
45                                process all
46    -m date_format              What format the dates are currently in; 'us'
47                                or 'metric' (REQUIRED)
48    -d                          Run in debugging mode
49
50 =head1 DESCRIPTION
51
52 This script fixes the date code in the description of fines. Previously, the
53 format of this was determined by which script you were using to update fines (see the -m option)
54
55 =over 8
56
57 =item B<--help>
58
59 Prints a brief usage message and exits.
60
61 =item B<--man>
62
63 Prints a full manual page and exits.
64
65 =item B<-n>
66
67 Process only a certain amount of fines. If this option is left off, this script
68 will process everything.
69
70 =item B<-m>
71
72 This required option tells the script what format your dates are currently in.
73 If you were previously using the fines2.pl or fines-sanop.pl script to update 
74 your fines, they will be in 'metric' format. If you were using the fines-ll.pl
75 script, they will be in 'us' format. After this script is finished, they will
76 be in whatever format your 'dateformat' system preference specifies.
77
78 =item B<-d>
79
80 Run in debugging mode; this prints out a lot of information and should be used
81 only if there is a problem and with the '-n' option.
82
83 =cut
84
85 my $mode = '';
86 my $want_help = 0;
87 my $limit = -1;
88 my $done = 0;
89 my $DEBUG = 0;
90
91 # Regexes for the two date formats
92 our $US_DATE = '((0\d|1[0-2])\/([0-2]\d|3[01])\/(\d{4}))';
93 our $METRIC_DATE = '(([0-2]\d|3[01])\/(0\d|1[0-2])\/(\d{4}))';
94
95 sub print_usage {
96     print <<_USAGE_
97 $0: Fix the date code in the description of fines
98
99 Due to the multiple scripts used to update fines in earlier versions of Koha,
100 this script should be used to change the format of the date codes in the
101 accountlines table before you start using Koha 3.0.
102
103 Parameters:
104   --mode or -m        This should be 'us' or 'metric', and tells the script
105                       what format your old dates are in.
106   --debug or -d       Run this script in debug mode.
107   --limit or -n       How many accountlines rows to fix; useful for testing.
108   --help or -h        Print out this help message.
109 _USAGE_
110 }
111
112 my $result = GetOptions(
113     'm=s' => \$mode,
114     'd'  => \$DEBUG,
115     'n=i'  => \$limit, 
116     'help|h'   => \$want_help,
117 );
118
119 if (not $result or $want_help or ($mode ne 'us' and $mode ne 'metric')) {
120     print_usage();
121     exit 0;
122 }
123
124 our $dbh = C4::Context->dbh;
125 $dbh->{AutoCommit} = 0;
126 my $sth = $dbh->prepare("
127 SELECT borrowernumber, itemnumber, accountno, description
128   FROM accountlines
129   WHERE accounttype in ('FU', 'F', 'O', 'M')
130 ;");
131 $sth->execute();
132
133 my $update_sth = $dbh->prepare('
134 UPDATE accountlines
135   SET description = ?
136   WHERE borrowernumber = ? AND itemnumber = ? AND accountno = ?
137 ;');
138
139
140 while (my $accountline = $sth->fetchrow_hashref) {
141     my $description = $accountline->{'description'};
142     my $updated = 0;
143
144     if ($mode eq 'us') {
145         if ($description =~ /$US_DATE/) { # mm/dd/yyyy
146             my $date = C4::Dates->new($1, 'us');
147             print "Converting $1 (us) to " . $date->output() . "\n" if $DEBUG;
148             $description =~ s/$US_DATE/$date->output()/;
149             $updated = 1;
150         }
151     } elsif ($mode eq 'metric') {
152         if ($description =~ /$METRIC_DATE/) { # dd/mm/yyyy
153             my $date = C4::Dates->new($1, 'metric');
154             print "Converting $1 (metric) to " . $date->output() . "\n" if $DEBUG;
155             $description =~ s/$METRIC_DATE/$date->output()/;
156             $updated = 2;
157         }
158     }
159
160     print "Changing description from '" . $accountline->{'description'} . "' to '" . $description . "'\n" if $DEBUG;
161     $update_sth->execute($description, $accountline->{'borrowernumber'}, $accountline->{'itemnumber'}, $accountline->{'accountno'});
162
163     $done++;
164
165     last if ($done == $limit); # $done can't be -1, so this works
166 }
167
168 $dbh->commit();