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