fix malformed call of XSLTParse4Display
[koha.git] / misc / maintenance / fix_accountlines_date.pl
1 #!/usr/bin/perl
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
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" };
27 }
28
29 use C4::Context;
30 use C4::Dates;
31 use Getopt::Long;
32 use Pod::Usage;
33
34 =head1 NAME
35
36 fix_accountlines_date.pl - Fix date code in the description of fines
37
38 =head1 SYNOPSIS
39
40 fix_accountlines_date.pl -m date_format [ -n fines_to_process ] [ -d ] [ --help or -h ]
41
42  Options:
43    --help or -h                Brief usage message
44    --man                       Full documentation
45    -n fines_to_process         How many fines to process; if left off will
46                                process all
47    -m date_format              What format the dates are currently in; 'us'
48                                or 'metric' (REQUIRED)
49    -d                          Run in debugging mode
50
51 =head1 DESCRIPTION
52
53 This script fixes the date code in the description of fines. Previously, the
54 format of this was determined by which script you were using to update fines (see the -m option)
55
56 =over 8
57
58 =item B<--help>
59
60 Prints a brief usage message and exits.
61
62 =item B<--man>
63
64 Prints a full manual page and exits.
65
66 =item B<-n>
67
68 Process only a certain amount of fines. If this option is left off, this script
69 will process everything.
70
71 =item B<-m>
72
73 This required option tells the script what format your dates are currently in.
74 If you were previously using the fines2.pl or fines-sanop.pl script to update 
75 your fines, they will be in 'metric' format. If you were using the fines-ll.pl
76 script, they will be in 'us' format. After this script is finished, they will
77 be in whatever format your 'dateformat' system preference specifies.
78
79 =item B<-d>
80
81 Run in debugging mode; this prints out a lot of information and should be used
82 only if there is a problem and with the '-n' option.
83
84 =cut
85
86 my $mode = '';
87 my $want_help = 0;
88 my $limit = -1;
89 my $done = 0;
90 my $DEBUG = 0;
91
92 # Regexes for the two date formats
93 our $US_DATE = '((0\d|1[0-2])\/([0-2]\d|3[01])\/(\d{4}))';
94 our $METRIC_DATE = '(([0-2]\d|3[01])\/(0\d|1[0-2])\/(\d{4}))';
95
96 sub print_usage {
97     print <<_USAGE_
98 $0: Fix the date code in the description of fines
99
100 Due to the multiple scripts used to update fines in earlier versions of Koha,
101 this script should be used to change the format of the date codes in the
102 accountlines table before you start using Koha 3.0.
103
104 Parameters:
105   --mode or -m        This should be 'us' or 'metric', and tells the script
106                       what format your old dates are in.
107   --debug or -d       Run this script in debug mode.
108   --limit or -n       How many accountlines rows to fix; useful for testing.
109   --help or -h        Print out this help message.
110 _USAGE_
111 }
112
113 my $result = GetOptions(
114     'm=s' => \$mode,
115     'd'  => \$DEBUG,
116     'n=i'  => \$limit, 
117     'help|h'   => \$want_help,
118 );
119
120 if (not $result or $want_help or ($mode ne 'us' and $mode ne 'metric')) {
121     print_usage();
122     exit 0;
123 }
124
125 our $dbh = C4::Context->dbh;
126 $dbh->{AutoCommit} = 0;
127 my $sth = $dbh->prepare("
128 SELECT borrowernumber, itemnumber, accountno, description
129   FROM accountlines
130   WHERE accounttype in ('FU', 'F', 'O', 'M')
131 ;");
132 $sth->execute();
133
134 my $update_sth = $dbh->prepare('
135 UPDATE accountlines
136   SET description = ?
137   WHERE borrowernumber = ? AND itemnumber = ? AND accountno = ?
138 ;');
139
140
141 while (my $accountline = $sth->fetchrow_hashref) {
142     my $description = $accountline->{'description'};
143     my $updated = 0;
144
145     if ($mode eq 'us') {
146         if ($description =~ /$US_DATE/) { # mm/dd/yyyy
147             my $date = C4::Dates->new($1, 'us');
148             print "Converting $1 (us) to " . $date->output() . "\n" if $DEBUG;
149             $description =~ s/$US_DATE/$date->output()/;
150             $updated = 1;
151         }
152     } elsif ($mode eq 'metric') {
153         if ($description =~ /$METRIC_DATE/) { # dd/mm/yyyy
154             my $date = C4::Dates->new($1, 'metric');
155             print "Converting $1 (metric) to " . $date->output() . "\n" if $DEBUG;
156             $description =~ s/$METRIC_DATE/$date->output()/;
157             $updated = 2;
158         }
159     }
160
161     print "Changing description from '" . $accountline->{'description'} . "' to '" . $description . "'\n" if $DEBUG;
162     $update_sth->execute($description, $accountline->{'borrowernumber'}, $accountline->{'itemnumber'}, $accountline->{'accountno'});
163
164     $done++;
165
166     last if ($done == $limit); # $done can't be -1, so this works
167 }
168
169 $dbh->commit();